70 行
1.7 KiB
Rust
70 行
1.7 KiB
Rust
use serde::{Deserialize, Serialize};
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct MonitorGeometry {
|
|
pub left: i32,
|
|
pub top: i32,
|
|
pub width: u32,
|
|
pub height: u32,
|
|
pub work_left: i32,
|
|
pub work_top: i32,
|
|
pub work_width: u32,
|
|
pub work_height: u32,
|
|
pub is_primary: bool,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub enum PetPositionMode {
|
|
Free,
|
|
Edge,
|
|
Monitor,
|
|
FollowMainWindow,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct PetPlacement {
|
|
pub monitor_index: usize,
|
|
pub x: i32,
|
|
pub y: i32,
|
|
}
|
|
|
|
pub fn place_at_safe_edge(
|
|
monitor: MonitorGeometry,
|
|
pet_width: u32,
|
|
pet_height: u32,
|
|
margin: i32,
|
|
) -> PetPlacement {
|
|
let width = pet_width.min(monitor.work_width) as i32;
|
|
let height = pet_height.min(monitor.work_height) as i32;
|
|
PetPlacement {
|
|
monitor_index: 0,
|
|
x: monitor.work_left + (monitor.work_width as i32 - width - margin).max(0),
|
|
y: monitor.work_top + (monitor.work_height as i32 - height - margin).max(0),
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn keeps_pet_inside_work_area() {
|
|
let monitor = MonitorGeometry {
|
|
left: 0,
|
|
top: 0,
|
|
width: 1920,
|
|
height: 1080,
|
|
work_left: 0,
|
|
work_top: 0,
|
|
work_width: 1920,
|
|
work_height: 1040,
|
|
is_primary: true,
|
|
};
|
|
let placement = place_at_safe_edge(monitor, 120, 120, 16);
|
|
assert_eq!(placement.x, 1784);
|
|
assert_eq!(placement.y, 904);
|
|
}
|
|
}
|