summaryrefslogtreecommitdiffstats
path: root/native/src/widget
diff options
context:
space:
mode:
Diffstat (limited to 'native/src/widget')
-rw-r--r--native/src/widget/action.rs88
-rw-r--r--native/src/widget/button.rs469
-rw-r--r--native/src/widget/checkbox.rs53
-rw-r--r--native/src/widget/column.rs107
-rw-r--r--native/src/widget/container.rs179
-rw-r--r--native/src/widget/helpers.rs283
-rw-r--r--native/src/widget/id.rs43
-rw-r--r--native/src/widget/image.rs82
-rw-r--r--native/src/widget/image/viewer.rs164
-rw-r--r--native/src/widget/operation.rs60
-rw-r--r--native/src/widget/operation/focusable.rs169
-rw-r--r--native/src/widget/operation/scrollable.rs35
-rw-r--r--native/src/widget/pane_grid.rs1008
-rw-r--r--native/src/widget/pane_grid/axis.rs7
-rw-r--r--native/src/widget/pane_grid/configuration.rs4
-rw-r--r--native/src/widget/pane_grid/content.rs156
-rw-r--r--native/src/widget/pane_grid/draggable.rs12
-rw-r--r--native/src/widget/pane_grid/node.rs20
-rw-r--r--native/src/widget/pane_grid/state.rs141
-rw-r--r--native/src/widget/pane_grid/title_bar.rs184
-rw-r--r--native/src/widget/pick_list.rs660
-rw-r--r--native/src/widget/progress_bar.rs41
-rw-r--r--native/src/widget/radio.rs61
-rw-r--r--native/src/widget/row.rs126
-rw-r--r--native/src/widget/rule.rs52
-rw-r--r--native/src/widget/scrollable.rs1094
-rw-r--r--native/src/widget/slider.rs483
-rw-r--r--native/src/widget/space.rs3
-rw-r--r--native/src/widget/svg.rs7
-rw-r--r--native/src/widget/text.rs106
-rw-r--r--native/src/widget/text_input.rs1086
-rw-r--r--native/src/widget/text_input/editor.rs19
-rw-r--r--native/src/widget/text_input/value.rs7
-rw-r--r--native/src/widget/toggler.rs56
-rw-r--r--native/src/widget/tooltip.rs335
-rw-r--r--native/src/widget/tree.rs187
36 files changed, 4950 insertions, 2637 deletions
diff --git a/native/src/widget/action.rs b/native/src/widget/action.rs
new file mode 100644
index 00000000..766e902b
--- /dev/null
+++ b/native/src/widget/action.rs
@@ -0,0 +1,88 @@
+use crate::widget::operation::{self, Operation};
+use crate::widget::Id;
+
+use iced_futures::MaybeSend;
+
+/// An operation to be performed on the widget tree.
+#[allow(missing_debug_implementations)]
+pub struct Action<T>(Box<dyn Operation<T>>);
+
+impl<T> Action<T> {
+ /// Creates a new [`Action`] with the given [`Operation`].
+ pub fn new(operation: impl Operation<T> + 'static) -> Self {
+ Self(Box::new(operation))
+ }
+
+ /// Maps the output of an [`Action`] using the given function.
+ pub fn map<A>(
+ self,
+ f: impl Fn(T) -> A + 'static + MaybeSend + Sync,
+ ) -> Action<A>
+ where
+ T: 'static,
+ A: 'static,
+ {
+ Action(Box::new(Map {
+ operation: self.0,
+ f: Box::new(f),
+ }))
+ }
+
+ /// Consumes the [`Action`] and returns the internal [`Operation`].
+ pub fn into_operation(self) -> Box<dyn Operation<T>> {
+ self.0
+ }
+}
+
+#[allow(missing_debug_implementations)]
+struct Map<A, B> {
+ operation: Box<dyn Operation<A>>,
+ f: Box<dyn Fn(A) -> B>,
+}
+
+impl<A, B> Operation<B> for Map<A, B>
+where
+ A: 'static,
+ B: 'static,
+{
+ fn container(
+ &mut self,
+ id: Option<&Id>,
+ operate_on_children: &mut dyn FnMut(&mut dyn Operation<B>),
+ ) {
+ struct MapRef<'a, A, B> {
+ operation: &'a mut dyn Operation<A>,
+ f: &'a dyn Fn(A) -> B,
+ }
+
+ impl<'a, A, B> Operation<B> for MapRef<'a, A, B> {
+ fn container(
+ &mut self,
+ id: Option<&Id>,
+ operate_on_children: &mut dyn FnMut(&mut dyn Operation<B>),
+ ) {
+ let Self { operation, f } = self;
+
+ operation.container(id, &mut |operation| {
+ operate_on_children(&mut MapRef { operation, f });
+ });
+ }
+ }
+
+ let Self { operation, f } = self;
+
+ MapRef {
+ operation: operation.as_mut(),
+ f,
+ }
+ .container(id, operate_on_children);
+ }
+
+ fn focusable(
+ &mut self,
+ state: &mut dyn operation::Focusable,
+ id: Option<&Id>,
+ ) {
+ self.operation.focusable(state, id);
+ }
+}
diff --git a/native/src/widget/button.rs b/native/src/widget/button.rs
index 57fdd7d4..6c0b8f6e 100644
--- a/native/src/widget/button.rs
+++ b/native/src/widget/button.rs
@@ -7,18 +7,18 @@ use crate::mouse;
use crate::overlay;
use crate::renderer;
use crate::touch;
+use crate::widget::tree::{self, Tree};
+use crate::widget::Operation;
use crate::{
Background, Clipboard, Color, Element, Layout, Length, Padding, Point,
Rectangle, Shell, Vector, Widget,
};
-pub use iced_style::button::{Style, StyleSheet};
+pub use iced_style::button::{Appearance, StyleSheet};
/// A generic widget that produces a message when pressed.
///
/// ```
-/// # use iced_native::widget::{button, Text};
-/// #
/// # type Button<'a, Message> =
/// # iced_native::widget::Button<'a, Message, iced_native::renderer::Null>;
/// #
@@ -27,17 +27,13 @@ pub use iced_style::button::{Style, StyleSheet};
/// ButtonPressed,
/// }
///
-/// let mut state = button::State::new();
-/// let button = Button::new(&mut state, Text::new("Press me!"))
-/// .on_press(Message::ButtonPressed);
+/// let button = Button::new("Press me!").on_press(Message::ButtonPressed);
/// ```
///
/// If a [`Button::on_press`] handler is not set, the resulting [`Button`] will
/// be disabled:
///
/// ```
-/// # use iced_native::widget::{button, Text};
-/// #
/// # type Button<'a, Message> =
/// # iced_native::widget::Button<'a, Message, iced_native::renderer::Null>;
/// #
@@ -46,48 +42,42 @@ pub use iced_style::button::{Style, StyleSheet};
/// ButtonPressed,
/// }
///
-/// fn disabled_button(state: &mut button::State) -> Button<'_, Message> {
-/// Button::new(state, Text::new("I'm disabled!"))
+/// fn disabled_button<'a>() -> Button<'a, Message> {
+/// Button::new("I'm disabled!")
/// }
///
-/// fn enabled_button(state: &mut button::State) -> Button<'_, Message> {
-/// disabled_button(state).on_press(Message::ButtonPressed)
+/// fn enabled_button<'a>() -> Button<'a, Message> {
+/// disabled_button().on_press(Message::ButtonPressed)
/// }
/// ```
#[allow(missing_debug_implementations)]
-pub struct Button<'a, Message, Renderer> {
- state: &'a mut State,
+pub struct Button<'a, Message, Renderer>
+where
+ Renderer: crate::Renderer,
+ Renderer::Theme: StyleSheet,
+{
content: Element<'a, Message, Renderer>,
on_press: Option<Message>,
width: Length,
height: Length,
- min_width: u32,
- min_height: u32,
padding: Padding,
- style_sheet: Box<dyn StyleSheet + 'a>,
+ style: <Renderer::Theme as StyleSheet>::Style,
}
impl<'a, Message, Renderer> Button<'a, Message, Renderer>
where
- Message: Clone,
Renderer: crate::Renderer,
+ Renderer::Theme: StyleSheet,
{
- /// Creates a new [`Button`] with some local [`State`] and the given
- /// content.
- pub fn new<E>(state: &'a mut State, content: E) -> Self
- where
- E: Into<Element<'a, Message, Renderer>>,
- {
+ /// Creates a new [`Button`] with the given content.
+ pub fn new(content: impl Into<Element<'a, Message, Renderer>>) -> Self {
Button {
- state,
content: content.into(),
on_press: None,
width: Length::Shrink,
height: Length::Shrink,
- min_width: 0,
- min_height: 0,
padding: Padding::new(5),
- style_sheet: Default::default(),
+ style: <Renderer::Theme as StyleSheet>::Style::default(),
}
}
@@ -103,18 +93,6 @@ where
self
}
- /// Sets the minimum width of the [`Button`].
- pub fn min_width(mut self, min_width: u32) -> Self {
- self.min_width = min_width;
- self
- }
-
- /// Sets the minimum height of the [`Button`].
- pub fn min_height(mut self, min_height: u32) -> Self {
- self.min_height = min_height;
- self
- }
-
/// Sets the [`Padding`] of the [`Button`].
pub fn padding<P: Into<Padding>>(mut self, padding: P) -> Self {
self.padding = padding.into();
@@ -122,41 +100,46 @@ where
}
/// Sets the message that will be produced when the [`Button`] is pressed.
- /// If on_press isn't set, button will be disabled.
+ ///
+ /// Unless `on_press` is called, the [`Button`] will be disabled.
pub fn on_press(mut self, msg: Message) -> Self {
self.on_press = Some(msg);
self
}
- /// Sets the style of the [`Button`].
+ /// Sets the style variant of this [`Button`].
pub fn style(
mut self,
- style_sheet: impl Into<Box<dyn StyleSheet + 'a>>,
+ style: <Renderer::Theme as StyleSheet>::Style,
) -> Self {
- self.style_sheet = style_sheet.into();
+ self.style = style;
self
}
}
-/// The local state of a [`Button`].
-#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
-pub struct State {
- is_pressed: bool,
-}
-
-impl State {
- /// Creates a new [`State`].
- pub fn new() -> State {
- State::default()
- }
-}
-
impl<'a, Message, Renderer> Widget<Message, Renderer>
for Button<'a, Message, Renderer>
where
- Message: Clone,
- Renderer: crate::Renderer,
+ Message: 'a + Clone,
+ Renderer: 'a + crate::Renderer,
+ Renderer::Theme: StyleSheet,
{
+ fn tag(&self) -> tree::Tag {
+ tree::Tag::of::<State>()
+ }
+
+ fn state(&self) -> tree::State {
+ tree::State::new(State::new())
+ }
+
+ fn children(&self) -> Vec<Tree> {
+ vec![Tree::new(&self.content)]
+ }
+
+ fn diff(&self, tree: &mut Tree) {
+ tree.diff_children(std::slice::from_ref(&self.content))
+ }
+
fn width(&self) -> Length {
self.width
}
@@ -170,26 +153,36 @@ where
renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
- let limits = limits
- .min_width(self.min_width)
- .min_height(self.min_height)
- .width(self.width)
- .height(self.height)
- .pad(self.padding);
-
- let mut content = self.content.layout(renderer, &limits);
- content.move_to(Point::new(
- self.padding.left.into(),
- self.padding.top.into(),
- ));
-
- let size = limits.resolve(content.size()).pad(self.padding);
-
- layout::Node::with_children(size, vec![content])
+ layout(
+ renderer,
+ limits,
+ self.width,
+ self.height,
+ self.padding,
+ |renderer, limits| {
+ self.content.as_widget().layout(renderer, limits)
+ },
+ )
+ }
+
+ fn operate(
+ &self,
+ tree: &mut Tree,
+ layout: Layout<'_>,
+ operation: &mut dyn Operation<Message>,
+ ) {
+ operation.container(None, &mut |operation| {
+ self.content.as_widget().operate(
+ &mut tree.children[0],
+ layout.children().next().unwrap(),
+ operation,
+ );
+ });
}
fn on_event(
&mut self,
+ tree: &mut Tree,
event: Event,
layout: Layout<'_>,
cursor_position: Point,
@@ -197,7 +190,8 @@ where
clipboard: &mut dyn Clipboard,
shell: &mut Shell<'_, Message>,
) -> event::Status {
- if let event::Status::Captured = self.content.on_event(
+ if let event::Status::Captured = self.content.as_widget_mut().on_event(
+ &mut tree.children[0],
event.clone(),
layout.children().next().unwrap(),
cursor_position,
@@ -208,64 +202,21 @@ where
return event::Status::Captured;
}
- match event {
- Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
- | Event::Touch(touch::Event::FingerPressed { .. }) => {
- if self.on_press.is_some() {
- let bounds = layout.bounds();
-
- if bounds.contains(cursor_position) {
- self.state.is_pressed = true;
-
- return event::Status::Captured;
- }
- }
- }
- Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left))
- | Event::Touch(touch::Event::FingerLifted { .. }) => {
- if let Some(on_press) = self.on_press.clone() {
- let bounds = layout.bounds();
-
- if self.state.is_pressed {
- self.state.is_pressed = false;
-
- if bounds.contains(cursor_position) {
- shell.publish(on_press);
- }
-
- return event::Status::Captured;
- }
- }
- }
- Event::Touch(touch::Event::FingerLost { .. }) => {
- self.state.is_pressed = false;
- }
- _ => {}
- }
-
- event::Status::Ignored
- }
-
- fn mouse_interaction(
- &self,
- layout: Layout<'_>,
- cursor_position: Point,
- _viewport: &Rectangle,
- _renderer: &Renderer,
- ) -> mouse::Interaction {
- let is_mouse_over = layout.bounds().contains(cursor_position);
- let is_disabled = self.on_press.is_none();
-
- if is_mouse_over && !is_disabled {
- mouse::Interaction::Pointer
- } else {
- mouse::Interaction::default()
- }
+ update(
+ event,
+ layout,
+ cursor_position,
+ shell,
+ &self.on_press,
+ || tree.state.downcast_mut::<State>(),
+ )
}
fn draw(
&self,
+ tree: &Tree,
renderer: &mut Renderer,
+ theme: &Renderer::Theme,
_style: &renderer::Style,
layout: Layout<'_>,
cursor_position: Point,
@@ -274,54 +225,20 @@ where
let bounds = layout.bounds();
let content_layout = layout.children().next().unwrap();
- let is_mouse_over = bounds.contains(cursor_position);
- let is_disabled = self.on_press.is_none();
-
- let styling = if is_disabled {
- self.style_sheet.disabled()
- } else if is_mouse_over {
- if self.state.is_pressed {
- self.style_sheet.pressed()
- } else {
- self.style_sheet.hovered()
- }
- } else {
- self.style_sheet.active()
- };
-
- if styling.background.is_some() || styling.border_width > 0.0 {
- if styling.shadow_offset != Vector::default() {
- // TODO: Implement proper shadow support
- renderer.fill_quad(
- renderer::Quad {
- bounds: Rectangle {
- x: bounds.x + styling.shadow_offset.x,
- y: bounds.y + styling.shadow_offset.y,
- ..bounds
- },
- border_radius: styling.border_radius,
- border_width: 0.0,
- border_color: Color::TRANSPARENT,
- },
- Background::Color([0.0, 0.0, 0.0, 0.5].into()),
- );
- }
-
- renderer.fill_quad(
- renderer::Quad {
- bounds,
- border_radius: styling.border_radius,
- border_width: styling.border_width,
- border_color: styling.border_color,
- },
- styling
- .background
- .unwrap_or(Background::Color(Color::TRANSPARENT)),
- );
- }
+ let styling = draw(
+ renderer,
+ bounds,
+ cursor_position,
+ self.on_press.is_some(),
+ theme,
+ self.style,
+ || tree.state.downcast_ref::<State>(),
+ );
- self.content.draw(
+ self.content.as_widget().draw(
+ &tree.children[0],
renderer,
+ theme,
&renderer::Style {
text_color: styling.text_color,
},
@@ -331,25 +248,205 @@ where
);
}
- fn overlay(
- &mut self,
+ fn mouse_interaction(
+ &self,
+ _tree: &Tree,
+ layout: Layout<'_>,
+ cursor_position: Point,
+ _viewport: &Rectangle,
+ _renderer: &Renderer,
+ ) -> mouse::Interaction {
+ mouse_interaction(layout, cursor_position, self.on_press.is_some())
+ }
+
+ fn overlay<'b>(
+ &'b self,
+ tree: &'b mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
- ) -> Option<overlay::Element<'_, Message, Renderer>> {
- self.content
- .overlay(layout.children().next().unwrap(), renderer)
+ ) -> Option<overlay::Element<'b, Message, Renderer>> {
+ self.content.as_widget().overlay(
+ &mut tree.children[0],
+ layout.children().next().unwrap(),
+ renderer,
+ )
}
}
impl<'a, Message, Renderer> From<Button<'a, Message, Renderer>>
for Element<'a, Message, Renderer>
where
- Message: 'a + Clone,
- Renderer: 'a + crate::Renderer,
+ Message: Clone + 'a,
+ Renderer: crate::Renderer + 'a,
+ Renderer::Theme: StyleSheet,
{
- fn from(
- button: Button<'a, Message, Renderer>,
- ) -> Element<'a, Message, Renderer> {
- Element::new(button)
+ fn from(button: Button<'a, Message, Renderer>) -> Self {
+ Self::new(button)
+ }
+}
+
+/// The local state of a [`Button`].
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
+pub struct State {
+ is_pressed: bool,
+}
+
+impl State {
+ /// Creates a new [`State`].
+ pub fn new() -> State {
+ State::default()
+ }
+}
+
+/// Processes the given [`Event`] and updates the [`State`] of a [`Button`]
+/// accordingly.
+pub fn update<'a, Message: Clone>(
+ event: Event,
+ layout: Layout<'_>,
+ cursor_position: Point,
+ shell: &mut Shell<'_, Message>,
+ on_press: &Option<Message>,
+ state: impl FnOnce() -> &'a mut State,
+) -> event::Status {
+ match event {
+ Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
+ | Event::Touch(touch::Event::FingerPressed { .. }) => {
+ if on_press.is_some() {
+ let bounds = layout.bounds();
+
+ if bounds.contains(cursor_position) {
+ let state = state();
+
+ state.is_pressed = true;
+
+ return event::Status::Captured;
+ }
+ }
+ }
+ Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left))
+ | Event::Touch(touch::Event::FingerLifted { .. }) => {
+ if let Some(on_press) = on_press.clone() {
+ let state = state();
+
+ if state.is_pressed {
+ state.is_pressed = false;
+
+ let bounds = layout.bounds();
+
+ if bounds.contains(cursor_position) {
+ shell.publish(on_press);
+ }
+
+ return event::Status::Captured;
+ }
+ }
+ }
+ Event::Touch(touch::Event::FingerLost { .. }) => {
+ let state = state();
+
+ state.is_pressed = false;
+ }
+ _ => {}
+ }
+
+ event::Status::Ignored
+}
+
+/// Draws a [`Button`].
+pub fn draw<'a, Renderer: crate::Renderer>(
+ renderer: &mut Renderer,
+ bounds: Rectangle,
+ cursor_position: Point,
+ is_enabled: bool,
+ style_sheet: &dyn StyleSheet<
+ Style = <Renderer::Theme as StyleSheet>::Style,
+ >,
+ style: <Renderer::Theme as StyleSheet>::Style,
+ state: impl FnOnce() -> &'a State,
+) -> Appearance
+where
+ Renderer::Theme: StyleSheet,
+{
+ let is_mouse_over = bounds.contains(cursor_position);
+
+ let styling = if !is_enabled {
+ style_sheet.disabled(style)
+ } else if is_mouse_over {
+ let state = state();
+
+ if state.is_pressed {
+ style_sheet.pressed(style)
+ } else {
+ style_sheet.hovered(style)
+ }
+ } else {
+ style_sheet.active(style)
+ };
+
+ if styling.background.is_some() || styling.border_width > 0.0 {
+ if styling.shadow_offset != Vector::default() {
+ // TODO: Implement proper shadow support
+ renderer.fill_quad(
+ renderer::Quad {
+ bounds: Rectangle {
+ x: bounds.x + styling.shadow_offset.x,
+ y: bounds.y + styling.shadow_offset.y,
+ ..bounds
+ },
+ border_radius: styling.border_radius,
+ border_width: 0.0,
+ border_color: Color::TRANSPARENT,
+ },
+ Background::Color([0.0, 0.0, 0.0, 0.5].into()),
+ );
+ }
+
+ renderer.fill_quad(
+ renderer::Quad {
+ bounds,
+ border_radius: styling.border_radius,
+ border_width: styling.border_width,
+ border_color: styling.border_color,
+ },
+ styling
+ .background
+ .unwrap_or(Background::Color(Color::TRANSPARENT)),
+ );
+ }
+
+ styling
+}
+
+/// Computes the layout of a [`Button`].
+pub fn layout<Renderer>(
+ renderer: &Renderer,
+ limits: &layout::Limits,
+ width: Length,
+ height: Length,
+ padding: Padding,
+ layout_content: impl FnOnce(&Renderer, &layout::Limits) -> layout::Node,
+) -> layout::Node {
+ let limits = limits.width(width).height(height).pad(padding);
+
+ let mut content = layout_content(renderer, &limits);
+ content.move_to(Point::new(padding.left.into(), padding.top.into()));
+
+ let size = limits.resolve(content.size()).pad(padding);
+
+ layout::Node::with_children(size, vec![content])
+}
+
+/// Returns the [`mouse::Interaction`] of a [`Button`].
+pub fn mouse_interaction(
+ layout: Layout<'_>,
+ cursor_position: Point,
+ is_enabled: bool,
+) -> mouse::Interaction {
+ let is_mouse_over = layout.bounds().contains(cursor_position);
+
+ if is_mouse_over && is_enabled {
+ mouse::Interaction::Pointer
+ } else {
+ mouse::Interaction::default()
}
}
diff --git a/native/src/widget/checkbox.rs b/native/src/widget/checkbox.rs
index 15cbf93a..dc3c0bd0 100644
--- a/native/src/widget/checkbox.rs
+++ b/native/src/widget/checkbox.rs
@@ -6,13 +6,13 @@ use crate::mouse;
use crate::renderer;
use crate::text;
use crate::touch;
-use crate::widget::{self, Row, Text};
+use crate::widget::{self, Row, Text, Tree};
use crate::{
Alignment, Clipboard, Element, Layout, Length, Point, Rectangle, Shell,
Widget,
};
-pub use iced_style::checkbox::{Style, StyleSheet};
+pub use iced_style::checkbox::{Appearance, StyleSheet};
/// A box that can be checked.
///
@@ -32,19 +32,27 @@ pub use iced_style::checkbox::{Style, StyleSheet};
///
/// ![Checkbox drawn by `iced_wgpu`](https://github.com/iced-rs/iced/blob/7760618fb112074bc40b148944521f312152012a/docs/images/checkbox.png?raw=true)
#[allow(missing_debug_implementations)]
-pub struct Checkbox<'a, Message, Renderer: text::Renderer> {
+pub struct Checkbox<'a, Message, Renderer>
+where
+ Renderer: text::Renderer,
+ Renderer::Theme: StyleSheet + widget::text::StyleSheet,
+{
is_checked: bool,
- on_toggle: Box<dyn Fn(bool) -> Message>,
+ on_toggle: Box<dyn Fn(bool) -> Message + 'a>,
label: String,
width: Length,
size: u16,
spacing: u16,
text_size: Option<u16>,
font: Renderer::Font,
- style_sheet: Box<dyn StyleSheet + 'a>,
+ style: <Renderer::Theme as StyleSheet>::Style,
}
-impl<'a, Message, Renderer: text::Renderer> Checkbox<'a, Message, Renderer> {
+impl<'a, Message, Renderer> Checkbox<'a, Message, Renderer>
+where
+ Renderer: text::Renderer,
+ Renderer::Theme: StyleSheet + widget::text::StyleSheet,
+{
/// The default size of a [`Checkbox`].
const DEFAULT_SIZE: u16 = 20;
@@ -61,7 +69,7 @@ impl<'a, Message, Renderer: text::Renderer> Checkbox<'a, Message, Renderer> {
/// `Message`.
pub fn new<F>(is_checked: bool, label: impl Into<String>, f: F) -> Self
where
- F: 'static + Fn(bool) -> Message,
+ F: 'a + Fn(bool) -> Message,
{
Checkbox {
is_checked,
@@ -72,7 +80,7 @@ impl<'a, Message, Renderer: text::Renderer> Checkbox<'a, Message, Renderer> {
spacing: Self::DEFAULT_SPACING,
text_size: None,
font: Renderer::Font::default(),
- style_sheet: Default::default(),
+ style: Default::default(),
}
}
@@ -102,7 +110,7 @@ impl<'a, Message, Renderer: text::Renderer> Checkbox<'a, Message, Renderer> {
/// Sets the [`Font`] of the text of the [`Checkbox`].
///
- /// [`Font`]: crate::widget::text::Renderer::Font
+ /// [`Font`]: crate::text::Renderer::Font
pub fn font(mut self, font: Renderer::Font) -> Self {
self.font = font;
self
@@ -111,9 +119,9 @@ impl<'a, Message, Renderer: text::Renderer> Checkbox<'a, Message, Renderer> {
/// Sets the style of the [`Checkbox`].
pub fn style(
mut self,
- style_sheet: impl Into<Box<dyn StyleSheet + 'a>>,
+ style: impl Into<<Renderer::Theme as StyleSheet>::Style>,
) -> Self {
- self.style_sheet = style_sheet.into();
+ self.style = style.into();
self
}
}
@@ -122,6 +130,7 @@ impl<'a, Message, Renderer> Widget<Message, Renderer>
for Checkbox<'a, Message, Renderer>
where
Renderer: text::Renderer,
+ Renderer::Theme: StyleSheet + widget::text::StyleSheet,
{
fn width(&self) -> Length {
self.width
@@ -149,13 +158,17 @@ where
Text::new(&self.label)
.font(self.font.clone())
.width(self.width)
- .size(self.text_size.unwrap_or(renderer.default_size())),
+ .size(
+ self.text_size
+ .unwrap_or_else(|| renderer.default_size()),
+ ),
)
.layout(renderer, limits)
}
fn on_event(
&mut self,
+ _tree: &mut Tree,
event: Event,
layout: Layout<'_>,
cursor_position: Point,
@@ -182,6 +195,7 @@ where
fn mouse_interaction(
&self,
+ _tree: &Tree,
layout: Layout<'_>,
cursor_position: Point,
_viewport: &Rectangle,
@@ -196,7 +210,9 @@ where
fn draw(
&self,
+ _tree: &Tree,
renderer: &mut Renderer,
+ theme: &Renderer::Theme,
style: &renderer::Style,
layout: Layout<'_>,
cursor_position: Point,
@@ -208,9 +224,9 @@ where
let mut children = layout.children();
let custom_style = if is_mouse_over {
- self.style_sheet.hovered(self.is_checked)
+ theme.hovered(self.style, self.is_checked)
} else {
- self.style_sheet.active(self.is_checked)
+ theme.active(self.style, self.is_checked)
};
{
@@ -252,9 +268,11 @@ where
style,
label_layout,
&self.label,
- self.font.clone(),
self.text_size,
- custom_style.text_color,
+ self.font.clone(),
+ widget::text::Appearance {
+ color: custom_style.text_color,
+ },
alignment::Horizontal::Left,
alignment::Vertical::Center,
);
@@ -265,8 +283,9 @@ where
impl<'a, Message, Renderer> From<Checkbox<'a, Message, Renderer>>
for Element<'a, Message, Renderer>
where
- Renderer: 'a + text::Renderer,
Message: 'a,
+ Renderer: 'a + text::Renderer,
+ Renderer::Theme: StyleSheet + widget::text::StyleSheet,
{
fn from(
checkbox: Checkbox<'a, Message, Renderer>,
diff --git a/native/src/widget/column.rs b/native/src/widget/column.rs
index f161d1f2..a8b0f183 100644
--- a/native/src/widget/column.rs
+++ b/native/src/widget/column.rs
@@ -4,6 +4,7 @@ use crate::layout;
use crate::mouse;
use crate::overlay;
use crate::renderer;
+use crate::widget::{Operation, Tree};
use crate::{
Alignment, Clipboard, Element, Layout, Length, Padding, Point, Rectangle,
Shell, Widget,
@@ -19,7 +20,6 @@ pub struct Column<'a, Message, Renderer> {
width: Length,
height: Length,
max_width: u32,
- max_height: u32,
align_items: Alignment,
children: Vec<Element<'a, Message, Renderer>>,
}
@@ -40,7 +40,6 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> {
width: Length::Shrink,
height: Length::Shrink,
max_width: u32::MAX,
- max_height: u32::MAX,
align_items: Alignment::Start,
children,
}
@@ -48,7 +47,7 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> {
/// Sets the vertical spacing _between_ elements.
///
- /// Custom margins per element do not exist in Iced. You should use this
+ /// Custom margins per element do not exist in iced. You should use this
/// method instead! While less flexible, it helps you keep spacing between
/// elements consistent.
pub fn spacing(mut self, units: u16) -> Self {
@@ -80,12 +79,6 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> {
self
}
- /// Sets the maximum height of the [`Column`] in pixels.
- pub fn max_height(mut self, max_height: u32) -> Self {
- self.max_height = max_height;
- self
- }
-
/// Sets the horizontal alignment of the contents of the [`Column`] .
pub fn align_items(mut self, align: Alignment) -> Self {
self.align_items = align;
@@ -93,20 +86,34 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> {
}
/// Adds an element to the [`Column`].
- pub fn push<E>(mut self, child: E) -> Self
- where
- E: Into<Element<'a, Message, Renderer>>,
- {
+ pub fn push(
+ mut self,
+ child: impl Into<Element<'a, Message, Renderer>>,
+ ) -> Self {
self.children.push(child.into());
self
}
}
+impl<'a, Message, Renderer> Default for Column<'a, Message, Renderer> {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
impl<'a, Message, Renderer> Widget<Message, Renderer>
for Column<'a, Message, Renderer>
where
Renderer: crate::Renderer,
{
+ fn children(&self) -> Vec<Tree> {
+ self.children.iter().map(Tree::new).collect()
+ }
+
+ fn diff(&self, tree: &mut Tree) {
+ tree.diff_children(&self.children);
+ }
+
fn width(&self) -> Length {
self.width
}
@@ -122,7 +129,6 @@ where
) -> layout::Node {
let limits = limits
.max_width(self.max_width)
- .max_height(self.max_height)
.width(self.width)
.height(self.height);
@@ -137,8 +143,26 @@ where
)
}
+ fn operate(
+ &self,
+ tree: &mut Tree,
+ layout: Layout<'_>,
+ operation: &mut dyn Operation<Message>,
+ ) {
+ operation.container(None, &mut |operation| {
+ self.children
+ .iter()
+ .zip(&mut tree.children)
+ .zip(layout.children())
+ .for_each(|((child, state), layout)| {
+ child.as_widget().operate(state, layout, operation);
+ })
+ });
+ }
+
fn on_event(
&mut self,
+ tree: &mut Tree,
event: Event,
layout: Layout<'_>,
cursor_position: Point,
@@ -148,9 +172,11 @@ where
) -> event::Status {
self.children
.iter_mut()
+ .zip(&mut tree.children)
.zip(layout.children())
- .map(|(child, layout)| {
- child.widget.on_event(
+ .map(|((child, state), layout)| {
+ child.as_widget_mut().on_event(
+ state,
event.clone(),
layout,
cursor_position,
@@ -164,6 +190,7 @@ where
fn mouse_interaction(
&self,
+ tree: &Tree,
layout: Layout<'_>,
cursor_position: Point,
viewport: &Rectangle,
@@ -171,9 +198,11 @@ where
) -> mouse::Interaction {
self.children
.iter()
+ .zip(&tree.children)
.zip(layout.children())
- .map(|(child, layout)| {
- child.widget.mouse_interaction(
+ .map(|((child, state), layout)| {
+ child.as_widget().mouse_interaction(
+ state,
layout,
cursor_position,
viewport,
@@ -186,41 +215,49 @@ where
fn draw(
&self,
+ tree: &Tree,
renderer: &mut Renderer,
+ theme: &Renderer::Theme,
style: &renderer::Style,
layout: Layout<'_>,
cursor_position: Point,
viewport: &Rectangle,
) {
- for (child, layout) in self.children.iter().zip(layout.children()) {
- child.draw(renderer, style, layout, cursor_position, viewport);
+ for ((child, state), layout) in self
+ .children
+ .iter()
+ .zip(&tree.children)
+ .zip(layout.children())
+ {
+ child.as_widget().draw(
+ state,
+ renderer,
+ theme,
+ style,
+ layout,
+ cursor_position,
+ viewport,
+ );
}
}
- fn overlay(
- &mut self,
+ fn overlay<'b>(
+ &'b self,
+ tree: &'b mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
- ) -> Option<overlay::Element<'_, Message, Renderer>> {
- self.children
- .iter_mut()
- .zip(layout.children())
- .filter_map(|(child, layout)| {
- child.widget.overlay(layout, renderer)
- })
- .next()
+ ) -> Option<overlay::Element<'b, Message, Renderer>> {
+ overlay::from_children(&self.children, tree, layout, renderer)
}
}
impl<'a, Message, Renderer> From<Column<'a, Message, Renderer>>
for Element<'a, Message, Renderer>
where
- Renderer: 'a + crate::Renderer,
Message: 'a,
+ Renderer: crate::Renderer + 'a,
{
- fn from(
- column: Column<'a, Message, Renderer>,
- ) -> Element<'a, Message, Renderer> {
- Element::new(column)
+ fn from(column: Column<'a, Message, Renderer>) -> Self {
+ Self::new(column)
}
}
diff --git a/native/src/widget/container.rs b/native/src/widget/container.rs
index ca85a425..2afad3f2 100644
--- a/native/src/widget/container.rs
+++ b/native/src/widget/container.rs
@@ -5,6 +5,7 @@ use crate::layout;
use crate::mouse;
use crate::overlay;
use crate::renderer;
+use crate::widget::{Operation, Tree};
use crate::{
Background, Clipboard, Color, Element, Layout, Length, Padding, Point,
Rectangle, Shell, Widget,
@@ -12,13 +13,17 @@ use crate::{
use std::u32;
-pub use iced_style::container::{Style, StyleSheet};
+pub use iced_style::container::{Appearance, StyleSheet};
/// An element decorating some content.
///
/// It is normally used for alignment purposes.
#[allow(missing_debug_implementations)]
-pub struct Container<'a, Message, Renderer> {
+pub struct Container<'a, Message, Renderer>
+where
+ Renderer: crate::Renderer,
+ Renderer::Theme: StyleSheet,
+{
padding: Padding,
width: Length,
height: Length,
@@ -26,13 +31,14 @@ pub struct Container<'a, Message, Renderer> {
max_height: u32,
horizontal_alignment: alignment::Horizontal,
vertical_alignment: alignment::Vertical,
- style_sheet: Box<dyn StyleSheet + 'a>,
+ style: <Renderer::Theme as StyleSheet>::Style,
content: Element<'a, Message, Renderer>,
}
impl<'a, Message, Renderer> Container<'a, Message, Renderer>
where
Renderer: crate::Renderer,
+ Renderer::Theme: StyleSheet,
{
/// Creates an empty [`Container`].
pub fn new<T>(content: T) -> Self
@@ -47,7 +53,7 @@ where
max_height: u32::MAX,
horizontal_alignment: alignment::Horizontal::Left,
vertical_alignment: alignment::Vertical::Top,
- style_sheet: Default::default(),
+ style: Default::default(),
content: content.into(),
}
}
@@ -109,9 +115,9 @@ where
/// Sets the style of the [`Container`].
pub fn style(
mut self,
- style_sheet: impl Into<Box<dyn StyleSheet + 'a>>,
+ style: impl Into<<Renderer::Theme as StyleSheet>::Style>,
) -> Self {
- self.style_sheet = style_sheet.into();
+ self.style = style.into();
self
}
}
@@ -120,7 +126,16 @@ impl<'a, Message, Renderer> Widget<Message, Renderer>
for Container<'a, Message, Renderer>
where
Renderer: crate::Renderer,
+ Renderer::Theme: StyleSheet,
{
+ fn children(&self) -> Vec<Tree> {
+ vec![Tree::new(&self.content)]
+ }
+
+ fn diff(&self, tree: &mut Tree) {
+ tree.diff_children(std::slice::from_ref(&self.content))
+ }
+
fn width(&self) -> Length {
self.width
}
@@ -134,32 +149,40 @@ where
renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
- let limits = limits
- .loose()
- .max_width(self.max_width)
- .max_height(self.max_height)
- .width(self.width)
- .height(self.height)
- .pad(self.padding);
-
- let mut content = self.content.layout(renderer, &limits.loose());
- let size = limits.resolve(content.size());
-
- content.move_to(Point::new(
- self.padding.left.into(),
- self.padding.top.into(),
- ));
- content.align(
- Alignment::from(self.horizontal_alignment),
- Alignment::from(self.vertical_alignment),
- size,
- );
+ layout(
+ renderer,
+ limits,
+ self.width,
+ self.height,
+ self.max_width,
+ self.max_height,
+ self.padding,
+ self.horizontal_alignment,
+ self.vertical_alignment,
+ |renderer, limits| {
+ self.content.as_widget().layout(renderer, limits)
+ },
+ )
+ }
- layout::Node::with_children(size.pad(self.padding), vec![content])
+ fn operate(
+ &self,
+ tree: &mut Tree,
+ layout: Layout<'_>,
+ operation: &mut dyn Operation<Message>,
+ ) {
+ operation.container(None, &mut |operation| {
+ self.content.as_widget().operate(
+ &mut tree.children[0],
+ layout.children().next().unwrap(),
+ operation,
+ );
+ });
}
fn on_event(
&mut self,
+ tree: &mut Tree,
event: Event,
layout: Layout<'_>,
cursor_position: Point,
@@ -167,7 +190,8 @@ where
clipboard: &mut dyn Clipboard,
shell: &mut Shell<'_, Message>,
) -> event::Status {
- self.content.widget.on_event(
+ self.content.as_widget_mut().on_event(
+ &mut tree.children[0],
event,
layout.children().next().unwrap(),
cursor_position,
@@ -179,12 +203,14 @@ where
fn mouse_interaction(
&self,
+ tree: &Tree,
layout: Layout<'_>,
cursor_position: Point,
viewport: &Rectangle,
renderer: &Renderer,
) -> mouse::Interaction {
- self.content.widget.mouse_interaction(
+ self.content.as_widget().mouse_interaction(
+ &tree.children[0],
layout.children().next().unwrap(),
cursor_position,
viewport,
@@ -194,18 +220,22 @@ where
fn draw(
&self,
+ tree: &Tree,
renderer: &mut Renderer,
+ theme: &Renderer::Theme,
renderer_style: &renderer::Style,
layout: Layout<'_>,
cursor_position: Point,
viewport: &Rectangle,
) {
- let style = self.style_sheet.style();
+ let style = theme.appearance(self.style);
draw_background(renderer, &style, layout.bounds());
- self.content.draw(
+ self.content.as_widget().draw(
+ &tree.children[0],
renderer,
+ theme,
&renderer::Style {
text_color: style
.text_color
@@ -217,48 +247,87 @@ where
);
}
- fn overlay(
- &mut self,
+ fn overlay<'b>(
+ &'b self,
+ tree: &'b mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
- ) -> Option<overlay::Element<'_, Message, Renderer>> {
- self.content
- .overlay(layout.children().next().unwrap(), renderer)
+ ) -> Option<overlay::Element<'b, Message, Renderer>> {
+ self.content.as_widget().overlay(
+ &mut tree.children[0],
+ layout.children().next().unwrap(),
+ renderer,
+ )
}
}
+impl<'a, Message, Renderer> From<Container<'a, Message, Renderer>>
+ for Element<'a, Message, Renderer>
+where
+ Message: 'a,
+ Renderer: 'a + crate::Renderer,
+ Renderer::Theme: StyleSheet,
+{
+ fn from(
+ column: Container<'a, Message, Renderer>,
+ ) -> Element<'a, Message, Renderer> {
+ Element::new(column)
+ }
+}
+
+/// Computes the layout of a [`Container`].
+pub fn layout<Renderer>(
+ renderer: &Renderer,
+ limits: &layout::Limits,
+ width: Length,
+ height: Length,
+ max_width: u32,
+ max_height: u32,
+ padding: Padding,
+ horizontal_alignment: alignment::Horizontal,
+ vertical_alignment: alignment::Vertical,
+ layout_content: impl FnOnce(&Renderer, &layout::Limits) -> layout::Node,
+) -> layout::Node {
+ let limits = limits
+ .loose()
+ .max_width(max_width)
+ .max_height(max_height)
+ .width(width)
+ .height(height)
+ .pad(padding);
+
+ let mut content = layout_content(renderer, &limits.loose());
+ let size = limits.resolve(content.size());
+
+ content.move_to(Point::new(padding.left.into(), padding.top.into()));
+ content.align(
+ Alignment::from(horizontal_alignment),
+ Alignment::from(vertical_alignment),
+ size,
+ );
+
+ layout::Node::with_children(size.pad(padding), vec![content])
+}
+
/// Draws the background of a [`Container`] given its [`Style`] and its `bounds`.
pub fn draw_background<Renderer>(
renderer: &mut Renderer,
- style: &Style,
+ appearance: &Appearance,
bounds: Rectangle,
) where
Renderer: crate::Renderer,
{
- if style.background.is_some() || style.border_width > 0.0 {
+ if appearance.background.is_some() || appearance.border_width > 0.0 {
renderer.fill_quad(
renderer::Quad {
bounds,
- border_radius: style.border_radius,
- border_width: style.border_width,
- border_color: style.border_color,
+ border_radius: appearance.border_radius,
+ border_width: appearance.border_width,
+ border_color: appearance.border_color,
},
- style
+ appearance
.background
.unwrap_or(Background::Color(Color::TRANSPARENT)),
);
}
}
-
-impl<'a, Message, Renderer> From<Container<'a, Message, Renderer>>
- for Element<'a, Message, Renderer>
-where
- Renderer: 'a + crate::Renderer,
- Message: 'a,
-{
- fn from(
- column: Container<'a, Message, Renderer>,
- ) -> Element<'a, Message, Renderer> {
- Element::new(column)
- }
-}
diff --git a/native/src/widget/helpers.rs b/native/src/widget/helpers.rs
new file mode 100644
index 00000000..79751878
--- /dev/null
+++ b/native/src/widget/helpers.rs
@@ -0,0 +1,283 @@
+//! Helper functions to create pure widgets.
+use crate::widget;
+use crate::{Element, Length};
+
+use std::borrow::Cow;
+use std::ops::RangeInclusive;
+
+/// Creates a [`Column`] with the given children.
+///
+/// [`Column`]: widget::Column
+#[macro_export]
+macro_rules! column {
+ () => (
+ $crate::widget::Column::new()
+ );
+ ($($x:expr),+ $(,)?) => (
+ $crate::widget::Column::with_children(vec![$($crate::Element::from($x)),+])
+ );
+}
+
+/// Creates a [Row`] with the given children.
+///
+/// [`Row`]: widget::Row
+#[macro_export]
+macro_rules! row {
+ () => (
+ $crate::widget::Row::new()
+ );
+ ($($x:expr),+ $(,)?) => (
+ $crate::widget::Row::with_children(vec![$($crate::Element::from($x)),+])
+ );
+}
+
+/// Creates a new [`Container`] with the provided content.
+///
+/// [`Container`]: widget::Container
+pub fn container<'a, Message, Renderer>(
+ content: impl Into<Element<'a, Message, Renderer>>,
+) -> widget::Container<'a, Message, Renderer>
+where
+ Renderer: crate::Renderer,
+ Renderer::Theme: widget::container::StyleSheet,
+{
+ widget::Container::new(content)
+}
+
+/// Creates a new [`Column`] with the given children.
+///
+/// [`Column`]: widget::Column
+pub fn column<Message, Renderer>(
+ children: Vec<Element<'_, Message, Renderer>>,
+) -> widget::Column<'_, Message, Renderer> {
+ widget::Column::with_children(children)
+}
+
+/// Creates a new [`Row`] with the given children.
+///
+/// [`Row`]: widget::Row
+pub fn row<Message, Renderer>(
+ children: Vec<Element<'_, Message, Renderer>>,
+) -> widget::Row<'_, Message, Renderer> {
+ widget::Row::with_children(children)
+}
+
+/// Creates a new [`Scrollable`] with the provided content.
+///
+/// [`Scrollable`]: widget::Scrollable
+pub fn scrollable<'a, Message, Renderer>(
+ content: impl Into<Element<'a, Message, Renderer>>,
+) -> widget::Scrollable<'a, Message, Renderer>
+where
+ Renderer: crate::Renderer,
+ Renderer::Theme: widget::scrollable::StyleSheet,
+{
+ widget::Scrollable::new(content)
+}
+
+/// Creates a new [`Button`] with the provided content.
+///
+/// [`Button`]: widget::Button
+pub fn button<'a, Message, Renderer>(
+ content: impl Into<Element<'a, Message, Renderer>>,
+) -> widget::Button<'a, Message, Renderer>
+where
+ Renderer: crate::Renderer,
+ Renderer::Theme: widget::button::StyleSheet,
+{
+ widget::Button::new(content)
+}
+
+/// Creates a new [`Tooltip`] with the provided content, tooltip text, and [`tooltip::Position`].
+///
+/// [`Tooltip`]: widget::Tooltip
+/// [`tooltip::Position`]: widget::tooltip::Position
+pub fn tooltip<'a, Message, Renderer>(
+ content: impl Into<Element<'a, Message, Renderer>>,
+ tooltip: impl ToString,
+ position: widget::tooltip::Position,
+) -> widget::Tooltip<'a, Message, Renderer>
+where
+ Renderer: crate::text::Renderer,
+ Renderer::Theme: widget::container::StyleSheet + widget::text::StyleSheet,
+{
+ widget::Tooltip::new(content, tooltip.to_string(), position)
+}
+
+/// Creates a new [`Text`] widget with the provided content.
+///
+/// [`Text`]: widget::Text
+pub fn text<'a, Renderer>(text: impl ToString) -> widget::Text<'a, Renderer>
+where
+ Renderer: crate::text::Renderer,
+ Renderer::Theme: widget::text::StyleSheet,
+{
+ widget::Text::new(text.to_string())
+}
+
+/// Creates a new [`Checkbox`].
+///
+/// [`Checkbox`]: widget::Checkbox
+pub fn checkbox<'a, Message, Renderer>(
+ label: impl Into<String>,
+ is_checked: bool,
+ f: impl Fn(bool) -> Message + 'a,
+) -> widget::Checkbox<'a, Message, Renderer>
+where
+ Renderer: crate::text::Renderer,
+ Renderer::Theme: widget::checkbox::StyleSheet + widget::text::StyleSheet,
+{
+ widget::Checkbox::new(is_checked, label, f)
+}
+
+/// Creates a new [`Radio`].
+///
+/// [`Radio`]: widget::Radio
+pub fn radio<Message, Renderer, V>(
+ label: impl Into<String>,
+ value: V,
+ selected: Option<V>,
+ on_click: impl FnOnce(V) -> Message,
+) -> widget::Radio<Message, Renderer>
+where
+ Message: Clone,
+ Renderer: crate::text::Renderer,
+ Renderer::Theme: widget::radio::StyleSheet,
+ V: Copy + Eq,
+{
+ widget::Radio::new(value, label, selected, on_click)
+}
+
+/// Creates a new [`Toggler`].
+///
+/// [`Toggler`]: widget::Toggler
+pub fn toggler<'a, Message, Renderer>(
+ label: impl Into<Option<String>>,
+ is_checked: bool,
+ f: impl Fn(bool) -> Message + 'a,
+) -> widget::Toggler<'a, Message, Renderer>
+where
+ Renderer: crate::text::Renderer,
+ Renderer::Theme: widget::toggler::StyleSheet,
+{
+ widget::Toggler::new(is_checked, label, f)
+}
+
+/// Creates a new [`TextInput`].
+///
+/// [`TextInput`]: widget::TextInput
+pub fn text_input<'a, Message, Renderer>(
+ placeholder: &str,
+ value: &str,
+ on_change: impl Fn(String) -> Message + 'a,
+) -> widget::TextInput<'a, Message, Renderer>
+where
+ Message: Clone,
+ Renderer: crate::text::Renderer,
+ Renderer::Theme: widget::text_input::StyleSheet,
+{
+ widget::TextInput::new(placeholder, value, on_change)
+}
+
+/// Creates a new [`Slider`].
+///
+/// [`Slider`]: widget::Slider
+pub fn slider<'a, T, Message, Renderer>(
+ range: std::ops::RangeInclusive<T>,
+ value: T,
+ on_change: impl Fn(T) -> Message + 'a,
+) -> widget::Slider<'a, T, Message, Renderer>
+where
+ T: Copy + From<u8> + std::cmp::PartialOrd,
+ Message: Clone,
+ Renderer: crate::Renderer,
+ Renderer::Theme: widget::slider::StyleSheet,
+{
+ widget::Slider::new(range, value, on_change)
+}
+
+/// Creates a new [`PickList`].
+///
+/// [`PickList`]: widget::PickList
+pub fn pick_list<'a, Message, Renderer, T>(
+ options: impl Into<Cow<'a, [T]>>,
+ selected: Option<T>,
+ on_selected: impl Fn(T) -> Message + 'a,
+) -> widget::PickList<'a, T, Message, Renderer>
+where
+ T: ToString + Eq + 'static,
+ [T]: ToOwned<Owned = Vec<T>>,
+ Renderer: crate::text::Renderer,
+ Renderer::Theme: widget::pick_list::StyleSheet,
+{
+ widget::PickList::new(options, selected, on_selected)
+}
+
+/// Creates a new [`Image`].
+///
+/// [`Image`]: widget::Image
+pub fn image<Handle>(handle: impl Into<Handle>) -> widget::Image<Handle> {
+ widget::Image::new(handle.into())
+}
+
+/// Creates a new horizontal [`Space`] with the given [`Length`].
+///
+/// [`Space`]: widget::Space
+pub fn horizontal_space(width: Length) -> widget::Space {
+ widget::Space::with_width(width)
+}
+
+/// Creates a new vertical [`Space`] with the given [`Length`].
+///
+/// [`Space`]: widget::Space
+pub fn vertical_space(height: Length) -> widget::Space {
+ widget::Space::with_height(height)
+}
+
+/// Creates a horizontal [`Rule`] with the given height.
+///
+/// [`Rule`]: widget::Rule
+pub fn horizontal_rule<Renderer>(height: u16) -> widget::Rule<Renderer>
+where
+ Renderer: crate::Renderer,
+ Renderer::Theme: widget::rule::StyleSheet,
+{
+ widget::Rule::horizontal(height)
+}
+
+/// Creates a vertical [`Rule`] with the given width.
+///
+/// [`Rule`]: widget::Rule
+pub fn vertical_rule<Renderer>(width: u16) -> widget::Rule<Renderer>
+where
+ Renderer: crate::Renderer,
+ Renderer::Theme: widget::rule::StyleSheet,
+{
+ widget::Rule::vertical(width)
+}
+
+/// Creates a new [`ProgressBar`].
+///
+/// It expects:
+/// * an inclusive range of possible values, and
+/// * the current value of the [`ProgressBar`].
+///
+/// [`ProgressBar`]: widget::ProgressBar
+pub fn progress_bar<Renderer>(
+ range: RangeInclusive<f32>,
+ value: f32,
+) -> widget::ProgressBar<Renderer>
+where
+ Renderer: crate::Renderer,
+ Renderer::Theme: widget::progress_bar::StyleSheet,
+{
+ widget::ProgressBar::new(range, value)
+}
+
+/// Creates a new [`Svg`] widget from the given [`Handle`].
+///
+/// [`Svg`]: widget::Svg
+/// [`Handle`]: widget::svg::Handle
+pub fn svg(handle: impl Into<widget::svg::Handle>) -> widget::Svg {
+ widget::Svg::new(handle)
+}
diff --git a/native/src/widget/id.rs b/native/src/widget/id.rs
new file mode 100644
index 00000000..4b8fedf1
--- /dev/null
+++ b/native/src/widget/id.rs
@@ -0,0 +1,43 @@
+use std::borrow;
+use std::sync::atomic::{self, AtomicUsize};
+
+static NEXT_ID: AtomicUsize = AtomicUsize::new(0);
+
+/// The identifier of a generic widget.
+#[derive(Debug, Clone, PartialEq, Eq, Hash)]
+pub struct Id(Internal);
+
+impl Id {
+ /// Creates a custom [`Id`].
+ pub fn new(id: impl Into<borrow::Cow<'static, str>>) -> Self {
+ Self(Internal::Custom(id.into()))
+ }
+
+ /// Creates a unique [`Id`].
+ ///
+ /// This function produces a different [`Id`] every time it is called.
+ pub fn unique() -> Self {
+ let id = NEXT_ID.fetch_add(1, atomic::Ordering::Relaxed);
+
+ Self(Internal::Unique(id))
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq, Hash)]
+pub enum Internal {
+ Unique(usize),
+ Custom(borrow::Cow<'static, str>),
+}
+
+#[cfg(test)]
+mod tests {
+ use super::Id;
+
+ #[test]
+ fn unique_generates_different_ids() {
+ let a = Id::unique();
+ let b = Id::unique();
+
+ assert_ne!(a, b);
+ }
+}
diff --git a/native/src/widget/image.rs b/native/src/widget/image.rs
index de0ffbc0..91d68e34 100644
--- a/native/src/widget/image.rs
+++ b/native/src/widget/image.rs
@@ -5,12 +5,18 @@ pub use viewer::Viewer;
use crate::image;
use crate::layout;
use crate::renderer;
+use crate::widget::Tree;
use crate::{
ContentFit, Element, Layout, Length, Point, Rectangle, Size, Vector, Widget,
};
use std::hash::Hash;
+/// Creates a new [`Viewer`] with the given image `Handle`.
+pub fn viewer<Handle>(handle: Handle) -> Viewer<Handle> {
+ Viewer::new(handle)
+}
+
/// A frame that displays an image while keeping aspect ratio.
///
/// # Example
@@ -65,6 +71,46 @@ impl<Handle> Image<Handle> {
}
}
+/// Computes the layout of an [`Image`].
+pub fn layout<Renderer, Handle>(
+ renderer: &Renderer,
+ limits: &layout::Limits,
+ handle: &Handle,
+ width: Length,
+ height: Length,
+ content_fit: ContentFit,
+) -> layout::Node
+where
+ Renderer: image::Renderer<Handle = Handle>,
+{
+ // The raw w/h of the underlying image
+ let image_size = {
+ let (width, height) = renderer.dimensions(handle);
+
+ Size::new(width as f32, height as f32)
+ };
+
+ // The size to be available to the widget prior to `Shrink`ing
+ let raw_size = limits.width(width).height(height).resolve(image_size);
+
+ // The uncropped size of the image when fit to the bounds above
+ let full_size = content_fit.fit(image_size, raw_size);
+
+ // Shrink the widget to fit the resized image, if requested
+ let final_size = Size {
+ width: match width {
+ Length::Shrink => f32::min(raw_size.width, full_size.width),
+ _ => raw_size.width,
+ },
+ height: match height {
+ Length::Shrink => f32::min(raw_size.height, full_size.height),
+ _ => raw_size.height,
+ },
+ };
+
+ layout::Node::new(final_size)
+}
+
impl<Message, Renderer, Handle> Widget<Message, Renderer> for Image<Handle>
where
Renderer: image::Renderer<Handle = Handle>,
@@ -83,37 +129,21 @@ where
renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
- // The raw w/h of the underlying image
- let (width, height) = renderer.dimensions(&self.handle);
- let image_size = Size::new(width as f32, height as f32);
-
- // The size to be available to the widget prior to `Shrink`ing
- let raw_size = limits
- .width(self.width)
- .height(self.height)
- .resolve(image_size);
-
- // The uncropped size of the image when fit to the bounds above
- let full_size = self.content_fit.fit(image_size, raw_size);
-
- // Shrink the widget to fit the resized image, if requested
- let final_size = Size {
- width: match self.width {
- Length::Shrink => f32::min(raw_size.width, full_size.width),
- _ => raw_size.width,
- },
- height: match self.height {
- Length::Shrink => f32::min(raw_size.height, full_size.height),
- _ => raw_size.height,
- },
- };
-
- layout::Node::new(final_size)
+ layout(
+ renderer,
+ limits,
+ &self.handle,
+ self.width,
+ self.height,
+ self.content_fit,
+ )
}
fn draw(
&self,
+ _state: &Tree,
renderer: &mut Renderer,
+ _theme: &Renderer::Theme,
_style: &renderer::Style,
layout: Layout<'_>,
_cursor_position: Point,
diff --git a/native/src/widget/image/viewer.rs b/native/src/widget/image/viewer.rs
index 840b88e5..b1fe596c 100644
--- a/native/src/widget/image/viewer.rs
+++ b/native/src/widget/image/viewer.rs
@@ -4,6 +4,7 @@ use crate::image;
use crate::layout;
use crate::mouse;
use crate::renderer;
+use crate::widget::tree::{self, Tree};
use crate::{
Clipboard, Element, Layout, Length, Point, Rectangle, Shell, Size, Vector,
Widget,
@@ -13,8 +14,7 @@ use std::hash::Hash;
/// A frame that displays an image with the ability to zoom in/out and pan.
#[allow(missing_debug_implementations)]
-pub struct Viewer<'a, Handle> {
- state: &'a mut State,
+pub struct Viewer<Handle> {
padding: u16,
width: Length,
height: Length,
@@ -24,11 +24,10 @@ pub struct Viewer<'a, Handle> {
handle: Handle,
}
-impl<'a, Handle> Viewer<'a, Handle> {
+impl<Handle> Viewer<Handle> {
/// Creates a new [`Viewer`] with the given [`State`].
- pub fn new(state: &'a mut State, handle: Handle) -> Self {
+ pub fn new(handle: Handle) -> Self {
Viewer {
- state,
padding: 0,
width: Length::Shrink,
height: Length::Shrink,
@@ -81,43 +80,21 @@ impl<'a, Handle> Viewer<'a, Handle> {
self.scale_step = scale_step;
self
}
-
- /// Returns the bounds of the underlying image, given the bounds of
- /// the [`Viewer`]. Scaling will be applied and original aspect ratio
- /// will be respected.
- fn image_size<Renderer>(&self, renderer: &Renderer, bounds: Size) -> Size
- where
- Renderer: image::Renderer<Handle = Handle>,
- {
- let (width, height) = renderer.dimensions(&self.handle);
-
- let (width, height) = {
- let dimensions = (width as f32, height as f32);
-
- let width_ratio = bounds.width / dimensions.0;
- let height_ratio = bounds.height / dimensions.1;
-
- let ratio = width_ratio.min(height_ratio);
-
- let scale = self.state.scale;
-
- if ratio < 1.0 {
- (dimensions.0 * ratio * scale, dimensions.1 * ratio * scale)
- } else {
- (dimensions.0 * scale, dimensions.1 * scale)
- }
- };
-
- Size::new(width, height)
- }
}
-impl<'a, Message, Renderer, Handle> Widget<Message, Renderer>
- for Viewer<'a, Handle>
+impl<Message, Renderer, Handle> Widget<Message, Renderer> for Viewer<Handle>
where
Renderer: image::Renderer<Handle = Handle>,
Handle: Clone + Hash,
{
+ fn tag(&self) -> tree::Tag {
+ tree::Tag::of::<State>()
+ }
+
+ fn state(&self) -> tree::State {
+ tree::State::new(State::new())
+ }
+
fn width(&self) -> Length {
self.width
}
@@ -164,6 +141,7 @@ where
fn on_event(
&mut self,
+ tree: &mut Tree,
event: Event,
layout: Layout<'_>,
cursor_position: Point,
@@ -181,39 +159,43 @@ where
match delta {
mouse::ScrollDelta::Lines { y, .. }
| mouse::ScrollDelta::Pixels { y, .. } => {
- let previous_scale = self.state.scale;
+ let state = tree.state.downcast_mut::<State>();
+ let previous_scale = state.scale;
if y < 0.0 && previous_scale > self.min_scale
|| y > 0.0 && previous_scale < self.max_scale
{
- self.state.scale = (if y > 0.0 {
- self.state.scale * (1.0 + self.scale_step)
+ state.scale = (if y > 0.0 {
+ state.scale * (1.0 + self.scale_step)
} else {
- self.state.scale / (1.0 + self.scale_step)
+ state.scale / (1.0 + self.scale_step)
})
.max(self.min_scale)
.min(self.max_scale);
- let image_size =
- self.image_size(renderer, bounds.size());
+ let image_size = image_size(
+ renderer,
+ &self.handle,
+ state,
+ bounds.size(),
+ );
- let factor =
- self.state.scale / previous_scale - 1.0;
+ let factor = state.scale / previous_scale - 1.0;
let cursor_to_center =
cursor_position - bounds.center();
let adjustment = cursor_to_center * factor
- + self.state.current_offset * factor;
+ + state.current_offset * factor;
- self.state.current_offset = Vector::new(
+ state.current_offset = Vector::new(
if image_size.width > bounds.width {
- self.state.current_offset.x + adjustment.x
+ state.current_offset.x + adjustment.x
} else {
0.0
},
if image_size.height > bounds.height {
- self.state.current_offset.y + adjustment.y
+ state.current_offset.y + adjustment.y
} else {
0.0
},
@@ -227,21 +209,34 @@ where
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
if is_mouse_over =>
{
- self.state.cursor_grabbed_at = Some(cursor_position);
- self.state.starting_offset = self.state.current_offset;
+ let state = tree.state.downcast_mut::<State>();
+
+ state.cursor_grabbed_at = Some(cursor_position);
+ state.starting_offset = state.current_offset;
event::Status::Captured
}
- Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left))
- if self.state.cursor_grabbed_at.is_some() =>
- {
- self.state.cursor_grabbed_at = None;
+ Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) => {
+ let state = tree.state.downcast_mut::<State>();
- event::Status::Captured
+ if state.cursor_grabbed_at.is_some() {
+ state.cursor_grabbed_at = None;
+
+ event::Status::Captured
+ } else {
+ event::Status::Ignored
+ }
}
Event::Mouse(mouse::Event::CursorMoved { position }) => {
- if let Some(origin) = self.state.cursor_grabbed_at {
- let image_size = self.image_size(renderer, bounds.size());
+ let state = tree.state.downcast_mut::<State>();
+
+ if let Some(origin) = state.cursor_grabbed_at {
+ let image_size = image_size(
+ renderer,
+ &self.handle,
+ state,
+ bounds.size(),
+ );
let hidden_width = (image_size.width - bounds.width / 2.0)
.max(0.0)
@@ -255,7 +250,7 @@ where
let delta = position - origin;
let x = if bounds.width < image_size.width {
- (self.state.starting_offset.x - delta.x)
+ (state.starting_offset.x - delta.x)
.min(hidden_width)
.max(-hidden_width)
} else {
@@ -263,14 +258,14 @@ where
};
let y = if bounds.height < image_size.height {
- (self.state.starting_offset.y - delta.y)
+ (state.starting_offset.y - delta.y)
.min(hidden_height)
.max(-hidden_height)
} else {
0.0
};
- self.state.current_offset = Vector::new(x, y);
+ state.current_offset = Vector::new(x, y);
event::Status::Captured
} else {
@@ -283,15 +278,17 @@ where
fn mouse_interaction(
&self,
+ tree: &Tree,
layout: Layout<'_>,
cursor_position: Point,
_viewport: &Rectangle,
_renderer: &Renderer,
) -> mouse::Interaction {
+ let state = tree.state.downcast_ref::<State>();
let bounds = layout.bounds();
let is_mouse_over = bounds.contains(cursor_position);
- if self.state.is_cursor_grabbed() {
+ if state.is_cursor_grabbed() {
mouse::Interaction::Grabbing
} else if is_mouse_over {
mouse::Interaction::Grab
@@ -302,15 +299,19 @@ where
fn draw(
&self,
+ tree: &Tree,
renderer: &mut Renderer,
+ _theme: &Renderer::Theme,
_style: &renderer::Style,
layout: Layout<'_>,
_cursor_position: Point,
_viewport: &Rectangle,
) {
+ let state = tree.state.downcast_ref::<State>();
let bounds = layout.bounds();
- let image_size = self.image_size(renderer, bounds.size());
+ let image_size =
+ image_size(renderer, &self.handle, state, bounds.size());
let translation = {
let image_top_left = Vector::new(
@@ -318,7 +319,7 @@ where
bounds.height / 2.0 - image_size.height / 2.0,
);
- image_top_left - self.state.offset(bounds, image_size)
+ image_top_left - state.offset(bounds, image_size)
};
renderer.with_layer(bounds, |renderer| {
@@ -384,14 +385,47 @@ impl State {
}
}
-impl<'a, Message, Renderer, Handle> From<Viewer<'a, Handle>>
+impl<'a, Message, Renderer, Handle> From<Viewer<Handle>>
for Element<'a, Message, Renderer>
where
Renderer: 'a + image::Renderer<Handle = Handle>,
Message: 'a,
Handle: Clone + Hash + 'a,
{
- fn from(viewer: Viewer<'a, Handle>) -> Element<'a, Message, Renderer> {
+ fn from(viewer: Viewer<Handle>) -> Element<'a, Message, Renderer> {
Element::new(viewer)
}
}
+
+/// Returns the bounds of the underlying image, given the bounds of
+/// the [`Viewer`]. Scaling will be applied and original aspect ratio
+/// will be respected.
+pub fn image_size<Renderer>(
+ renderer: &Renderer,
+ handle: &<Renderer as image::Renderer>::Handle,
+ state: &State,
+ bounds: Size,
+) -> Size
+where
+ Renderer: image::Renderer,
+{
+ let (width, height) = renderer.dimensions(handle);
+
+ let (width, height) = {
+ let dimensions = (width as f32, height as f32);
+
+ let width_ratio = bounds.width / dimensions.0;
+ let height_ratio = bounds.height / dimensions.1;
+
+ let ratio = width_ratio.min(height_ratio);
+ let scale = state.scale;
+
+ if ratio < 1.0 {
+ (dimensions.0 * ratio * scale, dimensions.1 * ratio * scale)
+ } else {
+ (dimensions.0 * scale, dimensions.1 * scale)
+ }
+ };
+
+ Size::new(width, height)
+}
diff --git a/native/src/widget/operation.rs b/native/src/widget/operation.rs
new file mode 100644
index 00000000..ef636aa2
--- /dev/null
+++ b/native/src/widget/operation.rs
@@ -0,0 +1,60 @@
+//! Query or update internal widget state.
+pub mod focusable;
+pub mod scrollable;
+
+pub use focusable::Focusable;
+pub use scrollable::Scrollable;
+
+use crate::widget::Id;
+
+use std::fmt;
+
+/// A piece of logic that can traverse the widget tree of an application in
+/// order to query or update some widget state.
+pub trait Operation<T> {
+ /// Operates on a widget that contains other widgets.
+ ///
+ /// The `operate_on_children` function can be called to return control to
+ /// the widget tree and keep traversing it.
+ fn container(
+ &mut self,
+ id: Option<&Id>,
+ operate_on_children: &mut dyn FnMut(&mut dyn Operation<T>),
+ );
+
+ /// Operates on a widget that can be focused.
+ fn focusable(&mut self, _state: &mut dyn Focusable, _id: Option<&Id>) {}
+
+ /// Operates on a widget that can be scrolled.
+ fn scrollable(&mut self, _state: &mut dyn Scrollable, _id: Option<&Id>) {}
+
+ /// Finishes the [`Operation`] and returns its [`Outcome`].
+ fn finish(&self) -> Outcome<T> {
+ Outcome::None
+ }
+}
+
+/// The result of an [`Operation`].
+pub enum Outcome<T> {
+ /// The [`Operation`] produced no result.
+ None,
+
+ /// The [`Operation`] produced some result.
+ Some(T),
+
+ /// The [`Operation`] needs to be followed by another [`Operation`].
+ Chain(Box<dyn Operation<T>>),
+}
+
+impl<T> fmt::Debug for Outcome<T>
+where
+ T: fmt::Debug,
+{
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ match self {
+ Self::None => write!(f, "Outcome::None"),
+ Self::Some(output) => write!(f, "Outcome::Some({:?})", output),
+ Self::Chain(_) => write!(f, "Outcome::Chain(...)"),
+ }
+ }
+}
diff --git a/native/src/widget/operation/focusable.rs b/native/src/widget/operation/focusable.rs
new file mode 100644
index 00000000..f17bf178
--- /dev/null
+++ b/native/src/widget/operation/focusable.rs
@@ -0,0 +1,169 @@
+//! Operate on widgets that can be focused.
+use crate::widget::operation::{Operation, Outcome};
+use crate::widget::Id;
+
+/// The internal state of a widget that can be focused.
+pub trait Focusable {
+ /// Returns whether the widget is focused or not.
+ fn is_focused(&self) -> bool;
+
+ /// Focuses the widget.
+ fn focus(&mut self);
+
+ /// Unfocuses the widget.
+ fn unfocus(&mut self);
+}
+
+/// A summary of the focusable widgets present on a widget tree.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
+pub struct Count {
+ /// The index of the current focused widget, if any.
+ focused: Option<usize>,
+
+ /// The total amount of focusable widgets.
+ total: usize,
+}
+
+/// Produces an [`Operation`] that focuses the widget with the given [`Id`].
+pub fn focus<T>(target: Id) -> impl Operation<T> {
+ struct Focus {
+ target: Id,
+ }
+
+ impl<T> Operation<T> for Focus {
+ fn focusable(&mut self, state: &mut dyn Focusable, id: Option<&Id>) {
+ match id {
+ Some(id) if id == &self.target => {
+ state.focus();
+ }
+ _ => {
+ state.unfocus();
+ }
+ }
+ }
+
+ fn container(
+ &mut self,
+ _id: Option<&Id>,
+ operate_on_children: &mut dyn FnMut(&mut dyn Operation<T>),
+ ) {
+ operate_on_children(self)
+ }
+ }
+
+ Focus { target }
+}
+
+/// Produces an [`Operation`] that generates a [`Count`] and chains it with the
+/// provided function to build a new [`Operation`].
+pub fn count<T, O>(f: fn(Count) -> O) -> impl Operation<T>
+where
+ O: Operation<T> + 'static,
+{
+ struct CountFocusable<O> {
+ count: Count,
+ next: fn(Count) -> O,
+ }
+
+ impl<T, O> Operation<T> for CountFocusable<O>
+ where
+ O: Operation<T> + 'static,
+ {
+ fn focusable(&mut self, state: &mut dyn Focusable, _id: Option<&Id>) {
+ if state.is_focused() {
+ self.count.focused = Some(self.count.total);
+ }
+
+ self.count.total += 1;
+ }
+
+ fn container(
+ &mut self,
+ _id: Option<&Id>,
+ operate_on_children: &mut dyn FnMut(&mut dyn Operation<T>),
+ ) {
+ operate_on_children(self)
+ }
+
+ fn finish(&self) -> Outcome<T> {
+ Outcome::Chain(Box::new((self.next)(self.count)))
+ }
+ }
+
+ CountFocusable {
+ count: Count::default(),
+ next: f,
+ }
+}
+
+/// Produces an [`Operation`] that searches for the current focused widget, and
+/// - if found, focuses the previous focusable widget.
+/// - if not found, focuses the last focusable widget.
+pub fn focus_previous<T>() -> impl Operation<T> {
+ struct FocusPrevious {
+ count: Count,
+ current: usize,
+ }
+
+ impl<T> Operation<T> for FocusPrevious {
+ fn focusable(&mut self, state: &mut dyn Focusable, _id: Option<&Id>) {
+ if self.count.total == 0 {
+ return;
+ }
+
+ match self.count.focused {
+ None if self.current == self.count.total - 1 => state.focus(),
+ Some(0) if self.current == 0 => state.unfocus(),
+ Some(0) => {}
+ Some(focused) if focused == self.current => state.unfocus(),
+ Some(focused) if focused - 1 == self.current => state.focus(),
+ _ => {}
+ }
+
+ self.current += 1;
+ }
+
+ fn container(
+ &mut self,
+ _id: Option<&Id>,
+ operate_on_children: &mut dyn FnMut(&mut dyn Operation<T>),
+ ) {
+ operate_on_children(self)
+ }
+ }
+
+ count(|count| FocusPrevious { count, current: 0 })
+}
+
+/// Produces an [`Operation`] that searches for the current focused widget, and
+/// - if found, focuses the next focusable widget.
+/// - if not found, focuses the first focusable widget.
+pub fn focus_next<T>() -> impl Operation<T> {
+ struct FocusNext {
+ count: Count,
+ current: usize,
+ }
+
+ impl<T> Operation<T> for FocusNext {
+ fn focusable(&mut self, state: &mut dyn Focusable, _id: Option<&Id>) {
+ match self.count.focused {
+ None if self.current == 0 => state.focus(),
+ Some(focused) if focused == self.current => state.unfocus(),
+ Some(focused) if focused + 1 == self.current => state.focus(),
+ _ => {}
+ }
+
+ self.current += 1;
+ }
+
+ fn container(
+ &mut self,
+ _id: Option<&Id>,
+ operate_on_children: &mut dyn FnMut(&mut dyn Operation<T>),
+ ) {
+ operate_on_children(self)
+ }
+ }
+
+ count(|count| FocusNext { count, current: 0 })
+}
diff --git a/native/src/widget/operation/scrollable.rs b/native/src/widget/operation/scrollable.rs
new file mode 100644
index 00000000..2210137d
--- /dev/null
+++ b/native/src/widget/operation/scrollable.rs
@@ -0,0 +1,35 @@
+//! Operate on widgets that can be scrolled.
+use crate::widget::{Id, Operation};
+
+/// The internal state of a widget that can be scrolled.
+pub trait Scrollable {
+ /// Snaps the scroll of the widget to the given `percentage`.
+ fn snap_to(&mut self, percentage: f32);
+}
+
+/// Produces an [`Operation`] that snaps the widget with the given [`Id`] to
+/// the provided `percentage`.
+pub fn snap_to<T>(target: Id, percentage: f32) -> impl Operation<T> {
+ struct SnapTo {
+ target: Id,
+ percentage: f32,
+ }
+
+ impl<T> Operation<T> for SnapTo {
+ fn scrollable(&mut self, state: &mut dyn Scrollable, id: Option<&Id>) {
+ if Some(&self.target) == id {
+ state.snap_to(self.percentage);
+ }
+ }
+
+ fn container(
+ &mut self,
+ _id: Option<&Id>,
+ operate_on_children: &mut dyn FnMut(&mut dyn Operation<T>),
+ ) {
+ operate_on_children(self)
+ }
+ }
+
+ SnapTo { target, percentage }
+}
diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs
index 8ad63cf1..d84fb7a0 100644
--- a/native/src/widget/pane_grid.rs
+++ b/native/src/widget/pane_grid.rs
@@ -6,40 +6,45 @@
//! The [`pane_grid` example] showcases how to use a [`PaneGrid`] with resizing,
//! drag and drop, and hotkey support.
//!
-//! [`pane_grid` example]: https://github.com/iced-rs/iced/tree/0.3/examples/pane_grid
+//! [`pane_grid` example]: https://github.com/iced-rs/iced/tree/0.4/examples/pane_grid
mod axis;
mod configuration;
mod content;
mod direction;
+mod draggable;
mod node;
mod pane;
mod split;
-mod state;
mod title_bar;
+pub mod state;
+
pub use axis::Axis;
pub use configuration::Configuration;
pub use content::Content;
pub use direction::Direction;
+pub use draggable::Draggable;
pub use node::Node;
pub use pane::Pane;
pub use split::Split;
pub use state::State;
pub use title_bar::TitleBar;
+pub use iced_style::pane_grid::{Line, StyleSheet};
+
use crate::event::{self, Event};
use crate::layout;
use crate::mouse;
use crate::overlay;
use crate::renderer;
use crate::touch;
+use crate::widget::container;
+use crate::widget::tree::{self, Tree};
use crate::{
Clipboard, Color, Element, Layout, Length, Point, Rectangle, Shell, Size,
Vector, Widget,
};
-pub use iced_style::pane_grid::{Line, StyleSheet};
-
/// A collection of panes distributed using either vertical or horizontal splits
/// to completely fill the space available.
///
@@ -62,7 +67,7 @@ pub use iced_style::pane_grid::{Line, StyleSheet};
/// ## Example
///
/// ```
-/// # use iced_native::widget::{pane_grid, Text};
+/// # use iced_native::widget::{pane_grid, text};
/// #
/// # type PaneGrid<'a, Message> =
/// # iced_native::widget::PaneGrid<'a, Message, iced_native::renderer::Null>;
@@ -80,18 +85,22 @@ pub use iced_style::pane_grid::{Line, StyleSheet};
/// let (mut state, _) = pane_grid::State::new(PaneState::SomePane);
///
/// let pane_grid =
-/// PaneGrid::new(&mut state, |pane, state| {
+/// PaneGrid::new(&state, |pane, state| {
/// pane_grid::Content::new(match state {
-/// PaneState::SomePane => Text::new("This is some pane"),
-/// PaneState::AnotherKindOfPane => Text::new("This is another kind of pane"),
+/// PaneState::SomePane => text("This is some pane"),
+/// PaneState::AnotherKindOfPane => text("This is another kind of pane"),
/// })
/// })
/// .on_drag(Message::PaneDragged)
/// .on_resize(10, Message::PaneResized);
/// ```
#[allow(missing_debug_implementations)]
-pub struct PaneGrid<'a, Message, Renderer> {
- state: &'a mut state::Internal,
+pub struct PaneGrid<'a, Message, Renderer>
+where
+ Renderer: crate::Renderer,
+ Renderer::Theme: StyleSheet + container::StyleSheet,
+{
+ state: &'a state::Internal,
elements: Vec<(Pane, Content<'a, Message, Renderer>)>,
width: Length,
height: Length,
@@ -99,39 +108,40 @@ pub struct PaneGrid<'a, Message, Renderer> {
on_click: Option<Box<dyn Fn(Pane) -> Message + 'a>>,
on_drag: Option<Box<dyn Fn(DragEvent) -> Message + 'a>>,
on_resize: Option<(u16, Box<dyn Fn(ResizeEvent) -> Message + 'a>)>,
- style_sheet: Box<dyn StyleSheet + 'a>,
+ style: <Renderer::Theme as StyleSheet>::Style,
}
impl<'a, Message, Renderer> PaneGrid<'a, Message, Renderer>
where
Renderer: crate::Renderer,
+ Renderer::Theme: StyleSheet + container::StyleSheet,
{
/// Creates a [`PaneGrid`] with the given [`State`] and view function.
///
/// The view function will be called to display each [`Pane`] present in the
/// [`State`].
pub fn new<T>(
- state: &'a mut State<T>,
- view: impl Fn(Pane, &'a mut T) -> Content<'a, Message, Renderer>,
+ state: &'a State<T>,
+ view: impl Fn(Pane, &'a T) -> Content<'a, Message, Renderer>,
) -> Self {
let elements = {
state
.panes
- .iter_mut()
+ .iter()
.map(|(pane, pane_state)| (*pane, view(*pane, pane_state)))
.collect()
};
Self {
- state: &mut state.internal,
elements,
+ state: &state.internal,
width: Length::Fill,
height: Length::Fill,
spacing: 0,
on_click: None,
on_drag: None,
on_resize: None,
- style_sheet: Default::default(),
+ style: Default::default(),
}
}
@@ -191,133 +201,44 @@ where
}
/// Sets the style of the [`PaneGrid`].
- pub fn style(mut self, style: impl Into<Box<dyn StyleSheet + 'a>>) -> Self {
- self.style_sheet = style.into();
+ pub fn style(
+ mut self,
+ style: impl Into<<Renderer::Theme as StyleSheet>::Style>,
+ ) -> Self {
+ self.style = style.into();
self
}
}
-impl<'a, Message, Renderer> PaneGrid<'a, Message, Renderer>
+impl<'a, Message, Renderer> Widget<Message, Renderer>
+ for PaneGrid<'a, Message, Renderer>
where
Renderer: crate::Renderer,
+ Renderer::Theme: StyleSheet + container::StyleSheet,
{
- fn click_pane(
- &mut self,
- layout: Layout<'_>,
- cursor_position: Point,
- shell: &mut Shell<'_, Message>,
- ) {
- let mut clicked_region =
- self.elements.iter().zip(layout.children()).filter(
- |(_, layout)| layout.bounds().contains(cursor_position),
- );
-
- if let Some(((pane, content), layout)) = clicked_region.next() {
- if let Some(on_click) = &self.on_click {
- shell.publish(on_click(*pane));
- }
-
- if let Some(on_drag) = &self.on_drag {
- if content.can_be_picked_at(layout, cursor_position) {
- let pane_position = layout.position();
-
- let origin = cursor_position
- - Vector::new(pane_position.x, pane_position.y);
-
- self.state.pick_pane(pane, origin);
-
- shell.publish(on_drag(DragEvent::Picked { pane: *pane }));
- }
- }
- }
+ fn tag(&self) -> tree::Tag {
+ tree::Tag::of::<state::Action>()
}
- fn trigger_resize(
- &mut self,
- layout: Layout<'_>,
- cursor_position: Point,
- shell: &mut Shell<'_, Message>,
- ) -> event::Status {
- if let Some((_, on_resize)) = &self.on_resize {
- if let Some((split, _)) = self.state.picked_split() {
- let bounds = layout.bounds();
-
- let splits = self.state.split_regions(
- f32::from(self.spacing),
- Size::new(bounds.width, bounds.height),
- );
-
- if let Some((axis, rectangle, _)) = splits.get(&split) {
- let ratio = match axis {
- Axis::Horizontal => {
- let position =
- cursor_position.y - bounds.y - rectangle.y;
-
- (position / rectangle.height).max(0.1).min(0.9)
- }
- Axis::Vertical => {
- let position =
- cursor_position.x - bounds.x - rectangle.x;
-
- (position / rectangle.width).max(0.1).min(0.9)
- }
- };
-
- shell.publish(on_resize(ResizeEvent { split, ratio }));
-
- return event::Status::Captured;
- }
- }
- }
-
- event::Status::Ignored
+ fn state(&self) -> tree::State {
+ tree::State::new(state::Action::Idle)
}
-}
-
-/// An event produced during a drag and drop interaction of a [`PaneGrid`].
-#[derive(Debug, Clone, Copy)]
-pub enum DragEvent {
- /// A [`Pane`] was picked for dragging.
- Picked {
- /// The picked [`Pane`].
- pane: Pane,
- },
-
- /// A [`Pane`] was dropped on top of another [`Pane`].
- Dropped {
- /// The picked [`Pane`].
- pane: Pane,
-
- /// The [`Pane`] where the picked one was dropped on.
- target: Pane,
- },
-
- /// A [`Pane`] was picked and then dropped outside of other [`Pane`]
- /// boundaries.
- Canceled {
- /// The picked [`Pane`].
- pane: Pane,
- },
-}
-/// An event produced during a resize interaction of a [`PaneGrid`].
-#[derive(Debug, Clone, Copy)]
-pub struct ResizeEvent {
- /// The [`Split`] that is being dragged for resizing.
- pub split: Split,
+ fn children(&self) -> Vec<Tree> {
+ self.elements
+ .iter()
+ .map(|(_, content)| content.state())
+ .collect()
+ }
- /// The new ratio of the [`Split`].
- ///
- /// The ratio is a value in [0, 1], representing the exact position of a
- /// [`Split`] between two panes.
- pub ratio: f32,
-}
+ fn diff(&self, tree: &mut Tree) {
+ tree.diff_children_custom(
+ &self.elements,
+ |state, (_, content)| content.diff(state),
+ |(_, content)| content.state(),
+ )
+ }
-impl<'a, Message, Renderer> Widget<Message, Renderer>
- for PaneGrid<'a, Message, Renderer>
-where
- Renderer: crate::Renderer,
-{
fn width(&self) -> Length {
self.width
}
@@ -331,32 +252,21 @@ where
renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
- let limits = limits.width(self.width).height(self.height);
- let size = limits.resolve(Size::ZERO);
-
- let regions = self.state.pane_regions(f32::from(self.spacing), size);
-
- let children = self
- .elements
- .iter()
- .filter_map(|(pane, element)| {
- let region = regions.get(pane)?;
- let size = Size::new(region.width, region.height);
-
- let mut node =
- element.layout(renderer, &layout::Limits::new(size, size));
-
- node.move_to(Point::new(region.x, region.y));
-
- Some(node)
- })
- .collect();
-
- layout::Node::with_children(size, children)
+ layout(
+ renderer,
+ limits,
+ self.state,
+ self.width,
+ self.height,
+ self.spacing,
+ self.elements.iter().map(|(pane, content)| (*pane, content)),
+ |element, renderer, limits| element.layout(renderer, limits),
+ )
}
fn on_event(
&mut self,
+ tree: &mut Tree,
event: Event,
layout: Layout<'_>,
cursor_position: Point,
@@ -364,97 +274,33 @@ where
clipboard: &mut dyn Clipboard,
shell: &mut Shell<'_, Message>,
) -> event::Status {
- let mut event_status = event::Status::Ignored;
-
- match event {
- Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
- | Event::Touch(touch::Event::FingerPressed { .. }) => {
- let bounds = layout.bounds();
-
- if bounds.contains(cursor_position) {
- event_status = event::Status::Captured;
-
- match self.on_resize {
- Some((leeway, _)) => {
- let relative_cursor = Point::new(
- cursor_position.x - bounds.x,
- cursor_position.y - bounds.y,
- );
-
- let splits = self.state.split_regions(
- f32::from(self.spacing),
- Size::new(bounds.width, bounds.height),
- );
-
- let clicked_split = hovered_split(
- splits.iter(),
- f32::from(self.spacing + leeway),
- relative_cursor,
- );
-
- if let Some((split, axis, _)) = clicked_split {
- self.state.pick_split(&split, axis);
- } else {
- self.click_pane(layout, cursor_position, shell);
- }
- }
- None => {
- self.click_pane(layout, cursor_position, shell);
- }
- }
- }
- }
- Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left))
- | Event::Touch(touch::Event::FingerLifted { .. })
- | Event::Touch(touch::Event::FingerLost { .. }) => {
- if let Some((pane, _)) = self.state.picked_pane() {
- if let Some(on_drag) = &self.on_drag {
- let mut dropped_region =
- self.elements.iter().zip(layout.children()).filter(
- |(_, layout)| {
- layout.bounds().contains(cursor_position)
- },
- );
-
- let event = match dropped_region.next() {
- Some(((target, _), _)) if pane != *target => {
- DragEvent::Dropped {
- pane,
- target: *target,
- }
- }
- _ => DragEvent::Canceled { pane },
- };
-
- shell.publish(on_drag(event));
- }
-
- self.state.idle();
-
- event_status = event::Status::Captured;
- } else if self.state.picked_split().is_some() {
- self.state.idle();
-
- event_status = event::Status::Captured;
- }
- }
- Event::Mouse(mouse::Event::CursorMoved { .. })
- | Event::Touch(touch::Event::FingerMoved { .. }) => {
- event_status =
- self.trigger_resize(layout, cursor_position, shell);
- }
- _ => {}
- }
-
- let picked_pane = self.state.picked_pane().map(|(pane, _)| pane);
+ let action = tree.state.downcast_mut::<state::Action>();
+
+ let event_status = update(
+ action,
+ self.state,
+ &event,
+ layout,
+ cursor_position,
+ shell,
+ self.spacing,
+ self.elements.iter().map(|(pane, content)| (*pane, content)),
+ &self.on_click,
+ &self.on_drag,
+ &self.on_resize,
+ );
+
+ let picked_pane = action.picked_pane().map(|(pane, _)| pane);
self.elements
.iter_mut()
+ .zip(&mut tree.children)
.zip(layout.children())
- .map(|((pane, content), layout)| {
+ .map(|(((pane, content), tree), layout)| {
let is_picked = picked_pane == Some(*pane);
content.on_event(
+ tree,
event.clone(),
layout,
cursor_position,
@@ -469,229 +315,560 @@ where
fn mouse_interaction(
&self,
+ tree: &Tree,
layout: Layout<'_>,
cursor_position: Point,
viewport: &Rectangle,
renderer: &Renderer,
) -> mouse::Interaction {
- if self.state.picked_pane().is_some() {
- return mouse::Interaction::Grab;
- }
-
- let resize_axis =
- self.state.picked_split().map(|(_, axis)| axis).or_else(|| {
- self.on_resize.as_ref().and_then(|(leeway, _)| {
- let bounds = layout.bounds();
-
- let splits = self
- .state
- .split_regions(f32::from(self.spacing), bounds.size());
-
- let relative_cursor = Point::new(
- cursor_position.x - bounds.x,
- cursor_position.y - bounds.y,
- );
-
- hovered_split(
- splits.iter(),
- f32::from(self.spacing + leeway),
- relative_cursor,
+ mouse_interaction(
+ tree.state.downcast_ref(),
+ self.state,
+ layout,
+ cursor_position,
+ self.spacing,
+ self.on_resize.as_ref().map(|(leeway, _)| *leeway),
+ )
+ .unwrap_or_else(|| {
+ self.elements
+ .iter()
+ .zip(&tree.children)
+ .zip(layout.children())
+ .map(|(((_pane, content), tree), layout)| {
+ content.mouse_interaction(
+ tree,
+ layout,
+ cursor_position,
+ viewport,
+ renderer,
)
- .map(|(_, axis, _)| axis)
})
- });
-
- if let Some(resize_axis) = resize_axis {
- return match resize_axis {
- Axis::Horizontal => mouse::Interaction::ResizingVertically,
- Axis::Vertical => mouse::Interaction::ResizingHorizontally,
- };
- }
-
- self.elements
- .iter()
- .zip(layout.children())
- .map(|((_pane, content), layout)| {
- content.mouse_interaction(
- layout,
- cursor_position,
- viewport,
- renderer,
- )
- })
- .max()
- .unwrap_or_default()
+ .max()
+ .unwrap_or_default()
+ })
}
fn draw(
&self,
+ tree: &Tree,
renderer: &mut Renderer,
+ theme: &Renderer::Theme,
style: &renderer::Style,
layout: Layout<'_>,
cursor_position: Point,
viewport: &Rectangle,
) {
- let picked_pane = self.state.picked_pane();
+ draw(
+ tree.state.downcast_ref(),
+ self.state,
+ layout,
+ cursor_position,
+ renderer,
+ theme,
+ style,
+ viewport,
+ self.spacing,
+ self.on_resize.as_ref().map(|(leeway, _)| *leeway),
+ self.style,
+ self.elements
+ .iter()
+ .zip(&tree.children)
+ .map(|((pane, content), tree)| (*pane, (content, tree))),
+ |(content, tree),
+ renderer,
+ style,
+ layout,
+ cursor_position,
+ rectangle| {
+ content.draw(
+ tree,
+ renderer,
+ theme,
+ style,
+ layout,
+ cursor_position,
+ rectangle,
+ );
+ },
+ )
+ }
- let picked_split = self
- .state
- .picked_split()
- .and_then(|(split, axis)| {
- let bounds = layout.bounds();
+ fn overlay<'b>(
+ &'b self,
+ tree: &'b mut Tree,
+ layout: Layout<'_>,
+ renderer: &Renderer,
+ ) -> Option<overlay::Element<'_, Message, Renderer>> {
+ self.elements
+ .iter()
+ .zip(&mut tree.children)
+ .zip(layout.children())
+ .filter_map(|(((_, pane), tree), layout)| {
+ pane.overlay(tree, layout, renderer)
+ })
+ .next()
+ }
+}
- let splits = self
- .state
- .split_regions(f32::from(self.spacing), bounds.size());
+impl<'a, Message, Renderer> From<PaneGrid<'a, Message, Renderer>>
+ for Element<'a, Message, Renderer>
+where
+ Message: 'a,
+ Renderer: 'a + crate::Renderer,
+ Renderer::Theme: StyleSheet + container::StyleSheet,
+{
+ fn from(
+ pane_grid: PaneGrid<'a, Message, Renderer>,
+ ) -> Element<'a, Message, Renderer> {
+ Element::new(pane_grid)
+ }
+}
- let (_axis, region, ratio) = splits.get(&split)?;
+/// Calculates the [`Layout`] of a [`PaneGrid`].
+pub fn layout<Renderer, T>(
+ renderer: &Renderer,
+ limits: &layout::Limits,
+ state: &state::Internal,
+ width: Length,
+ height: Length,
+ spacing: u16,
+ elements: impl Iterator<Item = (Pane, T)>,
+ layout_element: impl Fn(T, &Renderer, &layout::Limits) -> layout::Node,
+) -> layout::Node {
+ let limits = limits.width(width).height(height);
+ let size = limits.resolve(Size::ZERO);
+
+ let regions = state.pane_regions(f32::from(spacing), size);
+ let children = elements
+ .filter_map(|(pane, element)| {
+ let region = regions.get(&pane)?;
+ let size = Size::new(region.width, region.height);
+
+ let mut node = layout_element(
+ element,
+ renderer,
+ &layout::Limits::new(size, size),
+ );
- let region = axis.split_line_bounds(
- *region,
- *ratio,
- f32::from(self.spacing),
- );
+ node.move_to(Point::new(region.x, region.y));
- Some((axis, region + Vector::new(bounds.x, bounds.y), true))
- })
- .or_else(|| match self.on_resize {
- Some((leeway, _)) => {
- let bounds = layout.bounds();
+ Some(node)
+ })
+ .collect();
- let relative_cursor = Point::new(
- cursor_position.x - bounds.x,
- cursor_position.y - bounds.y,
- );
+ layout::Node::with_children(size, children)
+}
+
+/// Processes an [`Event`] and updates the [`state`] of a [`PaneGrid`]
+/// accordingly.
+pub fn update<'a, Message, T: Draggable>(
+ action: &mut state::Action,
+ state: &state::Internal,
+ event: &Event,
+ layout: Layout<'_>,
+ cursor_position: Point,
+ shell: &mut Shell<'_, Message>,
+ spacing: u16,
+ elements: impl Iterator<Item = (Pane, T)>,
+ on_click: &Option<Box<dyn Fn(Pane) -> Message + 'a>>,
+ on_drag: &Option<Box<dyn Fn(DragEvent) -> Message + 'a>>,
+ on_resize: &Option<(u16, Box<dyn Fn(ResizeEvent) -> Message + 'a>)>,
+) -> event::Status {
+ let mut event_status = event::Status::Ignored;
+
+ match event {
+ Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
+ | Event::Touch(touch::Event::FingerPressed { .. }) => {
+ let bounds = layout.bounds();
+
+ if bounds.contains(cursor_position) {
+ event_status = event::Status::Captured;
+
+ match on_resize {
+ Some((leeway, _)) => {
+ let relative_cursor = Point::new(
+ cursor_position.x - bounds.x,
+ cursor_position.y - bounds.y,
+ );
+
+ let splits = state.split_regions(
+ f32::from(spacing),
+ Size::new(bounds.width, bounds.height),
+ );
+
+ let clicked_split = hovered_split(
+ splits.iter(),
+ f32::from(spacing + leeway),
+ relative_cursor,
+ );
+
+ if let Some((split, axis, _)) = clicked_split {
+ if action.picked_pane().is_none() {
+ *action =
+ state::Action::Resizing { split, axis };
+ }
+ } else {
+ click_pane(
+ action,
+ layout,
+ cursor_position,
+ shell,
+ elements,
+ on_click,
+ on_drag,
+ );
+ }
+ }
+ None => {
+ click_pane(
+ action,
+ layout,
+ cursor_position,
+ shell,
+ elements,
+ on_click,
+ on_drag,
+ );
+ }
+ }
+ }
+ }
+ Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left))
+ | Event::Touch(touch::Event::FingerLifted { .. })
+ | Event::Touch(touch::Event::FingerLost { .. }) => {
+ if let Some((pane, _)) = action.picked_pane() {
+ if let Some(on_drag) = on_drag {
+ let mut dropped_region = elements
+ .zip(layout.children())
+ .filter(|(_, layout)| {
+ layout.bounds().contains(cursor_position)
+ });
+
+ let event = match dropped_region.next() {
+ Some(((target, _), _)) if pane != target => {
+ DragEvent::Dropped { pane, target }
+ }
+ _ => DragEvent::Canceled { pane },
+ };
- let splits = self
- .state
- .split_regions(f32::from(self.spacing), bounds.size());
-
- let (_split, axis, region) = hovered_split(
- splits.iter(),
- f32::from(self.spacing + leeway),
- relative_cursor,
- )?;
-
- Some((
- axis,
- region + Vector::new(bounds.x, bounds.y),
- false,
- ))
+ shell.publish(on_drag(event));
}
- None => None,
- });
- let pane_cursor_position = if picked_pane.is_some() {
- // TODO: Remove once cursor availability is encoded in the type
- // system
- Point::new(-1.0, -1.0)
- } else {
- cursor_position
- };
+ *action = state::Action::Idle;
- for ((id, pane), layout) in self.elements.iter().zip(layout.children())
- {
- match picked_pane {
- Some((dragging, origin)) if *id == dragging => {
+ event_status = event::Status::Captured;
+ } else if action.picked_split().is_some() {
+ *action = state::Action::Idle;
+
+ event_status = event::Status::Captured;
+ }
+ }
+ Event::Mouse(mouse::Event::CursorMoved { .. })
+ | Event::Touch(touch::Event::FingerMoved { .. }) => {
+ if let Some((_, on_resize)) = on_resize {
+ if let Some((split, _)) = action.picked_split() {
let bounds = layout.bounds();
- renderer.with_translation(
- cursor_position
- - Point::new(
- bounds.x + origin.x,
- bounds.y + origin.y,
- ),
- |renderer| {
- renderer.with_layer(bounds, |renderer| {
- pane.draw(
- renderer,
- style,
- layout,
- pane_cursor_position,
- viewport,
- );
- });
- },
- );
- }
- _ => {
- pane.draw(
- renderer,
- style,
- layout,
- pane_cursor_position,
- viewport,
+ let splits = state.split_regions(
+ f32::from(spacing),
+ Size::new(bounds.width, bounds.height),
);
+
+ if let Some((axis, rectangle, _)) = splits.get(&split) {
+ let ratio = match axis {
+ Axis::Horizontal => {
+ let position =
+ cursor_position.y - bounds.y - rectangle.y;
+
+ (position / rectangle.height).max(0.1).min(0.9)
+ }
+ Axis::Vertical => {
+ let position =
+ cursor_position.x - bounds.x - rectangle.x;
+
+ (position / rectangle.width).max(0.1).min(0.9)
+ }
+ };
+
+ shell.publish(on_resize(ResizeEvent { split, ratio }));
+
+ event_status = event::Status::Captured;
+ }
}
}
}
+ _ => {}
+ }
- if let Some((axis, split_region, is_picked)) = picked_split {
- let highlight = if is_picked {
- self.style_sheet.picked_split()
- } else {
- self.style_sheet.hovered_split()
- };
-
- if let Some(highlight) = highlight {
- renderer.fill_quad(
- renderer::Quad {
- bounds: match axis {
- Axis::Horizontal => Rectangle {
- x: split_region.x,
- y: (split_region.y
- + (split_region.height - highlight.width)
- / 2.0)
- .round(),
- width: split_region.width,
- height: highlight.width,
- },
- Axis::Vertical => Rectangle {
- x: (split_region.x
- + (split_region.width - highlight.width)
- / 2.0)
- .round(),
- y: split_region.y,
- width: highlight.width,
- height: split_region.height,
- },
- },
- border_radius: 0.0,
- border_width: 0.0,
- border_color: Color::TRANSPARENT,
- },
- highlight.color,
- );
+ event_status
+}
+
+fn click_pane<'a, Message, T>(
+ action: &mut state::Action,
+ layout: Layout<'_>,
+ cursor_position: Point,
+ shell: &mut Shell<'_, Message>,
+ elements: impl Iterator<Item = (Pane, T)>,
+ on_click: &Option<Box<dyn Fn(Pane) -> Message + 'a>>,
+ on_drag: &Option<Box<dyn Fn(DragEvent) -> Message + 'a>>,
+) where
+ T: Draggable,
+{
+ let mut clicked_region = elements
+ .zip(layout.children())
+ .filter(|(_, layout)| layout.bounds().contains(cursor_position));
+
+ if let Some(((pane, content), layout)) = clicked_region.next() {
+ if let Some(on_click) = &on_click {
+ shell.publish(on_click(pane));
+ }
+
+ if let Some(on_drag) = &on_drag {
+ if content.can_be_dragged_at(layout, cursor_position) {
+ let pane_position = layout.position();
+
+ let origin = cursor_position
+ - Vector::new(pane_position.x, pane_position.y);
+
+ *action = state::Action::Dragging { pane, origin };
+
+ shell.publish(on_drag(DragEvent::Picked { pane }));
}
}
}
+}
- fn overlay(
- &mut self,
- layout: Layout<'_>,
- renderer: &Renderer,
- ) -> Option<overlay::Element<'_, Message, Renderer>> {
- self.elements
- .iter_mut()
- .zip(layout.children())
- .filter_map(|((_, pane), layout)| pane.overlay(layout, renderer))
- .next()
+/// Returns the current [`mouse::Interaction`] of a [`PaneGrid`].
+pub fn mouse_interaction(
+ action: &state::Action,
+ state: &state::Internal,
+ layout: Layout<'_>,
+ cursor_position: Point,
+ spacing: u16,
+ resize_leeway: Option<u16>,
+) -> Option<mouse::Interaction> {
+ if action.picked_pane().is_some() {
+ return Some(mouse::Interaction::Grab);
}
+
+ let resize_axis =
+ action.picked_split().map(|(_, axis)| axis).or_else(|| {
+ resize_leeway.and_then(|leeway| {
+ let bounds = layout.bounds();
+
+ let splits =
+ state.split_regions(f32::from(spacing), bounds.size());
+
+ let relative_cursor = Point::new(
+ cursor_position.x - bounds.x,
+ cursor_position.y - bounds.y,
+ );
+
+ hovered_split(
+ splits.iter(),
+ f32::from(spacing + leeway),
+ relative_cursor,
+ )
+ .map(|(_, axis, _)| axis)
+ })
+ });
+
+ if let Some(resize_axis) = resize_axis {
+ return Some(match resize_axis {
+ Axis::Horizontal => mouse::Interaction::ResizingVertically,
+ Axis::Vertical => mouse::Interaction::ResizingHorizontally,
+ });
+ }
+
+ None
}
-impl<'a, Message, Renderer> From<PaneGrid<'a, Message, Renderer>>
- for Element<'a, Message, Renderer>
-where
- Renderer: 'a + crate::Renderer,
- Message: 'a,
+/// Draws a [`PaneGrid`].
+pub fn draw<Renderer, T>(
+ action: &state::Action,
+ state: &state::Internal,
+ layout: Layout<'_>,
+ cursor_position: Point,
+ renderer: &mut Renderer,
+ theme: &Renderer::Theme,
+ default_style: &renderer::Style,
+ viewport: &Rectangle,
+ spacing: u16,
+ resize_leeway: Option<u16>,
+ style: <Renderer::Theme as StyleSheet>::Style,
+ elements: impl Iterator<Item = (Pane, T)>,
+ draw_pane: impl Fn(
+ T,
+ &mut Renderer,
+ &renderer::Style,
+ Layout<'_>,
+ Point,
+ &Rectangle,
+ ),
+) where
+ Renderer: crate::Renderer,
+ Renderer::Theme: StyleSheet,
{
- fn from(
- pane_grid: PaneGrid<'a, Message, Renderer>,
- ) -> Element<'a, Message, Renderer> {
- Element::new(pane_grid)
+ let picked_pane = action.picked_pane();
+
+ let picked_split = action
+ .picked_split()
+ .and_then(|(split, axis)| {
+ let bounds = layout.bounds();
+
+ let splits = state.split_regions(f32::from(spacing), bounds.size());
+
+ let (_axis, region, ratio) = splits.get(&split)?;
+
+ let region =
+ axis.split_line_bounds(*region, *ratio, f32::from(spacing));
+
+ Some((axis, region + Vector::new(bounds.x, bounds.y), true))
+ })
+ .or_else(|| match resize_leeway {
+ Some(leeway) => {
+ let bounds = layout.bounds();
+
+ let relative_cursor = Point::new(
+ cursor_position.x - bounds.x,
+ cursor_position.y - bounds.y,
+ );
+
+ let splits =
+ state.split_regions(f32::from(spacing), bounds.size());
+
+ let (_split, axis, region) = hovered_split(
+ splits.iter(),
+ f32::from(spacing + leeway),
+ relative_cursor,
+ )?;
+
+ Some((axis, region + Vector::new(bounds.x, bounds.y), false))
+ }
+ None => None,
+ });
+
+ let pane_cursor_position = if picked_pane.is_some() {
+ // TODO: Remove once cursor availability is encoded in the type
+ // system
+ Point::new(-1.0, -1.0)
+ } else {
+ cursor_position
+ };
+
+ for ((id, pane), layout) in elements.zip(layout.children()) {
+ match picked_pane {
+ Some((dragging, origin)) if id == dragging => {
+ let bounds = layout.bounds();
+
+ renderer.with_translation(
+ cursor_position
+ - Point::new(bounds.x + origin.x, bounds.y + origin.y),
+ |renderer| {
+ renderer.with_layer(bounds, |renderer| {
+ draw_pane(
+ pane,
+ renderer,
+ default_style,
+ layout,
+ pane_cursor_position,
+ viewport,
+ );
+ });
+ },
+ );
+ }
+ _ => {
+ draw_pane(
+ pane,
+ renderer,
+ default_style,
+ layout,
+ pane_cursor_position,
+ viewport,
+ );
+ }
+ }
+ }
+
+ if let Some((axis, split_region, is_picked)) = picked_split {
+ let highlight = if is_picked {
+ theme.picked_split(style)
+ } else {
+ theme.hovered_split(style)
+ };
+
+ if let Some(highlight) = highlight {
+ renderer.fill_quad(
+ renderer::Quad {
+ bounds: match axis {
+ Axis::Horizontal => Rectangle {
+ x: split_region.x,
+ y: (split_region.y
+ + (split_region.height - highlight.width)
+ / 2.0)
+ .round(),
+ width: split_region.width,
+ height: highlight.width,
+ },
+ Axis::Vertical => Rectangle {
+ x: (split_region.x
+ + (split_region.width - highlight.width) / 2.0)
+ .round(),
+ y: split_region.y,
+ width: highlight.width,
+ height: split_region.height,
+ },
+ },
+ border_radius: 0.0,
+ border_width: 0.0,
+ border_color: Color::TRANSPARENT,
+ },
+ highlight.color,
+ );
+ }
}
}
+/// An event produced during a drag and drop interaction of a [`PaneGrid`].
+#[derive(Debug, Clone, Copy)]
+pub enum DragEvent {
+ /// A [`Pane`] was picked for dragging.
+ Picked {
+ /// The picked [`Pane`].
+ pane: Pane,
+ },
+
+ /// A [`Pane`] was dropped on top of another [`Pane`].
+ Dropped {
+ /// The picked [`Pane`].
+ pane: Pane,
+
+ /// The [`Pane`] where the picked one was dropped on.
+ target: Pane,
+ },
+
+ /// A [`Pane`] was picked and then dropped outside of other [`Pane`]
+ /// boundaries.
+ Canceled {
+ /// The picked [`Pane`].
+ pane: Pane,
+ },
+}
+
+/// An event produced during a resize interaction of a [`PaneGrid`].
+#[derive(Debug, Clone, Copy)]
+pub struct ResizeEvent {
+ /// The [`Split`] that is being dragged for resizing.
+ pub split: Split,
+
+ /// The new ratio of the [`Split`].
+ ///
+ /// The ratio is a value in [0, 1], representing the exact position of a
+ /// [`Split`] between two panes.
+ pub ratio: f32,
+}
+
/*
* Helpers
*/
@@ -702,8 +879,7 @@ fn hovered_split<'a>(
) -> Option<(Split, Axis, Rectangle)> {
splits
.filter_map(|(split, (axis, region, ratio))| {
- let bounds =
- axis.split_line_bounds(*region, *ratio, f32::from(spacing));
+ let bounds = axis.split_line_bounds(*region, *ratio, spacing);
if bounds.contains(cursor_position) {
Some((*split, *axis, bounds))
diff --git a/native/src/widget/pane_grid/axis.rs b/native/src/widget/pane_grid/axis.rs
index 2320cb7c..02bde064 100644
--- a/native/src/widget/pane_grid/axis.rs
+++ b/native/src/widget/pane_grid/axis.rs
@@ -10,7 +10,9 @@ pub enum Axis {
}
impl Axis {
- pub(super) fn split(
+ /// Splits the provided [`Rectangle`] on the current [`Axis`] with the
+ /// given `ratio` and `spacing`.
+ pub fn split(
&self,
rectangle: &Rectangle,
ratio: f32,
@@ -54,7 +56,8 @@ impl Axis {
}
}
- pub(super) fn split_line_bounds(
+ /// Calculates the bounds of the split line in a [`Rectangle`] region.
+ pub fn split_line_bounds(
&self,
rectangle: Rectangle,
ratio: f32,
diff --git a/native/src/widget/pane_grid/configuration.rs b/native/src/widget/pane_grid/configuration.rs
index 4c52bad4..7d68fb46 100644
--- a/native/src/widget/pane_grid/configuration.rs
+++ b/native/src/widget/pane_grid/configuration.rs
@@ -2,7 +2,7 @@ use crate::widget::pane_grid::Axis;
/// The arrangement of a [`PaneGrid`].
///
-/// [`PaneGrid`]: crate::pane_grid::PaneGrid
+/// [`PaneGrid`]: crate::widget::PaneGrid
#[derive(Debug, Clone)]
pub enum Configuration<T> {
/// A split of the available space.
@@ -21,6 +21,6 @@ pub enum Configuration<T> {
},
/// A [`Pane`].
///
- /// [`Pane`]: crate::pane_grid::Pane
+ /// [`Pane`]: crate::widget::pane_grid::Pane
Pane(T),
}
diff --git a/native/src/widget/pane_grid/content.rs b/native/src/widget/pane_grid/content.rs
index 8b0e8d2a..98ce2c4b 100644
--- a/native/src/widget/pane_grid/content.rs
+++ b/native/src/widget/pane_grid/content.rs
@@ -4,29 +4,35 @@ use crate::mouse;
use crate::overlay;
use crate::renderer;
use crate::widget::container;
-use crate::widget::pane_grid::TitleBar;
+use crate::widget::pane_grid::{Draggable, TitleBar};
+use crate::widget::Tree;
use crate::{Clipboard, Element, Layout, Point, Rectangle, Shell, Size};
/// The content of a [`Pane`].
///
/// [`Pane`]: crate::widget::pane_grid::Pane
#[allow(missing_debug_implementations)]
-pub struct Content<'a, Message, Renderer> {
+pub struct Content<'a, Message, Renderer>
+where
+ Renderer: crate::Renderer,
+ Renderer::Theme: container::StyleSheet,
+{
title_bar: Option<TitleBar<'a, Message, Renderer>>,
body: Element<'a, Message, Renderer>,
- style_sheet: Box<dyn container::StyleSheet + 'a>,
+ style: <Renderer::Theme as container::StyleSheet>::Style,
}
impl<'a, Message, Renderer> Content<'a, Message, Renderer>
where
Renderer: crate::Renderer,
+ Renderer::Theme: container::StyleSheet,
{
/// Creates a new [`Content`] with the provided body.
pub fn new(body: impl Into<Element<'a, Message, Renderer>>) -> Self {
Self {
title_bar: None,
body: body.into(),
- style_sheet: Default::default(),
+ style: Default::default(),
}
}
@@ -42,9 +48,9 @@ where
/// Sets the style of the [`Content`].
pub fn style(
mut self,
- style_sheet: impl Into<Box<dyn container::StyleSheet + 'a>>,
+ style: impl Into<<Renderer::Theme as container::StyleSheet>::Style>,
) -> Self {
- self.style_sheet = style_sheet.into();
+ self.style = style.into();
self
}
}
@@ -52,22 +58,52 @@ where
impl<'a, Message, Renderer> Content<'a, Message, Renderer>
where
Renderer: crate::Renderer,
+ Renderer::Theme: container::StyleSheet,
{
+ pub(super) fn state(&self) -> Tree {
+ let children = if let Some(title_bar) = self.title_bar.as_ref() {
+ vec![Tree::new(&self.body), title_bar.state()]
+ } else {
+ vec![Tree::new(&self.body), Tree::empty()]
+ };
+
+ Tree {
+ children,
+ ..Tree::empty()
+ }
+ }
+
+ pub(super) fn diff(&self, tree: &mut Tree) {
+ if tree.children.len() == 2 {
+ if let Some(title_bar) = self.title_bar.as_ref() {
+ title_bar.diff(&mut tree.children[1]);
+ }
+
+ tree.children[0].diff(&self.body);
+ } else {
+ *tree = self.state();
+ }
+ }
+
/// Draws the [`Content`] with the provided [`Renderer`] and [`Layout`].
///
- /// [`Renderer`]: crate::widget::pane_grid::Renderer
+ /// [`Renderer`]: iced_native::Renderer
pub fn draw(
&self,
+ tree: &Tree,
renderer: &mut Renderer,
+ theme: &Renderer::Theme,
style: &renderer::Style,
layout: Layout<'_>,
cursor_position: Point,
viewport: &Rectangle,
) {
+ use container::StyleSheet;
+
let bounds = layout.bounds();
{
- let style = self.style_sheet.style();
+ let style = theme.appearance(self.style);
container::draw_background(renderer, &style, bounds);
}
@@ -80,7 +116,9 @@ where
let show_controls = bounds.contains(cursor_position);
title_bar.draw(
+ &tree.children[1],
renderer,
+ theme,
style,
title_bar_layout,
cursor_position,
@@ -88,33 +126,25 @@ where
show_controls,
);
- self.body.draw(
+ self.body.as_widget().draw(
+ &tree.children[0],
renderer,
+ theme,
style,
body_layout,
cursor_position,
viewport,
);
} else {
- self.body
- .draw(renderer, style, layout, cursor_position, viewport);
- }
- }
-
- /// Returns whether the [`Content`] with the given [`Layout`] can be picked
- /// at the provided cursor position.
- pub fn can_be_picked_at(
- &self,
- layout: Layout<'_>,
- cursor_position: Point,
- ) -> bool {
- if let Some(title_bar) = &self.title_bar {
- let mut children = layout.children();
- let title_bar_layout = children.next().unwrap();
-
- title_bar.is_over_pick_area(title_bar_layout, cursor_position)
- } else {
- false
+ self.body.as_widget().draw(
+ &tree.children[0],
+ renderer,
+ theme,
+ style,
+ layout,
+ cursor_position,
+ viewport,
+ );
}
}
@@ -131,7 +161,7 @@ where
let title_bar_size = title_bar_layout.size();
- let mut body_layout = self.body.layout(
+ let mut body_layout = self.body.as_widget().layout(
renderer,
&layout::Limits::new(
Size::ZERO,
@@ -149,12 +179,13 @@ where
vec![title_bar_layout, body_layout],
)
} else {
- self.body.layout(renderer, limits)
+ self.body.as_widget().layout(renderer, limits)
}
}
pub(crate) fn on_event(
&mut self,
+ tree: &mut Tree,
event: Event,
layout: Layout<'_>,
cursor_position: Point,
@@ -169,6 +200,7 @@ where
let mut children = layout.children();
event_status = title_bar.on_event(
+ &mut tree.children[1],
event.clone(),
children.next().unwrap(),
cursor_position,
@@ -185,7 +217,8 @@ where
let body_status = if is_picked {
event::Status::Ignored
} else {
- self.body.on_event(
+ self.body.as_widget_mut().on_event(
+ &mut tree.children[0],
event,
body_layout,
cursor_position,
@@ -200,6 +233,7 @@ where
pub(crate) fn mouse_interaction(
&self,
+ tree: &Tree,
layout: Layout<'_>,
cursor_position: Point,
viewport: &Rectangle,
@@ -218,6 +252,7 @@ where
}
let mouse_interaction = title_bar.mouse_interaction(
+ &tree.children[1],
title_bar_layout,
cursor_position,
viewport,
@@ -230,25 +265,67 @@ where
};
self.body
- .mouse_interaction(body_layout, cursor_position, viewport, renderer)
+ .as_widget()
+ .mouse_interaction(
+ &tree.children[0],
+ body_layout,
+ cursor_position,
+ viewport,
+ renderer,
+ )
.max(title_bar_interaction)
}
- pub(crate) fn overlay(
- &mut self,
+ pub(crate) fn overlay<'b>(
+ &'b self,
+ tree: &'b mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
- ) -> Option<overlay::Element<'_, Message, Renderer>> {
- if let Some(title_bar) = self.title_bar.as_mut() {
+ ) -> Option<overlay::Element<'b, Message, Renderer>> {
+ if let Some(title_bar) = self.title_bar.as_ref() {
let mut children = layout.children();
let title_bar_layout = children.next()?;
- match title_bar.overlay(title_bar_layout, renderer) {
+ let mut states = tree.children.iter_mut();
+ let body_state = states.next().unwrap();
+ let title_bar_state = states.next().unwrap();
+
+ match title_bar.overlay(title_bar_state, title_bar_layout, renderer)
+ {
Some(overlay) => Some(overlay),
- None => self.body.overlay(children.next()?, renderer),
+ None => self.body.as_widget().overlay(
+ body_state,
+ children.next()?,
+ renderer,
+ ),
}
} else {
- self.body.overlay(layout, renderer)
+ self.body.as_widget().overlay(
+ &mut tree.children[0],
+ layout,
+ renderer,
+ )
+ }
+ }
+}
+
+impl<'a, Message, Renderer> Draggable for &Content<'a, Message, Renderer>
+where
+ Renderer: crate::Renderer,
+ Renderer::Theme: container::StyleSheet,
+{
+ fn can_be_dragged_at(
+ &self,
+ layout: Layout<'_>,
+ cursor_position: Point,
+ ) -> bool {
+ if let Some(title_bar) = &self.title_bar {
+ let mut children = layout.children();
+ let title_bar_layout = children.next().unwrap();
+
+ title_bar.is_over_pick_area(title_bar_layout, cursor_position)
+ } else {
+ false
}
}
}
@@ -257,6 +334,7 @@ impl<'a, T, Message, Renderer> From<T> for Content<'a, Message, Renderer>
where
T: Into<Element<'a, Message, Renderer>>,
Renderer: crate::Renderer,
+ Renderer::Theme: container::StyleSheet,
{
fn from(element: T) -> Self {
Self::new(element)
diff --git a/native/src/widget/pane_grid/draggable.rs b/native/src/widget/pane_grid/draggable.rs
new file mode 100644
index 00000000..6044871d
--- /dev/null
+++ b/native/src/widget/pane_grid/draggable.rs
@@ -0,0 +1,12 @@
+use crate::{Layout, Point};
+
+/// A pane that can be dragged.
+pub trait Draggable {
+ /// Returns whether the [`Draggable`] with the given [`Layout`] can be picked
+ /// at the provided cursor position.
+ fn can_be_dragged_at(
+ &self,
+ layout: Layout<'_>,
+ cursor_position: Point,
+ ) -> bool;
+}
diff --git a/native/src/widget/pane_grid/node.rs b/native/src/widget/pane_grid/node.rs
index af6573a0..cc304b96 100644
--- a/native/src/widget/pane_grid/node.rs
+++ b/native/src/widget/pane_grid/node.rs
@@ -36,14 +36,11 @@ impl Node {
std::iter::from_fn(move || {
while let Some(node) = unvisited_nodes.pop() {
- match node {
- Node::Split { id, a, b, .. } => {
- unvisited_nodes.push(a);
- unvisited_nodes.push(b);
+ if let Node::Split { id, a, b, .. } = node {
+ unvisited_nodes.push(a);
+ unvisited_nodes.push(b);
- return Some(id);
- }
- _ => {}
+ return Some(id);
}
}
@@ -124,12 +121,9 @@ impl Node {
}
pub(crate) fn update(&mut self, f: &impl Fn(&mut Node)) {
- match self {
- Node::Split { a, b, .. } => {
- a.update(f);
- b.update(f);
- }
- _ => {}
+ if let Node::Split { a, b, .. } = self {
+ a.update(f);
+ b.update(f);
}
f(self);
diff --git a/native/src/widget/pane_grid/state.rs b/native/src/widget/pane_grid/state.rs
index feea0dec..cdca6267 100644
--- a/native/src/widget/pane_grid/state.rs
+++ b/native/src/widget/pane_grid/state.rs
@@ -1,3 +1,6 @@
+//! The state of a [`PaneGrid`].
+//!
+//! [`PaneGrid`]: crate::widget::PaneGrid
use crate::widget::pane_grid::{
Axis, Configuration, Direction, Node, Pane, Split,
};
@@ -19,8 +22,15 @@ use std::collections::{BTreeMap, HashMap};
/// [`PaneGrid::new`]: crate::widget::PaneGrid::new
#[derive(Debug, Clone)]
pub struct State<T> {
- pub(super) panes: HashMap<Pane, T>,
- pub(super) internal: Internal,
+ /// The panes of the [`PaneGrid`].
+ ///
+ /// [`PaneGrid`]: crate::widget::PaneGrid
+ pub panes: HashMap<Pane, T>,
+
+ /// The internal state of the [`PaneGrid`].
+ ///
+ /// [`PaneGrid`]: crate::widget::PaneGrid
+ pub internal: Internal,
}
impl<T> State<T> {
@@ -39,17 +49,10 @@ impl<T> State<T> {
pub fn with_configuration(config: impl Into<Configuration<T>>) -> Self {
let mut panes = HashMap::new();
- let (layout, last_id) =
- Self::distribute_content(&mut panes, config.into(), 0);
+ let internal =
+ Internal::from_configuration(&mut panes, config.into(), 0);
- State {
- panes,
- internal: Internal {
- layout,
- last_id,
- action: Action::Idle,
- },
- }
+ State { panes, internal }
}
/// Returns the total amount of panes in the [`State`].
@@ -57,6 +60,11 @@ impl<T> State<T> {
self.panes.len()
}
+ /// Returns `true` if the amount of panes in the [`State`] is 0.
+ pub fn is_empty(&self) -> bool {
+ self.len() == 0
+ }
+
/// Returns the internal state of the given [`Pane`], if it exists.
pub fn get(&self, pane: &Pane) -> Option<&T> {
self.panes.get(pane)
@@ -192,16 +200,38 @@ impl<T> State<T> {
None
}
}
+}
+
+/// The internal state of a [`PaneGrid`].
+///
+/// [`PaneGrid`]: crate::widget::PaneGrid
+#[derive(Debug, Clone)]
+pub struct Internal {
+ layout: Node,
+ last_id: usize,
+}
- fn distribute_content(
+impl Internal {
+ /// Initializes the [`Internal`] state of a [`PaneGrid`] from a
+ /// [`Configuration`].
+ ///
+ /// [`PaneGrid`]: crate::widget::PaneGrid
+ pub fn from_configuration<T>(
panes: &mut HashMap<Pane, T>,
content: Configuration<T>,
next_id: usize,
- ) -> (Node, usize) {
- match content {
+ ) -> Self {
+ let (layout, last_id) = match content {
Configuration::Split { axis, ratio, a, b } => {
- let (a, next_id) = Self::distribute_content(panes, *a, next_id);
- let (b, next_id) = Self::distribute_content(panes, *b, next_id);
+ let Internal {
+ layout: a,
+ last_id: next_id,
+ } = Self::from_configuration(panes, *a, next_id);
+
+ let Internal {
+ layout: b,
+ last_id: next_id,
+ } = Self::from_configuration(panes, *b, next_id);
(
Node::Split {
@@ -220,39 +250,63 @@ impl<T> State<T> {
(Node::Pane(id), next_id + 1)
}
- }
- }
-}
+ };
-#[derive(Debug, Clone)]
-pub struct Internal {
- layout: Node,
- last_id: usize,
- action: Action,
+ Self { layout, last_id }
+ }
}
+/// The current action of a [`PaneGrid`].
+///
+/// [`PaneGrid`]: crate::widget::PaneGrid
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Action {
+ /// The [`PaneGrid`] is idle.
+ ///
+ /// [`PaneGrid`]: crate::widget::PaneGrid
Idle,
- Dragging { pane: Pane, origin: Point },
- Resizing { split: Split, axis: Axis },
+ /// A [`Pane`] in the [`PaneGrid`] is being dragged.
+ ///
+ /// [`PaneGrid`]: crate::widget::PaneGrid
+ Dragging {
+ /// The [`Pane`] being dragged.
+ pane: Pane,
+ /// The starting [`Point`] of the drag interaction.
+ origin: Point,
+ },
+ /// A [`Split`] in the [`PaneGrid`] is being dragged.
+ ///
+ /// [`PaneGrid`]: crate::widget::PaneGrid
+ Resizing {
+ /// The [`Split`] being dragged.
+ split: Split,
+ /// The [`Axis`] of the [`Split`].
+ axis: Axis,
+ },
}
-impl Internal {
+impl Action {
+ /// Returns the current [`Pane`] that is being dragged, if any.
pub fn picked_pane(&self) -> Option<(Pane, Point)> {
- match self.action {
+ match *self {
Action::Dragging { pane, origin, .. } => Some((pane, origin)),
_ => None,
}
}
+ /// Returns the current [`Split`] that is being dragged, if any.
pub fn picked_split(&self) -> Option<(Split, Axis)> {
- match self.action {
+ match *self {
Action::Resizing { split, axis, .. } => Some((split, axis)),
_ => None,
}
}
+}
+impl Internal {
+ /// Calculates the current [`Pane`] regions from the [`PaneGrid`] layout.
+ ///
+ /// [`PaneGrid`]: crate::widget::PaneGrid
pub fn pane_regions(
&self,
spacing: f32,
@@ -261,6 +315,9 @@ impl Internal {
self.layout.pane_regions(spacing, size)
}
+ /// Calculates the current [`Split`] regions from the [`PaneGrid`] layout.
+ ///
+ /// [`PaneGrid`]: crate::widget::PaneGrid
pub fn split_regions(
&self,
spacing: f32,
@@ -268,28 +325,4 @@ impl Internal {
) -> BTreeMap<Split, (Axis, Rectangle, f32)> {
self.layout.split_regions(spacing, size)
}
-
- pub fn pick_pane(&mut self, pane: &Pane, origin: Point) {
- self.action = Action::Dragging {
- pane: *pane,
- origin,
- };
- }
-
- pub fn pick_split(&mut self, split: &Split, axis: Axis) {
- // TODO: Obtain `axis` from layout itself. Maybe we should implement
- // `Node::find_split`
- if self.picked_pane().is_some() {
- return;
- }
-
- self.action = Action::Resizing {
- split: *split,
- axis,
- };
- }
-
- pub fn idle(&mut self) {
- self.action = Action::Idle;
- }
}
diff --git a/native/src/widget/pane_grid/title_bar.rs b/native/src/widget/pane_grid/title_bar.rs
index d56972ec..eb85f924 100644
--- a/native/src/widget/pane_grid/title_bar.rs
+++ b/native/src/widget/pane_grid/title_bar.rs
@@ -4,6 +4,7 @@ use crate::mouse;
use crate::overlay;
use crate::renderer;
use crate::widget::container;
+use crate::widget::Tree;
use crate::{
Clipboard, Element, Layout, Padding, Point, Rectangle, Shell, Size,
};
@@ -12,17 +13,22 @@ use crate::{
///
/// [`Pane`]: crate::widget::pane_grid::Pane
#[allow(missing_debug_implementations)]
-pub struct TitleBar<'a, Message, Renderer> {
+pub struct TitleBar<'a, Message, Renderer>
+where
+ Renderer: crate::Renderer,
+ Renderer::Theme: container::StyleSheet,
+{
content: Element<'a, Message, Renderer>,
controls: Option<Element<'a, Message, Renderer>>,
padding: Padding,
always_show_controls: bool,
- style_sheet: Box<dyn container::StyleSheet + 'a>,
+ style: <Renderer::Theme as container::StyleSheet>::Style,
}
impl<'a, Message, Renderer> TitleBar<'a, Message, Renderer>
where
Renderer: crate::Renderer,
+ Renderer::Theme: container::StyleSheet,
{
/// Creates a new [`TitleBar`] with the given content.
pub fn new<E>(content: E) -> Self
@@ -34,7 +40,7 @@ where
controls: None,
padding: Padding::ZERO,
always_show_controls: false,
- style_sheet: Default::default(),
+ style: Default::default(),
}
}
@@ -56,9 +62,9 @@ where
/// Sets the style of the [`TitleBar`].
pub fn style(
mut self,
- style: impl Into<Box<dyn container::StyleSheet + 'a>>,
+ style: impl Into<<Renderer::Theme as container::StyleSheet>::Style>,
) -> Self {
- self.style_sheet = style.into();
+ self.style = style.into();
self
}
@@ -79,21 +85,51 @@ where
impl<'a, Message, Renderer> TitleBar<'a, Message, Renderer>
where
Renderer: crate::Renderer,
+ Renderer::Theme: container::StyleSheet,
{
+ pub(super) fn state(&self) -> Tree {
+ let children = if let Some(controls) = self.controls.as_ref() {
+ vec![Tree::new(&self.content), Tree::new(controls)]
+ } else {
+ vec![Tree::new(&self.content), Tree::empty()]
+ };
+
+ Tree {
+ children,
+ ..Tree::empty()
+ }
+ }
+
+ pub(super) fn diff(&self, tree: &mut Tree) {
+ if tree.children.len() == 2 {
+ if let Some(controls) = self.controls.as_ref() {
+ tree.children[1].diff(controls);
+ }
+
+ tree.children[0].diff(&self.content);
+ } else {
+ *tree = self.state();
+ }
+ }
+
/// Draws the [`TitleBar`] with the provided [`Renderer`] and [`Layout`].
///
- /// [`Renderer`]: crate::widget::pane_grid::Renderer
+ /// [`Renderer`]: iced_native::Renderer
pub fn draw(
&self,
+ tree: &Tree,
renderer: &mut Renderer,
+ theme: &Renderer::Theme,
inherited_style: &renderer::Style,
layout: Layout<'_>,
cursor_position: Point,
viewport: &Rectangle,
show_controls: bool,
) {
+ use container::StyleSheet;
+
let bounds = layout.bounds();
- let style = self.style_sheet.style();
+ let style = theme.appearance(self.style);
let inherited_style = renderer::Style {
text_color: style.text_color.unwrap_or(inherited_style.text_color),
};
@@ -105,21 +141,21 @@ where
let mut children = padded.children();
let title_layout = children.next().unwrap();
-
- self.content.draw(
- renderer,
- &inherited_style,
- title_layout,
- cursor_position,
- viewport,
- );
+ let mut show_title = true;
if let Some(controls) = &self.controls {
- let controls_layout = children.next().unwrap();
-
if show_controls || self.always_show_controls {
- controls.draw(
+ let controls_layout = children.next().unwrap();
+ if title_layout.bounds().width + controls_layout.bounds().width
+ > padded.bounds().width
+ {
+ show_title = false;
+ }
+
+ controls.as_widget().draw(
+ &tree.children[1],
renderer,
+ theme,
&inherited_style,
controls_layout,
cursor_position,
@@ -127,6 +163,18 @@ where
);
}
}
+
+ if show_title {
+ self.content.as_widget().draw(
+ &tree.children[0],
+ renderer,
+ theme,
+ &inherited_style,
+ title_layout,
+ cursor_position,
+ viewport,
+ );
+ }
}
/// Returns whether the mouse cursor is over the pick area of the
@@ -147,8 +195,14 @@ where
if self.controls.is_some() {
let controls_layout = children.next().unwrap();
- !controls_layout.bounds().contains(cursor_position)
- && !title_layout.bounds().contains(cursor_position)
+ if title_layout.bounds().width + controls_layout.bounds().width
+ > padded.bounds().width
+ {
+ !controls_layout.bounds().contains(cursor_position)
+ } else {
+ !controls_layout.bounds().contains(cursor_position)
+ && !title_layout.bounds().contains(cursor_position)
+ }
} else {
!title_layout.bounds().contains(cursor_position)
}
@@ -167,11 +221,14 @@ where
let title_layout = self
.content
+ .as_widget()
.layout(renderer, &layout::Limits::new(Size::ZERO, max_size));
+
let title_size = title_layout.size();
let mut node = if let Some(controls) = &self.controls {
let mut controls_layout = controls
+ .as_widget()
.layout(renderer, &layout::Limits::new(Size::ZERO, max_size));
let controls_size = controls_layout.size();
@@ -202,6 +259,7 @@ where
pub(crate) fn on_event(
&mut self,
+ tree: &mut Tree,
event: Event,
layout: Layout<'_>,
cursor_position: Point,
@@ -214,11 +272,18 @@ where
let mut children = padded.children();
let title_layout = children.next().unwrap();
+ let mut show_title = true;
let control_status = if let Some(controls) = &mut self.controls {
let controls_layout = children.next().unwrap();
+ if title_layout.bounds().width + controls_layout.bounds().width
+ > padded.bounds().width
+ {
+ show_title = false;
+ }
- controls.on_event(
+ controls.as_widget_mut().on_event(
+ &mut tree.children[1],
event.clone(),
controls_layout,
cursor_position,
@@ -230,20 +295,26 @@ where
event::Status::Ignored
};
- let title_status = self.content.on_event(
- event,
- title_layout,
- cursor_position,
- renderer,
- clipboard,
- shell,
- );
+ let title_status = if show_title {
+ self.content.as_widget_mut().on_event(
+ &mut tree.children[0],
+ event,
+ title_layout,
+ cursor_position,
+ renderer,
+ clipboard,
+ shell,
+ )
+ } else {
+ event::Status::Ignored
+ };
control_status.merge(title_status)
}
pub(crate) fn mouse_interaction(
&self,
+ tree: &Tree,
layout: Layout<'_>,
cursor_position: Point,
viewport: &Rectangle,
@@ -255,7 +326,8 @@ where
let mut children = padded.children();
let title_layout = children.next().unwrap();
- let title_interaction = self.content.mouse_interaction(
+ let title_interaction = self.content.as_widget().mouse_interaction(
+ &tree.children[0],
title_layout,
cursor_position,
viewport,
@@ -264,25 +336,32 @@ where
if let Some(controls) = &self.controls {
let controls_layout = children.next().unwrap();
+ let controls_interaction = controls.as_widget().mouse_interaction(
+ &tree.children[1],
+ controls_layout,
+ cursor_position,
+ viewport,
+ renderer,
+ );
- controls
- .mouse_interaction(
- controls_layout,
- cursor_position,
- viewport,
- renderer,
- )
- .max(title_interaction)
+ if title_layout.bounds().width + controls_layout.bounds().width
+ > padded.bounds().width
+ {
+ controls_interaction
+ } else {
+ controls_interaction.max(title_interaction)
+ }
} else {
title_interaction
}
}
- pub(crate) fn overlay(
- &mut self,
+ pub(crate) fn overlay<'b>(
+ &'b self,
+ tree: &'b mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
- ) -> Option<overlay::Element<'_, Message, Renderer>> {
+ ) -> Option<overlay::Element<'b, Message, Renderer>> {
let mut children = layout.children();
let padded = children.next()?;
@@ -293,12 +372,23 @@ where
content, controls, ..
} = self;
- content.overlay(title_layout, renderer).or_else(move || {
- controls.as_mut().and_then(|controls| {
- let controls_layout = children.next()?;
-
- controls.overlay(controls_layout, renderer)
+ let mut states = tree.children.iter_mut();
+ let title_state = states.next().unwrap();
+ let controls_state = states.next().unwrap();
+
+ content
+ .as_widget()
+ .overlay(title_state, title_layout, renderer)
+ .or_else(move || {
+ controls.as_ref().and_then(|controls| {
+ let controls_layout = children.next()?;
+
+ controls.as_widget().overlay(
+ controls_state,
+ controls_layout,
+ renderer,
+ )
+ })
})
- })
}
}
diff --git a/native/src/widget/pick_list.rs b/native/src/widget/pick_list.rs
index 3be6c20c..c334804e 100644
--- a/native/src/widget/pick_list.rs
+++ b/native/src/widget/pick_list.rs
@@ -9,26 +9,24 @@ use crate::overlay::menu::{self, Menu};
use crate::renderer;
use crate::text::{self, Text};
use crate::touch;
+use crate::widget::tree::{self, Tree};
use crate::{
Clipboard, Element, Layout, Length, Padding, Point, Rectangle, Shell, Size,
Widget,
};
use std::borrow::Cow;
-pub use iced_style::pick_list::{Style, StyleSheet};
+pub use iced_style::pick_list::{Appearance, StyleSheet};
/// A widget for selecting a single value from a list of options.
#[allow(missing_debug_implementations)]
-pub struct PickList<'a, T, Message, Renderer: text::Renderer>
+pub struct PickList<'a, T, Message, Renderer>
where
[T]: ToOwned<Owned = Vec<T>>,
+ Renderer: text::Renderer,
+ Renderer::Theme: StyleSheet,
{
- menu: &'a mut menu::State,
- keyboard_modifiers: &'a mut keyboard::Modifiers,
- is_open: &'a mut bool,
- hovered_option: &'a mut Option<usize>,
- last_selection: &'a mut Option<T>,
- on_selected: Box<dyn Fn(T) -> Message>,
+ on_selected: Box<dyn Fn(T) -> Message + 'a>,
options: Cow<'a, [T]>,
placeholder: Option<String>,
selected: Option<T>,
@@ -36,63 +34,27 @@ where
padding: Padding,
text_size: Option<u16>,
font: Renderer::Font,
- style_sheet: Box<dyn StyleSheet + 'a>,
+ style: <Renderer::Theme as StyleSheet>::Style,
}
-/// The local state of a [`PickList`].
-#[derive(Debug, Clone)]
-pub struct State<T> {
- menu: menu::State,
- keyboard_modifiers: keyboard::Modifiers,
- is_open: bool,
- hovered_option: Option<usize>,
- last_selection: Option<T>,
-}
-
-impl<T> Default for State<T> {
- fn default() -> Self {
- Self {
- menu: menu::State::default(),
- keyboard_modifiers: keyboard::Modifiers::default(),
- is_open: bool::default(),
- hovered_option: Option::default(),
- last_selection: Option::default(),
- }
- }
-}
-
-impl<'a, T: 'a, Message, Renderer: text::Renderer>
- PickList<'a, T, Message, Renderer>
+impl<'a, T: 'a, Message, Renderer> PickList<'a, T, Message, Renderer>
where
T: ToString + Eq,
[T]: ToOwned<Owned = Vec<T>>,
+ Renderer: text::Renderer,
+ Renderer::Theme: StyleSheet,
{
/// The default padding of a [`PickList`].
pub const DEFAULT_PADDING: Padding = Padding::new(5);
- /// Creates a new [`PickList`] with the given [`State`], a list of options,
- /// the current selected value, and the message to produce when an option is
- /// selected.
+ /// Creates a new [`PickList`] with the given list of options, the current
+ /// selected value, and the message to produce when an option is selected.
pub fn new(
- state: &'a mut State<T>,
options: impl Into<Cow<'a, [T]>>,
selected: Option<T>,
- on_selected: impl Fn(T) -> Message + 'static,
+ on_selected: impl Fn(T) -> Message + 'a,
) -> Self {
- let State {
- menu,
- keyboard_modifiers,
- is_open,
- hovered_option,
- last_selection,
- } = state;
-
Self {
- menu,
- keyboard_modifiers,
- is_open,
- hovered_option,
- last_selection,
on_selected: Box::new(on_selected),
options: options.into(),
placeholder: None,
@@ -101,7 +63,7 @@ where
text_size: None,
padding: Self::DEFAULT_PADDING,
font: Default::default(),
- style_sheet: Default::default(),
+ style: Default::default(),
}
}
@@ -138,9 +100,9 @@ where
/// Sets the style of the [`PickList`].
pub fn style(
mut self,
- style_sheet: impl Into<Box<dyn StyleSheet + 'a>>,
+ style: impl Into<<Renderer::Theme as StyleSheet>::Style>,
) -> Self {
- self.style_sheet = style_sheet.into();
+ self.style = style.into();
self
}
}
@@ -148,11 +110,20 @@ where
impl<'a, T: 'a, Message, Renderer> Widget<Message, Renderer>
for PickList<'a, T, Message, Renderer>
where
- T: Clone + ToString + Eq,
+ T: Clone + ToString + Eq + 'static,
[T]: ToOwned<Owned = Vec<T>>,
- Message: 'static,
+ Message: 'a,
Renderer: text::Renderer + 'a,
+ Renderer::Theme: StyleSheet,
{
+ fn tag(&self) -> tree::Tag {
+ tree::Tag::of::<State<T>>()
+ }
+
+ fn state(&self) -> tree::State {
+ tree::State::new(State::<T>::new())
+ }
+
fn width(&self) -> Length {
self.width
}
@@ -166,62 +137,21 @@ where
renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
- use std::f32;
-
- let limits = limits
- .width(self.width)
- .height(Length::Shrink)
- .pad(self.padding);
-
- let text_size = self.text_size.unwrap_or(renderer.default_size());
- let font = self.font.clone();
-
- let max_width = match self.width {
- Length::Shrink => {
- let measure = |label: &str| -> u32 {
- let (width, _) = renderer.measure(
- label,
- text_size,
- font.clone(),
- Size::new(f32::INFINITY, f32::INFINITY),
- );
-
- width.round() as u32
- };
-
- let labels = self.options.iter().map(ToString::to_string);
-
- let labels_width =
- labels.map(|label| measure(&label)).max().unwrap_or(100);
-
- let placeholder_width = self
- .placeholder
- .as_ref()
- .map(String::as_str)
- .map(measure)
- .unwrap_or(100);
-
- labels_width.max(placeholder_width)
- }
- _ => 0,
- };
-
- let size = {
- let intrinsic = Size::new(
- max_width as f32
- + f32::from(text_size)
- + f32::from(self.padding.left),
- f32::from(text_size),
- );
-
- limits.resolve(intrinsic).pad(self.padding)
- };
-
- layout::Node::new(size)
+ layout(
+ renderer,
+ limits,
+ self.width,
+ self.padding,
+ self.text_size,
+ &self.font,
+ self.placeholder.as_deref(),
+ &self.options,
+ )
}
fn on_event(
&mut self,
+ tree: &mut Tree,
event: Event,
layout: Layout<'_>,
cursor_position: Point,
@@ -229,44 +159,228 @@ where
_clipboard: &mut dyn Clipboard,
shell: &mut Shell<'_, Message>,
) -> event::Status {
- match event {
- Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
- | Event::Touch(touch::Event::FingerPressed { .. }) => {
- let event_status = if *self.is_open {
- // TODO: Encode cursor availability in the type system
- *self.is_open =
- cursor_position.x < 0.0 || cursor_position.y < 0.0;
-
- event::Status::Captured
- } else if layout.bounds().contains(cursor_position) {
- let selected = self.selected.as_ref();
-
- *self.is_open = true;
- *self.hovered_option = self
- .options
- .iter()
- .position(|option| Some(option) == selected);
-
- event::Status::Captured
- } else {
- event::Status::Ignored
- };
+ update(
+ event,
+ layout,
+ cursor_position,
+ shell,
+ self.on_selected.as_ref(),
+ self.selected.as_ref(),
+ &self.options,
+ || tree.state.downcast_mut::<State<T>>(),
+ )
+ }
- if let Some(last_selection) = self.last_selection.take() {
- shell.publish((self.on_selected)(last_selection));
+ fn mouse_interaction(
+ &self,
+ _tree: &Tree,
+ layout: Layout<'_>,
+ cursor_position: Point,
+ _viewport: &Rectangle,
+ _renderer: &Renderer,
+ ) -> mouse::Interaction {
+ mouse_interaction(layout, cursor_position)
+ }
- *self.is_open = false;
+ fn draw(
+ &self,
+ _tree: &Tree,
+ renderer: &mut Renderer,
+ theme: &Renderer::Theme,
+ _style: &renderer::Style,
+ layout: Layout<'_>,
+ cursor_position: Point,
+ _viewport: &Rectangle,
+ ) {
+ draw(
+ renderer,
+ theme,
+ layout,
+ cursor_position,
+ self.padding,
+ self.text_size,
+ &self.font,
+ self.placeholder.as_deref(),
+ self.selected.as_ref(),
+ self.style,
+ )
+ }
- event::Status::Captured
- } else {
- event_status
- }
+ fn overlay<'b>(
+ &'b self,
+ tree: &'b mut Tree,
+ layout: Layout<'_>,
+ _renderer: &Renderer,
+ ) -> Option<overlay::Element<'b, Message, Renderer>> {
+ let state = tree.state.downcast_mut::<State<T>>();
+
+ overlay(
+ layout,
+ state,
+ self.padding,
+ self.text_size,
+ self.font.clone(),
+ &self.options,
+ self.style,
+ )
+ }
+}
+
+impl<'a, T: 'a, Message, Renderer> From<PickList<'a, T, Message, Renderer>>
+ for Element<'a, Message, Renderer>
+where
+ T: Clone + ToString + Eq + 'static,
+ [T]: ToOwned<Owned = Vec<T>>,
+ Message: 'a,
+ Renderer: text::Renderer + 'a,
+ Renderer::Theme: StyleSheet,
+{
+ fn from(pick_list: PickList<'a, T, Message, Renderer>) -> Self {
+ Self::new(pick_list)
+ }
+}
+
+/// The local state of a [`PickList`].
+#[derive(Debug)]
+pub struct State<T> {
+ menu: menu::State,
+ keyboard_modifiers: keyboard::Modifiers,
+ is_open: bool,
+ hovered_option: Option<usize>,
+ last_selection: Option<T>,
+}
+
+impl<T> State<T> {
+ /// Creates a new [`State`] for a [`PickList`].
+ pub fn new() -> Self {
+ Self {
+ menu: menu::State::default(),
+ keyboard_modifiers: keyboard::Modifiers::default(),
+ is_open: bool::default(),
+ hovered_option: Option::default(),
+ last_selection: Option::default(),
+ }
+ }
+}
+
+impl<T> Default for State<T> {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
+/// Computes the layout of a [`PickList`].
+pub fn layout<Renderer, T>(
+ renderer: &Renderer,
+ limits: &layout::Limits,
+ width: Length,
+ padding: Padding,
+ text_size: Option<u16>,
+ font: &Renderer::Font,
+ placeholder: Option<&str>,
+ options: &[T],
+) -> layout::Node
+where
+ Renderer: text::Renderer,
+ T: ToString,
+{
+ use std::f32;
+
+ let limits = limits.width(width).height(Length::Shrink).pad(padding);
+
+ let text_size = text_size.unwrap_or_else(|| renderer.default_size());
+
+ let max_width = match width {
+ Length::Shrink => {
+ let measure = |label: &str| -> u32 {
+ let (width, _) = renderer.measure(
+ label,
+ text_size,
+ font.clone(),
+ Size::new(f32::INFINITY, f32::INFINITY),
+ );
+
+ width.round() as u32
+ };
+
+ let labels = options.iter().map(ToString::to_string);
+
+ let labels_width =
+ labels.map(|label| measure(&label)).max().unwrap_or(100);
+
+ let placeholder_width = placeholder.map(measure).unwrap_or(100);
+
+ labels_width.max(placeholder_width)
+ }
+ _ => 0,
+ };
+
+ let size = {
+ let intrinsic = Size::new(
+ max_width as f32 + f32::from(text_size) + f32::from(padding.left),
+ f32::from(text_size),
+ );
+
+ limits.resolve(intrinsic).pad(padding)
+ };
+
+ layout::Node::new(size)
+}
+
+/// Processes an [`Event`] and updates the [`State`] of a [`PickList`]
+/// accordingly.
+pub fn update<'a, T, Message>(
+ event: Event,
+ layout: Layout<'_>,
+ cursor_position: Point,
+ shell: &mut Shell<'_, Message>,
+ on_selected: &dyn Fn(T) -> Message,
+ selected: Option<&T>,
+ options: &[T],
+ state: impl FnOnce() -> &'a mut State<T>,
+) -> event::Status
+where
+ T: PartialEq + Clone + 'a,
+{
+ match event {
+ Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
+ | Event::Touch(touch::Event::FingerPressed { .. }) => {
+ let state = state();
+
+ let event_status = if state.is_open {
+ // TODO: Encode cursor availability in the type system
+ state.is_open =
+ cursor_position.x < 0.0 || cursor_position.y < 0.0;
+
+ event::Status::Captured
+ } else if layout.bounds().contains(cursor_position) {
+ state.is_open = true;
+ state.hovered_option =
+ options.iter().position(|option| Some(option) == selected);
+
+ event::Status::Captured
+ } else {
+ event::Status::Ignored
+ };
+
+ if let Some(last_selection) = state.last_selection.take() {
+ shell.publish((on_selected)(last_selection));
+
+ state.is_open = false;
+
+ event::Status::Captured
+ } else {
+ event_status
}
- Event::Mouse(mouse::Event::WheelScrolled {
- delta: mouse::ScrollDelta::Lines { y, .. },
- }) if self.keyboard_modifiers.command()
+ }
+ Event::Mouse(mouse::Event::WheelScrolled {
+ delta: mouse::ScrollDelta::Lines { y, .. },
+ }) => {
+ let state = state();
+
+ if state.keyboard_modifiers.command()
&& layout.bounds().contains(cursor_position)
- && !*self.is_open =>
+ && !state.is_open
{
fn find_next<'a, T: PartialEq>(
selected: &'a T,
@@ -278,162 +392,170 @@ where
}
let next_option = if y < 0.0 {
- if let Some(selected) = self.selected.as_ref() {
- find_next(selected, self.options.iter())
+ if let Some(selected) = selected {
+ find_next(selected, options.iter())
} else {
- self.options.first()
+ options.first()
}
} else if y > 0.0 {
- if let Some(selected) = self.selected.as_ref() {
- find_next(selected, self.options.iter().rev())
+ if let Some(selected) = selected {
+ find_next(selected, options.iter().rev())
} else {
- self.options.last()
+ options.last()
}
} else {
None
};
if let Some(next_option) = next_option {
- shell.publish((self.on_selected)(next_option.clone()));
+ shell.publish((on_selected)(next_option.clone()));
}
event::Status::Captured
- }
- Event::Keyboard(keyboard::Event::ModifiersChanged(modifiers)) => {
- *self.keyboard_modifiers = modifiers;
-
+ } else {
event::Status::Ignored
}
- _ => event::Status::Ignored,
}
- }
+ Event::Keyboard(keyboard::Event::ModifiersChanged(modifiers)) => {
+ let state = state();
- fn mouse_interaction(
- &self,
- layout: Layout<'_>,
- cursor_position: Point,
- _viewport: &Rectangle,
- _renderer: &Renderer,
- ) -> mouse::Interaction {
- let bounds = layout.bounds();
- let is_mouse_over = bounds.contains(cursor_position);
+ state.keyboard_modifiers = modifiers;
- if is_mouse_over {
- mouse::Interaction::Pointer
- } else {
- mouse::Interaction::default()
+ event::Status::Ignored
}
+ _ => event::Status::Ignored,
}
+}
- fn draw(
- &self,
- renderer: &mut Renderer,
- _style: &renderer::Style,
- layout: Layout<'_>,
- cursor_position: Point,
- _viewport: &Rectangle,
- ) {
- let bounds = layout.bounds();
- let is_mouse_over = bounds.contains(cursor_position);
- let is_selected = self.selected.is_some();
-
- let style = if is_mouse_over {
- self.style_sheet.hovered()
- } else {
- self.style_sheet.active()
- };
-
- renderer.fill_quad(
- renderer::Quad {
- bounds,
- border_color: style.border_color,
- border_width: style.border_width,
- border_radius: style.border_radius,
- },
- style.background,
- );
-
- renderer.fill_text(Text {
- content: &Renderer::ARROW_DOWN_ICON.to_string(),
- font: Renderer::ICON_FONT,
- size: bounds.height * style.icon_size,
- bounds: Rectangle {
- x: bounds.x + bounds.width
- - f32::from(self.padding.horizontal()),
- y: bounds.center_y(),
- ..bounds
- },
- color: style.text_color,
- horizontal_alignment: alignment::Horizontal::Right,
- vertical_alignment: alignment::Vertical::Center,
- });
-
- if let Some(label) = self
- .selected
- .as_ref()
- .map(ToString::to_string)
- .as_ref()
- .or_else(|| self.placeholder.as_ref())
- {
- renderer.fill_text(Text {
- content: label,
- size: f32::from(
- self.text_size.unwrap_or(renderer.default_size()),
- ),
- font: self.font.clone(),
- color: is_selected
- .then(|| style.text_color)
- .unwrap_or(style.placeholder_color),
- bounds: Rectangle {
- x: bounds.x + f32::from(self.padding.left),
- y: bounds.center_y(),
- ..bounds
- },
- horizontal_alignment: alignment::Horizontal::Left,
- vertical_alignment: alignment::Vertical::Center,
- })
- }
+/// Returns the current [`mouse::Interaction`] of a [`PickList`].
+pub fn mouse_interaction(
+ layout: Layout<'_>,
+ cursor_position: Point,
+) -> mouse::Interaction {
+ let bounds = layout.bounds();
+ let is_mouse_over = bounds.contains(cursor_position);
+
+ if is_mouse_over {
+ mouse::Interaction::Pointer
+ } else {
+ mouse::Interaction::default()
}
+}
- fn overlay(
- &mut self,
- layout: Layout<'_>,
- _renderer: &Renderer,
- ) -> Option<overlay::Element<'_, Message, Renderer>> {
- if *self.is_open {
- let bounds = layout.bounds();
-
- let mut menu = Menu::new(
- &mut self.menu,
- &self.options,
- &mut self.hovered_option,
- &mut self.last_selection,
- )
- .width(bounds.width.round() as u16)
- .padding(self.padding)
- .font(self.font.clone())
- .style(self.style_sheet.menu());
-
- if let Some(text_size) = self.text_size {
- menu = menu.text_size(text_size);
- }
+/// Returns the current overlay of a [`PickList`].
+pub fn overlay<'a, T, Message, Renderer>(
+ layout: Layout<'_>,
+ state: &'a mut State<T>,
+ padding: Padding,
+ text_size: Option<u16>,
+ font: Renderer::Font,
+ options: &'a [T],
+ style: <Renderer::Theme as StyleSheet>::Style,
+) -> Option<overlay::Element<'a, Message, Renderer>>
+where
+ T: Clone + ToString,
+ Message: 'a,
+ Renderer: text::Renderer + 'a,
+ Renderer::Theme: StyleSheet,
+{
+ if state.is_open {
+ let bounds = layout.bounds();
- Some(menu.overlay(layout.position(), bounds.height))
- } else {
- None
+ let mut menu = Menu::new(
+ &mut state.menu,
+ options,
+ &mut state.hovered_option,
+ &mut state.last_selection,
+ )
+ .width(bounds.width.round() as u16)
+ .padding(padding)
+ .font(font)
+ .style(style);
+
+ if let Some(text_size) = text_size {
+ menu = menu.text_size(text_size);
}
+
+ Some(menu.overlay(layout.position(), bounds.height))
+ } else {
+ None
}
}
-impl<'a, T: 'a, Message, Renderer> Into<Element<'a, Message, Renderer>>
- for PickList<'a, T, Message, Renderer>
-where
- T: Clone + ToString + Eq,
- [T]: ToOwned<Owned = Vec<T>>,
- Renderer: text::Renderer + 'a,
- Message: 'static,
+/// Draws a [`PickList`].
+pub fn draw<T, Renderer>(
+ renderer: &mut Renderer,
+ theme: &Renderer::Theme,
+ layout: Layout<'_>,
+ cursor_position: Point,
+ padding: Padding,
+ text_size: Option<u16>,
+ font: &Renderer::Font,
+ placeholder: Option<&str>,
+ selected: Option<&T>,
+ style: <Renderer::Theme as StyleSheet>::Style,
+) where
+ Renderer: text::Renderer,
+ Renderer::Theme: StyleSheet,
+ T: ToString,
{
- fn into(self) -> Element<'a, Message, Renderer> {
- Element::new(self)
+ let bounds = layout.bounds();
+ let is_mouse_over = bounds.contains(cursor_position);
+ let is_selected = selected.is_some();
+
+ let style = if is_mouse_over {
+ theme.hovered(style)
+ } else {
+ theme.active(style)
+ };
+
+ renderer.fill_quad(
+ renderer::Quad {
+ bounds,
+ border_color: style.border_color,
+ border_width: style.border_width,
+ border_radius: style.border_radius,
+ },
+ style.background,
+ );
+
+ renderer.fill_text(Text {
+ content: &Renderer::ARROW_DOWN_ICON.to_string(),
+ font: Renderer::ICON_FONT,
+ size: bounds.height * style.icon_size,
+ bounds: Rectangle {
+ x: bounds.x + bounds.width - f32::from(padding.horizontal()),
+ y: bounds.center_y(),
+ ..bounds
+ },
+ color: style.text_color,
+ horizontal_alignment: alignment::Horizontal::Right,
+ vertical_alignment: alignment::Vertical::Center,
+ });
+
+ let label = selected.map(ToString::to_string);
+
+ if let Some(label) = label.as_deref().or(placeholder) {
+ let text_size =
+ f32::from(text_size.unwrap_or_else(|| renderer.default_size()));
+
+ renderer.fill_text(Text {
+ content: label,
+ size: text_size,
+ font: font.clone(),
+ color: if is_selected {
+ style.text_color
+ } else {
+ style.placeholder_color
+ },
+ bounds: Rectangle {
+ x: bounds.x + f32::from(padding.left),
+ y: bounds.center_y() - text_size / 2.0,
+ width: bounds.width - f32::from(padding.horizontal()),
+ height: text_size,
+ },
+ horizontal_alignment: alignment::Horizontal::Left,
+ vertical_alignment: alignment::Vertical::Top,
+ });
}
}
diff --git a/native/src/widget/progress_bar.rs b/native/src/widget/progress_bar.rs
index c26c38fa..8a945433 100644
--- a/native/src/widget/progress_bar.rs
+++ b/native/src/widget/progress_bar.rs
@@ -1,17 +1,18 @@
//! Provide progress feedback to your users.
use crate::layout;
use crate::renderer;
+use crate::widget::Tree;
use crate::{Color, Element, Layout, Length, Point, Rectangle, Size, Widget};
use std::ops::RangeInclusive;
-pub use iced_style::progress_bar::{Style, StyleSheet};
+pub use iced_style::progress_bar::{Appearance, StyleSheet};
/// A bar that displays progress.
///
/// # Example
/// ```
-/// # use iced_native::widget::ProgressBar;
+/// # type ProgressBar = iced_native::widget::ProgressBar<iced_native::renderer::Null>;
/// let value = 50.0;
///
/// ProgressBar::new(0.0..=100.0, value);
@@ -19,15 +20,23 @@ pub use iced_style::progress_bar::{Style, StyleSheet};
///
/// ![Progress bar drawn with `iced_wgpu`](https://user-images.githubusercontent.com/18618951/71662391-a316c200-2d51-11ea-9cef-52758cab85e3.png)
#[allow(missing_debug_implementations)]
-pub struct ProgressBar<'a> {
+pub struct ProgressBar<Renderer>
+where
+ Renderer: crate::Renderer,
+ Renderer::Theme: StyleSheet,
+{
range: RangeInclusive<f32>,
value: f32,
width: Length,
height: Option<Length>,
- style_sheet: Box<dyn StyleSheet + 'a>,
+ style: <Renderer::Theme as StyleSheet>::Style,
}
-impl<'a> ProgressBar<'a> {
+impl<Renderer> ProgressBar<Renderer>
+where
+ Renderer: crate::Renderer,
+ Renderer::Theme: StyleSheet,
+{
/// The default height of a [`ProgressBar`].
pub const DEFAULT_HEIGHT: u16 = 30;
@@ -42,7 +51,7 @@ impl<'a> ProgressBar<'a> {
range,
width: Length::Fill,
height: None,
- style_sheet: Default::default(),
+ style: Default::default(),
}
}
@@ -61,16 +70,17 @@ impl<'a> ProgressBar<'a> {
/// Sets the style of the [`ProgressBar`].
pub fn style(
mut self,
- style_sheet: impl Into<Box<dyn StyleSheet + 'a>>,
+ style: impl Into<<Renderer::Theme as StyleSheet>::Style>,
) -> Self {
- self.style_sheet = style_sheet.into();
+ self.style = style.into();
self
}
}
-impl<'a, Message, Renderer> Widget<Message, Renderer> for ProgressBar<'a>
+impl<Message, Renderer> Widget<Message, Renderer> for ProgressBar<Renderer>
where
Renderer: crate::Renderer,
+ Renderer::Theme: StyleSheet,
{
fn width(&self) -> Length {
self.width
@@ -96,7 +106,9 @@ where
fn draw(
&self,
+ _state: &Tree,
renderer: &mut Renderer,
+ theme: &Renderer::Theme,
_style: &renderer::Style,
layout: Layout<'_>,
_cursor_position: Point,
@@ -112,7 +124,7 @@ where
/ (range_end - range_start)
};
- let style = self.style_sheet.style();
+ let style = theme.appearance(self.style);
renderer.fill_quad(
renderer::Quad {
@@ -141,13 +153,16 @@ where
}
}
-impl<'a, Message, Renderer> From<ProgressBar<'a>>
+impl<'a, Message, Renderer> From<ProgressBar<Renderer>>
for Element<'a, Message, Renderer>
where
- Renderer: 'a + crate::Renderer,
Message: 'a,
+ Renderer: 'a + crate::Renderer,
+ Renderer::Theme: StyleSheet,
{
- fn from(progress_bar: ProgressBar<'a>) -> Element<'a, Message, Renderer> {
+ fn from(
+ progress_bar: ProgressBar<Renderer>,
+ ) -> Element<'a, Message, Renderer> {
Element::new(progress_bar)
}
}
diff --git a/native/src/widget/radio.rs b/native/src/widget/radio.rs
index fed2925b..c9152d05 100644
--- a/native/src/widget/radio.rs
+++ b/native/src/widget/radio.rs
@@ -6,20 +6,20 @@ use crate::mouse;
use crate::renderer;
use crate::text;
use crate::touch;
-use crate::widget::{self, Row, Text};
+use crate::widget::{self, Row, Text, Tree};
use crate::{
Alignment, Clipboard, Color, Element, Layout, Length, Point, Rectangle,
Shell, Widget,
};
-pub use iced_style::radio::{Style, StyleSheet};
+pub use iced_style::radio::{Appearance, StyleSheet};
/// A circular button representing a choice.
///
/// # Example
/// ```
-/// # type Radio<'a, Message> =
-/// # iced_native::widget::Radio<'a, Message, iced_native::renderer::Null>;
+/// # type Radio<Message> =
+/// # iced_native::widget::Radio<Message, iced_native::renderer::Null>;
/// #
/// #[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// pub enum Choice {
@@ -41,7 +41,11 @@ pub use iced_style::radio::{Style, StyleSheet};
///
/// ![Radio buttons drawn by `iced_wgpu`](https://github.com/iced-rs/iced/blob/7760618fb112074bc40b148944521f312152012a/docs/images/radio.png?raw=true)
#[allow(missing_debug_implementations)]
-pub struct Radio<'a, Message, Renderer: text::Renderer> {
+pub struct Radio<Message, Renderer>
+where
+ Renderer: text::Renderer,
+ Renderer::Theme: StyleSheet,
+{
is_selected: bool,
on_click: Message,
label: String,
@@ -50,12 +54,14 @@ pub struct Radio<'a, Message, Renderer: text::Renderer> {
spacing: u16,
text_size: Option<u16>,
font: Renderer::Font,
- style_sheet: Box<dyn StyleSheet + 'a>,
+ style: <Renderer::Theme as StyleSheet>::Style,
}
-impl<'a, Message, Renderer: text::Renderer> Radio<'a, Message, Renderer>
+impl<Message, Renderer> Radio<Message, Renderer>
where
Message: Clone,
+ Renderer: text::Renderer,
+ Renderer::Theme: StyleSheet,
{
/// The default size of a [`Radio`] button.
pub const DEFAULT_SIZE: u16 = 28;
@@ -79,7 +85,7 @@ where
) -> Self
where
V: Eq + Copy,
- F: 'static + Fn(V) -> Message,
+ F: FnOnce(V) -> Message,
{
Radio {
is_selected: Some(value) == selected,
@@ -90,7 +96,7 @@ where
spacing: Self::DEFAULT_SPACING, //15
text_size: None,
font: Default::default(),
- style_sheet: Default::default(),
+ style: Default::default(),
}
}
@@ -127,18 +133,18 @@ where
/// Sets the style of the [`Radio`] button.
pub fn style(
mut self,
- style_sheet: impl Into<Box<dyn StyleSheet + 'a>>,
+ style: impl Into<<Renderer::Theme as StyleSheet>::Style>,
) -> Self {
- self.style_sheet = style_sheet.into();
+ self.style = style.into();
self
}
}
-impl<'a, Message, Renderer> Widget<Message, Renderer>
- for Radio<'a, Message, Renderer>
+impl<Message, Renderer> Widget<Message, Renderer> for Radio<Message, Renderer>
where
Message: Clone,
Renderer: text::Renderer,
+ Renderer::Theme: StyleSheet + widget::text::StyleSheet,
{
fn width(&self) -> Length {
self.width
@@ -162,16 +168,15 @@ where
.width(Length::Units(self.size))
.height(Length::Units(self.size)),
)
- .push(
- Text::new(&self.label)
- .width(self.width)
- .size(self.text_size.unwrap_or(renderer.default_size())),
- )
+ .push(Text::new(&self.label).width(self.width).size(
+ self.text_size.unwrap_or_else(|| renderer.default_size()),
+ ))
.layout(renderer, limits)
}
fn on_event(
&mut self,
+ _state: &mut Tree,
event: Event,
layout: Layout<'_>,
cursor_position: Point,
@@ -196,6 +201,7 @@ where
fn mouse_interaction(
&self,
+ _state: &Tree,
layout: Layout<'_>,
cursor_position: Point,
_viewport: &Rectangle,
@@ -210,7 +216,9 @@ where
fn draw(
&self,
+ _state: &Tree,
renderer: &mut Renderer,
+ theme: &Renderer::Theme,
style: &renderer::Style,
layout: Layout<'_>,
cursor_position: Point,
@@ -222,9 +230,9 @@ where
let mut children = layout.children();
let custom_style = if is_mouse_over {
- self.style_sheet.hovered()
+ theme.hovered(self.style)
} else {
- self.style_sheet.active()
+ theme.active(self.style)
};
{
@@ -270,9 +278,11 @@ where
style,
label_layout,
&self.label,
- self.font.clone(),
self.text_size,
- custom_style.text_color,
+ self.font.clone(),
+ widget::text::Appearance {
+ color: custom_style.text_color,
+ },
alignment::Horizontal::Left,
alignment::Vertical::Center,
);
@@ -280,15 +290,14 @@ where
}
}
-impl<'a, Message, Renderer> From<Radio<'a, Message, Renderer>>
+impl<'a, Message, Renderer> From<Radio<Message, Renderer>>
for Element<'a, Message, Renderer>
where
Message: 'a + Clone,
Renderer: 'a + text::Renderer,
+ Renderer::Theme: StyleSheet + widget::text::StyleSheet,
{
- fn from(
- radio: Radio<'a, Message, Renderer>,
- ) -> Element<'a, Message, Renderer> {
+ fn from(radio: Radio<Message, Renderer>) -> Element<'a, Message, Renderer> {
Element::new(radio)
}
}
diff --git a/native/src/widget/row.rs b/native/src/widget/row.rs
index e34befb2..eda7c2d3 100644
--- a/native/src/widget/row.rs
+++ b/native/src/widget/row.rs
@@ -1,16 +1,15 @@
//! Distribute content horizontally.
use crate::event::{self, Event};
-use crate::layout;
+use crate::layout::{self, Layout};
use crate::mouse;
use crate::overlay;
use crate::renderer;
+use crate::widget::{Operation, Tree};
use crate::{
- Alignment, Clipboard, Element, Layout, Length, Padding, Point, Rectangle,
- Shell, Widget,
+ Alignment, Clipboard, Element, Length, Padding, Point, Rectangle, Shell,
+ Widget,
};
-use std::u32;
-
/// A container that distributes its contents horizontally.
#[allow(missing_debug_implementations)]
pub struct Row<'a, Message, Renderer> {
@@ -18,8 +17,6 @@ pub struct Row<'a, Message, Renderer> {
padding: Padding,
width: Length,
height: Length,
- max_width: u32,
- max_height: u32,
align_items: Alignment,
children: Vec<Element<'a, Message, Renderer>>,
}
@@ -39,8 +36,6 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> {
padding: Padding::ZERO,
width: Length::Shrink,
height: Length::Shrink,
- max_width: u32::MAX,
- max_height: u32::MAX,
align_items: Alignment::Start,
children,
}
@@ -48,7 +43,7 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> {
/// Sets the horizontal spacing _between_ elements.
///
- /// Custom margins per element do not exist in Iced. You should use this
+ /// Custom margins per element do not exist in iced. You should use this
/// method instead! While less flexible, it helps you keep spacing between
/// elements consistent.
pub fn spacing(mut self, units: u16) -> Self {
@@ -74,18 +69,6 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> {
self
}
- /// Sets the maximum width of the [`Row`].
- pub fn max_width(mut self, max_width: u32) -> Self {
- self.max_width = max_width;
- self
- }
-
- /// Sets the maximum height of the [`Row`].
- pub fn max_height(mut self, max_height: u32) -> Self {
- self.max_height = max_height;
- self
- }
-
/// Sets the vertical alignment of the contents of the [`Row`] .
pub fn align_items(mut self, align: Alignment) -> Self {
self.align_items = align;
@@ -93,20 +76,34 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> {
}
/// Adds an [`Element`] to the [`Row`].
- pub fn push<E>(mut self, child: E) -> Self
- where
- E: Into<Element<'a, Message, Renderer>>,
- {
+ pub fn push(
+ mut self,
+ child: impl Into<Element<'a, Message, Renderer>>,
+ ) -> Self {
self.children.push(child.into());
self
}
}
+impl<'a, Message, Renderer> Default for Row<'a, Message, Renderer> {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
impl<'a, Message, Renderer> Widget<Message, Renderer>
for Row<'a, Message, Renderer>
where
Renderer: crate::Renderer,
{
+ fn children(&self) -> Vec<Tree> {
+ self.children.iter().map(Tree::new).collect()
+ }
+
+ fn diff(&self, tree: &mut Tree) {
+ tree.diff_children(&self.children)
+ }
+
fn width(&self) -> Length {
self.width
}
@@ -120,11 +117,7 @@ where
renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
- let limits = limits
- .max_width(self.max_width)
- .max_height(self.max_height)
- .width(self.width)
- .height(self.height);
+ let limits = limits.width(self.width).height(self.height);
layout::flex::resolve(
layout::flex::Axis::Horizontal,
@@ -137,8 +130,26 @@ where
)
}
+ fn operate(
+ &self,
+ tree: &mut Tree,
+ layout: Layout<'_>,
+ operation: &mut dyn Operation<Message>,
+ ) {
+ operation.container(None, &mut |operation| {
+ self.children
+ .iter()
+ .zip(&mut tree.children)
+ .zip(layout.children())
+ .for_each(|((child, state), layout)| {
+ child.as_widget().operate(state, layout, operation);
+ })
+ });
+ }
+
fn on_event(
&mut self,
+ tree: &mut Tree,
event: Event,
layout: Layout<'_>,
cursor_position: Point,
@@ -148,9 +159,11 @@ where
) -> event::Status {
self.children
.iter_mut()
+ .zip(&mut tree.children)
.zip(layout.children())
- .map(|(child, layout)| {
- child.widget.on_event(
+ .map(|((child, state), layout)| {
+ child.as_widget_mut().on_event(
+ state,
event.clone(),
layout,
cursor_position,
@@ -164,6 +177,7 @@ where
fn mouse_interaction(
&self,
+ tree: &Tree,
layout: Layout<'_>,
cursor_position: Point,
viewport: &Rectangle,
@@ -171,9 +185,11 @@ where
) -> mouse::Interaction {
self.children
.iter()
+ .zip(&tree.children)
.zip(layout.children())
- .map(|(child, layout)| {
- child.widget.mouse_interaction(
+ .map(|((child, state), layout)| {
+ child.as_widget().mouse_interaction(
+ state,
layout,
cursor_position,
viewport,
@@ -186,39 +202,49 @@ where
fn draw(
&self,
+ tree: &Tree,
renderer: &mut Renderer,
+ theme: &Renderer::Theme,
style: &renderer::Style,
layout: Layout<'_>,
cursor_position: Point,
viewport: &Rectangle,
) {
- for (child, layout) in self.children.iter().zip(layout.children()) {
- child.draw(renderer, style, layout, cursor_position, viewport);
+ for ((child, state), layout) in self
+ .children
+ .iter()
+ .zip(&tree.children)
+ .zip(layout.children())
+ {
+ child.as_widget().draw(
+ state,
+ renderer,
+ theme,
+ style,
+ layout,
+ cursor_position,
+ viewport,
+ );
}
}
- fn overlay(
- &mut self,
+ fn overlay<'b>(
+ &'b self,
+ tree: &'b mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
- ) -> Option<overlay::Element<'_, Message, Renderer>> {
- self.children
- .iter_mut()
- .zip(layout.children())
- .filter_map(|(child, layout)| {
- child.widget.overlay(layout, renderer)
- })
- .next()
+ ) -> Option<overlay::Element<'b, Message, Renderer>> {
+ overlay::from_children(&self.children, tree, layout, renderer)
}
}
impl<'a, Message, Renderer> From<Row<'a, Message, Renderer>>
for Element<'a, Message, Renderer>
where
- Renderer: 'a + crate::Renderer,
Message: 'a,
+ Renderer: crate::Renderer + 'a,
{
- fn from(row: Row<'a, Message, Renderer>) -> Element<'a, Message, Renderer> {
- Element::new(row)
+ fn from(row: Row<'a, Message, Renderer>) -> Self {
+ Self::new(row)
}
}
diff --git a/native/src/widget/rule.rs b/native/src/widget/rule.rs
index b0cc3768..56f8c80d 100644
--- a/native/src/widget/rule.rs
+++ b/native/src/widget/rule.rs
@@ -1,53 +1,63 @@
//! Display a horizontal or vertical rule for dividing content.
use crate::layout;
use crate::renderer;
+use crate::widget::Tree;
use crate::{Color, Element, Layout, Length, Point, Rectangle, Size, Widget};
-pub use iced_style::rule::{FillMode, Style, StyleSheet};
+pub use iced_style::rule::{Appearance, FillMode, StyleSheet};
/// Display a horizontal or vertical rule for dividing content.
#[allow(missing_debug_implementations)]
-pub struct Rule<'a> {
+pub struct Rule<Renderer>
+where
+ Renderer: crate::Renderer,
+ Renderer::Theme: StyleSheet,
+{
width: Length,
height: Length,
is_horizontal: bool,
- style_sheet: Box<dyn StyleSheet + 'a>,
+ style: <Renderer::Theme as StyleSheet>::Style,
}
-impl<'a> Rule<'a> {
- /// Creates a horizontal [`Rule`] for dividing content by the given vertical spacing.
- pub fn horizontal(spacing: u16) -> Self {
+impl<Renderer> Rule<Renderer>
+where
+ Renderer: crate::Renderer,
+ Renderer::Theme: StyleSheet,
+{
+ /// Creates a horizontal [`Rule`] with the given height.
+ pub fn horizontal(height: u16) -> Self {
Rule {
width: Length::Fill,
- height: Length::from(Length::Units(spacing)),
+ height: Length::Units(height),
is_horizontal: true,
- style_sheet: Default::default(),
+ style: Default::default(),
}
}
- /// Creates a vertical [`Rule`] for dividing content by the given horizontal spacing.
- pub fn vertical(spacing: u16) -> Self {
+ /// Creates a vertical [`Rule`] with the given width.
+ pub fn vertical(width: u16) -> Self {
Rule {
- width: Length::from(Length::Units(spacing)),
+ width: Length::Units(width),
height: Length::Fill,
is_horizontal: false,
- style_sheet: Default::default(),
+ style: Default::default(),
}
}
/// Sets the style of the [`Rule`].
pub fn style(
mut self,
- style_sheet: impl Into<Box<dyn StyleSheet + 'a>>,
+ style: impl Into<<Renderer::Theme as StyleSheet>::Style>,
) -> Self {
- self.style_sheet = style_sheet.into();
+ self.style = style.into();
self
}
}
-impl<'a, Message, Renderer> Widget<Message, Renderer> for Rule<'a>
+impl<Message, Renderer> Widget<Message, Renderer> for Rule<Renderer>
where
Renderer: crate::Renderer,
+ Renderer::Theme: StyleSheet,
{
fn width(&self) -> Length {
self.width
@@ -69,14 +79,16 @@ where
fn draw(
&self,
+ _state: &Tree,
renderer: &mut Renderer,
+ theme: &Renderer::Theme,
_style: &renderer::Style,
layout: Layout<'_>,
_cursor_position: Point,
_viewport: &Rectangle,
) {
let bounds = layout.bounds();
- let style = self.style_sheet.style();
+ let style = theme.style(self.style);
let bounds = if self.is_horizontal {
let line_y = (bounds.y + (bounds.height / 2.0)
@@ -120,12 +132,14 @@ where
}
}
-impl<'a, Message, Renderer> From<Rule<'a>> for Element<'a, Message, Renderer>
+impl<'a, Message, Renderer> From<Rule<Renderer>>
+ for Element<'a, Message, Renderer>
where
- Renderer: 'a + crate::Renderer,
Message: 'a,
+ Renderer: 'a + crate::Renderer,
+ Renderer::Theme: StyleSheet,
{
- fn from(rule: Rule<'a>) -> Element<'a, Message, Renderer> {
+ fn from(rule: Rule<Renderer>) -> Element<'a, Message, Renderer> {
Element::new(rule)
}
}
diff --git a/native/src/widget/scrollable.rs b/native/src/widget/scrollable.rs
index ce734ad8..4ebb07a0 100644
--- a/native/src/widget/scrollable.rs
+++ b/native/src/widget/scrollable.rs
@@ -5,66 +5,65 @@ use crate::mouse;
use crate::overlay;
use crate::renderer;
use crate::touch;
-use crate::widget::Column;
+use crate::widget;
+use crate::widget::operation::{self, Operation};
+use crate::widget::tree::{self, Tree};
use crate::{
- Alignment, Background, Clipboard, Color, Element, Layout, Length, Padding,
- Point, Rectangle, Shell, Size, Vector, Widget,
+ Background, Clipboard, Color, Command, Element, Layout, Length, Point,
+ Rectangle, Shell, Size, Vector, Widget,
};
use std::{f32, u32};
pub use iced_style::scrollable::StyleSheet;
+pub mod style {
+ //! The styles of a [`Scrollable`].
+ //!
+ //! [`Scrollable`]: crate::widget::Scrollable
+ pub use iced_style::scrollable::{Scrollbar, Scroller};
+}
+
/// A widget that can vertically display an infinite amount of content with a
/// scrollbar.
#[allow(missing_debug_implementations)]
-pub struct Scrollable<'a, Message, Renderer> {
- state: &'a mut State,
+pub struct Scrollable<'a, Message, Renderer>
+where
+ Renderer: crate::Renderer,
+ Renderer::Theme: StyleSheet,
+{
+ id: Option<Id>,
height: Length,
- max_height: u32,
scrollbar_width: u16,
scrollbar_margin: u16,
scroller_width: u16,
- content: Column<'a, Message, Renderer>,
- on_scroll: Option<Box<dyn Fn(f32) -> Message>>,
- style_sheet: Box<dyn StyleSheet + 'a>,
+ content: Element<'a, Message, Renderer>,
+ on_scroll: Option<Box<dyn Fn(f32) -> Message + 'a>>,
+ style: <Renderer::Theme as StyleSheet>::Style,
}
-impl<'a, Message, Renderer: crate::Renderer> Scrollable<'a, Message, Renderer> {
- /// Creates a new [`Scrollable`] with the given [`State`].
- pub fn new(state: &'a mut State) -> Self {
+impl<'a, Message, Renderer> Scrollable<'a, Message, Renderer>
+where
+ Renderer: crate::Renderer,
+ Renderer::Theme: StyleSheet,
+{
+ /// Creates a new [`Scrollable`].
+ pub fn new(content: impl Into<Element<'a, Message, Renderer>>) -> Self {
Scrollable {
- state,
+ id: None,
height: Length::Shrink,
- max_height: u32::MAX,
scrollbar_width: 10,
scrollbar_margin: 0,
scroller_width: 10,
- content: Column::new(),
+ content: content.into(),
on_scroll: None,
- style_sheet: Default::default(),
+ style: Default::default(),
}
}
- /// Sets the vertical spacing _between_ elements.
- ///
- /// Custom margins per element do not exist in Iced. You should use this
- /// method instead! While less flexible, it helps you keep spacing between
- /// elements consistent.
- pub fn spacing(mut self, units: u16) -> Self {
- self.content = self.content.spacing(units);
- self
- }
-
- /// Sets the [`Padding`] of the [`Scrollable`].
- pub fn padding<P: Into<Padding>>(mut self, padding: P) -> Self {
- self.content = self.content.padding(padding);
- self
- }
-
- /// Sets the width of the [`Scrollable`].
- pub fn width(mut self, width: Length) -> Self {
- self.content = self.content.width(width);
+ /// Sets the [`Id`] of the [`Scrollable`].
+ pub fn id(mut self, id: Id) -> Self {
+ self.id = Some(id);
self
}
@@ -74,24 +73,6 @@ impl<'a, Message, Renderer: crate::Renderer> Scrollable<'a, Message, Renderer> {
self
}
- /// Sets the maximum width of the [`Scrollable`].
- pub fn max_width(mut self, max_width: u32) -> Self {
- self.content = self.content.max_width(max_width);
- self
- }
-
- /// Sets the maximum height of the [`Scrollable`] in pixels.
- pub fn max_height(mut self, max_height: u32) -> Self {
- self.max_height = max_height;
- self
- }
-
- /// Sets the horizontal alignment of the contents of the [`Scrollable`] .
- pub fn align_items(mut self, align_items: Alignment) -> Self {
- self.content = self.content.align_items(align_items);
- self
- }
-
/// Sets the scrollbar width of the [`Scrollable`] .
/// Silently enforces a minimum value of 1.
pub fn scrollbar_width(mut self, scrollbar_width: u16) -> Self {
@@ -117,7 +98,7 @@ impl<'a, Message, Renderer: crate::Renderer> Scrollable<'a, Message, Renderer> {
///
/// The function takes the new relative offset of the [`Scrollable`]
/// (e.g. `0` means top, while `1` means bottom).
- pub fn on_scroll(mut self, f: impl Fn(f32) -> Message + 'static) -> Self {
+ pub fn on_scroll(mut self, f: impl Fn(f32) -> Message + 'a) -> Self {
self.on_scroll = Some(Box::new(f));
self
}
@@ -125,97 +106,37 @@ impl<'a, Message, Renderer: crate::Renderer> Scrollable<'a, Message, Renderer> {
/// Sets the style of the [`Scrollable`] .
pub fn style(
mut self,
- style_sheet: impl Into<Box<dyn StyleSheet + 'a>>,
+ style: impl Into<<Renderer::Theme as StyleSheet>::Style>,
) -> Self {
- self.style_sheet = style_sheet.into();
+ self.style = style.into();
self
}
+}
- /// Adds an element to the [`Scrollable`].
- pub fn push<E>(mut self, child: E) -> Self
- where
- E: Into<Element<'a, Message, Renderer>>,
- {
- self.content = self.content.push(child);
- self
+impl<'a, Message, Renderer> Widget<Message, Renderer>
+ for Scrollable<'a, Message, Renderer>
+where
+ Renderer: crate::Renderer,
+ Renderer::Theme: StyleSheet,
+{
+ fn tag(&self) -> tree::Tag {
+ tree::Tag::of::<State>()
}
- fn notify_on_scroll(
- &self,
- bounds: Rectangle,
- content_bounds: Rectangle,
- shell: &mut Shell<'_, Message>,
- ) {
- if content_bounds.height <= bounds.height {
- return;
- }
+ fn state(&self) -> tree::State {
+ tree::State::new(State::new())
+ }
- if let Some(on_scroll) = &self.on_scroll {
- shell.publish(on_scroll(
- self.state.offset.absolute(bounds, content_bounds)
- / (content_bounds.height - bounds.height),
- ));
- }
+ fn children(&self) -> Vec<Tree> {
+ vec![Tree::new(&self.content)]
}
- fn scrollbar(
- &self,
- bounds: Rectangle,
- content_bounds: Rectangle,
- ) -> Option<Scrollbar> {
- let offset = self.state.offset(bounds, content_bounds);
-
- if content_bounds.height > bounds.height {
- let outer_width = self.scrollbar_width.max(self.scroller_width)
- + 2 * self.scrollbar_margin;
-
- let outer_bounds = Rectangle {
- x: bounds.x + bounds.width - outer_width as f32,
- y: bounds.y,
- width: outer_width as f32,
- height: bounds.height,
- };
-
- let scrollbar_bounds = Rectangle {
- x: bounds.x + bounds.width
- - f32::from(outer_width / 2 + self.scrollbar_width / 2),
- y: bounds.y,
- width: self.scrollbar_width as f32,
- height: bounds.height,
- };
-
- let ratio = bounds.height / content_bounds.height;
- let scroller_height = bounds.height * ratio;
- let y_offset = offset as f32 * ratio;
-
- let scroller_bounds = Rectangle {
- x: bounds.x + bounds.width
- - f32::from(outer_width / 2 + self.scroller_width / 2),
- y: scrollbar_bounds.y + y_offset,
- width: self.scroller_width as f32,
- height: scroller_height,
- };
-
- Some(Scrollbar {
- outer_bounds,
- bounds: scrollbar_bounds,
- scroller: Scroller {
- bounds: scroller_bounds,
- },
- })
- } else {
- None
- }
+ fn diff(&self, tree: &mut Tree) {
+ tree.diff_children(std::slice::from_ref(&self.content))
}
-}
-impl<'a, Message, Renderer> Widget<Message, Renderer>
- for Scrollable<'a, Message, Renderer>
-where
- Renderer: crate::Renderer,
-{
fn width(&self) -> Length {
- Widget::<Message, Renderer>::width(&self.content)
+ self.content.as_widget().width()
}
fn height(&self) -> Length {
@@ -227,24 +148,40 @@ where
renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
- let limits = limits
- .max_height(self.max_height)
- .width(Widget::<Message, Renderer>::width(&self.content))
- .height(self.height);
-
- let child_limits = layout::Limits::new(
- Size::new(limits.min().width, 0.0),
- Size::new(limits.max().width, f32::INFINITY),
- );
+ layout(
+ renderer,
+ limits,
+ Widget::<Message, Renderer>::width(self),
+ self.height,
+ u32::MAX,
+ |renderer, limits| {
+ self.content.as_widget().layout(renderer, limits)
+ },
+ )
+ }
+
+ fn operate(
+ &self,
+ tree: &mut Tree,
+ layout: Layout<'_>,
+ operation: &mut dyn Operation<Message>,
+ ) {
+ let state = tree.state.downcast_mut::<State>();
- let content = self.content.layout(renderer, &child_limits);
- let size = limits.resolve(content.size());
+ operation.scrollable(state, self.id.as_ref().map(|id| &id.0));
- layout::Node::with_children(size, vec![content])
+ operation.container(None, &mut |operation| {
+ self.content.as_widget().operate(
+ &mut tree.children[0],
+ layout.children().next().unwrap(),
+ operation,
+ );
+ });
}
fn on_event(
&mut self,
+ tree: &mut Tree,
event: Event,
layout: Layout<'_>,
cursor_position: Point,
@@ -252,122 +189,351 @@ where
clipboard: &mut dyn Clipboard,
shell: &mut Shell<'_, Message>,
) -> event::Status {
- let bounds = layout.bounds();
- let is_mouse_over = bounds.contains(cursor_position);
-
- let content = layout.children().next().unwrap();
- let content_bounds = content.bounds();
-
- let scrollbar = self.scrollbar(bounds, content_bounds);
- let is_mouse_over_scrollbar = scrollbar
- .as_ref()
- .map(|scrollbar| scrollbar.is_mouse_over(cursor_position))
- .unwrap_or(false);
-
- let event_status = {
- let cursor_position = if is_mouse_over && !is_mouse_over_scrollbar {
- Point::new(
- cursor_position.x,
- cursor_position.y
- + self.state.offset(bounds, content_bounds) as f32,
+ update(
+ tree.state.downcast_mut::<State>(),
+ event,
+ layout,
+ cursor_position,
+ clipboard,
+ shell,
+ self.scrollbar_width,
+ self.scrollbar_margin,
+ self.scroller_width,
+ &self.on_scroll,
+ |event, layout, cursor_position, clipboard, shell| {
+ self.content.as_widget_mut().on_event(
+ &mut tree.children[0],
+ event,
+ layout,
+ cursor_position,
+ renderer,
+ clipboard,
+ shell,
)
- } else {
- // TODO: Make `cursor_position` an `Option<Point>` so we can encode
- // cursor availability.
- // This will probably happen naturally once we add multi-window
- // support.
- Point::new(cursor_position.x, -1.0)
- };
-
- self.content.on_event(
- event.clone(),
- content,
- cursor_position,
+ },
+ )
+ }
+
+ fn draw(
+ &self,
+ tree: &Tree,
+ renderer: &mut Renderer,
+ theme: &Renderer::Theme,
+ style: &renderer::Style,
+ layout: Layout<'_>,
+ cursor_position: Point,
+ _viewport: &Rectangle,
+ ) {
+ draw(
+ tree.state.downcast_ref::<State>(),
+ renderer,
+ theme,
+ layout,
+ cursor_position,
+ self.scrollbar_width,
+ self.scrollbar_margin,
+ self.scroller_width,
+ self.style,
+ |renderer, layout, cursor_position, viewport| {
+ self.content.as_widget().draw(
+ &tree.children[0],
+ renderer,
+ theme,
+ style,
+ layout,
+ cursor_position,
+ viewport,
+ )
+ },
+ )
+ }
+
+ fn mouse_interaction(
+ &self,
+ tree: &Tree,
+ layout: Layout<'_>,
+ cursor_position: Point,
+ _viewport: &Rectangle,
+ renderer: &Renderer,
+ ) -> mouse::Interaction {
+ mouse_interaction(
+ tree.state.downcast_ref::<State>(),
+ layout,
+ cursor_position,
+ self.scrollbar_width,
+ self.scrollbar_margin,
+ self.scroller_width,
+ |layout, cursor_position, viewport| {
+ self.content.as_widget().mouse_interaction(
+ &tree.children[0],
+ layout,
+ cursor_position,
+ viewport,
+ renderer,
+ )
+ },
+ )
+ }
+
+ fn overlay<'b>(
+ &'b self,
+ tree: &'b mut Tree,
+ layout: Layout<'_>,
+ renderer: &Renderer,
+ ) -> Option<overlay::Element<'b, Message, Renderer>> {
+ self.content
+ .as_widget()
+ .overlay(
+ &mut tree.children[0],
+ layout.children().next().unwrap(),
renderer,
- clipboard,
- shell,
)
+ .map(|overlay| {
+ let bounds = layout.bounds();
+ let content_layout = layout.children().next().unwrap();
+ let content_bounds = content_layout.bounds();
+ let offset = tree
+ .state
+ .downcast_ref::<State>()
+ .offset(bounds, content_bounds);
+
+ overlay.translate(Vector::new(0.0, -(offset as f32)))
+ })
+ }
+}
+
+impl<'a, Message, Renderer> From<Scrollable<'a, Message, Renderer>>
+ for Element<'a, Message, Renderer>
+where
+ Message: 'a,
+ Renderer: 'a + crate::Renderer,
+ Renderer::Theme: StyleSheet,
+{
+ fn from(
+ text_input: Scrollable<'a, Message, Renderer>,
+ ) -> Element<'a, Message, Renderer> {
+ Element::new(text_input)
+ }
+}
+
+/// The identifier of a [`Scrollable`].
+#[derive(Debug, Clone, PartialEq, Eq, Hash)]
+pub struct Id(widget::Id);
+
+impl Id {
+ /// Creates a custom [`Id`].
+ pub fn new(id: impl Into<std::borrow::Cow<'static, str>>) -> Self {
+ Self(widget::Id::new(id))
+ }
+
+ /// Creates a unique [`Id`].
+ ///
+ /// This function produces a different [`Id`] every time it is called.
+ pub fn unique() -> Self {
+ Self(widget::Id::unique())
+ }
+}
+
+/// Produces a [`Command`] that snaps the [`Scrollable`] with the given [`Id`]
+/// to the provided `percentage`.
+pub fn snap_to<Message: 'static>(id: Id, percentage: f32) -> Command<Message> {
+ Command::widget(operation::scrollable::snap_to(id.0, percentage))
+}
+
+/// Computes the layout of a [`Scrollable`].
+pub fn layout<Renderer>(
+ renderer: &Renderer,
+ limits: &layout::Limits,
+ width: Length,
+ height: Length,
+ max_height: u32,
+ layout_content: impl FnOnce(&Renderer, &layout::Limits) -> layout::Node,
+) -> layout::Node {
+ let limits = limits.max_height(max_height).width(width).height(height);
+
+ let child_limits = layout::Limits::new(
+ Size::new(limits.min().width, 0.0),
+ Size::new(limits.max().width, f32::INFINITY),
+ );
+
+ let content = layout_content(renderer, &child_limits);
+ let size = limits.resolve(content.size());
+
+ layout::Node::with_children(size, vec![content])
+}
+
+/// Processes an [`Event`] and updates the [`State`] of a [`Scrollable`]
+/// accordingly.
+pub fn update<Message>(
+ state: &mut State,
+ event: Event,
+ layout: Layout<'_>,
+ cursor_position: Point,
+ clipboard: &mut dyn Clipboard,
+ shell: &mut Shell<'_, Message>,
+ scrollbar_width: u16,
+ scrollbar_margin: u16,
+ scroller_width: u16,
+ on_scroll: &Option<Box<dyn Fn(f32) -> Message + '_>>,
+ update_content: impl FnOnce(
+ Event,
+ Layout<'_>,
+ Point,
+ &mut dyn Clipboard,
+ &mut Shell<'_, Message>,
+ ) -> event::Status,
+) -> event::Status {
+ let bounds = layout.bounds();
+ let is_mouse_over = bounds.contains(cursor_position);
+
+ let content = layout.children().next().unwrap();
+ let content_bounds = content.bounds();
+
+ let scrollbar = scrollbar(
+ state,
+ scrollbar_width,
+ scrollbar_margin,
+ scroller_width,
+ bounds,
+ content_bounds,
+ );
+ let is_mouse_over_scrollbar = scrollbar
+ .as_ref()
+ .map(|scrollbar| scrollbar.is_mouse_over(cursor_position))
+ .unwrap_or(false);
+
+ let event_status = {
+ let cursor_position = if is_mouse_over && !is_mouse_over_scrollbar {
+ Point::new(
+ cursor_position.x,
+ cursor_position.y + state.offset(bounds, content_bounds) as f32,
+ )
+ } else {
+ // TODO: Make `cursor_position` an `Option<Point>` so we can encode
+ // cursor availability.
+ // This will probably happen naturally once we add multi-window
+ // support.
+ Point::new(cursor_position.x, -1.0)
};
- if let event::Status::Captured = event_status {
- return event::Status::Captured;
- }
+ update_content(
+ event.clone(),
+ content,
+ cursor_position,
+ clipboard,
+ shell,
+ )
+ };
+
+ if let event::Status::Captured = event_status {
+ return event::Status::Captured;
+ }
+
+ if is_mouse_over {
+ match event {
+ Event::Mouse(mouse::Event::WheelScrolled { delta }) => {
+ match delta {
+ mouse::ScrollDelta::Lines { y, .. } => {
+ // TODO: Configurable speed (?)
+ state.scroll(y * 60.0, bounds, content_bounds);
+ }
+ mouse::ScrollDelta::Pixels { y, .. } => {
+ state.scroll(y, bounds, content_bounds);
+ }
+ }
- if is_mouse_over {
- match event {
- Event::Mouse(mouse::Event::WheelScrolled { delta }) => {
- match delta {
- mouse::ScrollDelta::Lines { y, .. } => {
- // TODO: Configurable speed (?)
- self.state.scroll(y * 60.0, bounds, content_bounds);
- }
- mouse::ScrollDelta::Pixels { y, .. } => {
- self.state.scroll(y, bounds, content_bounds);
- }
+ notify_on_scroll(
+ state,
+ on_scroll,
+ bounds,
+ content_bounds,
+ shell,
+ );
+
+ return event::Status::Captured;
+ }
+ Event::Touch(event) => {
+ match event {
+ touch::Event::FingerPressed { .. } => {
+ state.scroll_box_touched_at = Some(cursor_position);
}
+ touch::Event::FingerMoved { .. } => {
+ if let Some(scroll_box_touched_at) =
+ state.scroll_box_touched_at
+ {
+ let delta =
+ cursor_position.y - scroll_box_touched_at.y;
- self.notify_on_scroll(bounds, content_bounds, shell);
+ state.scroll(delta, bounds, content_bounds);
- return event::Status::Captured;
- }
- Event::Touch(event) => {
- match event {
- touch::Event::FingerPressed { .. } => {
- self.state.scroll_box_touched_at =
- Some(cursor_position);
- }
- touch::Event::FingerMoved { .. } => {
- if let Some(scroll_box_touched_at) =
- self.state.scroll_box_touched_at
- {
- let delta =
- cursor_position.y - scroll_box_touched_at.y;
-
- self.state.scroll(
- delta,
- bounds,
- content_bounds,
- );
-
- self.state.scroll_box_touched_at =
- Some(cursor_position);
-
- self.notify_on_scroll(
- bounds,
- content_bounds,
- shell,
- );
- }
- }
- touch::Event::FingerLifted { .. }
- | touch::Event::FingerLost { .. } => {
- self.state.scroll_box_touched_at = None;
+ state.scroll_box_touched_at = Some(cursor_position);
+
+ notify_on_scroll(
+ state,
+ on_scroll,
+ bounds,
+ content_bounds,
+ shell,
+ );
}
}
-
- return event::Status::Captured;
+ touch::Event::FingerLifted { .. }
+ | touch::Event::FingerLost { .. } => {
+ state.scroll_box_touched_at = None;
+ }
}
- _ => {}
+
+ return event::Status::Captured;
}
+ _ => {}
}
+ }
- if self.state.is_scroller_grabbed() {
- match event {
- Event::Mouse(mouse::Event::ButtonReleased(
- mouse::Button::Left,
- ))
- | Event::Touch(touch::Event::FingerLifted { .. })
- | Event::Touch(touch::Event::FingerLost { .. }) => {
- self.state.scroller_grabbed_at = None;
+ if state.is_scroller_grabbed() {
+ match event {
+ Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left))
+ | Event::Touch(touch::Event::FingerLifted { .. })
+ | Event::Touch(touch::Event::FingerLost { .. }) => {
+ state.scroller_grabbed_at = None;
+
+ return event::Status::Captured;
+ }
+ Event::Mouse(mouse::Event::CursorMoved { .. })
+ | Event::Touch(touch::Event::FingerMoved { .. }) => {
+ if let (Some(scrollbar), Some(scroller_grabbed_at)) =
+ (scrollbar, state.scroller_grabbed_at)
+ {
+ state.scroll_to(
+ scrollbar.scroll_percentage(
+ scroller_grabbed_at,
+ cursor_position,
+ ),
+ bounds,
+ content_bounds,
+ );
+
+ notify_on_scroll(
+ state,
+ on_scroll,
+ bounds,
+ content_bounds,
+ shell,
+ );
return event::Status::Captured;
}
- Event::Mouse(mouse::Event::CursorMoved { .. })
- | Event::Touch(touch::Event::FingerMoved { .. }) => {
- if let (Some(scrollbar), Some(scroller_grabbed_at)) =
- (scrollbar, self.state.scroller_grabbed_at)
+ }
+ _ => {}
+ }
+ } else if is_mouse_over_scrollbar {
+ match event {
+ Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
+ | Event::Touch(touch::Event::FingerPressed { .. }) => {
+ if let Some(scrollbar) = scrollbar {
+ if let Some(scroller_grabbed_at) =
+ scrollbar.grab_scroller(cursor_position)
{
- self.state.scroll_to(
+ state.scroll_to(
scrollbar.scroll_percentage(
scroller_grabbed_at,
cursor_position,
@@ -376,112 +542,63 @@ where
content_bounds,
);
- self.notify_on_scroll(bounds, content_bounds, shell);
+ state.scroller_grabbed_at = Some(scroller_grabbed_at);
- return event::Status::Captured;
- }
- }
- _ => {}
- }
- } else if is_mouse_over_scrollbar {
- match event {
- Event::Mouse(mouse::Event::ButtonPressed(
- mouse::Button::Left,
- ))
- | Event::Touch(touch::Event::FingerPressed { .. }) => {
- if let Some(scrollbar) = scrollbar {
- if let Some(scroller_grabbed_at) =
- scrollbar.grab_scroller(cursor_position)
- {
- self.state.scroll_to(
- scrollbar.scroll_percentage(
- scroller_grabbed_at,
- cursor_position,
- ),
- bounds,
- content_bounds,
- );
-
- self.state.scroller_grabbed_at =
- Some(scroller_grabbed_at);
-
- self.notify_on_scroll(
- bounds,
- content_bounds,
- shell,
- );
+ notify_on_scroll(
+ state,
+ on_scroll,
+ bounds,
+ content_bounds,
+ shell,
+ );
- return event::Status::Captured;
- }
+ return event::Status::Captured;
}
}
- _ => {}
}
+ _ => {}
}
-
- event::Status::Ignored
}
- fn mouse_interaction(
- &self,
- layout: Layout<'_>,
- cursor_position: Point,
- _viewport: &Rectangle,
- renderer: &Renderer,
- ) -> mouse::Interaction {
- let bounds = layout.bounds();
- let content_layout = layout.children().next().unwrap();
- let content_bounds = content_layout.bounds();
- let scrollbar = self.scrollbar(bounds, content_bounds);
-
- let is_mouse_over = bounds.contains(cursor_position);
- let is_mouse_over_scrollbar = scrollbar
- .as_ref()
- .map(|scrollbar| scrollbar.is_mouse_over(cursor_position))
- .unwrap_or(false);
-
- if is_mouse_over_scrollbar || self.state.is_scroller_grabbed() {
- mouse::Interaction::Idle
- } else {
- let offset = self.state.offset(bounds, content_bounds);
-
- let cursor_position = if is_mouse_over && !is_mouse_over_scrollbar {
- Point::new(cursor_position.x, cursor_position.y + offset as f32)
- } else {
- Point::new(cursor_position.x, -1.0)
- };
-
- self.content.mouse_interaction(
- content_layout,
- cursor_position,
- &Rectangle {
- y: bounds.y + offset as f32,
- ..bounds
- },
- renderer,
- )
- }
- }
+ event::Status::Ignored
+}
- fn draw(
- &self,
- renderer: &mut Renderer,
- style: &renderer::Style,
- layout: Layout<'_>,
- cursor_position: Point,
- _viewport: &Rectangle,
- ) {
- let bounds = layout.bounds();
- let content_layout = layout.children().next().unwrap();
- let content_bounds = content_layout.bounds();
- let offset = self.state.offset(bounds, content_bounds);
- let scrollbar = self.scrollbar(bounds, content_bounds);
-
- let is_mouse_over = bounds.contains(cursor_position);
- let is_mouse_over_scrollbar = scrollbar
- .as_ref()
- .map(|scrollbar| scrollbar.is_mouse_over(cursor_position))
- .unwrap_or(false);
+/// Computes the current [`mouse::Interaction`] of a [`Scrollable`].
+pub fn mouse_interaction(
+ state: &State,
+ layout: Layout<'_>,
+ cursor_position: Point,
+ scrollbar_width: u16,
+ scrollbar_margin: u16,
+ scroller_width: u16,
+ content_interaction: impl FnOnce(
+ Layout<'_>,
+ Point,
+ &Rectangle,
+ ) -> mouse::Interaction,
+) -> mouse::Interaction {
+ let bounds = layout.bounds();
+ let content_layout = layout.children().next().unwrap();
+ let content_bounds = content_layout.bounds();
+ let scrollbar = scrollbar(
+ state,
+ scrollbar_width,
+ scrollbar_margin,
+ scroller_width,
+ bounds,
+ content_bounds,
+ );
+
+ let is_mouse_over = bounds.contains(cursor_position);
+ let is_mouse_over_scrollbar = scrollbar
+ .as_ref()
+ .map(|scrollbar| scrollbar.is_mouse_over(cursor_position))
+ .unwrap_or(false);
+
+ if is_mouse_over_scrollbar || state.is_scroller_grabbed() {
+ mouse::Interaction::Idle
+ } else {
+ let offset = state.offset(bounds, content_bounds);
let cursor_position = if is_mouse_over && !is_mouse_over_scrollbar {
Point::new(cursor_position.x, cursor_position.y + offset as f32)
@@ -489,104 +606,206 @@ where
Point::new(cursor_position.x, -1.0)
};
- if let Some(scrollbar) = scrollbar {
- renderer.with_layer(bounds, |renderer| {
- renderer.with_translation(
- Vector::new(0.0, -(offset as f32)),
- |renderer| {
- self.content.draw(
- renderer,
- style,
- content_layout,
- cursor_position,
- &Rectangle {
- y: bounds.y + offset as f32,
- ..bounds
- },
- );
- },
- );
- });
-
- let style = if self.state.is_scroller_grabbed() {
- self.style_sheet.dragging()
- } else if is_mouse_over_scrollbar {
- self.style_sheet.hovered()
- } else {
- self.style_sheet.active()
- };
-
- let is_scrollbar_visible =
- style.background.is_some() || style.border_width > 0.0;
+ content_interaction(
+ content_layout,
+ cursor_position,
+ &Rectangle {
+ y: bounds.y + offset as f32,
+ ..bounds
+ },
+ )
+ }
+}
- renderer.with_layer(
- Rectangle {
- width: bounds.width + 2.0,
- height: bounds.height + 2.0,
- ..bounds
- },
+/// Draws a [`Scrollable`].
+pub fn draw<Renderer>(
+ state: &State,
+ renderer: &mut Renderer,
+ theme: &Renderer::Theme,
+ layout: Layout<'_>,
+ cursor_position: Point,
+ scrollbar_width: u16,
+ scrollbar_margin: u16,
+ scroller_width: u16,
+ style: <Renderer::Theme as StyleSheet>::Style,
+ draw_content: impl FnOnce(&mut Renderer, Layout<'_>, Point, &Rectangle),
+) where
+ Renderer: crate::Renderer,
+ Renderer::Theme: StyleSheet,
+{
+ let bounds = layout.bounds();
+ let content_layout = layout.children().next().unwrap();
+ let content_bounds = content_layout.bounds();
+ let offset = state.offset(bounds, content_bounds);
+ let scrollbar = scrollbar(
+ state,
+ scrollbar_width,
+ scrollbar_margin,
+ scroller_width,
+ bounds,
+ content_bounds,
+ );
+
+ let is_mouse_over = bounds.contains(cursor_position);
+ let is_mouse_over_scrollbar = scrollbar
+ .as_ref()
+ .map(|scrollbar| scrollbar.is_mouse_over(cursor_position))
+ .unwrap_or(false);
+
+ let cursor_position = if is_mouse_over && !is_mouse_over_scrollbar {
+ Point::new(cursor_position.x, cursor_position.y + offset as f32)
+ } else {
+ Point::new(cursor_position.x, -1.0)
+ };
+
+ if let Some(scrollbar) = scrollbar {
+ renderer.with_layer(bounds, |renderer| {
+ renderer.with_translation(
+ Vector::new(0.0, -(offset as f32)),
|renderer| {
- if is_scrollbar_visible {
- renderer.fill_quad(
- renderer::Quad {
- bounds: scrollbar.bounds,
- border_radius: style.border_radius,
- border_width: style.border_width,
- border_color: style.border_color,
- },
- style.background.unwrap_or(Background::Color(
- Color::TRANSPARENT,
- )),
- );
- }
-
- if is_mouse_over
- || self.state.is_scroller_grabbed()
- || is_scrollbar_visible
- {
- renderer.fill_quad(
- renderer::Quad {
- bounds: scrollbar.scroller.bounds,
- border_radius: style.scroller.border_radius,
- border_width: style.scroller.border_width,
- border_color: style.scroller.border_color,
- },
- style.scroller.color,
- );
- }
+ draw_content(
+ renderer,
+ content_layout,
+ cursor_position,
+ &Rectangle {
+ y: bounds.y + offset as f32,
+ ..bounds
+ },
+ );
},
);
+ });
+
+ let style = if state.is_scroller_grabbed() {
+ theme.dragging(style)
+ } else if is_mouse_over_scrollbar {
+ theme.hovered(style)
} else {
- self.content.draw(
- renderer,
- style,
- content_layout,
- cursor_position,
- &Rectangle {
- y: bounds.y + offset as f32,
- ..bounds
- },
- );
- }
+ theme.active(style)
+ };
+
+ let is_scrollbar_visible =
+ style.background.is_some() || style.border_width > 0.0;
+
+ renderer.with_layer(
+ Rectangle {
+ width: bounds.width + 2.0,
+ height: bounds.height + 2.0,
+ ..bounds
+ },
+ |renderer| {
+ if is_scrollbar_visible {
+ renderer.fill_quad(
+ renderer::Quad {
+ bounds: scrollbar.bounds,
+ border_radius: style.border_radius,
+ border_width: style.border_width,
+ border_color: style.border_color,
+ },
+ style
+ .background
+ .unwrap_or(Background::Color(Color::TRANSPARENT)),
+ );
+ }
+
+ if is_mouse_over
+ || state.is_scroller_grabbed()
+ || is_scrollbar_visible
+ {
+ renderer.fill_quad(
+ renderer::Quad {
+ bounds: scrollbar.scroller.bounds,
+ border_radius: style.scroller.border_radius,
+ border_width: style.scroller.border_width,
+ border_color: style.scroller.border_color,
+ },
+ style.scroller.color,
+ );
+ }
+ },
+ );
+ } else {
+ draw_content(
+ renderer,
+ content_layout,
+ cursor_position,
+ &Rectangle {
+ y: bounds.y + offset as f32,
+ ..bounds
+ },
+ );
}
+}
- fn overlay(
- &mut self,
- layout: Layout<'_>,
- renderer: &Renderer,
- ) -> Option<overlay::Element<'_, Message, Renderer>> {
- let Self { content, state, .. } = self;
+fn scrollbar(
+ state: &State,
+ scrollbar_width: u16,
+ scrollbar_margin: u16,
+ scroller_width: u16,
+ bounds: Rectangle,
+ content_bounds: Rectangle,
+) -> Option<Scrollbar> {
+ let offset = state.offset(bounds, content_bounds);
+
+ if content_bounds.height > bounds.height {
+ let outer_width =
+ scrollbar_width.max(scroller_width) + 2 * scrollbar_margin;
+
+ let outer_bounds = Rectangle {
+ x: bounds.x + bounds.width - outer_width as f32,
+ y: bounds.y,
+ width: outer_width as f32,
+ height: bounds.height,
+ };
- content
- .overlay(layout.children().next().unwrap(), renderer)
- .map(|overlay| {
- let bounds = layout.bounds();
- let content_layout = layout.children().next().unwrap();
- let content_bounds = content_layout.bounds();
- let offset = state.offset(bounds, content_bounds);
+ let scrollbar_bounds = Rectangle {
+ x: bounds.x + bounds.width
+ - f32::from(outer_width / 2 + scrollbar_width / 2),
+ y: bounds.y,
+ width: scrollbar_width as f32,
+ height: bounds.height,
+ };
- overlay.translate(Vector::new(0.0, -(offset as f32)))
- })
+ let ratio = bounds.height / content_bounds.height;
+ let scroller_height = bounds.height * ratio;
+ let y_offset = offset as f32 * ratio;
+
+ let scroller_bounds = Rectangle {
+ x: bounds.x + bounds.width
+ - f32::from(outer_width / 2 + scroller_width / 2),
+ y: scrollbar_bounds.y + y_offset,
+ width: scroller_width as f32,
+ height: scroller_height,
+ };
+
+ Some(Scrollbar {
+ outer_bounds,
+ bounds: scrollbar_bounds,
+ scroller: Scroller {
+ bounds: scroller_bounds,
+ },
+ })
+ } else {
+ None
+ }
+}
+
+fn notify_on_scroll<Message>(
+ state: &State,
+ on_scroll: &Option<Box<dyn Fn(f32) -> Message + '_>>,
+ bounds: Rectangle,
+ content_bounds: Rectangle,
+ shell: &mut Shell<'_, Message>,
+) {
+ if content_bounds.height <= bounds.height {
+ return;
+ }
+
+ if let Some(on_scroll) = on_scroll {
+ shell.publish(on_scroll(
+ state.offset.absolute(bounds, content_bounds)
+ / (content_bounds.height - bounds.height),
+ ));
}
}
@@ -608,6 +827,12 @@ impl Default for State {
}
}
+impl operation::Scrollable for State {
+ fn snap_to(&mut self, percentage: f32) {
+ State::snap_to(self, percentage);
+ }
+}
+
/// The local state of a [`Scrollable`].
#[derive(Debug, Clone, Copy)]
enum Offset {
@@ -752,16 +977,3 @@ struct Scroller {
/// The bounds of the [`Scroller`].
bounds: Rectangle,
}
-
-impl<'a, Message, Renderer> From<Scrollable<'a, Message, Renderer>>
- for Element<'a, Message, Renderer>
-where
- Renderer: 'a + crate::Renderer,
- Message: 'a,
-{
- fn from(
- scrollable: Scrollable<'a, Message, Renderer>,
- ) -> Element<'a, Message, Renderer> {
- Element::new(scrollable)
- }
-}
diff --git a/native/src/widget/slider.rs b/native/src/widget/slider.rs
index 289f75f5..585d9c35 100644
--- a/native/src/widget/slider.rs
+++ b/native/src/widget/slider.rs
@@ -6,6 +6,7 @@ use crate::layout;
use crate::mouse;
use crate::renderer;
use crate::touch;
+use crate::widget::tree::{self, Tree};
use crate::{
Background, Clipboard, Color, Element, Layout, Length, Point, Rectangle,
Shell, Size, Widget,
@@ -13,7 +14,7 @@ use crate::{
use std::ops::RangeInclusive;
-pub use iced_style::slider::{Handle, HandleShape, Style, StyleSheet};
+pub use iced_style::slider::{Appearance, Handle, HandleShape, StyleSheet};
/// An horizontal bar and a handle that selects a single value from a range of
/// values.
@@ -25,37 +26,44 @@ pub use iced_style::slider::{Handle, HandleShape, Style, StyleSheet};
///
/// # Example
/// ```
-/// # use iced_native::widget::slider::{self, Slider};
+/// # use iced_native::widget::slider;
+/// # use iced_native::renderer::Null;
+/// #
+/// # type Slider<'a, T, Message> = slider::Slider<'a, T, Message, Null>;
/// #
/// #[derive(Clone)]
/// pub enum Message {
/// SliderChanged(f32),
/// }
///
-/// let state = &mut slider::State::new();
/// let value = 50.0;
///
-/// Slider::new(state, 0.0..=100.0, value, Message::SliderChanged);
+/// Slider::new(0.0..=100.0, value, Message::SliderChanged);
/// ```
///
/// ![Slider drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/slider.png?raw=true)
#[allow(missing_debug_implementations)]
-pub struct Slider<'a, T, Message> {
- state: &'a mut State,
+pub struct Slider<'a, T, Message, Renderer>
+where
+ Renderer: crate::Renderer,
+ Renderer::Theme: StyleSheet,
+{
range: RangeInclusive<T>,
step: T,
value: T,
- on_change: Box<dyn Fn(T) -> Message>,
+ on_change: Box<dyn Fn(T) -> Message + 'a>,
on_release: Option<Message>,
width: Length,
height: u16,
- style_sheet: Box<dyn StyleSheet + 'a>,
+ style: <Renderer::Theme as StyleSheet>::Style,
}
-impl<'a, T, Message> Slider<'a, T, Message>
+impl<'a, T, Message, Renderer> Slider<'a, T, Message, Renderer>
where
T: Copy + From<u8> + std::cmp::PartialOrd,
Message: Clone,
+ Renderer: crate::Renderer,
+ Renderer::Theme: StyleSheet,
{
/// The default height of a [`Slider`].
pub const DEFAULT_HEIGHT: u16 = 22;
@@ -63,20 +71,14 @@ where
/// Creates a new [`Slider`].
///
/// It expects:
- /// * the local [`State`] of the [`Slider`]
/// * an inclusive range of possible values
/// * the current value of the [`Slider`]
/// * a function that will be called when the [`Slider`] is dragged.
/// It receives the new value of the [`Slider`] and must produce a
/// `Message`.
- pub fn new<F>(
- state: &'a mut State,
- range: RangeInclusive<T>,
- value: T,
- on_change: F,
- ) -> Self
+ pub fn new<F>(range: RangeInclusive<T>, value: T, on_change: F) -> Self
where
- F: 'static + Fn(T) -> Message,
+ F: 'a + Fn(T) -> Message,
{
let value = if value >= *range.start() {
value
@@ -91,7 +93,6 @@ where
};
Slider {
- state,
value,
range,
step: T::from(1),
@@ -99,7 +100,7 @@ where
on_release: None,
width: Length::Fill,
height: Self::DEFAULT_HEIGHT,
- style_sheet: Default::default(),
+ style: Default::default(),
}
}
@@ -129,9 +130,9 @@ where
/// Sets the style of the [`Slider`].
pub fn style(
mut self,
- style_sheet: impl Into<Box<dyn StyleSheet + 'a>>,
+ style: impl Into<<Renderer::Theme as StyleSheet>::Style>,
) -> Self {
- self.style_sheet = style_sheet.into();
+ self.style = style.into();
self
}
@@ -142,26 +143,22 @@ where
}
}
-/// The local state of a [`Slider`].
-#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
-pub struct State {
- is_dragging: bool,
-}
-
-impl State {
- /// Creates a new [`State`].
- pub fn new() -> State {
- State::default()
- }
-}
-
impl<'a, T, Message, Renderer> Widget<Message, Renderer>
- for Slider<'a, T, Message>
+ for Slider<'a, T, Message, Renderer>
where
T: Copy + Into<f64> + num_traits::FromPrimitive,
Message: Clone,
Renderer: crate::Renderer,
+ Renderer::Theme: StyleSheet,
{
+ fn tag(&self) -> tree::Tag {
+ tree::Tag::of::<State>()
+ }
+
+ fn state(&self) -> tree::State {
+ tree::State::new(State::new())
+ }
+
fn width(&self) -> Length {
self.width
}
@@ -185,6 +182,7 @@ where
fn on_event(
&mut self,
+ tree: &mut Tree,
event: Event,
layout: Layout<'_>,
cursor_position: Point,
@@ -192,197 +190,286 @@ where
_clipboard: &mut dyn Clipboard,
shell: &mut Shell<'_, Message>,
) -> event::Status {
- let is_dragging = self.state.is_dragging;
-
- let mut change = || {
- let bounds = layout.bounds();
- let new_value = if cursor_position.x <= bounds.x {
- *self.range.start()
- } else if cursor_position.x >= bounds.x + bounds.width {
- *self.range.end()
- } else {
- let step = self.step.into();
- let start = (*self.range.start()).into();
- let end = (*self.range.end()).into();
-
- let percent = f64::from(cursor_position.x - bounds.x)
- / f64::from(bounds.width);
-
- let steps = (percent * (end - start) / step).round();
- let value = steps * step + start;
-
- if let Some(value) = T::from_f64(value) {
- value
- } else {
- return;
- }
- };
-
- if (self.value.into() - new_value.into()).abs() > f64::EPSILON {
- shell.publish((self.on_change)(new_value));
-
- self.value = new_value;
- }
- };
-
- match event {
- Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
- | Event::Touch(touch::Event::FingerPressed { .. }) => {
- if layout.bounds().contains(cursor_position) {
- change();
- self.state.is_dragging = true;
-
- return event::Status::Captured;
- }
- }
- Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left))
- | Event::Touch(touch::Event::FingerLifted { .. })
- | Event::Touch(touch::Event::FingerLost { .. }) => {
- if is_dragging {
- if let Some(on_release) = self.on_release.clone() {
- shell.publish(on_release);
- }
- self.state.is_dragging = false;
-
- return event::Status::Captured;
- }
- }
- Event::Mouse(mouse::Event::CursorMoved { .. })
- | Event::Touch(touch::Event::FingerMoved { .. }) => {
- if is_dragging {
- change();
-
- return event::Status::Captured;
- }
- }
- _ => {}
- }
-
- event::Status::Ignored
+ update(
+ event,
+ layout,
+ cursor_position,
+ shell,
+ tree.state.downcast_mut::<State>(),
+ &mut self.value,
+ &self.range,
+ self.step,
+ self.on_change.as_ref(),
+ &self.on_release,
+ )
}
fn draw(
&self,
+ tree: &Tree,
renderer: &mut Renderer,
+ theme: &Renderer::Theme,
_style: &renderer::Style,
layout: Layout<'_>,
cursor_position: Point,
_viewport: &Rectangle,
) {
- let bounds = layout.bounds();
- let is_mouse_over = bounds.contains(cursor_position);
-
- let style = if self.state.is_dragging {
- self.style_sheet.dragging()
- } else if is_mouse_over {
- self.style_sheet.hovered()
- } else {
- self.style_sheet.active()
- };
-
- let rail_y = bounds.y + (bounds.height / 2.0).round();
-
- renderer.fill_quad(
- renderer::Quad {
- bounds: Rectangle {
- x: bounds.x,
- y: rail_y,
- width: bounds.width,
- height: 2.0,
- },
- border_radius: 0.0,
- border_width: 0.0,
- border_color: Color::TRANSPARENT,
- },
- style.rail_colors.0,
- );
-
- renderer.fill_quad(
- renderer::Quad {
- bounds: Rectangle {
- x: bounds.x,
- y: rail_y + 2.0,
- width: bounds.width,
- height: 2.0,
- },
- border_radius: 0.0,
- border_width: 0.0,
- border_color: Color::TRANSPARENT,
- },
- Background::Color(style.rail_colors.1),
- );
-
- let (handle_width, handle_height, handle_border_radius) = match style
- .handle
- .shape
- {
- HandleShape::Circle { radius } => {
- (radius * 2.0, radius * 2.0, radius)
- }
- HandleShape::Rectangle {
- width,
- border_radius,
- } => (f32::from(width), f32::from(bounds.height), border_radius),
- };
-
- let value = self.value.into() as f32;
- let (range_start, range_end) = {
- let (start, end) = self.range.clone().into_inner();
-
- (start.into() as f32, end.into() as f32)
- };
-
- let handle_offset = if range_start >= range_end {
- 0.0
- } else {
- (bounds.width - handle_width) * (value - range_start)
- / (range_end - range_start)
- };
-
- renderer.fill_quad(
- renderer::Quad {
- bounds: Rectangle {
- x: bounds.x + handle_offset.round(),
- y: rail_y - handle_height / 2.0,
- width: handle_width,
- height: handle_height,
- },
- border_radius: handle_border_radius,
- border_width: style.handle.border_width,
- border_color: style.handle.border_color,
- },
- style.handle.color,
- );
+ draw(
+ renderer,
+ layout,
+ cursor_position,
+ tree.state.downcast_ref::<State>(),
+ self.value,
+ &self.range,
+ theme,
+ self.style,
+ )
}
fn mouse_interaction(
&self,
+ tree: &Tree,
layout: Layout<'_>,
cursor_position: Point,
_viewport: &Rectangle,
_renderer: &Renderer,
) -> mouse::Interaction {
- let bounds = layout.bounds();
- let is_mouse_over = bounds.contains(cursor_position);
-
- if self.state.is_dragging {
- mouse::Interaction::Grabbing
- } else if is_mouse_over {
- mouse::Interaction::Grab
- } else {
- mouse::Interaction::default()
- }
+ mouse_interaction(
+ layout,
+ cursor_position,
+ tree.state.downcast_ref::<State>(),
+ )
}
}
-impl<'a, T, Message, Renderer> From<Slider<'a, T, Message>>
+impl<'a, T, Message, Renderer> From<Slider<'a, T, Message, Renderer>>
for Element<'a, Message, Renderer>
where
T: 'a + Copy + Into<f64> + num_traits::FromPrimitive,
Message: 'a + Clone,
Renderer: 'a + crate::Renderer,
+ Renderer::Theme: StyleSheet,
{
- fn from(slider: Slider<'a, T, Message>) -> Element<'a, Message, Renderer> {
+ fn from(
+ slider: Slider<'a, T, Message, Renderer>,
+ ) -> Element<'a, Message, Renderer> {
Element::new(slider)
}
}
+
+/// Processes an [`Event`] and updates the [`State`] of a [`Slider`]
+/// accordingly.
+pub fn update<Message, T>(
+ event: Event,
+ layout: Layout<'_>,
+ cursor_position: Point,
+ shell: &mut Shell<'_, Message>,
+ state: &mut State,
+ value: &mut T,
+ range: &RangeInclusive<T>,
+ step: T,
+ on_change: &dyn Fn(T) -> Message,
+ on_release: &Option<Message>,
+) -> event::Status
+where
+ T: Copy + Into<f64> + num_traits::FromPrimitive,
+ Message: Clone,
+{
+ let is_dragging = state.is_dragging;
+
+ let mut change = || {
+ let bounds = layout.bounds();
+ let new_value = if cursor_position.x <= bounds.x {
+ *range.start()
+ } else if cursor_position.x >= bounds.x + bounds.width {
+ *range.end()
+ } else {
+ let step = step.into();
+ let start = (*range.start()).into();
+ let end = (*range.end()).into();
+
+ let percent = f64::from(cursor_position.x - bounds.x)
+ / f64::from(bounds.width);
+
+ let steps = (percent * (end - start) / step).round();
+ let value = steps * step + start;
+
+ if let Some(value) = T::from_f64(value) {
+ value
+ } else {
+ return;
+ }
+ };
+
+ if ((*value).into() - new_value.into()).abs() > f64::EPSILON {
+ shell.publish((on_change)(new_value));
+
+ *value = new_value;
+ }
+ };
+
+ match event {
+ Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
+ | Event::Touch(touch::Event::FingerPressed { .. }) => {
+ if layout.bounds().contains(cursor_position) {
+ change();
+ state.is_dragging = true;
+
+ return event::Status::Captured;
+ }
+ }
+ Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left))
+ | Event::Touch(touch::Event::FingerLifted { .. })
+ | Event::Touch(touch::Event::FingerLost { .. }) => {
+ if is_dragging {
+ if let Some(on_release) = on_release.clone() {
+ shell.publish(on_release);
+ }
+ state.is_dragging = false;
+
+ return event::Status::Captured;
+ }
+ }
+ Event::Mouse(mouse::Event::CursorMoved { .. })
+ | Event::Touch(touch::Event::FingerMoved { .. }) => {
+ if is_dragging {
+ change();
+
+ return event::Status::Captured;
+ }
+ }
+ _ => {}
+ }
+
+ event::Status::Ignored
+}
+
+/// Draws a [`Slider`].
+pub fn draw<T, R>(
+ renderer: &mut R,
+ layout: Layout<'_>,
+ cursor_position: Point,
+ state: &State,
+ value: T,
+ range: &RangeInclusive<T>,
+ style_sheet: &dyn StyleSheet<Style = <R::Theme as StyleSheet>::Style>,
+ style: <R::Theme as StyleSheet>::Style,
+) where
+ T: Into<f64> + Copy,
+ R: crate::Renderer,
+ R::Theme: StyleSheet,
+{
+ let bounds = layout.bounds();
+ let is_mouse_over = bounds.contains(cursor_position);
+
+ let style = if state.is_dragging {
+ style_sheet.dragging(style)
+ } else if is_mouse_over {
+ style_sheet.hovered(style)
+ } else {
+ style_sheet.active(style)
+ };
+
+ let rail_y = bounds.y + (bounds.height / 2.0).round();
+
+ renderer.fill_quad(
+ renderer::Quad {
+ bounds: Rectangle {
+ x: bounds.x,
+ y: rail_y - 1.0,
+ width: bounds.width,
+ height: 2.0,
+ },
+ border_radius: 0.0,
+ border_width: 0.0,
+ border_color: Color::TRANSPARENT,
+ },
+ style.rail_colors.0,
+ );
+
+ renderer.fill_quad(
+ renderer::Quad {
+ bounds: Rectangle {
+ x: bounds.x,
+ y: rail_y + 1.0,
+ width: bounds.width,
+ height: 2.0,
+ },
+ border_radius: 0.0,
+ border_width: 0.0,
+ border_color: Color::TRANSPARENT,
+ },
+ Background::Color(style.rail_colors.1),
+ );
+
+ let (handle_width, handle_height, handle_border_radius) = match style
+ .handle
+ .shape
+ {
+ HandleShape::Circle { radius } => (radius * 2.0, radius * 2.0, radius),
+ HandleShape::Rectangle {
+ width,
+ border_radius,
+ } => (f32::from(width), bounds.height, border_radius),
+ };
+
+ let value = value.into() as f32;
+ let (range_start, range_end) = {
+ let (start, end) = range.clone().into_inner();
+
+ (start.into() as f32, end.into() as f32)
+ };
+
+ let handle_offset = if range_start >= range_end {
+ 0.0
+ } else {
+ bounds.width * (value - range_start) / (range_end - range_start)
+ - handle_width / 2.0
+ };
+
+ renderer.fill_quad(
+ renderer::Quad {
+ bounds: Rectangle {
+ x: bounds.x + handle_offset.round(),
+ y: rail_y - handle_height / 2.0,
+ width: handle_width,
+ height: handle_height,
+ },
+ border_radius: handle_border_radius,
+ border_width: style.handle.border_width,
+ border_color: style.handle.border_color,
+ },
+ style.handle.color,
+ );
+}
+
+/// Computes the current [`mouse::Interaction`] of a [`Slider`].
+pub fn mouse_interaction(
+ layout: Layout<'_>,
+ cursor_position: Point,
+ state: &State,
+) -> mouse::Interaction {
+ let bounds = layout.bounds();
+ let is_mouse_over = bounds.contains(cursor_position);
+
+ if state.is_dragging {
+ mouse::Interaction::Grabbing
+ } else if is_mouse_over {
+ mouse::Interaction::Grab
+ } else {
+ mouse::Interaction::default()
+ }
+}
+
+/// The local state of a [`Slider`].
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
+pub struct State {
+ is_dragging: bool,
+}
+
+impl State {
+ /// Creates a new [`State`].
+ pub fn new() -> State {
+ State::default()
+ }
+}
diff --git a/native/src/widget/space.rs b/native/src/widget/space.rs
index 4135d1b8..9f835893 100644
--- a/native/src/widget/space.rs
+++ b/native/src/widget/space.rs
@@ -1,6 +1,7 @@
//! Distribute content vertically.
use crate::layout;
use crate::renderer;
+use crate::widget::Tree;
use crate::{Element, Layout, Length, Point, Rectangle, Size, Widget};
/// An amount of empty space.
@@ -59,7 +60,9 @@ where
fn draw(
&self,
+ _state: &Tree,
_renderer: &mut Renderer,
+ _theme: &Renderer::Theme,
_style: &renderer::Style,
_layout: Layout<'_>,
_cursor_position: Point,
diff --git a/native/src/widget/svg.rs b/native/src/widget/svg.rs
index 008ab356..aa68bfb8 100644
--- a/native/src/widget/svg.rs
+++ b/native/src/widget/svg.rs
@@ -1,13 +1,16 @@
//! Display vector graphics in your application.
use crate::layout;
use crate::renderer;
-use crate::svg::{self, Handle};
+use crate::svg;
+use crate::widget::Tree;
use crate::{
ContentFit, Element, Layout, Length, Point, Rectangle, Size, Vector, Widget,
};
use std::path::PathBuf;
+pub use svg::Handle;
+
/// A vector graphics image.
///
/// An [`Svg`] image resizes smoothly without losing any quality.
@@ -109,7 +112,9 @@ where
fn draw(
&self,
+ _state: &Tree,
renderer: &mut Renderer,
+ _theme: &Renderer::Theme,
_style: &renderer::Style,
layout: Layout<'_>,
_cursor_position: Point,
diff --git a/native/src/widget/text.rs b/native/src/widget/text.rs
index 6f00c9c8..dab6e874 100644
--- a/native/src/widget/text.rs
+++ b/native/src/widget/text.rs
@@ -3,45 +3,60 @@ use crate::alignment;
use crate::layout;
use crate::renderer;
use crate::text;
-use crate::{Color, Element, Layout, Length, Point, Rectangle, Size, Widget};
+use crate::widget::Tree;
+use crate::{Element, Layout, Length, Point, Rectangle, Size, Widget};
+
+use std::borrow::Cow;
+
+pub use iced_style::text::{Appearance, StyleSheet};
/// A paragraph of text.
///
/// # Example
///
/// ```
-/// # type Text = iced_native::widget::Text<iced_native::renderer::Null>;
+/// # use iced_native::Color;
+/// #
+/// # type Text<'a> = iced_native::widget::Text<'a, iced_native::renderer::Null>;
/// #
/// Text::new("I <3 iced!")
-/// .color([0.0, 0.0, 1.0])
-/// .size(40);
+/// .size(40)
+/// .style(Color::from([0.0, 0.0, 1.0]));
/// ```
///
/// ![Text drawn by `iced_wgpu`](https://github.com/iced-rs/iced/blob/7760618fb112074bc40b148944521f312152012a/docs/images/text.png?raw=true)
-#[derive(Debug)]
-pub struct Text<Renderer: text::Renderer> {
- content: String,
+#[allow(missing_debug_implementations)]
+pub struct Text<'a, Renderer>
+where
+ Renderer: text::Renderer,
+ Renderer::Theme: StyleSheet,
+{
+ content: Cow<'a, str>,
size: Option<u16>,
- color: Option<Color>,
- font: Renderer::Font,
width: Length,
height: Length,
horizontal_alignment: alignment::Horizontal,
vertical_alignment: alignment::Vertical,
+ font: Renderer::Font,
+ style: <Renderer::Theme as StyleSheet>::Style,
}
-impl<Renderer: text::Renderer> Text<Renderer> {
+impl<'a, Renderer> Text<'a, Renderer>
+where
+ Renderer: text::Renderer,
+ Renderer::Theme: StyleSheet,
+{
/// Create a new fragment of [`Text`] with the given contents.
- pub fn new<T: Into<String>>(label: T) -> Self {
+ pub fn new(content: impl Into<Cow<'a, str>>) -> Self {
Text {
- content: label.into(),
+ content: content.into(),
size: None,
- color: None,
font: Default::default(),
width: Length::Shrink,
height: Length::Shrink,
horizontal_alignment: alignment::Horizontal::Left,
vertical_alignment: alignment::Vertical::Top,
+ style: Default::default(),
}
}
@@ -51,20 +66,23 @@ impl<Renderer: text::Renderer> Text<Renderer> {
self
}
- /// Sets the [`Color`] of the [`Text`].
- pub fn color<C: Into<Color>>(mut self, color: C) -> Self {
- self.color = Some(color.into());
- self
- }
-
/// Sets the [`Font`] of the [`Text`].
///
- /// [`Font`]: Renderer::Font
+ /// [`Font`]: crate::text::Renderer::Font
pub fn font(mut self, font: impl Into<Renderer::Font>) -> Self {
self.font = font.into();
self
}
+ /// Sets the [`Color`] of the [`Text`].
+ pub fn style(
+ mut self,
+ style: impl Into<<Renderer::Theme as StyleSheet>::Style>,
+ ) -> Self {
+ self.style = style.into();
+ self
+ }
+
/// Sets the width of the [`Text`] boundaries.
pub fn width(mut self, width: Length) -> Self {
self.width = width;
@@ -77,7 +95,7 @@ impl<Renderer: text::Renderer> Text<Renderer> {
self
}
- /// Sets the [`HorizontalAlignment`] of the [`Text`].
+ /// Sets the [`alignment::Horizontal`] of the [`Text`].
pub fn horizontal_alignment(
mut self,
alignment: alignment::Horizontal,
@@ -86,7 +104,7 @@ impl<Renderer: text::Renderer> Text<Renderer> {
self
}
- /// Sets the [`VerticalAlignment`] of the [`Text`].
+ /// Sets the [`alignment::Vertical`] of the [`Text`].
pub fn vertical_alignment(
mut self,
alignment: alignment::Vertical,
@@ -96,9 +114,10 @@ impl<Renderer: text::Renderer> Text<Renderer> {
}
}
-impl<Message, Renderer> Widget<Message, Renderer> for Text<Renderer>
+impl<'a, Message, Renderer> Widget<Message, Renderer> for Text<'a, Renderer>
where
Renderer: text::Renderer,
+ Renderer::Theme: StyleSheet,
{
fn width(&self) -> Length {
self.width
@@ -115,7 +134,7 @@ where
) -> layout::Node {
let limits = limits.width(self.width).height(self.height);
- let size = self.size.unwrap_or(renderer.default_size());
+ let size = self.size.unwrap_or_else(|| renderer.default_size());
let bounds = limits.max();
@@ -129,7 +148,9 @@ where
fn draw(
&self,
+ _state: &Tree,
renderer: &mut Renderer,
+ theme: &Renderer::Theme,
style: &renderer::Style,
layout: Layout<'_>,
_cursor_position: Point,
@@ -140,9 +161,9 @@ where
style,
layout,
&self.content,
- self.font.clone(),
self.size,
- self.color,
+ self.font.clone(),
+ theme.appearance(self.style),
self.horizontal_alignment,
self.vertical_alignment,
);
@@ -164,9 +185,9 @@ pub fn draw<Renderer>(
style: &renderer::Style,
layout: Layout<'_>,
content: &str,
- font: Renderer::Font,
size: Option<u16>,
- color: Option<Color>,
+ font: Renderer::Font,
+ appearance: Appearance,
horizontal_alignment: alignment::Horizontal,
vertical_alignment: alignment::Vertical,
) where
@@ -188,36 +209,51 @@ pub fn draw<Renderer>(
renderer.fill_text(crate::text::Text {
content,
- size: f32::from(size.unwrap_or(renderer.default_size())),
+ size: f32::from(size.unwrap_or_else(|| renderer.default_size())),
bounds: Rectangle { x, y, ..bounds },
- color: color.unwrap_or(style.text_color),
+ color: appearance.color.unwrap_or(style.text_color),
font,
horizontal_alignment,
vertical_alignment,
});
}
-impl<'a, Message, Renderer> From<Text<Renderer>>
+impl<'a, Message, Renderer> From<Text<'a, Renderer>>
for Element<'a, Message, Renderer>
where
Renderer: text::Renderer + 'a,
+ Renderer::Theme: StyleSheet,
{
- fn from(text: Text<Renderer>) -> Element<'a, Message, Renderer> {
+ fn from(text: Text<'a, Renderer>) -> Element<'a, Message, Renderer> {
Element::new(text)
}
}
-impl<Renderer: text::Renderer> Clone for Text<Renderer> {
+impl<'a, Renderer> Clone for Text<'a, Renderer>
+where
+ Renderer: text::Renderer,
+ Renderer::Theme: StyleSheet,
+{
fn clone(&self) -> Self {
Self {
content: self.content.clone(),
size: self.size,
- color: self.color,
- font: self.font.clone(),
width: self.width,
height: self.height,
horizontal_alignment: self.horizontal_alignment,
vertical_alignment: self.vertical_alignment,
+ font: self.font.clone(),
+ style: self.style,
}
}
}
+
+impl<'a, Message, Renderer> From<&'a str> for Element<'a, Message, Renderer>
+where
+ Renderer: text::Renderer + 'a,
+ Renderer::Theme: StyleSheet,
+{
+ fn from(contents: &'a str) -> Self {
+ Text::new(contents).into()
+ }
+}
diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs
index e30e2343..c2d25520 100644
--- a/native/src/widget/text_input.rs
+++ b/native/src/widget/text_input.rs
@@ -19,33 +19,29 @@ use crate::mouse::{self, click};
use crate::renderer;
use crate::text::{self, Text};
use crate::touch;
+use crate::widget;
+use crate::widget::operation::{self, Operation};
+use crate::widget::tree::{self, Tree};
use crate::{
- Clipboard, Color, Element, Layout, Length, Padding, Point, Rectangle,
- Shell, Size, Vector, Widget,
+ Clipboard, Color, Command, Element, Layout, Length, Padding, Point,
+ Rectangle, Shell, Size, Vector, Widget,
};
-use std::u32;
-
-pub use iced_style::text_input::{Style, StyleSheet};
+pub use iced_style::text_input::{Appearance, StyleSheet};
/// A field that can be filled with text.
///
/// # Example
/// ```
-/// # use iced_native::renderer::Null;
-/// # use iced_native::widget::text_input;
-/// #
-/// # pub type TextInput<'a, Message> = iced_native::widget::TextInput<'a, Message, Null>;
+/// # pub type TextInput<'a, Message> = iced_native::widget::TextInput<'a, Message, iced_native::renderer::Null>;
/// #[derive(Debug, Clone)]
/// enum Message {
/// TextInputChanged(String),
/// }
///
-/// let mut state = text_input::State::new();
/// let value = "Some text";
///
/// let input = TextInput::new(
-/// &mut state,
/// "This is the placeholder...",
/// value,
/// Message::TextInputChanged,
@@ -54,68 +50,82 @@ pub use iced_style::text_input::{Style, StyleSheet};
/// ```
/// ![Text input drawn by `iced_wgpu`](https://github.com/iced-rs/iced/blob/7760618fb112074bc40b148944521f312152012a/docs/images/text_input.png?raw=true)
#[allow(missing_debug_implementations)]
-pub struct TextInput<'a, Message, Renderer: text::Renderer> {
- state: &'a mut State,
+pub struct TextInput<'a, Message, Renderer>
+where
+ Renderer: text::Renderer,
+ Renderer::Theme: StyleSheet,
+{
+ id: Option<Id>,
placeholder: String,
value: Value,
is_secure: bool,
font: Renderer::Font,
width: Length,
- max_width: u32,
padding: Padding,
size: Option<u16>,
- on_change: Box<dyn Fn(String) -> Message>,
+ on_change: Box<dyn Fn(String) -> Message + 'a>,
+ on_paste: Option<Box<dyn Fn(String) -> Message + 'a>>,
on_submit: Option<Message>,
- style_sheet: Box<dyn StyleSheet + 'a>,
+ style: <Renderer::Theme as StyleSheet>::Style,
}
impl<'a, Message, Renderer> TextInput<'a, Message, Renderer>
where
Message: Clone,
Renderer: text::Renderer,
+ Renderer::Theme: StyleSheet,
{
/// Creates a new [`TextInput`].
///
/// It expects:
- /// - some [`State`]
- /// - a placeholder
- /// - the current value
- /// - a function that produces a message when the [`TextInput`] changes
- pub fn new<F>(
- state: &'a mut State,
- placeholder: &str,
- value: &str,
- on_change: F,
- ) -> Self
+ /// - a placeholder,
+ /// - the current value, and
+ /// - a function that produces a message when the [`TextInput`] changes.
+ pub fn new<F>(placeholder: &str, value: &str, on_change: F) -> Self
where
- F: 'static + Fn(String) -> Message,
+ F: 'a + Fn(String) -> Message,
{
TextInput {
- state,
+ id: None,
placeholder: String::from(placeholder),
value: Value::new(value),
is_secure: false,
font: Default::default(),
width: Length::Fill,
- max_width: u32::MAX,
padding: Padding::ZERO,
size: None,
on_change: Box::new(on_change),
+ on_paste: None,
on_submit: None,
- style_sheet: Default::default(),
+ style: Default::default(),
}
}
+ /// Sets the [`Id`] of the [`TextInput`].
+ pub fn id(mut self, id: Id) -> Self {
+ self.id = Some(id);
+ self
+ }
+
/// Converts the [`TextInput`] into a secure password input.
pub fn password(mut self) -> Self {
self.is_secure = true;
self
}
- /// Sets the [`Font`] of the [`Text`].
+ /// Sets the message that should be produced when some text is pasted into
+ /// the [`TextInput`].
+ pub fn on_paste(
+ mut self,
+ on_paste: impl Fn(String) -> Message + 'a,
+ ) -> Self {
+ self.on_paste = Some(Box::new(on_paste));
+ self
+ }
+
+ /// Sets the [`Font`] of the [`TextInput`].
///
- /// [`Font`]: crate::widget::text::Renderer::Font
- /// [`Text`]: crate::widget::Text
+ /// [`Font`]: text::Renderer::Font
pub fn font(mut self, font: Renderer::Font) -> Self {
self.font = font;
self
@@ -126,12 +136,6 @@ where
self
}
- /// Sets the maximum width of the [`TextInput`].
- pub fn max_width(mut self, max_width: u32) -> Self {
- self.max_width = max_width;
- self
- }
-
/// Sets the [`Padding`] of the [`TextInput`].
pub fn padding<P: Into<Padding>>(mut self, padding: P) -> Self {
self.padding = padding.into();
@@ -154,190 +158,38 @@ where
/// Sets the style of the [`TextInput`].
pub fn style(
mut self,
- style_sheet: impl Into<Box<dyn StyleSheet + 'a>>,
+ style: impl Into<<Renderer::Theme as StyleSheet>::Style>,
) -> Self {
- self.style_sheet = style_sheet.into();
+ self.style = style.into();
self
}
- /// Returns the current [`State`] of the [`TextInput`].
- pub fn state(&self) -> &State {
- self.state
- }
-}
-
-impl<'a, Message, Renderer> TextInput<'a, Message, Renderer>
-where
- Renderer: text::Renderer,
-{
/// Draws the [`TextInput`] with the given [`Renderer`], overriding its
- /// [`Value`] if provided.
+ /// [`text_input::Value`] if provided.
+ ///
+ /// [`Renderer`]: text::Renderer
pub fn draw(
&self,
+ tree: &Tree,
renderer: &mut Renderer,
+ theme: &Renderer::Theme,
layout: Layout<'_>,
cursor_position: Point,
value: Option<&Value>,
) {
- let value = value.unwrap_or(&self.value);
- let secure_value = self.is_secure.then(|| value.secure());
- let value = secure_value.as_ref().unwrap_or(&value);
-
- let bounds = layout.bounds();
- let text_bounds = layout.children().next().unwrap().bounds();
-
- let is_mouse_over = bounds.contains(cursor_position);
-
- let style = if self.state.is_focused() {
- self.style_sheet.focused()
- } else if is_mouse_over {
- self.style_sheet.hovered()
- } else {
- self.style_sheet.active()
- };
-
- renderer.fill_quad(
- renderer::Quad {
- bounds,
- border_radius: style.border_radius,
- border_width: style.border_width,
- border_color: style.border_color,
- },
- style.background,
- );
-
- let text = value.to_string();
- let size = self.size.unwrap_or(renderer.default_size());
-
- let (cursor, offset) = if self.state.is_focused() {
- match self.state.cursor.state(&value) {
- cursor::State::Index(position) => {
- let (text_value_width, offset) =
- measure_cursor_and_scroll_offset(
- renderer,
- text_bounds,
- &value,
- size,
- position,
- self.font.clone(),
- );
-
- (
- Some((
- renderer::Quad {
- bounds: Rectangle {
- x: text_bounds.x + text_value_width,
- y: text_bounds.y,
- width: 1.0,
- height: text_bounds.height,
- },
- border_radius: 0.0,
- border_width: 0.0,
- border_color: Color::TRANSPARENT,
- },
- self.style_sheet.value_color(),
- )),
- offset,
- )
- }
- cursor::State::Selection { start, end } => {
- let left = start.min(end);
- let right = end.max(start);
-
- let (left_position, left_offset) =
- measure_cursor_and_scroll_offset(
- renderer,
- text_bounds,
- &value,
- size,
- left,
- self.font.clone(),
- );
-
- let (right_position, right_offset) =
- measure_cursor_and_scroll_offset(
- renderer,
- text_bounds,
- &value,
- size,
- right,
- self.font.clone(),
- );
-
- let width = right_position - left_position;
-
- (
- Some((
- renderer::Quad {
- bounds: Rectangle {
- x: text_bounds.x + left_position,
- y: text_bounds.y,
- width,
- height: text_bounds.height,
- },
- border_radius: 0.0,
- border_width: 0.0,
- border_color: Color::TRANSPARENT,
- },
- self.style_sheet.selection_color(),
- )),
- if end == right {
- right_offset
- } else {
- left_offset
- },
- )
- }
- }
- } else {
- (None, 0.0)
- };
-
- let text_width = renderer.measure_width(
- if text.is_empty() {
- &self.placeholder
- } else {
- &text
- },
- size,
- self.font.clone(),
- );
-
- let render = |renderer: &mut Renderer| {
- if let Some((cursor, color)) = cursor {
- renderer.fill_quad(cursor, color);
- }
-
- renderer.fill_text(Text {
- content: if text.is_empty() {
- &self.placeholder
- } else {
- &text
- },
- color: if text.is_empty() {
- self.style_sheet.placeholder_color()
- } else {
- self.style_sheet.value_color()
- },
- font: self.font.clone(),
- bounds: Rectangle {
- y: text_bounds.center_y(),
- width: f32::INFINITY,
- ..text_bounds
- },
- size: f32::from(size),
- horizontal_alignment: alignment::Horizontal::Left,
- vertical_alignment: alignment::Vertical::Center,
- });
- };
-
- if text_width > text_bounds.width {
- renderer.with_layer(text_bounds, |renderer| {
- renderer.with_translation(Vector::new(-offset, 0.0), render)
- });
- } else {
- render(renderer);
- }
+ draw(
+ renderer,
+ theme,
+ layout,
+ cursor_position,
+ tree.state.downcast_ref::<State>(),
+ value.unwrap_or(&self.value),
+ &self.placeholder,
+ self.size,
+ &self.font,
+ self.is_secure,
+ self.style,
+ )
}
}
@@ -346,7 +198,16 @@ impl<'a, Message, Renderer> Widget<Message, Renderer>
where
Message: Clone,
Renderer: text::Renderer,
+ Renderer::Theme: StyleSheet,
{
+ fn tag(&self) -> tree::Tag {
+ tree::Tag::of::<State>()
+ }
+
+ fn state(&self) -> tree::State {
+ tree::State::new(State::new())
+ }
+
fn width(&self) -> Length {
self.width
}
@@ -360,25 +221,23 @@ where
renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
- let text_size = self.size.unwrap_or(renderer.default_size());
-
- let limits = limits
- .pad(self.padding)
- .width(self.width)
- .max_width(self.max_width)
- .height(Length::Units(text_size));
+ layout(renderer, limits, self.width, self.padding, self.size)
+ }
- let mut text = layout::Node::new(limits.resolve(Size::ZERO));
- text.move_to(Point::new(
- self.padding.left.into(),
- self.padding.top.into(),
- ));
+ fn operate(
+ &self,
+ tree: &mut Tree,
+ _layout: Layout<'_>,
+ operation: &mut dyn Operation<Message>,
+ ) {
+ let state = tree.state.downcast_mut::<State>();
- layout::Node::with_children(text.size().pad(self.padding), vec![text])
+ operation.focusable(state, self.id.as_ref().map(|id| &id.0));
}
fn on_event(
&mut self,
+ tree: &mut Tree,
event: Event,
layout: Layout<'_>,
cursor_position: Point,
@@ -386,305 +245,407 @@ where
clipboard: &mut dyn Clipboard,
shell: &mut Shell<'_, Message>,
) -> event::Status {
- match event {
- Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
- | Event::Touch(touch::Event::FingerPressed { .. }) => {
- let is_clicked = layout.bounds().contains(cursor_position);
+ update(
+ event,
+ layout,
+ cursor_position,
+ renderer,
+ clipboard,
+ shell,
+ &mut self.value,
+ self.size,
+ &self.font,
+ self.is_secure,
+ self.on_change.as_ref(),
+ self.on_paste.as_deref(),
+ &self.on_submit,
+ || tree.state.downcast_mut::<State>(),
+ )
+ }
- self.state.is_focused = is_clicked;
+ fn draw(
+ &self,
+ tree: &Tree,
+ renderer: &mut Renderer,
+ theme: &Renderer::Theme,
+ _style: &renderer::Style,
+ layout: Layout<'_>,
+ cursor_position: Point,
+ _viewport: &Rectangle,
+ ) {
+ draw(
+ renderer,
+ theme,
+ layout,
+ cursor_position,
+ tree.state.downcast_ref::<State>(),
+ &self.value,
+ &self.placeholder,
+ self.size,
+ &self.font,
+ self.is_secure,
+ self.style,
+ )
+ }
- if is_clicked {
- let text_layout = layout.children().next().unwrap();
- let target = cursor_position.x - text_layout.bounds().x;
+ fn mouse_interaction(
+ &self,
+ _state: &Tree,
+ layout: Layout<'_>,
+ cursor_position: Point,
+ _viewport: &Rectangle,
+ _renderer: &Renderer,
+ ) -> mouse::Interaction {
+ mouse_interaction(layout, cursor_position)
+ }
+}
- let click = mouse::Click::new(
- cursor_position,
- self.state.last_click,
- );
+impl<'a, Message, Renderer> From<TextInput<'a, Message, Renderer>>
+ for Element<'a, Message, Renderer>
+where
+ Message: 'a + Clone,
+ Renderer: 'a + text::Renderer,
+ Renderer::Theme: StyleSheet,
+{
+ fn from(
+ text_input: TextInput<'a, Message, Renderer>,
+ ) -> Element<'a, Message, Renderer> {
+ Element::new(text_input)
+ }
+}
+
+/// The identifier of a [`TextInput`].
+#[derive(Debug, Clone, PartialEq, Eq, Hash)]
+pub struct Id(widget::Id);
+
+impl Id {
+ /// Creates a custom [`Id`].
+ pub fn new(id: impl Into<std::borrow::Cow<'static, str>>) -> Self {
+ Self(widget::Id::new(id))
+ }
+
+ /// Creates a unique [`Id`].
+ ///
+ /// This function produces a different [`Id`] every time it is called.
+ pub fn unique() -> Self {
+ Self(widget::Id::unique())
+ }
+}
+
+/// Produces a [`Command`] that focuses the [`TextInput`] with the given [`Id`].
+pub fn focus<Message: 'static>(id: Id) -> Command<Message> {
+ Command::widget(operation::focusable::focus(id.0))
+}
+
+/// Computes the layout of a [`TextInput`].
+pub fn layout<Renderer>(
+ renderer: &Renderer,
+ limits: &layout::Limits,
+ width: Length,
+ padding: Padding,
+ size: Option<u16>,
+) -> layout::Node
+where
+ Renderer: text::Renderer,
+{
+ let text_size = size.unwrap_or_else(|| renderer.default_size());
+
+ let limits = limits
+ .pad(padding)
+ .width(width)
+ .height(Length::Units(text_size));
+
+ let mut text = layout::Node::new(limits.resolve(Size::ZERO));
+ text.move_to(Point::new(padding.left.into(), padding.top.into()));
+
+ layout::Node::with_children(text.size().pad(padding), vec![text])
+}
- match click.kind() {
- click::Kind::Single => {
- let position = if target > 0.0 {
- let value = if self.is_secure {
- self.value.secure()
- } else {
- self.value.clone()
- };
-
- find_cursor_position(
- renderer,
- text_layout.bounds(),
- self.font.clone(),
- self.size,
- &value,
- &self.state,
- target,
- )
+/// Processes an [`Event`] and updates the [`State`] of a [`TextInput`]
+/// accordingly.
+pub fn update<'a, Message, Renderer>(
+ event: Event,
+ layout: Layout<'_>,
+ cursor_position: Point,
+ renderer: &Renderer,
+ clipboard: &mut dyn Clipboard,
+ shell: &mut Shell<'_, Message>,
+ value: &mut Value,
+ size: Option<u16>,
+ font: &Renderer::Font,
+ is_secure: bool,
+ on_change: &dyn Fn(String) -> Message,
+ on_paste: Option<&dyn Fn(String) -> Message>,
+ on_submit: &Option<Message>,
+ state: impl FnOnce() -> &'a mut State,
+) -> event::Status
+where
+ Message: Clone,
+ Renderer: text::Renderer,
+{
+ match event {
+ Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
+ | Event::Touch(touch::Event::FingerPressed { .. }) => {
+ let state = state();
+ let is_clicked = layout.bounds().contains(cursor_position);
+
+ state.is_focused = is_clicked;
+
+ if is_clicked {
+ let text_layout = layout.children().next().unwrap();
+ let target = cursor_position.x - text_layout.bounds().x;
+
+ let click =
+ mouse::Click::new(cursor_position, state.last_click);
+
+ match click.kind() {
+ click::Kind::Single => {
+ let position = if target > 0.0 {
+ let value = if is_secure {
+ value.secure()
} else {
- None
+ value.clone()
};
- self.state.cursor.move_to(position.unwrap_or(0));
- self.state.is_dragging = true;
- }
- click::Kind::Double => {
- if self.is_secure {
- self.state.cursor.select_all(&self.value);
- } else {
- let position = find_cursor_position(
- renderer,
- text_layout.bounds(),
- self.font.clone(),
- self.size,
- &self.value,
- &self.state,
- target,
- )
- .unwrap_or(0);
-
- self.state.cursor.select_range(
- self.value.previous_start_of_word(position),
- self.value.next_end_of_word(position),
- );
- }
+ find_cursor_position(
+ renderer,
+ text_layout.bounds(),
+ font.clone(),
+ size,
+ &value,
+ state,
+ target,
+ )
+ } else {
+ None
+ };
- self.state.is_dragging = false;
- }
- click::Kind::Triple => {
- self.state.cursor.select_all(&self.value);
- self.state.is_dragging = false;
+ state.cursor.move_to(position.unwrap_or(0));
+ state.is_dragging = true;
+ }
+ click::Kind::Double => {
+ if is_secure {
+ state.cursor.select_all(value);
+ } else {
+ let position = find_cursor_position(
+ renderer,
+ text_layout.bounds(),
+ font.clone(),
+ size,
+ value,
+ state,
+ target,
+ )
+ .unwrap_or(0);
+
+ state.cursor.select_range(
+ value.previous_start_of_word(position),
+ value.next_end_of_word(position),
+ );
}
+
+ state.is_dragging = false;
+ }
+ click::Kind::Triple => {
+ state.cursor.select_all(value);
+ state.is_dragging = false;
}
+ }
- self.state.last_click = Some(click);
+ state.last_click = Some(click);
- return event::Status::Captured;
- }
- }
- Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left))
- | Event::Touch(touch::Event::FingerLifted { .. })
- | Event::Touch(touch::Event::FingerLost { .. }) => {
- self.state.is_dragging = false;
+ return event::Status::Captured;
}
- Event::Mouse(mouse::Event::CursorMoved { position })
- | Event::Touch(touch::Event::FingerMoved { position, .. }) => {
- if self.state.is_dragging {
- let text_layout = layout.children().next().unwrap();
- let target = position.x - text_layout.bounds().x;
-
- let value = if self.is_secure {
- self.value.secure()
- } else {
- self.value.clone()
- };
+ }
+ Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left))
+ | Event::Touch(touch::Event::FingerLifted { .. })
+ | Event::Touch(touch::Event::FingerLost { .. }) => {
+ state().is_dragging = false;
+ }
+ Event::Mouse(mouse::Event::CursorMoved { position })
+ | Event::Touch(touch::Event::FingerMoved { position, .. }) => {
+ let state = state();
- let position = find_cursor_position(
- renderer,
- text_layout.bounds(),
- self.font.clone(),
- self.size,
- &value,
- &self.state,
- target,
- )
- .unwrap_or(0);
-
- self.state.cursor.select_range(
- self.state.cursor.start(&value),
- position,
- );
+ if state.is_dragging {
+ let text_layout = layout.children().next().unwrap();
+ let target = position.x - text_layout.bounds().x;
- return event::Status::Captured;
- }
+ let value = if is_secure {
+ value.secure()
+ } else {
+ value.clone()
+ };
+
+ let position = find_cursor_position(
+ renderer,
+ text_layout.bounds(),
+ font.clone(),
+ size,
+ &value,
+ state,
+ target,
+ )
+ .unwrap_or(0);
+
+ state
+ .cursor
+ .select_range(state.cursor.start(&value), position);
+
+ return event::Status::Captured;
}
- Event::Keyboard(keyboard::Event::CharacterReceived(c))
- if self.state.is_focused
- && self.state.is_pasting.is_none()
- && !self.state.keyboard_modifiers.command()
- && !c.is_control() =>
+ }
+ Event::Keyboard(keyboard::Event::CharacterReceived(c)) => {
+ let state = state();
+
+ if state.is_focused
+ && state.is_pasting.is_none()
+ && !state.keyboard_modifiers.command()
+ && !c.is_control()
{
- let mut editor =
- Editor::new(&mut self.value, &mut self.state.cursor);
+ let mut editor = Editor::new(value, &mut state.cursor);
editor.insert(c);
- let message = (self.on_change)(editor.contents());
+ let message = (on_change)(editor.contents());
shell.publish(message);
return event::Status::Captured;
}
- Event::Keyboard(keyboard::Event::KeyPressed {
- key_code, ..
- }) if self.state.is_focused => {
- let modifiers = self.state.keyboard_modifiers;
+ }
+ Event::Keyboard(keyboard::Event::KeyPressed { key_code, .. }) => {
+ let state = state();
+
+ if state.is_focused {
+ let modifiers = state.keyboard_modifiers;
match key_code {
keyboard::KeyCode::Enter
| keyboard::KeyCode::NumpadEnter => {
- if let Some(on_submit) = self.on_submit.clone() {
+ if let Some(on_submit) = on_submit.clone() {
shell.publish(on_submit);
}
}
keyboard::KeyCode::Backspace => {
if platform::is_jump_modifier_pressed(modifiers)
- && self
- .state
- .cursor
- .selection(&self.value)
- .is_none()
+ && state.cursor.selection(value).is_none()
{
- if self.is_secure {
- let cursor_pos =
- self.state.cursor.end(&self.value);
- self.state.cursor.select_range(0, cursor_pos);
+ if is_secure {
+ let cursor_pos = state.cursor.end(value);
+ state.cursor.select_range(0, cursor_pos);
} else {
- self.state
- .cursor
- .select_left_by_words(&self.value);
+ state.cursor.select_left_by_words(value);
}
}
- let mut editor = Editor::new(
- &mut self.value,
- &mut self.state.cursor,
- );
-
+ let mut editor = Editor::new(value, &mut state.cursor);
editor.backspace();
- let message = (self.on_change)(editor.contents());
+ let message = (on_change)(editor.contents());
shell.publish(message);
}
keyboard::KeyCode::Delete => {
if platform::is_jump_modifier_pressed(modifiers)
- && self
- .state
- .cursor
- .selection(&self.value)
- .is_none()
+ && state.cursor.selection(value).is_none()
{
- if self.is_secure {
- let cursor_pos =
- self.state.cursor.end(&self.value);
- self.state
+ if is_secure {
+ let cursor_pos = state.cursor.end(value);
+ state
.cursor
- .select_range(cursor_pos, self.value.len());
+ .select_range(cursor_pos, value.len());
} else {
- self.state
- .cursor
- .select_right_by_words(&self.value);
+ state.cursor.select_right_by_words(value);
}
}
- let mut editor = Editor::new(
- &mut self.value,
- &mut self.state.cursor,
- );
-
+ let mut editor = Editor::new(value, &mut state.cursor);
editor.delete();
- let message = (self.on_change)(editor.contents());
+ let message = (on_change)(editor.contents());
shell.publish(message);
}
keyboard::KeyCode::Left => {
if platform::is_jump_modifier_pressed(modifiers)
- && !self.is_secure
+ && !is_secure
{
if modifiers.shift() {
- self.state
- .cursor
- .select_left_by_words(&self.value);
+ state.cursor.select_left_by_words(value);
} else {
- self.state
- .cursor
- .move_left_by_words(&self.value);
+ state.cursor.move_left_by_words(value);
}
} else if modifiers.shift() {
- self.state.cursor.select_left(&self.value)
+ state.cursor.select_left(value)
} else {
- self.state.cursor.move_left(&self.value);
+ state.cursor.move_left(value);
}
}
keyboard::KeyCode::Right => {
if platform::is_jump_modifier_pressed(modifiers)
- && !self.is_secure
+ && !is_secure
{
if modifiers.shift() {
- self.state
- .cursor
- .select_right_by_words(&self.value);
+ state.cursor.select_right_by_words(value);
} else {
- self.state
- .cursor
- .move_right_by_words(&self.value);
+ state.cursor.move_right_by_words(value);
}
} else if modifiers.shift() {
- self.state.cursor.select_right(&self.value)
+ state.cursor.select_right(value)
} else {
- self.state.cursor.move_right(&self.value);
+ state.cursor.move_right(value);
}
}
keyboard::KeyCode::Home => {
if modifiers.shift() {
- self.state.cursor.select_range(
- self.state.cursor.start(&self.value),
- 0,
- );
+ state
+ .cursor
+ .select_range(state.cursor.start(value), 0);
} else {
- self.state.cursor.move_to(0);
+ state.cursor.move_to(0);
}
}
keyboard::KeyCode::End => {
if modifiers.shift() {
- self.state.cursor.select_range(
- self.state.cursor.start(&self.value),
- self.value.len(),
+ state.cursor.select_range(
+ state.cursor.start(value),
+ value.len(),
);
} else {
- self.state.cursor.move_to(self.value.len());
+ state.cursor.move_to(value.len());
}
}
keyboard::KeyCode::C
- if self.state.keyboard_modifiers.command() =>
+ if state.keyboard_modifiers.command() =>
{
- match self.state.cursor.selection(&self.value) {
- Some((start, end)) => {
- clipboard.write(
- self.value.select(start, end).to_string(),
- );
- }
- None => {}
+ if let Some((start, end)) =
+ state.cursor.selection(value)
+ {
+ clipboard
+ .write(value.select(start, end).to_string());
}
}
keyboard::KeyCode::X
- if self.state.keyboard_modifiers.command() =>
+ if state.keyboard_modifiers.command() =>
{
- match self.state.cursor.selection(&self.value) {
- Some((start, end)) => {
- clipboard.write(
- self.value.select(start, end).to_string(),
- );
- }
- None => {}
+ if let Some((start, end)) =
+ state.cursor.selection(value)
+ {
+ clipboard
+ .write(value.select(start, end).to_string());
}
- let mut editor = Editor::new(
- &mut self.value,
- &mut self.state.cursor,
- );
-
+ let mut editor = Editor::new(value, &mut state.cursor);
editor.delete();
- let message = (self.on_change)(editor.contents());
+ let message = (on_change)(editor.contents());
shell.publish(message);
}
keyboard::KeyCode::V => {
- if self.state.keyboard_modifiers.command() {
- let content = match self.state.is_pasting.take() {
+ if state.keyboard_modifiers.command() {
+ let content = match state.is_pasting.take() {
Some(content) => content,
None => {
let content: String = clipboard
.read()
- .unwrap_or(String::new())
+ .unwrap_or_default()
.chars()
.filter(|c| !c.is_control())
.collect();
@@ -693,32 +654,34 @@ where
}
};
- let mut editor = Editor::new(
- &mut self.value,
- &mut self.state.cursor,
- );
+ let mut editor =
+ Editor::new(value, &mut state.cursor);
editor.paste(content.clone());
- let message = (self.on_change)(editor.contents());
+ let message = if let Some(paste) = &on_paste {
+ (paste)(editor.contents())
+ } else {
+ (on_change)(editor.contents())
+ };
shell.publish(message);
- self.state.is_pasting = Some(content);
+ state.is_pasting = Some(content);
} else {
- self.state.is_pasting = None;
+ state.is_pasting = None;
}
}
keyboard::KeyCode::A
- if self.state.keyboard_modifiers.command() =>
+ if state.keyboard_modifiers.command() =>
{
- self.state.cursor.select_all(&self.value);
+ state.cursor.select_all(value);
}
keyboard::KeyCode::Escape => {
- self.state.is_focused = false;
- self.state.is_dragging = false;
- self.state.is_pasting = None;
+ state.is_focused = false;
+ state.is_dragging = false;
+ state.is_pasting = None;
- self.state.keyboard_modifiers =
+ state.keyboard_modifiers =
keyboard::Modifiers::default();
}
keyboard::KeyCode::Tab
@@ -731,12 +694,14 @@ where
return event::Status::Captured;
}
- Event::Keyboard(keyboard::Event::KeyReleased {
- key_code, ..
- }) if self.state.is_focused => {
+ }
+ Event::Keyboard(keyboard::Event::KeyReleased { key_code, .. }) => {
+ let state = state();
+
+ if state.is_focused {
match key_code {
keyboard::KeyCode::V => {
- self.state.is_pasting = None;
+ state.is_pasting = None;
}
keyboard::KeyCode::Tab
| keyboard::KeyCode::Up
@@ -748,53 +713,201 @@ where
return event::Status::Captured;
}
- Event::Keyboard(keyboard::Event::ModifiersChanged(modifiers))
- if self.state.is_focused =>
- {
- self.state.keyboard_modifiers = modifiers;
- }
- _ => {}
}
+ Event::Keyboard(keyboard::Event::ModifiersChanged(modifiers)) => {
+ let state = state();
- event::Status::Ignored
+ if state.is_focused {
+ state.keyboard_modifiers = modifiers;
+ }
+ }
+ _ => {}
}
- fn mouse_interaction(
- &self,
- layout: Layout<'_>,
- cursor_position: Point,
- _viewport: &Rectangle,
- _renderer: &Renderer,
- ) -> mouse::Interaction {
- if layout.bounds().contains(cursor_position) {
- mouse::Interaction::Text
- } else {
- mouse::Interaction::default()
+ event::Status::Ignored
+}
+
+/// Draws the [`TextInput`] with the given [`Renderer`], overriding its
+/// [`Value`] if provided.
+///
+/// [`Renderer`]: text::Renderer
+pub fn draw<Renderer>(
+ renderer: &mut Renderer,
+ theme: &Renderer::Theme,
+ layout: Layout<'_>,
+ cursor_position: Point,
+ state: &State,
+ value: &Value,
+ placeholder: &str,
+ size: Option<u16>,
+ font: &Renderer::Font,
+ is_secure: bool,
+ style: <Renderer::Theme as StyleSheet>::Style,
+) where
+ Renderer: text::Renderer,
+ Renderer::Theme: StyleSheet,
+{
+ let secure_value = is_secure.then(|| value.secure());
+ let value = secure_value.as_ref().unwrap_or(value);
+
+ let bounds = layout.bounds();
+ let text_bounds = layout.children().next().unwrap().bounds();
+
+ let is_mouse_over = bounds.contains(cursor_position);
+
+ let appearance = if state.is_focused() {
+ theme.focused(style)
+ } else if is_mouse_over {
+ theme.hovered(style)
+ } else {
+ theme.active(style)
+ };
+
+ renderer.fill_quad(
+ renderer::Quad {
+ bounds,
+ border_radius: appearance.border_radius,
+ border_width: appearance.border_width,
+ border_color: appearance.border_color,
+ },
+ appearance.background,
+ );
+
+ let text = value.to_string();
+ let size = size.unwrap_or_else(|| renderer.default_size());
+
+ let (cursor, offset) = if state.is_focused() {
+ match state.cursor.state(value) {
+ cursor::State::Index(position) => {
+ let (text_value_width, offset) =
+ measure_cursor_and_scroll_offset(
+ renderer,
+ text_bounds,
+ value,
+ size,
+ position,
+ font.clone(),
+ );
+
+ (
+ Some((
+ renderer::Quad {
+ bounds: Rectangle {
+ x: text_bounds.x + text_value_width,
+ y: text_bounds.y,
+ width: 1.0,
+ height: text_bounds.height,
+ },
+ border_radius: 0.0,
+ border_width: 0.0,
+ border_color: Color::TRANSPARENT,
+ },
+ theme.value_color(style),
+ )),
+ offset,
+ )
+ }
+ cursor::State::Selection { start, end } => {
+ let left = start.min(end);
+ let right = end.max(start);
+
+ let (left_position, left_offset) =
+ measure_cursor_and_scroll_offset(
+ renderer,
+ text_bounds,
+ value,
+ size,
+ left,
+ font.clone(),
+ );
+
+ let (right_position, right_offset) =
+ measure_cursor_and_scroll_offset(
+ renderer,
+ text_bounds,
+ value,
+ size,
+ right,
+ font.clone(),
+ );
+
+ let width = right_position - left_position;
+
+ (
+ Some((
+ renderer::Quad {
+ bounds: Rectangle {
+ x: text_bounds.x + left_position,
+ y: text_bounds.y,
+ width,
+ height: text_bounds.height,
+ },
+ border_radius: 0.0,
+ border_width: 0.0,
+ border_color: Color::TRANSPARENT,
+ },
+ theme.selection_color(style),
+ )),
+ if end == right {
+ right_offset
+ } else {
+ left_offset
+ },
+ )
+ }
+ }
+ } else {
+ (None, 0.0)
+ };
+
+ let text_width = renderer.measure_width(
+ if text.is_empty() { placeholder } else { &text },
+ size,
+ font.clone(),
+ );
+
+ let render = |renderer: &mut Renderer| {
+ if let Some((cursor, color)) = cursor {
+ renderer.fill_quad(cursor, color);
}
- }
- fn draw(
- &self,
- renderer: &mut Renderer,
- _style: &renderer::Style,
- layout: Layout<'_>,
- cursor_position: Point,
- _viewport: &Rectangle,
- ) {
- self.draw(renderer, layout, cursor_position, None)
+ renderer.fill_text(Text {
+ content: if text.is_empty() { placeholder } else { &text },
+ color: if text.is_empty() {
+ theme.placeholder_color(style)
+ } else {
+ theme.value_color(style)
+ },
+ font: font.clone(),
+ bounds: Rectangle {
+ y: text_bounds.center_y(),
+ width: f32::INFINITY,
+ ..text_bounds
+ },
+ size: f32::from(size),
+ horizontal_alignment: alignment::Horizontal::Left,
+ vertical_alignment: alignment::Vertical::Center,
+ });
+ };
+
+ if text_width > text_bounds.width {
+ renderer.with_layer(text_bounds, |renderer| {
+ renderer.with_translation(Vector::new(-offset, 0.0), render)
+ });
+ } else {
+ render(renderer);
}
}
-impl<'a, Message, Renderer> From<TextInput<'a, Message, Renderer>>
- for Element<'a, Message, Renderer>
-where
- Message: 'a + Clone,
- Renderer: 'a + text::Renderer,
-{
- fn from(
- text_input: TextInput<'a, Message, Renderer>,
- ) -> Element<'a, Message, Renderer> {
- Element::new(text_input)
+/// Computes the current [`mouse::Interaction`] of the [`TextInput`].
+pub fn mouse_interaction(
+ layout: Layout<'_>,
+ cursor_position: Point,
+) -> mouse::Interaction {
+ if layout.bounds().contains(cursor_position) {
+ mouse::Interaction::Text
+ } else {
+ mouse::Interaction::default()
}
}
@@ -841,6 +954,7 @@ impl State {
/// Focuses the [`TextInput`].
pub fn focus(&mut self) {
self.is_focused = true;
+ self.move_cursor_to_end();
}
/// Unfocuses the [`TextInput`].
@@ -869,6 +983,20 @@ impl State {
}
}
+impl operation::Focusable for State {
+ fn is_focused(&self) -> bool {
+ State::is_focused(self)
+ }
+
+ fn focus(&mut self) {
+ State::focus(self)
+ }
+
+ fn unfocus(&mut self) {
+ State::unfocus(self)
+ }
+}
+
mod platform {
use crate::keyboard;
@@ -950,16 +1078,16 @@ fn find_cursor_position<Renderer>(
where
Renderer: text::Renderer,
{
- let size = size.unwrap_or(renderer.default_size());
+ let size = size.unwrap_or_else(|| renderer.default_size());
let offset =
- offset(renderer, text_bounds, font.clone(), size, &value, &state);
+ offset(renderer, text_bounds, font.clone(), size, value, state);
renderer
.hit_test(
&value.to_string(),
size.into(),
- font.clone(),
+ font,
Size::INFINITY,
Point::new(x + offset, text_bounds.height / 2.0),
true,
diff --git a/native/src/widget/text_input/editor.rs b/native/src/widget/text_input/editor.rs
index bac530e1..d53fa8d9 100644
--- a/native/src/widget/text_input/editor.rs
+++ b/native/src/widget/text_input/editor.rs
@@ -15,12 +15,9 @@ impl<'a> Editor<'a> {
}
pub fn insert(&mut self, character: char) {
- match self.cursor.selection(self.value) {
- Some((left, right)) => {
- self.cursor.move_left(self.value);
- self.value.remove_many(left, right);
- }
- _ => {}
+ if let Some((left, right)) = self.cursor.selection(self.value) {
+ self.cursor.move_left(self.value);
+ self.value.remove_many(left, right);
}
self.value.insert(self.cursor.end(self.value), character);
@@ -29,13 +26,9 @@ impl<'a> Editor<'a> {
pub fn paste(&mut self, content: Value) {
let length = content.len();
-
- match self.cursor.selection(self.value) {
- Some((left, right)) => {
- self.cursor.move_left(self.value);
- self.value.remove_many(left, right);
- }
- _ => {}
+ if let Some((left, right)) = self.cursor.selection(self.value) {
+ self.cursor.move_left(self.value);
+ self.value.remove_many(left, right);
}
self.value.insert_many(self.cursor.end(self.value), content);
diff --git a/native/src/widget/text_input/value.rs b/native/src/widget/text_input/value.rs
index 2034cca4..cf4da562 100644
--- a/native/src/widget/text_input/value.rs
+++ b/native/src/widget/text_input/value.rs
@@ -37,7 +37,7 @@ impl Value {
let previous_string =
&self.graphemes[..index.min(self.graphemes.len())].concat();
- UnicodeSegmentation::split_word_bound_indices(&previous_string as &str)
+ UnicodeSegmentation::split_word_bound_indices(previous_string as &str)
.filter(|(_, word)| !word.trim_start().is_empty())
.next_back()
.map(|(i, previous_word)| {
@@ -58,9 +58,8 @@ impl Value {
pub fn next_end_of_word(&self, index: usize) -> usize {
let next_string = &self.graphemes[index..].concat();
- UnicodeSegmentation::split_word_bound_indices(&next_string as &str)
- .filter(|(_, word)| !word.trim_start().is_empty())
- .next()
+ UnicodeSegmentation::split_word_bound_indices(next_string as &str)
+ .find(|(_, word)| !word.trim_start().is_empty())
.map(|(i, next_word)| {
index
+ UnicodeSegmentation::graphemes(next_word, true).count()
diff --git a/native/src/widget/toggler.rs b/native/src/widget/toggler.rs
index 48237edb..7893f78c 100644
--- a/native/src/widget/toggler.rs
+++ b/native/src/widget/toggler.rs
@@ -1,20 +1,19 @@
//! Show toggle controls using togglers.
-
use crate::alignment;
use crate::event;
use crate::layout;
use crate::mouse;
use crate::renderer;
use crate::text;
-use crate::widget::{Row, Text};
+use crate::widget::{self, Row, Text, Tree};
use crate::{
Alignment, Clipboard, Element, Event, Layout, Length, Point, Rectangle,
Shell, Widget,
};
-pub use iced_style::toggler::{Style, StyleSheet};
+pub use iced_style::toggler::{Appearance, StyleSheet};
-/// A toggler widget
+/// A toggler widget.
///
/// # Example
///
@@ -30,9 +29,13 @@ pub use iced_style::toggler::{Style, StyleSheet};
/// Toggler::new(is_active, String::from("Toggle me!"), |b| Message::TogglerToggled(b));
/// ```
#[allow(missing_debug_implementations)]
-pub struct Toggler<'a, Message, Renderer: text::Renderer> {
+pub struct Toggler<'a, Message, Renderer>
+where
+ Renderer: text::Renderer,
+ Renderer::Theme: StyleSheet,
+{
is_active: bool,
- on_toggle: Box<dyn Fn(bool) -> Message>,
+ on_toggle: Box<dyn Fn(bool) -> Message + 'a>,
label: Option<String>,
width: Length,
size: u16,
@@ -40,10 +43,14 @@ pub struct Toggler<'a, Message, Renderer: text::Renderer> {
text_alignment: alignment::Horizontal,
spacing: u16,
font: Renderer::Font,
- style_sheet: Box<dyn StyleSheet + 'a>,
+ style: <Renderer::Theme as StyleSheet>::Style,
}
-impl<'a, Message, Renderer: text::Renderer> Toggler<'a, Message, Renderer> {
+impl<'a, Message, Renderer> Toggler<'a, Message, Renderer>
+where
+ Renderer: text::Renderer,
+ Renderer::Theme: StyleSheet,
+{
/// The default size of a [`Toggler`].
pub const DEFAULT_SIZE: u16 = 20;
@@ -61,7 +68,7 @@ impl<'a, Message, Renderer: text::Renderer> Toggler<'a, Message, Renderer> {
f: F,
) -> Self
where
- F: 'static + Fn(bool) -> Message,
+ F: 'a + Fn(bool) -> Message,
{
Toggler {
is_active,
@@ -73,7 +80,7 @@ impl<'a, Message, Renderer: text::Renderer> Toggler<'a, Message, Renderer> {
text_alignment: alignment::Horizontal::Left,
spacing: 0,
font: Renderer::Font::default(),
- style_sheet: Default::default(),
+ style: Default::default(),
}
}
@@ -108,6 +115,8 @@ impl<'a, Message, Renderer: text::Renderer> Toggler<'a, Message, Renderer> {
}
/// Sets the [`Font`] of the text of the [`Toggler`]
+ ///
+ /// [`Font`]: crate::text::Renderer::Font
pub fn font(mut self, font: Renderer::Font) -> Self {
self.font = font;
self
@@ -116,9 +125,9 @@ impl<'a, Message, Renderer: text::Renderer> Toggler<'a, Message, Renderer> {
/// Sets the style of the [`Toggler`].
pub fn style(
mut self,
- style_sheet: impl Into<Box<dyn StyleSheet + 'a>>,
+ style: impl Into<<Renderer::Theme as StyleSheet>::Style>,
) -> Self {
- self.style_sheet = style_sheet.into();
+ self.style = style.into();
self
}
}
@@ -127,6 +136,7 @@ impl<'a, Message, Renderer> Widget<Message, Renderer>
for Toggler<'a, Message, Renderer>
where
Renderer: text::Renderer,
+ Renderer::Theme: StyleSheet + widget::text::StyleSheet,
{
fn width(&self) -> Length {
self.width
@@ -152,7 +162,10 @@ where
.horizontal_alignment(self.text_alignment)
.font(self.font.clone())
.width(self.width)
- .size(self.text_size.unwrap_or(renderer.default_size())),
+ .size(
+ self.text_size
+ .unwrap_or_else(|| renderer.default_size()),
+ ),
);
}
@@ -167,6 +180,7 @@ where
fn on_event(
&mut self,
+ _state: &mut Tree,
event: Event,
layout: Layout<'_>,
cursor_position: Point,
@@ -192,6 +206,7 @@ where
fn mouse_interaction(
&self,
+ _state: &Tree,
layout: Layout<'_>,
cursor_position: Point,
_viewport: &Rectangle,
@@ -206,7 +221,9 @@ where
fn draw(
&self,
+ _state: &Tree,
renderer: &mut Renderer,
+ theme: &Renderer::Theme,
style: &renderer::Style,
layout: Layout<'_>,
cursor_position: Point,
@@ -228,10 +245,10 @@ where
renderer,
style,
label_layout,
- &label,
- self.font.clone(),
+ label,
self.text_size,
- None,
+ self.font.clone(),
+ Default::default(),
self.text_alignment,
alignment::Vertical::Center,
);
@@ -243,9 +260,9 @@ where
let is_mouse_over = bounds.contains(cursor_position);
let style = if is_mouse_over {
- self.style_sheet.hovered(self.is_active)
+ theme.hovered(self.style, self.is_active)
} else {
- self.style_sheet.active(self.is_active)
+ theme.active(self.style, self.is_active)
};
let border_radius = bounds.height as f32 / BORDER_RADIUS_RATIO;
@@ -299,8 +316,9 @@ where
impl<'a, Message, Renderer> From<Toggler<'a, Message, Renderer>>
for Element<'a, Message, Renderer>
where
- Renderer: 'a + text::Renderer,
Message: 'a,
+ Renderer: 'a + text::Renderer,
+ Renderer::Theme: StyleSheet + widget::text::StyleSheet,
{
fn from(
toggler: Toggler<'a, Message, Renderer>,
diff --git a/native/src/widget/tooltip.rs b/native/src/widget/tooltip.rs
index 7989c768..674f2ba6 100644
--- a/native/src/widget/tooltip.rs
+++ b/native/src/widget/tooltip.rs
@@ -4,46 +4,57 @@ use crate::layout;
use crate::mouse;
use crate::renderer;
use crate::text;
+use crate::widget;
use crate::widget::container;
-use crate::widget::text::Text;
+use crate::widget::overlay;
+use crate::widget::{Text, Tree};
use crate::{
Clipboard, Element, Event, Layout, Length, Padding, Point, Rectangle,
Shell, Size, Vector, Widget,
};
+use std::borrow::Cow;
+
/// An element to display a widget over another.
#[allow(missing_debug_implementations)]
-pub struct Tooltip<'a, Message, Renderer: text::Renderer> {
+pub struct Tooltip<'a, Message, Renderer: text::Renderer>
+where
+ Renderer: text::Renderer,
+ Renderer::Theme: container::StyleSheet + widget::text::StyleSheet,
+{
content: Element<'a, Message, Renderer>,
- tooltip: Text<Renderer>,
+ tooltip: Text<'a, Renderer>,
position: Position,
- style_sheet: Box<dyn container::StyleSheet + 'a>,
gap: u16,
padding: u16,
+ snap_within_viewport: bool,
+ style: <Renderer::Theme as container::StyleSheet>::Style,
}
impl<'a, Message, Renderer> Tooltip<'a, Message, Renderer>
where
Renderer: text::Renderer,
+ Renderer::Theme: container::StyleSheet + widget::text::StyleSheet,
{
/// The default padding of a [`Tooltip`] drawn by this renderer.
const DEFAULT_PADDING: u16 = 5;
- /// Creates an empty [`Tooltip`].
+ /// Creates a new [`Tooltip`].
///
/// [`Tooltip`]: struct.Tooltip.html
pub fn new(
content: impl Into<Element<'a, Message, Renderer>>,
- tooltip: impl ToString,
+ tooltip: impl Into<Cow<'a, str>>,
position: Position,
) -> Self {
Tooltip {
content: content.into(),
- tooltip: Text::new(tooltip.to_string()),
+ tooltip: Text::new(tooltip),
position,
- style_sheet: Default::default(),
gap: 0,
padding: Self::DEFAULT_PADDING,
+ snap_within_viewport: true,
+ style: Default::default(),
}
}
@@ -73,42 +84,42 @@ where
self
}
+ /// Sets whether the [`Tooltip`] is snapped within the viewport.
+ pub fn snap_within_viewport(mut self, snap: bool) -> Self {
+ self.snap_within_viewport = snap;
+ self
+ }
+
/// Sets the style of the [`Tooltip`].
pub fn style(
mut self,
- style_sheet: impl Into<Box<dyn container::StyleSheet + 'a>>,
+ style: impl Into<<Renderer::Theme as container::StyleSheet>::Style>,
) -> Self {
- self.style_sheet = style_sheet.into();
+ self.style = style.into();
self
}
}
-/// The position of the tooltip. Defaults to following the cursor.
-#[derive(Debug, Clone, Copy, PartialEq, Eq)]
-pub enum Position {
- /// The tooltip will follow the cursor.
- FollowCursor,
- /// The tooltip will appear on the top of the widget.
- Top,
- /// The tooltip will appear on the bottom of the widget.
- Bottom,
- /// The tooltip will appear on the left of the widget.
- Left,
- /// The tooltip will appear on the right of the widget.
- Right,
-}
-
impl<'a, Message, Renderer> Widget<Message, Renderer>
for Tooltip<'a, Message, Renderer>
where
Renderer: text::Renderer,
+ Renderer::Theme: container::StyleSheet + widget::text::StyleSheet,
{
+ fn children(&self) -> Vec<Tree> {
+ vec![Tree::new(&self.content)]
+ }
+
+ fn diff(&self, tree: &mut Tree) {
+ tree.diff_children(std::slice::from_ref(&self.content))
+ }
+
fn width(&self) -> Length {
- self.content.width()
+ self.content.as_widget().width()
}
fn height(&self) -> Length {
- self.content.height()
+ self.content.as_widget().height()
}
fn layout(
@@ -116,11 +127,12 @@ where
renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
- self.content.layout(renderer, limits)
+ self.content.as_widget().layout(renderer, limits)
}
fn on_event(
&mut self,
+ tree: &mut Tree,
event: Event,
layout: Layout<'_>,
cursor_position: Point,
@@ -128,7 +140,8 @@ where
clipboard: &mut dyn Clipboard,
shell: &mut Shell<'_, Message>,
) -> event::Status {
- self.content.widget.on_event(
+ self.content.as_widget_mut().on_event(
+ &mut tree.children[0],
event,
layout,
cursor_position,
@@ -140,12 +153,14 @@ where
fn mouse_interaction(
&self,
+ tree: &Tree,
layout: Layout<'_>,
cursor_position: Point,
viewport: &Rectangle,
renderer: &Renderer,
) -> mouse::Interaction {
- self.content.mouse_interaction(
+ self.content.as_widget().mouse_interaction(
+ &tree.children[0],
layout,
cursor_position,
viewport,
@@ -155,77 +170,185 @@ where
fn draw(
&self,
+ tree: &Tree,
renderer: &mut Renderer,
+ theme: &Renderer::Theme,
inherited_style: &renderer::Style,
layout: Layout<'_>,
cursor_position: Point,
viewport: &Rectangle,
) {
- self.content.draw(
+ self.content.as_widget().draw(
+ &tree.children[0],
renderer,
+ theme,
inherited_style,
layout,
cursor_position,
viewport,
);
- let bounds = layout.bounds();
+ let tooltip = &self.tooltip;
- if bounds.contains(cursor_position) {
- let gap = f32::from(self.gap);
- let style = self.style_sheet.style();
+ draw(
+ renderer,
+ theme,
+ inherited_style,
+ layout,
+ cursor_position,
+ viewport,
+ self.position,
+ self.gap,
+ self.padding,
+ self.snap_within_viewport,
+ self.style,
+ |renderer, limits| {
+ Widget::<(), Renderer>::layout(tooltip, renderer, limits)
+ },
+ |renderer, defaults, layout, cursor_position, viewport| {
+ Widget::<(), Renderer>::draw(
+ tooltip,
+ &Tree::empty(),
+ renderer,
+ theme,
+ defaults,
+ layout,
+ cursor_position,
+ viewport,
+ );
+ },
+ );
+ }
- let defaults = renderer::Style {
- text_color: style
- .text_color
- .unwrap_or(inherited_style.text_color),
- };
+ fn overlay<'b>(
+ &'b self,
+ tree: &'b mut Tree,
+ layout: Layout<'_>,
+ renderer: &Renderer,
+ ) -> Option<overlay::Element<'b, Message, Renderer>> {
+ self.content.as_widget().overlay(
+ &mut tree.children[0],
+ layout,
+ renderer,
+ )
+ }
+}
- let text_layout = Widget::<(), Renderer>::layout(
- &self.tooltip,
- renderer,
- &layout::Limits::new(Size::ZERO, viewport.size())
- .pad(Padding::new(self.padding)),
- );
-
- let padding = f32::from(self.padding);
- let text_bounds = text_layout.bounds();
- let x_center = bounds.x + (bounds.width - text_bounds.width) / 2.0;
- let y_center =
- bounds.y + (bounds.height - text_bounds.height) / 2.0;
-
- let mut tooltip_bounds = {
- let offset = match self.position {
- Position::Top => Vector::new(
- x_center,
- bounds.y - text_bounds.height - gap - padding,
- ),
- Position::Bottom => Vector::new(
- x_center,
- bounds.y + bounds.height + gap + padding,
- ),
- Position::Left => Vector::new(
- bounds.x - text_bounds.width - gap - padding,
- y_center,
- ),
- Position::Right => Vector::new(
- bounds.x + bounds.width + gap + padding,
- y_center,
- ),
- Position::FollowCursor => Vector::new(
- cursor_position.x,
- cursor_position.y - text_bounds.height,
- ),
- };
-
- Rectangle {
- x: offset.x - padding,
- y: offset.y - padding,
- width: text_bounds.width + padding * 2.0,
- height: text_bounds.height + padding * 2.0,
- }
+impl<'a, Message, Renderer> From<Tooltip<'a, Message, Renderer>>
+ for Element<'a, Message, Renderer>
+where
+ Message: 'a,
+ Renderer: 'a + text::Renderer,
+ Renderer::Theme: container::StyleSheet + widget::text::StyleSheet,
+{
+ fn from(
+ tooltip: Tooltip<'a, Message, Renderer>,
+ ) -> Element<'a, Message, Renderer> {
+ Element::new(tooltip)
+ }
+}
+
+/// The position of the tooltip. Defaults to following the cursor.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum Position {
+ /// The tooltip will follow the cursor.
+ FollowCursor,
+ /// The tooltip will appear on the top of the widget.
+ Top,
+ /// The tooltip will appear on the bottom of the widget.
+ Bottom,
+ /// The tooltip will appear on the left of the widget.
+ Left,
+ /// The tooltip will appear on the right of the widget.
+ Right,
+}
+
+/// Draws a [`Tooltip`].
+pub fn draw<Renderer>(
+ renderer: &mut Renderer,
+ theme: &Renderer::Theme,
+ inherited_style: &renderer::Style,
+ layout: Layout<'_>,
+ cursor_position: Point,
+ viewport: &Rectangle,
+ position: Position,
+ gap: u16,
+ padding: u16,
+ snap_within_viewport: bool,
+ style: <Renderer::Theme as container::StyleSheet>::Style,
+ layout_text: impl FnOnce(&Renderer, &layout::Limits) -> layout::Node,
+ draw_text: impl FnOnce(
+ &mut Renderer,
+ &renderer::Style,
+ Layout<'_>,
+ Point,
+ &Rectangle,
+ ),
+) where
+ Renderer: crate::Renderer,
+ Renderer::Theme: container::StyleSheet,
+{
+ use container::StyleSheet;
+
+ let bounds = layout.bounds();
+
+ if bounds.contains(cursor_position) {
+ let gap = f32::from(gap);
+ let style = theme.appearance(style);
+
+ let defaults = renderer::Style {
+ text_color: style.text_color.unwrap_or(inherited_style.text_color),
+ };
+
+ let text_layout = layout_text(
+ renderer,
+ &layout::Limits::new(
+ Size::ZERO,
+ snap_within_viewport
+ .then(|| viewport.size())
+ .unwrap_or(Size::INFINITY),
+ )
+ .pad(Padding::new(padding)),
+ );
+
+ let padding = f32::from(padding);
+ let text_bounds = text_layout.bounds();
+ let x_center = bounds.x + (bounds.width - text_bounds.width) / 2.0;
+ let y_center = bounds.y + (bounds.height - text_bounds.height) / 2.0;
+
+ let mut tooltip_bounds = {
+ let offset = match position {
+ Position::Top => Vector::new(
+ x_center,
+ bounds.y - text_bounds.height - gap - padding,
+ ),
+ Position::Bottom => Vector::new(
+ x_center,
+ bounds.y + bounds.height + gap + padding,
+ ),
+ Position::Left => Vector::new(
+ bounds.x - text_bounds.width - gap - padding,
+ y_center,
+ ),
+ Position::Right => Vector::new(
+ bounds.x + bounds.width + gap + padding,
+ y_center,
+ ),
+ Position::FollowCursor => Vector::new(
+ cursor_position.x,
+ cursor_position.y - text_bounds.height,
+ ),
};
+ Rectangle {
+ x: offset.x - padding,
+ y: offset.y - padding,
+ width: text_bounds.width + padding * 2.0,
+ height: text_bounds.height + padding * 2.0,
+ }
+ };
+
+ if snap_within_viewport {
if tooltip_bounds.x < viewport.x {
tooltip_bounds.x = viewport.x;
} else if viewport.x + viewport.width
@@ -243,38 +366,24 @@ where
tooltip_bounds.y =
viewport.y + viewport.height - tooltip_bounds.height;
}
+ }
- renderer.with_layer(*viewport, |renderer| {
- container::draw_background(renderer, &style, tooltip_bounds);
+ renderer.with_layer(Rectangle::with_size(Size::INFINITY), |renderer| {
+ container::draw_background(renderer, &style, tooltip_bounds);
- Widget::<(), Renderer>::draw(
- &self.tooltip,
- renderer,
- &defaults,
- Layout::with_offset(
- Vector::new(
- tooltip_bounds.x + padding,
- tooltip_bounds.y + padding,
- ),
- &text_layout,
+ draw_text(
+ renderer,
+ &defaults,
+ Layout::with_offset(
+ Vector::new(
+ tooltip_bounds.x + padding,
+ tooltip_bounds.y + padding,
),
- cursor_position,
- viewport,
- );
- });
- }
- }
-}
-
-impl<'a, Message, Renderer> From<Tooltip<'a, Message, Renderer>>
- for Element<'a, Message, Renderer>
-where
- Renderer: 'a + text::Renderer,
- Message: 'a,
-{
- fn from(
- column: Tooltip<'a, Message, Renderer>,
- ) -> Element<'a, Message, Renderer> {
- Element::new(column)
+ &text_layout,
+ ),
+ cursor_position,
+ viewport,
+ )
+ });
}
}
diff --git a/native/src/widget/tree.rs b/native/src/widget/tree.rs
new file mode 100644
index 00000000..a8b1a185
--- /dev/null
+++ b/native/src/widget/tree.rs
@@ -0,0 +1,187 @@
+//! Store internal widget state in a state tree to ensure continuity.
+use crate::Widget;
+
+use std::any::{self, Any};
+use std::borrow::Borrow;
+use std::fmt;
+
+/// A persistent state widget tree.
+///
+/// A [`Tree`] is normally associated with a specific widget in the widget tree.
+#[derive(Debug)]
+pub struct Tree {
+ /// The tag of the [`Tree`].
+ pub tag: Tag,
+
+ /// The [`State`] of the [`Tree`].
+ pub state: State,
+
+ /// The children of the root widget of the [`Tree`].
+ pub children: Vec<Tree>,
+}
+
+impl Tree {
+ /// Creates an empty, stateless [`Tree`] with no children.
+ pub fn empty() -> Self {
+ Self {
+ tag: Tag::stateless(),
+ state: State::None,
+ children: Vec::new(),
+ }
+ }
+
+ /// Creates a new [`Tree`] for the provided [`Element`].
+ pub fn new<'a, Message, Renderer>(
+ widget: impl Borrow<dyn Widget<Message, Renderer> + 'a>,
+ ) -> Self
+ where
+ Renderer: crate::Renderer,
+ {
+ let widget = widget.borrow();
+
+ Self {
+ tag: widget.tag(),
+ state: widget.state(),
+ children: widget.children(),
+ }
+ }
+
+ /// Reconciliates the current tree with the provided [`Element`].
+ ///
+ /// If the tag of the [`Element`] matches the tag of the [`Tree`], then the
+ /// [`Element`] proceeds with the reconciliation (i.e. [`Widget::diff`] is called).
+ ///
+ /// Otherwise, the whole [`Tree`] is recreated.
+ ///
+ /// [`Widget::diff`]: crate::Widget::diff
+ pub fn diff<'a, Message, Renderer>(
+ &mut self,
+ new: impl Borrow<dyn Widget<Message, Renderer> + 'a>,
+ ) where
+ Renderer: crate::Renderer,
+ {
+ if self.tag == new.borrow().tag() {
+ new.borrow().diff(self)
+ } else {
+ *self = Self::new(new);
+ }
+ }
+
+ /// Reconciliates the children of the tree with the provided list of [`Element`].
+ pub fn diff_children<'a, Message, Renderer>(
+ &mut self,
+ new_children: &[impl Borrow<dyn Widget<Message, Renderer> + 'a>],
+ ) where
+ Renderer: crate::Renderer,
+ {
+ self.diff_children_custom(
+ new_children,
+ |tree, widget| tree.diff(widget.borrow()),
+ |widget| Self::new(widget.borrow()),
+ )
+ }
+
+ /// Reconciliates the children of the tree with the provided list of [`Element`] using custom
+ /// logic both for diffing and creating new widget state.
+ pub fn diff_children_custom<T>(
+ &mut self,
+ new_children: &[T],
+ diff: impl Fn(&mut Tree, &T),
+ new_state: impl Fn(&T) -> Self,
+ ) {
+ if self.children.len() > new_children.len() {
+ self.children.truncate(new_children.len());
+ }
+
+ for (child_state, new) in
+ self.children.iter_mut().zip(new_children.iter())
+ {
+ diff(child_state, new);
+ }
+
+ if self.children.len() < new_children.len() {
+ self.children.extend(
+ new_children[self.children.len()..].iter().map(new_state),
+ );
+ }
+ }
+}
+
+/// The identifier of some widget state.
+#[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash)]
+pub struct Tag(any::TypeId);
+
+impl Tag {
+ /// Creates a [`Tag`] for a state of type `T`.
+ pub fn of<T>() -> Self
+ where
+ T: 'static,
+ {
+ Self(any::TypeId::of::<T>())
+ }
+
+ /// Creates a [`Tag`] for a stateless widget.
+ pub fn stateless() -> Self {
+ Self::of::<()>()
+ }
+}
+
+/// The internal [`State`] of a widget.
+pub enum State {
+ /// No meaningful internal state.
+ None,
+
+ /// Some meaningful internal state.
+ Some(Box<dyn Any>),
+}
+
+impl State {
+ /// Creates a new [`State`].
+ pub fn new<T>(state: T) -> Self
+ where
+ T: 'static,
+ {
+ State::Some(Box::new(state))
+ }
+
+ /// Downcasts the [`State`] to `T` and returns a reference to it.
+ ///
+ /// # Panics
+ /// This method will panic if the downcast fails or the [`State`] is [`State::None`].
+ pub fn downcast_ref<T>(&self) -> &T
+ where
+ T: 'static,
+ {
+ match self {
+ State::None => panic!("Downcast on stateless state"),
+ State::Some(state) => {
+ state.downcast_ref().expect("Downcast widget state")
+ }
+ }
+ }
+
+ /// Downcasts the [`State`] to `T` and returns a mutable reference to it.
+ ///
+ /// # Panics
+ /// This method will panic if the downcast fails or the [`State`] is [`State::None`].
+ pub fn downcast_mut<T>(&mut self) -> &mut T
+ where
+ T: 'static,
+ {
+ match self {
+ State::None => panic!("Downcast on stateless state"),
+ State::Some(state) => {
+ state.downcast_mut().expect("Downcast widget state")
+ }
+ }
+ }
+}
+
+impl fmt::Debug for State {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ match self {
+ Self::None => write!(f, "State::None"),
+ Self::Some(_) => write!(f, "State::Some"),
+ }
+ }
+}