Skip to content

Commit

Permalink
only scale px values
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasWischeropp committed Oct 2, 2024
1 parent 661a17e commit 2285c43
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
7 changes: 7 additions & 0 deletions packages/desktop/src/common/length_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ impl LengthValue {
LengthUnit::Pixel => self.amount as i32,
}
}

pub fn to_px_scaled(&self, total_px: i32, scale_factor: f32) -> i32 {
match self.unit {
LengthUnit::Percentage => self.to_px(total_px),
LengthUnit::Pixel => (scale_factor * self.amount) as i32,
}
}
}

impl FromStr for LengthValue {
Expand Down
46 changes: 22 additions & 24 deletions packages/desktop/src/widget_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use std::{
use anyhow::{bail, Context};
use serde::Serialize;
use tauri::{
AppHandle, LogicalPosition, LogicalSize, Manager, PhysicalPosition,
PhysicalSize, WebviewUrl, WebviewWindowBuilder, WindowEvent,
AppHandle, Manager, PhysicalPosition, PhysicalSize, WebviewUrl,
WebviewWindowBuilder, WindowEvent,
};
use tokio::{
sync::{broadcast, Mutex},
Expand Down Expand Up @@ -278,30 +278,28 @@ impl WidgetFactory {
),
};

let size = {
let size = PhysicalSize::<f64>::from_logical(
LogicalSize::new(
placement.width.to_px(monitor.width as i32),
placement.height.to_px(monitor.height as i32),
),
monitor.scale_factor,
);
PhysicalSize::new(
f64::min(size.width, monitor.width as f64),
f64::min(size.height, monitor.height as f64),
)
};
let width = placement
.width
.to_px_scaled(monitor.width as i32, monitor.scale_factor as f32);
let height = placement.height.to_px_scaled(
monitor.height as i32,
monitor.scale_factor as f32,
);
let size = PhysicalSize::<f64>::new(
i32::min(width, monitor.width as i32) as f64,
i32::min(height, monitor.height as i32) as f64,
);

let offset = PhysicalPosition::<f64>::from_logical(
LogicalPosition::new(
placement.offset_x.to_px(monitor.width as i32),
placement.offset_y.to_px(monitor.height as i32),
),
monitor.scale_factor,
let offset_x = placement
.offset_x
.to_px_scaled(monitor.width as i32, monitor.scale_factor as f32);
let offset_y = placement.offset_y.to_px_scaled(
monitor.height as i32,
monitor.scale_factor as f32,
);
let position = PhysicalPosition::new(
anchor_x as f64 + offset.x,
anchor_y as f64 + offset.y,
let position = PhysicalPosition::<f64>::new(
(anchor_x + offset_x) as f64,
(anchor_y + offset_y) as f64,
);

placements.push((size, position));
Expand Down

0 comments on commit 2285c43

Please sign in to comment.