Introduce Shell type in iced_native

Widgets now can invalidate the current layout of the application on demand.
This commit is contained in:
Héctor Ramón Jiménez 2021-11-29 16:22:01 +07:00
parent f7792d89d6
commit bbd9355450
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
26 changed files with 218 additions and 130 deletions

View file

@ -9,7 +9,7 @@ use crate::renderer;
use crate::touch;
use crate::{
Background, Clipboard, Color, Element, Hasher, Layout, Length, Padding,
Point, Rectangle, Vector, Widget,
Point, Rectangle, Shell, Vector, Widget,
};
use std::hash::Hash;
@ -197,7 +197,7 @@ where
cursor_position: Point,
renderer: &Renderer,
clipboard: &mut dyn Clipboard,
messages: &mut Vec<Message>,
shell: &mut Shell<'_, Message>,
) -> event::Status {
if let event::Status::Captured = self.content.on_event(
event.clone(),
@ -205,7 +205,7 @@ where
cursor_position,
renderer,
clipboard,
messages,
shell,
) {
return event::Status::Captured;
}
@ -232,7 +232,7 @@ where
self.state.is_pressed = false;
if bounds.contains(cursor_position) {
messages.push(on_press);
shell.publish(on_press);
}
return event::Status::Captured;

View file

@ -11,7 +11,7 @@ use crate::touch;
use crate::widget::{self, Row, Text};
use crate::{
Alignment, Clipboard, Color, Element, Hasher, Layout, Length, Point,
Rectangle, Widget,
Rectangle, Shell, Widget,
};
pub use iced_style::checkbox::{Style, StyleSheet};
@ -171,7 +171,7 @@ where
cursor_position: Point,
_renderer: &Renderer,
_clipboard: &mut dyn Clipboard,
messages: &mut Vec<Message>,
shell: &mut Shell<'_, Message>,
) -> event::Status {
match event {
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
@ -179,7 +179,7 @@ where
let mouse_over = layout.bounds().contains(cursor_position);
if mouse_over {
messages.push((self.on_toggle)(!self.is_checked));
shell.publish((self.on_toggle)(!self.is_checked));
return event::Status::Captured;
}

View file

@ -8,7 +8,7 @@ use crate::overlay;
use crate::renderer;
use crate::{
Alignment, Clipboard, Element, Hasher, Layout, Length, Padding, Point,
Rectangle, Widget,
Rectangle, Shell, Widget,
};
use std::u32;
@ -146,7 +146,7 @@ where
cursor_position: Point,
renderer: &Renderer,
clipboard: &mut dyn Clipboard,
messages: &mut Vec<Message>,
shell: &mut Shell<'_, Message>,
) -> event::Status {
self.children
.iter_mut()
@ -158,7 +158,7 @@ where
cursor_position,
renderer,
clipboard,
messages,
shell,
)
})
.fold(event::Status::Ignored, event::Status::merge)

View file

@ -9,7 +9,7 @@ use crate::overlay;
use crate::renderer;
use crate::{
Background, Clipboard, Color, Element, Hasher, Layout, Length, Padding,
Point, Rectangle, Widget,
Point, Rectangle, Shell, Widget,
};
use std::u32;
@ -167,7 +167,7 @@ where
cursor_position: Point,
renderer: &Renderer,
clipboard: &mut dyn Clipboard,
messages: &mut Vec<Message>,
shell: &mut Shell<'_, Message>,
) -> event::Status {
self.content.widget.on_event(
event,
@ -175,7 +175,7 @@ where
cursor_position,
renderer,
clipboard,
messages,
shell,
)
}

View file

@ -5,8 +5,8 @@ use crate::layout;
use crate::mouse;
use crate::renderer;
use crate::{
Clipboard, Element, Hasher, Layout, Length, Point, Rectangle, Size, Vector,
Widget,
Clipboard, Element, Hasher, Layout, Length, Point, Rectangle, Shell, Size,
Vector, Widget,
};
use std::hash::Hash;
@ -169,7 +169,7 @@ where
cursor_position: Point,
renderer: &Renderer,
_clipboard: &mut dyn Clipboard,
_messages: &mut Vec<Message>,
_shell: &mut Shell<'_, Message>,
) -> event::Status {
let bounds = layout.bounds();
let is_mouse_over = bounds.contains(cursor_position);

View file

@ -34,8 +34,8 @@ use crate::overlay;
use crate::renderer;
use crate::touch;
use crate::{
Clipboard, Color, Element, Hasher, Layout, Length, Point, Rectangle, Size,
Vector, Widget,
Clipboard, Color, Element, Hasher, Layout, Length, Point, Rectangle, Shell,
Size, Vector, Widget,
};
pub use iced_style::pane_grid::{Line, StyleSheet};
@ -205,7 +205,7 @@ where
&mut self,
layout: Layout<'_>,
cursor_position: Point,
messages: &mut Vec<Message>,
shell: &mut Shell<'_, Message>,
) {
let mut clicked_region =
self.elements.iter().zip(layout.children()).filter(
@ -214,7 +214,7 @@ where
if let Some(((pane, content), layout)) = clicked_region.next() {
if let Some(on_click) = &self.on_click {
messages.push(on_click(*pane));
shell.publish(on_click(*pane));
}
if let Some(on_drag) = &self.on_drag {
@ -226,7 +226,7 @@ where
self.state.pick_pane(pane, origin);
messages.push(on_drag(DragEvent::Picked { pane: *pane }));
shell.publish(on_drag(DragEvent::Picked { pane: *pane }));
}
}
}
@ -236,7 +236,7 @@ where
&mut self,
layout: Layout<'_>,
cursor_position: Point,
messages: &mut Vec<Message>,
shell: &mut Shell<'_, Message>,
) -> event::Status {
if let Some((_, on_resize)) = &self.on_resize {
if let Some((split, _)) = self.state.picked_split() {
@ -263,7 +263,7 @@ where
}
};
messages.push(on_resize(ResizeEvent { split, ratio }));
shell.publish(on_resize(ResizeEvent { split, ratio }));
return event::Status::Captured;
}
@ -362,7 +362,7 @@ where
cursor_position: Point,
renderer: &Renderer,
clipboard: &mut dyn Clipboard,
messages: &mut Vec<Message>,
shell: &mut Shell<'_, Message>,
) -> event::Status {
let mut event_status = event::Status::Ignored;
@ -395,15 +395,11 @@ where
if let Some((split, axis, _)) = clicked_split {
self.state.pick_split(&split, axis);
} else {
self.click_pane(
layout,
cursor_position,
messages,
);
self.click_pane(layout, cursor_position, shell);
}
}
None => {
self.click_pane(layout, cursor_position, messages);
self.click_pane(layout, cursor_position, shell);
}
}
}
@ -430,7 +426,7 @@ where
_ => DragEvent::Canceled { pane },
};
messages.push(on_drag(event));
shell.publish(on_drag(event));
}
self.state.idle();
@ -445,7 +441,7 @@ where
Event::Mouse(mouse::Event::CursorMoved { .. })
| Event::Touch(touch::Event::FingerMoved { .. }) => {
event_status =
self.trigger_resize(layout, cursor_position, messages);
self.trigger_resize(layout, cursor_position, shell);
}
_ => {}
}
@ -464,7 +460,7 @@ where
cursor_position,
renderer,
clipboard,
messages,
shell,
is_picked,
)
})

View file

@ -5,7 +5,9 @@ use crate::overlay;
use crate::renderer;
use crate::widget::container;
use crate::widget::pane_grid::TitleBar;
use crate::{Clipboard, Element, Hasher, Layout, Point, Rectangle, Size};
use crate::{
Clipboard, Element, Hasher, Layout, Point, Rectangle, Shell, Size,
};
/// The content of a [`Pane`].
///
@ -160,7 +162,7 @@ where
cursor_position: Point,
renderer: &Renderer,
clipboard: &mut dyn Clipboard,
messages: &mut Vec<Message>,
shell: &mut Shell<'_, Message>,
is_picked: bool,
) -> event::Status {
let mut event_status = event::Status::Ignored;
@ -174,7 +176,7 @@ where
cursor_position,
renderer,
clipboard,
messages,
shell,
);
children.next().unwrap()
@ -191,7 +193,7 @@ where
cursor_position,
renderer,
clipboard,
messages,
shell,
)
};

View file

@ -5,7 +5,7 @@ use crate::overlay;
use crate::renderer;
use crate::widget::container;
use crate::{
Clipboard, Element, Hasher, Layout, Padding, Point, Rectangle, Size,
Clipboard, Element, Hasher, Layout, Padding, Point, Rectangle, Shell, Size,
};
/// The title bar of a [`Pane`].
@ -218,7 +218,7 @@ where
cursor_position: Point,
renderer: &Renderer,
clipboard: &mut dyn Clipboard,
messages: &mut Vec<Message>,
shell: &mut Shell<'_, Message>,
) -> event::Status {
let mut children = layout.children();
let padded = children.next().unwrap();
@ -235,7 +235,7 @@ where
cursor_position,
renderer,
clipboard,
messages,
shell,
)
} else {
event::Status::Ignored
@ -247,7 +247,7 @@ where
cursor_position,
renderer,
clipboard,
messages,
shell,
);
control_status.merge(title_status)

View file

