iced_dialog/lib.rs
1//! Custom dialog for `iced`
2//!
3//! # Example
4//! ```no_run
5//! use iced::{
6//! Element, Length, Task,
7//! widget::{button, center, column, text},
8//! };
9//! use iced_dialog::dialog;
10//!
11//! #[derive(Default)]
12//! struct State {
13//! is_open: bool,
14//! action_text: String,
15//! }
16//!
17//! #[derive(Debug, Clone)]
18//! enum Message {
19//! OpenDialog,
20//! Saved,
21//! Cancelled,
22//! }
23//!
24//! fn main() -> iced::Result {
25//! iced::run(State::update, State::view)
26//! }
27//!
28//! impl State {
29//! fn update(&mut self, message: Message) -> Task<Message> {
30//! match message {
31//! Message::OpenDialog => self.is_open = true,
32//! Message::Saved => {
33//! self.action_text = "User saved their work".to_owned();
34//! self.is_open = false;
35//! }
36//! Message::Cancelled => {
37//! self.action_text = "User cancelled the dialog".to_owned();
38//! self.is_open = false;
39//! }
40//! }
41//! Task::none()
42//! }
43//!
44//! fn view(&self) -> Element<'_, Message> {
45//! let base = center(
46//! column![
47//! text(&self.action_text),
48//! button("Open Dialog").on_press(Message::OpenDialog)
49//! ]
50//! .spacing(14.0),
51//! )
52//! .width(Length::Fill)
53//! .height(Length::Fill);
54//!
55//! let dialog_content = text("Do you want to save?");
56//!
57//! dialog(self.is_open, base, dialog_content)
58//! .title("Save")
59//! .push_button(iced_dialog::button("Save", Message::Saved))
60//! .push_button(iced_dialog::button("Cancel", Message::Cancelled))
61//! .width(350)
62//! .height(234)
63//! .into()
64//! }
65//! }
66//! ```
67pub mod dialog;
68pub use dialog::Dialog;
69use iced_core as core;
70use iced_core::alignment::Horizontal;
71use iced_widget::Button;
72use iced_widget::{container, text};
73
74/// Creates a new [`Dialog`] with the given base and dialog content.
75pub fn dialog<'a, Message, Theme, Renderer>(
76 is_open: bool,
77 base: impl Into<core::Element<'a, Message, Theme, Renderer>>,
78 content: impl Into<core::Element<'a, Message, Theme, Renderer>>,
79) -> Dialog<'a, Message, Theme, Renderer>
80where
81 Renderer: 'a + core::Renderer + core::text::Renderer,
82 Theme: 'a + dialog::Catalog,
83 Message: 'a + Clone,
84 <Theme as container::Catalog>::Class<'a>:
85 From<container::StyleFn<'a, Theme>>,
86{
87 Dialog::new(is_open, base, content)
88}
89
90/// Pre-styled [`Button`] for [`Dialog`]s.
91///
92/// [`Button`]: https://docs.iced.rs/iced/widget/struct.Button.html
93pub fn button<'a, Message, Theme, Renderer>(
94 content: &'a str,
95 message: Message,
96) -> Button<'a, Message, Theme, Renderer>
97where
98 Theme: 'a + iced_widget::button::Catalog + text::Catalog,
99 Renderer: 'a + core::Renderer + core::text::Renderer,
100{
101 iced_widget::button(
102 text(content)
103 .size(14)
104 .line_height(text::LineHeight::Absolute(core::Pixels(20.0)))
105 .align_x(Horizontal::Center),
106 )
107 .on_press(message)
108 .height(32)
109 .width(core::Length::Fill)
110}