use gpui::{point, px, size, AnyWindowHandle, App, Window, WindowHandle, WindowId};
use super::PopoutShell;
pub(super) struct Live {
pub always_on_top: bool,
pub click_through: Option<bool>,
pub protection: Option<bool>,
pub bounds: Option<[Option<f32>; 4]>,
}
pub(super) fn apply_live(handle: WindowHandle<PopoutShell>, live: Live, cx: &mut App) {
let _ = AnyWindowHandle::from(handle).update(cx, |_, window, _| {
window.vn_set_always_on_top(live.always_on_top);
if let Some(on) = live.click_through {
window.vn_set_ignore_mouse(on);
}
if let Some(on) = live.protection {
window.vn_set_content_protection(on);
}
if let Some(bounds) = live.bounds {
set_client_bounds(window, bounds);
}
});
}
fn set_client_bounds(window: &mut Window, [x, y, w, h]: [Option<f32>; 4]) {
let frame = window.bounds();
let client = window.viewport_size();
let title_bar = (frame.size.height - client.height).max(px(0.0));
if w.is_some() || h.is_some() {
window.resize(size(
w.map_or(client.width, px),
h.map_or(client.height, px),
));
}
if x.is_some() || y.is_some() {
let origin = point(
x.map_or(frame.origin.x, px),
y.map_or(frame.origin.y, |y| px(y) - title_bar),
);
window.vn_set_frame(Some(origin), None);
}
}
pub(super) fn show(
id: WindowId,
handle: WindowHandle<PopoutShell>,
cover: bool,
activate: bool,
on_top: bool,
cx: &mut App,
) {
let _ = AnyWindowHandle::from(handle).update(cx, |_, window, cx| {
if cover {
if let Some(display) = window.display(cx) {
let b = display.bounds();
window.vn_set_frame(Some(b.origin), Some(b.size));
}
}
window.refresh();
});
cx.spawn(async move |cx| {
cx.update(|cx| {
let _ = AnyWindowHandle::from(handle).update(cx, |_, window, _| {
if !window.show_window(activate, || true, |_| {}) && activate {
window.activate_window();
}
if on_top {
window.vn_set_always_on_top(true);
}
});
super::shown(id, cx);
});
})
.detach();
}
pub(super) fn hide(id: WindowId, cx: &mut App) -> bool {
let Some(handle) =
super::POPOUTS.with(|p| p.borrow().iter().find(|w| w.id == id).map(|w| w.handle))
else {
return false;
};
AnyWindowHandle::from(handle)
.update(cx, |_, window, _| {
if !window.vn_hide() {
window.minimize_window();
}
})
.is_ok()
}