2021-11-30 17:50:23 +01:00
|
|
|
use raw_window_handle::{AppKitHandle, RawWindowHandle};
|
2019-05-01 17:03:30 -06:00
|
|
|
use std::{
|
2019-06-21 11:33:15 -04:00
|
|
|
collections::VecDeque,
|
2022-01-10 21:39:17 +01:00
|
|
|
convert::TryInto,
|
2022-01-23 21:35:26 +01:00
|
|
|
f64, ops,
|
2019-06-21 11:33:15 -04:00
|
|
|
os::raw::c_void,
|
|
|
|
|
sync::{
|
|
|
|
|
atomic::{AtomicBool, Ordering},
|
2022-01-23 21:35:26 +01:00
|
|
|
Arc, Mutex, MutexGuard, Weak,
|
2019-06-21 11:33:15 -04:00
|
|
|
},
|
2018-06-14 19:42:18 -04:00
|
|
|
};
|
2017-02-03 23:05:57 +11:00
|
|
|
|
2019-06-18 02:27:00 +08:00
|
|
|
use crate::{
|
2020-01-04 01:32:34 -05:00
|
|
|
dpi::{
|
|
|
|
|
LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize, Position, Size, Size::Logical,
|
|
|
|
|
},
|
2019-05-29 21:29:54 -04:00
|
|
|
error::{ExternalError, NotSupportedError, OsError as RootOsError},
|
2019-06-21 11:33:15 -04:00
|
|
|
icon::Icon,
|
2019-07-29 21:16:14 +03:00
|
|
|
monitor::{MonitorHandle as RootMonitorHandle, VideoMode as RootVideoMode},
|
2021-04-30 11:31:28 +02:00
|
|
|
platform::macos::WindowExtMacOS,
|
2019-06-21 11:33:15 -04:00
|
|
|
platform_impl::platform::{
|
|
|
|
|
app_state::AppState,
|
|
|
|
|
ffi,
|
2019-07-29 21:16:14 +03:00
|
|
|
monitor::{self, MonitorHandle, VideoMode},
|
2019-06-21 11:33:15 -04:00
|
|
|
util::{self, IdRef},
|
2020-01-03 09:34:14 +09:00
|
|
|
view::CursorState,
|
2019-06-21 11:33:15 -04:00
|
|
|
view::{self, new_view},
|
|
|
|
|
window_delegate::new_delegate,
|
|
|
|
|
OsError,
|
2019-05-01 17:03:30 -06:00
|
|
|
},
|
2020-11-27 03:03:08 +01:00
|
|
|
window::{
|
|
|
|
|
CursorIcon, Fullscreen, UserAttentionType, WindowAttributes, WindowId as RootWindowId,
|
|
|
|
|
},
|
2019-07-29 21:16:14 +03:00
|
|
|
};
|
|
|
|
|
use cocoa::{
|
|
|
|
|
appkit::{
|
2021-04-30 11:31:28 +02:00
|
|
|
self, CGFloat, NSApp, NSApplication, NSApplicationPresentationOptions, NSColor,
|
|
|
|
|
NSRequestUserAttentionType, NSScreen, NSView, NSWindow, NSWindowButton, NSWindowStyleMask,
|
2019-07-29 21:16:14 +03:00
|
|
|
},
|
|
|
|
|
base::{id, nil},
|
2022-01-10 21:39:17 +01:00
|
|
|
foundation::{NSDictionary, NSPoint, NSRect, NSSize, NSUInteger},
|
2019-07-29 21:16:14 +03:00
|
|
|
};
|
|
|
|
|
use core_graphics::display::{CGDisplay, CGDisplayMode};
|
|
|
|
|
use objc::{
|
|
|
|
|
declare::ClassDecl,
|
2021-05-27 17:38:41 +02:00
|
|
|
rc::autoreleasepool,
|
2019-07-29 21:16:14 +03:00
|
|
|
runtime::{Class, Object, Sel, BOOL, NO, YES},
|
2018-06-14 19:42:18 -04:00
|
|
|
};
|
2022-06-08 11:50:26 -07:00
|
|
|
use once_cell::sync::Lazy;
|
2017-02-03 23:05:57 +11:00
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
2022-03-18 14:09:39 +01:00
|
|
|
pub struct WindowId(pub usize);
|
2017-02-03 23:05:57 +11:00
|
|
|
|
2022-03-18 14:09:39 +01:00
|
|
|
impl WindowId {
|
2021-08-30 19:40:02 +02:00
|
|
|
pub const unsafe fn dummy() -> Self {
|
2022-03-18 14:09:39 +01:00
|
|
|
Self(0)
|
2018-12-21 09:51:48 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-01 17:03:30 -06:00
|
|
|
// Convert the `cocoa::base::id` associated with a window to a usize to use as a unique identifier
|
|
|
|
|
// for the window.
|
2022-03-18 14:09:39 +01:00
|
|
|
pub fn get_window_id(window_cocoa_id: id) -> WindowId {
|
|
|
|
|
WindowId(window_cocoa_id as *const Object as _)
|
2018-04-18 02:07:54 +08:00
|
|
|
}
|
|
|
|
|
|
2020-08-14 02:10:34 +08:00
|
|
|
#[derive(Clone)]
|
2019-05-01 17:03:30 -06:00
|
|
|
pub struct PlatformSpecificWindowBuilderAttributes {
|
|
|
|
|
pub movable_by_window_background: bool,
|
|
|
|
|
pub titlebar_transparent: bool,
|
|
|
|
|
pub title_hidden: bool,
|
|
|
|
|
pub titlebar_hidden: bool,
|
|
|
|
|
pub titlebar_buttons_hidden: bool,
|
|
|
|
|
pub fullsize_content_view: bool,
|
2020-01-04 01:33:07 -05:00
|
|
|
pub resize_increments: Option<LogicalSize<f64>>,
|
2019-07-29 16:07:36 +08:00
|
|
|
pub disallow_hidpi: bool,
|
2020-08-14 02:10:34 +08:00
|
|
|
pub has_shadow: bool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for PlatformSpecificWindowBuilderAttributes {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
movable_by_window_background: false,
|
|
|
|
|
titlebar_transparent: false,
|
|
|
|
|
title_hidden: false,
|
|
|
|
|
titlebar_hidden: false,
|
|
|
|
|
titlebar_buttons_hidden: false,
|
|
|
|
|
fullsize_content_view: false,
|
|
|
|
|
resize_increments: None,
|
|
|
|
|
disallow_hidpi: false,
|
|
|
|
|
has_shadow: true,
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-01 17:03:30 -06:00
|
|
|
}
|
2018-04-18 02:07:54 +08:00
|
|
|
|
2019-07-29 16:07:36 +08:00
|
|
|
unsafe fn create_view(
|
|
|
|
|
ns_window: id,
|
|
|
|
|
pl_attribs: &PlatformSpecificWindowBuilderAttributes,
|
2020-01-03 09:34:14 +09:00
|
|
|
) -> Option<(IdRef, Weak<Mutex<CursorState>>)> {
|
|
|
|
|
let (ns_view, cursor_state) = new_view(ns_window);
|
2019-06-11 01:09:38 +02:00
|
|
|
ns_view.non_nil().map(|ns_view| {
|
2019-07-29 16:07:36 +08:00
|
|
|
if !pl_attribs.disallow_hidpi {
|
|
|
|
|
ns_view.setWantsBestResolutionOpenGLSurface_(YES);
|
|
|
|
|
}
|
2018-04-18 02:07:54 +08:00
|
|
|
|
2019-05-01 17:03:30 -06:00
|
|
|
// On Mojave, views automatically become layer-backed shortly after being added to
|
|
|
|
|
// a window. Changing the layer-backedness of a view breaks the association between
|
|
|
|
|
// the view and its associated OpenGL context. To work around this, on Mojave we
|
|
|
|
|
// explicitly make the view layer-backed up front so that AppKit doesn't do it
|
|
|
|
|
// itself and break the association with its context.
|
|
|
|
|
if f64::floor(appkit::NSAppKitVersionNumber) > appkit::NSAppKitVersionNumber10_12 {
|
2019-06-11 01:09:38 +02:00
|
|
|
ns_view.setWantsLayer(YES);
|
2018-04-18 02:07:54 +08:00
|
|
|
}
|
2017-02-03 23:05:57 +11:00
|
|
|
|
2020-01-03 09:34:14 +09:00
|
|
|
(ns_view, cursor_state)
|
2019-05-01 17:03:30 -06:00
|
|
|
})
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
|
2019-05-01 17:03:30 -06:00
|
|
|
fn create_window(
|
|
|
|
|
attrs: &WindowAttributes,
|
|
|
|
|
pl_attrs: &PlatformSpecificWindowBuilderAttributes,
|
|
|
|
|
) -> Option<IdRef> {
|
2021-05-27 17:38:41 +02:00
|
|
|
autoreleasepool(|| unsafe {
|
2019-05-01 17:03:30 -06:00
|
|
|
let screen = match attrs.fullscreen {
|
2020-09-22 04:54:47 +03:00
|
|
|
Some(Fullscreen::Borderless(Some(RootMonitorHandle { inner: ref monitor })))
|
2019-07-29 21:16:14 +03:00
|
|
|
| Some(Fullscreen::Exclusive(RootVideoMode {
|
|
|
|
|
video_mode: VideoMode { ref monitor, .. },
|
|
|
|
|
})) => {
|
|
|
|
|
let monitor_screen = monitor.ns_screen();
|
2022-01-16 11:14:59 +11:00
|
|
|
Some(monitor_screen.unwrap_or_else(|| appkit::NSScreen::mainScreen(nil)))
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2020-09-22 04:54:47 +03:00
|
|
|
Some(Fullscreen::Borderless(None)) => Some(appkit::NSScreen::mainScreen(nil)),
|
2019-07-29 21:16:14 +03:00
|
|
|
None => None,
|
2019-05-01 17:03:30 -06:00
|
|
|
};
|
|
|
|
|
let frame = match screen {
|
2020-01-04 01:32:34 -05:00
|
|
|
Some(screen) => NSScreen::frame(screen),
|
2019-05-01 17:03:30 -06:00
|
|
|
None => {
|
2020-01-04 01:32:34 -05:00
|
|
|
let screen = NSScreen::mainScreen(nil);
|
2020-01-03 14:52:27 -05:00
|
|
|
let scale_factor = NSScreen::backingScaleFactor(screen) as f64;
|
2020-01-04 01:32:34 -05:00
|
|
|
let (width, height) = match attrs.inner_size {
|
|
|
|
|
Some(size) => {
|
2020-01-03 14:52:27 -05:00
|
|
|
let logical = size.to_logical(scale_factor);
|
2020-01-04 01:32:34 -05:00
|
|
|
(logical.width, logical.height)
|
|
|
|
|
}
|
|
|
|
|
None => (800.0, 600.0),
|
|
|
|
|
};
|
2021-03-25 21:18:51 +03:00
|
|
|
let (left, bottom) = match attrs.position {
|
|
|
|
|
Some(position) => {
|
|
|
|
|
let logical = util::window_position(position.to_logical(scale_factor));
|
|
|
|
|
// macOS wants the position of the bottom left corner,
|
|
|
|
|
// but caller is setting the position of top left corner
|
|
|
|
|
(logical.x, logical.y - height)
|
|
|
|
|
}
|
|
|
|
|
// This value is ignored by calling win.center() below
|
|
|
|
|
None => (0.0, 0.0),
|
|
|
|
|
};
|
|
|
|
|
NSRect::new(NSPoint::new(left, bottom), NSSize::new(width, height))
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2018-06-14 19:42:18 -04:00
|
|
|
};
|
2017-07-27 10:51:00 +08:00
|
|
|
|
2022-01-16 11:14:59 +11:00
|
|
|
let mut masks = if (!attrs.decorations && screen.is_none()) || pl_attrs.titlebar_hidden {
|
2019-05-01 17:03:30 -06:00
|
|
|
// Resizable UnownedWindow without a titlebar or borders
|
|
|
|
|
// if decorations is set to false, ignore pl_attrs
|
2022-01-16 11:14:59 +11:00
|
|
|
//
|
2019-05-01 17:03:30 -06:00
|
|
|
// if the titlebar is hidden, ignore other pl_attrs
|
2020-02-01 00:07:36 +09:00
|
|
|
NSWindowStyleMask::NSBorderlessWindowMask
|
|
|
|
|
| NSWindowStyleMask::NSResizableWindowMask
|
|
|
|
|
| NSWindowStyleMask::NSMiniaturizableWindowMask
|
2019-05-01 17:03:30 -06:00
|
|
|
} else {
|
|
|
|
|
// default case, resizable window with titlebar and titlebar buttons
|
2019-06-21 11:33:15 -04:00
|
|
|
NSWindowStyleMask::NSClosableWindowMask
|
|
|
|
|
| NSWindowStyleMask::NSMiniaturizableWindowMask
|
|
|
|
|
| NSWindowStyleMask::NSResizableWindowMask
|
|
|
|
|
| NSWindowStyleMask::NSTitledWindowMask
|
2019-05-01 17:03:30 -06:00
|
|
|
};
|
2017-07-27 00:59:42 +08:00
|
|
|
|
2019-05-01 17:03:30 -06:00
|
|
|
if !attrs.resizable {
|
|
|
|
|
masks &= !NSWindowStyleMask::NSResizableWindowMask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if pl_attrs.fullsize_content_view {
|
|
|
|
|
masks |= NSWindowStyleMask::NSFullSizeContentViewWindowMask;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-11 01:09:38 +02:00
|
|
|
let ns_window: id = msg_send![WINDOW_CLASS.0, alloc];
|
|
|
|
|
let ns_window = IdRef::new(ns_window.initWithContentRect_styleMask_backing_defer_(
|
2019-05-01 17:03:30 -06:00
|
|
|
frame,
|
|
|
|
|
masks,
|
|
|
|
|
appkit::NSBackingStoreBuffered,
|
|
|
|
|
NO,
|
|
|
|
|
));
|
2022-01-16 11:14:59 +11:00
|
|
|
|
|
|
|
|
ns_window.non_nil().map(|ns_window| {
|
2020-01-10 16:02:42 -08:00
|
|
|
let title = util::ns_string_id_ref(&attrs.title);
|
2019-06-11 01:09:38 +02:00
|
|
|
ns_window.setReleasedWhenClosed_(NO);
|
|
|
|
|
ns_window.setTitle_(*title);
|
|
|
|
|
ns_window.setAcceptsMouseMovedEvents_(YES);
|
2019-05-01 17:03:30 -06:00
|
|
|
|
|
|
|
|
if pl_attrs.titlebar_transparent {
|
2019-06-11 01:09:38 +02:00
|
|
|
ns_window.setTitlebarAppearsTransparent_(YES);
|
2019-05-01 17:03:30 -06:00
|
|
|
}
|
|
|
|
|
if pl_attrs.title_hidden {
|
2019-06-11 01:09:38 +02:00
|
|
|
ns_window.setTitleVisibility_(appkit::NSWindowTitleVisibility::NSWindowTitleHidden);
|
2019-05-01 17:03:30 -06:00
|
|
|
}
|
|
|
|
|
if pl_attrs.titlebar_buttons_hidden {
|
|
|
|
|
for titlebar_button in &[
|
|
|
|
|
NSWindowButton::NSWindowFullScreenButton,
|
|
|
|
|
NSWindowButton::NSWindowMiniaturizeButton,
|
|
|
|
|
NSWindowButton::NSWindowCloseButton,
|
|
|
|
|
NSWindowButton::NSWindowZoomButton,
|
|
|
|
|
] {
|
2019-06-11 01:09:38 +02:00
|
|
|
let button = ns_window.standardWindowButton_(*titlebar_button);
|
2019-06-21 11:33:15 -04:00
|
|
|
let _: () = msg_send![button, setHidden: YES];
|
2017-07-27 00:59:42 +08:00
|
|
|
}
|
2017-07-27 10:56:34 +08:00
|
|
|
}
|
2019-05-01 17:03:30 -06:00
|
|
|
if pl_attrs.movable_by_window_background {
|
2019-06-11 01:09:38 +02:00
|
|
|
ns_window.setMovableByWindowBackground_(YES);
|
2018-04-18 02:07:54 +08:00
|
|
|
}
|
|
|
|
|
|
2019-05-01 17:03:30 -06:00
|
|
|
if attrs.always_on_top {
|
2019-06-21 11:33:15 -04:00
|
|
|
let _: () = msg_send![
|
|
|
|
|
*ns_window,
|
|
|
|
|
setLevel: ffi::NSWindowLevel::NSFloatingWindowLevel
|
|
|
|
|
];
|
2018-04-18 02:07:54 +08:00
|
|
|
}
|
|
|
|
|
|
2019-05-01 17:03:30 -06:00
|
|
|
if let Some(increments) = pl_attrs.resize_increments {
|
|
|
|
|
let (x, y) = (increments.width, increments.height);
|
|
|
|
|
if x >= 1.0 && y >= 1.0 {
|
|
|
|
|
let size = NSSize::new(x as CGFloat, y as CGFloat);
|
2019-06-11 01:09:38 +02:00
|
|
|
ns_window.setResizeIncrements_(size);
|
2018-04-18 02:07:54 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-14 02:10:34 +08:00
|
|
|
if !pl_attrs.has_shadow {
|
|
|
|
|
ns_window.setHasShadow_(NO);
|
|
|
|
|
}
|
2021-03-25 21:18:51 +03:00
|
|
|
if attrs.position.is_none() {
|
|
|
|
|
ns_window.center();
|
|
|
|
|
}
|
2019-06-11 01:09:38 +02:00
|
|
|
ns_window
|
2022-01-16 11:14:59 +11:00
|
|
|
})
|
2021-05-27 17:38:41 +02:00
|
|
|
})
|
2019-05-01 17:03:30 -06:00
|
|
|
}
|
2017-02-03 23:05:57 +11:00
|
|
|
|
2019-05-01 17:03:30 -06:00
|
|
|
struct WindowClass(*const Class);
|
|
|
|
|
unsafe impl Send for WindowClass {}
|
|
|
|
|
unsafe impl Sync for WindowClass {}
|
|
|
|
|
|
2022-06-08 11:50:26 -07:00
|
|
|
static WINDOW_CLASS: Lazy<WindowClass> = Lazy::new(|| unsafe {
|
|
|
|
|
let window_superclass = class!(NSWindow);
|
|
|
|
|
let mut decl = ClassDecl::new("WinitWindow", window_superclass).unwrap();
|
2022-01-23 21:35:26 +01:00
|
|
|
|
2022-06-08 11:50:26 -07:00
|
|
|
pub extern "C" fn can_become_main_window(_: &Object, _: Sel) -> BOOL {
|
|
|
|
|
trace_scope!("canBecomeMainWindow");
|
|
|
|
|
YES
|
|
|
|
|
}
|
2022-01-23 21:35:26 +01:00
|
|
|
|
2022-06-08 11:50:26 -07:00
|
|
|
pub extern "C" fn can_become_key_window(_: &Object, _: Sel) -> BOOL {
|
|
|
|
|
trace_scope!("canBecomeKeyWindow");
|
|
|
|
|
YES
|
|
|
|
|
}
|
2022-01-23 21:35:26 +01:00
|
|
|
|
2022-06-08 11:50:26 -07:00
|
|
|
decl.add_method(
|
|
|
|
|
sel!(canBecomeMainWindow),
|
|
|
|
|
can_become_main_window as extern "C" fn(&Object, Sel) -> BOOL,
|
|
|
|
|
);
|
|
|
|
|
decl.add_method(
|
|
|
|
|
sel!(canBecomeKeyWindow),
|
|
|
|
|
can_become_key_window as extern "C" fn(&Object, Sel) -> BOOL,
|
|
|
|
|
);
|
|
|
|
|
WindowClass(decl.register())
|
|
|
|
|
});
|
2018-04-28 18:10:06 +02:00
|
|
|
|
2019-05-01 17:03:30 -06:00
|
|
|
#[derive(Default)]
|
|
|
|
|
pub struct SharedState {
|
|
|
|
|
pub resizable: bool,
|
2019-07-29 21:16:14 +03:00
|
|
|
pub fullscreen: Option<Fullscreen>,
|
2019-12-25 03:56:56 +09:00
|
|
|
// This is true between windowWillEnterFullScreen and windowDidEnterFullScreen
|
|
|
|
|
// or windowWillExitFullScreen and windowDidExitFullScreen.
|
|
|
|
|
// We must not toggle fullscreen when this is true.
|
|
|
|
|
pub in_fullscreen_transition: bool,
|
|
|
|
|
// If it is attempted to toggle fullscreen when in_fullscreen_transition is true,
|
|
|
|
|
// Set target_fullscreen and do after fullscreen transition is end.
|
|
|
|
|
pub target_fullscreen: Option<Option<Fullscreen>>,
|
2019-05-01 17:03:30 -06:00
|
|
|
pub maximized: bool,
|
2019-06-18 09:34:27 +03:00
|
|
|
pub standard_frame: Option<NSRect>,
|
2019-05-01 17:03:30 -06:00
|
|
|
is_simple_fullscreen: bool,
|
|
|
|
|
pub saved_style: Option<NSWindowStyleMask>,
|
2019-07-29 21:16:14 +03:00
|
|
|
/// Presentation options saved before entering `set_simple_fullscreen`, and
|
Show the menu bar in borderless fullscreen on macOS (#2053)
* In MacOS, only disable menu bar in exclusive fullscreen
* Save and restore fullscreen options in set_fullscreen
* Don't always cache presentation options when entering exclusive fullscreen
This commit caches presentation options when entering exclusive fullscreen
only if we're coming from borderless fullscreen.
Then, when transitioning from exclusive -> borderless, if no cached presentation
options are present, then the default borderless options are applied.
This fixes the menu bar being unavailable when taking the following path:
[not fullscreen] -> [exclusive fullscreen] -> [borderless fullscreen].
Without this commit, the presentation options from [not fullscreen] were being
cached and then applied to [borderless fullscreen].
* Restore the window level when switching to exclusive fullscreen
The hack of using `CGShieldingWindowLevel() + 1` in borderless fullscreen needs
to be undone when switching from [borderless] -> [exclusive] fullscreen,
otherwise there are menu bar glitches when following a path through
[borderless] -> [exclusive] -> [borderless] modes.
Now, this might appear to conflict with the 'always on top' feature which uses
the 'floating window' level, but this feature appears to be broken anyway when
entering and exiting fullscreen with an always-on-top window. So, rather than
introducing logic to attempt to restore to the 'floating' level here, I think
it's better to do the simple thing for now and then introduce logic for
always-on-top windows when fixing the overall fullscreen behaviour.
* Update the changelog
Co-authored-by: Ehden Sinai <ehdens@gmail.com>
Co-authored-by: Francesca Lovebloom <francesca@brainiumstudios.com>
2021-11-19 22:05:36 +01:00
|
|
|
/// restored upon exiting it. Also used when transitioning from Borderless to
|
|
|
|
|
/// Exclusive fullscreen in `set_fullscreen` because we need to disable the menu
|
|
|
|
|
/// bar in exclusive fullscreen but want to restore the original options when
|
|
|
|
|
/// transitioning back to borderless fullscreen.
|
2019-05-01 17:03:30 -06:00
|
|
|
save_presentation_opts: Option<NSApplicationPresentationOptions>,
|
2019-07-29 21:16:14 +03:00
|
|
|
pub saved_desktop_display_mode: Option<(CGDisplay, CGDisplayMode)>,
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
|
2019-06-18 09:34:27 +03:00
|
|
|
impl SharedState {
|
|
|
|
|
pub fn saved_standard_frame(&self) -> NSRect {
|
|
|
|
|
self.standard_frame
|
|
|
|
|
.unwrap_or_else(|| NSRect::new(NSPoint::new(50.0, 50.0), NSSize::new(800.0, 600.0)))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-01 17:03:30 -06:00
|
|
|
impl From<WindowAttributes> for SharedState {
|
|
|
|
|
fn from(attribs: WindowAttributes) -> Self {
|
|
|
|
|
SharedState {
|
|
|
|
|
resizable: attribs.resizable,
|
|
|
|
|
// This fullscreen field tracks the current state of the window
|
|
|
|
|
// (as seen by `WindowDelegate`), and since the window hasn't
|
|
|
|
|
// actually been fullscreened yet, we can't set it yet. This is
|
|
|
|
|
// necessary for state transitions to work right, since otherwise
|
|
|
|
|
// the initial value and the first `set_fullscreen` call would be
|
|
|
|
|
// identical, resulting in a no-op.
|
|
|
|
|
fullscreen: None,
|
|
|
|
|
maximized: attribs.maximized,
|
2019-06-21 11:33:15 -04:00
|
|
|
..Default::default()
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-23 21:35:26 +01:00
|
|
|
pub(crate) struct SharedStateMutexGuard<'a> {
|
|
|
|
|
guard: MutexGuard<'a, SharedState>,
|
|
|
|
|
called_from_fn: &'static str,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> SharedStateMutexGuard<'a> {
|
|
|
|
|
#[inline]
|
|
|
|
|
pub(crate) fn new(guard: MutexGuard<'a, SharedState>, called_from_fn: &'static str) -> Self {
|
|
|
|
|
trace!("Locked shared state in `{}`", called_from_fn);
|
|
|
|
|
Self {
|
|
|
|
|
guard,
|
|
|
|
|
called_from_fn,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ops::Deref for SharedStateMutexGuard<'_> {
|
|
|
|
|
type Target = SharedState;
|
|
|
|
|
#[inline]
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
|
self.guard.deref()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ops::DerefMut for SharedStateMutexGuard<'_> {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
|
self.guard.deref_mut()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Drop for SharedStateMutexGuard<'_> {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
trace!("Unlocked shared state in `{}`", self.called_from_fn);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-01 17:03:30 -06:00
|
|
|
pub struct UnownedWindow {
|
2019-06-11 01:09:38 +02:00
|
|
|
pub ns_window: IdRef, // never changes
|
2019-06-21 11:33:15 -04:00
|
|
|
pub ns_view: IdRef, // never changes
|
2022-01-23 21:35:26 +01:00
|
|
|
shared_state: Arc<Mutex<SharedState>>,
|
2019-05-01 17:03:30 -06:00
|
|
|
decorations: AtomicBool,
|
2020-01-03 09:34:14 +09:00
|
|
|
cursor_state: Weak<Mutex<CursorState>>,
|
2020-01-04 01:33:07 -05:00
|
|
|
pub inner_rect: Option<PhysicalSize<u32>>,
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
|
2019-05-01 17:03:30 -06:00
|
|
|
unsafe impl Send for UnownedWindow {}
|
|
|
|
|
unsafe impl Sync for UnownedWindow {}
|
2018-12-19 15:07:33 +11:00
|
|
|
|
2019-05-01 17:03:30 -06:00
|
|
|
impl UnownedWindow {
|
2018-04-18 02:07:54 +08:00
|
|
|
pub fn new(
|
2018-06-07 13:29:23 -04:00
|
|
|
mut win_attribs: WindowAttributes,
|
2018-05-07 17:36:21 -04:00
|
|
|
pl_attribs: PlatformSpecificWindowBuilderAttributes,
|
2019-05-29 21:29:54 -04:00
|
|
|
) -> Result<(Arc<Self>, IdRef), RootOsError> {
|
2017-02-05 19:53:13 +11:00
|
|
|
unsafe {
|
2022-01-10 21:39:17 +01:00
|
|
|
let is_main_thread: BOOL = msg_send!(class!(NSThread), isMainThread);
|
|
|
|
|
if is_main_thread == NO {
|
2017-02-05 19:53:13 +11:00
|
|
|
panic!("Windows can only be created on the main thread on macOS");
|
|
|
|
|
}
|
2017-02-05 19:47:39 +11:00
|
|
|
}
|
2021-04-07 22:24:49 +02:00
|
|
|
trace!("Creating new window");
|
2018-06-07 13:29:23 -04:00
|
|
|
|
2021-05-27 17:38:41 +02:00
|
|
|
let ns_window = create_window(&win_attribs, &pl_attribs)
|
|
|
|
|
.ok_or_else(|| os_error!(OsError::CreationError("Couldn't create `NSWindow`")))?;
|
2017-02-03 23:05:57 +11:00
|
|
|
|
2021-05-27 17:38:41 +02:00
|
|
|
let (ns_view, cursor_state) = unsafe { create_view(*ns_window, &pl_attribs) }
|
|
|
|
|
.ok_or_else(|| os_error!(OsError::CreationError("Couldn't create `NSView`")))?;
|
2017-02-03 23:05:57 +11:00
|
|
|
|
2021-04-29 12:52:41 +02:00
|
|
|
// Configure the new view as the "key view" for the window
|
|
|
|
|
unsafe {
|
|
|
|
|
ns_window.setContentView_(*ns_view);
|
|
|
|
|
ns_window.setInitialFirstResponder_(*ns_view);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-13 20:41:41 +01:00
|
|
|
let scale_factor = unsafe { NSWindow::backingScaleFactor(*ns_window) as f64 };
|
2020-01-04 01:32:34 -05:00
|
|
|
|
2017-02-03 23:05:57 +11:00
|
|
|
unsafe {
|
|
|
|
|
if win_attribs.transparent {
|
2019-06-11 01:09:38 +02:00
|
|
|
ns_window.setOpaque_(NO);
|
|
|
|
|
ns_window.setBackgroundColor_(NSColor::clearColor(nil));
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
|
2022-01-16 11:14:59 +11:00
|
|
|
if let Some(dim) = win_attribs.min_inner_size {
|
2020-02-13 20:41:41 +01:00
|
|
|
let logical_dim = dim.to_logical(scale_factor);
|
2022-01-16 11:14:59 +11:00
|
|
|
set_min_inner_size(*ns_window, logical_dim);
|
|
|
|
|
}
|
|
|
|
|
if let Some(dim) = win_attribs.max_inner_size {
|
2020-02-13 20:41:41 +01:00
|
|
|
let logical_dim = dim.to_logical(scale_factor);
|
2022-01-16 11:14:59 +11:00
|
|
|
set_max_inner_size(*ns_window, logical_dim);
|
|
|
|
|
}
|
2017-07-27 00:59:42 +08:00
|
|
|
|
|
|
|
|
use cocoa::foundation::NSArray;
|
|
|
|
|
// register for drag and drop operations.
|
2022-06-10 13:43:33 +03:00
|
|
|
let _: () = msg_send![
|
2019-06-21 11:33:15 -04:00
|
|
|
*ns_window,
|
|
|
|
|
registerForDraggedTypes:
|
|
|
|
|
NSArray::arrayWithObject(nil, appkit::NSFilenamesPboardType)
|
|
|
|
|
];
|
2019-05-01 17:03:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Since `win_attribs` is put into a mutex below, we'll just copy these
|
|
|
|
|
// attributes now instead of bothering to lock it later.
|
|
|
|
|
// Also, `SharedState` doesn't carry `fullscreen` over; it's set
|
|
|
|
|
// indirectly by us calling `set_fullscreen` below, causing handlers in
|
|
|
|
|
// `WindowDelegate` to update the state.
|
|
|
|
|
let fullscreen = win_attribs.fullscreen.take();
|
|
|
|
|
let maximized = win_attribs.maximized;
|
|
|
|
|
let visible = win_attribs.visible;
|
|
|
|
|
let decorations = win_attribs.decorations;
|
2020-01-04 01:32:34 -05:00
|
|
|
let inner_rect = win_attribs
|
|
|
|
|
.inner_size
|
2020-02-13 20:41:41 +01:00
|
|
|
.map(|size| size.to_physical(scale_factor));
|
2019-05-01 17:03:30 -06:00
|
|
|
|
|
|
|
|
let window = Arc::new(UnownedWindow {
|
2019-06-11 01:09:38 +02:00
|
|
|
ns_view,
|
|
|
|
|
ns_window,
|
2019-05-01 17:03:30 -06:00
|
|
|
shared_state: Arc::new(Mutex::new(win_attribs.into())),
|
|
|
|
|
decorations: AtomicBool::new(decorations),
|
2020-01-03 09:34:14 +09:00
|
|
|
cursor_state,
|
2020-01-04 01:32:34 -05:00
|
|
|
inner_rect,
|
2019-05-01 17:03:30 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let delegate = new_delegate(&window, fullscreen.is_some());
|
2017-02-03 23:05:57 +11:00
|
|
|
|
2018-04-18 02:07:54 +08:00
|
|
|
// Set fullscreen mode after we setup everything
|
2019-07-29 21:16:14 +03:00
|
|
|
window.set_fullscreen(fullscreen);
|
2018-04-18 02:07:54 +08:00
|
|
|
|
2019-05-01 17:03:30 -06:00
|
|
|
// Setting the window as key has to happen *after* we set the fullscreen
|
|
|
|
|
// state, since otherwise we'll briefly see the window at normal size
|
|
|
|
|
// before it transitions.
|
2021-04-29 19:49:17 +02:00
|
|
|
if visible {
|
|
|
|
|
// Tightly linked with `app_state::window_activation_hack`
|
|
|
|
|
unsafe { window.ns_window.makeKeyAndOrderFront_(nil) };
|
2018-04-18 02:07:54 +08:00
|
|
|
}
|
|
|
|
|
|
2019-05-01 17:03:30 -06:00
|
|
|
if maximized {
|
|
|
|
|
window.set_maximized(maximized);
|
2018-04-18 02:07:54 +08:00
|
|
|
}
|
2022-05-07 05:29:25 +03:00
|
|
|
trace!("Done unowned window::new");
|
2019-05-01 17:03:30 -06:00
|
|
|
Ok((window, delegate))
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
|
2022-01-23 21:35:26 +01:00
|
|
|
#[track_caller]
|
|
|
|
|
pub(crate) fn lock_shared_state(
|
|
|
|
|
&self,
|
|
|
|
|
called_from_fn: &'static str,
|
|
|
|
|
) -> SharedStateMutexGuard<'_> {
|
|
|
|
|
SharedStateMutexGuard::new(self.shared_state.lock().unwrap(), called_from_fn)
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-01 17:03:30 -06:00
|
|
|
fn set_style_mask_async(&self, mask: NSWindowStyleMask) {
|
2019-06-21 11:33:15 -04:00
|
|
|
unsafe { util::set_style_mask_async(*self.ns_window, *self.ns_view, mask) };
|
2018-04-28 18:10:06 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-01 17:03:30 -06:00
|
|
|
fn set_style_mask_sync(&self, mask: NSWindowStyleMask) {
|
2019-06-21 11:33:15 -04:00
|
|
|
unsafe { util::set_style_mask_sync(*self.ns_window, *self.ns_view, mask) };
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
|
2022-03-18 14:09:39 +01:00
|
|
|
pub fn id(&self) -> WindowId {
|
2019-06-11 01:09:38 +02:00
|
|
|
get_window_id(*self.ns_window)
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn set_title(&self, title: &str) {
|
|
|
|
|
unsafe {
|
2019-06-18 09:34:27 +03:00
|
|
|
util::set_title_async(*self.ns_window, title.to_string());
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-29 21:29:54 -04:00
|
|
|
pub fn set_visible(&self, visible: bool) {
|
|
|
|
|
match visible {
|
2019-06-11 01:09:38 +02:00
|
|
|
true => unsafe { util::make_key_and_order_front_async(*self.ns_window) },
|
|
|
|
|
false => unsafe { util::order_out_async(*self.ns_window) },
|
2019-05-29 21:29:54 -04:00
|
|
|
}
|
2019-05-01 17:03:30 -06:00
|
|
|
}
|
|
|
|
|
|
2022-02-17 20:44:14 +02:00
|
|
|
#[inline]
|
|
|
|
|
pub fn is_visible(&self) -> Option<bool> {
|
|
|
|
|
let is_visible: BOOL = unsafe { msg_send![*self.ns_window, isVisible] };
|
|
|
|
|
Some(is_visible == YES)
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-01 17:03:30 -06:00
|
|
|
pub fn request_redraw(&self) {
|
|
|
|
|
AppState::queue_redraw(RootWindowId(self.id()));
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
|
2020-01-04 01:33:07 -05:00
|
|
|
pub fn outer_position(&self) -> Result<PhysicalPosition<i32>, NotSupportedError> {
|
2019-06-11 01:09:38 +02:00
|
|
|
let frame_rect = unsafe { NSWindow::frame(*self.ns_window) };
|
2020-01-04 01:32:34 -05:00
|
|
|
let position = LogicalPosition::new(
|
2018-06-14 19:42:18 -04:00
|
|
|
frame_rect.origin.x as f64,
|
2018-05-17 21:28:30 -04:00
|
|
|
util::bottom_left_to_top_left(frame_rect),
|
2020-01-04 01:32:34 -05:00
|
|
|
);
|
2020-02-13 20:41:41 +01:00
|
|
|
let scale_factor = self.scale_factor();
|
|
|
|
|
Ok(position.to_physical(scale_factor))
|
2018-04-16 21:40:30 -04:00
|
|
|
}
|
2017-02-03 23:05:57 +11:00
|
|
|
|
2020-01-04 01:33:07 -05:00
|
|
|
pub fn inner_position(&self) -> Result<PhysicalPosition<i32>, NotSupportedError> {
|
2018-04-16 21:40:30 -04:00
|
|
|
let content_rect = unsafe {
|
2019-06-21 11:33:15 -04:00
|
|
|
NSWindow::contentRectForFrameRect_(*self.ns_window, NSWindow::frame(*self.ns_window))
|
2018-04-16 21:40:30 -04:00
|
|
|
};
|
2020-01-04 01:32:34 -05:00
|
|
|
let position = LogicalPosition::new(
|
2018-06-14 19:42:18 -04:00
|
|
|
content_rect.origin.x as f64,
|
2018-05-17 21:28:30 -04:00
|
|
|
util::bottom_left_to_top_left(content_rect),
|
2020-01-04 01:32:34 -05:00
|
|
|
);
|
2020-02-13 20:41:41 +01:00
|
|
|
let scale_factor = self.scale_factor();
|
|
|
|
|
Ok(position.to_physical(scale_factor))
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
|
2020-01-04 01:32:34 -05:00
|
|
|
pub fn set_outer_position(&self, position: Position) {
|
2020-02-13 20:41:41 +01:00
|
|
|
let scale_factor = self.scale_factor();
|
|
|
|
|
let position = position.to_logical(scale_factor);
|
2017-02-03 23:05:57 +11:00
|
|
|
unsafe {
|
2021-03-25 21:18:51 +03:00
|
|
|
util::set_frame_top_left_point_async(*self.ns_window, util::window_position(position));
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2020-01-04 01:33:07 -05:00
|
|
|
pub fn inner_size(&self) -> PhysicalSize<u32> {
|
2019-06-11 01:09:38 +02:00
|
|
|
let view_frame = unsafe { NSView::frame(*self.ns_view) };
|
2020-01-04 01:33:07 -05:00
|
|
|
let logical: LogicalSize<f64> =
|
2020-01-04 01:32:34 -05:00
|
|
|
(view_frame.size.width as f64, view_frame.size.height as f64).into();
|
2020-02-13 20:41:41 +01:00
|
|
|
let scale_factor = self.scale_factor();
|
|
|
|
|
logical.to_physical(scale_factor)
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2020-01-04 01:33:07 -05:00
|
|
|
pub fn outer_size(&self) -> PhysicalSize<u32> {
|
2019-06-11 01:09:38 +02:00
|
|
|
let view_frame = unsafe { NSWindow::frame(*self.ns_window) };
|
2020-01-04 01:33:07 -05:00
|
|
|
let logical: LogicalSize<f64> =
|
2020-01-04 01:32:34 -05:00
|
|
|
(view_frame.size.width as f64, view_frame.size.height as f64).into();
|
2020-02-13 20:41:41 +01:00
|
|
|
let scale_factor = self.scale_factor();
|
|
|
|
|
logical.to_physical(scale_factor)
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2020-01-04 01:32:34 -05:00
|
|
|
pub fn set_inner_size(&self, size: Size) {
|
2017-02-03 23:05:57 +11:00
|
|
|
unsafe {
|
2020-02-13 20:41:41 +01:00
|
|
|
let scale_factor = self.scale_factor();
|
|
|
|
|
util::set_content_size_async(*self.ns_window, size.to_logical(scale_factor));
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-04 01:32:34 -05:00
|
|
|
pub fn set_min_inner_size(&self, dimensions: Option<Size>) {
|
2018-03-23 05:35:35 -04:00
|
|
|
unsafe {
|
2020-01-04 01:32:34 -05:00
|
|
|
let dimensions = dimensions.unwrap_or(Logical(LogicalSize {
|
|
|
|
|
width: 0.0,
|
|
|
|
|
height: 0.0,
|
|
|
|
|
}));
|
2020-02-13 20:41:41 +01:00
|
|
|
let scale_factor = self.scale_factor();
|
|
|
|
|
set_min_inner_size(*self.ns_window, dimensions.to_logical(scale_factor));
|
2018-03-23 05:35:35 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-04 01:32:34 -05:00
|
|
|
pub fn set_max_inner_size(&self, dimensions: Option<Size>) {
|
2018-03-23 05:35:35 -04:00
|
|
|
unsafe {
|
2020-01-04 01:32:34 -05:00
|
|
|
let dimensions = dimensions.unwrap_or(Logical(LogicalSize {
|
|
|
|
|
width: std::f32::MAX as f64,
|
|
|
|
|
height: std::f32::MAX as f64,
|
|
|
|
|
}));
|
2020-02-13 20:41:41 +01:00
|
|
|
let scale_factor = self.scale_factor();
|
|
|
|
|
set_max_inner_size(*self.ns_window, dimensions.to_logical(scale_factor));
|
2018-03-23 05:35:35 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-07 13:29:23 -04:00
|
|
|
#[inline]
|
|
|
|
|
pub fn set_resizable(&self, resizable: bool) {
|
2019-05-01 17:03:30 -06:00
|
|
|
let fullscreen = {
|
2022-01-23 21:35:26 +01:00
|
|
|
let mut shared_state_lock = self.lock_shared_state("set_resizable");
|
2019-05-01 17:03:30 -06:00
|
|
|
shared_state_lock.resizable = resizable;
|
|
|
|
|
shared_state_lock.fullscreen.is_some()
|
|
|
|
|
};
|
|
|
|
|
if !fullscreen {
|
2019-06-11 01:09:38 +02:00
|
|
|
let mut mask = unsafe { self.ns_window.styleMask() };
|
2018-06-07 13:29:23 -04:00
|
|
|
if resizable {
|
|
|
|
|
mask |= NSWindowStyleMask::NSResizableWindowMask;
|
|
|
|
|
} else {
|
|
|
|
|
mask &= !NSWindowStyleMask::NSResizableWindowMask;
|
|
|
|
|
}
|
2019-05-01 17:03:30 -06:00
|
|
|
self.set_style_mask_async(mask);
|
2018-06-07 13:29:23 -04:00
|
|
|
} // Otherwise, we don't change the mask until we exit fullscreen.
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-17 17:03:17 +02:00
|
|
|
#[inline]
|
|
|
|
|
pub fn is_resizable(&self) -> bool {
|
|
|
|
|
let is_resizable: BOOL = unsafe { msg_send![*self.ns_window, isResizable] };
|
|
|
|
|
is_resizable == YES
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-29 21:29:54 -04:00
|
|
|
pub fn set_cursor_icon(&self, cursor: CursorIcon) {
|
2018-12-28 15:29:29 -05:00
|
|
|
let cursor = util::Cursor::from(cursor);
|
2020-01-03 09:34:14 +09:00
|
|
|
if let Some(cursor_access) = self.cursor_state.upgrade() {
|
|
|
|
|
cursor_access.lock().unwrap().cursor = cursor;
|
2018-12-28 15:29:29 -05:00
|
|
|
}
|
2017-02-03 23:05:57 +11:00
|
|
|
unsafe {
|
2019-06-11 01:09:38 +02:00
|
|
|
let _: () = msg_send![*self.ns_window,
|
|
|
|
|
invalidateCursorRectsForView:*self.ns_view
|
2018-12-28 15:29:29 -05:00
|
|
|
];
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-18 12:32:18 -04:00
|
|
|
#[inline]
|
2019-05-29 21:29:54 -04:00
|
|
|
pub fn set_cursor_grab(&self, grab: bool) -> Result<(), ExternalError> {
|
2018-06-18 12:32:18 -04:00
|
|
|
// TODO: Do this for real https://stackoverflow.com/a/40922095/5435443
|
|
|
|
|
CGDisplay::associate_mouse_and_mouse_cursor_position(!grab)
|
2019-05-29 21:29:54 -04:00
|
|
|
.map_err(|status| ExternalError::Os(os_error!(OsError::CGError(status))))
|
2018-06-18 12:32:18 -04:00
|
|
|
}
|
2017-02-03 23:05:57 +11:00
|
|
|
|
2018-06-18 12:32:18 -04:00
|
|
|
#[inline]
|
2019-05-29 21:29:54 -04:00
|
|
|
pub fn set_cursor_visible(&self, visible: bool) {
|
2020-01-03 09:34:14 +09:00
|
|
|
if let Some(cursor_access) = self.cursor_state.upgrade() {
|
|
|
|
|
let mut cursor_state = cursor_access.lock().unwrap();
|
|
|
|
|
if visible != cursor_state.visible {
|
|
|
|
|
cursor_state.visible = visible;
|
|
|
|
|
drop(cursor_state);
|
|
|
|
|
unsafe {
|
|
|
|
|
let _: () = msg_send![*self.ns_window,
|
|
|
|
|
invalidateCursorRectsForView:*self.ns_view
|
|
|
|
|
];
|
|
|
|
|
}
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2020-01-03 14:52:27 -05:00
|
|
|
pub fn scale_factor(&self) -> f64 {
|
2019-06-11 01:09:38 +02:00
|
|
|
unsafe { NSWindow::backingScaleFactor(*self.ns_window) as _ }
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2020-01-04 01:32:34 -05:00
|
|
|
pub fn set_cursor_position(&self, cursor_position: Position) -> Result<(), ExternalError> {
|
|
|
|
|
let physical_window_position = self.inner_position().unwrap();
|
2020-02-13 20:41:41 +01:00
|
|
|
let scale_factor = self.scale_factor();
|
|
|
|
|
let window_position = physical_window_position.to_logical::<CGFloat>(scale_factor);
|
|
|
|
|
let logical_cursor_position = cursor_position.to_logical::<CGFloat>(scale_factor);
|
2018-06-14 19:42:18 -04:00
|
|
|
let point = appkit::CGPoint {
|
2020-01-04 01:33:07 -05:00
|
|
|
x: logical_cursor_position.x + window_position.x,
|
|
|
|
|
y: logical_cursor_position.y + window_position.y,
|
2018-06-14 19:42:18 -04:00
|
|
|
};
|
|
|
|
|
CGDisplay::warp_mouse_cursor_position(point)
|
2019-05-29 21:29:54 -04:00
|
|
|
.map_err(|e| ExternalError::Os(os_error!(OsError::CGError(e))))?;
|
2018-06-14 19:42:18 -04:00
|
|
|
CGDisplay::associate_mouse_and_mouse_cursor_position(true)
|
2019-05-29 21:29:54 -04:00
|
|
|
.map_err(|e| ExternalError::Os(os_error!(OsError::CGError(e))))?;
|
2018-06-19 10:30:15 -04:00
|
|
|
|
2017-02-03 23:05:57 +11:00
|
|
|
Ok(())
|
|
|
|
|
}
|
2017-08-28 01:43:34 +01:00
|
|
|
|
2021-03-07 10:43:23 +01:00
|
|
|
#[inline]
|
|
|
|
|
pub fn drag_window(&self) -> Result<(), ExternalError> {
|
|
|
|
|
unsafe {
|
|
|
|
|
let event: id = msg_send![NSApp(), currentEvent];
|
|
|
|
|
let _: () = msg_send![*self.ns_window, performWindowDragWithEvent: event];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-12 19:10:46 +02:00
|
|
|
#[inline]
|
|
|
|
|
pub fn set_cursor_hittest(&self, hittest: bool) -> Result<(), ExternalError> {
|
|
|
|
|
unsafe {
|
|
|
|
|
util::set_ignore_mouse_events(*self.ns_window, !hittest);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-01 17:03:30 -06:00
|
|
|
pub(crate) fn is_zoomed(&self) -> bool {
|
|
|
|
|
// because `isZoomed` doesn't work if the window's borderless,
|
|
|
|
|
// we make it resizable temporalily.
|
2019-06-11 01:09:38 +02:00
|
|
|
let curr_mask = unsafe { self.ns_window.styleMask() };
|
2019-05-01 17:03:30 -06:00
|
|
|
|
2019-06-21 11:33:15 -04:00
|
|
|
let required =
|
|
|
|
|
NSWindowStyleMask::NSTitledWindowMask | NSWindowStyleMask::NSResizableWindowMask;
|
2019-05-01 17:03:30 -06:00
|
|
|
let needs_temp_mask = !curr_mask.contains(required);
|
|
|
|
|
if needs_temp_mask {
|
|
|
|
|
self.set_style_mask_sync(required);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-11 01:09:38 +02:00
|
|
|
let is_zoomed: BOOL = unsafe { msg_send![*self.ns_window, isZoomed] };
|
2019-05-01 17:03:30 -06:00
|
|
|
|
|
|
|
|
// Roll back temp styles
|
|
|
|
|
if needs_temp_mask {
|
|
|
|
|
self.set_style_mask_async(curr_mask);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-02 23:06:00 +02:00
|
|
|
is_zoomed != NO
|
2019-05-01 17:03:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn saved_style(&self, shared_state: &mut SharedState) -> NSWindowStyleMask {
|
2019-06-21 11:33:15 -04:00
|
|
|
let base_mask = shared_state
|
|
|
|
|
.saved_style
|
2019-05-01 17:03:30 -06:00
|
|
|
.take()
|
2019-06-11 01:09:38 +02:00
|
|
|
.unwrap_or_else(|| unsafe { self.ns_window.styleMask() });
|
2019-05-01 17:03:30 -06:00
|
|
|
if shared_state.resizable {
|
|
|
|
|
base_mask | NSWindowStyleMask::NSResizableWindowMask
|
|
|
|
|
} else {
|
|
|
|
|
base_mask & !NSWindowStyleMask::NSResizableWindowMask
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-29 21:16:14 +03:00
|
|
|
/// This is called when the window is exiting fullscreen, whether by the
|
|
|
|
|
/// user clicking on the green fullscreen button or programmatically by
|
|
|
|
|
/// `toggleFullScreen:`
|
2019-05-01 17:03:30 -06:00
|
|
|
pub(crate) fn restore_state_from_fullscreen(&self) {
|
2022-01-23 21:35:26 +01:00
|
|
|
let mut shared_state_lock = self.lock_shared_state("restore_state_from_fullscreen");
|
2019-05-01 17:03:30 -06:00
|
|
|
|
2019-07-29 21:16:14 +03:00
|
|
|
shared_state_lock.fullscreen = None;
|
2019-05-01 17:03:30 -06:00
|
|
|
|
2019-07-29 21:16:14 +03:00
|
|
|
let maximized = shared_state_lock.maximized;
|
|
|
|
|
let mask = self.saved_style(&mut *shared_state_lock);
|
2019-05-01 17:03:30 -06:00
|
|
|
|
2019-07-29 21:16:14 +03:00
|
|
|
drop(shared_state_lock);
|
|
|
|
|
|
|
|
|
|
self.set_style_mask_async(mask);
|
2019-05-01 17:03:30 -06:00
|
|
|
self.set_maximized(maximized);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-22 01:04:11 -05:00
|
|
|
#[inline]
|
|
|
|
|
pub fn set_minimized(&self, minimized: bool) {
|
|
|
|
|
let is_minimized: BOOL = unsafe { msg_send![*self.ns_window, isMiniaturized] };
|
|
|
|
|
let is_minimized: bool = is_minimized == YES;
|
|
|
|
|
if is_minimized == minimized {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if minimized {
|
|
|
|
|
unsafe {
|
|
|
|
|
NSWindow::miniaturize_(*self.ns_window, *self.ns_window);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
unsafe {
|
|
|
|
|
NSWindow::deminiaturize_(*self.ns_window, *self.ns_window);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-28 01:43:34 +01:00
|
|
|
#[inline]
|
2018-04-18 02:07:54 +08:00
|
|
|
pub fn set_maximized(&self, maximized: bool) {
|
2019-05-01 17:03:30 -06:00
|
|
|
let is_zoomed = self.is_zoomed();
|
2019-06-18 09:34:27 +03:00
|
|
|
if is_zoomed == maximized {
|
2019-05-01 17:03:30 -06:00
|
|
|
return;
|
2019-06-18 09:34:27 +03:00
|
|
|
};
|
|
|
|
|
unsafe {
|
|
|
|
|
util::set_maximized_async(
|
|
|
|
|
*self.ns_window,
|
|
|
|
|
is_zoomed,
|
|
|
|
|
maximized,
|
|
|
|
|
Arc::downgrade(&self.shared_state),
|
|
|
|
|
);
|
2019-05-01 17:03:30 -06:00
|
|
|
}
|
2017-09-07 09:33:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2019-07-29 21:16:14 +03:00
|
|
|
pub fn fullscreen(&self) -> Option<Fullscreen> {
|
2022-01-23 21:35:26 +01:00
|
|
|
let shared_state_lock = self.lock_shared_state("fullscreen");
|
2019-05-16 00:26:59 -04:00
|
|
|
shared_state_lock.fullscreen.clone()
|
2019-04-26 03:09:32 +10:00
|
|
|
}
|
|
|
|
|
|
2021-01-27 20:01:17 +02:00
|
|
|
#[inline]
|
|
|
|
|
pub fn is_maximized(&self) -> bool {
|
|
|
|
|
self.is_zoomed()
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-07 09:33:46 +01:00
|
|
|
#[inline]
|
2019-07-29 21:16:14 +03:00
|
|
|
pub fn set_fullscreen(&self, fullscreen: Option<Fullscreen>) {
|
2022-01-23 21:35:26 +01:00
|
|
|
let mut shared_state_lock = self.lock_shared_state("set_fullscreen");
|
2019-05-01 17:03:30 -06:00
|
|
|
if shared_state_lock.is_simple_fullscreen {
|
2019-07-29 21:16:14 +03:00
|
|
|
return;
|
|
|
|
|
}
|
2019-12-25 03:56:56 +09:00
|
|
|
if shared_state_lock.in_fullscreen_transition {
|
|
|
|
|
// We can't set fullscreen here.
|
|
|
|
|
// Set fullscreen after transition.
|
|
|
|
|
shared_state_lock.target_fullscreen = Some(fullscreen);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-07-29 21:16:14 +03:00
|
|
|
let old_fullscreen = shared_state_lock.fullscreen.clone();
|
|
|
|
|
if fullscreen == old_fullscreen {
|
2019-06-21 11:33:15 -04:00
|
|
|
return;
|
2018-12-19 15:07:33 +11:00
|
|
|
}
|
2019-07-29 21:16:14 +03:00
|
|
|
drop(shared_state_lock);
|
|
|
|
|
|
|
|
|
|
// If the fullscreen is on a different monitor, we must move the window
|
|
|
|
|
// to that monitor before we toggle fullscreen (as `toggleFullScreen`
|
|
|
|
|
// does not take a screen parameter, but uses the current screen)
|
|
|
|
|
if let Some(ref fullscreen) = fullscreen {
|
|
|
|
|
let new_screen = match fullscreen {
|
2020-09-22 04:54:47 +03:00
|
|
|
Fullscreen::Borderless(borderless) => {
|
|
|
|
|
let RootMonitorHandle { inner: monitor } = borderless
|
|
|
|
|
.clone()
|
|
|
|
|
.unwrap_or_else(|| self.current_monitor_inner());
|
|
|
|
|
monitor
|
|
|
|
|
}
|
2019-07-29 21:16:14 +03:00
|
|
|
Fullscreen::Exclusive(RootVideoMode {
|
|
|
|
|
video_mode: VideoMode { ref monitor, .. },
|
2020-09-22 04:54:47 +03:00
|
|
|
}) => monitor.clone(),
|
2019-07-29 21:16:14 +03:00
|
|
|
}
|
|
|
|
|
.ns_screen()
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
|
let old_screen = NSWindow::screen(*self.ns_window);
|
|
|
|
|
if old_screen != new_screen {
|
|
|
|
|
let mut screen_frame: NSRect = msg_send![new_screen, frame];
|
|
|
|
|
// The coordinate system here has its origin at bottom-left
|
|
|
|
|
// and Y goes up
|
|
|
|
|
screen_frame.origin.y += screen_frame.size.height;
|
|
|
|
|
util::set_frame_top_left_point_async(*self.ns_window, screen_frame.origin);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-19 15:07:33 +11:00
|
|
|
|
2019-07-29 21:16:14 +03:00
|
|
|
if let Some(Fullscreen::Exclusive(ref video_mode)) = fullscreen {
|
|
|
|
|
// Note: `enterFullScreenMode:withOptions:` seems to do the exact
|
|
|
|
|
// same thing as we're doing here (captures the display, sets the
|
|
|
|
|
// video mode, and hides the menu bar and dock), with the exception
|
|
|
|
|
// of that I couldn't figure out how to set the display mode with
|
|
|
|
|
// it. I think `enterFullScreenMode:withOptions:` is still using the
|
|
|
|
|
// older display mode API where display modes were of the type
|
|
|
|
|
// `CFDictionary`, but this has changed, so we can't obtain the
|
|
|
|
|
// correct parameter for this any longer. Apple's code samples for
|
|
|
|
|
// this function seem to just pass in "YES" for the display mode
|
|
|
|
|
// parameter, which is not consistent with the docs saying that it
|
|
|
|
|
// takes a `NSDictionary`..
|
|
|
|
|
|
|
|
|
|
let display_id = video_mode.monitor().inner.native_identifier();
|
|
|
|
|
|
|
|
|
|
let mut fade_token = ffi::kCGDisplayFadeReservationInvalidToken;
|
|
|
|
|
|
Show the menu bar in borderless fullscreen on macOS (#2053)
* In MacOS, only disable menu bar in exclusive fullscreen
* Save and restore fullscreen options in set_fullscreen
* Don't always cache presentation options when entering exclusive fullscreen
This commit caches presentation options when entering exclusive fullscreen
only if we're coming from borderless fullscreen.
Then, when transitioning from exclusive -> borderless, if no cached presentation
options are present, then the default borderless options are applied.
This fixes the menu bar being unavailable when taking the following path:
[not fullscreen] -> [exclusive fullscreen] -> [borderless fullscreen].
Without this commit, the presentation options from [not fullscreen] were being
cached and then applied to [borderless fullscreen].
* Restore the window level when switching to exclusive fullscreen
The hack of using `CGShieldingWindowLevel() + 1` in borderless fullscreen needs
to be undone when switching from [borderless] -> [exclusive] fullscreen,
otherwise there are menu bar glitches when following a path through
[borderless] -> [exclusive] -> [borderless] modes.
Now, this might appear to conflict with the 'always on top' feature which uses
the 'floating window' level, but this feature appears to be broken anyway when
entering and exiting fullscreen with an always-on-top window. So, rather than
introducing logic to attempt to restore to the 'floating' level here, I think
it's better to do the simple thing for now and then introduce logic for
always-on-top windows when fixing the overall fullscreen behaviour.
* Update the changelog
Co-authored-by: Ehden Sinai <ehdens@gmail.com>
Co-authored-by: Francesca Lovebloom <francesca@brainiumstudios.com>
2021-11-19 22:05:36 +01:00
|
|
|
if matches!(old_fullscreen, Some(Fullscreen::Borderless(_))) {
|
|
|
|
|
unsafe {
|
|
|
|
|
let app = NSApp();
|
2022-01-23 21:35:26 +01:00
|
|
|
let mut shared_state_lock = self.lock_shared_state("set_fullscreen");
|
Show the menu bar in borderless fullscreen on macOS (#2053)
* In MacOS, only disable menu bar in exclusive fullscreen
* Save and restore fullscreen options in set_fullscreen
* Don't always cache presentation options when entering exclusive fullscreen
This commit caches presentation options when entering exclusive fullscreen
only if we're coming from borderless fullscreen.
Then, when transitioning from exclusive -> borderless, if no cached presentation
options are present, then the default borderless options are applied.
This fixes the menu bar being unavailable when taking the following path:
[not fullscreen] -> [exclusive fullscreen] -> [borderless fullscreen].
Without this commit, the presentation options from [not fullscreen] were being
cached and then applied to [borderless fullscreen].
* Restore the window level when switching to exclusive fullscreen
The hack of using `CGShieldingWindowLevel() + 1` in borderless fullscreen needs
to be undone when switching from [borderless] -> [exclusive] fullscreen,
otherwise there are menu bar glitches when following a path through
[borderless] -> [exclusive] -> [borderless] modes.
Now, this might appear to conflict with the 'always on top' feature which uses
the 'floating window' level, but this feature appears to be broken anyway when
entering and exiting fullscreen with an always-on-top window. So, rather than
introducing logic to attempt to restore to the 'floating' level here, I think
it's better to do the simple thing for now and then introduce logic for
always-on-top windows when fixing the overall fullscreen behaviour.
* Update the changelog
Co-authored-by: Ehden Sinai <ehdens@gmail.com>
Co-authored-by: Francesca Lovebloom <francesca@brainiumstudios.com>
2021-11-19 22:05:36 +01:00
|
|
|
shared_state_lock.save_presentation_opts = Some(app.presentationOptions_());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-29 21:16:14 +03:00
|
|
|
unsafe {
|
|
|
|
|
// Fade to black (and wait for the fade to complete) to hide the
|
|
|
|
|
// flicker from capturing the display and switching display mode
|
|
|
|
|
if ffi::CGAcquireDisplayFadeReservation(5.0, &mut fade_token)
|
|
|
|
|
== ffi::kCGErrorSuccess
|
|
|
|
|
{
|
|
|
|
|
ffi::CGDisplayFade(
|
|
|
|
|
fade_token,
|
|
|
|
|
0.3,
|
|
|
|
|
ffi::kCGDisplayBlendNormal,
|
|
|
|
|
ffi::kCGDisplayBlendSolidColor,
|
|
|
|
|
0.0,
|
|
|
|
|
0.0,
|
|
|
|
|
0.0,
|
|
|
|
|
ffi::TRUE,
|
|
|
|
|
);
|
2019-06-24 12:14:55 -04:00
|
|
|
}
|
2019-07-29 21:16:14 +03:00
|
|
|
|
|
|
|
|
assert_eq!(ffi::CGDisplayCapture(display_id), ffi::kCGErrorSuccess);
|
2018-04-18 02:07:54 +08:00
|
|
|
}
|
|
|
|
|
|
2019-07-29 21:16:14 +03:00
|
|
|
unsafe {
|
|
|
|
|
let result = ffi::CGDisplaySetDisplayMode(
|
|
|
|
|
display_id,
|
|
|
|
|
video_mode.video_mode.native_mode.0,
|
|
|
|
|
std::ptr::null(),
|
|
|
|
|
);
|
|
|
|
|
assert!(result == ffi::kCGErrorSuccess, "failed to set video mode");
|
|
|
|
|
|
|
|
|
|
// After the display has been configured, fade back in
|
|
|
|
|
// asynchronously
|
|
|
|
|
if fade_token != ffi::kCGDisplayFadeReservationInvalidToken {
|
|
|
|
|
ffi::CGDisplayFade(
|
|
|
|
|
fade_token,
|
|
|
|
|
0.6,
|
|
|
|
|
ffi::kCGDisplayBlendSolidColor,
|
|
|
|
|
ffi::kCGDisplayBlendNormal,
|
|
|
|
|
0.0,
|
|
|
|
|
0.0,
|
|
|
|
|
0.0,
|
|
|
|
|
ffi::FALSE,
|
|
|
|
|
);
|
|
|
|
|
ffi::CGReleaseDisplayFadeReservation(fade_token);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-23 21:35:26 +01:00
|
|
|
let mut shared_state_lock = self.lock_shared_state("set_fullscreen");
|
2019-09-16 02:59:37 +03:00
|
|
|
shared_state_lock.fullscreen = fullscreen.clone();
|
|
|
|
|
|
2019-07-29 21:16:14 +03:00
|
|
|
match (&old_fullscreen, &fullscreen) {
|
2019-09-16 02:59:37 +03:00
|
|
|
(&None, &Some(_)) => unsafe {
|
|
|
|
|
util::toggle_full_screen_async(
|
|
|
|
|
*self.ns_window,
|
|
|
|
|
*self.ns_view,
|
|
|
|
|
old_fullscreen.is_none(),
|
|
|
|
|
Arc::downgrade(&self.shared_state),
|
|
|
|
|
);
|
2019-07-29 21:16:14 +03:00
|
|
|
},
|
2019-09-16 02:59:37 +03:00
|
|
|
(&Some(Fullscreen::Borderless(_)), &None) => unsafe {
|
|
|
|
|
// State is restored by `window_did_exit_fullscreen`
|
2019-07-29 21:16:14 +03:00
|
|
|
util::toggle_full_screen_async(
|
|
|
|
|
*self.ns_window,
|
|
|
|
|
*self.ns_view,
|
|
|
|
|
old_fullscreen.is_none(),
|
|
|
|
|
Arc::downgrade(&self.shared_state),
|
|
|
|
|
);
|
|
|
|
|
},
|
2019-09-16 02:59:37 +03:00
|
|
|
(&Some(Fullscreen::Exclusive(RootVideoMode { ref video_mode })), &None) => unsafe {
|
|
|
|
|
util::restore_display_mode_async(video_mode.monitor().inner.native_identifier());
|
|
|
|
|
// Rest of the state is restored by `window_did_exit_fullscreen`
|
2019-07-29 21:16:14 +03:00
|
|
|
util::toggle_full_screen_async(
|
|
|
|
|
*self.ns_window,
|
|
|
|
|
*self.ns_view,
|
|
|
|
|
old_fullscreen.is_none(),
|
|
|
|
|
Arc::downgrade(&self.shared_state),
|
|
|
|
|
);
|
|
|
|
|
},
|
2019-09-16 02:59:37 +03:00
|
|
|
(&Some(Fullscreen::Borderless(_)), &Some(Fullscreen::Exclusive(_))) => unsafe {
|
|
|
|
|
// If we're already in fullscreen mode, calling
|
|
|
|
|
// `CGDisplayCapture` will place the shielding window on top of
|
|
|
|
|
// our window, which results in a black display and is not what
|
|
|
|
|
// we want. So, we must place our window on top of the shielding
|
|
|
|
|
// window. Unfortunately, this also makes our window be on top
|
|
|
|
|
// of the menu bar, and this looks broken, so we must make sure
|
|
|
|
|
// that the menu bar is disabled. This is done in the window
|
|
|
|
|
// delegate in `window:willUseFullScreenPresentationOptions:`.
|
Show the menu bar in borderless fullscreen on macOS (#2053)
* In MacOS, only disable menu bar in exclusive fullscreen
* Save and restore fullscreen options in set_fullscreen
* Don't always cache presentation options when entering exclusive fullscreen
This commit caches presentation options when entering exclusive fullscreen
only if we're coming from borderless fullscreen.
Then, when transitioning from exclusive -> borderless, if no cached presentation
options are present, then the default borderless options are applied.
This fixes the menu bar being unavailable when taking the following path:
[not fullscreen] -> [exclusive fullscreen] -> [borderless fullscreen].
Without this commit, the presentation options from [not fullscreen] were being
cached and then applied to [borderless fullscreen].
* Restore the window level when switching to exclusive fullscreen
The hack of using `CGShieldingWindowLevel() + 1` in borderless fullscreen needs
to be undone when switching from [borderless] -> [exclusive] fullscreen,
otherwise there are menu bar glitches when following a path through
[borderless] -> [exclusive] -> [borderless] modes.
Now, this might appear to conflict with the 'always on top' feature which uses
the 'floating window' level, but this feature appears to be broken anyway when
entering and exiting fullscreen with an always-on-top window. So, rather than
introducing logic to attempt to restore to the 'floating' level here, I think
it's better to do the simple thing for now and then introduce logic for
always-on-top windows when fixing the overall fullscreen behaviour.
* Update the changelog
Co-authored-by: Ehden Sinai <ehdens@gmail.com>
Co-authored-by: Francesca Lovebloom <francesca@brainiumstudios.com>
2021-11-19 22:05:36 +01:00
|
|
|
let app = NSApp();
|
|
|
|
|
shared_state_lock.save_presentation_opts = Some(app.presentationOptions_());
|
|
|
|
|
|
|
|
|
|
let presentation_options =
|
|
|
|
|
NSApplicationPresentationOptions::NSApplicationPresentationFullScreen
|
|
|
|
|
| NSApplicationPresentationOptions::NSApplicationPresentationHideDock
|
|
|
|
|
| NSApplicationPresentationOptions::NSApplicationPresentationHideMenuBar;
|
|
|
|
|
app.setPresentationOptions_(presentation_options);
|
|
|
|
|
|
2022-06-10 13:43:33 +03:00
|
|
|
let _: () = msg_send![*self.ns_window, setLevel: ffi::CGShieldingWindowLevel() + 1];
|
2019-09-16 02:59:37 +03:00
|
|
|
},
|
|
|
|
|
(
|
|
|
|
|
&Some(Fullscreen::Exclusive(RootVideoMode { ref video_mode })),
|
|
|
|
|
&Some(Fullscreen::Borderless(_)),
|
|
|
|
|
) => unsafe {
|
Show the menu bar in borderless fullscreen on macOS (#2053)
* In MacOS, only disable menu bar in exclusive fullscreen
* Save and restore fullscreen options in set_fullscreen
* Don't always cache presentation options when entering exclusive fullscreen
This commit caches presentation options when entering exclusive fullscreen
only if we're coming from borderless fullscreen.
Then, when transitioning from exclusive -> borderless, if no cached presentation
options are present, then the default borderless options are applied.
This fixes the menu bar being unavailable when taking the following path:
[not fullscreen] -> [exclusive fullscreen] -> [borderless fullscreen].
Without this commit, the presentation options from [not fullscreen] were being
cached and then applied to [borderless fullscreen].
* Restore the window level when switching to exclusive fullscreen
The hack of using `CGShieldingWindowLevel() + 1` in borderless fullscreen needs
to be undone when switching from [borderless] -> [exclusive] fullscreen,
otherwise there are menu bar glitches when following a path through
[borderless] -> [exclusive] -> [borderless] modes.
Now, this might appear to conflict with the 'always on top' feature which uses
the 'floating window' level, but this feature appears to be broken anyway when
entering and exiting fullscreen with an always-on-top window. So, rather than
introducing logic to attempt to restore to the 'floating' level here, I think
it's better to do the simple thing for now and then introduce logic for
always-on-top windows when fixing the overall fullscreen behaviour.
* Update the changelog
Co-authored-by: Ehden Sinai <ehdens@gmail.com>
Co-authored-by: Francesca Lovebloom <francesca@brainiumstudios.com>
2021-11-19 22:05:36 +01:00
|
|
|
let presentation_options =
|
|
|
|
|
shared_state_lock.save_presentation_opts.unwrap_or_else(|| {
|
|
|
|
|
NSApplicationPresentationOptions::NSApplicationPresentationFullScreen
|
|
|
|
|
| NSApplicationPresentationOptions::NSApplicationPresentationAutoHideDock
|
|
|
|
|
| NSApplicationPresentationOptions::NSApplicationPresentationAutoHideMenuBar
|
|
|
|
|
});
|
|
|
|
|
NSApp().setPresentationOptions_(presentation_options);
|
|
|
|
|
|
2019-09-16 02:59:37 +03:00
|
|
|
util::restore_display_mode_async(video_mode.monitor().inner.native_identifier());
|
Show the menu bar in borderless fullscreen on macOS (#2053)
* In MacOS, only disable menu bar in exclusive fullscreen
* Save and restore fullscreen options in set_fullscreen
* Don't always cache presentation options when entering exclusive fullscreen
This commit caches presentation options when entering exclusive fullscreen
only if we're coming from borderless fullscreen.
Then, when transitioning from exclusive -> borderless, if no cached presentation
options are present, then the default borderless options are applied.
This fixes the menu bar being unavailable when taking the following path:
[not fullscreen] -> [exclusive fullscreen] -> [borderless fullscreen].
Without this commit, the presentation options from [not fullscreen] were being
cached and then applied to [borderless fullscreen].
* Restore the window level when switching to exclusive fullscreen
The hack of using `CGShieldingWindowLevel() + 1` in borderless fullscreen needs
to be undone when switching from [borderless] -> [exclusive] fullscreen,
otherwise there are menu bar glitches when following a path through
[borderless] -> [exclusive] -> [borderless] modes.
Now, this might appear to conflict with the 'always on top' feature which uses
the 'floating window' level, but this feature appears to be broken anyway when
entering and exiting fullscreen with an always-on-top window. So, rather than
introducing logic to attempt to restore to the 'floating' level here, I think
it's better to do the simple thing for now and then introduce logic for
always-on-top windows when fixing the overall fullscreen behaviour.
* Update the changelog
Co-authored-by: Ehden Sinai <ehdens@gmail.com>
Co-authored-by: Francesca Lovebloom <francesca@brainiumstudios.com>
2021-11-19 22:05:36 +01:00
|
|
|
|
|
|
|
|
// Restore the normal window level following the Borderless fullscreen
|
|
|
|
|
// `CGShieldingWindowLevel() + 1` hack.
|
2022-06-10 13:43:33 +03:00
|
|
|
let _: () = msg_send![
|
Show the menu bar in borderless fullscreen on macOS (#2053)
* In MacOS, only disable menu bar in exclusive fullscreen
* Save and restore fullscreen options in set_fullscreen
* Don't always cache presentation options when entering exclusive fullscreen
This commit caches presentation options when entering exclusive fullscreen
only if we're coming from borderless fullscreen.
Then, when transitioning from exclusive -> borderless, if no cached presentation
options are present, then the default borderless options are applied.
This fixes the menu bar being unavailable when taking the following path:
[not fullscreen] -> [exclusive fullscreen] -> [borderless fullscreen].
Without this commit, the presentation options from [not fullscreen] were being
cached and then applied to [borderless fullscreen].
* Restore the window level when switching to exclusive fullscreen
The hack of using `CGShieldingWindowLevel() + 1` in borderless fullscreen needs
to be undone when switching from [borderless] -> [exclusive] fullscreen,
otherwise there are menu bar glitches when following a path through
[borderless] -> [exclusive] -> [borderless] modes.
Now, this might appear to conflict with the 'always on top' feature which uses
the 'floating window' level, but this feature appears to be broken anyway when
entering and exiting fullscreen with an always-on-top window. So, rather than
introducing logic to attempt to restore to the 'floating' level here, I think
it's better to do the simple thing for now and then introduce logic for
always-on-top windows when fixing the overall fullscreen behaviour.
* Update the changelog
Co-authored-by: Ehden Sinai <ehdens@gmail.com>
Co-authored-by: Francesca Lovebloom <francesca@brainiumstudios.com>
2021-11-19 22:05:36 +01:00
|
|
|
*self.ns_window,
|
|
|
|
|
setLevel: ffi::NSWindowLevel::NSNormalWindowLevel
|
|
|
|
|
];
|
2019-09-16 02:59:37 +03:00
|
|
|
},
|
2022-06-08 08:22:54 -07:00
|
|
|
_ => {}
|
Show the menu bar in borderless fullscreen on macOS (#2053)
* In MacOS, only disable menu bar in exclusive fullscreen
* Save and restore fullscreen options in set_fullscreen
* Don't always cache presentation options when entering exclusive fullscreen
This commit caches presentation options when entering exclusive fullscreen
only if we're coming from borderless fullscreen.
Then, when transitioning from exclusive -> borderless, if no cached presentation
options are present, then the default borderless options are applied.
This fixes the menu bar being unavailable when taking the following path:
[not fullscreen] -> [exclusive fullscreen] -> [borderless fullscreen].
Without this commit, the presentation options from [not fullscreen] were being
cached and then applied to [borderless fullscreen].
* Restore the window level when switching to exclusive fullscreen
The hack of using `CGShieldingWindowLevel() + 1` in borderless fullscreen needs
to be undone when switching from [borderless] -> [exclusive] fullscreen,
otherwise there are menu bar glitches when following a path through
[borderless] -> [exclusive] -> [borderless] modes.
Now, this might appear to conflict with the 'always on top' feature which uses
the 'floating window' level, but this feature appears to be broken anyway when
entering and exiting fullscreen with an always-on-top window. So, rather than
introducing logic to attempt to restore to the 'floating' level here, I think
it's better to do the simple thing for now and then introduce logic for
always-on-top windows when fixing the overall fullscreen behaviour.
* Update the changelog
Co-authored-by: Ehden Sinai <ehdens@gmail.com>
Co-authored-by: Francesca Lovebloom <francesca@brainiumstudios.com>
2021-11-19 22:05:36 +01:00
|
|
|
};
|
2017-08-28 01:43:34 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-22 07:50:46 -05:00
|
|
|
#[inline]
|
2018-04-18 02:07:54 +08:00
|
|
|
pub fn set_decorations(&self, decorations: bool) {
|
2019-05-01 17:03:30 -06:00
|
|
|
if decorations != self.decorations.load(Ordering::Acquire) {
|
|
|
|
|
self.decorations.store(decorations, Ordering::Release);
|
|
|
|
|
|
|
|
|
|
let (fullscreen, resizable) = {
|
2022-01-23 21:35:26 +01:00
|
|
|
let shared_state_lock = self.lock_shared_state("set_decorations");
|
2019-05-01 17:03:30 -06:00
|
|
|
(
|
|
|
|
|
shared_state_lock.fullscreen.is_some(),
|
|
|
|
|
shared_state_lock.resizable,
|
|
|
|
|
)
|
|
|
|
|
};
|
2018-04-24 16:20:40 -04:00
|
|
|
|
2019-05-01 17:03:30 -06:00
|
|
|
// If we're in fullscreen mode, we wait to apply decoration changes
|
|
|
|
|
// until we're in `window_did_exit_fullscreen`.
|
2019-06-21 11:33:15 -04:00
|
|
|
if fullscreen {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-05-01 17:03:30 -06:00
|
|
|
|
|
|
|
|
let new_mask = {
|
|
|
|
|
let mut new_mask = if decorations {
|
|
|
|
|
NSWindowStyleMask::NSClosableWindowMask
|
|
|
|
|
| NSWindowStyleMask::NSMiniaturizableWindowMask
|
|
|
|
|
| NSWindowStyleMask::NSResizableWindowMask
|
|
|
|
|
| NSWindowStyleMask::NSTitledWindowMask
|
|
|
|
|
} else {
|
|
|
|
|
NSWindowStyleMask::NSBorderlessWindowMask
|
|
|
|
|
| NSWindowStyleMask::NSResizableWindowMask
|
|
|
|
|
};
|
|
|
|
|
if !resizable {
|
|
|
|
|
new_mask &= !NSWindowStyleMask::NSResizableWindowMask;
|
|
|
|
|
}
|
|
|
|
|
new_mask
|
|
|
|
|
};
|
|
|
|
|
self.set_style_mask_async(new_mask);
|
2018-04-18 02:07:54 +08:00
|
|
|
}
|
2019-05-01 17:03:30 -06:00
|
|
|
}
|
2018-04-18 02:07:54 +08:00
|
|
|
|
2022-02-17 15:31:13 +02:00
|
|
|
#[inline]
|
|
|
|
|
pub fn is_decorated(&self) -> bool {
|
|
|
|
|
self.decorations.load(Ordering::Acquire)
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-01 17:03:30 -06:00
|
|
|
#[inline]
|
|
|
|
|
pub fn set_always_on_top(&self, always_on_top: bool) {
|
|
|
|
|
let level = if always_on_top {
|
|
|
|
|
ffi::NSWindowLevel::NSFloatingWindowLevel
|
|
|
|
|
} else {
|
|
|
|
|
ffi::NSWindowLevel::NSNormalWindowLevel
|
|
|
|
|
};
|
2019-06-11 01:09:38 +02:00
|
|
|
unsafe { util::set_level_async(*self.ns_window, level) };
|
2019-05-01 17:03:30 -06:00
|
|
|
}
|
2018-04-18 02:07:54 +08:00
|
|
|
|
2019-05-01 17:03:30 -06:00
|
|
|
#[inline]
|
|
|
|
|
pub fn set_window_icon(&self, _icon: Option<Icon>) {
|
|
|
|
|
// macOS doesn't have window icons. Though, there is
|
|
|
|
|
// `setRepresentedFilename`, but that's semantically distinct and should
|
|
|
|
|
// only be used when the window is in some way representing a specific
|
|
|
|
|
// file/directory. For instance, Terminal.app uses this for the CWD.
|
|
|
|
|
// Anyway, that should eventually be implemented as
|
|
|
|
|
// `WindowBuilderExt::with_represented_file` or something, and doesn't
|
|
|
|
|
// have anything to do with `set_window_icon`.
|
|
|
|
|
// https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/WinPanel/Tasks/SettingWindowTitle.html
|
|
|
|
|
}
|
2018-04-18 02:07:54 +08:00
|
|
|
|
2019-05-01 17:03:30 -06:00
|
|
|
#[inline]
|
2020-01-04 01:32:34 -05:00
|
|
|
pub fn set_ime_position(&self, spot: Position) {
|
2020-02-13 20:41:41 +01:00
|
|
|
let scale_factor = self.scale_factor();
|
|
|
|
|
let logical_spot = spot.to_logical(scale_factor);
|
2022-03-18 14:50:24 +01:00
|
|
|
unsafe { view::set_ime_position(*self.ns_view, logical_spot) };
|
2017-12-22 07:50:46 -05:00
|
|
|
}
|
|
|
|
|
|
2022-05-07 05:29:25 +03:00
|
|
|
#[inline]
|
|
|
|
|
pub fn set_ime_allowed(&self, allowed: bool) {
|
|
|
|
|
unsafe {
|
|
|
|
|
view::set_ime_allowed(*self.ns_view, allowed);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-19 18:39:53 +02:00
|
|
|
#[inline]
|
|
|
|
|
pub fn focus_window(&self) {
|
|
|
|
|
let is_minimized: BOOL = unsafe { msg_send![*self.ns_window, isMiniaturized] };
|
|
|
|
|
let is_minimized = is_minimized == YES;
|
|
|
|
|
let is_visible: BOOL = unsafe { msg_send![*self.ns_window, isVisible] };
|
|
|
|
|
let is_visible = is_visible == YES;
|
|
|
|
|
|
|
|
|
|
if !is_minimized && is_visible {
|
|
|
|
|
unsafe {
|
|
|
|
|
NSApp().activateIgnoringOtherApps_(YES);
|
|
|
|
|
util::make_key_and_order_front_async(*self.ns_window);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-27 03:03:08 +01:00
|
|
|
#[inline]
|
|
|
|
|
pub fn request_user_attention(&self, request_type: Option<UserAttentionType>) {
|
|
|
|
|
let ns_request_type = request_type.map(|ty| match ty {
|
|
|
|
|
UserAttentionType::Critical => NSRequestUserAttentionType::NSCriticalRequest,
|
|
|
|
|
UserAttentionType::Informational => NSRequestUserAttentionType::NSInformationalRequest,
|
|
|
|
|
});
|
|
|
|
|
unsafe {
|
|
|
|
|
if let Some(ty) = ns_request_type {
|
|
|
|
|
NSApp().requestUserAttention_(ty);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-20 10:24:05 -04:00
|
|
|
#[inline]
|
2020-09-07 20:09:24 +03:00
|
|
|
// Allow directly accessing the current monitor internally without unwrapping.
|
|
|
|
|
pub(crate) fn current_monitor_inner(&self) -> RootMonitorHandle {
|
2018-05-20 10:24:05 -04:00
|
|
|
unsafe {
|
2019-06-11 01:09:38 +02:00
|
|
|
let screen: id = msg_send![*self.ns_window, screen];
|
2019-05-01 17:03:30 -06:00
|
|
|
let desc = NSScreen::deviceDescription(screen);
|
2020-01-10 16:02:42 -08:00
|
|
|
let key = util::ns_string_id_ref("NSScreenNumber");
|
2019-05-01 17:03:30 -06:00
|
|
|
let value = NSDictionary::valueForKey_(desc, *key);
|
2022-01-10 21:39:17 +01:00
|
|
|
let display_id: NSUInteger = msg_send![value, unsignedIntegerValue];
|
2019-06-21 11:33:15 -04:00
|
|
|
RootMonitorHandle {
|
2022-01-10 21:39:17 +01:00
|
|
|
inner: MonitorHandle::new(display_id.try_into().unwrap()),
|
2019-06-21 11:33:15 -04:00
|
|
|
}
|
2018-05-20 10:24:05 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 20:09:24 +03:00
|
|
|
#[inline]
|
|
|
|
|
pub fn current_monitor(&self) -> Option<RootMonitorHandle> {
|
|
|
|
|
Some(self.current_monitor_inner())
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-07 17:36:21 -04:00
|
|
|
#[inline]
|
2019-05-29 21:29:54 -04:00
|
|
|
pub fn available_monitors(&self) -> VecDeque<MonitorHandle> {
|
|
|
|
|
monitor::available_monitors()
|
2018-05-07 17:36:21 -04:00
|
|
|
}
|
|
|
|
|
|
2018-05-17 21:28:30 -04:00
|
|
|
#[inline]
|
2020-09-07 20:20:47 +03:00
|
|
|
pub fn primary_monitor(&self) -> Option<RootMonitorHandle> {
|
|
|
|
|
let monitor = monitor::primary_monitor();
|
|
|
|
|
Some(RootMonitorHandle { inner: monitor })
|
2018-05-17 21:28:30 -04:00
|
|
|
}
|
2019-08-14 07:57:16 -04:00
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn raw_window_handle(&self) -> RawWindowHandle {
|
2021-11-30 17:50:23 +01:00
|
|
|
let mut handle = AppKitHandle::empty();
|
|
|
|
|
handle.ns_window = *self.ns_window as *mut _;
|
|
|
|
|
handle.ns_view = *self.ns_view as *mut _;
|
|
|
|
|
RawWindowHandle::AppKit(handle)
|
2019-08-14 07:57:16 -04:00
|
|
|
}
|
2019-05-01 17:03:30 -06:00
|
|
|
}
|
2018-05-17 21:28:30 -04:00
|
|
|
|
2019-05-01 17:03:30 -06:00
|
|
|
impl WindowExtMacOS for UnownedWindow {
|
2017-08-28 01:43:34 +01:00
|
|
|
#[inline]
|
2019-06-11 01:09:38 +02:00
|
|
|
fn ns_window(&self) -> *mut c_void {
|
|
|
|
|
*self.ns_window as *mut _
|
2019-05-01 17:03:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2019-06-11 01:09:38 +02:00
|
|
|
fn ns_view(&self) -> *mut c_void {
|
|
|
|
|
*self.ns_view as *mut _
|
2019-05-01 17:03:30 -06:00
|
|
|
}
|
|
|
|
|
|
2019-05-16 00:26:59 -04:00
|
|
|
#[inline]
|
2019-05-29 21:29:54 -04:00
|
|
|
fn simple_fullscreen(&self) -> bool {
|
2019-05-16 00:26:59 -04:00
|
|
|
let shared_state_lock = self.shared_state.lock().unwrap();
|
|
|
|
|
shared_state_lock.is_simple_fullscreen
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-01 17:03:30 -06:00
|
|
|
#[inline]
|
|
|
|
|
fn set_simple_fullscreen(&self, fullscreen: bool) -> bool {
|
|
|
|
|
let mut shared_state_lock = self.shared_state.lock().unwrap();
|
|
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
|
let app = NSApp();
|
|
|
|
|
let is_native_fullscreen = shared_state_lock.fullscreen.is_some();
|
|
|
|
|
let is_simple_fullscreen = shared_state_lock.is_simple_fullscreen;
|
|
|
|
|
|
|
|
|
|
// Do nothing if native fullscreen is active.
|
2019-06-21 11:33:15 -04:00
|
|
|
if is_native_fullscreen
|
|
|
|
|
|| (fullscreen && is_simple_fullscreen)
|
|
|
|
|
|| (!fullscreen && !is_simple_fullscreen)
|
|
|
|
|
{
|
2019-05-01 17:03:30 -06:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if fullscreen {
|
|
|
|
|
// Remember the original window's settings
|
2020-02-12 17:27:11 +09:00
|
|
|
// Exclude title bar
|
|
|
|
|
shared_state_lock.standard_frame = Some(NSWindow::contentRectForFrameRect_(
|
|
|
|
|
*self.ns_window,
|
|
|
|
|
NSWindow::frame(*self.ns_window),
|
|
|
|
|
));
|
2019-06-11 01:09:38 +02:00
|
|
|
shared_state_lock.saved_style = Some(self.ns_window.styleMask());
|
2019-05-01 17:03:30 -06:00
|
|
|
shared_state_lock.save_presentation_opts = Some(app.presentationOptions_());
|
|
|
|
|
|
|
|
|
|
// Tell our window's state that we're in fullscreen
|
|
|
|
|
shared_state_lock.is_simple_fullscreen = true;
|
|
|
|
|
|
|
|
|
|
// Simulate pre-Lion fullscreen by hiding the dock and menu bar
|
|
|
|
|
let presentation_options =
|
|
|
|
|
NSApplicationPresentationOptions::NSApplicationPresentationAutoHideDock |
|
|
|
|
|
NSApplicationPresentationOptions::NSApplicationPresentationAutoHideMenuBar;
|
|
|
|
|
app.setPresentationOptions_(presentation_options);
|
|
|
|
|
|
|
|
|
|
// Hide the titlebar
|
2019-06-21 11:33:15 -04:00
|
|
|
util::toggle_style_mask(
|
|
|
|
|
*self.ns_window,
|
|
|
|
|
*self.ns_view,
|
|
|
|
|
NSWindowStyleMask::NSTitledWindowMask,
|
|
|
|
|
false,
|
|
|
|
|
);
|
2019-05-01 17:03:30 -06:00
|
|
|
|
|
|
|
|
// Set the window frame to the screen frame size
|
2019-06-11 01:09:38 +02:00
|
|
|
let screen = self.ns_window.screen();
|
2019-05-01 17:03:30 -06:00
|
|
|
let screen_frame = NSScreen::frame(screen);
|
2019-06-11 01:09:38 +02:00
|
|
|
NSWindow::setFrame_display_(*self.ns_window, screen_frame, YES);
|
2019-05-01 17:03:30 -06:00
|
|
|
|
|
|
|
|
// Fullscreen windows can't be resized, minimized, or moved
|
2019-06-21 11:33:15 -04:00
|
|
|
util::toggle_style_mask(
|
|
|
|
|
*self.ns_window,
|
|
|
|
|
*self.ns_view,
|
|
|
|
|
NSWindowStyleMask::NSMiniaturizableWindowMask,
|
|
|
|
|
false,
|
|
|
|
|
);
|
|
|
|
|
util::toggle_style_mask(
|
|
|
|
|
*self.ns_window,
|
|
|
|
|
*self.ns_view,
|
|
|
|
|
NSWindowStyleMask::NSResizableWindowMask,
|
|
|
|
|
false,
|
|
|
|
|
);
|
2019-06-11 01:09:38 +02:00
|
|
|
NSWindow::setMovable_(*self.ns_window, NO);
|
2019-05-01 17:03:30 -06:00
|
|
|
|
|
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
let new_mask = self.saved_style(&mut *shared_state_lock);
|
|
|
|
|
self.set_style_mask_async(new_mask);
|
|
|
|
|
shared_state_lock.is_simple_fullscreen = false;
|
|
|
|
|
|
|
|
|
|
if let Some(presentation_opts) = shared_state_lock.save_presentation_opts {
|
|
|
|
|
app.setPresentationOptions_(presentation_opts);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-18 09:34:27 +03:00
|
|
|
let frame = shared_state_lock.saved_standard_frame();
|
2019-06-11 01:09:38 +02:00
|
|
|
NSWindow::setFrame_display_(*self.ns_window, frame, YES);
|
|
|
|
|
NSWindow::setMovable_(*self.ns_window, YES);
|
2019-05-01 17:03:30 -06:00
|
|
|
|
|
|
|
|
true
|
|
|
|
|
}
|
2018-04-18 02:07:54 +08:00
|
|
|
}
|
2017-08-28 01:43:34 +01:00
|
|
|
}
|
2020-08-14 02:10:34 +08:00
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn has_shadow(&self) -> bool {
|
|
|
|
|
unsafe { self.ns_window.hasShadow() == YES }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn set_has_shadow(&self, has_shadow: bool) {
|
|
|
|
|
unsafe {
|
|
|
|
|
self.ns_window
|
|
|
|
|
.setHasShadow_(if has_shadow { YES } else { NO })
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
|
2019-05-01 17:03:30 -06:00
|
|
|
impl Drop for UnownedWindow {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
trace!("Dropping `UnownedWindow` ({:?})", self as *mut _);
|
|
|
|
|
// Close the window if it has not yet been closed.
|
2019-06-11 01:09:38 +02:00
|
|
|
if *self.ns_window != nil {
|
2021-03-06 11:17:23 +01:00
|
|
|
unsafe { util::close_async(self.ns_window.clone()) };
|
2019-05-01 17:03:30 -06:00
|
|
|
}
|
|
|
|
|
}
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
|
2020-01-04 01:33:07 -05:00
|
|
|
unsafe fn set_min_inner_size<V: NSWindow + Copy>(window: V, mut min_size: LogicalSize<f64>) {
|
2018-06-14 19:42:18 -04:00
|
|
|
let mut current_rect = NSWindow::frame(window);
|
|
|
|
|
let content_rect = NSWindow::contentRectForFrameRect_(window, NSWindow::frame(window));
|
|
|
|
|
// Convert from client area size to window size
|
|
|
|
|
min_size.width += (current_rect.size.width - content_rect.size.width) as f64; // this tends to be 0
|
|
|
|
|
min_size.height += (current_rect.size.height - content_rect.size.height) as f64;
|
2017-02-03 23:05:57 +11:00
|
|
|
window.setMinSize_(NSSize {
|
2018-06-14 19:42:18 -04:00
|
|
|
width: min_size.width as CGFloat,
|
|
|
|
|
height: min_size.height as CGFloat,
|
2017-02-03 23:05:57 +11:00
|
|
|
});
|
|
|
|
|
// If necessary, resize the window to match constraint
|
2018-06-14 19:42:18 -04:00
|
|
|
if current_rect.size.width < min_size.width {
|
|
|
|
|
current_rect.size.width = min_size.width;
|
2020-11-02 23:06:00 +02:00
|
|
|
window.setFrame_display_(current_rect, NO)
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
2018-06-14 19:42:18 -04:00
|
|
|
if current_rect.size.height < min_size.height {
|
|
|
|
|
// The origin point of a rectangle is at its bottom left in Cocoa.
|
|
|
|
|
// To ensure the window's top-left point remains the same:
|
|
|
|
|
current_rect.origin.y += current_rect.size.height - min_size.height;
|
|
|
|
|
current_rect.size.height = min_size.height;
|
2020-11-02 23:06:00 +02:00
|
|
|
window.setFrame_display_(current_rect, NO)
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-04 01:33:07 -05:00
|
|
|
unsafe fn set_max_inner_size<V: NSWindow + Copy>(window: V, mut max_size: LogicalSize<f64>) {
|
2018-06-14 19:42:18 -04:00
|
|
|
let mut current_rect = NSWindow::frame(window);
|
|
|
|
|
let content_rect = NSWindow::contentRectForFrameRect_(window, NSWindow::frame(window));
|
|
|
|
|
// Convert from client area size to window size
|
|
|
|
|
max_size.width += (current_rect.size.width - content_rect.size.width) as f64; // this tends to be 0
|
|
|
|
|
max_size.height += (current_rect.size.height - content_rect.size.height) as f64;
|
2017-02-03 23:05:57 +11:00
|
|
|
window.setMaxSize_(NSSize {
|
2018-06-14 19:42:18 -04:00
|
|
|
width: max_size.width as CGFloat,
|
|
|
|
|
height: max_size.height as CGFloat,
|
2017-02-03 23:05:57 +11:00
|
|
|
});
|
|
|
|
|
// If necessary, resize the window to match constraint
|
2018-06-14 19:42:18 -04:00
|
|
|
if current_rect.size.width > max_size.width {
|
|
|
|
|
current_rect.size.width = max_size.width;
|
2020-11-02 23:06:00 +02:00
|
|
|
window.setFrame_display_(current_rect, NO)
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
2018-06-14 19:42:18 -04:00
|
|
|
if current_rect.size.height > max_size.height {
|
|
|
|
|
// The origin point of a rectangle is at its bottom left in Cocoa.
|
|
|
|
|
// To ensure the window's top-left point remains the same:
|
|
|
|
|
current_rect.origin.y += current_rect.size.height - max_size.height;
|
|
|
|
|
current_rect.size.height = max_size.height;
|
2020-11-02 23:06:00 +02:00
|
|
|
window.setFrame_display_(current_rect, NO)
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
}
|