use iced::window::{self, Level, Position};
use iced::{Point, Size};
use crate::layout::{window_size_from_dock, DockSpec};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WindowKind {
Application,
Dialog,
Overlay,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HidePolicy {
Escape,
FocusLoss,
EscapeOrFocusLoss,
Manual,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HideEvent {
Escape,
FocusLoss,
}
pub fn should_hide(policy: HidePolicy, event: HideEvent, in_card: bool) -> bool {
if in_card && event == HideEvent::FocusLoss {
return false;
}
match (policy, event) {
(HidePolicy::Manual, _) => false,
(HidePolicy::Escape, HideEvent::Escape) => true,
(HidePolicy::FocusLoss, HideEvent::FocusLoss) => true,
(HidePolicy::EscapeOrFocusLoss, _) => true,
_ => false,
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct DisplayBounds {
pub x: f32,
pub y: f32,
pub width: f32,
pub height: f32,
}
impl DisplayBounds {
pub fn contains(self, pointer: (f32, f32)) -> bool {
pointer.0 >= self.x
&& pointer.0 < self.x + self.width
&& pointer.1 >= self.y
&& pointer.1 < self.y + self.height
}
}
pub fn place(pointer: (f32, f32), size: Size, displays: &[DisplayBounds]) -> Point {
let Some(d) = display_at(pointer, displays) else {
return Point::new(pointer.0, pointer.1);
};
clamp_on_display(pointer.0, pointer.1, size, d)
}
pub fn place_centered(pointer: (f32, f32), size: Size, displays: &[DisplayBounds]) -> Point {
let Some(d) = display_at(pointer, displays) else {
return Point::new(pointer.0 - size.width / 2.0, pointer.1 - size.height / 2.0);
};
clamp_on_display(
d.x + (d.width - size.width) / 2.0,
d.y + (d.height - size.height) / 2.0,
size,
d,
)
}
pub fn chosen_display(
pin: Option<usize>,
pointer: (f32, f32),
displays: &[DisplayBounds],
) -> Option<DisplayBounds> {
if let Some(i) = pin {
if let Some(d) = displays.get(i).copied() {
return Some(d);
}
}
display_at(pointer, displays)
}
pub fn place_pinned(
pin: Option<usize>,
pointer: (f32, f32),
size: Size,
displays: &[DisplayBounds],
) -> Point {
let Some(d) = chosen_display(pin, pointer, displays) else {
return Point::new(pointer.0, pointer.1);
};
clamp_on_display(pointer.0, pointer.1, size, d)
}
fn display_at(pointer: (f32, f32), displays: &[DisplayBounds]) -> Option<DisplayBounds> {
displays
.iter()
.copied()
.find(|d| d.contains(pointer))
.or_else(|| displays.first().copied())
}
fn clamp_on_display(x: f32, y: f32, size: Size, d: DisplayBounds) -> Point {
let max_x = (d.x + d.width - size.width).max(d.x);
let max_y = (d.y + d.height - size.height).max(d.y);
Point::new(x.clamp(d.x, max_x), y.clamp(d.y, max_y))
}
pub fn settings(kind: WindowKind, application_id: &str) -> window::Settings {
let (def, min) = window_size_from_dock(DockSpec::default());
let mut win = match kind {
WindowKind::Application => window::Settings {
size: def,
min_size: Some(min),
resizable: true,
decorations: true,
level: Level::Normal,
position: Position::Default,
exit_on_close_request: true,
..window::Settings::default()
},
WindowKind::Dialog => window::Settings {
size: Size::new(480.0, 320.0),
min_size: Some(Size::new(360.0, 200.0)),
resizable: true,
decorations: true,
level: Level::Normal,
position: Position::Centered,
exit_on_close_request: true,
..window::Settings::default()
},
WindowKind::Overlay => window::Settings {
size: Size::new(720.0, 480.0),
min_size: Some(Size::new(200.0, 160.0)),
max_size: None,
resizable: false,
decorations: false,
level: Level::AlwaysOnTop,
position: Position::Centered,
exit_on_close_request: false,
..window::Settings::default()
},
};
apply_app_id(&mut win, application_id, kind);
win
}
pub fn retarget(win: &mut window::Settings, application_id: &str) {
win.resizable = true;
win.decorations = true;
win.level = Level::Normal;
win.exit_on_close_request = true;
apply_app_id(win, application_id, WindowKind::Application);
}
fn apply_app_id(win: &mut window::Settings, application_id: &str, kind: WindowKind) {
#[cfg(target_os = "linux")]
{
win.platform_specific.application_id = application_id.to_string();
win.platform_specific.override_redirect = matches!(kind, WindowKind::Overlay);
}
#[cfg(target_os = "windows")]
{
win.platform_specific.skip_taskbar = matches!(kind, WindowKind::Overlay);
}
let _ = (win, application_id, kind);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn kinds_and_hide_policy() {
assert!(!should_hide(HidePolicy::Manual, HideEvent::Escape, false));
assert!(!should_hide(
HidePolicy::Manual,
HideEvent::FocusLoss,
false
));
assert!(should_hide(HidePolicy::Escape, HideEvent::Escape, false));
assert!(!should_hide(
HidePolicy::Escape,
HideEvent::FocusLoss,
false
));
assert!(should_hide(
HidePolicy::FocusLoss,
HideEvent::FocusLoss,
false
));
assert!(!should_hide(
HidePolicy::FocusLoss,
HideEvent::Escape,
false
));
assert!(should_hide(
HidePolicy::EscapeOrFocusLoss,
HideEvent::Escape,
false
));
assert!(should_hide(
HidePolicy::EscapeOrFocusLoss,
HideEvent::FocusLoss,
false
));
let app = settings(WindowKind::Application, "dev.icedtea.test");
assert!(app.decorations && app.resizable);
let dlg = settings(WindowKind::Dialog, "dev.icedtea.test");
assert!(matches!(dlg.position, Position::Centered));
let ov = settings(WindowKind::Overlay, "dev.icedtea.test");
assert!(!ov.decorations);
assert_eq!(ov.level, Level::AlwaysOnTop);
assert!(ov.max_size.is_none());
assert_eq!(ov.size, Size::new(720.0, 480.0));
let d = DisplayBounds {
x: 0.0,
y: 0.0,
width: 1920.0,
height: 1080.0,
};
let other = DisplayBounds {
x: 1920.0,
y: 0.0,
width: 1280.0,
height: 800.0,
};
let p = place((100.0, 80.0), Size::new(400.0, 300.0), &[d, other]);
assert_eq!(p, Point::new(100.0, 80.0));
let edge = place((1900.0, 1000.0), Size::new(400.0, 300.0), &[d]);
assert!((edge.x - 1520.0).abs() < 0.01);
assert!((edge.y - 780.0).abs() < 0.01);
let second = place((2000.0, 10.0), Size::new(200.0, 100.0), &[d, other]);
assert!(second.x >= 1920.0);
let raw = place((8.0, 9.0), Size::new(10.0, 10.0), &[]);
assert_eq!(raw, Point::new(8.0, 9.0));
let fallback = place((-10.0, -10.0), Size::new(100.0, 80.0), &[other]);
assert_eq!(fallback.x, other.x);
assert_eq!(fallback.y, other.y);
assert!(d.contains((10.0, 10.0)));
assert!(!d.contains((2000.0, 10.0)));
let pinned = place_pinned(Some(1), (10.0, 10.0), Size::new(400.0, 300.0), &[d, other]);
assert!(pinned.x >= 1920.0);
assert_eq!(
chosen_display(Some(1), (10.0, 10.0), &[d, other]),
Some(other)
);
assert_eq!(chosen_display(None, (100.0, 80.0), &[d, other]), Some(d));
let miss_pin = place_pinned(Some(9), (100.0, 80.0), Size::new(10.0, 10.0), &[d, other]);
assert_eq!(miss_pin, Point::new(100.0, 80.0));
let empty_pin = place_pinned(Some(0), (3.0, 4.0), Size::new(10.0, 10.0), &[]);
assert_eq!(empty_pin, Point::new(3.0, 4.0));
let none_pin = place_pinned(None, (3.0, 4.0), Size::new(10.0, 10.0), &[]);
assert_eq!(none_pin, Point::new(3.0, 4.0));
}
#[test]
fn place_centered_uses_pointer_display() {
let left = DisplayBounds {
x: 0.0,
y: 0.0,
width: 1920.0,
height: 1080.0,
};
let right = DisplayBounds {
x: 1920.0,
y: 0.0,
width: 1280.0,
height: 800.0,
};
let size = Size::new(400.0, 300.0);
let mid = place_centered((2000.0, 10.0), size, &[left, right]);
assert_eq!(mid, Point::new(2360.0, 250.0));
let first = place_centered((100.0, 80.0), size, &[left, right]);
assert_eq!(first, Point::new(760.0, 390.0));
let miss = place_centered((-10.0, -10.0), size, &[left, right]);
assert_eq!(miss, Point::new(760.0, 390.0));
let empty = place_centered((8.0, 9.0), Size::new(10.0, 10.0), &[]);
assert_eq!(empty, Point::new(3.0, 4.0));
let huge = place_centered((100.0, 80.0), Size::new(3000.0, 2000.0), &[left]);
assert_eq!(huge, Point::new(0.0, 0.0));
}
#[test]
fn in_card_suppresses_focus_loss_only() {
assert!(should_hide(HidePolicy::Escape, HideEvent::Escape, true));
assert!(should_hide(
HidePolicy::EscapeOrFocusLoss,
HideEvent::Escape,
true
));
assert!(!should_hide(HidePolicy::Escape, HideEvent::FocusLoss, true));
assert!(!should_hide(
HidePolicy::EscapeOrFocusLoss,
HideEvent::FocusLoss,
true
));
assert!(!should_hide(
HidePolicy::FocusLoss,
HideEvent::FocusLoss,
true
));
assert!(!should_hide(HidePolicy::Manual, HideEvent::Escape, true));
}
#[test]
fn retarget_makes_decorated_application() {
let mut s = settings(WindowKind::Overlay, "dev.icedtea.test");
let size = s.size;
assert!(!s.decorations);
assert!(!s.resizable);
assert_eq!(s.level, Level::AlwaysOnTop);
assert!(!s.exit_on_close_request);
#[cfg(target_os = "linux")]
assert!(s.platform_specific.override_redirect);
#[cfg(target_os = "windows")]
assert!(s.platform_specific.skip_taskbar);
retarget(&mut s, "dev.icedtea.test");
assert!(s.decorations && s.resizable);
assert_eq!(s.level, Level::Normal);
assert!(s.exit_on_close_request);
assert_eq!(s.size, size);
#[cfg(target_os = "linux")]
{
assert!(!s.platform_specific.override_redirect);
assert_eq!(s.platform_specific.application_id, "dev.icedtea.test");
}
#[cfg(target_os = "windows")]
assert!(!s.platform_specific.skip_taskbar);
}
}