Update winit to 0.20
This commit is contained in:
parent
4e9e051caa
commit
fb4a7968ca
7 changed files with 92 additions and 71 deletions
|
|
@ -39,9 +39,9 @@ pub trait Target {
|
||||||
/// [`Target`]: trait.Target.html
|
/// [`Target`]: trait.Target.html
|
||||||
fn new<W: HasRawWindowHandle>(
|
fn new<W: HasRawWindowHandle>(
|
||||||
window: &W,
|
window: &W,
|
||||||
width: u16,
|
width: u32,
|
||||||
height: u16,
|
height: u32,
|
||||||
dpi: f32,
|
scale_factor: f32,
|
||||||
renderer: &Self::Renderer,
|
renderer: &Self::Renderer,
|
||||||
) -> Self;
|
) -> Self;
|
||||||
|
|
||||||
|
|
@ -50,9 +50,9 @@ pub trait Target {
|
||||||
/// [`Target`]: trait.Target.html
|
/// [`Target`]: trait.Target.html
|
||||||
fn resize(
|
fn resize(
|
||||||
&mut self,
|
&mut self,
|
||||||
width: u16,
|
width: u32,
|
||||||
height: u16,
|
height: u32,
|
||||||
dpi: f32,
|
scale_factor: f32,
|
||||||
renderer: &Self::Renderer,
|
renderer: &Self::Renderer,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -92,7 +92,7 @@ impl Renderer {
|
||||||
log::debug!("Drawing");
|
log::debug!("Drawing");
|
||||||
|
|
||||||
let (width, height) = target.dimensions();
|
let (width, height) = target.dimensions();
|
||||||
let dpi = target.dpi();
|
let scale_factor = target.scale_factor();
|
||||||
let transformation = target.transformation();
|
let transformation = target.transformation();
|
||||||
let frame = target.next_frame();
|
let frame = target.next_frame();
|
||||||
|
|
||||||
|
|
@ -132,7 +132,13 @@ impl Renderer {
|
||||||
self.draw_overlay(overlay, &mut layers);
|
self.draw_overlay(overlay, &mut layers);
|
||||||
|
|
||||||
for layer in layers {
|
for layer in layers {
|
||||||
self.flush(dpi, transformation, &layer, &mut encoder, &frame.view);
|
self.flush(
|
||||||
|
scale_factor,
|
||||||
|
transformation,
|
||||||
|
&layer,
|
||||||
|
&mut encoder,
|
||||||
|
&frame.view,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.queue.submit(&[encoder.finish()]);
|
self.queue.submit(&[encoder.finish()]);
|
||||||
|
|
@ -330,19 +336,19 @@ impl Renderer {
|
||||||
|
|
||||||
fn flush(
|
fn flush(
|
||||||
&mut self,
|
&mut self,
|
||||||
dpi: f32,
|
scale_factor: f32,
|
||||||
transformation: Transformation,
|
transformation: Transformation,
|
||||||
layer: &Layer<'_>,
|
layer: &Layer<'_>,
|
||||||
encoder: &mut wgpu::CommandEncoder,
|
encoder: &mut wgpu::CommandEncoder,
|
||||||
target: &wgpu::TextureView,
|
target: &wgpu::TextureView,
|
||||||
) {
|
) {
|
||||||
let bounds = layer.bounds * dpi;
|
let bounds = layer.bounds * scale_factor;
|
||||||
|
|
||||||
if layer.meshes.len() > 0 {
|
if layer.meshes.len() > 0 {
|
||||||
let translated = transformation
|
let translated = transformation
|
||||||
* Transformation::translate(
|
* Transformation::translate(
|
||||||
-(layer.offset.x as f32) * dpi,
|
-(layer.offset.x as f32) * scale_factor,
|
||||||
-(layer.offset.y as f32) * dpi,
|
-(layer.offset.y as f32) * scale_factor,
|
||||||
);
|
);
|
||||||
|
|
||||||
self.triangle_pipeline.draw(
|
self.triangle_pipeline.draw(
|
||||||
|
|
@ -350,7 +356,7 @@ impl Renderer {
|
||||||
encoder,
|
encoder,
|
||||||
target,
|
target,
|
||||||
translated,
|
translated,
|
||||||
dpi,
|
scale_factor,
|
||||||
&layer.meshes,
|
&layer.meshes,
|
||||||
bounds,
|
bounds,
|
||||||
);
|
);
|
||||||
|
|
@ -362,7 +368,7 @@ impl Renderer {
|
||||||
encoder,
|
encoder,
|
||||||
&layer.quads,
|
&layer.quads,
|
||||||
transformation,
|
transformation,
|
||||||
dpi,
|
scale_factor,
|
||||||
bounds,
|
bounds,
|
||||||
target,
|
target,
|
||||||
);
|
);
|
||||||
|
|
@ -370,7 +376,7 @@ impl Renderer {
|
||||||
|
|
||||||
if layer.images.len() > 0 {
|
if layer.images.len() > 0 {
|
||||||
let translated_and_scaled = transformation
|
let translated_and_scaled = transformation
|
||||||
* Transformation::scale(dpi, dpi)
|
* Transformation::scale(scale_factor, scale_factor)
|
||||||
* Transformation::translate(
|
* Transformation::translate(
|
||||||
-(layer.offset.x as f32),
|
-(layer.offset.x as f32),
|
||||||
-(layer.offset.y as f32),
|
-(layer.offset.y as f32),
|
||||||
|
|
@ -383,7 +389,7 @@ impl Renderer {
|
||||||
translated_and_scaled,
|
translated_and_scaled,
|
||||||
bounds,
|
bounds,
|
||||||
target,
|
target,
|
||||||
dpi,
|
scale_factor,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -396,25 +402,25 @@ impl Renderer {
|
||||||
// bit "jumpy". We may be able to do better once we improve
|
// bit "jumpy". We may be able to do better once we improve
|
||||||
// our text rendering/caching pipeline.
|
// our text rendering/caching pipeline.
|
||||||
screen_position: (
|
screen_position: (
|
||||||
(text.screen_position.0 * dpi).round(),
|
(text.screen_position.0 * scale_factor).round(),
|
||||||
(text.screen_position.1 * dpi).round(),
|
(text.screen_position.1 * scale_factor).round(),
|
||||||
),
|
),
|
||||||
// TODO: Fix precision issues with some DPI factors.
|
// TODO: Fix precision issues with some scale factors.
|
||||||
//
|
//
|
||||||
// The `ceil` here can cause some words to render on the
|
// The `ceil` here can cause some words to render on the
|
||||||
// same line when they should not.
|
// same line when they should not.
|
||||||
//
|
//
|
||||||
// Ideally, `wgpu_glyph` should be able to compute layout
|
// Ideally, `wgpu_glyph` should be able to compute layout
|
||||||
// using logical positions, and then apply the proper
|
// using logical positions, and then apply the proper
|
||||||
// DPI scaling. This would ensure that both measuring and
|
// scaling when rendering. This would ensure that both
|
||||||
// rendering follow the same layout rules.
|
// measuring and rendering follow the same layout rules.
|
||||||
bounds: (
|
bounds: (
|
||||||
(text.bounds.0 * dpi).ceil(),
|
(text.bounds.0 * scale_factor).ceil(),
|
||||||
(text.bounds.1 * dpi).ceil(),
|
(text.bounds.1 * scale_factor).ceil(),
|
||||||
),
|
),
|
||||||
scale: wgpu_glyph::Scale {
|
scale: wgpu_glyph::Scale {
|
||||||
x: text.scale.x * dpi,
|
x: text.scale.x * scale_factor,
|
||||||
y: text.scale.y * dpi,
|
y: text.scale.y * scale_factor,
|
||||||
},
|
},
|
||||||
..*text
|
..*text
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -7,20 +7,20 @@ use raw_window_handle::HasRawWindowHandle;
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Target {
|
pub struct Target {
|
||||||
surface: wgpu::Surface,
|
surface: wgpu::Surface,
|
||||||
width: u16,
|
width: u32,
|
||||||
height: u16,
|
height: u32,
|
||||||
dpi: f32,
|
scale_factor: f32,
|
||||||
transformation: Transformation,
|
transformation: Transformation,
|
||||||
swap_chain: wgpu::SwapChain,
|
swap_chain: wgpu::SwapChain,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Target {
|
impl Target {
|
||||||
pub(crate) fn dimensions(&self) -> (u16, u16) {
|
pub(crate) fn dimensions(&self) -> (u32, u32) {
|
||||||
(self.width, self.height)
|
(self.width, self.height)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn dpi(&self) -> f32 {
|
pub(crate) fn scale_factor(&self) -> f32 {
|
||||||
self.dpi
|
self.scale_factor
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn transformation(&self) -> Transformation {
|
pub(crate) fn transformation(&self) -> Transformation {
|
||||||
|
|
@ -37,9 +37,9 @@ impl window::Target for Target {
|
||||||
|
|
||||||
fn new<W: HasRawWindowHandle>(
|
fn new<W: HasRawWindowHandle>(
|
||||||
window: &W,
|
window: &W,
|
||||||
width: u16,
|
width: u32,
|
||||||
height: u16,
|
height: u32,
|
||||||
dpi: f32,
|
scale_factor: f32,
|
||||||
renderer: &Renderer,
|
renderer: &Renderer,
|
||||||
) -> Target {
|
) -> Target {
|
||||||
let surface = wgpu::Surface::create(window);
|
let surface = wgpu::Surface::create(window);
|
||||||
|
|
@ -50,7 +50,7 @@ impl window::Target for Target {
|
||||||
surface,
|
surface,
|
||||||
width,
|
width,
|
||||||
height,
|
height,
|
||||||
dpi,
|
scale_factor,
|
||||||
transformation: Transformation::orthographic(width, height),
|
transformation: Transformation::orthographic(width, height),
|
||||||
swap_chain,
|
swap_chain,
|
||||||
}
|
}
|
||||||
|
|
@ -58,14 +58,14 @@ impl window::Target for Target {
|
||||||
|
|
||||||
fn resize(
|
fn resize(
|
||||||
&mut self,
|
&mut self,
|
||||||
width: u16,
|
width: u32,
|
||||||
height: u16,
|
height: u32,
|
||||||
dpi: f32,
|
scale_factor: f32,
|
||||||
renderer: &Renderer,
|
renderer: &Renderer,
|
||||||
) {
|
) {
|
||||||
self.width = width;
|
self.width = width;
|
||||||
self.height = height;
|
self.height = height;
|
||||||
self.dpi = dpi;
|
self.scale_factor = scale_factor;
|
||||||
self.transformation = Transformation::orthographic(width, height);
|
self.transformation = Transformation::orthographic(width, height);
|
||||||
self.swap_chain =
|
self.swap_chain =
|
||||||
new_swap_chain(&self.surface, width, height, &renderer.device);
|
new_swap_chain(&self.surface, width, height, &renderer.device);
|
||||||
|
|
@ -74,8 +74,8 @@ impl window::Target for Target {
|
||||||
|
|
||||||
fn new_swap_chain(
|
fn new_swap_chain(
|
||||||
surface: &wgpu::Surface,
|
surface: &wgpu::Surface,
|
||||||
width: u16,
|
width: u32,
|
||||||
height: u16,
|
height: u32,
|
||||||
device: &wgpu::Device,
|
device: &wgpu::Device,
|
||||||
) -> wgpu::SwapChain {
|
) -> wgpu::SwapChain {
|
||||||
device.create_swap_chain(
|
device.create_swap_chain(
|
||||||
|
|
@ -83,8 +83,8 @@ fn new_swap_chain(
|
||||||
&wgpu::SwapChainDescriptor {
|
&wgpu::SwapChainDescriptor {
|
||||||
usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT,
|
usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT,
|
||||||
format: wgpu::TextureFormat::Bgra8UnormSrgb,
|
format: wgpu::TextureFormat::Bgra8UnormSrgb,
|
||||||
width: u32::from(width),
|
width,
|
||||||
height: u32::from(height),
|
height,
|
||||||
present_mode: wgpu::PresentMode::Vsync,
|
present_mode: wgpu::PresentMode::Vsync,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -13,10 +13,10 @@ impl Transformation {
|
||||||
|
|
||||||
/// Creates an orthographic projection.
|
/// Creates an orthographic projection.
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
pub fn orthographic(width: u16, height: u16) -> Transformation {
|
pub fn orthographic(width: u32, height: u32) -> Transformation {
|
||||||
Transformation(Mat4::from_cols(
|
Transformation(Mat4::from_cols(
|
||||||
Vec4::new(2.0 / f32::from(width), 0.0, 0.0, 0.0),
|
Vec4::new(2.0 / width as f32, 0.0, 0.0, 0.0),
|
||||||
Vec4::new(0.0, 2.0 / f32::from(height), 0.0, 0.0),
|
Vec4::new(0.0, 2.0 / height as f32, 0.0, 0.0),
|
||||||
Vec4::new(0.0, 0.0, -1.0, 0.0),
|
Vec4::new(0.0, 0.0, -1.0, 0.0),
|
||||||
Vec4::new(-1.0, -1.0, 0.0, 1.0)
|
Vec4::new(-1.0, -1.0, 0.0, 1.0)
|
||||||
))
|
))
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ categories = ["gui"]
|
||||||
debug = []
|
debug = []
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
winit = { version = "0.20.0-alpha3", git = "https://github.com/hecrj/winit", rev = "709808eb4e69044705fcb214bcc30556db761405"}
|
winit = "0.20"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
|
|
||||||
[dependencies.iced_native]
|
[dependencies.iced_native]
|
||||||
|
|
|
||||||
|
|
@ -138,10 +138,7 @@ pub trait Application: Sized {
|
||||||
|
|
||||||
window_builder = window_builder
|
window_builder = window_builder
|
||||||
.with_title(&title)
|
.with_title(&title)
|
||||||
.with_inner_size(winit::dpi::LogicalSize {
|
.with_inner_size(winit::dpi::LogicalSize { width, height })
|
||||||
width: f64::from(width),
|
|
||||||
height: f64::from(height),
|
|
||||||
})
|
|
||||||
.with_resizable(settings.window.resizable)
|
.with_resizable(settings.window.resizable)
|
||||||
.with_decorations(settings.window.decorations)
|
.with_decorations(settings.window.decorations)
|
||||||
.with_fullscreen(conversion::fullscreen(
|
.with_fullscreen(conversion::fullscreen(
|
||||||
|
|
@ -161,18 +158,22 @@ pub trait Application: Sized {
|
||||||
window_builder.build(&event_loop).expect("Open window")
|
window_builder.build(&event_loop).expect("Open window")
|
||||||
};
|
};
|
||||||
|
|
||||||
let dpi = window.hidpi_factor();
|
let mut scale_factor = window.scale_factor();
|
||||||
let mut size = window.inner_size();
|
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, dpi);
|
let (width, height) = to_physical(size, scale_factor);
|
||||||
|
|
||||||
<Self::Renderer as window::Renderer>::Target::new(
|
<Self::Renderer as window::Renderer>::Target::new(
|
||||||
&window, width, height, dpi as f32, &renderer,
|
&window,
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
scale_factor as f32,
|
||||||
|
&renderer,
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -191,6 +192,7 @@ pub trait Application: Sized {
|
||||||
let mut cache = Some(user_interface.into_cache());
|
let mut cache = Some(user_interface.into_cache());
|
||||||
let mut events = Vec::new();
|
let mut events = Vec::new();
|
||||||
let mut mouse_cursor = MouseCursor::OutOfBounds;
|
let mut mouse_cursor = MouseCursor::OutOfBounds;
|
||||||
|
let mut modifiers = winit::event::ModifiersState::default();
|
||||||
debug.startup_finished();
|
debug.startup_finished();
|
||||||
|
|
||||||
window.request_redraw();
|
window.request_redraw();
|
||||||
|
|
@ -303,13 +305,12 @@ pub trait Application: Sized {
|
||||||
debug.render_started();
|
debug.render_started();
|
||||||
|
|
||||||
if resized {
|
if resized {
|
||||||
let dpi = window.hidpi_factor();
|
let (width, height) = to_physical(size, scale_factor);
|
||||||
let (width, height) = to_physical(size, dpi);
|
|
||||||
|
|
||||||
target.resize(
|
target.resize(
|
||||||
width,
|
width,
|
||||||
height,
|
height,
|
||||||
window.hidpi_factor() as f32,
|
scale_factor as f32,
|
||||||
&renderer,
|
&renderer,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -337,18 +338,21 @@ 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);
|
||||||
|
|
||||||
events.push(Event::Window(window::Event::Resized {
|
events.push(Event::Window(window::Event::Resized {
|
||||||
width: new_size.width.round() as u32,
|
width: size.width.round() as u32,
|
||||||
height: new_size.height.round() as u32,
|
height: size.height.round() as u32,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
size = new_size;
|
|
||||||
resized = true;
|
resized = true;
|
||||||
}
|
}
|
||||||
WindowEvent::CloseRequested => {
|
WindowEvent::CloseRequested => {
|
||||||
*control_flow = ControlFlow::Exit;
|
*control_flow = ControlFlow::Exit;
|
||||||
}
|
}
|
||||||
WindowEvent::CursorMoved { position, .. } => {
|
WindowEvent::CursorMoved { position, .. } => {
|
||||||
|
let position = position.to_logical::<f64>(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,
|
||||||
y: position.y as f32,
|
y: position.y as f32,
|
||||||
|
|
@ -397,7 +401,6 @@ pub trait Application: Sized {
|
||||||
winit::event::KeyboardInput {
|
winit::event::KeyboardInput {
|
||||||
virtual_keycode: Some(virtual_keycode),
|
virtual_keycode: Some(virtual_keycode),
|
||||||
state,
|
state,
|
||||||
modifiers,
|
|
||||||
..
|
..
|
||||||
},
|
},
|
||||||
..
|
..
|
||||||
|
|
@ -427,8 +430,20 @@ pub trait Application: Sized {
|
||||||
WindowEvent::HoveredFileCancelled => {
|
WindowEvent::HoveredFileCancelled => {
|
||||||
events.push(Event::Window(window::Event::FilesHoveredLeft));
|
events.push(Event::Window(window::Event::FilesHoveredLeft));
|
||||||
}
|
}
|
||||||
|
WindowEvent::ScaleFactorChanged {
|
||||||
|
scale_factor: new_scale_factor,
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
scale_factor = new_scale_factor;
|
||||||
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
},
|
},
|
||||||
|
event::Event::DeviceEvent {
|
||||||
|
event: event::DeviceEvent::ModifiersChanged(new_modifiers),
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
modifiers = new_modifiers;
|
||||||
|
}
|
||||||
_ => {
|
_ => {
|
||||||
*control_flow = ControlFlow::Wait;
|
*control_flow = ControlFlow::Wait;
|
||||||
}
|
}
|
||||||
|
|
@ -440,7 +455,7 @@ fn build_user_interface<'a, A: Application>(
|
||||||
application: &'a mut A,
|
application: &'a mut A,
|
||||||
cache: Cache,
|
cache: Cache,
|
||||||
renderer: &mut A::Renderer,
|
renderer: &mut A::Renderer,
|
||||||
size: winit::dpi::LogicalSize,
|
size: winit::dpi::LogicalSize<f64>,
|
||||||
debug: &mut Debug,
|
debug: &mut Debug,
|
||||||
) -> UserInterface<'a, A::Message, A::Renderer> {
|
) -> UserInterface<'a, A::Message, A::Renderer> {
|
||||||
debug.view_started();
|
debug.view_started();
|
||||||
|
|
@ -459,12 +474,12 @@ fn build_user_interface<'a, A: Application>(
|
||||||
user_interface
|
user_interface
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_physical(size: winit::dpi::LogicalSize, dpi: f64) -> (u16, u16) {
|
fn to_physical(size: winit::dpi::LogicalSize<f64>, dpi: f64) -> (u32, u32) {
|
||||||
let physical_size = size.to_physical(dpi);
|
let physical_size = size.to_physical::<f64>(dpi);
|
||||||
|
|
||||||
(
|
(
|
||||||
physical_size.width.round() as u16,
|
physical_size.width.round() as u32,
|
||||||
physical_size.height.round() as u16,
|
physical_size.height.round() as u32,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -74,10 +74,10 @@ pub fn modifiers_state(
|
||||||
modifiers: winit::event::ModifiersState,
|
modifiers: winit::event::ModifiersState,
|
||||||
) -> ModifiersState {
|
) -> ModifiersState {
|
||||||
ModifiersState {
|
ModifiersState {
|
||||||
shift: modifiers.shift,
|
shift: modifiers.shift(),
|
||||||
control: modifiers.ctrl,
|
control: modifiers.ctrl(),
|
||||||
alt: modifiers.alt,
|
alt: modifiers.alt(),
|
||||||
logo: modifiers.logo,
|
logo: modifiers.logo(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue