wgui: component callbacks refactoring

This commit is contained in:
Aleksander
2025-09-16 20:35:28 +02:00
parent 0fdc0e3828
commit dfec935388
7 changed files with 24 additions and 64 deletions

View File

@@ -42,7 +42,7 @@ pub trait Tab {
impl TabType { impl TabType {
pub fn register_button(this_rc: RcFrontend, btn: &Rc<ComponentButton>, tab: TabType) { pub fn register_button(this_rc: RcFrontend, btn: &Rc<ComponentButton>, tab: TabType) {
btn.on_click({ btn.on_click({
Box::new(move |_evt| { Box::new(move |_common, _evt| {
this_rc.borrow_mut().push_task(FrontendTask::SetTab(tab)); this_rc.borrow_mut().push_task(FrontendTask::SetTab(tab));
Ok(()) Ok(())
}) })

View File

@@ -32,14 +32,14 @@ fn button_click_callback(
label: Widget, label: Widget,
text: &'static str, text: &'static str,
) -> ButtonClickCallback { ) -> ButtonClickCallback {
Box::new(move |mut e| { Box::new(move |common, _e| {
label label
.get_as_mut::<WidgetLabel>() .get_as_mut::<WidgetLabel>()
.unwrap() .unwrap()
.set_text(&mut e.as_common(), Translation::from_raw_text(text)); .set_text(common, Translation::from_raw_text(text));
button.try_cast::<ComponentButton>()?.set_text( button.try_cast::<ComponentButton>()?.set_text(
&mut e.as_common(), common,
Translation::from_raw_text("this button has been clicked"), Translation::from_raw_text("this button has been clicked"),
); );
@@ -92,8 +92,8 @@ impl TestbedGeneric {
let button_click_me = state.fetch_component_as::<ComponentButton>("button_click_me")?; let button_click_me = state.fetch_component_as::<ComponentButton>("button_click_me")?;
let button = button_click_me.clone(); let button = button_click_me.clone();
button_click_me.on_click(Box::new(move |mut e| { button_click_me.on_click(Box::new(move |common, _e| {
button.set_text(&mut e.as_common(), Translation::from_raw_text("congrats!")); button.set_text(common, Translation::from_raw_text("congrats!"));
Ok(()) Ok(())
})); }));
@@ -111,10 +111,10 @@ impl TestbedGeneric {
let cb_first = state.fetch_component_as::<ComponentCheckbox>("cb_first")?; let cb_first = state.fetch_component_as::<ComponentCheckbox>("cb_first")?;
let label = label_cur_option.widget.clone(); let label = label_cur_option.widget.clone();
cb_first.on_toggle(Box::new(move |mut e| { cb_first.on_toggle(Box::new(move |common, e| {
let mut widget = label.get_as_mut::<WidgetLabel>().unwrap(); let mut widget = label.get_as_mut::<WidgetLabel>().unwrap();
let text = format!("checkbox toggle: {}", e.checked); let text = format!("checkbox toggle: {}", e.checked);
widget.set_text(&mut e.as_common(), Translation::from_raw_text(&text)); widget.set_text(common, Translation::from_raw_text(&text));
Ok(()) Ok(())
})); }));

View File

@@ -5,9 +5,9 @@ use crate::{
animation::{Animation, AnimationEasing}, animation::{Animation, AnimationEasing},
components::{Component, ComponentBase, ComponentTrait, InitData}, components::{Component, ComponentBase, ComponentTrait, InitData},
drawing::{self, Color}, drawing::{self, Color},
event::{CallbackDataCommon, EventAlterables, EventListenerCollection, EventListenerKind, ListenerHandleVec}, event::{CallbackDataCommon, EventListenerCollection, EventListenerKind, ListenerHandleVec},
i18n::Translation, i18n::Translation,
layout::{Layout, LayoutState, WidgetID}, layout::{Layout, WidgetID},
renderer_vk::text::{FontWeight, TextStyle}, renderer_vk::text::{FontWeight, TextStyle},
widget::{ widget::{
label::{WidgetLabel, WidgetLabelParams}, label::{WidgetLabel, WidgetLabelParams},
@@ -42,21 +42,8 @@ impl Default for Params {
} }
} }
pub struct ButtonClickEvent<'a> { pub struct ButtonClickEvent {}
pub state: &'a LayoutState, pub type ButtonClickCallback = Box<dyn Fn(&mut CallbackDataCommon, ButtonClickEvent) -> anyhow::Result<()>>;
pub alterables: &'a mut EventAlterables,
}
impl ButtonClickEvent<'_> {
pub const fn as_common(&mut self) -> CallbackDataCommon {
CallbackDataCommon {
alterables: self.alterables,
state: self.state,
}
}
}
pub type ButtonClickCallback = Box<dyn Fn(ButtonClickEvent) -> anyhow::Result<()>>;
struct State { struct State {
hovered: bool, hovered: bool,
@@ -231,10 +218,7 @@ fn register_event_mouse_release<U1, U2>(
if state.hovered { if state.hovered {
if let Some(on_click) = &state.on_click { if let Some(on_click) = &state.on_click {
on_click(ButtonClickEvent { on_click(common, ButtonClickEvent {})?;
state: common.state,
alterables: common.alterables,
})?;
} }
} }
} }

View File

@@ -37,22 +37,11 @@ impl Default for Params {
} }
} }
pub struct CheckboxToggleEvent<'a> { pub struct CheckboxToggleEvent {
pub state: &'a LayoutState,
pub alterables: &'a mut EventAlterables,
pub checked: bool, pub checked: bool,
} }
impl CheckboxToggleEvent<'_> { pub type CheckboxToggleCallback = Box<dyn Fn(&mut CallbackDataCommon, CheckboxToggleEvent) -> anyhow::Result<()>>;
pub const fn as_common(&mut self) -> CallbackDataCommon {
CallbackDataCommon {
alterables: self.alterables,
state: self.state,
}
}
}
pub type CheckboxToggleCallback = Box<dyn Fn(CheckboxToggleEvent) -> anyhow::Result<()>>;
struct State { struct State {
checked: bool, checked: bool,
@@ -242,11 +231,7 @@ fn register_event_mouse_release<U1, U2>(
if state.hovered { if state.hovered {
if let Some(on_toggle) = &state.on_toggle { if let Some(on_toggle) = &state.on_toggle {
on_toggle(CheckboxToggleEvent { on_toggle(common, CheckboxToggleEvent { checked: state.checked })?;
state: common.state,
alterables: common.alterables,
checked: state.checked,
})?;
} }
} }
} }

View File

@@ -2,8 +2,7 @@ use std::rc::Rc;
use crate::{ use crate::{
any::AnyTrait, any::AnyTrait,
event::{self, CallbackDataCommon, EventAlterables}, event::{self, CallbackDataCommon},
layout::LayoutState,
}; };
pub mod button; pub mod button;
@@ -11,17 +10,7 @@ pub mod checkbox;
pub mod slider; pub mod slider;
pub struct InitData<'a> { pub struct InitData<'a> {
pub state: &'a LayoutState, pub common: &'a mut CallbackDataCommon<'a>,
pub alterables: &'a mut EventAlterables,
}
impl InitData<'_> {
const fn as_common(&mut self) -> CallbackDataCommon {
CallbackDataCommon {
alterables: self.alterables,
state: self.state,
}
}
} }
// common component data // common component data

View File

@@ -69,7 +69,7 @@ impl ComponentTrait for ComponentSlider {
fn init(&self, init_data: &mut InitData) { fn init(&self, init_data: &mut InitData) {
let mut state = self.state.borrow_mut(); let mut state = self.state.borrow_mut();
let value = state.values.value; let value = state.values.value;
state.set_value(&mut init_data.as_common(), &self.data, value); state.set_value(init_data.common, &self.data, value);
} }
fn base(&mut self) -> &mut ComponentBase { fn base(&mut self) -> &mut ComponentBase {

View File

@@ -7,7 +7,7 @@ use std::{
use crate::{ use crate::{
animation::Animations, animation::Animations,
components::{Component, InitData}, components::{Component, InitData},
event::{self, EventAlterables, EventListenerCollection}, event::{self, CallbackDataCommon, EventAlterables, EventListenerCollection},
globals::WguiGlobals, globals::WguiGlobals,
transform_stack::Transform, transform_stack::Transform,
widget::{self, EventParams, WidgetObj, WidgetState, div::WidgetDiv}, widget::{self, EventParams, WidgetObj, WidgetState, div::WidgetDiv},
@@ -203,10 +203,12 @@ impl Layout {
let mut alterables = EventAlterables::default(); let mut alterables = EventAlterables::default();
while let Some(c) = self.components_to_init.pop_front() { while let Some(c) = self.components_to_init.pop_front() {
c.0.init(&mut InitData { let mut common = CallbackDataCommon {
state: &self.state, state: &self.state,
alterables: &mut alterables, alterables: &mut alterables,
}); };
c.0.init(&mut InitData { common: &mut common });
} }
self.process_alterables(alterables)?; self.process_alterables(alterables)?;