From a310329c426ffb94b286383ebd3483ebf31a3ff6 Mon Sep 17 00:00:00 2001 From: Julian Orth Date: Sat, 7 May 2022 18:11:31 +0200 Subject: [PATCH] tree: move container scroll logic to separate util --- src/tree/container.rs | 24 ++++++++---------------- src/utils.rs | 1 + src/utils/scroller.rs | 29 +++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 16 deletions(-) create mode 100644 src/utils/scroller.rs diff --git a/src/tree/container.rs b/src/tree/container.rs index 5267b08c..e31a9b53 100644 --- a/src/tree/container.rs +++ b/src/tree/container.rs @@ -4,9 +4,8 @@ use { cursor::KnownCursor, fixed::Fixed, ifs::wl_seat::{ - collect_kb_foci, collect_kb_foci2, - wl_pointer::{PendingScroll, VERTICAL_SCROLL}, - NodeSeatState, SeatId, WlSeatGlobal, BTN_LEFT, PX_PER_SCROLL, + collect_kb_foci, collect_kb_foci2, wl_pointer::PendingScroll, NodeSeatState, SeatId, + WlSeatGlobal, BTN_LEFT, }, rect::Rect, render::{Renderer, Texture}, @@ -23,6 +22,7 @@ use { linkedlist::{LinkedList, LinkedNode, NodeRef}, numcell::NumCell, rc_eq::rc_eq, + scroller::Scroller, }, }, ahash::AHashMap, @@ -113,7 +113,7 @@ pub struct ContainerNode { seats: RefCell>, state: Rc, pub render_data: RefCell, - scroll: Cell, + scroller: Scroller, toplevel_data: ToplevelData, } @@ -209,7 +209,7 @@ impl ContainerNode { seats: RefCell::new(Default::default()), state: state.clone(), render_data: Default::default(), - scroll: Cell::new(0.0), + scroller: Default::default(), toplevel_data: ToplevelData::new(state, Default::default(), None), }); slf.tl_set_parent(parent); @@ -1154,17 +1154,9 @@ impl Node for ContainerNode { Some(mc) => mc, _ => return, }; - let discrete = if let Some(d) = event.discrete[VERTICAL_SCROLL as usize].get() { - self.scroll.set(0.0); - d - } else if let Some(scroll) = event.axis[VERTICAL_SCROLL as usize].get() { - let mut scroll = self.scroll.get() + scroll.to_f64(); - let discrete = (scroll / PX_PER_SCROLL).trunc(); - scroll -= discrete * PX_PER_SCROLL; - self.scroll.set(scroll); - discrete as i32 - } else { - return; + let discrete = match self.scroller.handle(event) { + Some(d) => d, + _ => return, }; let mut new_mc = cur_mc.clone(); for _ in 0..discrete.abs() { diff --git a/src/utils.rs b/src/utils.rs index f53053a9..83a8bd72 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -21,6 +21,7 @@ pub mod ptr_ext; pub mod queue; pub mod rc_eq; pub mod run_toplevel; +pub mod scroller; pub mod smallmap; pub mod stack; pub mod syncqueue; diff --git a/src/utils/scroller.rs b/src/utils/scroller.rs new file mode 100644 index 00000000..db75eb99 --- /dev/null +++ b/src/utils/scroller.rs @@ -0,0 +1,29 @@ +use { + crate::ifs::wl_seat::{ + wl_pointer::{PendingScroll, VERTICAL_SCROLL}, + PX_PER_SCROLL, + }, + std::cell::Cell, +}; + +#[derive(Default)] +pub struct Scroller { + scroll: Cell, +} + +impl Scroller { + pub fn handle(&self, scroll: &PendingScroll) -> Option { + if let Some(d) = scroll.discrete[VERTICAL_SCROLL as usize].get() { + self.scroll.set(0.0); + Some(d) + } else if let Some(scroll) = scroll.axis[VERTICAL_SCROLL as usize].get() { + let mut scroll = self.scroll.get() + scroll.to_f64(); + let discrete = (scroll / PX_PER_SCROLL).trunc(); + scroll -= discrete * PX_PER_SCROLL; + self.scroll.set(scroll); + Some(discrete as i32) + } else { + None + } + } +}