Draft iced_test crate and test todos example

This commit is contained in:
Héctor Ramón Jiménez 2024-12-03 22:03:06 +01:00
parent d6182299b9
commit d09d5d45ae
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
18 changed files with 640 additions and 63 deletions

View file

@ -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`].

View file

@ -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 custom(&mut self, state: &mut dyn Any, id: Option<&Id>) {
self.as_mut().custom(state, id);
fn text(&mut self, id: Option<&Id>, bounds: Rectangle, text: &str) {
self.as_mut().text(id, bounds, text);
}
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,
state: &mut dyn TextInput,
id: Option<&Id>,
bounds: Rectangle,
state: &mut dyn TextInput,
) {
self.operation.text_input(state, id);
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);
}
}
@ -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 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<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> {

View file

@ -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();
}

View file

@ -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);

View file

@ -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();

View file

@ -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.