Skip to content

Commit

Permalink
WIP - Make EL2 DPI changes and implement on Windows (#895)
Browse files Browse the repository at this point in the history
* Modify DPI API publicly and on Windows

* Add generic Position and make dpi creation functions const

* Make examples work

* Fix fullscreen windows not appearing

* Replace Logical coordinates in window events with Physical coordinates

* Update HiDpiFactorChanged

* Document to_static
  • Loading branch information
Osspial committed Jun 24, 2019
1 parent a195ce8 commit 3a449af
Show file tree
Hide file tree
Showing 15 changed files with 414 additions and 363 deletions.
32 changes: 18 additions & 14 deletions examples/multithreaded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ extern crate env_logger;
use std::{collections::HashMap, sync::mpsc, thread, time::Duration};

use winit::{
dpi::{PhysicalPosition, PhysicalSize},
event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::{CursorIcon, WindowBuilder},
};

const WINDOW_COUNT: usize = 3;
const WINDOW_SIZE: (u32, u32) = (600, 400);
const WINDOW_SIZE: PhysicalSize = PhysicalSize::new(600, 400);

fn main() {
env_logger::init();
let event_loop = EventLoop::new();
let mut window_senders = HashMap::with_capacity(WINDOW_COUNT);
for _ in 0..WINDOW_COUNT {
let window = WindowBuilder::new()
.with_inner_size(WINDOW_SIZE.into())
.with_inner_size(WINDOW_SIZE)
.build(&event_loop)
.unwrap();
let (tx, rx) = mpsc::channel();
Expand Down Expand Up @@ -58,7 +59,7 @@ fn main() {
println!("-> inner_size : {:?}", window.inner_size());
}
L => window.set_min_inner_size(match state {
true => Some(WINDOW_SIZE.into()),
true => Some(WINDOW_SIZE),
false => None,
}),
M => window.set_maximized(state),
Expand All @@ -71,17 +72,18 @@ fn main() {
}),
Q => window.request_redraw(),
R => window.set_resizable(state),
S => window.set_inner_size(
match state {
true => (WINDOW_SIZE.0 + 100, WINDOW_SIZE.1 + 100),
false => WINDOW_SIZE,
}
.into(),
),
S => window.set_inner_size(match state {
true => PhysicalSize::new(
WINDOW_SIZE.width + 100,
WINDOW_SIZE.height + 100,
),
false => WINDOW_SIZE,
}),
W => window
.set_cursor_position(
(WINDOW_SIZE.0 as i32 / 2, WINDOW_SIZE.1 as i32 / 2).into(),
)
.set_cursor_position(PhysicalPosition::new(
WINDOW_SIZE.width as f64 / 2.0,
WINDOW_SIZE.height as f64 / 2.0,
))
.unwrap(),
Z => {
window.set_visible(false);
Expand Down Expand Up @@ -117,7 +119,9 @@ fn main() {
}
_ => {
if let Some(tx) = window_senders.get(&window_id) {
tx.send(event).unwrap();
if let Some(event) = event.to_static() {
tx.send(event).unwrap();
}
}
}
},
Expand Down
3 changes: 2 additions & 1 deletion examples/resizable.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use winit::{
dpi::LogicalSize,
event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
Expand All @@ -11,7 +12,7 @@ fn main() {

let window = WindowBuilder::new()
.with_title("Hit space to toggle resizability.")
.with_inner_size((400, 200).into())
.with_inner_size(LogicalSize::new(400.0, 200.0))
.with_resizable(resizable)
.build(&event_loop)
.unwrap();
Expand Down
1 change: 1 addition & 0 deletions examples/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ fn main() {

let window = WindowBuilder::new()
.with_title("A fantastic window!")
.with_inner_size(winit::dpi::LogicalSize::new(128.0, 128.0))
.build(&event_loop)
.unwrap();

Expand Down
116 changes: 90 additions & 26 deletions src/dpi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub struct LogicalPosition {

impl LogicalPosition {
#[inline]
pub fn new(x: f64, y: f64) -> Self {
pub const fn new(x: f64, y: f64) -> Self {
LogicalPosition { x, y }
}

Expand Down Expand Up @@ -160,7 +160,7 @@ pub struct PhysicalPosition {

impl PhysicalPosition {
#[inline]
pub fn new(x: f64, y: f64) -> Self {
pub const fn new(x: f64, y: f64) -> Self {
PhysicalPosition { x, y }
}

Expand Down Expand Up @@ -221,7 +221,7 @@ pub struct LogicalSize {

impl LogicalSize {
#[inline]
pub fn new(width: f64, height: f64) -> Self {
pub const fn new(width: f64, height: f64) -> Self {
LogicalSize { width, height }
}

Expand All @@ -235,7 +235,7 @@ impl LogicalSize {
assert!(validate_hidpi_factor(dpi_factor));
let width = self.width * dpi_factor;
let height = self.height * dpi_factor;
PhysicalSize::new(width, height)
PhysicalSize::new(width.round() as _, height.round() as _)
}
}

Expand Down Expand Up @@ -269,20 +269,16 @@ impl Into<(u32, u32)> for LogicalSize {
}

/// A size represented in physical pixels.
///
/// The size is stored as floats, so please be careful. Casting floats to integers truncates the fractional part,
/// which can cause noticable issues. To help with that, an `Into<(u32, u32)>` implementation is provided which
/// does the rounding for you.
#[derive(Debug, Copy, Clone, PartialEq)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct PhysicalSize {
pub width: f64,
pub height: f64,
pub width: u32,
pub height: u32,
}

impl PhysicalSize {
#[inline]
pub fn new(width: f64, height: f64) -> Self {
pub const fn new(width: u32, height: u32) -> Self {
PhysicalSize { width, height }
}

Expand All @@ -294,37 +290,105 @@ impl PhysicalSize {
#[inline]
pub fn to_logical(&self, dpi_factor: f64) -> LogicalSize {
assert!(validate_hidpi_factor(dpi_factor));
let width = self.width / dpi_factor;
let height = self.height / dpi_factor;
let width = self.width as f64 / dpi_factor;
let height = self.height as f64 / dpi_factor;
LogicalSize::new(width, height)
}
}

impl From<(f64, f64)> for PhysicalSize {
impl From<(u32, u32)> for PhysicalSize {
#[inline]
fn from((width, height): (f64, f64)) -> Self {
fn from((width, height): (u32, u32)) -> Self {
Self::new(width, height)
}
}

impl From<(u32, u32)> for PhysicalSize {
impl Into<(u32, u32)> for PhysicalSize {
/// Note that this rounds instead of truncating.
#[inline]
fn from((width, height): (u32, u32)) -> Self {
Self::new(width as f64, height as f64)
fn into(self) -> (u32, u32) {
(self.width, self.height)
}
}

impl Into<(f64, f64)> for PhysicalSize {
#[derive(Debug, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Size {
Physical(PhysicalSize),
Logical(LogicalSize),
}

impl Size {
pub fn new<S: Into<Size>>(size: S) -> Size {
size.into()
}

pub fn to_logical(&self, dpi_factor: f64) -> LogicalSize {
match *self {
Size::Physical(size) => size.to_logical(dpi_factor),
Size::Logical(size) => size,
}
}

pub fn to_physical(&self, dpi_factor: f64) -> PhysicalSize {
match *self {
Size::Physical(size) => size,
Size::Logical(size) => size.to_physical(dpi_factor),
}
}
}

impl From<PhysicalSize> for Size {
#[inline]
fn into(self) -> (f64, f64) {
(self.width, self.height)
fn from(size: PhysicalSize) -> Size {
Size::Physical(size)
}
}

impl Into<(u32, u32)> for PhysicalSize {
/// Note that this rounds instead of truncating.
impl From<LogicalSize> for Size {
#[inline]
fn into(self) -> (u32, u32) {
(self.width.round() as _, self.height.round() as _)
fn from(size: LogicalSize) -> Size {
Size::Logical(size)
}
}

#[derive(Debug, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Position {
Physical(PhysicalPosition),
Logical(LogicalPosition),
}

impl Position {
pub fn new<S: Into<Position>>(position: S) -> Position {
position.into()
}

pub fn to_logical(&self, dpi_factor: f64) -> LogicalPosition {
match *self {
Position::Physical(position) => position.to_logical(dpi_factor),
Position::Logical(position) => position,
}
}

pub fn to_physical(&self, dpi_factor: f64) -> PhysicalPosition {
match *self {
Position::Physical(position) => position,
Position::Logical(position) => position.to_physical(dpi_factor),
}
}
}

impl From<PhysicalPosition> for Position {
#[inline]
fn from(position: PhysicalPosition) -> Position {
Position::Physical(position)
}
}

impl From<LogicalPosition> for Position {
#[inline]
fn from(position: LogicalPosition) -> Position {
Position::Logical(position)
}
}
Loading

0 comments on commit 3a449af

Please sign in to comment.