From 75e6af529ad4b19915a882c72348e1dd5daae748 Mon Sep 17 00:00:00 2001 From: pml68 Date: Tue, 13 May 2025 00:51:32 +0200 Subject: feat: add example code to documentation and README --- README.md | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++-- example/src/main.rs | 2 +- src/lib.rs | 64 +++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 129 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 0f1c11c..668abfb 100644 --- a/README.md +++ b/README.md @@ -5,9 +5,72 @@ Custom dialog for [`iced`](https://iced.rs) It's mostly the dialog from @frgp42's [Fluent Iced Gallery](https://github.com/frgp42/fluent_iced_gallery), but made into a "widget" ## Example -See the [/example](/example) directory. -You can run it like this: +```rust +use iced::{ + Element, Length, Task, + widget::{button, center, column, text}, +}; +use iced_dialog::dialog; + +#[derive(Default)] +struct State { + is_open: bool, + action_text: String, +} + +#[derive(Debug, Clone)] +enum Message { + OpenDialog, + Saved, + Cancelled, +} + +fn main() -> iced::Result { + iced::run("Dialog Example", State::update, State::view) +} + +impl State { + fn update(&mut self, message: Message) -> Task { + match message { + Message::OpenDialog => self.is_open = true, + Message::Saved => { + self.action_text = "User saved their work".to_owned(); + self.is_open = false; + } + Message::Cancelled => { + self.action_text = "User cancelled the dialog".to_owned(); + self.is_open = false; + } + } + Task::none() + } + + fn view(&self) -> Element<'_, Message> { + let base = center( + column![ + text(&self.action_text), + button("Open Dialog").on_press(Message::OpenDialog) + ] + .spacing(14.0), + ) + .width(Length::Fill) + .height(Length::Fill); + + let dialog_content = text("Do you want to save?"); + + dialog(self.is_open, base, dialog_content) + .title("Save") + .push_button(iced_dialog::button("Save", Message::Saved)) + .push_button(iced_dialog::button("Cancel", Message::Cancelled)) + .width(350) + .height(234) + .into() + } +} +``` + +You can also run the above example: ```bash cargo run -p example ``` diff --git a/example/src/main.rs b/example/src/main.rs index 053a738..52903c8 100644 --- a/example/src/main.rs +++ b/example/src/main.rs @@ -1,5 +1,5 @@ use iced::{ - Element, Length, Task, color, + Element, Length, Task, widget::{button, center, column, text}, }; use iced_dialog::dialog; diff --git a/src/lib.rs b/src/lib.rs index 3fba6f2..1e536a4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,69 @@ //! Custom dialog for `iced` //! //! # Example -//! See [here](https://github.com/pml68/iced_dialog/tree/0.13/example) +//! ```no_run +//! use iced::{ +//! Element, Length, Task, +//! widget::{button, center, column, text}, +//! }; +//! use iced_dialog::dialog; +//! +//! #[derive(Default)] +//! struct State { +//! is_open: bool, +//! action_text: String, +//! } +//! +//! #[derive(Debug, Clone)] +//! enum Message { +//! OpenDialog, +//! Saved, +//! Cancelled, +//! } +//! +//! fn main() -> iced::Result { +//! iced::run("Dialog Example", State::update, State::view) +//! } +//! +//! impl State { +//! fn update(&mut self, message: Message) -> Task { +//! match message { +//! Message::OpenDialog => self.is_open = true, +//! Message::Saved => { +//! self.action_text = "User saved their work".to_owned(); +//! self.is_open = false; +//! } +//! Message::Cancelled => { +//! self.action_text = "User cancelled the dialog".to_owned(); +//! self.is_open = false; +//! } +//! } +//! Task::none() +//! } +//! +//! fn view(&self) -> Element<'_, Message> { +//! let base = center( +//! column![ +//! text(&self.action_text), +//! button("Open Dialog").on_press(Message::OpenDialog) +//! ] +//! .spacing(14.0), +//! ) +//! .width(Length::Fill) +//! .height(Length::Fill); +//! +//! let dialog_content = text("Do you want to save?"); +//! +//! dialog(self.is_open, base, dialog_content) +//! .title("Save") +//! .push_button(iced_dialog::button("Save", Message::Saved)) +//! .push_button(iced_dialog::button("Cancel", Message::Cancelled)) +//! .width(350) +//! .height(234) +//! .into() +//! } +//! } +//! ``` pub mod dialog; pub use dialog::Dialog; use iced_core as core; -- cgit v1.2.3