From 3e3bb8a8f1b808424f2eaf262130f8830dedbaa2 Mon Sep 17 00:00:00 2001 From: David Yamnitsky Date: Sun, 19 Jan 2020 18:38:52 -0500 Subject: [PATCH] add hide_application on macos (#1364) Co-authored-by: Osspial Co-authored-by: Bogaevsky --- CHANGELOG.md | 1 + src/platform/macos.rs | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0eea9c87..cd299bdc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # Unreleased +- On macOS, add `hide_application` to `EventLoopWindowTarget` via a new `EventLoopWindowTargetExtMacOS` trait. `hide_application` will hide the entire application by calling `-[NSApplication hide: nil]`. - On macOS, fix not sending ReceivedCharacter event for specific keys combinations. - On macOS, fix `CursorMoved` event reporting the cursor position using logical coordinates. - On macOS, fix issue where unbundled applications would sometimes open without being focused. diff --git a/src/platform/macos.rs b/src/platform/macos.rs index cfedaab2..b1a298b3 100644 --- a/src/platform/macos.rs +++ b/src/platform/macos.rs @@ -4,6 +4,7 @@ use std::os::raw::c_void; use crate::{ dpi::LogicalSize, + event_loop::{EventLoop, EventLoopWindowTarget}, monitor::MonitorHandle, window::{Window, WindowBuilder}, }; @@ -209,3 +210,17 @@ impl MonitorHandleExtMacOS for MonitorHandle { self.inner.ns_screen().map(|s| s as *mut c_void) } } + +/// Additional methods on `EventLoopWindowTarget` that are specific to macOS. +pub trait EventLoopWindowTargetExtMacOS { + /// Hide the entire application. In most applications this is typically triggered with Command-H. + fn hide_application(&self); +} + +impl EventLoopWindowTargetExtMacOS for EventLoopWindowTarget { + fn hide_application(&self) { + let cls = objc::runtime::Class::get("NSApplication").unwrap(); + let app: cocoa::base::id = unsafe { msg_send![cls, sharedApplication] }; + unsafe { msg_send![app, hide: 0] } + } +}