wgui: Refactoring, various changes (see desc)

- use parking_lot for mutex (less restrictive and easier to use)
- simplify event callbacks and widget type casting
- defer component initialization at start (required for setting the initial state of sliders)
- fix non-working scroll events
- update testbed.xml
- replace slider with the real one in bar.xml
- show slider text on its handle
This commit is contained in:
Aleksander
2025-07-29 22:12:01 +02:00
parent f950273a2d
commit 4e46c45bcf
21 changed files with 450 additions and 334 deletions

View File

@@ -8,8 +8,8 @@
</template>
<elements>
<div box_sizing="content_box" flex_direction="column" justify_content="center" align_content="center">
<rectangle padding="10" gap="8" round="100%" color="~bg_color" justify_content="center" align_content="center">
<div box_sizing="content_box" flex_direction="column" justify_content="center">
<rectangle padding="10" gap="8" round="100%" color="~bg_color" justify_content="center">
<TopButton id="lock" src="bar/lock_open.svg" />
<TopButton id="anchor" src="bar/anchor.svg" />
<TopButton id="mouse" src="bar/mouse.svg" />
@@ -19,17 +19,12 @@
<TopButton id="inout" src="bar/inout.svg" />
<TopButton id="delete" src="bar/delete.svg" />
</rectangle>
<rectangle padding="8" gap="8" round="100%" color="~bg_color_active" justify_content="center" align_content="center">
<rectangle padding="8" gap="8" round="100%" color="~bg_color_active" justify_content="center" align_items="center">
<label size="18" text="Opacity" color="~text_color" />
<label size="18" text="100%" color="~text_color" weight="bold" />
<rectangle width="200" height="20" round="100%" color="~slider_bg_color">
<div width="150" />
<rectangle width="50" round="100%" color="~slider_fg_color" justify_content="center" align_content="center">
</rectangle>
</rectangle>
<slider width="150" height="24" min_value="0" max_value="100" value="100" />
<label size="18" text="Additive:" color="~text_color" />
<sprite color="~device_color" width="20" height="20" src="bar/checkbox-checked.svg" />
</rectangle>
</div>
</elements>
</layout>
</layout>

View File

@@ -5,8 +5,8 @@ use vulkano::{command_buffer::CommandBufferUsage, image::view::ImageView};
use wgui::{
event::{
Event as WguiEvent, EventListenerCollection, InternalStateChangeEvent, ListenerHandleVec,
MouseButton, MouseButtonIndex, MouseDownEvent, MouseLeaveEvent, MouseMotionEvent,
MouseUpEvent, MouseWheelEvent,
MouseButtonIndex, MouseDownEvent, MouseLeaveEvent, MouseMotionEvent, MouseUpEvent,
MouseWheelEvent,
},
layout::Layout,
parser::ParserState,

View File

@@ -167,16 +167,12 @@ where
if let Some(widget_id) = gui_state_key.ids.get(&*my_id) {
let key_state = {
let widget = panel
let rect = panel
.layout
.widget_map
.get(*widget_id)
.unwrap() // want panic
.lock()
.get_as::<Rectangle>(*widget_id)
.unwrap(); // want panic
let rect = widget.obj.get_as::<Rectangle>();
Rc::new(KeyState {
button_state: key.button_state,
color: rect.params.color,
@@ -193,7 +189,7 @@ where
Box::new({
let k = key_state.clone();
move |common, data, _app, _state| {
common.trigger_haptics();
common.alterables.trigger_haptics();
on_enter_anim(k.clone(), common, data);
}
}),
@@ -205,7 +201,7 @@ where
Box::new({
let k = key_state.clone();
move |common, data, _app, _state| {
common.trigger_haptics();
common.alterables.trigger_haptics();
on_leave_anim(k.clone(), common, data);
}
}),
@@ -310,7 +306,7 @@ fn on_enter_anim(
common: &mut event::CallbackDataCommon,
data: &event::CallbackData,
) {
common.animate(Animation::new(
common.alterables.animate(Animation::new(
data.widget_id,
10,
AnimationEasing::OutBack,
@@ -318,7 +314,7 @@ fn on_enter_anim(
let rect = data.obj.get_as_mut::<Rectangle>();
set_anim_color(&key_state, rect, data.pos);
data.data.transform = get_anim_transform(data.pos, data.widget_size);
common.mark_redraw();
common.alterables.mark_redraw();
}),
));
}
@@ -328,7 +324,7 @@ fn on_leave_anim(
common: &mut event::CallbackDataCommon,
data: &event::CallbackData,
) {
common.animate(Animation::new(
common.alterables.animate(Animation::new(
data.widget_id,
15,
AnimationEasing::OutQuad,
@@ -336,7 +332,7 @@ fn on_leave_anim(
let rect = data.obj.get_as_mut::<Rectangle>();
set_anim_color(&key_state, rect, 1.0 - data.pos);
data.data.transform = get_anim_transform(1.0 - data.pos, data.widget_size);
common.mark_redraw();
common.alterables.mark_redraw();
}),
));
}
@@ -351,7 +347,7 @@ fn on_press_anim(
}
let rect = data.obj.get_as_mut::<Rectangle>();
rect.params.border_color = Color::new(1.0, 1.0, 1.0, 1.0);
common.mark_redraw();
common.alterables.mark_redraw();
key_state.drawn_state.set(true);
}
@@ -365,6 +361,6 @@ fn on_release_anim(
}
let rect = data.obj.get_as_mut::<Rectangle>();
rect.params.border_color = key_state.border_color;
common.mark_redraw();
common.alterables.mark_redraw();
key_state.drawn_state.set(false);
}

View File

@@ -41,15 +41,11 @@ where
.flatten();
let role = cap.get(2).unwrap().as_str();
let mut widget = panel
let mut label = panel
.layout
.widget_map
.get_mut(*widget_id)
.unwrap() // want panic
.lock()
.unwrap(); // want panic
let label = widget.obj.get_as_mut::<TextLabel>();
.get_as::<TextLabel>(*widget_id)
.unwrap();
let format = match role {
"tz" => {