refactor events; watch prep work

This commit is contained in:
galister
2025-06-27 04:16:24 +09:00
parent 1215a50324
commit afb4919970
21 changed files with 613 additions and 420 deletions

View File

@@ -11,7 +11,7 @@ pub fn create_anchor<O>(app: &mut AppState) -> anyhow::Result<OverlayData<O>>
where
O: Default,
{
let (panel, _) = GuiPanel::new_from_template(app, "gui/anchor.xml")?;
let (panel, _) = GuiPanel::new_from_template(app, "gui/anchor.xml", ())?;
Ok(OverlayData {
state: OverlayState {

View File

@@ -1,6 +1,6 @@
use std::sync::Arc;
use glam::{Vec3A, vec2};
use glam::Vec3A;
use crate::{
backend::overlay::{OverlayBackend, OverlayState},
@@ -18,7 +18,7 @@ pub fn create_custom(
unreachable!();
let panel = GuiPanel::new_blank(app).ok()?;
let panel = GuiPanel::new_blank(app, ()).ok()?;
panel.update_layout().ok()?;
let state = OverlayState {

View File

@@ -1,10 +1,10 @@
use std::{cell::RefCell, collections::HashMap, rc::Rc};
use std::{collections::HashMap, rc::Rc};
use glam::{Affine2, Mat4, Vec2, Vec3, vec2, vec3a};
use wgui::{
animation::{Animation, AnimationEasing},
drawing::Color,
event::{self, EventListener},
event::{self, CallbackMetadata, EventListenerKind},
renderer_vk::util,
taffy::{self, prelude::length},
widget::{
@@ -22,7 +22,8 @@ use crate::{
};
use super::{
KEYBOARD_NAME, KeyButtonData, KeyState, KeyboardBackend, KeyboardState,
KEYBOARD_NAME, KeyButtonData, KeyState, KeyboardBackend, KeyboardState, handle_press,
handle_release,
layout::{self, AltModifier, KeyCapType},
};
@@ -38,8 +39,7 @@ where
O: Default,
{
let layout = layout::Layout::load_from_disk();
let state = Rc::new(RefCell::new(KeyboardState {
invoke_action: None,
let state = KeyboardState {
modifiers: 0,
alt_modifier: match layout.alt_modifier {
AltModifier::Shift => SHIFT,
@@ -50,9 +50,9 @@ where
_ => 0,
},
processes: vec![],
}));
};
let mut panel = GuiPanel::new_blank(app)?;
let mut panel = GuiPanel::new_blank(app, state)?;
let (background, _) = panel.layout.add_child(
panel.layout.root_widget,
@@ -179,70 +179,70 @@ where
})
};
panel.layout.add_event_listener(
panel.listeners.add(
*widget_id,
EventListener::MouseEnter(Box::new({
let (k, kb) = (key_state.clone(), state.clone());
move |data, ()| {
EventListenerKind::MouseEnter,
Box::new({
let k = key_state.clone();
move |data, _app, _state| {
data.trigger_haptics = true;
on_enter_anim(k.clone(), kb.clone(), data);
on_enter_anim(k.clone(), data);
}
})),
}),
);
panel.layout.add_event_listener(
panel.listeners.add(
*widget_id,
EventListener::MouseLeave(Box::new({
let (k, kb) = (key_state.clone(), state.clone());
move |data, ()| {
EventListenerKind::MouseLeave,
Box::new({
let k = key_state.clone();
move |data, _app, _state| {
data.trigger_haptics = true;
on_leave_anim(k.clone(), kb.clone(), data);
on_leave_anim(k.clone(), data);
}
})),
}),
);
panel.layout.add_event_listener(
panel.listeners.add(
*widget_id,
EventListener::MousePress(Box::new({
let (k, kb) = (key_state.clone(), state.clone());
move |data, button| {
kb.borrow_mut().invoke_action = Some(super::InvokeAction {
key: k.clone(),
button,
pressed: true,
});
EventListenerKind::MousePress,
Box::new({
let k = key_state.clone();
move |data, app, state| {
let CallbackMetadata::MouseButton(button) = data.metadata else {
panic!("CallbackMetadata should contain MouseButton!");
};
handle_press(app, &k, state, button);
on_press_anim(k.clone(), data);
}
})),
}),
);
panel.layout.add_event_listener(
panel.listeners.add(
*widget_id,
EventListener::MouseRelease(Box::new({
let (k, kb) = (key_state.clone(), state.clone());
move |data, button| {
kb.borrow_mut().invoke_action = Some(super::InvokeAction {
key: k.clone(),
button,
pressed: false,
});
if !matches!(&k.button_state, KeyButtonData::Modifier { sticky, .. } if sticky.get()) {
EventListenerKind::MouseRelease,
Box::new({
let k = key_state.clone();
move |data, app, state| {
if handle_release(app, &k, state) {
on_release_anim(k.clone(), data);
}
}
})),
}),
);
if let Some(modifier) = my_modifier {
panel.layout.add_event_listener(
panel.listeners.add(
*widget_id,
EventListener::InternalStateChange(Box::new({
let (k, kb) = (key_state.clone(), state.clone());
move |data, _| {
if (kb.borrow().modifiers & modifier) != 0 {
EventListenerKind::InternalStateChange,
Box::new({
let k = key_state.clone();
move |data, _app, state| {
if (state.modifiers & modifier) != 0 {
on_press_anim(k.clone(), data);
} else {
on_release_anim(k.clone(), data);
}
}
})),
}),
);
}
} else {
@@ -273,7 +273,7 @@ where
interaction_transform,
..Default::default()
},
backend: Box::new(KeyboardBackend { panel, state }),
backend: Box::new(KeyboardBackend { panel }),
..Default::default()
})
}
@@ -300,11 +300,7 @@ fn set_anim_color(key_state: &KeyState, rect: &mut Rectangle, pos: f32) {
rect.params.color2.b = key_state.color2.b + br2;
}
fn on_enter_anim(
key_state: Rc<KeyState>,
_keyboard_state: Rc<RefCell<KeyboardState>>,
data: &mut event::CallbackData,
) {
fn on_enter_anim(key_state: Rc<KeyState>, data: &mut event::CallbackData) {
data.animations.push(Animation::new(
data.widget_id,
10,
@@ -318,11 +314,7 @@ fn on_enter_anim(
));
}
fn on_leave_anim(
key_state: Rc<KeyState>,
_keyboard_state: Rc<RefCell<KeyboardState>>,
data: &mut event::CallbackData,
) {
fn on_leave_anim(key_state: Rc<KeyState>, data: &mut event::CallbackData) {
data.animations.push(Animation::new(
data.widget_id,
15,

View File

@@ -1,7 +1,6 @@
use std::{
cell::{Cell, RefCell},
cell::Cell,
process::{Child, Command},
rc::Rc,
sync::Arc,
};
@@ -29,23 +28,7 @@ pub const KEYBOARD_NAME: &str = "kbd";
const AUTO_RELEASE_MODS: [KeyModifier; 5] = [SHIFT, CTRL, ALT, SUPER, META];
struct KeyboardBackend {
panel: GuiPanel,
state: Rc<RefCell<KeyboardState>>,
}
impl KeyboardBackend {
fn handle_invoke(&mut self, app: &mut AppState) {
let mut keyboard = self.state.borrow_mut();
let Some(action) = keyboard.invoke_action.take() else {
return;
};
if action.pressed {
handle_press(app, &action.key, &mut keyboard, action.button);
} else {
handle_release(app, &action.key, &mut keyboard);
}
}
panel: GuiPanel<KeyboardState>,
}
impl OverlayBackend for KeyboardBackend {
@@ -60,13 +43,10 @@ impl OverlayBackend for KeyboardBackend {
impl InteractionHandler for KeyboardBackend {
fn on_pointer(&mut self, app: &mut AppState, hit: &PointerHit, pressed: bool) {
self.panel.on_pointer(app, hit, pressed);
self.handle_invoke(app);
let _ = self
.panel
.layout
.push_event(&wgui::event::Event::InternalStateChange(
InternalStateChangeEvent { metadata: 0 },
));
self.panel.push_event(
app,
&wgui::event::Event::InternalStateChange(InternalStateChangeEvent { metadata: 0 }),
);
}
fn on_scroll(&mut self, app: &mut AppState, hit: &PointerHit, delta_y: f32, delta_x: f32) {
self.panel.on_scroll(app, hit, delta_y, delta_x);
@@ -99,29 +79,21 @@ impl OverlayRenderer for KeyboardBackend {
self.panel.frame_meta()
}
fn pause(&mut self, app: &mut AppState) -> anyhow::Result<()> {
self.state.borrow_mut().modifiers = 0;
self.panel.state.modifiers = 0;
app.hid_provider.set_modifiers_routed(0);
self.panel.pause(app)
}
fn resume(&mut self, app: &mut AppState) -> anyhow::Result<()> {
self.panel.resume(app)?;
self.panel
.layout
.push_event(&wgui::event::Event::InternalStateChange(
InternalStateChangeEvent { metadata: 0 },
))?;
self.panel.push_event(
app,
&wgui::event::Event::InternalStateChange(InternalStateChangeEvent { metadata: 0 }),
);
Ok(())
}
}
struct InvokeAction {
key: Rc<KeyState>,
button: MouseButton,
pressed: bool,
}
struct KeyboardState {
invoke_action: Option<InvokeAction>,
modifiers: KeyModifier,
alt_modifier: KeyModifier,
processes: Vec<Child>,
@@ -207,7 +179,7 @@ fn handle_press(
}
}
fn handle_release(app: &mut AppState, key: &KeyState, keyboard: &mut KeyboardState) {
fn handle_release(app: &mut AppState, key: &KeyState, keyboard: &mut KeyboardState) -> bool {
match &key.button_state {
KeyButtonData::Key { vk, pressed } => {
pressed.set(false);
@@ -219,11 +191,15 @@ fn handle_release(app: &mut AppState, key: &KeyState, keyboard: &mut KeyboardSta
}
app.hid_provider.send_key_routed(*vk, false);
app.hid_provider.set_modifiers_routed(keyboard.modifiers);
true
}
KeyButtonData::Modifier { modifier, sticky } => {
if !sticky.get() {
if sticky.get() {
false
} else {
keyboard.modifiers &= !*modifier;
app.hid_provider.set_modifiers_routed(keyboard.modifiers);
true
}
}
KeyButtonData::Exec {
@@ -241,7 +217,8 @@ fn handle_release(app: &mut AppState, key: &KeyState, keyboard: &mut KeyboardSta
keyboard.processes.push(child);
}
}
true
}
_ => {}
_ => true,
}
}

View File

@@ -166,7 +166,7 @@ fn new_toast(toast: Toast, app: &mut AppState) -> Option<(OverlayState, Box<dyn
toast.title
};
let mut panel = GuiPanel::new_blank(app).ok()?;
let mut panel = GuiPanel::new_blank(app, ()).ok()?;
let (rect, _) = panel
.layout

View File

@@ -1,55 +1,102 @@
use std::{rc::Rc, time::Duration};
use chrono::Local;
use chrono_tz::Tz;
use glam::Vec3A;
use regex::Regex;
use wgui::{
parser::parse_color_hex,
taffy::{self, prelude::length},
widget::{
rectangle::{Rectangle, RectangleParams},
util::WLength,
},
event::{self, EventListenerKind},
widget::text::TextLabel,
};
use crate::{
backend::overlay::{OverlayData, OverlayState, Positioning, Z_ORDER_WATCH, ui_transform},
gui::panel::GuiPanel,
gui::{panel::GuiPanel, timer::GuiTimer},
state::AppState,
};
pub const WATCH_NAME: &str = "watch";
struct WatchState {}
#[allow(clippy::significant_drop_tightening)]
pub fn create_watch<O>(app: &mut AppState) -> anyhow::Result<OverlayData<O>>
where
O: Default,
{
let (mut panel, parser) = GuiPanel::new_from_template(app, "gui/watch.xml")?;
let state = WatchState {};
let (mut panel, parser) = GuiPanel::new_from_template(app, "gui/watch.xml", state)?;
for (id, widget) in parser.ids.iter() {
if id.starts_with("clock") {}
panel
.timers
.push(GuiTimer::new(Duration::from_millis(100), 0));
let clock_regex = Regex::new(r"^clock([0-9])_([a-z]+)$").unwrap();
for (id, widget_id) in parser.ids {
if let Some(cap) = clock_regex.captures(&id) {
let tz_idx: usize = cap.get(1).unwrap().as_str().parse().unwrap(); // safe due to regex
let tz_str = (tz_idx > 0)
.then(|| app.session.config.timezones.get(tz_idx - 1))
.flatten();
let role = cap.get(2).unwrap().as_str();
let mut widget = panel
.layout
.widget_map
.get_mut(widget_id)
.unwrap() // want panic
.lock()
.unwrap(); // want panic
let label = widget.obj.get_as_mut::<TextLabel>();
let format = match role {
"tz" => {
if let Some(s) =
tz_str.and_then(|tz| tz.split('/').next_back().map(|x| x.replace('_', " ")))
{
label.set_text(&s);
} else {
label.set_text("Local");
}
continue;
}
"date" => "%x",
"dow" => "%A",
"time" => {
if app.session.config.clock_12h {
"%I:%M %p"
} else {
"%H:%M"
}
}
_ => {
label.set_text("ERR");
continue;
}
};
let clock = ClockState {
timezone: tz_str.and_then(|tz| {
tz.parse()
.inspect_err(|e| log::warn!("Invalid timezone: {e:?}"))
.ok()
}),
format: format.into(),
};
panel.listeners.add(
widget_id,
EventListenerKind::InternalStateChange,
Box::new(move |data, _, _| {
clock_on_tick(&clock, data);
}),
);
}
}
if let Ok(clock) = parser.require_by_id("clock0") {}
let (_, _) = panel.layout.add_child(
panel.layout.root_widget,
Rectangle::create(RectangleParams {
color: wgui::drawing::Color::new(0., 0., 0., 0.5),
border_color: parse_color_hex("#00ffff").unwrap(),
border: 2.0,
round: WLength::Units(4.0),
..Default::default()
})
.unwrap(),
taffy::Style {
size: taffy::Size {
width: length(100.),
height: length(50.),
},
align_items: Some(taffy::AlignItems::Center),
justify_content: Some(taffy::JustifyContent::Center),
padding: length(4.0),
..Default::default()
},
)?;
let positioning = Positioning::FollowHand {
hand: app.session.config.watch_hand as _,
lerp: 1.0,
@@ -103,3 +150,18 @@ where
watch.state.alpha = watch.state.alpha.clamp(0., 1.);
}
}
struct ClockState {
timezone: Option<Tz>,
format: Rc<str>,
}
fn clock_on_tick(clock: &ClockState, data: &mut event::CallbackData) {
let date_time = clock.timezone.as_ref().map_or_else(
|| format!("{}", Local::now().format(&clock.format)),
|tz| format!("{}", Local::now().with_timezone(tz).format(&clock.format)),
);
let label = data.obj.get_as_mut::<TextLabel>();
label.set_text(&date_time);
}