Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Application:: hide_application, hide_others and set_menu to Mac platform extension #1863

Merged
merged 5 commits into from
Jul 8, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 0 additions & 22 deletions druid-shell/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,28 +170,6 @@ impl Application {
self.backend_app.quit()
}

// TODO: do these three go in some kind of PlatformExt trait?
/// Hide the application this window belongs to. (cmd+H)
pub fn hide(&self) {
#[cfg(target_os = "macos")]
self.backend_app.hide()
}

/// Hide all other applications. (cmd+opt+H)
pub fn hide_others(&self) {
#[cfg(target_os = "macos")]
self.backend_app.hide_others()
}

/// Sets the global application menu, on platforms where there is one.
///
/// On platforms with no global application menu, this has no effect.
#[allow(unused_variables)]
pub fn set_menu(&self, menu: crate::Menu) {
#[cfg(target_os = "macos")]
self.backend_app.set_menu(menu.into_inner());
}

/// Returns a handle to the system clipboard.
pub fn clipboard(&self) -> Clipboard {
self.backend_app.clipboard().into()
Expand Down
45 changes: 22 additions & 23 deletions druid-shell/src/backend/mac/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ use crate::application::AppHandler;

use super::clipboard::Clipboard;
use super::error::Error;
use super::menu::Menu;
use super::util;

static APP_HANDLER_IVAR: &str = "druidAppHandler";
Expand Down Expand Up @@ -102,28 +101,6 @@ impl Application {
}
}

/// Hide the application this window belongs to. (cmd+H)
pub fn hide(&self) {
unsafe {
let () = msg_send![self.ns_app, hide: nil];
}
}

/// Hide all other applications. (cmd+opt+H)
pub fn hide_others(&self) {
unsafe {
let workspace = class!(NSWorkspace);
let shared: id = msg_send![workspace, sharedWorkspace];
let () = msg_send![shared, hideOtherApplications];
}
}

pub fn set_menu(&self, menu: Menu) {
unsafe {
NSApp().setMainMenu_(menu.menu);
}
}

pub fn clipboard(&self) -> Clipboard {
Clipboard
}
Expand All @@ -142,6 +119,28 @@ impl Application {
}
}

impl crate::platform::mac::MacApplicationExt for crate::Application {
fn hide(&self) {
unsafe {
let () = msg_send![self.backend_app.ns_app, hide: nil];
}
}

fn hide_others(&self) {
unsafe {
let workspace = class!(NSWorkspace);
let shared: id = msg_send![workspace, sharedWorkspace];
let () = msg_send![shared, hideOtherApplications];
}
}

fn set_menu(&self, menu: crate::Menu) {
unsafe {
NSApp().setMainMenu_(menu.0.menu);
}
}
}

struct DelegateState {
handler: Option<Box<dyn AppHandler>>,
}
Expand Down
2 changes: 1 addition & 1 deletion druid-shell/src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::hotkey::HotKey;
/// Currently, a menu and its items cannot be changed once created. If you need
/// to change anything about a menu (for instance, disabling or selecting items)
/// you need to create a new menu with the desired properties.
pub struct Menu(backend::Menu);
pub struct Menu(pub(crate) backend::Menu);

impl Menu {
/// Create a new empty window or application menu.
Expand Down
40 changes: 40 additions & 0 deletions druid-shell/src/platform/mac.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2021 The Druid Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! macOS specific extensions.

/// macOS specific extensions to [`Application`]
///
/// [`Application`]: crate::Application
pub trait MacApplicationExt {
/// Hide the application this window belongs to. (cmd+H)
fn hide(&self);

/// Hide all other applications. (cmd+opt+H)
fn hide_others(&self);

/// Sets the global application menu, on platforms where there is one.
///
/// On platforms with no global application menu, this has no effect.
fn set_menu(&self, menu: crate::Menu);
}

#[cfg(test)]
mod test {
use crate::Application;

use super::*;
use static_assertions as sa;
sa::assert_impl_all!(Application: MacApplicationExt);
}
3 changes: 3 additions & 0 deletions druid-shell/src/platform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@

#[cfg(any(doc, target_os = "linux"))]
pub mod linux;

#[cfg(any(doc, target_os = "macos"))]
pub mod mac;
12 changes: 10 additions & 2 deletions druid/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,18 @@ pub mod sys {
/// Quit the running application. This command is handled by the druid library.
pub const QUIT_APP: Selector = Selector::new("druid-builtin.quit-app");

/// Hide the application. (mac only?)
/// Hide the application. (mac only)
#[cfg_attr(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I spent a few minutes thinking about whether this is too fancy, but I think it's a good change. 👍

not(target_os = "macos"),
deprecated = "HIDE_APPLICATION is only supported on macOS"
)]
pub const HIDE_APPLICATION: Selector = Selector::new("druid-builtin.menu-hide-application");

/// Hide all other applications. (mac only?)
/// Hide all other applications. (mac only)
#[cfg_attr(
not(target_os = "macos"),
deprecated = "HIDE_OTHERS is only supported on macOS"
)]
pub const HIDE_OTHERS: Selector = Selector::new("druid-builtin.menu-hide-others");

/// The selector for a command to create a new window.
Expand Down
12 changes: 9 additions & 3 deletions druid/src/win_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,8 @@ impl<T: Data> Inner<T> {

#[cfg(target_os = "macos")]
{
use druid_shell::platform::mac::MacApplicationExt;

let windows = &mut self.windows;
let window = self.menu_window.and_then(|w| windows.get_mut(w));
if let Some(window) = window {
Expand Down Expand Up @@ -652,7 +654,9 @@ impl<T: Data> AppState<T> {
match cmd.target() {
// these are handled the same no matter where they come from
_ if cmd.is(sys_cmd::QUIT_APP) => self.quit(),
#[cfg(target_os = "macos")]
_ if cmd.is(sys_cmd::HIDE_APPLICATION) => self.hide_app(),
#[cfg(target_os = "macos")]
_ if cmd.is(sys_cmd::HIDE_OTHERS) => self.hide_others(),
_ if cmd.is(sys_cmd::NEW_WINDOW) => {
if let Err(e) = self.new_window(cmd) {
Expand Down Expand Up @@ -835,14 +839,16 @@ impl<T: Data> AppState<T> {
self.inner.borrow().app.quit()
}

#[cfg(target_os = "macos")]
fn hide_app(&self) {
#[cfg(target_os = "macos")]
use druid_shell::platform::mac::MacApplicationExt;
self.inner.borrow().app.hide()
}

#[cfg(target_os = "macos")]
fn hide_others(&mut self) {
#[cfg(target_os = "macos")]
self.inner.borrow().app.hide_others()
use druid_shell::platform::mac::MacApplicationExt;
self.inner.borrow().app.hide_others();
}

pub(crate) fn build_native_window(
Expand Down