Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::{
};

pub const DEFAULT_DPI: f64 = 96.0;
pub const DEFAULT_SCALE: f64 = 1.0;

pub struct NLockRenderBackgroundArgs<'a> {
pub buf_height: f64,
Expand Down Expand Up @@ -60,6 +61,7 @@ impl<'a> NLockRenderOverlayArgs<'a> {
#[derive(Default)]
pub struct NLockRenderer {
dpi: Option<f64>,
scale: Option<f64>,
subpixel_order: Option<cairo::SubpixelOrder>,
}

Expand All @@ -82,6 +84,12 @@ impl NLockRenderer {
self.subpixel_order = Some(order);
}

pub fn set_scale(&mut self, scale: f64) {
if scale > 0.0 {
self.scale = Some(scale);
}
}

fn clear_background(&self, context: &cairo::Context) -> Result<()> {
context.save()?;
context.set_operator(cairo::Operator::Source);
Expand Down Expand Up @@ -114,7 +122,8 @@ impl NLockRenderer {
);

let dpi = self.dpi.unwrap_or(DEFAULT_DPI);
context.set_font_size((config.font.size / 72.0) * dpi);
let scale = self.scale.unwrap_or(DEFAULT_SCALE);
context.set_font_size((config.font.size / 72.0) * dpi * scale);

Ok(())
}
Expand Down Expand Up @@ -236,12 +245,14 @@ impl NLockRenderer {
// Reset the context for fresh rendering
self.reset_cairo_context(context)?;

let scale = self.scale.unwrap_or(DEFAULT_SCALE);

// Draw border colour
context.save()?;
self.set_frame_border_color(config, context, auth_state);
context.set_line_width(config.frame.border);
context.set_line_width(config.frame.border * scale);

let frame_offset = config.frame.border / 2.0;
let frame_offset = (config.frame.border * scale) / 2.0;
let frame_w = buf_width - (frame_offset * 2.0);
let frame_h = buf_height - (frame_offset * 2.0);

Expand All @@ -251,7 +262,7 @@ impl NLockRenderer {
frame_offset,
frame_w,
frame_h,
config.frame.radius,
config.frame.radius * scale,
);
context.stroke()?;
context.restore()?;
Expand Down Expand Up @@ -283,8 +294,8 @@ impl NLockRenderer {
let inner_x = (buf_width - inner_w) / 2.0;
let inner_y = (buf_height - inner_h) / 2.0;

let outer_h = inner_h + (padding_y * 2.0) + config.input.border;
let outer_w = inner_w + (padding_x * 2.0) + config.input.border;
let outer_h = inner_h + (padding_y * 2.0) + (config.input.border * scale);
let outer_w = inner_w + (padding_x * 2.0) + (config.input.border * scale);
let outer_x = (buf_width - outer_w) / 2.0;
let outer_y = (buf_height - outer_h) / 2.0;

Expand All @@ -303,7 +314,7 @@ impl NLockRenderer {
context.ext_set_source_rgba(config.colors.input_bg);
context.fill_preserve()?;
context.ext_set_source_rgba(config.colors.input_border);
context.set_line_width(config.input.border);
context.set_line_width(config.input.border * scale);
context.stroke_preserve()?;
context.clip();

Expand Down
20 changes: 11 additions & 9 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,15 +331,17 @@ impl Dispatch<wl_output::WlOutput, usize> for NLockState {
state.surfaces[*data].output_name = Some(name);
}
wl_output::Event::Scale { factor } => {
debug!(
"Set output scale for '{}' to {factor}",
state.surfaces[*data]
.output_name
.as_ref()
.unwrap_or(&"".to_string())
);

state.surfaces[*data].output_scale = factor;
if let Err(e) = state.surfaces[*data].set_scale(factor) {
warn!("Failed to set output scale: {e}");
} else {
debug!(
"Set output scale for '{}' to {factor}",
state.surfaces[*data]
.output_name
.as_ref()
.unwrap_or(&"".to_string())
);
}
}
wl_output::Event::Done => {
if let (Some(compositor), Some(subcompositor), Some(session_lock)) =
Expand Down
13 changes: 12 additions & 1 deletion src/surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ pub struct NLockSurface {
pub bg_rendered: bool,
pub index: usize,
pub output_name: Option<String>,
pub output_scale: i32,

output_scale: i32,
width: Option<u32>,
height: Option<u32>,
last_width: Option<u32>,
Expand Down Expand Up @@ -156,6 +156,17 @@ impl NLockSurface {
self.renderer.set_subpixel_order(order);
}

pub fn set_scale(&mut self, scale: i32) -> Result<()> {
if scale <= 0 {
bail!("Invalid scale {}", scale);
}

self.output_scale = scale;
self.renderer.set_scale(scale as f64);

Ok(())
}

fn update_last_dimensions(&mut self) -> Result<()> {
let (width, height) = self.get_dimensions::<u32>()?;

Expand Down