Apply HiDPI to text, images, and clip primitives
Quads are a bit trickier to handle. We may need to change the shaders a bit.
This commit is contained in:
parent
0157121038
commit
db716b3bdf
6 changed files with 112 additions and 58 deletions
|
|
@ -28,3 +28,16 @@ impl Rectangle<f32> {
|
||||||
&& point.y <= self.y + self.height
|
&& point.y <= self.y + self.height
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl std::ops::Mul<f32> for Rectangle<u32> {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn mul(self, scale: f32) -> Self {
|
||||||
|
Self {
|
||||||
|
x: (self.x as f32 * scale).round() as u32,
|
||||||
|
y: (self.y as f32 * scale).round() as u32,
|
||||||
|
width: (self.width as f32 * scale).round() as u32,
|
||||||
|
height: (self.height as f32 * scale).round() as u32,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,8 +22,15 @@ pub trait Target {
|
||||||
window: &W,
|
window: &W,
|
||||||
width: u16,
|
width: u16,
|
||||||
height: u16,
|
height: u16,
|
||||||
|
dpi: f32,
|
||||||
renderer: &Self::Renderer,
|
renderer: &Self::Renderer,
|
||||||
) -> Self;
|
) -> Self;
|
||||||
|
|
||||||
fn resize(&mut self, width: u16, height: u16, renderer: &Self::Renderer);
|
fn resize(
|
||||||
|
&mut self,
|
||||||
|
width: u16,
|
||||||
|
height: u16,
|
||||||
|
dpi: f32,
|
||||||
|
renderer: &Self::Renderer,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -98,6 +98,7 @@ impl Renderer {
|
||||||
log::debug!("Drawing");
|
log::debug!("Drawing");
|
||||||
|
|
||||||
let (width, height) = target.dimensions();
|
let (width, height) = target.dimensions();
|
||||||
|
let dpi = target.dpi();
|
||||||
let transformation = target.transformation();
|
let transformation = target.transformation();
|
||||||
let frame = target.next_frame();
|
let frame = target.next_frame();
|
||||||
|
|
||||||
|
|
@ -137,7 +138,7 @@ impl Renderer {
|
||||||
self.draw_overlay(overlay, &mut layers);
|
self.draw_overlay(overlay, &mut layers);
|
||||||
|
|
||||||
for layer in layers {
|
for layer in layers {
|
||||||
self.flush(transformation, &layer, &mut encoder, &frame.view);
|
self.flush(dpi, transformation, &layer, &mut encoder, &frame.view);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.queue.submit(&[encoder.finish()]);
|
self.queue.submit(&[encoder.finish()]);
|
||||||
|
|
@ -190,7 +191,10 @@ impl Renderer {
|
||||||
|
|
||||||
layer.text.push(Section {
|
layer.text.push(Section {
|
||||||
text: &content,
|
text: &content,
|
||||||
screen_position: (x, y),
|
screen_position: (
|
||||||
|
x - layer.offset.x as f32,
|
||||||
|
y - layer.offset.y as f32,
|
||||||
|
),
|
||||||
bounds: (bounds.width, bounds.height),
|
bounds: (bounds.width, bounds.height),
|
||||||
scale: wgpu_glyph::Scale { x: *size, y: *size },
|
scale: wgpu_glyph::Scale { x: *size, y: *size },
|
||||||
color: color.into_linear(),
|
color: color.into_linear(),
|
||||||
|
|
@ -225,6 +229,7 @@ impl Renderer {
|
||||||
background,
|
background,
|
||||||
border_radius,
|
border_radius,
|
||||||
} => {
|
} => {
|
||||||
|
// TODO: Move some of this computations to the GPU (?)
|
||||||
layer.quads.push(Quad {
|
layer.quads.push(Quad {
|
||||||
position: [
|
position: [
|
||||||
bounds.x - layer.offset.x as f32,
|
bounds.x - layer.offset.x as f32,
|
||||||
|
|
@ -234,7 +239,7 @@ impl Renderer {
|
||||||
color: match background {
|
color: match background {
|
||||||
Background::Color(color) => color.into_linear(),
|
Background::Color(color) => color.into_linear(),
|
||||||
},
|
},
|
||||||
border_radius: u32::from(*border_radius),
|
border_radius: *border_radius as u32,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
Primitive::Image { path, bounds } => {
|
Primitive::Image { path, bounds } => {
|
||||||
|
|
@ -308,16 +313,13 @@ impl Renderer {
|
||||||
|
|
||||||
fn flush(
|
fn flush(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
dpi: f32,
|
||||||
transformation: Transformation,
|
transformation: Transformation,
|
||||||
layer: &Layer,
|
layer: &Layer,
|
||||||
encoder: &mut wgpu::CommandEncoder,
|
encoder: &mut wgpu::CommandEncoder,
|
||||||
target: &wgpu::TextureView,
|
target: &wgpu::TextureView,
|
||||||
) {
|
) {
|
||||||
let translated = transformation
|
let bounds = layer.bounds * dpi;
|
||||||
* Transformation::translate(
|
|
||||||
-(layer.offset.x as f32),
|
|
||||||
-(layer.offset.y as f32),
|
|
||||||
);
|
|
||||||
|
|
||||||
if layer.quads.len() > 0 {
|
if layer.quads.len() > 0 {
|
||||||
self.quad_pipeline.draw(
|
self.quad_pipeline.draw(
|
||||||
|
|
@ -325,18 +327,25 @@ impl Renderer {
|
||||||
encoder,
|
encoder,
|
||||||
&layer.quads,
|
&layer.quads,
|
||||||
transformation,
|
transformation,
|
||||||
layer.bounds,
|
bounds,
|
||||||
target,
|
target,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if layer.images.len() > 0 {
|
if layer.images.len() > 0 {
|
||||||
|
let translated = transformation
|
||||||
|
* Transformation::scale(dpi, dpi)
|
||||||
|
* Transformation::translate(
|
||||||
|
-(layer.offset.x as f32),
|
||||||
|
-(layer.offset.y as f32),
|
||||||
|
);
|
||||||
|
|
||||||
self.image_pipeline.draw(
|
self.image_pipeline.draw(
|
||||||
&mut self.device,
|
&mut self.device,
|
||||||
encoder,
|
encoder,
|
||||||
&layer.images,
|
&layer.images,
|
||||||
translated,
|
translated,
|
||||||
layer.bounds,
|
bounds,
|
||||||
target,
|
target,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -345,6 +354,20 @@ impl Renderer {
|
||||||
let mut glyph_brush = self.glyph_brush.borrow_mut();
|
let mut glyph_brush = self.glyph_brush.borrow_mut();
|
||||||
|
|
||||||
for text in layer.text.iter() {
|
for text in layer.text.iter() {
|
||||||
|
// Target physical coordinates directly to avoid blurry text
|
||||||
|
let text = Section {
|
||||||
|
screen_position: (
|
||||||
|
text.screen_position.0 * dpi,
|
||||||
|
text.screen_position.1 * dpi,
|
||||||
|
),
|
||||||
|
bounds: (text.bounds.0 * dpi, text.bounds.1 * dpi),
|
||||||
|
scale: wgpu_glyph::Scale {
|
||||||
|
x: text.scale.x * dpi,
|
||||||
|
y: text.scale.y * dpi,
|
||||||
|
},
|
||||||
|
..*text
|
||||||
|
};
|
||||||
|
|
||||||
glyph_brush.queue(text);
|
glyph_brush.queue(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -353,12 +376,12 @@ impl Renderer {
|
||||||
&mut self.device,
|
&mut self.device,
|
||||||
encoder,
|
encoder,
|
||||||
target,
|
target,
|
||||||
translated.into(),
|
transformation.into(),
|
||||||
wgpu_glyph::Region {
|
wgpu_glyph::Region {
|
||||||
x: layer.bounds.x,
|
x: bounds.x,
|
||||||
y: layer.bounds.y,
|
y: bounds.y,
|
||||||
width: layer.bounds.width,
|
width: bounds.width,
|
||||||
height: layer.bounds.height,
|
height: bounds.height,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
.expect("Draw text");
|
.expect("Draw text");
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ pub struct Target {
|
||||||
surface: wgpu::Surface,
|
surface: wgpu::Surface,
|
||||||
width: u16,
|
width: u16,
|
||||||
height: u16,
|
height: u16,
|
||||||
|
dpi: f32,
|
||||||
transformation: Transformation,
|
transformation: Transformation,
|
||||||
swap_chain: wgpu::SwapChain,
|
swap_chain: wgpu::SwapChain,
|
||||||
}
|
}
|
||||||
|
|
@ -15,6 +16,10 @@ impl Target {
|
||||||
(self.width, self.height)
|
(self.width, self.height)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn dpi(&self) -> f32 {
|
||||||
|
self.dpi
|
||||||
|
}
|
||||||
|
|
||||||
pub fn transformation(&self) -> Transformation {
|
pub fn transformation(&self) -> Transformation {
|
||||||
self.transformation
|
self.transformation
|
||||||
}
|
}
|
||||||
|
|
@ -31,6 +36,7 @@ impl iced_native::renderer::Target for Target {
|
||||||
window: &W,
|
window: &W,
|
||||||
width: u16,
|
width: u16,
|
||||||
height: u16,
|
height: u16,
|
||||||
|
dpi: f32,
|
||||||
renderer: &Renderer,
|
renderer: &Renderer,
|
||||||
) -> Target {
|
) -> Target {
|
||||||
let surface = wgpu::Surface::create(window);
|
let surface = wgpu::Surface::create(window);
|
||||||
|
|
@ -41,14 +47,22 @@ impl iced_native::renderer::Target for Target {
|
||||||
surface,
|
surface,
|
||||||
width,
|
width,
|
||||||
height,
|
height,
|
||||||
|
dpi,
|
||||||
transformation: Transformation::orthographic(width, height),
|
transformation: Transformation::orthographic(width, height),
|
||||||
swap_chain,
|
swap_chain,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn resize(&mut self, width: u16, height: u16, renderer: &Renderer) {
|
fn resize(
|
||||||
|
&mut self,
|
||||||
|
width: u16,
|
||||||
|
height: u16,
|
||||||
|
dpi: f32,
|
||||||
|
renderer: &Renderer,
|
||||||
|
) {
|
||||||
self.width = width;
|
self.width = width;
|
||||||
self.height = height;
|
self.height = height;
|
||||||
|
self.dpi = dpi;
|
||||||
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);
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,11 @@ impl Transformation {
|
||||||
pub fn translate(x: f32, y: f32) -> Transformation {
|
pub fn translate(x: f32, y: f32) -> Transformation {
|
||||||
Transformation(Mat4::from_translation(Vec3::new(x, y, 0.0)))
|
Transformation(Mat4::from_translation(Vec3::new(x, y, 0.0)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Creates a scale transformation.
|
||||||
|
pub fn scale(x: f32, y: f32) -> Transformation {
|
||||||
|
Transformation(Mat4::from_scale(Vec3::new(x, y, 0.0)))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Mul for Transformation {
|
impl Mul for Transformation {
|
||||||
|
|
|
||||||
|
|
@ -38,20 +38,19 @@ pub trait Application {
|
||||||
.build(&event_loop)
|
.build(&event_loop)
|
||||||
.expect("Open window");
|
.expect("Open window");
|
||||||
|
|
||||||
let mut size: Size = window
|
let dpi = window.hidpi_factor();
|
||||||
.inner_size()
|
let mut size = window.inner_size();
|
||||||
.to_physical(window.hidpi_factor())
|
let mut new_size: Option<winit::dpi::LogicalSize> = None;
|
||||||
.into();
|
|
||||||
let mut new_size: Option<Size> = None;
|
|
||||||
|
|
||||||
let mut renderer = Self::Renderer::new();
|
let mut renderer = Self::Renderer::new();
|
||||||
|
|
||||||
let mut target = <Self::Renderer as Windowed>::Target::new(
|
let mut target = {
|
||||||
&window,
|
let (width, height) = to_physical(size, dpi);
|
||||||
size.width,
|
|
||||||
size.height,
|
<Self::Renderer as Windowed>::Target::new(
|
||||||
&renderer,
|
&window, width, height, dpi as f32, &renderer,
|
||||||
);
|
)
|
||||||
|
};
|
||||||
|
|
||||||
debug.layout_started();
|
debug.layout_started();
|
||||||
let user_interface = UserInterface::build(
|
let user_interface = UserInterface::build(
|
||||||
|
|
@ -134,7 +133,15 @@ pub trait Application {
|
||||||
debug.render_started();
|
debug.render_started();
|
||||||
|
|
||||||
if let Some(new_size) = new_size.take() {
|
if let Some(new_size) = new_size.take() {
|
||||||
target.resize(new_size.width, new_size.height, &renderer);
|
let dpi = window.hidpi_factor();
|
||||||
|
let (width, height) = to_physical(new_size, dpi);
|
||||||
|
|
||||||
|
target.resize(
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
window.hidpi_factor() as f32,
|
||||||
|
&renderer,
|
||||||
|
);
|
||||||
|
|
||||||
size = new_size;
|
size = new_size;
|
||||||
}
|
}
|
||||||
|
|
@ -160,13 +167,9 @@ pub trait Application {
|
||||||
..
|
..
|
||||||
} => match window_event {
|
} => match window_event {
|
||||||
WindowEvent::CursorMoved { position, .. } => {
|
WindowEvent::CursorMoved { position, .. } => {
|
||||||
// TODO: Remove when renderer supports HiDPI
|
|
||||||
let physical_position =
|
|
||||||
position.to_physical(window.hidpi_factor());
|
|
||||||
|
|
||||||
events.push(Event::Mouse(mouse::Event::CursorMoved {
|
events.push(Event::Mouse(mouse::Event::CursorMoved {
|
||||||
x: physical_position.x as f32,
|
x: position.x as f32,
|
||||||
y: physical_position.y as f32,
|
y: position.y as f32,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
WindowEvent::MouseInput { button, state, .. } => {
|
WindowEvent::MouseInput { button, state, .. } => {
|
||||||
|
|
@ -190,15 +193,11 @@ pub trait Application {
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
winit::event::MouseScrollDelta::PixelDelta(position) => {
|
winit::event::MouseScrollDelta::PixelDelta(position) => {
|
||||||
// TODO: Remove when renderer supports HiDPI
|
|
||||||
let physical_position =
|
|
||||||
position.to_physical(window.hidpi_factor());
|
|
||||||
|
|
||||||
events.push(Event::Mouse(
|
events.push(Event::Mouse(
|
||||||
mouse::Event::WheelScrolled {
|
mouse::Event::WheelScrolled {
|
||||||
delta: mouse::ScrollDelta::Pixels {
|
delta: mouse::ScrollDelta::Pixels {
|
||||||
x: physical_position.x as f32,
|
x: position.x as f32,
|
||||||
y: physical_position.y as f32,
|
y: position.y as f32,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
));
|
));
|
||||||
|
|
@ -235,8 +234,7 @@ pub trait Application {
|
||||||
*control_flow = ControlFlow::Exit;
|
*control_flow = ControlFlow::Exit;
|
||||||
}
|
}
|
||||||
WindowEvent::Resized(size) => {
|
WindowEvent::Resized(size) => {
|
||||||
new_size =
|
new_size = Some(size.into());
|
||||||
Some(size.to_physical(window.hidpi_factor()).into());
|
|
||||||
|
|
||||||
log::debug!("Resized: {:?}", new_size);
|
log::debug!("Resized: {:?}", new_size);
|
||||||
}
|
}
|
||||||
|
|
@ -249,24 +247,18 @@ pub trait Application {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
|
fn to_physical(size: winit::dpi::LogicalSize, dpi: f64) -> (u16, u16) {
|
||||||
struct Size {
|
let physical_size = size.to_physical(dpi);
|
||||||
width: u16,
|
|
||||||
height: u16,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<winit::dpi::PhysicalSize> for Size {
|
(
|
||||||
fn from(physical_size: winit::dpi::PhysicalSize) -> Self {
|
physical_size.width.round() as u16,
|
||||||
Self {
|
physical_size.height.round() as u16,
|
||||||
width: physical_size.width.round() as u16,
|
)
|
||||||
height: physical_size.height.round() as u16,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn document<'a, Application>(
|
fn document<'a, Application>(
|
||||||
application: &'a mut Application,
|
application: &'a mut Application,
|
||||||
size: Size,
|
size: winit::dpi::LogicalSize,
|
||||||
debug: &mut Debug,
|
debug: &mut Debug,
|
||||||
) -> Element<'a, Application::Message, Application::Renderer>
|
) -> Element<'a, Application::Message, Application::Renderer>
|
||||||
where
|
where
|
||||||
|
|
@ -278,8 +270,8 @@ where
|
||||||
debug.view_finished();
|
debug.view_finished();
|
||||||
|
|
||||||
Column::new()
|
Column::new()
|
||||||
.width(Length::Units(size.width))
|
.width(Length::Units(size.width.round() as u16))
|
||||||
.height(Length::Units(size.height))
|
.height(Length::Units(size.height.round() as u16))
|
||||||
.push(view)
|
.push(view)
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue