diff options
author | 2024-12-03 22:03:06 +0100 | |
---|---|---|
committer | 2024-12-10 04:51:08 +0100 | |
commit | d09d5d45ae4697eef277dfe30756b91c7d802a94 (patch) | |
tree | ce1852b218aa46f5400cee383ccfb68faa61abaf /core/src | |
parent | d6182299b9db7a1928b7740736d53196e33d66e3 (diff) | |
download | iced-d09d5d45ae4697eef277dfe30756b91c7d802a94.tar.gz iced-d09d5d45ae4697eef277dfe30756b91c7d802a94.tar.bz2 iced-d09d5d45ae4697eef277dfe30756b91c7d802a94.zip |
Draft `iced_test` crate and test `todos` example
Diffstat (limited to 'core/src')
-rw-r--r-- | core/src/keyboard/key.rs | 6 | ||||
-rw-r--r-- | core/src/widget/operation.rs | 199 | ||||
-rw-r--r-- | core/src/widget/operation/focusable.rs | 35 | ||||
-rw-r--r-- | core/src/widget/operation/scrollable.rs | 6 | ||||
-rw-r--r-- | core/src/widget/operation/text_input.rs | 28 | ||||
-rw-r--r-- | core/src/widget/text.rs | 12 |
6 files changed, 231 insertions, 55 deletions
diff --git a/core/src/keyboard/key.rs b/core/src/keyboard/key.rs index 69a91902..47169d9a 100644 --- a/core/src/keyboard/key.rs +++ b/core/src/keyboard/key.rs @@ -32,6 +32,12 @@ impl Key { } } +impl From<Named> for Key { + fn from(named: Named) -> Self { + Self::Named(named) + } +} + /// A named key. /// /// This is mostly the `NamedKey` type found in [`winit`]. diff --git a/core/src/widget/operation.rs b/core/src/widget/operation.rs index 6bdb27f6..8fc627bf 100644 --- a/core/src/widget/operation.rs +++ b/core/src/widget/operation.rs @@ -30,24 +30,45 @@ pub trait Operation<T = ()>: Send { ); /// Operates on a widget that can be focused. - fn focusable(&mut self, _state: &mut dyn Focusable, _id: Option<&Id>) {} + fn focusable( + &mut self, + _id: Option<&Id>, + _bounds: Rectangle, + _state: &mut dyn Focusable, + ) { + } /// Operates on a widget that can be scrolled. fn scrollable( &mut self, - _state: &mut dyn Scrollable, _id: Option<&Id>, _bounds: Rectangle, _content_bounds: Rectangle, _translation: Vector, + _state: &mut dyn Scrollable, ) { } /// Operates on a widget that has text input. - fn text_input(&mut self, _state: &mut dyn TextInput, _id: Option<&Id>) {} + fn text_input( + &mut self, + _id: Option<&Id>, + _bounds: Rectangle, + _state: &mut dyn TextInput, + ) { + } + + /// Operates on a widget that contains some text. + fn text(&mut self, _id: Option<&Id>, _bounds: Rectangle, _text: &str) {} /// Operates on a custom widget with some state. - fn custom(&mut self, _state: &mut dyn Any, _id: Option<&Id>) {} + fn custom( + &mut self, + _id: Option<&Id>, + _bounds: Rectangle, + _state: &mut dyn Any, + ) { + } /// Finishes the [`Operation`] and returns its [`Outcome`]. fn finish(&self) -> Outcome<T> { @@ -68,33 +89,52 @@ where self.as_mut().container(id, bounds, operate_on_children); } - fn focusable(&mut self, state: &mut dyn Focusable, id: Option<&Id>) { - self.as_mut().focusable(state, id); + fn focusable( + &mut self, + id: Option<&Id>, + bounds: Rectangle, + state: &mut dyn Focusable, + ) { + self.as_mut().focusable(id, bounds, state); } fn scrollable( &mut self, - state: &mut dyn Scrollable, id: Option<&Id>, bounds: Rectangle, content_bounds: Rectangle, translation: Vector, + state: &mut dyn Scrollable, ) { self.as_mut().scrollable( - state, id, bounds, content_bounds, translation, + state, ); } - fn text_input(&mut self, state: &mut dyn TextInput, id: Option<&Id>) { - self.as_mut().text_input(state, id); + fn text_input( + &mut self, + id: Option<&Id>, + bounds: Rectangle, + state: &mut dyn TextInput, + ) { + self.as_mut().text_input(id, bounds, state); + } + + fn text(&mut self, id: Option<&Id>, bounds: Rectangle, text: &str) { + self.as_mut().text(id, bounds, text); } - fn custom(&mut self, state: &mut dyn Any, id: Option<&Id>) { - self.as_mut().custom(state, id); + fn custom( + &mut self, + id: Option<&Id>, + bounds: Rectangle, + state: &mut dyn Any, + ) { + self.as_mut().custom(id, bounds, state); } fn finish(&self) -> Outcome<O> { @@ -150,33 +190,52 @@ where }); } - fn focusable(&mut self, state: &mut dyn Focusable, id: Option<&Id>) { - self.operation.focusable(state, id); + fn focusable( + &mut self, + id: Option<&Id>, + bounds: Rectangle, + state: &mut dyn Focusable, + ) { + self.operation.focusable(id, bounds, state); } fn scrollable( &mut self, - state: &mut dyn Scrollable, id: Option<&Id>, bounds: Rectangle, content_bounds: Rectangle, translation: Vector, + state: &mut dyn Scrollable, ) { self.operation.scrollable( - state, id, bounds, content_bounds, translation, + state, ); } - fn text_input(&mut self, state: &mut dyn TextInput, id: Option<&Id>) { - self.operation.text_input(state, id); + fn text_input( + &mut self, + id: Option<&Id>, + bounds: Rectangle, + state: &mut dyn TextInput, + ) { + self.operation.text_input(id, bounds, state); } - fn custom(&mut self, state: &mut dyn Any, id: Option<&Id>) { - self.operation.custom(state, id); + fn text(&mut self, id: Option<&Id>, bounds: Rectangle, text: &str) { + self.operation.text(id, bounds, text); + } + + fn custom( + &mut self, + id: Option<&Id>, + bounds: Rectangle, + state: &mut dyn Any, + ) { + self.operation.custom(id, bounds, state); } fn finish(&self) -> Outcome<O> { @@ -234,39 +293,55 @@ where fn scrollable( &mut self, - state: &mut dyn Scrollable, id: Option<&Id>, bounds: Rectangle, content_bounds: Rectangle, translation: Vector, + state: &mut dyn Scrollable, ) { self.operation.scrollable( - state, id, bounds, content_bounds, translation, + state, ); } fn focusable( &mut self, - state: &mut dyn Focusable, id: Option<&Id>, + bounds: Rectangle, + state: &mut dyn Focusable, ) { - self.operation.focusable(state, id); + self.operation.focusable(id, bounds, state); } fn text_input( &mut self, + id: Option<&Id>, + bounds: Rectangle, state: &mut dyn TextInput, + ) { + self.operation.text_input(id, bounds, state); + } + + fn text( + &mut self, id: Option<&Id>, + bounds: Rectangle, + text: &str, ) { - self.operation.text_input(state, id); + self.operation.text(id, bounds, text); } - fn custom(&mut self, state: &mut dyn Any, id: Option<&Id>) { - self.operation.custom(state, id); + fn custom( + &mut self, + id: Option<&Id>, + bounds: Rectangle, + state: &mut dyn Any, + ) { + self.operation.custom(id, bounds, state); } } @@ -275,33 +350,52 @@ where MapRef { operation }.container(id, bounds, operate_on_children); } - fn focusable(&mut self, state: &mut dyn Focusable, id: Option<&Id>) { - self.operation.focusable(state, id); + fn focusable( + &mut self, + id: Option<&Id>, + bounds: Rectangle, + state: &mut dyn Focusable, + ) { + self.operation.focusable(id, bounds, state); } fn scrollable( &mut self, - state: &mut dyn Scrollable, id: Option<&Id>, bounds: Rectangle, content_bounds: Rectangle, translation: Vector, + state: &mut dyn Scrollable, ) { self.operation.scrollable( - state, id, bounds, content_bounds, translation, + state, ); } - fn text_input(&mut self, state: &mut dyn TextInput, id: Option<&Id>) { - self.operation.text_input(state, id); + fn text_input( + &mut self, + id: Option<&Id>, + bounds: Rectangle, + state: &mut dyn TextInput, + ) { + self.operation.text_input(id, bounds, state); + } + + fn text(&mut self, id: Option<&Id>, bounds: Rectangle, text: &str) { + self.operation.text(id, bounds, text); } - fn custom(&mut self, state: &mut dyn Any, id: Option<&Id>) { - self.operation.custom(state, id); + fn custom( + &mut self, + id: Option<&Id>, + bounds: Rectangle, + state: &mut dyn Any, + ) { + self.operation.custom(id, bounds, state); } fn finish(&self) -> Outcome<B> { @@ -361,33 +455,52 @@ where }); } - fn focusable(&mut self, state: &mut dyn Focusable, id: Option<&Id>) { - self.operation.focusable(state, id); + fn focusable( + &mut self, + id: Option<&Id>, + bounds: Rectangle, + state: &mut dyn Focusable, + ) { + self.operation.focusable(id, bounds, state); } fn scrollable( &mut self, - state: &mut dyn Scrollable, id: Option<&Id>, bounds: Rectangle, content_bounds: Rectangle, translation: crate::Vector, + state: &mut dyn Scrollable, ) { self.operation.scrollable( - state, id, bounds, content_bounds, translation, + state, ); } - fn text_input(&mut self, state: &mut dyn TextInput, id: Option<&Id>) { - self.operation.text_input(state, id); + fn text_input( + &mut self, + id: Option<&Id>, + bounds: Rectangle, + state: &mut dyn TextInput, + ) { + self.operation.text_input(id, bounds, state); } - fn custom(&mut self, state: &mut dyn std::any::Any, id: Option<&Id>) { - self.operation.custom(state, id); + fn text(&mut self, id: Option<&Id>, bounds: Rectangle, text: &str) { + self.operation.text(id, bounds, text); + } + + fn custom( + &mut self, + id: Option<&Id>, + bounds: Rectangle, + state: &mut dyn Any, + ) { + self.operation.custom(id, bounds, state); } fn finish(&self) -> Outcome<B> { diff --git a/core/src/widget/operation/focusable.rs b/core/src/widget/operation/focusable.rs index 867c682e..8f66e575 100644 --- a/core/src/widget/operation/focusable.rs +++ b/core/src/widget/operation/focusable.rs @@ -32,7 +32,12 @@ pub fn focus<T>(target: Id) -> impl Operation<T> { } impl<T> Operation<T> for Focus { - fn focusable(&mut self, state: &mut dyn Focusable, id: Option<&Id>) { + fn focusable( + &mut self, + id: Option<&Id>, + _bounds: Rectangle, + state: &mut dyn Focusable, + ) { match id { Some(id) if id == &self.target => { state.focus(); @@ -64,7 +69,12 @@ pub fn count() -> impl Operation<Count> { } impl Operation<Count> for CountFocusable { - fn focusable(&mut self, state: &mut dyn Focusable, _id: Option<&Id>) { + fn focusable( + &mut self, + _id: Option<&Id>, + _bounds: Rectangle, + state: &mut dyn Focusable, + ) { if state.is_focused() { self.count.focused = Some(self.count.total); } @@ -104,7 +114,12 @@ where } impl<T> Operation<T> for FocusPrevious { - fn focusable(&mut self, state: &mut dyn Focusable, _id: Option<&Id>) { + fn focusable( + &mut self, + _id: Option<&Id>, + _bounds: Rectangle, + state: &mut dyn Focusable, + ) { if self.count.total == 0 { return; } @@ -147,7 +162,12 @@ where } impl<T> Operation<T> for FocusNext { - fn focusable(&mut self, state: &mut dyn Focusable, _id: Option<&Id>) { + fn focusable( + &mut self, + _id: Option<&Id>, + _bounds: Rectangle, + state: &mut dyn Focusable, + ) { match self.count.focused { None if self.current == 0 => state.focus(), Some(focused) if focused == self.current => state.unfocus(), @@ -179,7 +199,12 @@ pub fn find_focused() -> impl Operation<Id> { } impl Operation<Id> for FindFocused { - fn focusable(&mut self, state: &mut dyn Focusable, id: Option<&Id>) { + fn focusable( + &mut self, + id: Option<&Id>, + _bounds: Rectangle, + state: &mut dyn Focusable, + ) { if state.is_focused() && id.is_some() { self.focused = id.cloned(); } diff --git a/core/src/widget/operation/scrollable.rs b/core/src/widget/operation/scrollable.rs index c2fecf56..7c78c087 100644 --- a/core/src/widget/operation/scrollable.rs +++ b/core/src/widget/operation/scrollable.rs @@ -39,11 +39,11 @@ pub fn snap_to<T>(target: Id, offset: RelativeOffset) -> impl Operation<T> { fn scrollable( &mut self, - state: &mut dyn Scrollable, id: Option<&Id>, _bounds: Rectangle, _content_bounds: Rectangle, _translation: Vector, + state: &mut dyn Scrollable, ) { if Some(&self.target) == id { state.snap_to(self.offset); @@ -74,11 +74,11 @@ pub fn scroll_to<T>(target: Id, offset: AbsoluteOffset) -> impl Operation<T> { fn scrollable( &mut self, - state: &mut dyn Scrollable, id: Option<&Id>, _bounds: Rectangle, _content_bounds: Rectangle, _translation: Vector, + state: &mut dyn Scrollable, ) { if Some(&self.target) == id { state.scroll_to(self.offset); @@ -109,11 +109,11 @@ pub fn scroll_by<T>(target: Id, offset: AbsoluteOffset) -> impl Operation<T> { fn scrollable( &mut self, - state: &mut dyn Scrollable, id: Option<&Id>, bounds: Rectangle, content_bounds: Rectangle, _translation: Vector, + state: &mut dyn Scrollable, ) { if Some(&self.target) == id { state.scroll_by(self.offset, bounds, content_bounds); diff --git a/core/src/widget/operation/text_input.rs b/core/src/widget/operation/text_input.rs index 41731d4c..a46f1a2d 100644 --- a/core/src/widget/operation/text_input.rs +++ b/core/src/widget/operation/text_input.rs @@ -23,7 +23,12 @@ pub fn move_cursor_to_front<T>(target: Id) -> impl Operation<T> { } impl<T> Operation<T> for MoveCursor { - fn text_input(&mut self, state: &mut dyn TextInput, id: Option<&Id>) { + fn text_input( + &mut self, + id: Option<&Id>, + _bounds: Rectangle, + state: &mut dyn TextInput, + ) { match id { Some(id) if id == &self.target => { state.move_cursor_to_front(); @@ -53,7 +58,12 @@ pub fn move_cursor_to_end<T>(target: Id) -> impl Operation<T> { } impl<T> Operation<T> for MoveCursor { - fn text_input(&mut self, state: &mut dyn TextInput, id: Option<&Id>) { + fn text_input( + &mut self, + id: Option<&Id>, + _bounds: Rectangle, + state: &mut dyn TextInput, + ) { match id { Some(id) if id == &self.target => { state.move_cursor_to_end(); @@ -84,7 +94,12 @@ pub fn move_cursor_to<T>(target: Id, position: usize) -> impl Operation<T> { } impl<T> Operation<T> for MoveCursor { - fn text_input(&mut self, state: &mut dyn TextInput, id: Option<&Id>) { + fn text_input( + &mut self, + id: Option<&Id>, + _bounds: Rectangle, + state: &mut dyn TextInput, + ) { match id { Some(id) if id == &self.target => { state.move_cursor_to(self.position); @@ -113,7 +128,12 @@ pub fn select_all<T>(target: Id) -> impl Operation<T> { } impl<T> Operation<T> for MoveCursor { - fn text_input(&mut self, state: &mut dyn TextInput, id: Option<&Id>) { + fn text_input( + &mut self, + id: Option<&Id>, + _bounds: Rectangle, + state: &mut dyn TextInput, + ) { match id { Some(id) if id == &self.target => { state.select_all(); diff --git a/core/src/widget/text.rs b/core/src/widget/text.rs index d3d1cffd..5dd7892f 100644 --- a/core/src/widget/text.rs +++ b/core/src/widget/text.rs @@ -267,6 +267,18 @@ where draw(renderer, defaults, layout, state.0.raw(), style, viewport); } + + fn operate( + &self, + _state: &mut Tree, + layout: Layout<'_>, + _renderer: &Renderer, + operation: &mut dyn super::Operation, + ) { + dbg!(&self.fragment); + + operation.text(None, layout.bounds(), &self.fragment); + } } /// Produces the [`layout::Node`] of a [`Text`] widget. |