Add scale_factor to Application and Sandbox
				
					
				
			This commit is contained in:
		
							parent
							
								
									d19c02035f
								
							
						
					
					
						commit
						c9696ca687
					
				@ -48,6 +48,7 @@ pub fn run<A, E, C>(
 | 
			
		||||
    let mut title = application.title();
 | 
			
		||||
    let mut mode = application.mode();
 | 
			
		||||
    let mut background_color = application.background_color();
 | 
			
		||||
    let mut scale_factor = application.scale_factor();
 | 
			
		||||
 | 
			
		||||
    let context = {
 | 
			
		||||
        let builder = settings.window.into_builder(
 | 
			
		||||
@ -75,7 +76,7 @@ pub fn run<A, E, C>(
 | 
			
		||||
    let physical_size = context.window().inner_size();
 | 
			
		||||
    let mut viewport = Viewport::with_physical_size(
 | 
			
		||||
        Size::new(physical_size.width, physical_size.height),
 | 
			
		||||
        context.window().scale_factor(),
 | 
			
		||||
        context.window().scale_factor() * scale_factor,
 | 
			
		||||
    );
 | 
			
		||||
    let mut resized = false;
 | 
			
		||||
 | 
			
		||||
@ -142,6 +143,20 @@ pub fn run<A, E, C>(
 | 
			
		||||
 | 
			
		||||
                // Update 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();
 | 
			
		||||
@ -195,6 +210,7 @@ pub fn run<A, E, C>(
 | 
			
		||||
            application::handle_window_event(
 | 
			
		||||
                &window_event,
 | 
			
		||||
                context.window(),
 | 
			
		||||
                scale_factor,
 | 
			
		||||
                control_flow,
 | 
			
		||||
                &mut modifiers,
 | 
			
		||||
                &mut viewport,
 | 
			
		||||
 | 
			
		||||
@ -186,6 +186,21 @@ pub trait Application: Sized {
 | 
			
		||||
        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`].
 | 
			
		||||
    ///
 | 
			
		||||
    /// On native platforms, this method will take control of the current thread
 | 
			
		||||
@ -272,6 +287,10 @@ where
 | 
			
		||||
    fn background_color(&self) -> Color {
 | 
			
		||||
        self.0.background_color()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn scale_factor(&self) -> f64 {
 | 
			
		||||
        self.0.scale_factor()
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[cfg(target_arch = "wasm32")]
 | 
			
		||||
 | 
			
		||||
@ -130,12 +130,27 @@ pub trait Sandbox {
 | 
			
		||||
    ///
 | 
			
		||||
    /// By default, it returns [`Color::WHITE`].
 | 
			
		||||
    ///
 | 
			
		||||
    /// [`Application`]: trait.Application.html
 | 
			
		||||
    /// [`Sandbox`]: trait.Sandbox.html
 | 
			
		||||
    /// [`Color::WHITE`]: struct.Color.html#const.WHITE
 | 
			
		||||
    fn background_color(&self) -> Color {
 | 
			
		||||
        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`].
 | 
			
		||||
    ///
 | 
			
		||||
    /// On native platforms, this method will take control of the current thread
 | 
			
		||||
@ -185,4 +200,8 @@ where
 | 
			
		||||
    fn background_color(&self) -> Color {
 | 
			
		||||
        T::background_color(self)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn scale_factor(&self) -> f64 {
 | 
			
		||||
        T::scale_factor(self)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -84,6 +84,21 @@ pub trait Application: Program {
 | 
			
		||||
    fn background_color(&self) -> Color {
 | 
			
		||||
        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
 | 
			
		||||
@ -124,6 +139,7 @@ pub fn run<A, E, C>(
 | 
			
		||||
    let mut title = application.title();
 | 
			
		||||
    let mut mode = application.mode();
 | 
			
		||||
    let mut background_color = application.background_color();
 | 
			
		||||
    let mut scale_factor = application.scale_factor();
 | 
			
		||||
 | 
			
		||||
    let window = settings
 | 
			
		||||
        .window
 | 
			
		||||
@ -138,7 +154,7 @@ pub fn run<A, E, C>(
 | 
			
		||||
    let physical_size = window.inner_size();
 | 
			
		||||
    let mut viewport = Viewport::with_physical_size(
 | 
			
		||||
        Size::new(physical_size.width, physical_size.height),
 | 
			
		||||
        window.scale_factor(),
 | 
			
		||||
        window.scale_factor() * scale_factor,
 | 
			
		||||
    );
 | 
			
		||||
    let mut resized = false;
 | 
			
		||||
 | 
			
		||||
@ -208,6 +224,20 @@ pub fn run<A, E, C>(
 | 
			
		||||
 | 
			
		||||
                // Update 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();
 | 
			
		||||
@ -259,6 +289,7 @@ pub fn run<A, E, C>(
 | 
			
		||||
            handle_window_event(
 | 
			
		||||
                &window_event,
 | 
			
		||||
                &window,
 | 
			
		||||
                scale_factor,
 | 
			
		||||
                control_flow,
 | 
			
		||||
                &mut modifiers,
 | 
			
		||||
                &mut viewport,
 | 
			
		||||
@ -286,6 +317,7 @@ pub fn run<A, E, C>(
 | 
			
		||||
pub fn handle_window_event(
 | 
			
		||||
    event: &winit::event::WindowEvent<'_>,
 | 
			
		||||
    window: &winit::window::Window,
 | 
			
		||||
    scale_factor: f64,
 | 
			
		||||
    control_flow: &mut winit::event_loop::ControlFlow,
 | 
			
		||||
    modifiers: &mut winit::event::ModifiersState,
 | 
			
		||||
    viewport: &mut Viewport,
 | 
			
		||||
@ -298,8 +330,10 @@ pub fn handle_window_event(
 | 
			
		||||
        WindowEvent::Resized(new_size) => {
 | 
			
		||||
            let size = Size::new(new_size.width, new_size.height);
 | 
			
		||||
 | 
			
		||||
            *viewport =
 | 
			
		||||
                Viewport::with_physical_size(size, window.scale_factor());
 | 
			
		||||
            *viewport = Viewport::with_physical_size(
 | 
			
		||||
                size,
 | 
			
		||||
                window.scale_factor() * scale_factor,
 | 
			
		||||
            );
 | 
			
		||||
            *resized = true;
 | 
			
		||||
        }
 | 
			
		||||
        WindowEvent::CloseRequested => {
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user