Implement an opaque Size
type
It immutably ties physical and logical sizes to a specific scale factor.
This commit is contained in:
parent
1d71f78f90
commit
fbc9deb424
@ -158,21 +158,20 @@ pub trait Application: Sized {
|
|||||||
window_builder.build(&event_loop).expect("Open window")
|
window_builder.build(&event_loop).expect("Open window")
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut scale_factor = window.scale_factor();
|
let mut size = Size::new(window.inner_size(), window.scale_factor());
|
||||||
let mut size = window.inner_size().to_logical::<f64>(scale_factor);
|
|
||||||
let mut resized = false;
|
let mut resized = false;
|
||||||
|
|
||||||
let clipboard = Clipboard::new(&window);
|
let clipboard = Clipboard::new(&window);
|
||||||
let mut renderer = Self::Renderer::new(renderer_settings);
|
let mut renderer = Self::Renderer::new(renderer_settings);
|
||||||
|
|
||||||
let mut target = {
|
let mut target = {
|
||||||
let (width, height) = to_physical(size, scale_factor);
|
let physical_size = size.physical();
|
||||||
|
|
||||||
<Self::Renderer as window::Renderer>::Target::new(
|
<Self::Renderer as window::Renderer>::Target::new(
|
||||||
&window,
|
&window,
|
||||||
width,
|
physical_size.width,
|
||||||
height,
|
physical_size.height,
|
||||||
scale_factor as f32,
|
size.scale_factor() as f32,
|
||||||
&renderer,
|
&renderer,
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
@ -181,7 +180,7 @@ pub trait Application: Sized {
|
|||||||
&mut application,
|
&mut application,
|
||||||
Cache::default(),
|
Cache::default(),
|
||||||
&mut renderer,
|
&mut renderer,
|
||||||
size,
|
size.logical(),
|
||||||
&mut debug,
|
&mut debug,
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -213,7 +212,7 @@ pub trait Application: Sized {
|
|||||||
&mut application,
|
&mut application,
|
||||||
cache.take().unwrap(),
|
cache.take().unwrap(),
|
||||||
&mut renderer,
|
&mut renderer,
|
||||||
size,
|
size.logical(),
|
||||||
&mut debug,
|
&mut debug,
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -285,7 +284,7 @@ pub trait Application: Sized {
|
|||||||
&mut application,
|
&mut application,
|
||||||
temp_cache,
|
temp_cache,
|
||||||
&mut renderer,
|
&mut renderer,
|
||||||
size,
|
size.logical(),
|
||||||
&mut debug,
|
&mut debug,
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -305,12 +304,12 @@ pub trait Application: Sized {
|
|||||||
debug.render_started();
|
debug.render_started();
|
||||||
|
|
||||||
if resized {
|
if resized {
|
||||||
let (width, height) = to_physical(size, scale_factor);
|
let physical_size = size.physical();
|
||||||
|
|
||||||
target.resize(
|
target.resize(
|
||||||
width,
|
physical_size.width,
|
||||||
height,
|
physical_size.height,
|
||||||
scale_factor as f32,
|
size.scale_factor() as f32,
|
||||||
&renderer,
|
&renderer,
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -338,11 +337,11 @@ pub trait Application: Sized {
|
|||||||
..
|
..
|
||||||
} => match window_event {
|
} => match window_event {
|
||||||
WindowEvent::Resized(new_size) => {
|
WindowEvent::Resized(new_size) => {
|
||||||
size = new_size.to_logical(scale_factor);
|
size = Size::new(new_size, size.scale_factor());
|
||||||
|
|
||||||
events.push(Event::Window(window::Event::Resized {
|
events.push(Event::Window(window::Event::Resized {
|
||||||
width: size.width.round() as u32,
|
width: size.logical().width.round() as u32,
|
||||||
height: size.height.round() as u32,
|
height: size.logical().height.round() as u32,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
resized = true;
|
resized = true;
|
||||||
@ -351,7 +350,8 @@ pub trait Application: Sized {
|
|||||||
*control_flow = ControlFlow::Exit;
|
*control_flow = ControlFlow::Exit;
|
||||||
}
|
}
|
||||||
WindowEvent::CursorMoved { position, .. } => {
|
WindowEvent::CursorMoved { position, .. } => {
|
||||||
let position = position.to_logical::<f64>(scale_factor);
|
let position =
|
||||||
|
position.to_logical::<f64>(size.scale_factor());
|
||||||
|
|
||||||
events.push(Event::Mouse(mouse::Event::CursorMoved {
|
events.push(Event::Mouse(mouse::Event::CursorMoved {
|
||||||
x: position.x as f32,
|
x: position.x as f32,
|
||||||
@ -430,11 +430,8 @@ pub trait Application: Sized {
|
|||||||
WindowEvent::HoveredFileCancelled => {
|
WindowEvent::HoveredFileCancelled => {
|
||||||
events.push(Event::Window(window::Event::FilesHoveredLeft));
|
events.push(Event::Window(window::Event::FilesHoveredLeft));
|
||||||
}
|
}
|
||||||
WindowEvent::ScaleFactorChanged {
|
WindowEvent::ScaleFactorChanged { scale_factor, .. } => {
|
||||||
scale_factor: new_scale_factor,
|
size = Size::new(size.physical(), scale_factor);
|
||||||
..
|
|
||||||
} => {
|
|
||||||
scale_factor = new_scale_factor;
|
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
},
|
},
|
||||||
@ -465,7 +462,10 @@ fn build_user_interface<'a, A: Application>(
|
|||||||
debug.layout_started();
|
debug.layout_started();
|
||||||
let user_interface = UserInterface::build(
|
let user_interface = UserInterface::build(
|
||||||
view,
|
view,
|
||||||
Size::new(size.width.round() as f32, size.height.round() as f32),
|
iced_native::Size::new(
|
||||||
|
size.width.round() as f32,
|
||||||
|
size.height.round() as f32,
|
||||||
|
),
|
||||||
cache,
|
cache,
|
||||||
renderer,
|
renderer,
|
||||||
);
|
);
|
||||||
@ -474,15 +474,6 @@ fn build_user_interface<'a, A: Application>(
|
|||||||
user_interface
|
user_interface
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_physical(size: winit::dpi::LogicalSize<f64>, dpi: f64) -> (u32, u32) {
|
|
||||||
let physical_size = size.to_physical::<f64>(dpi);
|
|
||||||
|
|
||||||
(
|
|
||||||
physical_size.width.round() as u32,
|
|
||||||
physical_size.height.round() as u32,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// As defined in: http://www.unicode.org/faq/private_use.html
|
// As defined in: http://www.unicode.org/faq/private_use.html
|
||||||
fn is_private_use_character(c: char) -> bool {
|
fn is_private_use_character(c: char) -> bool {
|
||||||
match c {
|
match c {
|
||||||
|
@ -32,6 +32,7 @@ mod application;
|
|||||||
mod clipboard;
|
mod clipboard;
|
||||||
mod mode;
|
mod mode;
|
||||||
mod proxy;
|
mod proxy;
|
||||||
|
mod size;
|
||||||
|
|
||||||
// We disable debug capabilities on release builds unless the `debug` feature
|
// We disable debug capabilities on release builds unless the `debug` feature
|
||||||
// is explicitly enabled.
|
// is explicitly enabled.
|
||||||
@ -49,3 +50,4 @@ pub use settings::Settings;
|
|||||||
|
|
||||||
use debug::Debug;
|
use debug::Debug;
|
||||||
use proxy::Proxy;
|
use proxy::Proxy;
|
||||||
|
use size::Size;
|
||||||
|
30
winit/src/size.rs
Normal file
30
winit/src/size.rs
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
pub struct Size {
|
||||||
|
physical: winit::dpi::PhysicalSize<u32>,
|
||||||
|
logical: winit::dpi::LogicalSize<f64>,
|
||||||
|
scale_factor: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Size {
|
||||||
|
pub fn new(
|
||||||
|
physical: winit::dpi::PhysicalSize<u32>,
|
||||||
|
scale_factor: f64,
|
||||||
|
) -> Size {
|
||||||
|
Size {
|
||||||
|
logical: physical.to_logical(scale_factor),
|
||||||
|
physical,
|
||||||
|
scale_factor,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn physical(&self) -> winit::dpi::PhysicalSize<u32> {
|
||||||
|
self.physical
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn logical(&self) -> winit::dpi::LogicalSize<f64> {
|
||||||
|
self.logical
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn scale_factor(&self) -> f64 {
|
||||||
|
self.scale_factor
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user