Skip to content

Commit

Permalink
Change points/pt to display points/dp.
Browse files Browse the repository at this point in the history
  • Loading branch information
xStrom committed May 9, 2020
1 parent aa17b14 commit 57eb993
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 143 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ While some features like the clipboard, menus or file dialogs are not yet availa
- Timer events will only be delivered to the widgets that requested them. ([#831] by [@sjoshid])
- `Event::Wheel` now contains a `MouseEvent` structure. ([#895] by [@teddemunnik])
- The `WindowHandle::get_dpi` method got replaced by `WindowHandle::get_scale`. ([#904] by [@xStrom])
- The `WinHandler::size` method now gets a `Size` in points. ([#904] by [@xStrom])
- The `WinHandler::size` method now gets a `Size` in display points. ([#904] by [@xStrom])

### Deprecated

Expand Down
2 changes: 1 addition & 1 deletion docs/src/widget.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ widgets].

Widgets are intended to be modular and composable, not monolithic. For instance,
widgets generally do not control their own alignment or padding; if you have
a label, and you would like it to have 8pt of horizontal padding and 4pt of
a label, and you would like it to have 8dp of horizontal padding and 4dp of
vertical padding, you can just do,

```rust,noplaypen
Expand Down
2 changes: 1 addition & 1 deletion druid-shell/src/mouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::keyboard::KeyModifiers;
/// Information about the mouse event.
#[derive(Debug, Clone, PartialEq)]
pub struct MouseEvent {
/// The location of the mouse in points in relation to the current window.
/// The location of the mouse in display points in relation to the current window.
pub pos: Point,
/// Mouse buttons being held down during a move or after a click event.
/// Thus it will contain the `button` that triggered a mouse-down event,
Expand Down
22 changes: 11 additions & 11 deletions druid-shell/src/platform/gtk/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ impl WindowBuilder {
.map(|c| c.get_default_screen().get_resolution() as f64)
.unwrap_or(96.0);
let mut scale = Scale::from_dpi(dpi, dpi);
let size_px = scale.set_size_pt(self.size);
let size_px = scale.set_size_dp(self.size);

window.set_default_size(size_px.width as i32, size_px.height as i32);

Expand Down Expand Up @@ -236,9 +236,9 @@ impl WindowBuilder {
});

// Set the minimum size
if let Some(size_pt) = self.min_size {
if let Some(size_dp) = self.min_size {
let mut scale = Scale::from_dpi(dpi, dpi);
let size_px = scale.set_size_pt(size_pt);
let size_px = scale.set_size_dp(size_dp);
win_state
.drawing_area
.set_size_request(size_px.width as i32, size_px.height as i32);
Expand All @@ -262,9 +262,9 @@ impl WindowBuilder {
let extents = widget.get_allocation();
let size_px = Size::new(extents.width as f64, extents.height as f64);
if scale.size_px() != size_px {
let size_pt = scale.set_size_px(size_px);
let size_dp = scale.set_size_px(size_px);
if let Ok(mut handler_borrow) = state.handler.try_borrow_mut() {
handler_borrow.size(size_pt);
handler_borrow.size(size_dp);
} else {
log::warn!("Failed to inform the handler of a resize because it was already borrowed");
}
Expand Down Expand Up @@ -306,7 +306,7 @@ impl WindowBuilder {
let button_state = event.get_state();
handler.mouse_down(
&MouseEvent {
pos: scale.px_to_pt_point(Point::from(event.get_position())),
pos: scale.to_dp(&Point::from(event.get_position())),
buttons: get_mouse_buttons_from_modifiers(button_state).with(button),
mods: get_modifiers(button_state),
count: get_mouse_click_count(event.get_event_type()),
Expand Down Expand Up @@ -334,7 +334,7 @@ impl WindowBuilder {
let button_state = event.get_state();
handler.mouse_up(
&MouseEvent {
pos: scale.px_to_pt_point(Point::from(event.get_position())),
pos: scale.to_dp(&Point::from(event.get_position())),
buttons: get_mouse_buttons_from_modifiers(button_state).without(button),
mods: get_modifiers(button_state),
count: 0,
Expand All @@ -359,7 +359,7 @@ impl WindowBuilder {
if let Ok(scale) = state.scale.try_borrow() {
let motion_state = motion.get_state();
let mouse_event = MouseEvent {
pos: scale.px_to_pt_point(Point::from(motion.get_position())),
pos: scale.to_dp(&Point::from(motion.get_position())),
buttons: get_mouse_buttons_from_modifiers(motion_state),
mods: get_modifiers(motion_state),
count: 0,
Expand All @@ -385,7 +385,7 @@ impl WindowBuilder {
if let Ok(scale) = state.scale.try_borrow() {
let crossing_state = crossing.get_state();
let mouse_event = MouseEvent {
pos: scale.px_to_pt_point(Point::from(crossing.get_position())),
pos: scale.to_dp(&Point::from(crossing.get_position())),
buttons: get_mouse_buttons_from_modifiers(crossing_state),
mods: get_modifiers(crossing_state),
count: 0,
Expand Down Expand Up @@ -443,7 +443,7 @@ impl WindowBuilder {

if let Some(wheel_delta) = wheel_delta{
let mouse_event = MouseEvent{
pos: scale.px_to_pt_point(Point::from(scroll.get_position())),
pos: scale.to_dp(&Point::from(scroll.get_position())),
buttons: get_mouse_buttons_from_modifiers(scroll.get_state()),
mods,
count: 0,
Expand Down Expand Up @@ -562,7 +562,7 @@ impl WindowHandle {
if let Some(state) = self.state.upgrade() {
if let Ok(scale) = state.scale.try_borrow() {
// GTK takes rects with non-negative integer width/height.
let r = scale::expand_rect(scale.pt_to_px_rect(rect.abs()));
let r = scale::expand_rect(scale.to_px(&rect.abs()));
let origin = state.drawing_area.get_allocation();
state.window.queue_draw_area(
r.x0 as i32 + origin.x,
Expand Down
22 changes: 11 additions & 11 deletions druid-shell/src/platform/web/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,16 +216,16 @@ fn setup_resize_callback(ws: &Rc<WindowState>) {
let state = ws.clone();
register_window_event_listener(ws, "resize", move |_: web_sys::UiEvent| {
let (css_width, css_height, dpr) = state.get_window_size_and_dpr();
let size_pt = state.scale.try_borrow_mut().map_or(None, |mut scale| {
let size_dp = state.scale.try_borrow_mut().map_or(None, |mut scale| {
*scale = Scale::from_scale(dpr, dpr);
let size_px = scale.set_size_pt(Size::new(css_width, css_height));
let size_px = scale.set_size_dp(Size::new(css_width, css_height));
state.canvas.set_width(size_px.width as u32);
state.canvas.set_height(size_px.height as u32);
let _ = state.context.scale(scale.scale_x(), scale.scale_y());
Some(scale.size_pt())
Some(scale.size_dp())
});
if let Some(size_pt) = size_pt {
state.handler.borrow_mut().size(size_pt);
if let Some(size_dp) = size_dp {
state.handler.borrow_mut().size(size_dp);
} else {
log::error!("Skipped resize event because couldn't borrow scale");
}
Expand Down Expand Up @@ -359,14 +359,14 @@ impl WindowBuilder {
Scale::from_scale(dpr, dpr)
};
let size_px = {
// The initial size in points isn't necessarily the final size in points
let size_pt = Size::new(canvas.offset_width() as f64, canvas.offset_height() as f64);
scale.set_size_pt(size_pt)
// The initial size in display points isn't necessarily the final size in display points
let size_dp = Size::new(canvas.offset_width() as f64, canvas.offset_height() as f64);
scale.set_size_dp(size_dp)
};
canvas.set_width(size_px.width as u32);
canvas.set_height(size_px.height as u32);
let _ = context.scale(scale.scale_x(), scale.scale_y());
let size_pt = scale.size_pt();
let size_dp = scale.size_dp();

set_cursor(&canvas, &self.cursor);

Expand All @@ -388,7 +388,7 @@ impl WindowBuilder {
let wh = window.clone();
window
.request_animation_frame(move || {
wh.handler.borrow_mut().size(size_pt);
wh.handler.borrow_mut().size(size_dp);
})
.expect("Failed to request animation frame");

Expand Down Expand Up @@ -436,7 +436,7 @@ impl WindowHandle {
pub fn invalidate(&self) {
if let Some(s) = self.0.upgrade() {
if let Ok(scale) = s.scale.try_borrow() {
s.invalid_rect.set(scale.size_pt().to_rect());
s.invalid_rect.set(scale.size_dp().to_rect());
} else {
log::error!("Failed to get scale");
}
Expand Down
47 changes: 26 additions & 21 deletions druid-shell/src/platform/windows/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,13 +453,12 @@ impl WndProc for MyWndProc {
s.render_target = rt.ok();
}
s.handler.rebuild_resources();
let rect_pt =
self.with_scale(|scale| scale.px_to_pt_rect(util::recti_to_rect(rect)));
let rect_dp = self.with_scale(|scale| scale.to_dp(&util::recti_to_rect(rect)));
s.render(
&self.d2d_factory,
&self.dwrite_factory,
&self.handle,
rect_pt,
rect_dp,
);
if let Some(ref mut ds) = s.dcomp_state {
let params = DXGI_PRESENT_PARAMETERS {
Expand All @@ -486,13 +485,13 @@ impl WndProc for MyWndProc {
let rt = paint::create_render_target(&self.d2d_factory, hwnd);
s.render_target = rt.ok();
{
let rect_pt = self.with_scale(|scale| scale.size_pt().to_rect());
let rect_dp = self.with_scale(|scale| scale.size_dp().to_rect());
s.handler.rebuild_resources();
s.render(
&self.d2d_factory,
&self.dwrite_factory,
&self.handle,
rect_pt,
rect_dp,
);
}

Expand Down Expand Up @@ -527,7 +526,7 @@ impl WndProc for MyWndProc {
&self.d2d_factory,
&self.dwrite_factory,
&self.handle,
scale.size_pt().to_rect(),
scale.size_dp().to_rect(),
);
(*s.dcomp_state.as_ref().unwrap().swap_chain).Present(0, 0);
} else {
Expand All @@ -554,9 +553,9 @@ impl WndProc for MyWndProc {
let s = s.as_mut().unwrap();
let width = LOWORD(lparam as u32) as u32;
let height = HIWORD(lparam as u32) as u32;
let size_pt = self
let size_dp = self
.with_scale_mut(|scale| scale.set_size_px((width as f64, height as f64)));
s.handler.size(size_pt);
s.handler.size(size_dp);
let use_hwnd = if let Some(ref dcomp_state) = s.dcomp_state {
dcomp_state.sizing
} else {
Expand Down Expand Up @@ -588,11 +587,16 @@ impl WndProc for MyWndProc {
);
}
if SUCCEEDED(res) {
let rect = size_pt.to_rect();
let rect_dp = size_dp.to_rect();
self.with_scale(|scale| {
s.rebuild_render_target(&self.d2d_factory, scale)
});
s.render(&self.d2d_factory, &self.dwrite_factory, &self.handle, rect);
s.render(
&self.d2d_factory,
&self.dwrite_factory,
&self.handle,
rect_dp,
);
if let Some(ref mut dcomp_state) = s.dcomp_state {
(*dcomp_state.swap_chain).Present(0, 0);
let _ = dcomp_state.dcomp_device.commit();
Expand Down Expand Up @@ -722,7 +726,7 @@ impl WndProc for MyWndProc {
}

let pos =
self.with_scale(|scale| scale.px_to_pt_point((p.x as f64, p.y as f64)));
self.with_scale(|scale| scale.to_dp(&(p.x as f64, p.y as f64).into()));
let buttons = get_buttons(down_state);
let event = MouseEvent {
pos,
Expand Down Expand Up @@ -767,7 +771,7 @@ impl WndProc for MyWndProc {
}
}

let pos = self.with_scale(|scale| scale.px_to_pt_point((x as f64, y as f64)));
let pos = self.with_scale(|scale| scale.to_dp(&(x as f64, y as f64).into()));
let mods = KeyModifiers {
shift: wparam & MK_SHIFT != 0,
alt: get_mod_state_alt(),
Expand Down Expand Up @@ -834,7 +838,7 @@ impl WndProc for MyWndProc {
let x = LOWORD(lparam as u32) as i16 as i32;
let y = HIWORD(lparam as u32) as i16 as i32;
let pos =
self.with_scale(|scale| scale.px_to_pt_point((x as f64, y as f64)));
self.with_scale(|scale| scale.to_dp(&(x as f64, y as f64).into()));
let mods = KeyModifiers {
shift: wparam & MK_SHIFT != 0,
alt: get_mod_state_alt(),
Expand Down Expand Up @@ -919,9 +923,9 @@ impl WndProc for MyWndProc {
let min_max_info = unsafe { &mut *(lparam as *mut MINMAXINFO) };
if let Ok(s) = self.state.try_borrow() {
let s = s.as_ref().unwrap();
if let Some(min_size_pt) = s.min_size {
if let Some(min_size_dp) = s.min_size {
let min_size_px = self.with_scale(|scale| {
Scale::from_dpi(scale.dpi_x(), scale.dpi_y()).set_size_pt(min_size_pt)
Scale::from_dpi(scale.dpi_x(), scale.dpi_y()).set_size_dp(min_size_dp)
});
min_max_info.ptMinTrackSize.x = min_size_px.width as i32;
min_max_info.ptMinTrackSize.y = min_size_px.height as i32;
Expand Down Expand Up @@ -1020,7 +1024,7 @@ impl WindowBuilder {
96.0
};
let mut scale = Scale::from_dpi(dpi, dpi);
let size_px = scale.set_size_pt(self.size);
let size_px = scale.set_size_dp(self.size);

let window = WindowState {
hwnd: Cell::new(0 as HWND),
Expand Down Expand Up @@ -1322,10 +1326,11 @@ impl WindowHandle {

pub fn invalidate_rect(&self, rect: Rect) {
if let Some(w) = self.state.upgrade() {
let rect =
w.scale.try_borrow().ok().map(|scale| {
util::rect_to_recti(scale::expand_rect(scale.pt_to_px_rect(rect)))
});
let rect = w
.scale
.try_borrow()
.ok()
.map(|scale| util::rect_to_recti(scale::expand_rect(scale.to_px(&rect))));
let hwnd = w.hwnd.get();
unsafe {
let result = match rect {
Expand Down Expand Up @@ -1412,7 +1417,7 @@ impl WindowHandle {
let hmenu = menu.into_hmenu();
if let Some(w) = self.state.upgrade() {
let hwnd = w.hwnd.get();
let pos = w.scale.borrow().pt_to_px_point(pos).round();
let pos = w.scale.borrow().to_px(&pos).round();
unsafe {
let mut point = POINT {
x: pos.x as i32,
Expand Down
Loading

0 comments on commit 57eb993

Please sign in to comment.