Merge remote-tracking branch 'origin/main' into next-dash-interface

[skip ci]
This commit is contained in:
Aleksander
2025-12-23 17:01:16 +01:00
parent 848674c143
commit 9a606dbce5
63 changed files with 2457 additions and 309 deletions

View File

@@ -22,7 +22,11 @@ use crate::{
},
};
use glam::{Mat4, Vec3};
use std::{cell::RefCell, rc::Rc};
use std::{
cell::RefCell,
rc::Rc,
time::{Duration, Instant},
};
use taffy::{AlignItems, JustifyContent, prelude::length};
pub struct Params<'a> {
@@ -41,6 +45,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<'_> {
@@ -58,6 +63,7 @@ impl Default for Params<'_> {
text_style: TextStyle::default(),
tooltip: None,
sticky: false,
long_press_time: 0.0,
}
}
}
@@ -79,6 +85,7 @@ struct State {
on_click: Option<ButtonClickCallback>,
active_tooltip: Option<Rc<ComponentTooltip>>,
colors: Colors,
last_pressed: Instant,
}
struct Data {
@@ -151,6 +158,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);
}
@@ -170,10 +181,13 @@ impl ComponentButton {
return;
}
let anim_mult = common.state.globals.defaults().animation_mult;
let anim_ticks = if sticky_down { 5. } else { 10. };
let state = self.state.clone();
let anim = Animation::new(
self.data.id_rect,
if sticky_down { 5 } else { 10 },
(anim_ticks * anim_mult) as _,
AnimationEasing::OutCubic,
Box::new(move |common, anim_data| {
let rect = anim_data.obj.get_as_mut::<WidgetRectangle>().unwrap();
@@ -227,10 +241,10 @@ fn anim_hover(
rect.params.border_color = init_border_color.lerp(&colors.hover_border_color, mult);
}
fn anim_hover_create(state: Rc<RefCell<State>>, widget_id: WidgetID, fade_in: bool) -> Animation {
fn anim_hover_create(state: Rc<RefCell<State>>, widget_id: WidgetID, fade_in: bool, anim_mult: f32) -> Animation {
Animation::new(
widget_id,
if fade_in { 5 } else { 10 },
((if fade_in { 5. } else { 10. }) * anim_mult) as _,
AnimationEasing::OutCubic,
Box::new(move |common, anim_data| {
let rect = anim_data.obj.get_as_mut::<WidgetRectangle>().unwrap();
@@ -254,6 +268,7 @@ fn register_event_mouse_enter(
state: Rc<RefCell<State>>,
listeners: &mut EventListenerCollection,
info: Option<components::tooltip::TooltipInfo>,
anim_mult: f32,
) -> EventListenerID {
listeners.register(
EventListenerKind::MouseEnter,
@@ -262,7 +277,7 @@ fn register_event_mouse_enter(
common.alterables.mark_redraw();
common
.alterables
.animate(anim_hover_create(state.clone(), event_data.widget_id, true));
.animate(anim_hover_create(state.clone(), event_data.widget_id, true, anim_mult));
if let Some(info) = info.clone() {
common.alterables.tasks.push(LayoutTask::ModifyLayoutState({
@@ -282,14 +297,18 @@ fn register_event_mouse_enter(
)
}
fn register_event_mouse_leave(state: Rc<RefCell<State>>, listeners: &mut EventListenerCollection) -> EventListenerID {
fn register_event_mouse_leave(
state: Rc<RefCell<State>>,
listeners: &mut EventListenerCollection,
anim_mult: f32,
) -> EventListenerID {
listeners.register(
EventListenerKind::MouseLeave,
Box::new(move |common, event_data, (), ()| {
common.alterables.trigger_haptics();
common
.alterables
.animate(anim_hover_create(state.clone(), event_data.widget_id, false));
.animate(anim_hover_create(state.clone(), event_data.widget_id, false, anim_mult));
let mut state = state.borrow_mut();
state.active_tooltip = None;
state.hovered = false;
@@ -320,7 +339,7 @@ fn register_event_mouse_press(state: Rc<RefCell<State>>, listeners: &mut EventLi
if state.hovered {
state.down = true;
state.active_tooltip = None;
state.last_pressed = Instant::now();
Ok(EventResult::Consumed)
} else {
Ok(EventResult::Pass)
@@ -349,7 +368,6 @@ fn register_event_mouse_release(
if state.down {
state.down = false;
if state.hovered
&& let Some(on_click) = &state.on_click
{
@@ -503,6 +521,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,
@@ -515,9 +534,16 @@ pub fn construct(ess: &mut ConstructEssentials, params: Params) -> anyhow::Resul
id: root.id,
lhandles: {
let mut widget = ess.layout.state.widgets.get(id_rect).unwrap().state();
let anim_mult = ess.layout.state.globals.defaults().animation_mult;
vec![
register_event_mouse_enter(data.clone(), state.clone(), &mut widget.event_listeners, params.tooltip),
register_event_mouse_leave(state.clone(), &mut widget.event_listeners),
register_event_mouse_enter(
data.clone(),
state.clone(),
&mut widget.event_listeners,
params.tooltip,
anim_mult,
),
register_event_mouse_leave(state.clone(), &mut widget.event_listeners, anim_mult),
register_event_mouse_press(state.clone(), &mut widget.event_listeners),
register_event_mouse_release(data.clone(), state.clone(), &mut widget.event_listeners),
]

View File

@@ -1,7 +1,7 @@
use std::{cell::RefCell, rc::Rc};
use taffy::{
AlignItems,
prelude::{length, percent},
AlignItems,
};
use crate::{
@@ -13,10 +13,10 @@ use crate::{
layout::{self, WidgetID, WidgetPair},
renderer_vk::text::{FontWeight, TextStyle},
widget::{
ConstructEssentials, EventResult,
label::{WidgetLabel, WidgetLabelParams},
rectangle::{WidgetRectangle, WidgetRectangleParams},
util::WLength,
ConstructEssentials, EventResult,
},
};
@@ -120,10 +120,10 @@ fn anim_hover(rect: &mut WidgetRectangle, pos: f32, pressed: bool) {
}
}
fn anim_hover_in(state: Rc<RefCell<State>>, widget_id: WidgetID) -> Animation {
fn anim_hover_in(state: Rc<RefCell<State>>, widget_id: WidgetID, anim_mult: f32) -> Animation {
Animation::new(
widget_id,
5,
(5. * anim_mult) as _,
AnimationEasing::OutQuad,
Box::new(move |common, anim_data| {
let rect = anim_data.obj.get_as_mut::<WidgetRectangle>().unwrap();
@@ -133,10 +133,10 @@ fn anim_hover_in(state: Rc<RefCell<State>>, widget_id: WidgetID) -> Animation {
)
}
fn anim_hover_out(state: Rc<RefCell<State>>, widget_id: WidgetID) -> Animation {
fn anim_hover_out(state: Rc<RefCell<State>>, widget_id: WidgetID, anim_mult: f32) -> Animation {
Animation::new(
widget_id,
8,
(8. * anim_mult) as _,
AnimationEasing::OutQuad,
Box::new(move |common, anim_data| {
let rect = anim_data.obj.get_as_mut::<WidgetRectangle>().unwrap();
@@ -146,28 +146,36 @@ fn anim_hover_out(state: Rc<RefCell<State>>, widget_id: WidgetID) -> Animation {
)
}
fn register_event_mouse_enter(state: Rc<RefCell<State>>, listeners: &mut EventListenerCollection) -> EventListenerID {
fn register_event_mouse_enter(
state: Rc<RefCell<State>>,
listeners: &mut EventListenerCollection,
anim_mult: f32,
) -> EventListenerID {
listeners.register(
EventListenerKind::MouseEnter,
Box::new(move |common, event_data, (), ()| {
common.alterables.trigger_haptics();
common
.alterables
.animate(anim_hover_in(state.clone(), event_data.widget_id));
.animate(anim_hover_in(state.clone(), event_data.widget_id, anim_mult));
state.borrow_mut().hovered = true;
Ok(EventResult::Pass)
}),
)
}
fn register_event_mouse_leave(state: Rc<RefCell<State>>, listeners: &mut EventListenerCollection) -> EventListenerID {
fn register_event_mouse_leave(
state: Rc<RefCell<State>>,
listeners: &mut EventListenerCollection,
anim_mult: f32,
) -> EventListenerID {
listeners.register(
EventListenerKind::MouseLeave,
Box::new(move |common, event_data, (), ()| {
common.alterables.trigger_haptics();
common
.alterables
.animate(anim_hover_out(state.clone(), event_data.widget_id));
.animate(anim_hover_out(state.clone(), event_data.widget_id, anim_mult));
state.borrow_mut().hovered = false;
Ok(EventResult::Pass)
}),
@@ -341,9 +349,10 @@ pub fn construct(ess: &mut ConstructEssentials, params: Params) -> anyhow::Resul
id: root.id,
lhandles: {
let mut widget = ess.layout.state.widgets.get(id_container).unwrap().state();
let anim_mult = ess.layout.state.globals.defaults().animation_mult;
vec![
register_event_mouse_enter(state.clone(), &mut widget.event_listeners),
register_event_mouse_leave(state.clone(), &mut widget.event_listeners),
register_event_mouse_enter(state.clone(), &mut widget.event_listeners, anim_mult),
register_event_mouse_leave(state.clone(), &mut widget.event_listeners, anim_mult),
register_event_mouse_press(state.clone(), &mut widget.event_listeners),
register_event_mouse_release(data.clone(), state.clone(), &mut widget.event_listeners),
]

View File

@@ -18,11 +18,11 @@ use crate::{
util,
},
widget::{
ConstructEssentials, EventResult,
div::WidgetDiv,
label::{WidgetLabel, WidgetLabelParams},
rectangle::{WidgetRectangle, WidgetRectangleParams},
util::WLength,
ConstructEssentials, EventResult,
},
};
@@ -266,10 +266,10 @@ fn anim_rect(rect: &mut WidgetRectangle, pos: f32) {
rect.params.border_color = drawing::Color::lerp(&HANDLE_BORDER_COLOR, &HANDLE_BORDER_COLOR_HOVERED, pos);
}
fn on_enter_anim(common: &mut event::CallbackDataCommon, handle_id: WidgetID) {
fn on_enter_anim(common: &mut event::CallbackDataCommon, handle_id: WidgetID, anim_mult: f32) {
common.alterables.animate(Animation::new(
handle_id,
20,
(20. * anim_mult) as _,
AnimationEasing::OutBack,
Box::new(move |common, data| {
let rect = data.obj.get_as_mut::<WidgetRectangle>().unwrap();
@@ -280,10 +280,10 @@ fn on_enter_anim(common: &mut event::CallbackDataCommon, handle_id: WidgetID) {
));
}
fn on_leave_anim(common: &mut event::CallbackDataCommon, handle_id: WidgetID) {
fn on_leave_anim(common: &mut event::CallbackDataCommon, handle_id: WidgetID, anim_mult: f32) {
common.alterables.animate(Animation::new(
handle_id,
10,
(10. * anim_mult) as _,
AnimationEasing::OutQuad,
Box::new(move |common, data| {
let rect = data.obj.get_as_mut::<WidgetRectangle>().unwrap();
@@ -298,13 +298,14 @@ fn register_event_mouse_enter(
data: Rc<Data>,
state: Rc<RefCell<State>>,
listeners: &mut EventListenerCollection,
anim_mult: f32,
) -> event::EventListenerID {
listeners.register(
EventListenerKind::MouseEnter,
Box::new(move |common, _data, (), ()| {
common.alterables.trigger_haptics();
state.borrow_mut().hovered = true;
on_enter_anim(common, data.slider_handle_rect_id);
on_enter_anim(common, data.slider_handle_rect_id, anim_mult);
Ok(EventResult::Pass)
}),
)
@@ -314,13 +315,14 @@ fn register_event_mouse_leave(
data: Rc<Data>,
state: Rc<RefCell<State>>,
listeners: &mut EventListenerCollection,
anim_mult: f32,
) -> event::EventListenerID {
listeners.register(
EventListenerKind::MouseLeave,
Box::new(move |common, _data, (), ()| {
common.alterables.trigger_haptics();
state.borrow_mut().hovered = false;
on_leave_anim(common, data.slider_handle_rect_id);
on_leave_anim(common, data.slider_handle_rect_id, anim_mult);
Ok(EventResult::Pass)
}),
)
@@ -508,12 +510,12 @@ pub fn construct(ess: &mut ConstructEssentials, params: Params) -> anyhow::Resul
id: root.id,
lhandles: {
let mut widget = ess.layout.state.widgets.get(body_id).unwrap().state();
let anim_mult = ess.layout.state.globals.defaults().animation_mult;
vec![
register_event_mouse_enter(data.clone(), state.clone(), &mut widget.event_listeners),
register_event_mouse_leave(data.clone(), state.clone(), &mut widget.event_listeners),
register_event_mouse_enter(data.clone(), state.clone(), &mut widget.event_listeners, anim_mult),
register_event_mouse_leave(data.clone(), state.clone(), &mut widget.event_listeners, anim_mult),
register_event_mouse_motion(data.clone(), state.clone(), &mut widget.event_listeners),
register_event_mouse_press(data.clone(), state.clone(), &mut widget.event_listeners),
register_event_mouse_leave(data.clone(), state.clone(), &mut widget.event_listeners),
register_event_mouse_release(state.clone(), &mut widget.event_listeners),
]
},

View File

@@ -22,7 +22,10 @@ pub struct Defaults {
pub accent_color: drawing::Color,
pub danger_color: drawing::Color,
pub faded_color: drawing::Color,
pub bg_color: drawing::Color,
pub translucent_alpha: f32,
pub animation_mult: f32,
pub rounding_mult: f32,
}
impl Default for Defaults {
@@ -34,7 +37,10 @@ impl Default for Defaults {
accent_color: drawing::Color::new(0.0, 0.54, 1.0, 1.0),
danger_color: drawing::Color::new(0.8, 0.0, 0.0, 1.0),
faded_color: drawing::Color::new(0.4, 0.5, 0.6, 1.0),
bg_color: drawing::Color::new(0.0039, 0.0078, 0.0235, 0.8352),
translucent_alpha: 0.25,
animation_mult: 1.0,
rounding_mult: 1.0,
}
}
}

View File

@@ -1,13 +1,13 @@
use crate::{
assets::AssetPath,
components::{Component, button, tooltip},
components::{button, tooltip, Component},
drawing::Color,
i18n::Translation,
layout::WidgetID,
parser::{
AttribPair, ParserContext, ParserFile, 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,
},
widget::util::WLength,
};
@@ -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;
@@ -45,7 +46,7 @@ pub fn parse_component_button<'a>(
translation = Some(Translation::from_translation_key(value));
}
"round" => {
parse_round(value, &mut round);
parse_round(value, &mut round, ctx.doc_params.globals.get().defaults.rounding_mult);
}
"color" => {
parse_color_opt(value, &mut color);
@@ -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

@@ -8,7 +8,7 @@ mod widget_rectangle;
mod widget_sprite;
use crate::{
assets::{AssetPath, AssetPathOwned, normalize_path},
assets::{normalize_path, AssetPath, AssetPathOwned},
components::{Component, ComponentWeak},
drawing::{self},
globals::WguiGlobals,
@@ -400,6 +400,7 @@ impl ParserContext<'_> {
insert_color_vars!(self, "accent", def.accent_color, def.translucent_alpha);
insert_color_vars!(self, "danger", def.danger_color, def.translucent_alpha);
insert_color_vars!(self, "faded", def.faded_color, def.translucent_alpha);
insert_color_vars!(self, "bg", def.bg_color, def.translucent_alpha);
}
}

View File

@@ -6,14 +6,14 @@ use taffy::{
use crate::{
drawing,
parser::{
AttribPair, is_percent, parse_color_hex, parse_f32, parse_percent, parse_size_unit, parse_val,
print_invalid_attrib, print_invalid_value,
is_percent, parse_color_hex, parse_f32, parse_percent, parse_size_unit, parse_val, print_invalid_attrib,
print_invalid_value, AttribPair,
},
renderer_vk::text::{FontWeight, HorizontalAlign, TextStyle},
widget::util::WLength,
};
pub fn parse_round(value: &str, round: &mut WLength) {
pub fn parse_round(value: &str, round: &mut WLength, multiplier: f32) {
if is_percent(value) {
if let Some(val) = parse_percent(value) {
*round = WLength::Percent(val);
@@ -21,7 +21,7 @@ pub fn parse_round(value: &str, round: &mut WLength) {
print_invalid_value(value);
}
} else if let Some(val) = parse_f32(value) {
*round = WLength::Units(val);
*round = WLength::Units((val * multiplier).max(0.));
} else {
print_invalid_value(value);
}

View File

@@ -2,8 +2,9 @@ use crate::{
drawing::GradientMode,
layout::WidgetID,
parser::{
AttribPair, ParserContext, ParserFile, parse_children, parse_widget_universal, print_invalid_attrib,
parse_children, parse_widget_universal, print_invalid_attrib,
style::{parse_color, parse_round, parse_style},
AttribPair, ParserContext, ParserFile,
},
widget::rectangle::{WidgetRectangle, WidgetRectangleParams},
};
@@ -40,7 +41,11 @@ pub fn parse_widget_rectangle<'a>(
}
}
"round" => {
parse_round(value, &mut params.round);
parse_round(
value,
&mut params.round,
ctx.doc_params.globals.get().defaults.rounding_mult,
);
}
"border" => {
params.border = value.parse().unwrap_or_else(|_| {

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
}
}
}