2019-08-29 01:35:37 +02:00
|
|
|
//! Use the built-in widgets or create your own.
|
2019-07-20 19:12:31 +02:00
|
|
|
//!
|
2019-08-28 06:45:39 +02:00
|
|
|
//! # Built-in widgets
|
|
|
|
|
//! Every built-in drawable widget has its own module with a `Renderer` trait
|
2019-08-29 01:28:00 +02:00
|
|
|
//! that must be implemented by a [renderer] before being able to use it as
|
|
|
|
|
//! a [`Widget`].
|
2019-08-28 06:45:39 +02:00
|
|
|
//!
|
|
|
|
|
//! # Custom widgets
|
|
|
|
|
//! If you want to implement a custom widget, you simply need to implement the
|
|
|
|
|
//! [`Widget`] trait. You can use the API of the built-in widgets as a guide or
|
|
|
|
|
//! source of inspiration.
|
2019-07-20 19:12:31 +02:00
|
|
|
//!
|
2020-11-25 07:11:27 +01:00
|
|
|
//! [renderer]: crate::renderer
|
2019-07-20 19:12:31 +02:00
|
|
|
pub mod button;
|
|
|
|
|
pub mod checkbox;
|
2019-10-05 03:56:18 +02:00
|
|
|
pub mod column;
|
2019-11-22 19:36:57 +01:00
|
|
|
pub mod container;
|
2022-07-27 06:49:20 +02:00
|
|
|
pub mod helpers;
|
2019-08-22 15:57:18 +02:00
|
|
|
pub mod image;
|
2022-07-28 02:46:51 +02:00
|
|
|
pub mod operation;
|
2020-03-10 02:57:13 +01:00
|
|
|
pub mod pane_grid;
|
2020-07-10 02:50:47 +02:00
|
|
|
pub mod pick_list;
|
2020-01-02 18:07:00 +01:00
|
|
|
pub mod progress_bar;
|
2019-07-20 19:12:31 +02:00
|
|
|
pub mod radio;
|
2019-10-05 03:56:18 +02:00
|
|
|
pub mod row;
|
2020-08-13 12:54:34 -05:00
|
|
|
pub mod rule;
|
2019-10-25 03:47:34 +02:00
|
|
|
pub mod scrollable;
|
2019-07-20 19:12:31 +02:00
|
|
|
pub mod slider;
|
2019-12-30 21:32:21 +01:00
|
|
|
pub mod space;
|
2019-12-18 07:45:49 +01:00
|
|
|
pub mod svg;
|
2019-07-20 19:12:31 +02:00
|
|
|
pub mod text;
|
2019-10-30 03:31:07 +01:00
|
|
|
pub mod text_input;
|
2020-09-19 18:44:27 +02:00
|
|
|
pub mod toggler;
|
2020-07-28 18:07:46 +03:00
|
|
|
pub mod tooltip;
|
2022-07-27 06:49:20 +02:00
|
|
|
pub mod tree;
|
2022-12-13 10:05:52 +01:00
|
|
|
pub mod vertical_slider;
|
2019-07-20 19:12:31 +02:00
|
|
|
|
2022-07-28 02:46:51 +02:00
|
|
|
mod action;
|
|
|
|
|
mod id;
|
|
|
|
|
|
2019-09-20 19:15:31 +02:00
|
|
|
#[doc(no_inline)]
|
2019-07-20 19:12:31 +02:00
|
|
|
pub use button::Button;
|
2019-09-20 19:15:31 +02:00
|
|
|
#[doc(no_inline)]
|
2019-07-20 19:12:31 +02:00
|
|
|
pub use checkbox::Checkbox;
|
2019-09-20 19:15:31 +02:00
|
|
|
#[doc(no_inline)]
|
2019-07-20 19:12:31 +02:00
|
|
|
pub use column::Column;
|
2019-09-20 19:15:31 +02:00
|
|
|
#[doc(no_inline)]
|
2019-11-11 05:26:08 +01:00
|
|
|
pub use container::Container;
|
|
|
|
|
#[doc(no_inline)]
|
2022-07-27 06:49:20 +02:00
|
|
|
pub use helpers::*;
|
|
|
|
|
#[doc(no_inline)]
|
2019-09-04 11:09:57 +02:00
|
|
|
pub use image::Image;
|
2019-09-20 19:15:31 +02:00
|
|
|
#[doc(no_inline)]
|
2020-03-10 02:57:13 +01:00
|
|
|
pub use pane_grid::PaneGrid;
|
2020-03-04 04:10:26 +01:00
|
|
|
#[doc(no_inline)]
|
2020-07-10 02:50:47 +02:00
|
|
|
pub use pick_list::PickList;
|
|
|
|
|
#[doc(no_inline)]
|
2020-01-02 18:07:00 +01:00
|
|
|
pub use progress_bar::ProgressBar;
|
2020-01-01 17:27:14 +01:00
|
|
|
#[doc(no_inline)]
|
2019-07-20 19:12:31 +02:00
|
|
|
pub use radio::Radio;
|
2019-09-20 19:15:31 +02:00
|
|
|
#[doc(no_inline)]
|
2019-07-20 19:12:31 +02:00
|
|
|
pub use row::Row;
|
2019-09-20 19:15:31 +02:00
|
|
|
#[doc(no_inline)]
|
2020-08-13 12:54:34 -05:00
|
|
|
pub use rule::Rule;
|
|
|
|
|
#[doc(no_inline)]
|
2019-10-25 03:47:34 +02:00
|
|
|
pub use scrollable::Scrollable;
|
|
|
|
|
#[doc(no_inline)]
|
2019-07-20 19:12:31 +02:00
|
|
|
pub use slider::Slider;
|
2019-09-20 19:15:31 +02:00
|
|
|
#[doc(no_inline)]
|
2019-12-30 21:32:21 +01:00
|
|
|
pub use space::Space;
|
|
|
|
|
#[doc(no_inline)]
|
2019-12-12 01:25:18 +01:00
|
|
|
pub use svg::Svg;
|
|
|
|
|
#[doc(no_inline)]
|
2019-07-20 19:12:31 +02:00
|
|
|
pub use text::Text;
|
2019-10-30 03:31:07 +01:00
|
|
|
#[doc(no_inline)]
|
|
|
|
|
pub use text_input::TextInput;
|
2020-07-28 18:07:46 +03:00
|
|
|
#[doc(no_inline)]
|
2020-09-19 18:44:27 +02:00
|
|
|
pub use toggler::Toggler;
|
|
|
|
|
#[doc(no_inline)]
|
2020-07-28 18:07:46 +03:00
|
|
|
pub use tooltip::Tooltip;
|
2022-07-27 06:49:20 +02:00
|
|
|
#[doc(no_inline)]
|
|
|
|
|
pub use tree::Tree;
|
2022-12-13 10:05:52 +01:00
|
|
|
#[doc(no_inline)]
|
|
|
|
|
pub use vertical_slider::VerticalSlider;
|
2019-07-20 19:12:31 +02:00
|
|
|
|
2022-07-28 02:46:51 +02:00
|
|
|
pub use action::Action;
|
|
|
|
|
pub use id::Id;
|
|
|
|
|
pub use operation::Operation;
|
|
|
|
|
|
2020-11-12 00:09:52 +01:00
|
|
|
use crate::event::{self, Event};
|
|
|
|
|
use crate::layout;
|
2021-10-18 16:43:18 +07:00
|
|
|
use crate::mouse;
|
2020-11-12 00:09:52 +01:00
|
|
|
use crate::overlay;
|
2021-10-18 15:19:04 +07:00
|
|
|
use crate::renderer;
|
2022-02-22 14:10:49 +07:00
|
|
|
use crate::{Clipboard, Layout, Length, Point, Rectangle, Shell};
|
2019-07-20 19:12:31 +02:00
|
|
|
|
2019-08-24 03:55:07 +02:00
|
|
|
/// A component that displays information and allows interaction.
|
|
|
|
|
///
|
|
|
|
|
/// If you want to build your own widgets, you will need to implement this
|
|
|
|
|
/// trait.
|
2019-07-20 19:12:31 +02:00
|
|
|
///
|
2020-04-02 02:35:23 +02:00
|
|
|
/// # Examples
|
|
|
|
|
/// The repository has some [examples] showcasing how to implement a custom
|
|
|
|
|
/// widget:
|
|
|
|
|
///
|
|
|
|
|
/// - [`bezier_tool`], a Paint-like tool for drawing Bézier curves using
|
|
|
|
|
/// [`lyon`].
|
|
|
|
|
/// - [`custom_widget`], a demonstration of how to build a custom widget that
|
|
|
|
|
/// draws a circle.
|
|
|
|
|
/// - [`geometry`], a custom widget showcasing how to draw geometry with the
|
|
|
|
|
/// `Mesh2D` primitive in [`iced_wgpu`].
|
|
|
|
|
///
|
2022-12-07 04:38:00 +01:00
|
|
|
/// [examples]: https://github.com/iced-rs/iced/tree/0.6/examples
|
|
|
|
|
/// [`bezier_tool`]: https://github.com/iced-rs/iced/tree/0.6/examples/bezier_tool
|
|
|
|
|
/// [`custom_widget`]: https://github.com/iced-rs/iced/tree/0.6/examples/custom_widget
|
|
|
|
|
/// [`geometry`]: https://github.com/iced-rs/iced/tree/0.6/examples/geometry
|
2020-04-02 02:35:23 +02:00
|
|
|
/// [`lyon`]: https://github.com/nical/lyon
|
2022-12-07 04:38:00 +01:00
|
|
|
/// [`iced_wgpu`]: https://github.com/iced-rs/iced/tree/0.6/wgpu
|
2020-07-10 02:01:30 +02:00
|
|
|
pub trait Widget<Message, Renderer>
|
2019-10-05 03:56:18 +02:00
|
|
|
where
|
|
|
|
|
Renderer: crate::Renderer,
|
|
|
|
|
{
|
2019-11-22 19:36:57 +01:00
|
|
|
/// Returns the width of the [`Widget`].
|
2019-11-16 22:08:49 +01:00
|
|
|
fn width(&self) -> Length;
|
|
|
|
|
|
2019-11-22 19:36:57 +01:00
|
|
|
/// Returns the height of the [`Widget`].
|
2019-11-16 22:08:49 +01:00
|
|
|
fn height(&self) -> Length;
|
|
|
|
|
|
2022-07-27 06:49:20 +02:00
|
|
|
/// Returns the [`layout::Node`] of the [`Widget`].
|
2019-07-20 19:12:31 +02:00
|
|
|
///
|
2022-07-27 06:49:20 +02:00
|
|
|
/// This [`layout::Node`] is used by the runtime to compute the [`Layout`] of the
|
2019-07-20 19:12:31 +02:00
|
|
|
/// user interface.
|
2019-11-10 06:05:20 +01:00
|
|
|
fn layout(
|
|
|
|
|
&self,
|
|
|
|
|
renderer: &Renderer,
|
|
|
|
|
limits: &layout::Limits,
|
|
|
|
|
) -> layout::Node;
|
|
|
|
|
|
2019-07-20 19:12:31 +02:00
|
|
|
/// Draws the [`Widget`] using the associated `Renderer`.
|
|
|
|
|
fn draw(
|
|
|
|
|
&self,
|
2022-07-27 06:49:20 +02:00
|
|
|
state: &Tree,
|
2019-07-20 19:12:31 +02:00
|
|
|
renderer: &mut Renderer,
|
2022-05-14 01:47:55 +02:00
|
|
|
theme: &Renderer::Theme,
|
2021-10-18 15:19:04 +07:00
|
|
|
style: &renderer::Style,
|
2019-11-10 06:05:20 +01:00
|
|
|
layout: Layout<'_>,
|
2019-07-20 19:12:31 +02:00
|
|
|
cursor_position: Point,
|
2020-08-18 03:37:32 +02:00
|
|
|
viewport: &Rectangle,
|
2021-10-14 16:07:22 +07:00
|
|
|
);
|
2019-07-20 19:12:31 +02:00
|
|
|
|
2022-07-27 06:49:20 +02:00
|
|
|
/// Returns the [`Tag`] of the [`Widget`].
|
|
|
|
|
///
|
|
|
|
|
/// [`Tag`]: tree::Tag
|
|
|
|
|
fn tag(&self) -> tree::Tag {
|
|
|
|
|
tree::Tag::stateless()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns the [`State`] of the [`Widget`].
|
2019-07-20 19:12:31 +02:00
|
|
|
///
|
2022-07-27 06:49:20 +02:00
|
|
|
/// [`State`]: tree::State
|
|
|
|
|
fn state(&self) -> tree::State {
|
|
|
|
|
tree::State::None
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns the state [`Tree`] of the children of the [`Widget`].
|
|
|
|
|
fn children(&self) -> Vec<Tree> {
|
|
|
|
|
Vec::new()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Reconciliates the [`Widget`] with the provided [`Tree`].
|
|
|
|
|
fn diff(&self, _tree: &mut Tree) {}
|
|
|
|
|
|
2022-07-28 02:46:51 +02:00
|
|
|
/// Applies an [`Operation`] to the [`Widget`].
|
|
|
|
|
fn operate(
|
|
|
|
|
&self,
|
2022-07-28 03:53:47 +02:00
|
|
|
_state: &mut Tree,
|
2022-07-28 02:46:51 +02:00
|
|
|
_layout: Layout<'_>,
|
|
|
|
|
_operation: &mut dyn Operation<Message>,
|
|
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-27 06:49:20 +02:00
|
|
|
/// Processes a runtime [`Event`].
|
2019-07-20 19:12:31 +02:00
|
|
|
///
|
|
|
|
|
/// By default, it does nothing.
|
|
|
|
|
fn on_event(
|
|
|
|
|
&mut self,
|
2022-07-27 06:49:20 +02:00
|
|
|
_state: &mut Tree,
|
2019-07-20 19:12:31 +02:00
|
|
|
_event: Event,
|
2019-11-10 06:05:20 +01:00
|
|
|
_layout: Layout<'_>,
|
2019-07-20 19:12:31 +02:00
|
|
|
_cursor_position: Point,
|
2019-10-29 03:34:21 +01:00
|
|
|
_renderer: &Renderer,
|
2021-03-10 01:59:02 +01:00
|
|
|
_clipboard: &mut dyn Clipboard,
|
2021-11-29 16:22:01 +07:00
|
|
|
_shell: &mut Shell<'_, Message>,
|
2020-11-12 00:09:52 +01:00
|
|
|
) -> event::Status {
|
|
|
|
|
event::Status::Ignored
|
2019-07-20 19:12:31 +02:00
|
|
|
}
|
2020-04-14 12:11:10 +02:00
|
|
|
|
2021-10-18 16:43:18 +07:00
|
|
|
/// Returns the current [`mouse::Interaction`] of the [`Widget`].
|
|
|
|
|
///
|
|
|
|
|
/// By default, it returns [`mouse::Interaction::Idle`].
|
|
|
|
|
fn mouse_interaction(
|
|
|
|
|
&self,
|
2022-07-27 06:49:20 +02:00
|
|
|
_state: &Tree,
|
2021-10-18 16:43:18 +07:00
|
|
|
_layout: Layout<'_>,
|
|
|
|
|
_cursor_position: Point,
|
2021-11-02 15:03:29 +07:00
|
|
|
_viewport: &Rectangle,
|
2022-01-11 14:12:28 +07:00
|
|
|
_renderer: &Renderer,
|
2021-10-18 16:43:18 +07:00
|
|
|
) -> mouse::Interaction {
|
|
|
|
|
mouse::Interaction::Idle
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-25 07:11:27 +01:00
|
|
|
/// Returns the overlay of the [`Widget`], if there is any.
|
2022-07-27 06:49:20 +02:00
|
|
|
fn overlay<'a>(
|
2022-11-19 12:25:59 -08:00
|
|
|
&'a mut self,
|
2022-07-27 06:49:20 +02:00
|
|
|
_state: &'a mut Tree,
|
2020-04-16 13:22:00 +02:00
|
|
|
_layout: Layout<'_>,
|
2022-01-11 14:12:28 +07:00
|
|
|
_renderer: &Renderer,
|
2022-07-27 06:49:20 +02:00
|
|
|
) -> Option<overlay::Element<'a, Message, Renderer>> {
|
2020-04-14 12:11:10 +02:00
|
|
|
None
|
|
|
|
|
}
|
2019-07-20 19:12:31 +02:00
|
|
|
}
|