Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement public API for high-DPI #319

Merged
merged 3 commits into from
Oct 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Unreleased

- Added event `WindowEvent::HiDPIFactorChanged`.
- Added method `MonitorId::get_hidpi_factor`.
- Deprecated `get_inner_size_pixels` and `get_inner_size_points` methods of `Window` in favor of
`get_inner_size`.

# Version 0.8.3 (2017-10-11)

- Fixed issue of calls to `set_inner_size` blocking on Windows.
Expand Down
9 changes: 9 additions & 0 deletions src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ pub enum WindowEvent {

/// Touch event has been received
Touch(Touch),

/// DPI scaling factor of the window has changed.
///
/// The following actions cause DPI changes:
///
/// * A user changes the resolution.
/// * A user changes the desktop scaling value (e.g. in Control Panel on Windows).
/// * A user moves the application window to a display with a different DPI.
HiDPIFactorChanged(f32),
}

/// Represents raw hardware events that are not associated with any particular window.
Expand Down
5 changes: 5 additions & 0 deletions src/platform/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ impl MonitorId {
// Android assumes single screen
(0, 0)
}

#[inline]
pub fn get_hidpi_factor(&self) -> f32 {
1.0
}
}

#[derive(Clone, Default)]
Expand Down
5 changes: 5 additions & 0 deletions src/platform/emscripten/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ impl MonitorId {
pub fn get_dimensions(&self) -> (u32, u32) {
(0, 0)
}

#[inline]
pub fn get_hidpi_factor(&self) -> f32 {
1.0
}
}

// Used to assign a callback to emscripten main loop
Expand Down
5 changes: 5 additions & 0 deletions src/platform/ios/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ impl MonitorId {
// iOS assumes single screen
(0, 0)
}

#[inline]
pub fn get_hidpi_factor(&self) -> f32 {
1.0
}
}

pub struct EventsLoop {
Expand Down
5 changes: 5 additions & 0 deletions src/platform/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ impl MonitorId {
&MonitorId::Wayland(ref m) => m.get_position(),
}
}

#[inline]
pub fn get_hidpi_factor(&self) -> f32 {
1.0
}
}

impl Window {
Expand Down
5 changes: 5 additions & 0 deletions src/platform/linux/wayland/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,11 @@ impl MonitorId {
// if we reach here, this monitor does not exist any more
(0,0)
}

#[inline]
pub fn get_hidpi_factor(&self) -> f32 {
1.0
}
}

// a handler to release the ressources acquired to draw the initial white screen as soon as
Expand Down
5 changes: 5 additions & 0 deletions src/platform/linux/x11/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,9 @@ impl MonitorId {
pub fn get_position(&self) -> (u32, u32) {
self.position
}

#[inline]
pub fn get_hidpi_factor(&self) -> f32 {
1.0
}
}
2 changes: 1 addition & 1 deletion src/platform/linux/x11/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ impl Window2 {

#[inline]
pub fn get_inner_size(&self) -> Option<(u32, u32)> {
self.get_geometry().map(|(_, _, w, h, _)| ((w as f32 / self.hidpi_factor()) as u32, (h as f32 / self.hidpi_factor()) as u32))
self.get_geometry().map(|(_, _, w, h, _)| (w, h))
}

#[inline]
Expand Down
5 changes: 5 additions & 0 deletions src/platform/macos/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,9 @@ impl MonitorId {
pub fn get_position(&self) -> (u32, u32) {
unimplemented!()
}

#[inline]
pub fn get_hidpi_factor(&self) -> f32 {
1.0
}
}
5 changes: 5 additions & 0 deletions src/platform/windows/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,9 @@ impl MonitorId {
pub fn get_position(&self) -> (u32, u32) {
self.position
}

#[inline]
pub fn get_hidpi_factor(&self) -> f32 {
1.0
}
}
37 changes: 23 additions & 14 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl WindowBuilder {
self
}

/// Enables multitouch
/// Enables multitouch.
#[inline]
pub fn with_multitouch(mut self) -> WindowBuilder {
self.window.multitouch = true;
Expand Down Expand Up @@ -183,22 +183,20 @@ impl Window {

/// Modifies the position of the window.
///
/// See `get_position` for more informations about the coordinates.
/// See `get_position` for more information about the coordinates.
///
/// This is a no-op if the window has already been closed.
#[inline]
pub fn set_position(&self, x: i32, y: i32) {
self.window.set_position(x, y)
}

/// Returns the size in points of the client area of the window.
/// Returns the size in pixels of the client area of the window.
///
/// The client area is the content of the window, excluding the title bar and borders.
/// To get the dimensions of the frame buffer when calling `glViewport`, multiply with hidpi factor.
/// These are the dimensions that need to be supplied to `glViewport`.
///
/// Returns `None` if the window no longer exists.
///
/// DEPRECATED
#[inline]
pub fn get_inner_size(&self) -> Option<(u32, u32)> {
self.window.get_inner_size()
Expand All @@ -210,25 +208,30 @@ impl Window {
/// To get the dimensions of the frame buffer when calling `glViewport`, multiply with hidpi factor.
///
/// Returns `None` if the window no longer exists.
///
/// DEPRECATED
#[inline]
#[deprecated]
pub fn get_inner_size_points(&self) -> Option<(u32, u32)> {
self.window.get_inner_size()
self.window.get_inner_size().map(|(x, y)| {
let hidpi = self.hidpi_factor();
((x as f32 / hidpi) as u32, (y as f32 / hidpi) as u32)
})
}


/// Returns the size in pixels of the client area of the window.
///
/// The client area is the content of the window, excluding the title bar and borders.
/// These are the dimensions of the frame buffer, and the dimensions that you should use
/// when you call `glViewport`.
///
/// Returns `None` if the window no longer exists.
///
/// DEPRECATED
#[inline]
#[deprecated]
pub fn get_inner_size_pixels(&self) -> Option<(u32, u32)> {
self.window.get_inner_size().map(|(x, y)| {
let hidpi = self.hidpi_factor();
((x as f32 * hidpi) as u32, (y as f32 * hidpi) as u32)
})
self.window.get_inner_size()
}

/// Returns the size in pixels of the window.
Expand All @@ -244,7 +247,7 @@ impl Window {

/// Modifies the inner size of the window.
///
/// See `get_inner_size` for more informations about the values.
/// See `get_inner_size` for more information about the values.
///
/// This is a no-op if the window has already been closed.
#[inline]
Expand Down Expand Up @@ -323,7 +326,7 @@ impl Window {
}

/// An iterator for the list of available monitors.
// Implementation note: we retreive the list once, then serve each element by one by one.
// Implementation note: we retrieve the list once, then serve each element by one by one.
// This may change in the future.
pub struct AvailableMonitorsIter {
pub(crate) data: VecDequeIter<platform::MonitorId>,
Expand Down Expand Up @@ -370,4 +373,10 @@ impl MonitorId {
pub fn get_position(&self) -> (u32, u32) {
self.inner.get_position()
}

/// Returns the ratio between the monitor's physical pixels and logical pixels.
#[inline]
pub fn get_hidpi_factor(&self) -> f32 {
self.inner.get_hidpi_factor()
}
}