Skip to content

Commit

Permalink
Unify scroll and hscroll
Browse files Browse the repository at this point in the history
  • Loading branch information
zaynetro committed Apr 24, 2019
1 parent 9fbcda0 commit 2cf48ec
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 32 deletions.
11 changes: 9 additions & 2 deletions druid-shell/src/mac/mod.rs
Expand Up @@ -309,8 +309,15 @@ extern "C" fn scroll_wheel(this: &mut Object, _: Sel, nsevent: id) {
unsafe {
let view_state: *mut c_void = *this.get_ivar("viewState");
let view_state = &mut *(view_state as *mut ViewState);
let dx = nsevent.scrollingDeltaX() as i32;
let dy = nsevent.scrollingDeltaY() as i32;
let (dx, dy) = {
let dx = nsevent.scrollingDeltaX() as i32;
let dy = nsevent.scrollingDeltaY() as i32;
if nsevent.hasPreciseScrollingDeltas() == cocoa::base::YES {
(dx, dy)
} else {
(dx * 32, dy * 32)
}
};
let mods = 0; // TODO:
if dx != 0 {
(*view_state)
Expand Down
11 changes: 11 additions & 0 deletions druid-shell/src/window.rs
Expand Up @@ -165,3 +165,14 @@ pub enum Cursor {
Arrow,
IBeam,
}

/// A scroll wheel event.
#[derive(Debug)]
pub struct ScrollEvent {
/// The scroll wheel’s horizontal delta.
pub dx: f32,
/// The scroll wheel’s vertical delta.
pub dy: f32,
/// Modifiers, as in raw WM message
pub mods: u32,
}
12 changes: 4 additions & 8 deletions examples/mouse.rs
Expand Up @@ -28,7 +28,7 @@ use druid_shell::win_main;

use druid::{Ui, UiMain, UiState};

use druid::widget::Widget;
use druid::widget::{Widget, ScrollEvent};
use druid::HandlerCtx;
use druid::{BoxConstraints, Geometry, LayoutResult};
use druid::{Id, LayoutCtx, PaintCtx};
Expand Down Expand Up @@ -69,13 +69,9 @@ impl Widget for FooWidget {
LayoutResult::Size(bc.constrain((100.0, 100.0)))
}

fn scroll(&mut self, dy: f32, ctx: &mut HandlerCtx) {
self.size.1 += dy as f64;
ctx.invalidate();
}

fn hscroll(&mut self, dx: f32, ctx: &mut HandlerCtx) {
self.size.0 += dx as f64;
fn scroll(&mut self, event: &ScrollEvent, ctx: &mut HandlerCtx) {
self.size.0 += event.dx as f64;
self.size.1 += event.dy as f64;
ctx.invalidate();
}

Expand Down
31 changes: 14 additions & 17 deletions src/lib.rs
Expand Up @@ -417,24 +417,13 @@ impl UiState {
}
}

fn handle_scroll(&mut self, dy: f32) {
fn handle_scroll(&mut self, event: &window::ScrollEvent) {
if let Some(id) = self.layout_ctx.hot {
let mut ctx = HandlerCtx {
id,
layout_ctx: &mut self.inner.layout_ctx,
};
self.inner.widgets[id].scroll(dy, &mut ctx);
self.dispatch_events();
}
}

fn handle_hscroll(&mut self, dx: f32) {
if let Some(id) = self.layout_ctx.hot {
let mut ctx = HandlerCtx {
id,
layout_ctx: &mut self.inner.layout_ctx,
};
self.inner.widgets[id].hscroll(dx, &mut ctx);
self.inner.widgets[id].scroll(event, &mut ctx);
self.dispatch_events();
}
}
Expand Down Expand Up @@ -948,14 +937,22 @@ impl WinHandler for UiMain {
state.handle_key_event(&key_event)
}

fn mouse_wheel(&self, delta: i32, _mods: u32) {
fn mouse_wheel(&self, dy: i32, mods: u32) {
let mut state = self.state.borrow_mut();
state.handle_scroll(delta as f32);
state.handle_scroll(&window::ScrollEvent{
dx: 0.0,
dy: dy as f32,
mods,
});
}

fn mouse_hwheel(&self, delta: i32, _mods: u32) {
fn mouse_hwheel(&self, dx: i32, mods: u32) {
let mut state = self.state.borrow_mut();
state.handle_hscroll(delta as f32);
state.handle_scroll(&window::ScrollEvent{
dx: dx as f32,
dy: 0.0,
mods,
});
}

fn mouse_move(&self, x: i32, y: i32, _mods: u32) {
Expand Down
7 changes: 2 additions & 5 deletions src/widget/mod.rs
Expand Up @@ -16,7 +16,7 @@

use std::any::Any;

pub use druid_shell::window::MouseButton;
pub use druid_shell::window::{MouseButton, ScrollEvent};

use crate::{BoxConstraints, Geometry, LayoutResult};
use crate::{HandlerCtx, Id, LayoutCtx, PaintCtx};
Expand Down Expand Up @@ -112,10 +112,7 @@ pub trait Widget {
}

#[allow(unused)]
fn scroll(&mut self, dy: f32, ctx: &mut HandlerCtx) {}

#[allow(unused)]
fn hscroll(&mut self, dx: f32, ctx: &mut HandlerCtx) {}
fn scroll(&mut self, event: &ScrollEvent, ctx: &mut HandlerCtx) {}

/// Called at the beginning of a new animation frame.
///
Expand Down

0 comments on commit 2cf48ec

Please sign in to comment.