use std::collections::HashMap;
use super::{SkinRuntime, HOST_WINDOWS};
use crate::draw::Rect;
use crate::gui::{Kind, ObjId};
use crate::host::SkinMenu;
use crate::maki::{Handle, Value};
const MAX_MENUS_PER_GESTURE: i32 = 3;
const TITLE_BAR: &str = "wasabi.titlebar";
const TITLE_BAR_TRAP: &str = "mousetrap";
const GRIP_EDGES: [&str; 4] = ["left", "right", "top", "bottom"];
const STATE_ACTIONS: [&str; 3] = ["TOGGLE", "EQ_TOGGLE", "EQ_AUTO"];
const POINTER_EVENTS: [&str; 9] = [
"onleftbuttondown",
"onleftbuttonup",
"onrightbuttondown",
"onrightbuttonup",
"onleftbuttondblclk",
"onrightbuttondblclk",
"onmousemove",
"onleftclick",
"onrightclick",
];
const TAP_EVENTS: [&str; 2] = ["onleftbuttondown", "onleftbuttonup"];
const FOLLOW_EVENTS: [&str; 1] = ["onmousemove"];
const PRESS_EVENTS: [&str; 1] = ["onleftbuttondown"];
#[derive(Debug, Clone)]
struct Resize {
window: ObjId,
edges: String,
x: i32,
y: i32,
size: (i32, i32),
steps: Option<(i32, i32)>,
}
#[derive(Debug, Clone, Copy)]
struct DeferredPress {
target: ObjId,
sx: i32,
sy: i32,
}
#[derive(Debug, Clone, Copy)]
struct MenuAim {
seq: i32,
target: ObjId,
x: i32,
y: i32,
}
#[derive(Debug, Default)]
pub(crate) struct PointerState {
captured_origin: Rect,
captured_scale: f32,
down_at: Option<(i32, i32)>,
deferred: Option<DeferredPress>,
resizing: Option<Resize>,
menu_aim: Option<MenuAim>,
move_seq: Option<i32>,
aimed: Option<ObjId>,
pub(crate) gesture_depth: i32,
menus_this_gesture: i32,
menus_ended: bool,
pub(crate) picked_this_gesture: bool,
pub(crate) press_ended: bool,
entered: bool,
cancelling: bool,
menu_refusals_logged: Vec<String>,
pub(crate) skin_menus: HashMap<ObjId, bool>,
skin_menu_reads: i64,
pub(crate) precise: bool,
pub(crate) ctrl: bool,
pub(crate) shift: bool,
}
impl SkinRuntime {
pub(crate) fn window_at(&mut self, x: i32, y: i32) -> Option<ObjId> {
if self.needs_layout {
self.do_layout();
}
if let Some(a) = self.pointer.aimed {
return Some(a);
}
let windows = self.z_windows();
if self.free_windows() {
if let Some(w) = windows
.iter()
.rev()
.find(|l| self.docking_rect(**l).contains(x, y))
{
return Some(*w);
}
}
let distance2 = |r: Rect| -> i64 {
let dx = (r.left - x).max(0).max(x - (r.right() - 1)) as i64;
let dy = (r.top - y).max(0).max(y - (r.bottom() - 1)) as i64;
dx * dx + dy * dy
};
if let Some(w) = windows
.iter()
.rev()
.find(|l| self.window_rect_of(**l).contains(x, y))
{
return Some(*w);
}
let slop = self.touch_slop as i64;
windows
.iter()
.copied()
.min_by_key(|l| distance2(self.window_rect_of(*l)))
.filter(|l| distance2(self.window_rect_of(*l)) <= slop * slop)
}
fn in_window<T>(
&mut self,
window: crate::host::WindowId,
x: i32,
y: i32,
block: impl FnOnce(&mut Self, i32, i32) -> T,
) -> T {
let l = self.layouts_by_id_lookup(window);
if self.needs_layout {
self.do_layout();
}
let origin = self.window_rect_of(l);
let before = self.pointer.aimed;
self.pointer.aimed = Some(l);
let r = block(self, origin.left + x, origin.top + y);
self.pointer.aimed = before;
r
}
fn layouts_by_id_lookup(&self, id: crate::host::WindowId) -> ObjId {
self.layouts_by_id[(id.0 as usize).wrapping_sub(1)]
}
pub fn pointer_down_in(&mut self, window: crate::host::WindowId, x: i32, y: i32, seq: i32) {
self.in_window(window, x, y, |rt, sx, sy| rt.pointer_down(sx, sy, seq));
}
pub fn pointer_move_in(&mut self, window: crate::host::WindowId, x: i32, y: i32) {
self.in_window(window, x, y, |rt, sx, sy| rt.pointer_move(sx, sy));
}
pub fn pointer_up_in(&mut self, window: crate::host::WindowId, x: i32, y: i32) {
self.in_window(window, x, y, |rt, sx, sy| rt.pointer_up(sx, sy));
}
pub fn double_tap_in(&mut self, window: crate::host::WindowId, x: i32, y: i32, seq: i32) {
self.in_window(window, x, y, |rt, sx, sy| rt.double_tap(sx, sy, seq));
}
fn captured_local(&self, sx: i32, sy: i32) -> (i32, i32) {
let at = self.pointer.captured_origin;
let s = if self.pointer.captured_scale > 0.0 {
self.pointer.captured_scale
} else {
1.0
};
(
((sx - at.left) as f32 / s).floor() as i32,
((sy - at.top) as f32 / s).floor() as i32,
)
}
pub(crate) fn hit_test_stack(&mut self, x: i32, y: i32) -> Option<ObjId> {
let l = self.window_at(x, y)?;
let (x, y) = self.layout_point(l, x, y);
self.hit_test(l, x, y)
}
fn reacts(&self, o: ObjId) -> bool {
self.is_control(o) || self.scripts.iter().any(|s| s.listens_to(Handle::Gui(o)))
}
fn is_control(&self, o: ObjId) -> bool {
matches!(
self.objects[o].kind,
Kind::Button(_)
| Kind::Slider(_)
| Kind::ColorThemeList(_)
| Kind::PlaylistView(_)
| Kind::ClassicWindow(_)
) || self.is_grip(o)
}
pub(crate) fn classic_filled(&self) -> bool {
self.stretch_last && self.view_size.is_some() && !self.free_windows()
}
pub(crate) fn is_grip(&self, o: ObjId) -> bool {
let Some(edges) = self.objects[o]
.base
.attr("resize")
.map(|e| e.to_lowercase())
else {
return false;
};
self.layout_object(o).is_some() && GRIP_EDGES.iter().any(|e| edges.contains(e))
}
fn drag_grip(&mut self, r: &Resize, x: i32, y: i32) {
let l = r.window;
let (mut w, mut h) = r.size;
if r.edges.contains("right") {
w += x - r.x;
}
if r.edges.contains("left") {
w -= x - r.x;
}
if r.edges.contains("bottom") {
h += y - r.y;
}
if r.edges.contains("top") {
h -= y - r.y;
}
let apart = self.free_windows();
let at = self.window_rect_of(l);
let scale = self.layout_scale(l);
let on_screen = |v: i32| (v as f32 * scale).round() as i32;
let in_layout = |v: i32| (v as f32 / scale).round() as i32;
if apart {
let mut want = Rect::new(at.left, at.top, on_screen(w), on_screen(h));
if r.edges.contains("left") {
want.left = at.right() - want.width;
}
if r.edges.contains("top") {
want.top = at.bottom() - want.height;
}
let got = self.autodock_edges(l, want, &r.edges);
w = in_layout(got.width);
h = in_layout(got.height);
}
let (vw, vh) = self.viewport();
let (vw, vh) = (in_layout(vw), in_layout(vh));
let d = self.objects[l].layout().expect("a layout");
let mut size = (
within(w, d.min_w, d.max_w, vw),
within(h, d.min_h, d.max_h, vh),
);
if let Some((sw, sh)) = r.steps {
let horizontal = r.edges.contains("left") || r.edges.contains("right");
let vertical = r.edges.contains("top") || r.edges.contains("bottom");
if horizontal {
size.0 = stepped(size.0, sw, |v| within(v, d.min_w, d.max_w, vw));
}
if vertical {
size.1 = stepped(size.1, sh, |v| within(v, d.min_h, d.max_h, vh));
}
}
if Some(l) != self.main_layout() {
self.objects[l].layout_mut().expect("a layout").user_size = Some(size);
}
if apart {
let x = if r.edges.contains("left") {
at.right() - on_screen(size.0)
} else {
at.left
};
let y = if r.edges.contains("top") {
at.bottom() - on_screen(size.1)
} else {
at.top
};
let d = self.objects[l].layout_mut().expect("a layout");
d.view_x = x;
d.view_y = y;
}
if size != self.layout_size(l) {
self.resize_layout_to(l, size.0, size.1);
}
}
fn end_grip(&mut self) {
let Some(r) = self.pointer.resizing.take() else {
return;
};
let l = r.window;
let (w, h) = self.layout_size(l);
let d = self.objects[l].layout().expect("a layout");
let (vx, vy) = (d.view_x, d.view_y);
self.dispatch(
Handle::Gui(l),
"onuserresize",
&[Value::Int(vx), Value::Int(vy), Value::Int(w), Value::Int(h)],
);
}
fn hears_tap(&self, o: ObjId) -> bool {
self.dispatch_chain(o).iter().any(|c| {
self.scripts
.iter()
.any(|s| s.listens_to_any(Handle::Gui(*c), &TAP_EVENTS))
})
}
fn follows_finger(&mut self, o: ObjId) -> bool {
let chain = self.dispatch_chain(o);
let mut any = false;
for c in &chain {
for s in 0..self.scripts.len() {
if self.scripts[s].listens_to_any(Handle::Gui(*c), &FOLLOW_EVENTS)
|| self.scripts[s].reads_mouse(Handle::Gui(*c), &PRESS_EVENTS)
{
any = true;
}
}
}
self.take_script_logs();
any
}
fn clickable(&mut self, o: ObjId, window: ObjId, x: i32, y: i32) -> bool {
let r = self.objects[o].base.rect;
let right = r.right() - 1;
let bottom = r.bottom() - 1;
let points = [
(
x.clamp(r.left, right.max(r.left)),
y.clamp(r.top, bottom.max(r.top)),
),
((r.left + right) / 2, (r.top + bottom) / 2),
(r.left, r.top),
(right, r.top),
(r.left, bottom),
(right, bottom),
];
points.iter().any(|(px, py)| {
let mut at = self.hit_test(window, *px, *py);
while let Some(h) = at {
if h == o {
return true;
}
at = self.objects[h].base.parent;
}
false
})
}
pub(crate) fn distance2(&self, o: ObjId, x: i32, y: i32) -> i64 {
let r = self.objects[o].base.rect;
let dx = (r.left - x).max(0).max(x - (r.right() - 1)) as i64;
let dy = (r.top - y).max(0).max(y - (r.bottom() - 1)) as i64;
dx * dx + dy * dy
}
pub(crate) fn touch_target(&mut self, sx: i32, sy: i32) -> Option<ObjId> {
let window = self.window_at(sx, sy)?;
let (x, y) = self.layout_point(window, sx, sy);
let hit = self.hit_test(window, x, y);
if let Some(h) = hit {
if let Some(grip) = self.classic_grip_near(h, x, y) {
return Some(grip);
}
if self.is_control(h)
|| matches!(self.objects[h].kind, Kind::Menu(_) | Kind::LayoutStatus(_))
{
return Some(h);
}
}
let slop2 = (self.touch_slop as i64) * (self.touch_slop as i64);
let objects = self.all_objects.clone();
let nearest = |rt: &mut SkinRuntime,
shown: bool,
pick: &dyn Fn(&SkinRuntime, ObjId) -> bool|
-> Option<ObjId> {
let mut best: Option<(ObjId, i64)> = None;
for o in objects.iter().rev().copied() {
if rt.layout_object(o) != Some(window) || !rt.touchable(o, shown) {
continue;
}
let d = rt.distance2(o, x, y);
if d > slop2 || !pick(rt, o) {
continue;
}
if shown && !rt.clickable(o, window, x, y) {
continue;
}
if best.is_none_or(|(_, bd)| d < bd) {
best = Some((o, d));
}
}
best.map(|(o, _)| o)
};
let control = nearest(self, true, &|rt, o| rt.is_control(o));
if let Some(h) = hit {
if self.hears_tap(h)
&& !control.is_some_and(|c| {
self.objects[c]
.base
.rect
.overlaps(&self.objects[h].base.rect)
})
{
return Some(h);
}
}
if let Some(c) = control {
return Some(c);
}
if let Some(h) = hit.filter(|h| self.reacts(*h)) {
return Some(h);
}
if let Some(o) = nearest(self, false, &|rt, o| {
!rt.is_control(o)
&& rt
.scripts
.iter()
.any(|s| s.listens_to_any(Handle::Gui(o), &POINTER_EVENTS))
}) {
return Some(o);
}
hit
}
pub fn pointer_down(&mut self, sx: i32, sy: i32, seq: i32) {
self.gesture(true, |rt| {
rt.aim_menu(sx, sy, seq);
rt.pointer.move_seq = None;
rt.pointer.precise = false;
if rt.free_windows() {
if let Some(w) = rt.window_under(sx, sy) {
rt.raise_window(w);
}
}
let touched = rt.pointer_down_at(sx, sy);
let held = rt.captured.or(rt.pointer.deferred.map(|d| d.target));
if touched.is_none() || held.is_some_and(|h| rt.moves_window(h)) {
rt.pointer.move_seq = Some(seq);
if rt.free_windows() {
if let Some(w) = rt.window_under(sx, sy) {
rt.free_drag_begin(w, sx, sy);
}
}
}
});
}
fn pointer_down_at(&mut self, sx: i32, sy: i32) -> Option<ObjId> {
self.drop_deferred();
if self.needs_layout {
self.do_layout();
}
let target = self.touch_target(sx, sy)?;
if let Some(l) = self.layout_object(target) {
self.take_focus(l);
}
if self.pointer.menu_aim.is_some_and(|a| a.target == target)
&& !matches!(self.objects[target].kind, Kind::Slider(_))
{
self.pointer.deferred = Some(DeferredPress { target, sx, sy });
self.show_pressed(target, true);
return Some(target);
}
self.press(target, sx, sy);
Some(target)
}
fn show_pressed(&mut self, target: ObjId, pressed: bool) {
if let Kind::Button(b) = &mut self.objects[target].kind {
b.pressed = pressed;
self.repaint();
}
}
fn press(&mut self, target: ObjId, sx: i32, sy: i32) {
let window = self.layout_object(target);
self.pointer.captured_origin = window.map_or(Rect::EMPTY, |l| self.window_rect_of(l));
self.pointer.captured_scale = window.map_or(1.0, |l| self.layout_scale(l));
let (x, y) = self.captured_local(sx, sy);
self.last_pointer = (x, y);
self.captured = Some(target);
if let Some(window) = self.layout_object(target) {
let filled =
self.classic_filled() && self.objects[target].base.id.starts_with("classic.");
if self.is_grip(target) && !filled {
let edges = self.objects[target]
.base
.attr("resize")
.unwrap_or("")
.to_lowercase();
let size = self.layout_size(window);
let steps = self.objects[target]
.base
.id
.starts_with("classic.")
.then_some(crate::classic::touch::CLASSIC_SIZE_STEP);
self.pointer.resizing = Some(Resize {
window,
edges,
x,
y,
size,
steps,
});
}
}
self.object_pointer_down(target, x, y);
if self.captured == Some(target) {
self.pointer.entered = true;
self.dispatch(Handle::Gui(target), "onenterarea", &[]);
}
}
pub fn pointer_move(&mut self, sx: i32, sy: i32) {
self.gesture(false, |rt| rt.pointer_move_at(sx, sy));
}
fn send_deferred(&mut self) {
let Some(d) = self.pointer.deferred else {
return;
};
self.drop_deferred();
if self.needs_layout {
self.do_layout();
}
if self.all_objects.contains(&d.target) {
self.press(d.target, d.sx, d.sy);
}
}
fn drop_deferred(&mut self) {
let Some(d) = self.pointer.deferred.take() else {
return;
};
self.show_pressed(d.target, false);
}
fn pointer_move_at(&mut self, sx: i32, sy: i32) {
if self.free_dragging() {
self.free_drag_to(sx, sy);
}
if let Some(d) = self.pointer.deferred {
let dx = sx - d.sx;
let dy = sy - d.sy;
if !self.pointer.precise && dx * dx + dy * dy <= self.touch_slop * self.touch_slop {
return;
}
self.send_deferred();
}
let (x, y) = self.captured_local(sx, sy);
self.last_pointer = (x, y);
if let Some(r) = self.pointer.resizing.clone() {
self.drag_grip(&r, x, y);
}
if let Some(c) = self.captured {
if let Some(next) = self.eq_band_reached(c, sx, sy) {
self.object_pointer_up(c, x, y, true);
self.leave_area(c);
self.captured = Some(next);
self.object_pointer_down(next, x, y);
if self.captured == Some(next) {
self.pointer.entered = true;
self.dispatch(Handle::Gui(next), "onenterarea", &[]);
}
return;
}
self.object_pointer_move(c, x, y);
}
}
pub fn pointer_up(&mut self, sx: i32, sy: i32) {
self.gesture(false, |rt| rt.pointer_up_at(sx, sy));
}
fn pointer_up_at(&mut self, sx: i32, sy: i32) {
if self.free_dragging() {
self.free_drag_to(sx, sy);
}
self.free_drag_end();
self.send_deferred();
let (x, y) = self.captured_local(sx, sy);
self.last_pointer = (x, y);
if let Some(r) = self.pointer.resizing.clone() {
self.drag_grip(&r, x, y);
}
self.end_grip();
let Some(target) = self.captured.take() else {
return;
};
if self.pointer.precise {
if let Some(under) = self.hit_test_stack(sx, sy).filter(|u| {
*u != target
&& self.reacts(*u)
&& !matches!(self.objects[*u].kind, Kind::ClassicWindow(_))
}) {
self.object_pointer_up(target, x, y, false);
self.leave_area(target);
self.pointer.precise = false;
self.press(under, sx, sy);
return self.pointer_up_at(sx, sy);
}
}
let r = self.objects[target].base.rect;
let slop = if self.pointer.precise {
0
} else {
self.touch_slop
};
let inside = x >= r.left - slop
&& x < r.right() + slop
&& y >= r.top - slop
&& y < r.bottom() + slop;
self.object_pointer_up(target, x, y, inside);
self.leave_area(target);
}
pub fn double_tap(&mut self, sx: i32, sy: i32, seq: i32) {
self.gesture(true, |rt| {
rt.aim_menu(sx, sy, seq);
rt.pointer.move_seq = None;
rt.double_tap_at(sx, sy);
});
}
fn double_tap_at(&mut self, sx: i32, sy: i32) {
self.drop_deferred();
let Some(target) = self.hit_test_stack(sx, sy) else {
return;
};
let (x, y) = match self.layout_object(target) {
Some(l) => self.layout_point(l, sx, sy),
None => (sx, sy),
};
self.object_double_tap(target, x, y);
}
pub fn menu_target(&self) -> Option<i32> {
self.pointer.menu_aim.map(|a| a.seq)
}
pub fn move_target(&self) -> Option<i32> {
if self.free.on {
return None;
}
self.pointer.move_seq
}
pub(crate) fn moves_window(&mut self, o: ObjId) -> bool {
if matches!(self.objects[o].kind, Kind::ClassicWindow(_)) {
return self.classic_moves_window(o);
}
if matches!(
self.objects[o].kind,
Kind::Slider(_) | Kind::ColorThemeList(_) | Kind::PlaylistView(_)
) {
return false;
}
if self.is_grip(o) {
return false;
}
if matches!(self.objects[o].kind, Kind::Button(_) | Kind::Menu(_)) {
return !self.follows_finger(o);
}
self.is_move_area(o) || !self.follows_finger(o)
}
fn is_move_area(&self, o: ObjId) -> bool {
if let Some(m) = self.objects[o].base.move_window {
return m;
}
let b = &self.objects[o].base;
if b.id == TITLE_BAR_TRAP
&& b.parent
.is_some_and(|p| self.objects[p].base.id == TITLE_BAR)
{
return true;
}
let mut p = b.parent;
while let Some(g) = p {
if let Some(m) = self.objects[g].base.move_window {
return m;
}
p = self.objects[g].base.parent;
}
false
}
fn aim_menu(&mut self, sx: i32, sy: i32, seq: i32) {
if self.needs_layout {
self.do_layout();
}
self.pointer.menu_aim = self.menu_aim_at(sx, sy, seq);
self.pointer.down_at = Some((sx, sy));
}
pub(crate) fn press_point(&self) -> Option<(i32, i32)> {
self.pointer
.down_at
.filter(|_| self.pointer.gesture_depth > 0)
}
fn menu_aim_at(&mut self, sx: i32, sy: i32, seq: i32) -> Option<MenuAim> {
let window = self.window_at(sx, sy)?;
let (x, y) = self.layout_point(window, sx, sy);
if let Some(target) = self.classic_row_menu_at(window, x, y) {
return Some(MenuAim { seq, target, x, y });
}
let target = self.menu_object_at(window, x, y)?;
Some(MenuAim { seq, target, x, y })
}
fn menu_object_at(&mut self, window: ObjId, x: i32, y: i32) -> Option<ObjId> {
let slop2 = (self.touch_slop as i64) * (self.touch_slop as i64);
let mut best: Option<(ObjId, i64)> = None;
for o in self.all_objects.clone().into_iter().rev() {
if self.layout_object(o) != Some(window)
|| self.objects[o].base.ghost
|| !self.touchable(o, true)
{
continue;
}
let d = self.distance2(o, x, y);
if d > slop2 {
continue;
}
let has = if let Some(h) = self.pointer.skin_menus.get(&o) {
*h
} else {
let h = self.has_skin_menu(o);
self.pointer.skin_menus.insert(o, h);
h
};
if has && best.is_none_or(|(_, bd)| d < bd) {
best = Some((o, d));
}
}
best.map(|(o, _)| o)
}
pub(crate) fn has_skin_menu(&mut self, o: ObjId) -> bool {
self.pointer.skin_menu_reads += 1;
let chain = self.dispatch_chain(o);
let is_button = matches!(self.objects[o].kind, Kind::Button(_));
let events = crate::maki::script::right_click_events(is_button);
let mut reaches = false;
for c in &chain {
for s in 0..self.scripts.len() {
if self.scripts[s].reaches_menu(Handle::Gui(*c), &events) {
reaches = true;
}
}
}
self.take_script_logs();
if reaches {
return true;
}
let mut called: Vec<String> = Vec::new();
for c in &chain {
for s in 0..self.scripts.len() {
for e in self.scripts[s].events_called(Handle::Gui(*c), &events) {
if !called.contains(&e) {
called.push(e);
}
}
}
}
let called: Vec<&str> = called.iter().map(|s| s.as_str()).collect();
let mut any = false;
if !called.is_empty() {
for c in &chain {
for s in 0..self.scripts.len() {
if self.scripts[s].reaches_menu(Handle::Gui(*c), &called) {
any = true;
}
}
}
}
self.take_script_logs();
any
}
fn take_script_logs(&mut self) {
for s in 0..self.scripts.len() {
if let Some(line) = self.scripts[s].pending_log.take() {
self.host.log(&line);
}
}
}
fn dispatch_chain(&self, o: ObjId) -> Vec<ObjId> {
let mut chain = vec![o];
let mut at = o;
while let Some(p) = self.objects[at].base.parent {
let exposes = self.objects[p]
.group
.as_ref()
.is_some_and(|g| g.embed_xui.as_deref() == Some(self.objects[at].base.id.as_str()));
if !exposes || chain.contains(&p) {
break;
}
chain.push(p);
at = p;
}
chain
}
pub fn right_click(&mut self, seq: i32) {
self.gesture(true, |rt| {
if let Some(aim) = rt.pointer.menu_aim.filter(|a| a.seq == seq) {
rt.right_click_at(aim);
}
});
}
fn right_click_at(&mut self, aim: MenuAim) {
self.drop_deferred();
if self.needs_layout {
self.do_layout();
}
if !self.all_objects.contains(&aim.target) {
return;
}
if self.captured.is_some() {
self.end_press();
}
self.last_pointer = (aim.x, aim.y);
self.object_right_click(aim.target, aim.x, aim.y);
if matches!(self.objects[aim.target].kind, Kind::Button(_)) {
self.dispatch(Handle::Gui(aim.target), "onrightclick", &[]);
}
}
pub fn gesture<T>(&mut self, fresh: bool, block: impl FnOnce(&mut Self) -> T) -> T {
self.gesture_begin(fresh);
let r = block(self);
self.gesture_end();
r
}
pub fn gesture_begin(&mut self, fresh: bool) {
if fresh {
self.pointer.menus_this_gesture = 0;
self.pointer.menus_ended = false;
self.pointer.picked_this_gesture = false;
self.pointer.press_ended = false;
}
self.pointer.gesture_depth += 1;
}
pub fn gesture_end(&mut self) {
self.pointer.gesture_depth = self.pointer.gesture_depth.saturating_sub(1);
}
pub fn menu_choice(&mut self, menu: &SkinMenu) -> i32 {
let refusal = if self.pointer.gesture_depth == 0 {
Some("outside a touch".to_string())
} else if self.pointer.menus_ended {
Some("after one closed without the listener".to_string())
} else if self.pointer.cancelling {
Some("while a press ends without a release".to_string())
} else if self.pointer.menus_this_gesture >= MAX_MENUS_PER_GESTURE {
Some(format!("more than {MAX_MENUS_PER_GESTURE} in one touch"))
} else {
None
};
if let Some(refusal) = refusal {
if !self.pointer.menu_refusals_logged.contains(&refusal) {
self.pointer.menu_refusals_logged.push(refusal.clone());
self.host.log(&format!("popup menu {refusal}: not shown"));
}
return 0;
}
self.pointer.menus_this_gesture += 1;
self.end_press();
let picked = self.host.choose_from_menu(menu);
if picked.is_none() {
self.pointer.menus_ended = true;
}
if picked.unwrap_or(0) != 0 {
self.pointer.picked_this_gesture = true;
}
picked.unwrap_or(0)
}
pub fn set_pointer_precise(&mut self, precise: bool) {
self.pointer.precise = precise;
}
pub fn set_pointer_modifiers(&mut self, ctrl: bool, shift: bool) {
self.pointer.ctrl = ctrl;
self.pointer.shift = shift;
}
pub fn press_controls(&mut self) -> Vec<crate::host::TouchControl> {
if let Some(press) = self.classic_press {
return self.classic_controls(press);
}
let Some(held) = self.captured else {
return Vec::new();
};
let Some(window) = self.layout_object(held) else {
return Vec::new();
};
let at = self.window_rect_of(window);
self.all_objects
.clone()
.into_iter()
.filter(|o| {
self.layout_object(*o) == Some(window)
&& self.is_control(*o)
&& self.touchable(*o, false)
})
.map(|o| {
let r = self.objects[o].base.rect;
crate::host::TouchControl {
rect: Rect::new(at.left + r.left, at.top + r.top, r.width, r.height),
magnify: false,
}
})
.collect()
}
pub fn pointer_cancel(&mut self) {
self.gesture(false, |rt| rt.end_press());
}
fn end_press(&mut self) {
self.pointer.press_ended = true;
self.drop_deferred();
self.end_grip();
let Some(held) = self.captured.take() else {
return;
};
self.pointer.cancelling = true;
self.object_pointer_cancel(held);
self.pointer.cancelling = false;
self.leave_area(held);
}
fn leave_area(&mut self, o: ObjId) {
if !self.pointer.entered {
return;
}
self.pointer.entered = false;
self.dispatch(Handle::Gui(o), "onleavearea", &[]);
}
pub(crate) fn action(
&mut self,
source: ObjId,
action: &str,
param: Option<&str>,
target: Option<&str>,
) {
let a = action.to_uppercase();
let point = self.press_point();
let from = self
.action_from
.take()
.or_else(|| self.stack_rect_of(source))
.map(|rect| crate::host::ActionFrom { rect, point });
let source_id = self.objects[source].base.id.clone();
self.host.log(&format!(
"action {a} {} from {source_id}",
param.unwrap_or("")
));
match a.as_str() {
"PLAY" => self.player.play(),
"PAUSE" => self.player.pause(),
"STOP" => self.player.stop(),
"PREV" => self.player.previous(),
"NEXT" => self.player.next(),
"EJECT" => self.player.eject(),
"VOLUME_UP" => {
let v = (self.player.volume() + 10).min(255);
self.player.set_volume(v);
}
"VOLUME_DOWN" => {
let v = (self.player.volume() - 10).max(0);
self.player.set_volume(v);
}
"TOGGLE" => {
let Some(param) = param else { return };
let id = param.to_lowercase();
let id = id.strip_prefix("guid:").unwrap_or(&id).to_string();
let kind = Self::window_kind(&id);
let answers = self.host_actions
&& (HOST_WINDOWS.contains(&kind.as_str()) || self.toggle_target(&id).is_none());
if answers {
self.host.host_action(&a, Some(&kind), from);
if HOST_WINDOWS.contains(&kind.as_str()) {
self.invalidate();
return;
}
}
self.toggle_window(&id, param);
self.invalidate();
}
"SWITCH" => {
if let (Some(p), Some(c)) = (
param,
self.layout_object(source)
.and_then(|l| self.objects[l].layout().and_then(|d| d.container)),
) {
self.switch_to(c, p);
}
}
"CB_PREVPAGE" | "CB_PREV" | "CB_NEXTPAGE" | "CB_NEXT" => {
if let Some(b) = self.bucket_for(source, target) {
self.bucket_page(b, if a.starts_with("CB_PREV") { -1 } else { 1 });
}
}
"CLOSE" => {
let c = self
.layout_object(source)
.and_then(|l| self.objects[l].layout().and_then(|d| d.container));
if c.is_some() && c == self.main_container() {
if self.host_actions {
self.host.host_action(&a, param, from);
}
} else if let Some(c) = c.filter(|c| self.containers[*c].can_close()) {
self.close_container(c);
}
}
"MINIMIZE" | "SYSMENU" | "CONTROLMENU" | "MENU" | "TRACKINFO" | "VIS_NEXT"
| "VIS_PREV" | "VIS_MENU" | "VIS_FS" | "VID_FS" | "VID_1X" | "VID_2X" | "VID_TV"
| "VID_MISC" | "EQ_PRESETS" | "PLAYLIST" | "PLAYLIST_SELECT" | "PLAYLIST_SELECTION"
| "PLAYLIST_MENU" | "ALWAYS_ON_TOP" | "DOUBLESIZE" | "ABOUT" | "PE_ADD" | "PE_REM"
| "PE_SEL" | "PE_MISC" | "PE_LIST" => {
if self.host_actions {
self.host.host_action(&a, param, from);
}
}
"EQ_TOGGLE" => {
let on = !self.player.eq_enabled();
self.player.set_eq_enabled(on);
}
"EQ_AUTO" => {
let on = !self.config_flags.get("eq_auto").copied().unwrap_or(false);
self.config_flags.insert("eq_auto".into(), on);
}
"TEXT_LARGER" | "TEXT_SMALLER" => {}
"COLORTHEMES_SWITCH" | "COLORTHEMES_PREVIOUS" | "COLORTHEMES_NEXT" => {
let parent = self.objects[source].base.parent;
let named = target.and_then(|t| {
parent.and_then(|p| self.find_object(p, t)).or_else(|| {
let windows = self.windows();
windows.iter().find_map(|l| self.find_object(*l, t))
})
});
let list = named
.filter(|l| matches!(self.objects[*l].kind, Kind::ColorThemeList(_)))
.or_else(|| {
self.all_objects
.iter()
.copied()
.find(|o| matches!(self.objects[*o].kind, Kind::ColorThemeList(_)))
});
let Some(list) = list else { return };
match a.as_str() {
"COLORTHEMES_SWITCH" => self.themes_switch_to_selected(list),
"COLORTHEMES_PREVIOUS" => self.themes_step(list, -1),
_ => self.themes_step(list, 1),
}
self.invalidate();
}
_ => {
let parent = self.objects[source].base.parent;
let t = target.and_then(|t| {
parent.and_then(|p| self.find_object(p, t)).or_else(|| {
let windows = self.windows();
windows.iter().find_map(|l| self.find_object(*l, t))
})
});
if let Some(t) = t {
self.dispatch(
Handle::Gui(t),
"onaction",
&[
Value::str(action),
Value::str(param.unwrap_or("")),
Value::Int(0),
Value::Int(0),
Value::Int(0),
Value::Int(0),
Value::Obj(Handle::Gui(source)),
],
);
}
}
}
self.invalidate();
}
fn toggle_window(&mut self, id: &str, param: &str) {
if let Some(c) = self.toggle_target(id) {
if self.opened.contains(&c) {
self.close_container(c);
} else {
let takes_focus =
param.to_lowercase().contains("guid:") || self.shown_before.contains(&c);
self.open_container(c);
if takes_focus {
self.focused = self.containers[c].current;
}
}
} else {
let key = format!("window:{id}");
let on = !self.config_flags.get(&key).copied().unwrap_or(false);
self.config_flags.insert(key, on);
}
}
pub(crate) fn action_state(&mut self, action: Option<&str>, param: Option<&str>) -> bool {
match action.map(|a| a.to_uppercase()).as_deref() {
Some("TOGGLE") => {
let Some(param) = param else { return false };
let id = param.to_lowercase();
let id = id.strip_prefix("guid:").unwrap_or(&id).to_string();
let kind = Self::window_kind(&id);
if let Some(open) = self.host.window_open(&kind) {
return open;
}
match self.toggle_target(&id) {
Some(c) => self.is_open(c),
None => self
.config_flags
.get(&format!("window:{id}"))
.copied()
.unwrap_or(false),
}
}
Some("EQ_TOGGLE") => self.player.eq_enabled(),
Some("EQ_AUTO") => self.config_flags.get("eq_auto").copied().unwrap_or(false),
_ => false,
}
}
pub(crate) fn has_action_state(action: Option<&str>) -> bool {
action.is_some_and(|a| STATE_ACTIONS.contains(&a.to_uppercase().as_str()))
}
}
fn stepped(v: i32, step: i32, fit: impl Fn(i32) -> i32) -> i32 {
let up = crate::classic::touch::classic_step_up(v, step);
if fit(up) == up {
up
} else {
(v / step * step).max(step)
}
}
fn within(v: i32, min: i32, max: i32, view: i32) -> i32 {
let top = if max > 0 { max.min(view) } else { view };
v.min(top).max(min.min(top)).max(1)
}