use std::sync::atomic::{AtomicBool, Ordering};
#[cfg(any(target_os = "macos", target_os = "windows"))]
use super::Message;
#[cfg(any(target_os = "macos", target_os = "windows"))]
use iced::Task;
#[cfg(any(target_os = "macos", target_os = "windows"))]
use iced::window;
#[cfg(target_os = "macos")]
use objc2::{MainThreadMarker, rc::Retained};
#[cfg(target_os = "macos")]
use objc2_app_kit::{NSApplication, NSWindow};
static HIDDEN: AtomicBool = AtomicBool::new(false);
pub(crate) fn hidden() -> bool {
HIDDEN.load(Ordering::Relaxed)
}
pub fn set_hidden(hidden: bool) {
HIDDEN.store(hidden, Ordering::Relaxed);
}
#[cfg(any(target_os = "macos", target_os = "windows"))]
static LOGIN_START: AtomicBool = AtomicBool::new(false);
#[cfg(any(target_os = "macos", target_os = "windows"))]
pub fn set_login_start(login_start: bool) {
LOGIN_START.store(login_start, Ordering::Relaxed);
}
#[cfg(any(target_os = "macos", target_os = "windows"))]
pub(crate) fn login_start() -> bool {
LOGIN_START.load(Ordering::Relaxed)
}
#[cfg(any(target_os = "macos", target_os = "windows"))]
pub(crate) fn hide() -> Task<Message> {
set_hidden(true);
window::latest().and_then(|id| window::set_mode(id, window::Mode::Hidden))
}
#[cfg(any(target_os = "macos", target_os = "windows"))]
pub(crate) fn show() -> Task<Message> {
set_hidden(false);
#[cfg(target_os = "macos")]
bring_forward();
window::latest().and_then(|id| {
let shown = window::set_mode(id, window::Mode::Windowed);
#[cfg(target_os = "windows")]
let shown = shown
.chain(window::minimize(id, false))
.chain(window::gain_focus(id));
shown
})
}
#[cfg(target_os = "macos")]
pub(crate) fn opened() -> Task<Message> {
hide_title_strip();
if hidden() { Task::none() } else { show() }
}
#[cfg(target_os = "macos")]
fn hide_title_strip() {
use objc2_app_kit::{NSTitlebarSeparatorStyle, NSWindowButton};
let Some(marker) = MainThreadMarker::new() else {
tracing::warn!(
"the window's title strip is left as it is: AppKit is not used on the main thread"
);
return;
};
let Some(window) = dashboard_window(marker) else {
tracing::warn!(
"the window's title strip is left as it is: the dashboard's window was not found"
);
return;
};
for button in [
NSWindowButton::CloseButton,
NSWindowButton::MiniaturizeButton,
NSWindowButton::ZoomButton,
] {
if let Some(button) = window.standardWindowButton(button) {
button.setHidden(true);
}
}
window.setTitlebarSeparatorStyle(NSTitlebarSeparatorStyle::None);
window.setMovable(false);
}
#[cfg(target_os = "macos")]
fn dashboard_window(marker: MainThreadMarker) -> Option<Retained<NSWindow>> {
NSApplication::sharedApplication(marker)
.windows()
.iter()
.find(|window| window.isMiniaturizable())
}
#[cfg(target_os = "macos")]
fn bring_forward() {
let Some(marker) = MainThreadMarker::new() else {
return;
};
let app = NSApplication::sharedApplication(marker);
let window = dashboard_window(marker);
app.unhide(None);
#[expect(deprecated)]
app.activateIgnoringOtherApps(true);
let Some(window) = window else {
return;
};
window.deminiaturize(None);
}