iced/
lib.rs

1//! iced is a cross-platform GUI library focused on simplicity and type-safety.
2//! Inspired by [Elm].
3//!
4//! [Elm]: https://elm-lang.org/
5//!
6//! # Disclaimer
7//! iced is __experimental__ software. If you expect the documentation to hold your hand
8//! as you learn the ropes, you are in for a frustrating experience.
9//!
10//! The library leverages Rust to its full extent: ownership, borrowing, lifetimes, futures,
11//! streams, first-class functions, trait bounds, closures, and more. This documentation
12//! is not meant to teach you any of these. Far from it, it will assume you have __mastered__
13//! all of them.
14//!
15//! Furthermore—just like Rust—iced is very unforgiving. It will not let you easily cut corners.
16//! The type signatures alone can be used to learn how to use most of the library.
17//! Everything is connected.
18//!
19//! Therefore, iced is easy to learn for __advanced__ Rust programmers; but plenty of patient
20//! beginners have learned it and had a good time with it. Since it leverages a lot of what
21//! Rust has to offer in a type-safe way, it can be a great way to discover Rust itself.
22//!
23//! If you don't like the sound of that, you expect to be spoonfed, or you feel frustrated
24//! and struggle to use the library; then I recommend you to wait patiently until [the book]
25//! is finished.
26//!
27//! [the book]: https://book.iced.rs
28//!
29//! # The Pocket Guide
30//! Start by calling [`run`]:
31//!
32//! ```rust,no_run
33//! pub fn main() -> iced::Result {
34//!     iced::run("A cool counter", update, view)
35//! }
36//! # fn update(state: &mut (), message: ()) {}
37//! # fn view(state: &()) -> iced::Element<()> { iced::widget::text("").into() }
38//! ```
39//!
40//! Define an `update` function to __change__ your state:
41//!
42//! ```rust
43//! fn update(counter: &mut u64, message: Message) {
44//!     match message {
45//!         Message::Increment => *counter += 1,
46//!     }
47//! }
48//! # #[derive(Clone)]
49//! # enum Message { Increment }
50//! ```
51//!
52//! Define a `view` function to __display__ your state:
53//!
54//! ```rust
55//! use iced::widget::{button, text};
56//! use iced::Element;
57//!
58//! fn view(counter: &u64) -> Element<Message> {
59//!     button(text(counter)).on_press(Message::Increment).into()
60//! }
61//! # #[derive(Clone)]
62//! # enum Message { Increment }
63//! ```
64//!
65//! And create a `Message` enum to __connect__ `view` and `update` together:
66//!
67//! ```rust
68//! #[derive(Debug, Clone)]
69//! enum Message {
70//!     Increment,
71//! }
72//! ```
73//!
74//! ## Custom State
75//! You can define your own struct for your state:
76//!
77//! ```rust
78//! #[derive(Default)]
79//! struct Counter {
80//!     value: u64,
81//! }
82//! ```
83//!
84//! But you have to change `update` and `view` accordingly:
85//!
86//! ```rust
87//! # struct Counter { value: u64 }
88//! # #[derive(Clone)]
89//! # enum Message { Increment }
90//! # use iced::widget::{button, text};
91//! # use iced::Element;
92//! fn update(counter: &mut Counter, message: Message) {
93//!     match message {
94//!         Message::Increment => counter.value += 1,
95//!     }
96//! }
97//!
98//! fn view(counter: &Counter) -> Element<Message> {
99//!     button(text(counter.value)).on_press(Message::Increment).into()
100//! }
101//! ```
102//!
103//! ## Widgets and Elements
104//! The `view` function must return an [`Element`]. An [`Element`] is just a generic [`widget`].
105//!
106//! The [`widget`] module contains a bunch of functions to help you build
107//! and use widgets.
108//!
109//! Widgets are configured using the builder pattern:
110//!
111//! ```rust
112//! # struct Counter { value: u64 }
113//! # #[derive(Clone)]
114//! # enum Message { Increment }
115//! use iced::widget::{button, column, text};
116//! use iced::Element;
117//!
118//! fn view(counter: &Counter) -> Element<Message> {
119//!     column![
120//!         text(counter.value).size(20),
121//!         button("Increment").on_press(Message::Increment),
122//!     ]
123//!     .spacing(10)
124//!     .into()
125//! }
126//! ```
127//!
128//! A widget can be turned into an [`Element`] by calling `into`.
129//!
130//! Widgets and elements are generic over the message type they produce. The
131//! [`Element`] returned by `view` must have the same `Message` type as
132//! your `update`.
133//!
134//! ## Layout
135//! There is no unified layout system in iced. Instead, each widget implements
136//! its own layout strategy.
137//!
138//! Building your layout will often consist in using a combination of
139//! [rows], [columns], and [containers]:
140//!
141//! ```rust
142//! # struct State;
143//! # enum Message {}
144//! use iced::widget::{column, container, row};
145//! use iced::{Fill, Element};
146//!
147//! fn view(state: &State) -> Element<Message> {
148//!     container(
149//!         column![
150//!             "Top",
151//!             row!["Left", "Right"].spacing(10),
152//!             "Bottom"
153//!         ]
154//!         .spacing(10)
155//!     )
156//!     .padding(10)
157//!     .center_x(Fill)
158//!     .center_y(Fill)
159//!     .into()
160//! }
161//! ```
162//!
163//! Rows and columns lay out their children horizontally and vertically,
164//! respectively. [Spacing] can be easily added between elements.
165//!
166//! Containers position or align a single widget inside their bounds.
167//!
168//! [rows]: widget::Row
169//! [columns]: widget::Column
170//! [containers]: widget::Container
171//! [Spacing]: widget::Column::spacing
172//!
173//! ## Sizing
174//! The width and height of widgets can generally be defined using a [`Length`].
175//!
176//! - [`Fill`] will make the widget take all the available space in a given axis.
177//! - [`Shrink`] will make the widget use its intrinsic size.
178//!
179//! Most widgets use a [`Shrink`] sizing strategy by default, but will inherit
180//! a [`Fill`] strategy from their children.
181//!
182//! A fixed numeric [`Length`] in [`Pixels`] can also be used:
183//!
184//! ```rust
185//! # struct State;
186//! # enum Message {}
187//! use iced::widget::container;
188//! use iced::Element;
189//!
190//! fn view(state: &State) -> Element<Message> {
191//!     container("I am 300px tall!").height(300).into()
192//! }
193//! ```
194//!
195//! ## Theming
196//! The default [`Theme`] of an application can be changed by defining a `theme`
197//! function and leveraging the [`Application`] builder, instead of directly
198//! calling [`run`]:
199//!
200//! ```rust,no_run
201//! # #[derive(Default)]
202//! # struct State;
203//! use iced::Theme;
204//!
205//! pub fn main() -> iced::Result {
206//!     iced::application("A cool application", update, view)
207//!         .theme(theme)
208//!         .run()
209//! }
210//!
211//! fn theme(state: &State) -> Theme {
212//!     Theme::TokyoNight
213//! }
214//! # fn update(state: &mut State, message: ()) {}
215//! # fn view(state: &State) -> iced::Element<()> { iced::widget::text("").into() }
216//! ```
217//!
218//! The `theme` function takes the current state of the application, allowing the
219//! returned [`Theme`] to be completely dynamic—just like `view`.
220//!
221//! There are a bunch of built-in [`Theme`] variants at your disposal, but you can
222//! also [create your own](Theme::custom).
223//!
224//! ## Styling
225//! As with layout, iced does not have a unified styling system. However, all
226//! of the built-in widgets follow the same styling approach.
227//!
228//! The appearance of a widget can be changed by calling its `style` method:
229//!
230//! ```rust
231//! # struct State;
232//! # enum Message {}
233//! use iced::widget::container;
234//! use iced::Element;
235//!
236//! fn view(state: &State) -> Element<Message> {
237//!     container("I am a rounded box!").style(container::rounded_box).into()
238//! }
239//! ```
240//!
241//! The `style` method of a widget takes a closure that, given the current active
242//! [`Theme`], returns the widget style:
243//!
244//! ```rust
245//! # struct State;
246//! # #[derive(Clone)]
247//! # enum Message {}
248//! use iced::widget::button;
249//! use iced::{Element, Theme};
250//!
251//! fn view(state: &State) -> Element<Message> {
252//!     button("I am a styled button!").style(|theme: &Theme, status| {
253//!         let palette = theme.extended_palette();
254//!
255//!         match status {
256//!             button::Status::Active => {
257//!                 button::Style::default()
258//!                    .with_background(palette.success.strong.color)
259//!             }
260//!             _ => button::primary(theme, status),
261//!         }
262//!     })
263//!     .into()
264//! }
265//! ```
266//!
267//! Widgets that can be in multiple different states will also provide the closure
268//! with some [`Status`], allowing you to use a different style for each state.
269//!
270//! You can extract the [`Palette`] colors of a [`Theme`] with the [`palette`] or
271//! [`extended_palette`] methods.
272//!
273//! Most widgets provide styling functions for your convenience in their respective modules;
274//! like [`container::rounded_box`], [`button::primary`], or [`text::danger`].
275//!
276//! [`Status`]: widget::button::Status
277//! [`palette`]: Theme::palette
278//! [`extended_palette`]: Theme::extended_palette
279//! [`container::rounded_box`]: widget::container::rounded_box
280//! [`button::primary`]: widget::button::primary
281//! [`text::danger`]: widget::text::danger
282//!
283//! ## Concurrent Tasks
284//! The `update` function can _optionally_ return a [`Task`].
285//!
286//! A [`Task`] can be leveraged to perform asynchronous work, like running a
287//! future or a stream:
288//!
289//! ```rust
290//! # #[derive(Clone)]
291//! # struct Weather;
292//! use iced::Task;
293//!
294//! struct State {
295//!     weather: Option<Weather>,
296//! }
297//!
298//! enum Message {
299//!    FetchWeather,
300//!    WeatherFetched(Weather),
301//! }
302//!
303//! fn update(state: &mut State, message: Message) -> Task<Message> {
304//!     match message {
305//!         Message::FetchWeather => Task::perform(
306//!             fetch_weather(),
307//!             Message::WeatherFetched,
308//!         ),
309//!         Message::WeatherFetched(weather) => {
310//!             state.weather = Some(weather);
311//!
312//!             Task::none()
313//!        }
314//!     }
315//! }
316//!
317//! async fn fetch_weather() -> Weather {
318//!     // ...
319//!     # unimplemented!()
320//! }
321//! ```
322//!
323//! Tasks can also be used to interact with the iced runtime. Some modules
324//! expose functions that create tasks for different purposes—like [changing
325//! window settings](window#functions), [focusing a widget](widget::focus_next), or
326//! [querying its visible bounds](widget::container::visible_bounds).
327//!
328//! Like futures and streams, tasks expose [a monadic interface](Task::then)—but they can also be
329//! [mapped](Task::map), [chained](Task::chain), [batched](Task::batch), [canceled](Task::abortable),
330//! and more.
331//!
332//! ## Passive Subscriptions
333//! Applications can subscribe to passive sources of data—like time ticks or runtime events.
334//!
335//! You will need to define a `subscription` function and use the [`Application`] builder:
336//!
337//! ```rust,no_run
338//! # #[derive(Default)]
339//! # struct State;
340//! use iced::window;
341//! use iced::{Size, Subscription};
342//!
343//! #[derive(Debug)]
344//! enum Message {
345//!     WindowResized(Size),
346//! }
347//!
348//! pub fn main() -> iced::Result {
349//!     iced::application("A cool application", update, view)
350//!         .subscription(subscription)
351//!         .run()
352//! }
353//!
354//! fn subscription(state: &State) -> Subscription<Message> {
355//!     window::resize_events().map(|(_id, size)| Message::WindowResized(size))
356//! }
357//! # fn update(state: &mut State, message: Message) {}
358//! # fn view(state: &State) -> iced::Element<Message> { iced::widget::text("").into() }
359//! ```
360//!
361//! A [`Subscription`] is [a _declarative_ builder of streams](Subscription#the-lifetime-of-a-subscription)
362//! that are not allowed to end on their own. Only the `subscription` function
363//! dictates the active subscriptions—just like `view` fully dictates the
364//! visible widgets of your user interface, at every moment.
365//!
366//! As with tasks, some modules expose convenient functions that build a [`Subscription`] for you—like
367//! [`time::every`] which can be used to listen to time, or [`keyboard::on_key_press`] which will notify you
368//! of any key presses. But you can also create your own with [`Subscription::run`] and [`run_with_id`].
369//!
370//! [`run_with_id`]: Subscription::run_with_id
371//!
372//! ## Scaling Applications
373//! The `update`, `view`, and `Message` triplet composes very nicely.
374//!
375//! A common pattern is to leverage this composability to split an
376//! application into different screens:
377//!
378//! ```rust
379//! # mod contacts {
380//! #     use iced::{Element, Task};
381//! #     pub struct Contacts;
382//! #     impl Contacts {
383//! #         pub fn update(&mut self, message: Message) -> Action { unimplemented!() }
384//! #         pub fn view(&self) -> Element<Message> { unimplemented!() }
385//! #     }
386//! #     #[derive(Debug)]
387//! #     pub enum Message {}
388//! #     pub enum Action { None, Run(Task<Message>), Chat(()) }
389//! # }
390//! # mod conversation {
391//! #     use iced::{Element, Task};
392//! #     pub struct Conversation;
393//! #     impl Conversation {
394//! #         pub fn new(contact: ()) -> (Self, Task<Message>) { unimplemented!() }
395//! #         pub fn update(&mut self, message: Message) -> Task<Message> { unimplemented!() }
396//! #         pub fn view(&self) -> Element<Message> { unimplemented!() }
397//! #     }
398//! #     #[derive(Debug)]
399//! #     pub enum Message {}
400//! # }
401//! use contacts::Contacts;
402//! use conversation::Conversation;
403//!
404//! use iced::{Element, Task};
405//!
406//! struct State {
407//!     screen: Screen,
408//! }
409//!
410//! enum Screen {
411//!     Contacts(Contacts),
412//!     Conversation(Conversation),
413//! }
414//!
415//! enum Message {
416//!    Contacts(contacts::Message),
417//!    Conversation(conversation::Message)
418//! }
419//!
420//! fn update(state: &mut State, message: Message) -> Task<Message> {
421//!     match message {
422//!         Message::Contacts(message) => {
423//!             if let Screen::Contacts(contacts) = &mut state.screen {
424//!                 let action = contacts.update(message);
425//!
426//!                 match action {
427//!                     contacts::Action::None => Task::none(),
428//!                     contacts::Action::Run(task) => task.map(Message::Contacts),
429//!                     contacts::Action::Chat(contact) => {
430//!                         let (conversation, task) = Conversation::new(contact);
431//!
432//!                         state.screen = Screen::Conversation(conversation);
433//!
434//!                         task.map(Message::Conversation)
435//!                     }
436//!                  }
437//!             } else {
438//!                 Task::none()    
439//!             }
440//!         }
441//!         Message::Conversation(message) => {
442//!             if let Screen::Conversation(conversation) = &mut state.screen {
443//!                 conversation.update(message).map(Message::Conversation)
444//!             } else {
445//!                 Task::none()    
446//!             }
447//!         }
448//!     }
449//! }
450//!
451//! fn view(state: &State) -> Element<Message> {
452//!     match &state.screen {
453//!         Screen::Contacts(contacts) => contacts.view().map(Message::Contacts),
454//!         Screen::Conversation(conversation) => conversation.view().map(Message::Conversation),
455//!     }
456//! }
457//! ```
458//!
459//! The `update` method of a screen can return an `Action` enum that can be leveraged by the parent to
460//! execute a task or transition to a completely different screen altogether. The variants of `Action` can
461//! have associated data. For instance, in the example above, the `Conversation` screen is created when
462//! `Contacts::update` returns an `Action::Chat` with the selected contact.
463//!
464//! Effectively, this approach lets you "tell a story" to connect different screens together in a type safe
465//! way.
466//!
467//! Furthermore, functor methods like [`Task::map`], [`Element::map`], and [`Subscription::map`] make composition
468//! seamless.
469#![doc(
470    html_logo_url = "https://raw.githubusercontent.com/iced-rs/iced/bdf0430880f5c29443f5f0a0ae4895866dfef4c6/docs/logo.svg"
471)]
472#![cfg_attr(docsrs, feature(doc_auto_cfg))]
473#![cfg_attr(docsrs, feature(doc_cfg))]
474use iced_widget::graphics;
475use iced_widget::renderer;
476use iced_winit as shell;
477use iced_winit::core;
478use iced_winit::runtime;
479
480pub use iced_futures::futures;
481pub use iced_futures::stream;
482
483#[cfg(feature = "highlighter")]
484pub use iced_highlighter as highlighter;
485
486mod error;
487mod program;
488
489pub mod application;
490pub mod daemon;
491pub mod settings;
492pub mod time;
493pub mod window;
494
495#[cfg(feature = "advanced")]
496pub mod advanced;
497
498pub use crate::core::alignment;
499pub use crate::core::border;
500pub use crate::core::color;
501pub use crate::core::gradient;
502pub use crate::core::padding;
503pub use crate::core::theme;
504pub use crate::core::{
505    Alignment, Background, Border, Color, ContentFit, Degrees, Gradient,
506    Length, Padding, Pixels, Point, Radians, Rectangle, Rotation, Shadow, Size,
507    Theme, Transformation, Vector,
508};
509pub use crate::runtime::exit;
510pub use iced_futures::Subscription;
511
512pub use alignment::Horizontal::{Left, Right};
513pub use alignment::Vertical::{Bottom, Top};
514pub use Alignment::Center;
515pub use Length::{Fill, FillPortion, Shrink};
516
517pub mod task {
518    //! Create runtime tasks.
519    pub use crate::runtime::task::{Handle, Task};
520}
521
522pub mod clipboard {
523    //! Access the clipboard.
524    pub use crate::runtime::clipboard::{
525        read, read_primary, write, write_primary,
526    };
527}
528
529pub mod executor {
530    //! Choose your preferred executor to power your application.
531    pub use iced_futures::Executor;
532
533    /// A default cross-platform executor.
534    ///
535    /// - On native platforms, it will use:
536    ///   - `iced_futures::backend::native::tokio` when the `tokio` feature is enabled.
537    ///   - `iced_futures::backend::native::async-std` when the `async-std` feature is
538    ///     enabled.
539    ///   - `iced_futures::backend::native::smol` when the `smol` feature is enabled.
540    ///   - `iced_futures::backend::native::thread_pool` otherwise.
541    ///
542    /// - On Wasm, it will use `iced_futures::backend::wasm::wasm_bindgen`.
543    pub type Default = iced_futures::backend::default::Executor;
544}
545
546pub mod font {
547    //! Load and use fonts.
548    pub use crate::core::font::*;
549    pub use crate::runtime::font::*;
550}
551
552pub mod event {
553    //! Handle events of a user interface.
554    pub use crate::core::event::{Event, Status};
555    pub use iced_futures::event::{listen, listen_raw, listen_with};
556}
557
558pub mod keyboard {
559    //! Listen and react to keyboard events.
560    pub use crate::core::keyboard::key;
561    pub use crate::core::keyboard::{Event, Key, Location, Modifiers};
562    pub use iced_futures::keyboard::{on_key_press, on_key_release};
563}
564
565pub mod mouse {
566    //! Listen and react to mouse events.
567    pub use crate::core::mouse::{
568        Button, Cursor, Event, Interaction, ScrollDelta,
569    };
570}
571
572#[cfg(feature = "system")]
573pub mod system {
574    //! Retrieve system information.
575    pub use crate::runtime::system::Information;
576    pub use crate::shell::system::*;
577}
578
579pub mod overlay {
580    //! Display interactive elements on top of other widgets.
581
582    /// A generic overlay.
583    ///
584    /// This is an alias of an [`overlay::Element`] with a default `Renderer`.
585    ///
586    /// [`overlay::Element`]: crate::core::overlay::Element
587    pub type Element<
588        'a,
589        Message,
590        Theme = crate::Renderer,
591        Renderer = crate::Renderer,
592    > = crate::core::overlay::Element<'a, Message, Theme, Renderer>;
593
594    pub use iced_widget::overlay::*;
595}
596
597pub mod touch {
598    //! Listen and react to touch events.
599    pub use crate::core::touch::{Event, Finger};
600}
601
602#[allow(hidden_glob_reexports)]
603pub mod widget {
604    //! Use the built-in widgets or create your own.
605    pub use iced_widget::*;
606
607    // We hide the re-exported modules by `iced_widget`
608    mod core {}
609    mod graphics {}
610    mod native {}
611    mod renderer {}
612    mod style {}
613    mod runtime {}
614}
615
616pub use application::Application;
617pub use daemon::Daemon;
618pub use error::Error;
619pub use event::Event;
620pub use executor::Executor;
621pub use font::Font;
622pub use renderer::Renderer;
623pub use settings::Settings;
624pub use task::Task;
625
626#[doc(inline)]
627pub use application::application;
628#[doc(inline)]
629pub use daemon::daemon;
630
631/// A generic widget.
632///
633/// This is an alias of an `iced_native` element with a default `Renderer`.
634pub type Element<
635    'a,
636    Message,
637    Theme = crate::Theme,
638    Renderer = crate::Renderer,
639> = crate::core::Element<'a, Message, Theme, Renderer>;
640
641/// The result of running an iced program.
642pub type Result = std::result::Result<(), Error>;
643
644/// Runs a basic iced application with default [`Settings`] given its title,
645/// update, and view logic.
646///
647/// This is equivalent to chaining [`application()`] with [`Application::run`].
648///
649/// # Example
650/// ```no_run
651/// use iced::widget::{button, column, text, Column};
652///
653/// pub fn main() -> iced::Result {
654///     iced::run("A counter", update, view)
655/// }
656///
657/// #[derive(Debug, Clone)]
658/// enum Message {
659///     Increment,
660/// }
661///
662/// fn update(value: &mut u64, message: Message) {
663///     match message {
664///         Message::Increment => *value += 1,
665///     }
666/// }
667///
668/// fn view(value: &u64) -> Column<Message> {
669///     column![
670///         text(value),
671///         button("+").on_press(Message::Increment),
672///     ]
673/// }
674/// ```
675pub fn run<State, Message, Theme, Renderer>(
676    title: impl application::Title<State> + 'static,
677    update: impl application::Update<State, Message> + 'static,
678    view: impl for<'a> application::View<'a, State, Message, Theme, Renderer>
679        + 'static,
680) -> Result
681where
682    State: Default + 'static,
683    Message: std::fmt::Debug + Send + 'static,
684    Theme: Default + program::DefaultStyle + 'static,
685    Renderer: program::Renderer + 'static,
686{
687    application(title, update, view).run()
688}