add universal _long_release and variants

This commit is contained in:
galister
2025-12-22 17:47:46 +09:00
parent a0bc4001c0
commit b90b7336e0
13 changed files with 223 additions and 104 deletions

View File

@@ -23,7 +23,11 @@ use crate::{
},
};
use glam::{Mat4, Vec3};
use std::{cell::RefCell, rc::Rc};
use std::{
cell::RefCell,
rc::Rc,
time::{Duration, Instant},
};
use taffy::{prelude::length, AlignItems, JustifyContent};
pub struct Params<'a> {
@@ -42,6 +46,7 @@ pub struct Params<'a> {
/// until "un-clicked". this is visual only.
/// set the initial state using `set_sticky_state`
pub sticky: bool,
pub long_press_time: f32,
}
impl Default for Params<'_> {
@@ -59,6 +64,7 @@ impl Default for Params<'_> {
text_style: TextStyle::default(),
tooltip: None,
sticky: false,
long_press_time: 0.0,
}
}
}
@@ -80,6 +86,7 @@ struct State {
on_click: Option<ButtonClickCallback>,
active_tooltip: Option<Rc<ComponentTooltip>>,
colors: Colors,
last_pressed: Instant,
}
struct Data {
@@ -152,6 +159,10 @@ impl ComponentButton {
rect.params.color2 = get_color2(&color);
}
pub fn get_time_since_last_pressed(&self) -> Duration {
self.state.borrow().last_pressed.elapsed()
}
pub fn on_click(&self, func: ButtonClickCallback) {
self.state.borrow_mut().on_click = Some(func);
}
@@ -329,6 +340,7 @@ fn register_event_mouse_press(state: Rc<RefCell<State>>, listeners: &mut EventLi
if state.hovered {
state.down = true;
state.last_pressed = Instant::now();
Ok(EventResult::Consumed)
} else {
Ok(EventResult::Pass)
@@ -357,7 +369,6 @@ fn register_event_mouse_release(
if state.down {
state.down = false;
if state.hovered
&& let Some(on_click) = &state.on_click
{
@@ -507,6 +518,7 @@ pub fn construct(ess: &mut ConstructEssentials, params: Params) -> anyhow::Resul
on_click: None,
active_tooltip: None,
sticky_down: false,
last_pressed: Instant::now(),
colors: Colors {
color,
border_color,

View File

@@ -5,7 +5,7 @@ use crate::{
i18n::Translation,
layout::WidgetID,
parser::{
parse_check_f32, parse_check_i32, parse_children, print_invalid_attrib, process_component,
parse_check_f32, parse_check_i32, parse_children, parse_f32, print_invalid_attrib, process_component,
style::{parse_color_opt, parse_round, parse_style, parse_text_style},
AttribPair, ParserContext, ParserFile,
},
@@ -28,6 +28,7 @@ pub fn parse_component_button<'a>(
let mut tooltip: Option<String> = None;
let mut tooltip_side: Option<tooltip::TooltipSide> = None;
let mut sticky: bool = false;
let mut long_press_time = 0.0;
let mut sprite_src: Option<AssetPath> = None;
let mut translation: Option<Translation> = None;
@@ -92,6 +93,9 @@ pub fn parse_component_button<'a>(
let mut sticky_i32 = 0;
sticky = parse_check_i32(value, &mut sticky_i32) && sticky_i32 == 1;
}
"long_press_time" => {
long_press_time = parse_f32(value).unwrap_or(long_press_time);
}
_ => {}
}
}
@@ -113,6 +117,7 @@ pub fn parse_component_button<'a>(
text: Translation::from_translation_key(&t),
}),
sticky,
long_press_time,
sprite_src,
},
)?;

View File

@@ -205,7 +205,11 @@ impl EventResult {
#[must_use]
pub fn merge(self, other: Self) -> Self {
if self > other { self } else { other }
if self > other {
self
} else {
other
}
}
}