wgui: component callbacks refactoring
This commit is contained in:
@@ -5,9 +5,9 @@ use crate::{
|
||||
animation::{Animation, AnimationEasing},
|
||||
components::{Component, ComponentBase, ComponentTrait, InitData},
|
||||
drawing::{self, Color},
|
||||
event::{CallbackDataCommon, EventAlterables, EventListenerCollection, EventListenerKind, ListenerHandleVec},
|
||||
event::{CallbackDataCommon, EventListenerCollection, EventListenerKind, ListenerHandleVec},
|
||||
i18n::Translation,
|
||||
layout::{Layout, LayoutState, WidgetID},
|
||||
layout::{Layout, WidgetID},
|
||||
renderer_vk::text::{FontWeight, TextStyle},
|
||||
widget::{
|
||||
label::{WidgetLabel, WidgetLabelParams},
|
||||
@@ -42,21 +42,8 @@ impl Default for Params {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ButtonClickEvent<'a> {
|
||||
pub state: &'a LayoutState,
|
||||
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<()>>;
|
||||
pub struct ButtonClickEvent {}
|
||||
pub type ButtonClickCallback = Box<dyn Fn(&mut CallbackDataCommon, ButtonClickEvent) -> anyhow::Result<()>>;
|
||||
|
||||
struct State {
|
||||
hovered: bool,
|
||||
@@ -231,10 +218,7 @@ fn register_event_mouse_release<U1, U2>(
|
||||
|
||||
if state.hovered {
|
||||
if let Some(on_click) = &state.on_click {
|
||||
on_click(ButtonClickEvent {
|
||||
state: common.state,
|
||||
alterables: common.alterables,
|
||||
})?;
|
||||
on_click(common, ButtonClickEvent {})?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,22 +37,11 @@ impl Default for Params {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct CheckboxToggleEvent<'a> {
|
||||
pub state: &'a LayoutState,
|
||||
pub alterables: &'a mut EventAlterables,
|
||||
pub struct CheckboxToggleEvent {
|
||||
pub checked: bool,
|
||||
}
|
||||
|
||||
impl CheckboxToggleEvent<'_> {
|
||||
pub const fn as_common(&mut self) -> CallbackDataCommon {
|
||||
CallbackDataCommon {
|
||||
alterables: self.alterables,
|
||||
state: self.state,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub type CheckboxToggleCallback = Box<dyn Fn(CheckboxToggleEvent) -> anyhow::Result<()>>;
|
||||
pub type CheckboxToggleCallback = Box<dyn Fn(&mut CallbackDataCommon, CheckboxToggleEvent) -> anyhow::Result<()>>;
|
||||
|
||||
struct State {
|
||||
checked: bool,
|
||||
@@ -242,11 +231,7 @@ fn register_event_mouse_release<U1, U2>(
|
||||
|
||||
if state.hovered {
|
||||
if let Some(on_toggle) = &state.on_toggle {
|
||||
on_toggle(CheckboxToggleEvent {
|
||||
state: common.state,
|
||||
alterables: common.alterables,
|
||||
checked: state.checked,
|
||||
})?;
|
||||
on_toggle(common, CheckboxToggleEvent { checked: state.checked })?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,7 @@ use std::rc::Rc;
|
||||
|
||||
use crate::{
|
||||
any::AnyTrait,
|
||||
event::{self, CallbackDataCommon, EventAlterables},
|
||||
layout::LayoutState,
|
||||
event::{self, CallbackDataCommon},
|
||||
};
|
||||
|
||||
pub mod button;
|
||||
@@ -11,17 +10,7 @@ pub mod checkbox;
|
||||
pub mod slider;
|
||||
|
||||
pub struct InitData<'a> {
|
||||
pub state: &'a LayoutState,
|
||||
pub alterables: &'a mut EventAlterables,
|
||||
}
|
||||
|
||||
impl InitData<'_> {
|
||||
const fn as_common(&mut self) -> CallbackDataCommon {
|
||||
CallbackDataCommon {
|
||||
alterables: self.alterables,
|
||||
state: self.state,
|
||||
}
|
||||
}
|
||||
pub common: &'a mut CallbackDataCommon<'a>,
|
||||
}
|
||||
|
||||
// common component data
|
||||
|
||||
@@ -69,7 +69,7 @@ impl ComponentTrait for ComponentSlider {
|
||||
fn init(&self, init_data: &mut InitData) {
|
||||
let mut state = self.state.borrow_mut();
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user