@ -11,7 +11,7 @@ use crate::text::{self, Text};
use crate::touch;
use crate::{
Clipboard, Element, Hasher, Layout, Length, Padding, Point, Rectangle,
Size, Widget,
Shell, Size, Widget,
};
use std::borrow::Cow;
@ -245,7 +245,7 @@ where
cursor_position: Point,
_renderer: &Renderer,
_clipboard: &mut dyn Clipboard,
messages: &mut Vec<Message>,
shell: &mut Shell<'_, Message>,
) -> event::Status {
match event {
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
@ -271,7 +271,7 @@ where
};
if let Some(last_selection) = self.last_selection.take() {
messages.push((self.on_selected)(last_selection));
shell.publish((self.on_selected)(last_selection));
*self.is_open = false;
@ -312,7 +312,7 @@ where
};
if let Some(next_option) = next_option {
messages.push((self.on_selected)(next_option.clone()));
shell.publish((self.on_selected)(next_option.clone()));
}
event::Status::Captured

View file

@ -11,7 +11,7 @@ use crate::touch;
use crate::widget::{self, Row, Text};
use crate::{
Alignment, Clipboard, Color, Element, Hasher, Layout, Length, Point,
Rectangle, Widget,
Rectangle, Shell, Widget,
};
pub use iced_style::radio::{Style, StyleSheet};
@ -187,13 +187,13 @@ where
cursor_position: Point,
_renderer: &Renderer,
_clipboard: &mut dyn Clipboard,
messages: &mut Vec<Message>,
shell: &mut Shell<'_, Message>,
) -> event::Status {
match event {
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
| Event::Touch(touch::Event::FingerPressed { .. }) => {
if layout.bounds().contains(cursor_position) {
messages.push(self.on_click.clone());
shell.publish(self.on_click.clone());
return event::Status::Captured;
}

View file

@ -6,7 +6,7 @@ use crate::overlay;
use crate::renderer;
use crate::{
Alignment, Clipboard, Element, Hasher, Layout, Length, Padding, Point,
Rectangle, Widget,
Rectangle, Shell, Widget,
};
use std::hash::Hash;
@ -145,7 +145,7 @@ where
cursor_position: Point,
renderer: &Renderer,
clipboard: &mut dyn Clipboard,
messages: &mut Vec<Message>,
shell: &mut Shell<'_, Message>,
) -> event::Status {
self.children
.iter_mut()
@ -157,7 +157,7 @@ where
cursor_position,
renderer,
clipboard,
messages,
shell,
)
})
.fold(event::Status::Ignored, event::Status::merge)

View file

@ -8,7 +8,7 @@ use crate::touch;
use crate::widget::Column;
use crate::{
Alignment, Background, Clipboard, Color, Element, Hasher, Layout, Length,
Padding, Point, Rectangle, Size, Vector, Widget,
Padding, Point, Rectangle, Shell, Size, Vector, Widget,
};
use std::{f32, hash::Hash, u32};
@ -144,14 +144,14 @@ impl<'a, Message, Renderer: crate::Renderer> Scrollable<'a, Message, Renderer> {
&self,
bounds: Rectangle,
content_bounds: Rectangle,
messages: &mut Vec<Message>,
shell: &mut Shell<'_, Message>,
) {
if content_bounds.height <= bounds.height {
return;
}
if let Some(on_scroll) = &self.on_scroll {
messages.push(on_scroll(
shell.publish(on_scroll(
self.state.offset.absolute(bounds, content_bounds)
/ (content_bounds.height - bounds.height),
));
@ -251,7 +251,7 @@ where
cursor_position: Point,
renderer: &Renderer,
clipboard: &mut dyn Clipboard,
messages: &mut Vec<Message>,
shell: &mut Shell<'_, Message>,
) -> event::Status {
let bounds = layout.bounds();
let is_mouse_over = bounds.contains(cursor_position);
@ -286,7 +286,7 @@ where
cursor_position,
renderer,
clipboard,
messages,
shell,
)
};
@ -307,7 +307,7 @@ where
}
}
self.notify_on_scroll(bounds, content_bounds, messages);
self.notify_on_scroll(bounds, content_bounds, shell);
return event::Status::Captured;
}
@ -336,7 +336,7 @@ where
self.notify_on_scroll(
bounds,
content_bounds,
messages,
shell,
);
}
}
@ -377,7 +377,7 @@ where
content_bounds,
);
self.notify_on_scroll(bounds, content_bounds, messages);
self.notify_on_scroll(bounds, content_bounds, shell);
return event::Status::Captured;
}
@ -409,7 +409,7 @@ where
self.notify_on_scroll(
bounds,
content_bounds,
messages,
shell,
);
return event::Status::Captured;

View file

