Skip to content
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
23 changes: 13 additions & 10 deletions examples/multithreaded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@ extern crate winit;
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 @@ -55,7 +56,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 @@ -69,13 +70,13 @@ 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),
true => PhysicalSize::new(WINDOW_SIZE.width + 100, WINDOW_SIZE.height + 100),
false => WINDOW_SIZE,
}.into()),
W => window.set_cursor_position((
WINDOW_SIZE.0 as i32 / 2,
WINDOW_SIZE.1 as i32 / 2,
).into()).unwrap(),
}),
W => window.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);
thread::sleep(Duration::from_secs(1));
Expand Down Expand Up @@ -105,7 +106,9 @@ fn main() {
window_senders.remove(&window_id);
},
_ => 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,5 +1,6 @@
extern crate winit;
use winit::window::WindowBuilder;
use winit::dpi::LogicalSize;
use winit::event::{Event, WindowEvent, VirtualKeyCode, ElementState, KeyboardInput};
use winit::event_loop::{EventLoop, ControlFlow};

Expand All @@ -10,7 +11,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 @@ -8,6 +8,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
117 changes: 91 additions & 26 deletions src/dpi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,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 @@ -161,7 +161,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 @@ -222,7 +222,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 @@ -236,7 +236,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 @@ -270,20 +270,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 @@ -295,37 +291,106 @@ 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)
}
}

#[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 Into<(f64, f64)> for PhysicalSize {
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