Skip to content

Commit

Permalink
Simplify Scale by removing DPI. (#1042)
Browse files Browse the repository at this point in the history
  • Loading branch information
xStrom authored Jun 18, 2020
1 parent fdba642 commit d810d30
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 107 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ You can find its changes [documented below](#060---2020-06-01).
### Changed

- `Image` and `ImageData` exported by default. ([#1011] by [@covercash2])
- `Scale::from_scale` to `Scale::new`, and `Scale` methods `scale_x` / `scale_y` to `x` / `y`. ([#1042] by [@xStrom])

### Deprecated

### Removed

- `Scale::from_dpi`, `Scale::dpi_x`, and `Scale::dpi_y`. ([#1042] by [@xStrom])

### Fixed

- macOS: Timers not firing during modal loop. ([#1028] by [@xStrom])
Expand Down Expand Up @@ -324,6 +327,7 @@ Last release without a changelog :(
[#1011]: https://github.com/xi-editor/druid/pull/1011
[#1013]: https://github.com/xi-editor/druid/pull/1013
[#1028]: https://github.com/xi-editor/druid/pull/1028
[#1042]: https://github.com/xi-editor/druid/pull/1042

[Unreleased]: https://github.com/xi-editor/druid/compare/v0.6.0...master
[0.6.0]: https://github.com/xi-editor/druid/compare/v0.5.0...v0.6.0
Expand Down
24 changes: 15 additions & 9 deletions druid-shell/src/platform/gtk/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ use super::dialog;
use super::menu::Menu;
use super::util;

/// The platform target DPI.
///
/// GTK considers 96 the default value which represents a 1.0 scale factor.
const SCALE_TARGET_DPI: f64 = 96.0;

/// Taken from https://gtk-rs.org/docs-src/tutorial/closures
/// It is used to reduce the boilerplate of setting up gtk callbacks
/// Example:
Expand Down Expand Up @@ -169,12 +174,13 @@ impl WindowBuilder {
window.set_resizable(self.resizable);
window.set_decorated(self.show_titlebar);

// Get the GTK reported DPI
let dpi = window
// Get the scale factor based on the GTK reported DPI
let scale_factor = window
.get_display()
.map(|c| c.get_default_screen().get_resolution() as f64)
.unwrap_or(96.0);
let scale = Scale::from_dpi(dpi, dpi);
.map(|c| c.get_default_screen().get_resolution())
.unwrap_or(SCALE_TARGET_DPI)
/ SCALE_TARGET_DPI;
let scale = Scale::new(scale_factor, scale_factor);
let area = ScaledArea::from_dp(self.size, &scale);
let size_px = area.size_px();

Expand Down Expand Up @@ -254,9 +260,9 @@ impl WindowBuilder {
let mut scale_changed = false;
// Check if the GTK reported DPI has changed,
// so that we can change our scale factor without restarting the application.
if let Some(dpi) = state.window.get_window()
.map(|w| w.get_display().get_default_screen().get_resolution()) {
let reported_scale = Scale::from_dpi(dpi, dpi);
if let Some(scale_factor) = state.window.get_window()
.map(|w| w.get_display().get_default_screen().get_resolution() / SCALE_TARGET_DPI) {
let reported_scale = Scale::new(scale_factor, scale_factor);
if scale != reported_scale {
scale = reported_scale;
state.scale.set(scale);
Expand Down Expand Up @@ -286,7 +292,7 @@ impl WindowBuilder {
if let Ok(mut handler_borrow) = state.handler.try_borrow_mut() {
// For some reason piet needs a mutable context, so give it one I guess.
let mut context = context.clone();
context.scale(scale.scale_x(), scale.scale_y());
context.scale(scale.x(), scale.y());
let (x0, y0, x1, y1) = context.clip_extents();
let invalid_rect = Rect::new(x0, y0, x1, y1);

Expand Down
2 changes: 1 addition & 1 deletion druid-shell/src/platform/mac/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,7 @@ impl WindowHandle {
/// Get the `Scale` of the window.
pub fn get_scale(&self) -> Result<Scale, Error> {
// TODO: Get actual Scale
Ok(Scale::from_dpi(96.0, 96.0))
Ok(Scale::new(1.0, 1.0))
}
}

Expand Down
8 changes: 4 additions & 4 deletions druid-shell/src/platform/web/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,12 @@ impl WindowState {
/// Updates the canvas size and scale factor and returns `Scale` and `ScaledArea`.
fn update_scale_and_area(&self) -> (Scale, ScaledArea) {
let (css_width, css_height, dpr) = self.get_window_size_and_dpr();
let scale = Scale::from_scale(dpr, dpr);
let scale = Scale::new(dpr, dpr);
let area = ScaledArea::from_dp(Size::new(css_width, css_height), &scale);
let size_px = area.size_px();
self.canvas.set_width(size_px.width as u32);
self.canvas.set_height(size_px.height as u32);
let _ = self.context.scale(scale.scale_x(), scale.scale_y());
let _ = self.context.scale(scale.x(), scale.y());
self.scale.set(scale);
self.area.set(area);
(scale, area)
Expand Down Expand Up @@ -373,7 +373,7 @@ impl WindowBuilder {
// Create the Scale for resolution scaling
let scale = {
let dpr = window.device_pixel_ratio();
Scale::from_scale(dpr, dpr)
Scale::new(dpr, dpr)
};
let area = {
// The initial size in display points isn't necessarily the final size in display points
Expand All @@ -383,7 +383,7 @@ impl WindowBuilder {
let size_px = area.size_px();
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 _ = context.scale(scale.x(), scale.y());
let size_dp = area.size_dp();

set_cursor(&canvas, &self.cursor);
Expand Down
5 changes: 3 additions & 2 deletions druid-shell/src/platform/windows/paint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use crate::scale::Scale;

use super::error::Error;
use super::util::as_result;
use super::window::SCALE_TARGET_DPI;

pub(crate) unsafe fn create_render_target(
d2d_factory: &D2DFactory,
Expand Down Expand Up @@ -82,8 +83,8 @@ pub(crate) unsafe fn create_render_target_dxgi(
format: DXGI_FORMAT_B8G8R8A8_UNORM,
alphaMode: D2D1_ALPHA_MODE_IGNORE,
},
dpiX: scale.dpi_x() as f32,
dpiY: scale.dpi_y() as f32,
dpiX: (scale.x() * SCALE_TARGET_DPI) as f32,
dpiY: (scale.y() * SCALE_TARGET_DPI) as f32,
usage: D2D1_RENDER_TARGET_USAGE_NONE,
minLevel: D2D1_FEATURE_LEVEL_DEFAULT,
};
Expand Down
15 changes: 10 additions & 5 deletions druid-shell/src/platform/windows/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ use crate::mouse::{Cursor, MouseButton, MouseButtons, MouseEvent};
use crate::scale::{Scale, ScaledArea};
use crate::window::{IdleToken, Text, TimerToken, WinHandler};

/// The platform target DPI.
///
/// Windows considers 96 the default value which represents a 1.0 scale factor.
pub(crate) const SCALE_TARGET_DPI: f64 = 96.0;

extern "system" {
pub fn DwmFlush();
}
Expand Down Expand Up @@ -1026,16 +1031,16 @@ impl WindowBuilder {
present_strategy: self.present_strategy,
};

// Simple scaling based on System DPI (96 is equivalent to 100%)
let dpi = if let Some(func) = OPTIONAL_FUNCTIONS.GetDpiForSystem {
// Simple scaling based on System DPI
let scale_factor = if let Some(func) = OPTIONAL_FUNCTIONS.GetDpiForSystem {
// Only supported on Windows 10
func() as f64
func() as f64 / SCALE_TARGET_DPI
} else {
// TODO GetDpiForMonitor is supported on Windows 8.1, try falling back to that here
// Probably GetDeviceCaps(..., LOGPIXELSX) is the best to do pre-10
96.0
1.0
};
let scale = Scale::from_dpi(dpi, dpi);
let scale = Scale::new(scale_factor, scale_factor);
let area = ScaledArea::from_dp(self.size, &scale);
let size_px = area.size_px();

Expand Down
4 changes: 2 additions & 2 deletions druid-shell/src/platform/x11/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ impl Window {

fn get_scale(&self) -> Result<Scale, Error> {
// TODO(x11/dpi_scaling): figure out DPI scaling
Ok(Scale::from_dpi(96.0, 96.0))
Ok(Scale::new(1.0, 1.0))
}

pub fn handle_expose(&self, expose: &xcb::ExposeEvent) -> Result<(), Error> {
Expand Down Expand Up @@ -820,7 +820,7 @@ impl WindowHandle {
Ok(w.get_scale()?)
} else {
log::error!("Window {} has already been dropped", self.id);
Ok(Scale::from_dpi(96.0, 96.0))
Ok(Scale::new(1.0, 1.0))
}
}
}
Loading

0 comments on commit d810d30

Please sign in to comment.