use winit::window::Window;
#[cfg(all(unix, not(target_os = "macos")))]
mod wayland;
pub fn raise(window: &Window, token: Option<&str>) {
#[cfg(all(unix, not(target_os = "macos")))]
if crate::active_window_system() == crate::WindowSystem::Wayland {
match token {
Some(token) => wayland::activate_with_token(window, token),
None => request_attention(window),
}
return;
}
let _ = token;
#[cfg(target_os = "macos")]
if window.is_minimized() == Some(true) && raise_miniaturized(window) {
return;
}
window.focus_window();
}
#[cfg(target_os = "macos")]
fn raise_miniaturized(window: &Window) -> bool {
use objc2::MainThreadMarker;
use objc2::rc::Retained;
use objc2_app_kit::{NSApplication, NSView};
use winit::raw_window_handle::{HasWindowHandle, RawWindowHandle};
let Some(mtm) = MainThreadMarker::new() else {
return false;
};
let Ok(handle) = window.window_handle() else {
return false;
};
let RawWindowHandle::AppKit(appkit) = handle.as_raw() else {
return false;
};
unsafe {
let Some(view) = Retained::<NSView>::retain(appkit.ns_view.as_ptr().cast()) else {
return false;
};
let Some(ns_window) = view.window() else {
return false;
};
#[allow(deprecated)]
NSApplication::sharedApplication(mtm).activateIgnoringOtherApps(true);
ns_window.makeKeyAndOrderFront(None);
}
true
}
pub fn apply_token(
attrs: winit::window::WindowAttributes,
token: String,
) -> winit::window::WindowAttributes {
#[cfg(all(unix, not(target_os = "macos")))]
{
use winit::platform::startup_notify::WindowAttributesExtStartupNotify;
attrs.with_activation_token(winit::window::ActivationToken::from_raw(token))
}
#[cfg(not(all(unix, not(target_os = "macos"))))]
{
let _ = token;
attrs
}
}
pub fn apply_creation_token(
attrs: winit::window::WindowAttributes,
event_loop: &winit::event_loop::ActiveEventLoop,
) -> winit::window::WindowAttributes {
#[cfg(all(unix, not(target_os = "macos")))]
{
use winit::platform::startup_notify::{
EventLoopExtStartupNotify, WindowAttributesExtStartupNotify, reset_activation_token_env,
};
if let Some(token) = event_loop.read_token_from_env() {
reset_activation_token_env();
return attrs.with_activation_token(token);
}
}
let _ = event_loop;
attrs
}
pub fn request_activation_token(window: &Window) -> bool {
#[cfg(all(unix, not(target_os = "macos")))]
{
use winit::platform::startup_notify::WindowExtStartupNotify;
window.request_activation_token().is_ok()
}
#[cfg(not(all(unix, not(target_os = "macos"))))]
{
let _ = window;
false
}
}
pub fn set_child_activation_env(cmd: &mut std::process::Command, token: &str) {
#[cfg(all(unix, not(target_os = "macos")))]
{
cmd.env("XDG_ACTIVATION_TOKEN", token);
cmd.env("DESKTOP_STARTUP_ID", token);
}
#[cfg(not(all(unix, not(target_os = "macos"))))]
{
let _ = (cmd, token);
}
}
#[cfg(all(unix, not(target_os = "macos")))]
fn request_attention(window: &Window) {
window.request_user_attention(Some(winit::window::UserAttentionType::Informational));
}