From 283d7fd64e4029e2b2358b512b010b986b789563 Mon Sep 17 00:00:00 2001 From: Aleksander Date: Wed, 2 Jul 2025 22:06:19 +0200 Subject: [PATCH] separate CallbackData and CallbackDataCommon, refactoring --- wgui/src/animation.rs | 41 ++-- wgui/src/components/button.rs | 39 ++-- wgui/src/components/slider.rs | 176 +++++++++++++----- wgui/src/event.rs | 55 +++--- wgui/src/widget/mod.rs | 22 ++- .../src/overlays/keyboard/builder.rs | 46 +++-- wlx-overlay-s/src/overlays/watch.rs | 2 +- 7 files changed, 233 insertions(+), 148 deletions(-) diff --git a/wgui/src/animation.rs b/wgui/src/animation.rs index 683c65d..03660d5 100644 --- a/wgui/src/animation.rs +++ b/wgui/src/animation.rs @@ -1,7 +1,7 @@ use glam::{FloatExt, Vec2}; use crate::{ - event::WidgetCallback, + event::CallbackDataCommon, layout::{WidgetID, WidgetMap, WidgetNodeMap}, widget::{WidgetData, WidgetObj}, }; @@ -37,28 +37,12 @@ impl AnimationEasing { pub struct CallbackData<'a> { pub obj: &'a mut dyn WidgetObj, pub data: &'a mut WidgetData, - pub widgets: &'a WidgetMap, pub widget_id: WidgetID, pub widget_size: Vec2, pub pos: f32, // 0.0 (start of animation) - 1.0 (end of animation) - pub needs_redraw: bool, - pub dirty_nodes: &'a mut Vec, -} - -impl<'a> WidgetCallback<'a> for CallbackData<'a> { - fn get_widgets(&self) -> &'a WidgetMap { - self.widgets - } - - fn mark_redraw(&mut self) { - self.needs_redraw = true; - } - - fn mark_dirty(&mut self, node_id: taffy::NodeId) { - self.dirty_nodes.push(node_id); - } } +pub type AnimationCallback = Box; pub struct Animation { target_widget: WidgetID, @@ -72,7 +56,7 @@ pub struct Animation { pos_prev: f32, last_tick: bool, - callback: Box, + callback: AnimationCallback, } #[derive(Default)] @@ -85,7 +69,7 @@ impl Animation { target_widget: WidgetID, ticks: u32, easing: AnimationEasing, - callback: Box, + callback: AnimationCallback, ) -> Self { Animation::new_ex(target_widget, 0, ticks, easing, callback) } @@ -95,7 +79,7 @@ impl Animation { animation_id: u32, ticks: u32, easing: AnimationEasing, - callback: Box, + callback: AnimationCallback, ) -> Self { Self { target_widget, @@ -133,18 +117,23 @@ impl Animation { let data = &mut CallbackData { widget_id: self.target_widget, - dirty_nodes, - widgets: widget_map, widget_size: Vec2::new(layout.size.width, layout.size.height), obj, data, pos, - needs_redraw: false, }; - (self.callback)(data); + let common = &mut CallbackDataCommon { + dirty_nodes, + needs_redraw: false, + trigger_haptics: false, + widgets: widget_map, + taffy_layout: layout, + }; - if data.needs_redraw { + (self.callback)(common, data); + + if common.needs_redraw { res.needs_redraw = true; } diff --git a/wgui/src/components/button.rs b/wgui/src/components/button.rs index fbc82a6..30c5cc2 100644 --- a/wgui/src/components/button.rs +++ b/wgui/src/components/button.rs @@ -1,11 +1,11 @@ -use std::sync::Arc; +use std::rc::Rc; use taffy::{AlignItems, JustifyContent, prelude::length}; use crate::{ animation::{Animation, AnimationEasing}, components::Component, drawing::{self, Color}, - event::{EventListenerCollection, EventListenerKind, WidgetCallback}, + event::{CallbackDataCommon, EventListenerCollection, EventListenerKind}, layout::{Layout, WidgetID}, renderer_vk::text::{FontWeight, TextStyle}, widget::{ @@ -48,10 +48,7 @@ pub struct Button { impl Component for Button {} impl Button { - pub fn set_text<'a, C>(&self, callback_data: &mut C, text: &str) - where - C: WidgetCallback<'a>, - { + pub fn set_text<'a, 'b, C>(&self, callback_data: &mut CallbackDataCommon, text: &str) { callback_data.call_on_widget(self.text_id, |label: &mut TextLabel| { label.set_text(text); }); @@ -72,28 +69,28 @@ fn anim_hover(rect: &mut Rectangle, button: &Button, pos: f32) { rect.params.border = 3.0; } -fn anim_hover_in(button: Arc