Implement UI scale overriding

Closes #16
This commit is contained in:
meehl committed 2025-11-30 11:16:04 +01:00
1 parent df06c8f9c6
commit 1812edcbe9
7 files changed
+46 -24

No files matched your search

+4
View File
@@ -2,6 +2,10 @@
## [Unreleased] - ReleaseDate
### Added
- Implement UI scale overriding
### Changed
- Reduce CPU usage while window is unfocused and not hovered over
+4 -7
View File
@@ -390,9 +390,8 @@ unsafe extern "C-unwind" fn draw_string(state: *mut ffi::lua_State) -> c_int {
ctx.layers().set_draw_color(last_segment.color);
}
let layout = ctx.fonts().layout(job, ctx.window().scale_factor);
ctx
.layers()
let layout = ctx.fonts().layout(job, ctx.window().scale_factor());
ctx.layers()
.draw_text(position, layout, is_absolute_position);
0
@@ -415,9 +414,7 @@ unsafe extern "C-unwind" fn get_string_width(state: *mut ffi::lua_State) -> c_in
};
let job = build_layout_job(text, Srgba::WHITE, font_type, line_height, None);
let width = ctx
.fonts()
.get_text_width(job, ctx.window().scale_factor);
let width = ctx.fonts().get_text_width(job, ctx.window().scale_factor());
unsafe { ffi::lua_pushnumber(state, width as f64) };
1
@@ -436,7 +433,7 @@ fn get_index_at_cur(
let index = ctx.fonts().get_text_index_at_cursor(
job,
Point::new(cur_x, cur_y),
ctx.window().scale_factor,
ctx.window().scale_factor(),
);
// convert to lua's 1-based indexing
+14 -6
View File
@@ -18,7 +18,7 @@ pub fn get_screen_size(l: &Lua, _: ()) -> LuaResult<(u32, u32)> {
pub fn get_screen_scale(l: &Lua, _: ()) -> LuaResult<f32> {
let ctx = l.app_data_ref::<&'static Context>().unwrap();
let scale_factor = ctx.window().scale_factor;
let scale_factor = ctx.window().scale_factor();
Ok(scale_factor)
}
@@ -34,12 +34,20 @@ pub fn set_foreground(l: &Lua, _: ()) -> LuaResult<()> {
Ok(())
}
pub fn set_dpi_scale_override(_: &Lua, _percent: i32) -> LuaResult<()> {
// TODO:
pub fn set_dpi_scale_override(l: &Lua, percent: i32) -> LuaResult<()> {
let socket = l.app_data_ref::<&'static Context>().unwrap();
match percent {
0 => socket.window().scale_factor_override = None,
p if p > 0 => socket.window().scale_factor_override = Some(p as f32 / 100.0),
_ => {}
}
Ok(())
}
pub fn get_dpi_scale_override(_: &Lua, _: ()) -> LuaResult<i32> {
// TODO:
Ok(0)
pub fn get_dpi_scale_override(l: &Lua, _: ()) -> LuaResult<i32> {
let socket = l.app_data_ref::<&'static Context>().unwrap();
match socket.window().scale_factor_override {
Some(scale_factor) => Ok((scale_factor * 100.0) as i32),
None => Ok(0),
}
}
+4 -4
View File
@@ -32,7 +32,7 @@ pub struct AppState {
impl AppState {
fn set_mouse_pos(&mut self, pos: PhysicalPoint<f32>) {
self.input
.set_mouse_pos(pos.to_logical(self.window.scale_factor));
.set_mouse_pos(pos.to_logical(self.window.scale_factor()));
}
}
@@ -98,7 +98,7 @@ impl App {
let meshes = self.tessellator.convert_clipped_primitives(
mode_output.primitives,
font_atlas_size,
self.state.window.scale_factor,
self.state.window.scale_factor(),
);
RenderJob::Render {
@@ -209,7 +209,7 @@ impl ApplicationHandler<GraphicsContext> for App {
};
if let Some(ref mut gfx) = self.gfx_context {
match gfx.render(render_job) {
match gfx.render(render_job, self.state.window.scale_factor()) {
Ok(_) => {
self.force_render = should_continue;
@@ -242,7 +242,7 @@ impl ApplicationHandler<GraphicsContext> for App {
}
}
WindowEvent::ScaleFactorChanged { scale_factor, .. } => {
self.state.window.scale_factor = scale_factor as f32;
self.state.window.set_scale_factor(scale_factor as f32);
}
WindowEvent::KeyboardInput {
event:
+7 -4
View File
@@ -144,7 +144,11 @@ impl GraphicsContext {
}
}
pub fn render(&mut self, render_job: RenderJob) -> Result<(), wgpu::SurfaceError> {
pub fn render(
&mut self,
render_job: RenderJob,
scale_factor: f32,
) -> Result<(), wgpu::SurfaceError> {
profiling::scope!("render");
if !self.is_surface_configured {
@@ -172,7 +176,6 @@ impl GraphicsContext {
} = render_job
{
let screen_size = PhysicalSize::new(self.config.width, self.config.height);
let pixels_per_point = self.window.as_ref().scale_factor() as f32;
// upload new textures
self.renderer
@@ -184,7 +187,7 @@ impl GraphicsContext {
&self.queue,
&meshes,
screen_size,
pixels_per_point,
scale_factor,
);
let rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
@@ -212,7 +215,7 @@ impl GraphicsContext {
&mut rpass.forget_lifetime(),
&meshes,
screen_size,
pixels_per_point,
scale_factor,
);
self.renderer.free_textures(&textures_delta);
+1 -1
View File
@@ -137,7 +137,7 @@ impl InstallMode {
};
job.append(&progress_text, Srgba::WHITE);
let layout = app_state.fonts.layout(job, app_state.window.scale_factor);
let layout = app_state.fonts.layout(job, app_state.window.scale_factor());
// center text vertically and horizontally
let screen_size = app_state.window.logical_size().cast::<f32>();
+12 -2
View File
@@ -11,7 +11,8 @@ pub struct WindowState {
clipboard: Option<Clipboard>,
pub window: Option<Arc<Window>>,
pub size: PhysicalSize<u32>,
pub scale_factor: f32,
scale_factor: f32,
pub scale_factor_override: Option<f32>,
pending_window_title: std::cell::Cell<Option<String>>,
pub is_hovered: bool,
pub is_focused: bool,
@@ -23,6 +24,7 @@ impl Default for WindowState {
window: None,
size: Default::default(),
scale_factor: 1.0,
scale_factor_override: None,
pending_window_title: std::cell::Cell::new(None),
clipboard: None,
is_hovered: true,
@@ -55,7 +57,15 @@ impl WindowState {
}
pub fn logical_size(&self) -> LogicalSize<u32> {
self.size.to_logical(self.scale_factor)
self.size.to_logical(self.scale_factor())
}
pub fn scale_factor(&self) -> f32 {
self.scale_factor_override.unwrap_or(self.scale_factor)
}
pub fn set_scale_factor(&mut self, scale_factor: f32) {
self.scale_factor = scale_factor;
}
pub fn focus(&self) {