summaryrefslogtreecommitdiffstats
path: root/src/widget.rs
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2019-07-20 19:12:31 +0200
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2019-07-20 19:12:31 +0200
commit2b7ad3d50eae48b1963aa8e866e184c41133ca3d (patch)
treeae5b0c851aebb2dd8c01c08620d1cea7aa9d2466 /src/widget.rs
parenteefdcbe06cce97b452ee71ccb6fcd1a423d29075 (diff)
downloadiced-2b7ad3d50eae48b1963aa8e866e184c41133ca3d.tar.gz
iced-2b7ad3d50eae48b1963aa8e866e184c41133ca3d.tar.bz2
iced-2b7ad3d50eae48b1963aa8e866e184c41133ca3d.zip
Decouple `iced` from `coffee`
Diffstat (limited to 'src/widget.rs')
-rw-r--r--src/widget.rs103
1 files changed, 103 insertions, 0 deletions
diff --git a/src/widget.rs b/src/widget.rs
new file mode 100644
index 00000000..d1ad8123
--- /dev/null
+++ b/src/widget.rs
@@ -0,0 +1,103 @@
+//! Use the built-in widgets in your user interface.
+//!
+//! # Customization
+//! Every drawable widget has its own module with a `Renderer` trait that must
+//! be implemented by a custom renderer before being able to use the
+//! widget.
+//!
+//! The built-in [`Renderer`] supports all the widgets in this module!
+//!
+//! [`ui` module]: ../index.html
+//! [`Row`]: struct.Row.html
+//! [`Column`]: struct.Column.html
+//! [`Renderer`]: ../struct.Renderer.html
+mod column;
+mod row;
+
+pub mod button;
+pub mod checkbox;
+pub mod radio;
+pub mod slider;
+pub mod text;
+
+pub use button::Button;
+pub use checkbox::Checkbox;
+pub use column::Column;
+pub use radio::Radio;
+pub use row::Row;
+pub use slider::Slider;
+pub use text::Text;
+
+use crate::{Event, Hasher, Layout, MouseCursor, Node, Point};
+
+/// A component that displays information or allows interaction.
+///
+/// If you want to build a custom widget, you will need to implement this trait.
+/// Additionally, remember to also provide [`Into<Element>`] so your users can
+/// easily turn your [`Widget`] into a generic [`Element`]
+///
+/// [`Into<Element>`]: struct.Element.html
+/// [`Widget`]: trait.Widget.html
+/// [`Element`]: struct.Element.html
+pub trait Widget<Message, Renderer>: std::fmt::Debug {
+ /// Returns the [`Node`] of the [`Widget`].
+ ///
+ /// This [`Node`] is used by the runtime to compute the [`Layout`] of the
+ /// user interface.
+ ///
+ /// [`Node`]: struct.Node.html
+ /// [`Widget`]: trait.Widget.html
+ /// [`Layout`]: struct.Layout.html
+ fn node(&self, renderer: &Renderer) -> Node;
+
+ /// Draws the [`Widget`] using the associated `Renderer`.
+ ///
+ /// It must return the [`MouseCursor`] state for the [`Widget`].
+ ///
+ /// [`Widget`]: trait.Widget.html
+ /// [`MouseCursor`]: enum.MouseCursor.html
+ fn draw(
+ &self,
+ renderer: &mut Renderer,
+ layout: Layout<'_>,
+ cursor_position: Point,
+ ) -> MouseCursor;
+
+ /// Computes the _layout_ hash of the [`Widget`].
+ ///
+ /// The produced hash is used by the runtime to decide if the [`Layout`]
+ /// needs to be recomputed between frames. Therefore, to ensure maximum
+ /// efficiency, the hash should only be affected by the properties of the
+ /// [`Widget`] that can affect layouting.
+ ///
+ /// For example, the [`Text`] widget does not hash its color property, as
+ /// its value cannot affect the overall [`Layout`] of the user interface.
+ ///
+ /// [`Widget`]: trait.Widget.html
+ /// [`Layout`]: struct.Layout.html
+ /// [`Text`]: ../widget/text/struct.Text.html
+ fn hash(&self, state: &mut Hasher);
+
+ /// Processes a runtime [`Event`].
+ ///
+ /// It receives:
+ /// * an [`Event`] describing user interaction
+ /// * the computed [`Layout`] of the [`Widget`]
+ /// * the current cursor position
+ /// * a mutable `Message` vector, allowing the [`Widget`] to produce
+ /// new messages based on user interaction.
+ ///
+ /// By default, it does nothing.
+ ///
+ /// [`Event`]: enum.Event.html
+ /// [`Widget`]: trait.Widget.html
+ /// [`Layout`]: struct.Layout.html
+ fn on_event(
+ &mut self,
+ _event: Event,
+ _layout: Layout<'_>,
+ _cursor_position: Point,
+ _messages: &mut Vec<Message>,
+ ) {
+ }
+}