@ -8,7 +8,7 @@ use crate::renderer;
use crate::touch;
use crate::{
Background, Clipboard, Color, Element, Hasher, Layout, Length, Point,
Rectangle, Size, Widget,
Rectangle, Shell, Size, Widget,
};
use std::hash::Hash;
@ -191,7 +191,7 @@ where
cursor_position: Point,
_renderer: &Renderer,
_clipboard: &mut dyn Clipboard,
messages: &mut Vec<Message>,
shell: &mut Shell<'_, Message>,
) -> event::Status {
let is_dragging = self.state.is_dragging;
@ -220,7 +220,7 @@ where
};
if (self.value.into() - new_value.into()).abs() > f64::EPSILON {
messages.push((self.on_change)(new_value));
shell.publish((self.on_change)(new_value));
self.value = new_value;
}
@ -241,7 +241,7 @@ where
| Event::Touch(touch::Event::FingerLost { .. }) => {
if is_dragging {
if let Some(on_release) = self.on_release.clone() {
messages.push(on_release);
shell.publish(on_release);
}
self.state.is_dragging = false;

View file

@ -21,7 +21,7 @@ use crate::text::{self, Text};
use crate::touch;
use crate::{
Clipboard, Color, Element, Hasher, Layout, Length, Padding, Point,
Rectangle, Size, Vector, Widget,
Rectangle, Shell, Size, Vector, Widget,
};
use std::u32;
@ -384,7 +384,7 @@ where
cursor_position: Point,
renderer: &Renderer,
clipboard: &mut dyn Clipboard,
messages: &mut Vec<Message>,
shell: &mut Shell<'_, Message>,
) -> event::Status {
match event {
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
@ -509,7 +509,7 @@ where
editor.insert(c);
let message = (self.on_change)(editor.contents());
messages.push(message);
shell.publish(message);
return event::Status::Captured;
}
@ -521,7 +521,7 @@ where
match key_code {
keyboard::KeyCode::Enter => {
if let Some(on_submit) = self.on_submit.clone() {
messages.push(on_submit);
shell.publish(on_submit);
}
}
keyboard::KeyCode::Backspace => {
@ -551,7 +551,7 @@ where
editor.backspace();
let message = (self.on_change)(editor.contents());
messages.push(message);
shell.publish(message);
}
keyboard::KeyCode::Delete => {
if platform::is_jump_modifier_pressed(modifiers)
@ -582,7 +582,7 @@ where
editor.delete();
let message = (self.on_change)(editor.contents());
messages.push(message);
shell.publish(message);
}
keyboard::KeyCode::Left => {
if platform::is_jump_modifier_pressed(modifiers)
@ -674,7 +674,7 @@ where
editor.delete();
let message = (self.on_change)(editor.contents());
messages.push(message);
shell.publish(message);
}
keyboard::KeyCode::V => {
if self.state.keyboard_modifiers.command() {
@ -700,7 +700,7 @@ where
editor.paste(content.clone());
let message = (self.on_change)(editor.contents());
messages.push(message);
shell.publish(message);
self.state.is_pasting = Some(content);
} else {

View file

@ -10,7 +10,7 @@ use crate::text;
use crate::widget::{Row, Text};
use crate::{
Alignment, Clipboard, Element, Event, Hasher, Layout, Length, Point,
Rectangle, Widget,
Rectangle, Shell, Widget,
};
pub use iced_style::toggler::{Style, StyleSheet};
@ -173,14 +173,14 @@ where
cursor_position: Point,
_renderer: &Renderer,
_clipboard: &mut dyn Clipboard,
messages: &mut Vec<Message>,
shell: &mut Shell<'_, Message>,
) -> event::Status {
match event {
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) => {
let mouse_over = layout.bounds().contains(cursor_position);
if mouse_over {
messages.push((self.on_toggle)(!self.is_active));
shell.publish((self.on_toggle)(!self.is_active));
event::Status::Captured
} else {

View file

@ -11,8 +11,8 @@ use crate::text;
use crate::widget::container;
use crate::widget::text::Text;
use crate::{
Clipboard, Element, Event, Hasher, Layout, Length, Padding, Point, Size,
Vector, Widget,
Clipboard, Element, Event, Hasher, Layout, Length, Padding, Point, Shell,
Size, Vector, Widget,
};
/// An element to display a widget over another.
@ -130,7 +130,7 @@ where
cursor_position: Point,
renderer: &Renderer,
clipboard: &mut dyn Clipboard,
messages: &mut Vec<Message>,
shell: &mut Shell<'_, Message>,
) -> event::Status {
self.content.widget.on_event(
event,
@ -138,7 +138,7 @@ where
cursor_position,
renderer,
clipboard,
messages,
shell,
)
}