Skip to content

feat: add scroll events to MouseEvent#142

Open
0xferrous wants to merge 1 commit intoratatui:mainfrom
0xferrous:push-vvtyzmusrpoo
Open

feat: add scroll events to MouseEvent#142
0xferrous wants to merge 1 commit intoratatui:mainfrom
0xferrous:push-vvtyzmusrpoo

Conversation

@0xferrous
Copy link
Contributor

No description provided.

Copy link
Member

@junkdog junkdog left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

neat, looking pretty good overall! i had some thoughts on the ScrollDelta/deltaMode handling; comments below.

let mouse_position = Rc::new(RefCell::new((0, 0)));
let mouse_button = Rc::new(RefCell::new(None::<MouseButton>));
let mouse_event_kind = Rc::new(RefCell::new(None::<MouseEventKind>));
let scroll_count = Rc::new(RefCell::new(0i32));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe track scrolled and h&v separately, otherwise it can give the impression that ratzilla does not distinguish between the two.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is h&v?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, horizontal and vertical

/// Handles wheel events.
///
/// This method takes a closure that will be called on every `wheel` event.
fn on_wheel_event<F>(&self, mut callback: F)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it make sense to merge on_wheel_event with on_mouse_event? it's one less closure to care about, so the callback function could handle all mouse event kinds in one place.

/// - Pages: Multiplied by ~10 notches per page
pub fn to_steps(self) -> i32 {
match self {
ScrollDelta::Pixels(px) => px / 100,
Copy link
Member

@junkdog junkdog Jan 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

px here leads me to believe this is in pixel units, but it's actually converted to rows (or cols) from pixels. (pages is similarly suspect) i can't read today

where did you get 100 from? i think this value could vary across devices. we probably have to measure it if we care about pixels to rows/cols

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its claude suggested and I approximately tried to verify that and "pages" to approximate 10 wheel events to scroll one page view, etc. But yes, I agree, they are arbitrary, I suspected this would be a problem.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i tried with:

document.addEventListener('wheel', (e) => {
    console.log({
      deltaX: e.deltaX,
      deltaY: e.deltaY,
      deltaMode: ['PIXEL', 'LINE', 'PAGE'][e.deltaMode],
      ctrlKey: e.ctrlKey,
      shiftKey: e.shiftKey
    });
  });

depending on how quick i scroll the wheel, on at 27" 1440p screen, runing kde/wayland:

  • firefox: values range between 16-70px,
  • chromium: 16-360(!)

so 100 is definitely too high, esp for ff.

// Create ScrollDelta based on the browser's delta mode
let to_scroll_delta = |delta: f64| -> ScrollDelta {
let delta_int = delta as i32;
match delta_mode {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a thought, but the more i think about it: would it maybe be easier if we treated all scroll events as either -1 or +1 (or some other increment)? i don't really see pixel-perfect scrolling translating to TUIs. this would omit the need for resolving actual values behind ScrollDelta::Pages and ::Pixels (+ the need to intercept when said values change)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's how I use it, but thought it better left to the user to decide if they want to use that information or discard it.

callback(event.into());
});
let window = window().unwrap();
let document = window.document().unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use get_document() to retrieve it with proper error handling (i know the other on_*_event functions do not, feel free to update them too)

https://github.com/orhun/ratzilla/blob/82d9438e0b7afdedf7f2f980de94a2f6eb207e65/src/backend/utils.rs#L144-L148

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants