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//! ```no_run,standalone_crate
33//! pub fn main() -> iced::Result {
34//!     iced::run(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//! ```standalone_crate
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//! ```standalone_crate
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//! ```standalone_crate
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//! ```standalone_crate
78//! #[derive(Default)]
79//! struct Counter {
80//!     value: u64,
81//! }
82//! ```
83//!
84//! But you have to change `update` and `view` accordingly:
85//!
86//! ```standalone_crate
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//! ```standalone_crate
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//! ```standalone_crate
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//! ```standalone_crate
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//! ```no_run,standalone_crate
201//! # struct State;
202//! use iced::Theme;
203//!
204//! pub fn main() -> iced::Result {
205//!     iced::application(new, update, view)
206//!         .theme(theme)
207//!         .run()
208//! }
209//!
210//! fn new() -> State {
211//!     // ...
212//!     # State
213//! }
214//!
215//! fn theme(state: &State) -> Theme {
216//!     Theme::TokyoNight
217//! }
218//! # fn update(state: &mut State, message: ()) {}
219//! # fn view(state: &State) -> iced::Element<()> { iced::widget::text("").into() }
220//! ```
221//!
222//! The `theme` function takes the current state of the application, allowing the
223//! returned [`Theme`] to be completely dynamic—just like `view`.
224//!
225//! There are a bunch of built-in [`Theme`] variants at your disposal, but you can
226//! also [create your own](Theme::custom).
227//!
228//! ## Styling
229//! As with layout, iced does not have a unified styling system. However, all
230//! of the built-in widgets follow the same styling approach.
231//!
232//! The appearance of a widget can be changed by calling its `style` method:
233//!
234//! ```standalone_crate
235//! # struct State;
236//! # enum Message {}
237//! use iced::widget::container;
238//! use iced::Element;
239//!
240//! fn view(state: &State) -> Element<Message> {
241//!     container("I am a rounded box!").style(container::rounded_box).into()
242//! }
243//! ```
244//!
245//! The `style` method of a widget takes a closure that, given the current active
246//! [`Theme`], returns the widget style:
247//!
248//! ```standalone_crate
249//! # struct State;
250//! # #[derive(Clone)]
251//! # enum Message {}
252//! use iced::widget::button;
253//! use iced::{Element, Theme};
254//!
255//! fn view(state: &State) -> Element<Message> {
256//!     button("I am a styled button!").style(|theme: &Theme, status| {
257//!         let palette = theme.extended_palette();
258//!
259//!         match status {
260//!             button::Status::Active => {
261//!                 button::Style::default()
262//!                    .with_background(palette.success.strong.color)
263//!             }
264//!             _ => button::primary(theme, status),
265//!         }
266//!     })
267//!     .into()
268//! }
269//! ```
270//!
271//! Widgets that can be in multiple different states will also provide the closure
272//! with some [`Status`], allowing you to use a different style for each state.
273//!
274//! You can extract the [`Palette`] colors of a [`Theme`] with the [`palette`] or
275//! [`extended_palette`] methods.
276//!
277//! Most widgets provide styling functions for your convenience in their respective modules;
278//! like [`container::rounded_box`], [`button::primary`], or [`text::danger`].
279//!
280//! [`Status`]: widget::button::Status
281//! [`palette`]: Theme::palette
282//! [`extended_palette`]: Theme::extended_palette
283//! [`container::rounded_box`]: widget::container::rounded_box
284//! [`button::primary`]: widget::button::primary
285//! [`text::danger`]: widget::text::danger
286//!
287//! ## Concurrent Tasks
288//! The `update` function can _optionally_ return a [`Task`].
289//!
290//! A [`Task`] can be leveraged to perform asynchronous work, like running a
291//! future or a stream:
292//!
293//! ```standalone_crate
294//! # #[derive(Clone)]
295//! # struct Weather;
296//! use iced::Task;
297//!
298//! struct State {
299//!     weather: Option<Weather>,
300//! }
301//!
302//! enum Message {
303//!    FetchWeather,
304//!    WeatherFetched(Weather),
305//! }
306//!
307//! fn update(state: &mut State, message: Message) -> Task<Message> {
308//!     match message {
309//!         Message::FetchWeather => Task::perform(
310//!             fetch_weather(),
311//!             Message::WeatherFetched,
312//!         ),
313//!         Message::WeatherFetched(weather) => {
314//!             state.weather = Some(weather);
315//!
316//!             Task::none()
317//!        }
318//!     }
319//! }
320//!
321//! async fn fetch_weather() -> Weather {
322//!     // ...
323//!     # unimplemented!()
324//! }
325//! ```
326//!
327//! Tasks can also be used to interact with the iced runtime. Some modules
328//! expose functions that create tasks for different purposes—like [changing
329//! window settings](window#functions), [focusing a widget](widget::focus_next), or
330//! [querying its visible bounds](widget::container::visible_bounds).
331//!
332//! Like futures and streams, tasks expose [a monadic interface](Task::then)—but they can also be
333//! [mapped](Task::map), [chained](Task::chain), [batched](Task::batch), [canceled](Task::abortable),
334//! and more.
335//!
336//! ## Passive Subscriptions
337//! Applications can subscribe to passive sources of data—like time ticks or runtime events.
338//!
339//! You will need to define a `subscription` function and use the [`Application`] builder:
340//!
341//! ```no_run,standalone_crate
342//! # struct State;
343//! use iced::window;
344//! use iced::{Size, Subscription};
345//!
346//! #[derive(Debug)]
347//! enum Message {
348//!     WindowResized(Size),
349//! }
350//!
351//! pub fn main() -> iced::Result {
352//!     iced::application(new, update, view)
353//!         .subscription(subscription)
354//!         .run()
355//! }
356//!
357//! fn subscription(state: &State) -> Subscription<Message> {
358//!     window::resize_events().map(|(_id, size)| Message::WindowResized(size))
359//! }
360//! # fn new() -> State { State }
361//! # fn update(state: &mut State, message: Message) {}
362//! # fn view(state: &State) -> iced::Element<Message> { iced::widget::text("").into() }
363//! ```
364//!
365//! A [`Subscription`] is [a _declarative_ builder of streams](Subscription#the-lifetime-of-a-subscription)
366//! that are not allowed to end on their own. Only the `subscription` function
367//! dictates the active subscriptions—just like `view` fully dictates the
368//! visible widgets of your user interface, at every moment.
369//!
370//! As with tasks, some modules expose convenient functions that build a [`Subscription`] for you—like
371//! [`time::every`] which can be used to listen to time, or [`keyboard::on_key_press`] which will notify you
372//! of any key presses. But you can also create your own with [`Subscription::run`] and [`run_with`].
373//!
374//! [`run_with`]: Subscription::run_with
375//!
376//! ## Scaling Applications
377//! The `update`, `view`, and `Message` triplet composes very nicely.
378//!
379//! A common pattern is to leverage this composability to split an
380//! application into different screens:
381//!
382//! ```standalone_crate
383//! # mod contacts {
384//! #     use iced::{Element, Task};
385//! #     pub struct Contacts;
386//! #     impl Contacts {
387//! #         pub fn update(&mut self, message: Message) -> Action { unimplemented!() }
388//! #         pub fn view(&self) -> Element<Message> { unimplemented!() }
389//! #     }
390//! #     #[derive(Debug)]
391//! #     pub enum Message {}
392//! #     pub enum Action { None, Run(Task<Message>), Chat(()) }
393//! # }
394//! # mod conversation {
395//! #     use iced::{Element, Task};
396//! #     pub struct Conversation;
397//! #     impl Conversation {
398//! #         pub fn new(contact: ()) -> (Self, Task<Message>) { unimplemented!() }
399//! #         pub fn update(&mut self, message: Message) -> Task<Message> { unimplemented!() }
400//! #         pub fn view(&self) -> Element<Message> { unimplemented!() }
401//! #     }
402//! #     #[derive(Debug)]
403//! #     pub enum Message {}
404//! # }
405//! use contacts::Contacts;
406//! use conversation::Conversation;
407//!
408//! use iced::{Element, Task};
409//!
410//! struct State {
411//!     screen: Screen,
412//! }
413//!
414//! enum Screen {
415//!     Contacts(Contacts),
416//!     Conversation(Conversation),
417//! }
418//!
419//! enum Message {
420//!    Contacts(contacts::Message),
421//!    Conversation(conversation::Message)
422//! }
423//!
424//! fn update(state: &mut State, message: Message) -> Task<Message> {
425//!     match message {
426//!         Message::Contacts(message) => {
427//!             if let Screen::Contacts(contacts) = &mut state.screen {
428//!                 let action = contacts.update(message);
429//!
430//!                 match action {
431//!                     contacts::Action::None => Task::none(),
432//!                     contacts::Action::Run(task) => task.map(Message::Contacts),
433//!                     contacts::Action::Chat(contact) => {
434//!                         let (conversation, task) = Conversation::new(contact);
435//!
436//!                         state.screen = Screen::Conversation(conversation);
437//!
438//!                         task.map(Message::Conversation)
439//!                     }
440//!                  }
441//!             } else {
442//!                 Task::none()    
443//!             }
444//!         }
445//!         Message::Conversation(message) => {
446//!             if let Screen::Conversation(conversation) = &mut state.screen {
447//!                 conversation.update(message).map(Message::Conversation)
448//!             } else {
449//!                 Task::none()    
450//!             }
451//!         }
452//!     }
453//! }
454//!
455//! fn view(state: &State) -> Element<Message> {
456//!     match &state.screen {
457//!         Screen::Contacts(contacts) => contacts.view().map(Message::Contacts),
458//!         Screen::Conversation(conversation) => conversation.view().map(Message::Conversation),
459//!     }
460//! }
461//! ```
462//!
463//! The `update` method of a screen can return an `Action` enum that can be leveraged by the parent to
464//! execute a task or transition to a completely different screen altogether. The variants of `Action` can
465//! have associated data. For instance, in the example above, the `Conversation` screen is created when
466//! `Contacts::update` returns an `Action::Chat` with the selected contact.
467//!
468//! Effectively, this approach lets you "tell a story" to connect different screens together in a type safe
469//! way.
470//!
471//! Furthermore, functor methods like [`Task::map`], [`Element::map`], and [`Subscription::map`] make composition
472//! seamless.
473#![doc(
474    html_logo_url = "https://raw.githubusercontent.com/iced-rs/iced/bdf0430880f5c29443f5f0a0ae4895866dfef4c6/docs/logo.svg"
475)]
476#![cfg_attr(docsrs, feature(doc_auto_cfg))]
477#![cfg_attr(docsrs, feature(doc_cfg))]
478use iced_widget::graphics;
479use iced_widget::renderer;
480use iced_winit as shell;
481use iced_winit::core;
482use iced_winit::program;
483use iced_winit::runtime;
484
485pub use iced_futures::futures;
486pub use iced_futures::stream;
487
488#[cfg(not(any(
489    target_arch = "wasm32",
490    feature = "thread-pool",
491    feature = "tokio",
492    feature = "smol"
493)))]
494compile_error!(
495    "No futures executor has been enabled! You must enable an \
496    executor feature.\n\
497    Available options: thread-pool, tokio, or smol."
498);
499
500#[cfg(feature = "highlighter")]
501pub use iced_highlighter as highlighter;
502
503#[cfg(feature = "wgpu")]
504pub use iced_renderer::wgpu::wgpu;
505
506mod error;
507
508pub mod application;
509pub mod daemon;
510pub mod time;
511pub mod window;
512
513#[cfg(feature = "advanced")]
514pub mod advanced;
515
516pub use crate::core::alignment;
517pub use crate::core::animation;
518pub use crate::core::border;
519pub use crate::core::color;
520pub use crate::core::gradient;
521pub use crate::core::padding;
522pub use crate::core::theme;
523pub use crate::core::{
524    Alignment, Animation, Background, Border, Color, ContentFit, Degrees,
525    Function, Gradient, Length, Padding, Pixels, Point, Radians, Rectangle,
526    Rotation, Settings, Shadow, Size, Theme, Transformation, Vector, never,
527};
528pub use crate::runtime::exit;
529pub use iced_futures::Subscription;
530
531pub use Alignment::Center;
532pub use Length::{Fill, FillPortion, Shrink};
533pub use alignment::Horizontal::{Left, Right};
534pub use alignment::Vertical::{Bottom, Top};
535
536pub mod debug {
537    //! Debug your applications.
538    pub use iced_debug::{Span, skip_next_timing, time, time_with};
539}
540
541pub mod task {
542    //! Create runtime tasks.
543    pub use crate::runtime::task::{Handle, Task};
544
545    #[cfg(feature = "sipper")]
546    pub use crate::runtime::task::{Never, Sipper, Straw, sipper, stream};
547}
548
549pub mod clipboard {
550    //! Access the clipboard.
551    pub use crate::runtime::clipboard::{
552        read, read_primary, write, write_primary,
553    };
554}
555
556pub mod executor {
557    //! Choose your preferred executor to power your application.
558    pub use iced_futures::Executor;
559    pub use iced_futures::backend::default::Executor as Default;
560}
561
562pub mod font {
563    //! Load and use fonts.
564    pub use crate::core::font::*;
565    pub use crate::runtime::font::*;
566}
567
568pub mod event {
569    //! Handle events of a user interface.
570    pub use crate::core::event::{Event, Status};
571    pub use iced_futures::event::{
572        listen, listen_raw, listen_url, listen_with,
573    };
574}
575
576pub mod keyboard {
577    //! Listen and react to keyboard events.
578    pub use crate::core::keyboard::key;
579    pub use crate::core::keyboard::{Event, Key, Location, Modifiers};
580    pub use iced_futures::keyboard::{on_key_press, on_key_release};
581}
582
583pub mod mouse {
584    //! Listen and react to mouse events.
585    pub use crate::core::mouse::{
586        Button, Cursor, Event, Interaction, ScrollDelta,
587    };
588}
589
590#[cfg(feature = "system")]
591pub mod system {
592    //! Retrieve system information.
593    pub use crate::runtime::system::Information;
594    pub use crate::shell::system::*;
595}
596
597pub mod overlay {
598    //! Display interactive elements on top of other widgets.
599
600    /// A generic overlay.
601    ///
602    /// This is an alias of an [`overlay::Element`] with a default `Renderer`.
603    ///
604    /// [`overlay::Element`]: crate::core::overlay::Element
605    pub type Element<
606        'a,
607        Message,
608        Theme = crate::Renderer,
609        Renderer = crate::Renderer,
610    > = crate::core::overlay::Element<'a, Message, Theme, Renderer>;
611
612    pub use iced_widget::overlay::*;
613}
614
615pub mod touch {
616    //! Listen and react to touch events.
617    pub use crate::core::touch::{Event, Finger};
618}
619
620#[allow(hidden_glob_reexports)]
621pub mod widget {
622    //! Use the built-in widgets or create your own.
623    pub use iced_widget::*;
624
625    // We hide the re-exported modules by `iced_widget`
626    mod core {}
627    mod graphics {}
628    mod native {}
629    mod renderer {}
630    mod style {}
631    mod runtime {}
632}
633
634pub use application::Application;
635pub use daemon::Daemon;
636pub use error::Error;
637pub use event::Event;
638pub use executor::Executor;
639pub use font::Font;
640pub use program::Program;
641pub use renderer::Renderer;
642pub use task::Task;
643
644#[doc(inline)]
645pub use application::application;
646#[doc(inline)]
647pub use daemon::daemon;
648
649/// A generic widget.
650///
651/// This is an alias of an `iced_native` element with a default `Renderer`.
652pub type Element<
653    'a,
654    Message,
655    Theme = crate::Theme,
656    Renderer = crate::Renderer,
657> = crate::core::Element<'a, Message, Theme, Renderer>;
658
659/// The result of running an iced program.
660pub type Result = std::result::Result<(), Error>;
661
662/// Runs a basic iced application with default [`Settings`] given its title,
663/// update, and view logic.
664///
665/// This is equivalent to chaining [`application()`] with [`Application::run`].
666///
667/// # Example
668/// ```no_run,standalone_crate
669/// use iced::widget::{button, column, text, Column};
670///
671/// pub fn main() -> iced::Result {
672///     iced::run(update, view)
673/// }
674///
675/// #[derive(Debug, Clone)]
676/// enum Message {
677///     Increment,
678/// }
679///
680/// fn update(value: &mut u64, message: Message) {
681///     match message {
682///         Message::Increment => *value += 1,
683///     }
684/// }
685///
686/// fn view(value: &u64) -> Column<Message> {
687///     column![
688///         text(value),
689///         button("+").on_press(Message::Increment),
690///     ]
691/// }
692/// ```
693pub fn run<State, Message, Theme, Renderer>(
694    update: impl application::Update<State, Message> + 'static,
695    view: impl for<'a> application::View<'a, State, Message, Theme, Renderer>
696    + 'static,
697) -> Result
698where
699    State: Default + 'static,
700    Message: std::fmt::Debug + Send + 'static,
701    Theme: Default + theme::Base + 'static,
702    Renderer: program::Renderer + 'static,
703{
704    application(State::default, update, view).run()
705}