1
0
Fork 0
forked from wry/wry

tree: move container scroll logic to separate util

This commit is contained in:
Julian Orth 2022-05-07 18:11:31 +02:00
parent ec9710983c
commit a310329c42
3 changed files with 38 additions and 16 deletions

View file

@ -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<AHashMap<SeatId, SeatState>>,
state: Rc<State>,
pub render_data: RefCell<ContainerRenderData>,
scroll: Cell<f64>,
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() {

View file

@ -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;

29
src/utils/scroller.rs Normal file
View file

@ -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<f64>,
}
impl Scroller {
pub fn handle(&self, scroll: &PendingScroll) -> Option<i32> {
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
}
}
}