Add scale_factor to Application and Sandbox

This commit is contained in:
Héctor Ramón Jiménez 2020-06-19 19:17:05 +02:00
parent d19c02035f
commit c9696ca687
4 changed files with 93 additions and 5 deletions

View File

@ -48,6 +48,7 @@ pub fn run<A, E, C>(
let mut title = application.title(); let mut title = application.title();
let mut mode = application.mode(); let mut mode = application.mode();
let mut background_color = application.background_color(); let mut background_color = application.background_color();
let mut scale_factor = application.scale_factor();
let context = { let context = {
let builder = settings.window.into_builder( let builder = settings.window.into_builder(
@ -75,7 +76,7 @@ pub fn run<A, E, C>(
let physical_size = context.window().inner_size(); let physical_size = context.window().inner_size();
let mut viewport = Viewport::with_physical_size( let mut viewport = Viewport::with_physical_size(
Size::new(physical_size.width, physical_size.height), Size::new(physical_size.width, physical_size.height),
context.window().scale_factor(), context.window().scale_factor() * scale_factor,
); );
let mut resized = false; let mut resized = false;
@ -142,6 +143,20 @@ pub fn run<A, E, C>(
// Update background color // Update background color
background_color = program.background_color(); background_color = program.background_color();
// Update scale factor
let new_scale_factor = program.scale_factor();
if scale_factor != new_scale_factor {
let size = context.window().inner_size();
viewport = Viewport::with_physical_size(
Size::new(size.width, size.height),
context.window().scale_factor() * new_scale_factor,
);
scale_factor = new_scale_factor;
}
} }
context.window().request_redraw(); context.window().request_redraw();
@ -195,6 +210,7 @@ pub fn run<A, E, C>(
application::handle_window_event( application::handle_window_event(
&window_event, &window_event,
context.window(), context.window(),
scale_factor,
control_flow, control_flow,
&mut modifiers, &mut modifiers,
&mut viewport, &mut viewport,

View File

@ -186,6 +186,21 @@ pub trait Application: Sized {
Color::WHITE Color::WHITE
} }
/// Returns the scale factor of the [`Application`].
///
/// It can be used to dynamically control the size of the UI at runtime
/// (i.e. zooming).
///
/// For instance, a scale factor of `2.0` will make widgets twice as big,
/// while a scale factor of `0.5` will shrink them to half their size.
///
/// By default, it returns `1.0`.
///
/// [`Application`]: trait.Application.html
fn scale_factor(&self) -> f64 {
1.0
}
/// Runs the [`Application`]. /// Runs the [`Application`].
/// ///
/// On native platforms, this method will take control of the current thread /// On native platforms, this method will take control of the current thread
@ -272,6 +287,10 @@ where
fn background_color(&self) -> Color { fn background_color(&self) -> Color {
self.0.background_color() self.0.background_color()
} }
fn scale_factor(&self) -> f64 {
self.0.scale_factor()
}
} }
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]

View File

@ -130,12 +130,27 @@ pub trait Sandbox {
/// ///
/// By default, it returns [`Color::WHITE`]. /// By default, it returns [`Color::WHITE`].
/// ///
/// [`Application`]: trait.Application.html /// [`Sandbox`]: trait.Sandbox.html
/// [`Color::WHITE`]: struct.Color.html#const.WHITE /// [`Color::WHITE`]: struct.Color.html#const.WHITE
fn background_color(&self) -> Color { fn background_color(&self) -> Color {
Color::WHITE Color::WHITE
} }
/// Returns the scale factor of the [`Sandbox`].
///
/// It can be used to dynamically control the size of the UI at runtime
/// (i.e. zooming).
///
/// For instance, a scale factor of `2.0` will make widgets twice as big,
/// while a scale factor of `0.5` will shrink them to half their size.
///
/// By default, it returns `1.0`.
///
/// [`Sandbox`]: trait.Sandbox.html
fn scale_factor(&self) -> f64 {
1.0
}
/// Runs the [`Sandbox`]. /// Runs the [`Sandbox`].
/// ///
/// On native platforms, this method will take control of the current thread /// On native platforms, this method will take control of the current thread
@ -185,4 +200,8 @@ where
fn background_color(&self) -> Color { fn background_color(&self) -> Color {
T::background_color(self) T::background_color(self)
} }
fn scale_factor(&self) -> f64 {
T::scale_factor(self)
}
} }

View File

@ -84,6 +84,21 @@ pub trait Application: Program {
fn background_color(&self) -> Color { fn background_color(&self) -> Color {
Color::WHITE Color::WHITE
} }
/// Returns the scale factor of the [`Application`].
///
/// It can be used to dynamically control the size of the UI at runtime
/// (i.e. zooming).
///
/// For instance, a scale factor of `2.0` will make widgets twice as big,
/// while a scale factor of `0.5` will shrink them to half their size.
///
/// By default, it returns `1.0`.
///
/// [`Application`]: trait.Application.html
fn scale_factor(&self) -> f64 {
1.0
}
} }
/// Runs an [`Application`] with an executor, compositor, and the provided /// Runs an [`Application`] with an executor, compositor, and the provided
@ -124,6 +139,7 @@ pub fn run<A, E, C>(
let mut title = application.title(); let mut title = application.title();
let mut mode = application.mode(); let mut mode = application.mode();
let mut background_color = application.background_color(); let mut background_color = application.background_color();
let mut scale_factor = application.scale_factor();
let window = settings let window = settings
.window .window
@ -138,7 +154,7 @@ pub fn run<A, E, C>(
let physical_size = window.inner_size(); let physical_size = window.inner_size();
let mut viewport = Viewport::with_physical_size( let mut viewport = Viewport::with_physical_size(
Size::new(physical_size.width, physical_size.height), Size::new(physical_size.width, physical_size.height),
window.scale_factor(), window.scale_factor() * scale_factor,
); );
let mut resized = false; let mut resized = false;
@ -208,6 +224,20 @@ pub fn run<A, E, C>(
// Update background color // Update background color
background_color = program.background_color(); background_color = program.background_color();
// Update scale factor
let new_scale_factor = program.scale_factor();
if scale_factor != new_scale_factor {
let size = window.inner_size();
viewport = Viewport::with_physical_size(
Size::new(size.width, size.height),
window.scale_factor() * new_scale_factor,
);
scale_factor = new_scale_factor;
}
} }
window.request_redraw(); window.request_redraw();
@ -259,6 +289,7 @@ pub fn run<A, E, C>(
handle_window_event( handle_window_event(
&window_event, &window_event,
&window, &window,
scale_factor,
control_flow, control_flow,
&mut modifiers, &mut modifiers,
&mut viewport, &mut viewport,
@ -286,6 +317,7 @@ pub fn run<A, E, C>(
pub fn handle_window_event( pub fn handle_window_event(
event: &winit::event::WindowEvent<'_>, event: &winit::event::WindowEvent<'_>,
window: &winit::window::Window, window: &winit::window::Window,
scale_factor: f64,
control_flow: &mut winit::event_loop::ControlFlow, control_flow: &mut winit::event_loop::ControlFlow,
modifiers: &mut winit::event::ModifiersState, modifiers: &mut winit::event::ModifiersState,
viewport: &mut Viewport, viewport: &mut Viewport,
@ -298,8 +330,10 @@ pub fn handle_window_event(
WindowEvent::Resized(new_size) => { WindowEvent::Resized(new_size) => {
let size = Size::new(new_size.width, new_size.height); let size = Size::new(new_size.width, new_size.height);
*viewport = *viewport = Viewport::with_physical_size(
Viewport::with_physical_size(size, window.scale_factor()); size,
window.scale_factor() * scale_factor,
);
*resized = true; *resized = true;
} }
WindowEvent::CloseRequested => { WindowEvent::CloseRequested => {