2022-12-22 21:35:33 +02:00
|
|
|
#![allow(clippy::unnecessary_cast)]
|
|
|
|
|
|
2024-01-14 04:44:10 +01:00
|
|
|
use std::cell::{Cell, RefCell};
|
2023-07-08 22:36:42 +03:00
|
|
|
use std::collections::VecDeque;
|
|
|
|
|
use std::f64;
|
2017-02-03 23:05:57 +11:00
|
|
|
|
2022-09-08 16:45:29 +02:00
|
|
|
use core_graphics::display::{CGDisplay, CGPoint};
|
2023-12-23 20:58:38 +01:00
|
|
|
use icrate::AppKit::{
|
2023-12-23 23:07:55 +01:00
|
|
|
NSAppKitVersionNumber, NSAppKitVersionNumber10_12, NSAppearance, NSApplication,
|
|
|
|
|
NSApplicationPresentationAutoHideDock, NSApplicationPresentationAutoHideMenuBar,
|
|
|
|
|
NSApplicationPresentationFullScreen, NSApplicationPresentationHideDock,
|
|
|
|
|
NSApplicationPresentationHideMenuBar, NSApplicationPresentationOptions, NSBackingStoreBuffered,
|
|
|
|
|
NSColor, NSCriticalRequest, NSFilenamesPboardType, NSInformationalRequest, NSResponder,
|
|
|
|
|
NSScreen, NSView, NSWindow, NSWindowAbove, NSWindowCloseButton, NSWindowFullScreenButton,
|
|
|
|
|
NSWindowLevel, NSWindowMiniaturizeButton, NSWindowSharingNone, NSWindowSharingReadOnly,
|
|
|
|
|
NSWindowStyleMask, NSWindowStyleMaskBorderless, NSWindowStyleMaskClosable,
|
|
|
|
|
NSWindowStyleMaskFullSizeContentView, NSWindowStyleMaskMiniaturizable,
|
|
|
|
|
NSWindowStyleMaskResizable, NSWindowStyleMaskTitled, NSWindowTabbingModePreferred,
|
|
|
|
|
NSWindowTitleHidden, NSWindowZoomButton,
|
2023-12-23 20:58:38 +01:00
|
|
|
};
|
2023-07-29 00:33:03 +02:00
|
|
|
use icrate::Foundation::{
|
2023-12-23 23:07:55 +01:00
|
|
|
CGFloat, MainThreadBound, MainThreadMarker, NSArray, NSCopying, NSObject, NSPoint, NSRect,
|
|
|
|
|
NSSize, NSString,
|
2019-07-29 21:16:14 +03:00
|
|
|
};
|
2023-07-29 00:33:03 +02:00
|
|
|
use objc2::rc::{autoreleasepool, Id};
|
2023-12-23 18:04:24 +01:00
|
|
|
use objc2::{declare_class, msg_send, msg_send_id, mutability, sel, ClassType, DeclaredClass};
|
2022-09-02 18:46:18 +02:00
|
|
|
|
2024-01-14 03:37:53 +01:00
|
|
|
use super::app_delegate::ApplicationDelegate;
|
2023-12-23 20:58:38 +01:00
|
|
|
use super::cursor::cursor_from_icon;
|
2024-01-14 03:37:53 +01:00
|
|
|
use super::event_loop::EventLoopWindowTarget;
|
|
|
|
|
use super::ffi::{
|
|
|
|
|
self, kCGFloatingWindowLevel, kCGNormalWindowLevel, CGSMainConnectionID,
|
|
|
|
|
CGSSetWindowBackgroundBlurRadius,
|
|
|
|
|
};
|
|
|
|
|
use super::monitor::{self, MonitorHandle, VideoModeHandle};
|
2023-12-24 10:12:09 +01:00
|
|
|
use super::monitor::{flip_window_screen_coordinates, get_display_id};
|
2024-01-14 03:37:53 +01:00
|
|
|
use super::view::WinitView;
|
|
|
|
|
use super::window_delegate::WinitWindowDelegate;
|
|
|
|
|
use super::{Fullscreen, OsError};
|
|
|
|
|
use crate::{
|
|
|
|
|
cursor::Cursor,
|
|
|
|
|
dpi::{
|
|
|
|
|
LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize, Position, Size, Size::Logical,
|
|
|
|
|
},
|
|
|
|
|
error::{ExternalError, NotSupportedError, OsError as RootOsError},
|
|
|
|
|
event::WindowEvent,
|
|
|
|
|
icon::Icon,
|
|
|
|
|
platform::macos::{OptionAsAlt, WindowExtMacOS},
|
|
|
|
|
window::{
|
|
|
|
|
CursorGrabMode, ImePurpose, ResizeDirection, Theme, UserAttentionType, WindowAttributes,
|
|
|
|
|
WindowButtons, WindowLevel,
|
|
|
|
|
},
|
|
|
|
|
};
|
2017-02-03 23:05:57 +11:00
|
|
|
|
2023-08-14 21:19:57 +02:00
|
|
|
pub(crate) struct Window {
|
|
|
|
|
window: MainThreadBound<Id<WinitWindow>>,
|
|
|
|
|
// We keep this around so that it doesn't get dropped until the window does.
|
|
|
|
|
_delegate: MainThreadBound<Id<WinitWindowDelegate>>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Drop for Window {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
self.window
|
2023-12-23 18:04:24 +01:00
|
|
|
.get_on_main(|window| autoreleasepool(|_| window.close()))
|
2023-08-14 21:19:57 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Window {
|
2024-01-13 21:36:53 +01:00
|
|
|
pub(crate) fn new(
|
|
|
|
|
_window_target: &EventLoopWindowTarget,
|
2023-08-14 21:19:57 +02:00
|
|
|
attributes: WindowAttributes,
|
|
|
|
|
pl_attribs: PlatformSpecificWindowBuilderAttributes,
|
|
|
|
|
) -> Result<Self, RootOsError> {
|
|
|
|
|
let mtm = MainThreadMarker::new()
|
|
|
|
|
.expect("windows can only be created on the main thread on macOS");
|
2023-12-23 20:58:38 +01:00
|
|
|
let (window, _delegate) =
|
|
|
|
|
autoreleasepool(|_| WinitWindow::new(attributes, pl_attribs, mtm))?;
|
2023-08-14 21:19:57 +02:00
|
|
|
Ok(Window {
|
|
|
|
|
window: MainThreadBound::new(window, mtm),
|
|
|
|
|
_delegate: MainThreadBound::new(_delegate, mtm),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn maybe_queue_on_main(&self, f: impl FnOnce(&WinitWindow) + Send + 'static) {
|
|
|
|
|
// For now, don't actually do queuing, since it may be less predictable
|
|
|
|
|
self.maybe_wait_on_main(f)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn maybe_wait_on_main<R: Send>(
|
|
|
|
|
&self,
|
|
|
|
|
f: impl FnOnce(&WinitWindow) -> R + Send,
|
|
|
|
|
) -> R {
|
2023-12-23 18:04:24 +01:00
|
|
|
self.window.get_on_main(|window| f(window))
|
2023-08-14 21:19:57 +02:00
|
|
|
}
|
2023-12-22 22:33:50 +01:00
|
|
|
|
|
|
|
|
#[cfg(feature = "rwh_06")]
|
|
|
|
|
#[inline]
|
|
|
|
|
pub(crate) fn raw_window_handle_rwh_06(
|
|
|
|
|
&self,
|
|
|
|
|
) -> Result<rwh_06::RawWindowHandle, rwh_06::HandleError> {
|
2023-12-22 23:18:35 +01:00
|
|
|
if let Some(mtm) = MainThreadMarker::new() {
|
|
|
|
|
Ok(self.window.get(mtm).raw_window_handle_rwh_06())
|
|
|
|
|
} else {
|
|
|
|
|
Err(rwh_06::HandleError::Unavailable)
|
|
|
|
|
}
|
2023-12-22 22:33:50 +01:00
|
|
|
}
|
2023-12-26 20:13:02 +01:00
|
|
|
|
|
|
|
|
#[cfg(feature = "rwh_06")]
|
|
|
|
|
#[inline]
|
|
|
|
|
pub(crate) fn raw_display_handle_rwh_06(
|
|
|
|
|
&self,
|
|
|
|
|
) -> Result<rwh_06::RawDisplayHandle, rwh_06::HandleError> {
|
|
|
|
|
Ok(rwh_06::RawDisplayHandle::AppKit(
|
|
|
|
|
rwh_06::AppKitDisplayHandle::new(),
|
|
|
|
|
))
|
|
|
|
|
}
|
2023-08-14 21:19:57 +02:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-02 14:27:19 +03:00
|
|
|
impl From<WindowId> for u64 {
|
|
|
|
|
fn from(window_id: WindowId) -> Self {
|
|
|
|
|
window_id.0 as u64
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<u64> for WindowId {
|
|
|
|
|
fn from(raw_id: u64) -> Self {
|
|
|
|
|
Self(raw_id as usize)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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,
|
2019-07-29 16:07:36 +08:00
|
|
|
pub disallow_hidpi: bool,
|
2020-08-14 02:10:34 +08:00
|
|
|
pub has_shadow: bool,
|
2022-09-13 21:11:18 +02:00
|
|
|
pub accepts_first_mouse: bool,
|
2023-07-13 06:52:34 +00:00
|
|
|
pub tabbing_identifier: Option<String>,
|
2023-06-20 19:07:49 +00:00
|
|
|
pub option_as_alt: OptionAsAlt,
|
2020-08-14 02:10:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
disallow_hidpi: false,
|
|
|
|
|
has_shadow: true,
|
2022-09-13 21:11:18 +02:00
|
|
|
accepts_first_mouse: true,
|
2023-07-13 06:52:34 +00:00
|
|
|
tabbing_identifier: None,
|
2023-06-20 19:07:49 +00:00
|
|
|
option_as_alt: Default::default(),
|
2020-08-14 02:10:34 +08:00
|
|
|
}
|
|
|
|
|
}
|
2019-05-01 17:03:30 -06:00
|
|
|
}
|
2018-04-18 02:07:54 +08:00
|
|
|
|
2022-09-02 18:46:18 +02:00
|
|
|
declare_class!(
|
2022-09-08 16:45:29 +02:00
|
|
|
#[derive(Debug)]
|
2023-12-23 18:04:24 +01:00
|
|
|
pub struct WinitWindow;
|
2023-07-29 00:33:03 +02:00
|
|
|
|
2022-09-02 18:46:18 +02:00
|
|
|
unsafe impl ClassType for WinitWindow {
|
|
|
|
|
#[inherits(NSResponder, NSObject)]
|
2022-09-08 16:45:29 +02:00
|
|
|
type Super = NSWindow;
|
2023-12-23 20:58:38 +01:00
|
|
|
type Mutability = mutability::MainThreadOnly;
|
2023-07-29 00:33:03 +02:00
|
|
|
const NAME: &'static str = "WinitWindow";
|
2022-06-08 11:50:26 -07:00
|
|
|
}
|
2022-01-23 21:35:26 +01:00
|
|
|
|
2023-12-23 18:04:24 +01:00
|
|
|
impl DeclaredClass for WinitWindow {
|
2024-01-14 04:44:10 +01:00
|
|
|
type Ivars = State;
|
2023-07-08 22:36:42 +03:00
|
|
|
}
|
|
|
|
|
|
2022-09-02 18:46:18 +02:00
|
|
|
unsafe impl WinitWindow {
|
2023-07-29 00:33:03 +02:00
|
|
|
#[method(canBecomeMainWindow)]
|
2022-09-02 18:46:18 +02:00
|
|
|
fn can_become_main_window(&self) -> bool {
|
|
|
|
|
trace_scope!("canBecomeMainWindow");
|
|
|
|
|
true
|
|
|
|
|
}
|
2022-01-23 21:35:26 +01:00
|
|
|
|
2023-07-29 00:33:03 +02:00
|
|
|
#[method(canBecomeKeyWindow)]
|
2022-09-02 18:46:18 +02:00
|
|
|
fn can_become_key_window(&self) -> bool {
|
|
|
|
|
trace_scope!("canBecomeKeyWindow");
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
2018-04-28 18:10:06 +02:00
|
|
|
|
2022-09-08 16:45:29 +02:00
|
|
|
#[derive(Debug, Default)]
|
2024-01-14 04:44:10 +01:00
|
|
|
pub struct State {
|
|
|
|
|
resizable: Cell<bool>,
|
2022-09-08 16:45:29 +02:00
|
|
|
/// This field tracks the current fullscreen state of the window
|
|
|
|
|
/// (as seen by `WindowDelegate`).
|
2024-01-14 04:44:10 +01:00
|
|
|
pub(super) fullscreen: RefCell<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.
|
2024-01-14 04:44:10 +01:00
|
|
|
pub(super) in_fullscreen_transition: Cell<bool>,
|
2019-12-25 03:56:56 +09:00
|
|
|
// If it is attempted to toggle fullscreen when in_fullscreen_transition is true,
|
|
|
|
|
// Set target_fullscreen and do after fullscreen transition is end.
|
2024-01-14 04:44:10 +01:00
|
|
|
pub(crate) target_fullscreen: RefCell<Option<Option<Fullscreen>>>,
|
|
|
|
|
pub(super) maximized: Cell<bool>,
|
|
|
|
|
standard_frame: Cell<Option<NSRect>>,
|
|
|
|
|
pub(crate) is_simple_fullscreen: Cell<bool>,
|
|
|
|
|
pub(super) saved_style: Cell<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.
|
2024-01-14 04:44:10 +01:00
|
|
|
save_presentation_opts: Cell<Option<NSApplicationPresentationOptions>>,
|
|
|
|
|
pub(super) current_theme: Cell<Option<Theme>>,
|
2023-01-31 01:35:49 -08:00
|
|
|
|
2024-01-14 04:44:10 +01:00
|
|
|
/// The current resize increments for the window content.
|
|
|
|
|
pub(crate) resize_increments: Cell<NSSize>,
|
2023-06-20 19:07:49 +00:00
|
|
|
/// The state of the `Option` as `Alt`.
|
2024-01-14 04:44:10 +01:00
|
|
|
option_as_alt: Cell<OptionAsAlt>,
|
|
|
|
|
/// Whether the window is showing decorations.
|
|
|
|
|
decorations: Cell<bool>,
|
2022-01-23 21:35:26 +01:00
|
|
|
}
|
|
|
|
|
|
2022-09-08 16:45:29 +02:00
|
|
|
impl WinitWindow {
|
|
|
|
|
#[allow(clippy::type_complexity)]
|
2023-08-14 21:19:57 +02:00
|
|
|
fn new(
|
2022-09-08 16:45:29 +02:00
|
|
|
attrs: WindowAttributes,
|
|
|
|
|
pl_attrs: PlatformSpecificWindowBuilderAttributes,
|
2023-12-23 20:58:38 +01:00
|
|
|
mtm: MainThreadMarker,
|
2023-07-29 00:33:03 +02:00
|
|
|
) -> Result<(Id<Self>, Id<WinitWindowDelegate>), RootOsError> {
|
2022-09-08 16:45:29 +02:00
|
|
|
trace_scope!("WinitWindow::new");
|
|
|
|
|
|
|
|
|
|
let this = autoreleasepool(|_| {
|
2023-12-26 20:13:02 +01:00
|
|
|
let screen = match attrs.fullscreen.clone().map(Into::into) {
|
2022-09-21 10:04:28 +02:00
|
|
|
Some(Fullscreen::Borderless(Some(monitor)))
|
2023-12-26 22:12:33 +01:00
|
|
|
| Some(Fullscreen::Exclusive(VideoModeHandle { monitor, .. })) => {
|
2023-12-23 20:58:38 +01:00
|
|
|
monitor.ns_screen(mtm).or_else(|| NSScreen::mainScreen(mtm))
|
2022-09-21 10:04:28 +02:00
|
|
|
}
|
2023-12-23 20:58:38 +01:00
|
|
|
Some(Fullscreen::Borderless(None)) => NSScreen::mainScreen(mtm),
|
2022-09-08 16:45:29 +02:00
|
|
|
None => None,
|
|
|
|
|
};
|
|
|
|
|
let frame = match &screen {
|
|
|
|
|
Some(screen) => screen.frame(),
|
|
|
|
|
None => {
|
2023-12-23 20:58:38 +01:00
|
|
|
let scale_factor = NSScreen::mainScreen(mtm)
|
2022-09-08 16:45:29 +02:00
|
|
|
.map(|screen| screen.backingScaleFactor() as f64)
|
|
|
|
|
.unwrap_or(1.0);
|
2023-12-24 10:12:09 +01:00
|
|
|
let size = match attrs.inner_size {
|
2022-09-08 16:45:29 +02:00
|
|
|
Some(size) => {
|
2023-12-24 10:12:09 +01:00
|
|
|
let size = size.to_logical(scale_factor);
|
|
|
|
|
NSSize::new(size.width, size.height)
|
2022-09-08 16:45:29 +02:00
|
|
|
}
|
2023-12-24 10:12:09 +01:00
|
|
|
None => NSSize::new(800.0, 600.0),
|
2022-09-08 16:45:29 +02:00
|
|
|
};
|
2023-12-24 10:12:09 +01:00
|
|
|
let position = match attrs.position {
|
2022-09-08 16:45:29 +02:00
|
|
|
Some(position) => {
|
2023-12-24 10:12:09 +01:00
|
|
|
let position = position.to_logical(scale_factor);
|
|
|
|
|
flip_window_screen_coordinates(NSRect::new(
|
|
|
|
|
NSPoint::new(position.x, position.y),
|
|
|
|
|
size,
|
|
|
|
|
))
|
2022-09-08 16:45:29 +02:00
|
|
|
}
|
|
|
|
|
// This value is ignored by calling win.center() below
|
2023-12-24 10:12:09 +01:00
|
|
|
None => NSPoint::new(0.0, 0.0),
|
2022-09-08 16:45:29 +02:00
|
|
|
};
|
2023-12-24 10:12:09 +01:00
|
|
|
NSRect::new(position, size)
|
2022-09-08 16:45:29 +02:00
|
|
|
}
|
|
|
|
|
};
|
2021-04-29 12:52:41 +02:00
|
|
|
|
2022-09-08 16:45:29 +02:00
|
|
|
let mut masks = if (!attrs.decorations && screen.is_none()) || pl_attrs.titlebar_hidden
|
|
|
|
|
{
|
|
|
|
|
// Resizable without a titlebar or borders
|
|
|
|
|
// if decorations is set to false, ignore pl_attrs
|
|
|
|
|
//
|
|
|
|
|
// if the titlebar is hidden, ignore other pl_attrs
|
2023-12-23 23:07:55 +01:00
|
|
|
NSWindowStyleMaskBorderless
|
|
|
|
|
| NSWindowStyleMaskResizable
|
|
|
|
|
| NSWindowStyleMaskMiniaturizable
|
2022-09-08 16:45:29 +02:00
|
|
|
} else {
|
|
|
|
|
// default case, resizable window with titlebar and titlebar buttons
|
2023-12-23 23:07:55 +01:00
|
|
|
NSWindowStyleMaskClosable
|
|
|
|
|
| NSWindowStyleMaskMiniaturizable
|
|
|
|
|
| NSWindowStyleMaskResizable
|
|
|
|
|
| NSWindowStyleMaskTitled
|
2022-09-08 16:45:29 +02:00
|
|
|
};
|
2020-01-04 01:32:34 -05:00
|
|
|
|
2022-09-08 16:45:29 +02:00
|
|
|
if !attrs.resizable {
|
2023-12-23 23:07:55 +01:00
|
|
|
masks &= !NSWindowStyleMaskResizable;
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
|
2022-11-29 12:03:51 +02:00
|
|
|
if !attrs.enabled_buttons.contains(WindowButtons::MINIMIZE) {
|
2023-12-23 23:07:55 +01:00
|
|
|
masks &= !NSWindowStyleMaskMiniaturizable;
|
2022-11-29 12:03:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !attrs.enabled_buttons.contains(WindowButtons::CLOSE) {
|
2023-12-23 23:07:55 +01:00
|
|
|
masks &= !NSWindowStyleMaskClosable;
|
2022-11-29 12:03:51 +02:00
|
|
|
}
|
|
|
|
|
|
2022-09-08 16:45:29 +02:00
|
|
|
if pl_attrs.fullsize_content_view {
|
2023-12-23 23:07:55 +01:00
|
|
|
masks |= NSWindowStyleMaskFullSizeContentView;
|
2022-01-16 11:14:59 +11:00
|
|
|
}
|
2017-07-27 00:59:42 +08:00
|
|
|
|
2024-01-14 04:44:10 +01:00
|
|
|
let this = mtm.alloc().set_ivars(State {
|
|
|
|
|
resizable: Cell::new(attrs.resizable),
|
|
|
|
|
maximized: Cell::new(attrs.maximized),
|
|
|
|
|
decorations: Cell::new(attrs.decorations),
|
2023-07-08 22:36:42 +03:00
|
|
|
..Default::default()
|
2024-01-14 04:44:10 +01:00
|
|
|
});
|
2023-07-29 00:33:03 +02:00
|
|
|
let this: Option<Id<Self>> = unsafe {
|
2022-09-08 16:45:29 +02:00
|
|
|
msg_send_id![
|
2023-12-23 18:04:24 +01:00
|
|
|
super(this),
|
2022-09-08 16:45:29 +02:00
|
|
|
initWithContentRect: frame,
|
|
|
|
|
styleMask: masks,
|
2023-12-23 23:07:55 +01:00
|
|
|
backing: NSBackingStoreBuffered,
|
2023-12-23 18:04:24 +01:00
|
|
|
defer: false,
|
2022-09-08 16:45:29 +02:00
|
|
|
]
|
|
|
|
|
};
|
2023-07-08 22:36:42 +03:00
|
|
|
let this = this?;
|
2022-09-08 16:45:29 +02:00
|
|
|
|
2023-12-23 18:04:24 +01:00
|
|
|
// It is very important for correct memory management that we
|
|
|
|
|
// disable the extra release that would otherwise happen when
|
|
|
|
|
// calling `close` on the window.
|
2023-12-23 23:07:55 +01:00
|
|
|
unsafe { this.setReleasedWhenClosed(false) };
|
2023-12-23 18:04:24 +01:00
|
|
|
|
2023-07-08 22:36:42 +03:00
|
|
|
let resize_increments = match attrs
|
|
|
|
|
.resize_increments
|
|
|
|
|
.map(|i| i.to_logical::<f64>(this.scale_factor()))
|
|
|
|
|
{
|
|
|
|
|
Some(LogicalSize { width, height }) if width >= 1. && height >= 1. => {
|
|
|
|
|
NSSize::new(width, height)
|
|
|
|
|
}
|
|
|
|
|
_ => NSSize::new(1., 1.),
|
|
|
|
|
};
|
2023-02-15 03:32:55 +03:00
|
|
|
|
2024-01-14 04:44:10 +01:00
|
|
|
this.ivars().resize_increments.set(resize_increments);
|
2022-09-08 16:45:29 +02:00
|
|
|
|
2023-07-08 22:36:42 +03:00
|
|
|
this.setTitle(&NSString::from_str(&attrs.title));
|
|
|
|
|
this.setAcceptsMouseMovedEvents(true);
|
2022-09-08 16:45:29 +02:00
|
|
|
|
2023-07-13 06:52:34 +00:00
|
|
|
if let Some(identifier) = pl_attrs.tabbing_identifier {
|
|
|
|
|
this.setTabbingIdentifier(&NSString::from_str(&identifier));
|
2023-12-23 23:07:55 +01:00
|
|
|
this.setTabbingMode(NSWindowTabbingModePreferred);
|
2023-07-13 06:52:34 +00:00
|
|
|
}
|
|
|
|
|
|
2023-07-08 22:36:42 +03:00
|
|
|
if attrs.content_protected {
|
2023-12-23 23:07:55 +01:00
|
|
|
this.setSharingType(NSWindowSharingNone);
|
2023-07-08 22:36:42 +03:00
|
|
|
}
|
2022-11-23 15:51:34 +02:00
|
|
|
|
2023-07-08 22:36:42 +03:00
|
|
|
if pl_attrs.titlebar_transparent {
|
|
|
|
|
this.setTitlebarAppearsTransparent(true);
|
|
|
|
|
}
|
|
|
|
|
if pl_attrs.title_hidden {
|
2023-12-23 23:07:55 +01:00
|
|
|
this.setTitleVisibility(NSWindowTitleHidden);
|
2023-07-08 22:36:42 +03:00
|
|
|
}
|
|
|
|
|
if pl_attrs.titlebar_buttons_hidden {
|
|
|
|
|
for titlebar_button in &[
|
|
|
|
|
#[allow(deprecated)]
|
2023-12-23 23:07:55 +01:00
|
|
|
NSWindowFullScreenButton,
|
|
|
|
|
NSWindowMiniaturizeButton,
|
|
|
|
|
NSWindowCloseButton,
|
|
|
|
|
NSWindowZoomButton,
|
2023-07-08 22:36:42 +03:00
|
|
|
] {
|
|
|
|
|
if let Some(button) = this.standardWindowButton(*titlebar_button) {
|
|
|
|
|
button.setHidden(true);
|
2022-09-08 16:45:29 +02:00
|
|
|
}
|
|
|
|
|
}
|
2023-07-08 22:36:42 +03:00
|
|
|
}
|
|
|
|
|
if pl_attrs.movable_by_window_background {
|
|
|
|
|
this.setMovableByWindowBackground(true);
|
|
|
|
|
}
|
2022-09-08 16:45:29 +02:00
|
|
|
|
2023-07-08 22:36:42 +03:00
|
|
|
if !attrs.enabled_buttons.contains(WindowButtons::MAXIMIZE) {
|
2023-12-23 23:07:55 +01:00
|
|
|
if let Some(button) = this.standardWindowButton(NSWindowZoomButton) {
|
2023-07-08 22:36:42 +03:00
|
|
|
button.setEnabled(false);
|
2022-11-29 12:03:51 +02:00
|
|
|
}
|
2023-07-08 22:36:42 +03:00
|
|
|
}
|
2022-11-29 12:03:51 +02:00
|
|
|
|
2023-07-08 22:36:42 +03:00
|
|
|
if !pl_attrs.has_shadow {
|
|
|
|
|
this.setHasShadow(false);
|
|
|
|
|
}
|
|
|
|
|
if attrs.position.is_none() {
|
|
|
|
|
this.center();
|
|
|
|
|
}
|
2022-09-08 16:45:29 +02:00
|
|
|
|
2023-07-08 22:36:42 +03:00
|
|
|
this.set_option_as_alt(pl_attrs.option_as_alt);
|
2023-06-20 19:07:49 +00:00
|
|
|
|
2023-07-08 22:36:42 +03:00
|
|
|
Some(this)
|
2022-09-08 16:45:29 +02:00
|
|
|
})
|
|
|
|
|
.ok_or_else(|| os_error!(OsError::CreationError("Couldn't create `NSWindow`")))?;
|
|
|
|
|
|
2023-10-14 19:07:39 -07:00
|
|
|
#[cfg(feature = "rwh_06")]
|
2023-12-26 20:13:02 +01:00
|
|
|
match attrs.parent_window.map(|handle| handle.0) {
|
2023-10-14 19:07:39 -07:00
|
|
|
Some(rwh_06::RawWindowHandle::AppKit(handle)) => {
|
2022-12-22 08:07:13 +08:00
|
|
|
// SAFETY: Caller ensures the pointer is valid or NULL
|
2023-10-14 19:07:39 -07:00
|
|
|
// Unwrap is fine, since the pointer comes from `NonNull`.
|
|
|
|
|
let parent_view: Id<NSView> =
|
|
|
|
|
unsafe { Id::retain(handle.ns_view.as_ptr().cast()) }.unwrap();
|
|
|
|
|
let parent = parent_view.window().ok_or_else(|| {
|
|
|
|
|
os_error!(OsError::CreationError(
|
|
|
|
|
"parent view should be installed in a window"
|
|
|
|
|
))
|
|
|
|
|
})?;
|
|
|
|
|
|
2022-12-22 08:07:13 +08:00
|
|
|
// SAFETY: We know that there are no parent -> child -> parent cycles since the only place in `winit`
|
|
|
|
|
// where we allow making a window a child window is right here, just after it's been created.
|
2023-12-23 23:07:55 +01:00
|
|
|
unsafe { parent.addChildWindow_ordered(&this, NSWindowAbove) };
|
2022-12-22 08:07:13 +08:00
|
|
|
}
|
|
|
|
|
Some(raw) => panic!("Invalid raw window handle {raw:?} on macOS"),
|
|
|
|
|
None => (),
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-13 21:11:18 +02:00
|
|
|
let view = WinitView::new(&this, pl_attrs.accepts_first_mouse);
|
2022-09-08 16:45:29 +02:00
|
|
|
|
|
|
|
|
// The default value of `setWantsBestResolutionOpenGLSurface:` was `false` until
|
|
|
|
|
// macos 10.14 and `true` after 10.15, we should set it to `YES` or `NO` to avoid
|
|
|
|
|
// always the default system value in favour of the user's code
|
2023-12-23 20:58:38 +01:00
|
|
|
#[allow(deprecated)]
|
2022-09-08 16:45:29 +02:00
|
|
|
view.setWantsBestResolutionOpenGLSurface(!pl_attrs.disallow_hidpi);
|
|
|
|
|
|
|
|
|
|
// 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.
|
2023-12-23 20:58:38 +01:00
|
|
|
if unsafe { NSAppKitVersionNumber }.floor() > NSAppKitVersionNumber10_12 {
|
2022-09-08 16:45:29 +02:00
|
|
|
view.setWantsLayer(true);
|
2019-05-01 17:03:30 -06:00
|
|
|
}
|
|
|
|
|
|
2022-09-08 16:45:29 +02:00
|
|
|
// Configure the new view as the "key view" for the window
|
2023-12-23 23:07:55 +01:00
|
|
|
this.setContentView(Some(&view));
|
|
|
|
|
this.setInitialFirstResponder(Some(&view));
|
2022-09-08 16:45:29 +02:00
|
|
|
|
|
|
|
|
if attrs.transparent {
|
|
|
|
|
this.setOpaque(false);
|
2023-12-23 23:07:55 +01:00
|
|
|
this.setBackgroundColor(Some(unsafe { &NSColor::clearColor() }));
|
2022-09-08 16:45:29 +02:00
|
|
|
}
|
2019-05-01 17:03:30 -06:00
|
|
|
|
2023-10-28 14:22:10 +04:00
|
|
|
if attrs.blur {
|
|
|
|
|
this.set_blur(attrs.blur);
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-08 16:45:29 +02:00
|
|
|
if let Some(dim) = attrs.min_inner_size {
|
|
|
|
|
this.set_min_inner_size(Some(dim));
|
|
|
|
|
}
|
|
|
|
|
if let Some(dim) = attrs.max_inner_size {
|
|
|
|
|
this.set_max_inner_size(Some(dim));
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-26 03:50:58 +02:00
|
|
|
this.set_window_level(attrs.window_level);
|
|
|
|
|
|
2022-09-08 16:45:29 +02:00
|
|
|
// register for drag and drop operations.
|
2023-07-29 00:33:03 +02:00
|
|
|
this.registerForDraggedTypes(&NSArray::from_id_slice(&[
|
2022-09-08 16:45:29 +02:00
|
|
|
unsafe { NSFilenamesPboardType }.copy()
|
|
|
|
|
]));
|
|
|
|
|
|
2022-10-19 03:34:36 +09:00
|
|
|
match attrs.preferred_theme {
|
|
|
|
|
Some(theme) => {
|
2024-01-14 04:44:10 +01:00
|
|
|
this.ivars().current_theme.set(Some(theme));
|
2022-10-19 03:34:36 +09:00
|
|
|
}
|
|
|
|
|
None => {
|
2024-01-14 04:44:10 +01:00
|
|
|
this.ivars().current_theme.set(Some(get_ns_theme(mtm)));
|
2022-10-19 03:34:36 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-26 19:50:58 +01:00
|
|
|
this.set_cursor(attrs.cursor);
|
|
|
|
|
|
2023-12-26 20:13:02 +01:00
|
|
|
let delegate = WinitWindowDelegate::new(&this, attrs.fullscreen.is_some());
|
2017-02-03 23:05:57 +11:00
|
|
|
|
2023-02-27 20:46:00 +03:00
|
|
|
// XXX Send `Focused(false)` right after creating the window delegate, so we won't
|
|
|
|
|
// obscure the real focused events on the startup.
|
|
|
|
|
delegate.queue_event(WindowEvent::Focused(false));
|
|
|
|
|
|
2018-04-18 02:07:54 +08:00
|
|
|
// Set fullscreen mode after we setup everything
|
2023-12-26 20:13:02 +01:00
|
|
|
this.set_fullscreen(attrs.fullscreen.map(Into::into));
|
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.
|
2022-09-08 16:45:29 +02:00
|
|
|
if attrs.visible {
|
2023-01-27 07:08:29 +02:00
|
|
|
if attrs.active {
|
|
|
|
|
// Tightly linked with `app_state::window_activation_hack`
|
|
|
|
|
this.makeKeyAndOrderFront(None);
|
|
|
|
|
} else {
|
|
|
|
|
this.orderFront(None);
|
|
|
|
|
}
|
2018-04-18 02:07:54 +08:00
|
|
|
}
|
|
|
|
|
|
2022-09-08 16:45:29 +02:00
|
|
|
if attrs.maximized {
|
|
|
|
|
this.set_maximized(attrs.maximized);
|
2018-04-18 02:07:54 +08:00
|
|
|
}
|
2022-09-08 16:45:29 +02:00
|
|
|
|
|
|
|
|
Ok((this, delegate))
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-23 23:07:55 +01:00
|
|
|
#[track_caller]
|
2023-07-29 00:33:03 +02:00
|
|
|
pub(super) fn view(&self) -> Id<WinitView> {
|
2022-09-08 16:45:29 +02:00
|
|
|
// SAFETY: The view inside WinitWindow is always `WinitView`
|
2023-12-23 23:07:55 +01:00
|
|
|
unsafe { Id::cast(self.contentView().unwrap()) }
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
|
2023-08-14 21:19:57 +02:00
|
|
|
fn set_style_mask(&self, mask: NSWindowStyleMask) {
|
|
|
|
|
self.setStyleMask(mask);
|
|
|
|
|
// If we don't do this, key handling will break
|
|
|
|
|
// (at least until the window is clicked again/etc.)
|
2023-12-23 23:07:55 +01:00
|
|
|
let _ = self.makeFirstResponder(Some(&self.contentView().unwrap()));
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
2023-08-14 21:19:57 +02:00
|
|
|
}
|
2017-02-03 23:05:57 +11:00
|
|
|
|
2023-08-14 21:19:57 +02:00
|
|
|
impl WinitWindow {
|
2022-03-18 14:09:39 +01:00
|
|
|
pub fn id(&self) -> WindowId {
|
2022-09-08 16:45:29 +02:00
|
|
|
WindowId(self as *const Self as usize)
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn set_title(&self, title: &str) {
|
2023-08-14 21:19:57 +02:00
|
|
|
self.setTitle(&NSString::from_str(title))
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
|
2023-01-15 23:39:36 +03:00
|
|
|
pub fn set_transparent(&self, transparent: bool) {
|
|
|
|
|
self.setOpaque(!transparent)
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-28 14:22:10 +04:00
|
|
|
pub fn set_blur(&self, blur: bool) {
|
|
|
|
|
// NOTE: in general we want to specify the blur radius, but the choice of 80
|
|
|
|
|
// should be a reasonable default.
|
|
|
|
|
let radius = if blur { 80 } else { 0 };
|
2023-12-23 23:07:55 +01:00
|
|
|
let window_number = unsafe { self.windowNumber() };
|
2023-10-28 14:22:10 +04:00
|
|
|
unsafe {
|
|
|
|
|
CGSSetWindowBackgroundBlurRadius(CGSMainConnectionID(), window_number, radius);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-08 22:53:15 +03:00
|
|
|
|
2019-05-29 21:29:54 -04:00
|
|
|
pub fn set_visible(&self, visible: bool) {
|
|
|
|
|
match visible {
|
2023-08-14 21:19:57 +02:00
|
|
|
true => self.makeKeyAndOrderFront(None),
|
|
|
|
|
false => self.orderOut(None),
|
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> {
|
2022-09-08 16:45:29 +02:00
|
|
|
Some(self.isVisible())
|
2022-02-17 20:44:14 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-01 17:03:30 -06:00
|
|
|
pub fn request_redraw(&self) {
|
2024-01-14 03:37:53 +01:00
|
|
|
let app_delegate = ApplicationDelegate::get(MainThreadMarker::from(self));
|
|
|
|
|
app_delegate.queue_redraw(self.id());
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
|
2023-06-22 08:08:53 +04:00
|
|
|
#[inline]
|
|
|
|
|
pub fn pre_present_notify(&self) {}
|
|
|
|
|
|
2020-01-04 01:33:07 -05:00
|
|
|
pub fn outer_position(&self) -> Result<PhysicalPosition<i32>, NotSupportedError> {
|
2023-12-24 10:12:09 +01:00
|
|
|
let position = flip_window_screen_coordinates(self.frame());
|
|
|
|
|
Ok(LogicalPosition::new(position.x, position.y).to_physical(self.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> {
|
2022-09-08 16:45:29 +02:00
|
|
|
let content_rect = self.contentRectForFrameRect(self.frame());
|
2023-12-24 10:12:09 +01:00
|
|
|
let position = flip_window_screen_coordinates(content_rect);
|
|
|
|
|
Ok(LogicalPosition::new(position.x, position.y).to_physical(self.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) {
|
2023-12-24 10:12:09 +01:00
|
|
|
let position = position.to_logical(self.scale_factor());
|
|
|
|
|
let point = flip_window_screen_coordinates(NSRect::new(
|
|
|
|
|
NSPoint::new(position.x, position.y),
|
|
|
|
|
self.frame().size,
|
|
|
|
|
));
|
|
|
|
|
unsafe { self.setFrameOrigin(point) };
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2020-01-04 01:33:07 -05:00
|
|
|
pub fn inner_size(&self) -> PhysicalSize<u32> {
|
2023-12-24 10:12:09 +01:00
|
|
|
let content_rect = self.contentRectForFrameRect(self.frame());
|
|
|
|
|
let logical = LogicalSize::new(content_rect.size.width, content_rect.size.height);
|
|
|
|
|
logical.to_physical(self.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> {
|
2022-09-08 16:45:29 +02:00
|
|
|
let frame = self.frame();
|
2023-12-24 10:12:09 +01:00
|
|
|
let logical = LogicalSize::new(frame.size.width, frame.size.height);
|
|
|
|
|
logical.to_physical(self.scale_factor())
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2023-07-10 04:02:26 +00:00
|
|
|
pub fn request_inner_size(&self, size: Size) -> Option<PhysicalSize<u32>> {
|
2022-09-08 16:45:29 +02:00
|
|
|
let scale_factor = self.scale_factor();
|
2023-12-24 10:12:09 +01:00
|
|
|
let size = size.to_logical(scale_factor);
|
|
|
|
|
self.setContentSize(NSSize::new(size.width, size.height));
|
2023-07-10 04:02:26 +00:00
|
|
|
None
|
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>) {
|
2022-09-08 16:45:29 +02:00
|
|
|
let dimensions = dimensions.unwrap_or(Logical(LogicalSize {
|
|
|
|
|
width: 0.0,
|
|
|
|
|
height: 0.0,
|
|
|
|
|
}));
|
|
|
|
|
let min_size = dimensions.to_logical::<CGFloat>(self.scale_factor());
|
|
|
|
|
|
2023-12-24 10:12:09 +01:00
|
|
|
let min_size = NSSize::new(min_size.width, min_size.height);
|
|
|
|
|
unsafe { self.setContentMinSize(min_size) };
|
|
|
|
|
|
2022-09-08 16:45:29 +02:00
|
|
|
// If necessary, resize the window to match constraint
|
2023-12-24 10:12:09 +01:00
|
|
|
let mut current_size = self.contentRectForFrameRect(self.frame()).size;
|
|
|
|
|
if current_size.width < min_size.width {
|
|
|
|
|
current_size.width = min_size.width;
|
2022-09-08 16:45:29 +02:00
|
|
|
}
|
2023-12-24 10:12:09 +01:00
|
|
|
if current_size.height < min_size.height {
|
|
|
|
|
current_size.height = min_size.height;
|
2018-03-23 05:35:35 -04:00
|
|
|
}
|
2023-12-24 10:12:09 +01:00
|
|
|
self.setContentSize(current_size);
|
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>) {
|
2022-09-08 16:45:29 +02:00
|
|
|
let dimensions = dimensions.unwrap_or(Logical(LogicalSize {
|
|
|
|
|
width: std::f32::MAX as f64,
|
|
|
|
|
height: std::f32::MAX as f64,
|
|
|
|
|
}));
|
|
|
|
|
let scale_factor = self.scale_factor();
|
|
|
|
|
let max_size = dimensions.to_logical::<CGFloat>(scale_factor);
|
|
|
|
|
|
2023-12-24 10:12:09 +01:00
|
|
|
let max_size = NSSize::new(max_size.width, max_size.height);
|
|
|
|
|
unsafe { self.setContentMaxSize(max_size) };
|
|
|
|
|
|
2022-09-08 16:45:29 +02:00
|
|
|
// If necessary, resize the window to match constraint
|
2023-12-24 10:12:09 +01:00
|
|
|
let mut current_size = self.contentRectForFrameRect(self.frame()).size;
|
|
|
|
|
if max_size.width < current_size.width {
|
|
|
|
|
current_size.width = max_size.width;
|
2022-09-08 16:45:29 +02:00
|
|
|
}
|
2023-12-24 10:12:09 +01:00
|
|
|
if max_size.height < current_size.height {
|
|
|
|
|
current_size.height = max_size.height;
|
2018-03-23 05:35:35 -04:00
|
|
|
}
|
2023-12-24 10:12:09 +01:00
|
|
|
self.setContentSize(current_size);
|
2018-03-23 05:35:35 -04:00
|
|
|
}
|
|
|
|
|
|
2022-09-03 21:50:22 +03:00
|
|
|
pub fn resize_increments(&self) -> Option<PhysicalSize<u32>> {
|
2024-01-14 04:44:10 +01:00
|
|
|
let increments = self.ivars().resize_increments.get();
|
2022-09-08 16:45:29 +02:00
|
|
|
let (w, h) = (increments.width, increments.height);
|
|
|
|
|
if w > 1.0 || h > 1.0 {
|
|
|
|
|
Some(LogicalSize::new(w, h).to_physical(self.scale_factor()))
|
2022-09-03 21:50:22 +03:00
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn set_resize_increments(&self, increments: Option<Size>) {
|
2023-02-15 03:32:55 +03:00
|
|
|
// XXX the resize increments are only used during live resizes.
|
2024-01-14 04:44:10 +01:00
|
|
|
self.ivars().resize_increments.set(
|
|
|
|
|
increments
|
|
|
|
|
.map(|increments| {
|
|
|
|
|
let logical = increments.to_logical::<f64>(self.scale_factor());
|
|
|
|
|
NSSize::new(logical.width.max(1.0), logical.height.max(1.0))
|
|
|
|
|
})
|
|
|
|
|
.unwrap_or_else(|| NSSize::new(1.0, 1.0)),
|
|
|
|
|
);
|
2023-02-15 03:32:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn set_resize_increments_inner(&self, size: NSSize) {
|
|
|
|
|
// It was concluded (#2411) that there is never a use-case for
|
|
|
|
|
// "outer" resize increments, hence we set "inner" ones here.
|
|
|
|
|
// ("outer" in macOS being just resizeIncrements, and "inner" - contentResizeIncrements)
|
|
|
|
|
// This is consistent with X11 size hints behavior
|
2022-09-08 16:45:29 +02:00
|
|
|
self.setContentResizeIncrements(size);
|
2022-09-03 21:50:22 +03:00
|
|
|
}
|
|
|
|
|
|
2018-06-07 13:29:23 -04:00
|
|
|
#[inline]
|
|
|
|
|
pub fn set_resizable(&self, resizable: bool) {
|
2024-01-14 04:44:10 +01:00
|
|
|
self.ivars().resizable.set(resizable);
|
|
|
|
|
let fullscreen = self.ivars().fullscreen.borrow().is_some();
|
2019-05-01 17:03:30 -06:00
|
|
|
if !fullscreen {
|
2022-09-08 16:45:29 +02:00
|
|
|
let mut mask = self.styleMask();
|
2018-06-07 13:29:23 -04:00
|
|
|
if resizable {
|
2023-12-23 23:07:55 +01:00
|
|
|
mask |= NSWindowStyleMaskResizable;
|
2018-06-07 13:29:23 -04:00
|
|
|
} else {
|
2023-12-23 23:07:55 +01:00
|
|
|
mask &= !NSWindowStyleMaskResizable;
|
2018-06-07 13:29:23 -04:00
|
|
|
}
|
2023-08-14 21:19:57 +02:00
|
|
|
self.set_style_mask(mask);
|
2022-11-30 14:30:32 +01:00
|
|
|
}
|
|
|
|
|
// Otherwise, we don't change the mask until we exit fullscreen.
|
2018-06-07 13:29:23 -04:00
|
|
|
}
|
|
|
|
|
|
2022-02-17 17:03:17 +02:00
|
|
|
#[inline]
|
|
|
|
|
pub fn is_resizable(&self) -> bool {
|
2022-09-08 16:45:29 +02:00
|
|
|
self.isResizable()
|
2022-02-17 17:03:17 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-29 12:03:51 +02:00
|
|
|
#[inline]
|
|
|
|
|
pub fn set_enabled_buttons(&self, buttons: WindowButtons) {
|
|
|
|
|
let mut mask = self.styleMask();
|
|
|
|
|
|
|
|
|
|
if buttons.contains(WindowButtons::CLOSE) {
|
2023-12-23 23:07:55 +01:00
|
|
|
mask |= NSWindowStyleMaskClosable;
|
2022-11-29 12:03:51 +02:00
|
|
|
} else {
|
2023-12-23 23:07:55 +01:00
|
|
|
mask &= !NSWindowStyleMaskClosable;
|
2022-11-29 12:03:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if buttons.contains(WindowButtons::MINIMIZE) {
|
2023-12-23 23:07:55 +01:00
|
|
|
mask |= NSWindowStyleMaskMiniaturizable;
|
2022-11-29 12:03:51 +02:00
|
|
|
} else {
|
2023-12-23 23:07:55 +01:00
|
|
|
mask &= !NSWindowStyleMaskMiniaturizable;
|
2022-11-29 12:03:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This must happen before the button's "enabled" status has been set,
|
|
|
|
|
// hence we do it synchronously.
|
2023-08-14 21:19:57 +02:00
|
|
|
self.set_style_mask(mask);
|
2022-11-29 12:03:51 +02:00
|
|
|
|
|
|
|
|
// We edit the button directly instead of using `NSResizableWindowMask`,
|
|
|
|
|
// since that mask also affect the resizability of the window (which is
|
|
|
|
|
// controllable by other means in `winit`).
|
2023-12-23 23:07:55 +01:00
|
|
|
if let Some(button) = self.standardWindowButton(NSWindowZoomButton) {
|
2022-11-29 12:03:51 +02:00
|
|
|
button.setEnabled(buttons.contains(WindowButtons::MAXIMIZE));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn enabled_buttons(&self) -> WindowButtons {
|
|
|
|
|
let mut buttons = WindowButtons::empty();
|
|
|
|
|
if self.isMiniaturizable() {
|
|
|
|
|
buttons |= WindowButtons::MINIMIZE;
|
|
|
|
|
}
|
|
|
|
|
if self
|
2023-12-23 23:07:55 +01:00
|
|
|
.standardWindowButton(NSWindowZoomButton)
|
2022-11-29 12:03:51 +02:00
|
|
|
.map(|b| b.isEnabled())
|
|
|
|
|
.unwrap_or(true)
|
|
|
|
|
{
|
|
|
|
|
buttons |= WindowButtons::MAXIMIZE;
|
|
|
|
|
}
|
|
|
|
|
if self.hasCloseBox() {
|
|
|
|
|
buttons |= WindowButtons::CLOSE;
|
|
|
|
|
}
|
|
|
|
|
buttons
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-25 07:20:52 +01:00
|
|
|
pub fn set_cursor(&self, cursor: Cursor) {
|
2023-12-26 19:26:50 +01:00
|
|
|
let view = self.view();
|
|
|
|
|
|
|
|
|
|
let cursor = match cursor {
|
|
|
|
|
Cursor::Icon(icon) => cursor_from_icon(icon),
|
|
|
|
|
Cursor::Custom(cursor) => cursor.inner.0,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if view.cursor_icon() == cursor {
|
|
|
|
|
return;
|
2023-12-25 07:20:52 +01:00
|
|
|
}
|
2023-12-26 19:26:50 +01:00
|
|
|
|
|
|
|
|
view.set_cursor_icon(cursor);
|
|
|
|
|
self.invalidateCursorRectsForView(&view);
|
2023-12-16 22:02:17 +02:00
|
|
|
}
|
|
|
|
|
|
2018-06-18 12:32:18 -04:00
|
|
|
#[inline]
|
2022-06-13 09:43:14 +03:00
|
|
|
pub fn set_cursor_grab(&self, mode: CursorGrabMode) -> Result<(), ExternalError> {
|
|
|
|
|
let associate_mouse_cursor = match mode {
|
|
|
|
|
CursorGrabMode::Locked => false,
|
|
|
|
|
CursorGrabMode::None => true,
|
|
|
|
|
CursorGrabMode::Confined => {
|
|
|
|
|
return Err(ExternalError::NotSupported(NotSupportedError::new()))
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-06-18 12:32:18 -04:00
|
|
|
// TODO: Do this for real https://stackoverflow.com/a/40922095/5435443
|
2022-06-13 09:43:14 +03:00
|
|
|
CGDisplay::associate_mouse_and_mouse_cursor_position(associate_mouse_cursor)
|
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) {
|
2022-09-08 16:45:29 +02:00
|
|
|
let view = self.view();
|
2023-07-08 22:36:42 +03:00
|
|
|
let state_changed = view.set_cursor_visible(visible);
|
|
|
|
|
if state_changed {
|
2022-09-08 16:45:29 +02:00
|
|
|
self.invalidateCursorRectsForView(&view);
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2020-01-03 14:52:27 -05:00
|
|
|
pub fn scale_factor(&self) -> f64 {
|
2022-09-08 16:45:29 +02:00
|
|
|
self.backingScaleFactor() as f64
|
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);
|
2022-09-08 16:45:29 +02:00
|
|
|
let point = 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> {
|
2023-12-23 23:07:55 +01:00
|
|
|
let mtm = MainThreadMarker::from(self);
|
|
|
|
|
let event = NSApplication::sharedApplication(mtm)
|
|
|
|
|
.currentEvent()
|
|
|
|
|
.unwrap();
|
|
|
|
|
self.performWindowDragWithEvent(&event);
|
2021-03-07 10:43:23 +01:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 18:07:09 +01:00
|
|
|
#[inline]
|
|
|
|
|
pub fn drag_resize_window(&self, _direction: ResizeDirection) -> Result<(), ExternalError> {
|
|
|
|
|
Err(ExternalError::NotSupported(NotSupportedError::new()))
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-11 01:16:16 +03:30
|
|
|
#[inline]
|
|
|
|
|
pub fn show_window_menu(&self, _position: Position) {}
|
|
|
|
|
|
2022-04-12 19:10:46 +02:00
|
|
|
#[inline]
|
|
|
|
|
pub fn set_cursor_hittest(&self, hittest: bool) -> Result<(), ExternalError> {
|
2023-08-14 21:19:57 +02:00
|
|
|
self.setIgnoresMouseEvents(!hittest);
|
2022-04-12 19:10:46 +02:00
|
|
|
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.
|
2022-09-08 16:45:29 +02:00
|
|
|
let curr_mask = self.styleMask();
|
2019-05-01 17:03:30 -06:00
|
|
|
|
2023-12-23 23:07:55 +01:00
|
|
|
let required = NSWindowStyleMaskTitled | NSWindowStyleMaskResizable;
|
|
|
|
|
let needs_temp_mask = !mask_contains(curr_mask, required);
|
2019-05-01 17:03:30 -06:00
|
|
|
if needs_temp_mask {
|
2023-08-14 21:19:57 +02:00
|
|
|
self.set_style_mask(required);
|
2019-05-01 17:03:30 -06:00
|
|
|
}
|
|
|
|
|
|
2022-09-08 16:45:29 +02:00
|
|
|
let is_zoomed = self.isZoomed();
|
2019-05-01 17:03:30 -06:00
|
|
|
|
|
|
|
|
// Roll back temp styles
|
|
|
|
|
if needs_temp_mask {
|
2023-08-14 21:19:57 +02:00
|
|
|
self.set_style_mask(curr_mask);
|
2019-05-01 17:03:30 -06:00
|
|
|
}
|
|
|
|
|
|
2022-09-02 15:48:02 +02:00
|
|
|
is_zoomed
|
2019-05-01 17:03:30 -06:00
|
|
|
}
|
|
|
|
|
|
2024-01-14 04:44:10 +01:00
|
|
|
fn saved_style(&self) -> NSWindowStyleMask {
|
|
|
|
|
let base_mask = self
|
|
|
|
|
.ivars()
|
2019-06-21 11:33:15 -04:00
|
|
|
.saved_style
|
2019-05-01 17:03:30 -06:00
|
|
|
.take()
|
2022-09-08 16:45:29 +02:00
|
|
|
.unwrap_or_else(|| self.styleMask());
|
2024-01-14 04:44:10 +01:00
|
|
|
if self.ivars().resizable.get() {
|
2023-12-23 23:07:55 +01:00
|
|
|
base_mask | NSWindowStyleMaskResizable
|
2019-05-01 17:03:30 -06:00
|
|
|
} else {
|
2023-12-23 23:07:55 +01:00
|
|
|
base_mask & !NSWindowStyleMaskResizable
|
2019-05-01 17:03:30 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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) {
|
2024-01-14 04:44:10 +01:00
|
|
|
self.ivars().fullscreen.replace(None);
|
2019-05-01 17:03:30 -06:00
|
|
|
|
2024-01-14 04:44:10 +01:00
|
|
|
let maximized = self.ivars().maximized.get();
|
|
|
|
|
let mask = self.saved_style();
|
2019-07-29 21:16:14 +03:00
|
|
|
|
2023-08-14 21:19:57 +02:00
|
|
|
self.set_style_mask(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) {
|
2022-09-08 16:45:29 +02:00
|
|
|
let is_minimized = self.isMiniaturized();
|
2019-12-22 01:04:11 -05:00
|
|
|
if is_minimized == minimized {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if minimized {
|
2022-09-08 16:45:29 +02:00
|
|
|
self.miniaturize(Some(self));
|
2019-12-22 01:04:11 -05:00
|
|
|
} else {
|
2023-12-23 23:07:55 +01:00
|
|
|
unsafe { self.deminiaturize(Some(self)) };
|
2019-12-22 01:04:11 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-19 23:39:04 +02:00
|
|
|
#[inline]
|
|
|
|
|
pub fn is_minimized(&self) -> Option<bool> {
|
|
|
|
|
Some(self.isMiniaturized())
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-28 01:43:34 +01:00
|
|
|
#[inline]
|
2018-04-18 02:07:54 +08:00
|
|
|
pub fn set_maximized(&self, maximized: bool) {
|
2023-12-23 20:58:38 +01:00
|
|
|
let mtm = MainThreadMarker::from(self);
|
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
|
|
|
};
|
2023-08-14 21:19:57 +02:00
|
|
|
|
|
|
|
|
// Save the standard frame sized if it is not zoomed
|
|
|
|
|
if !is_zoomed {
|
2024-01-14 04:44:10 +01:00
|
|
|
self.ivars().standard_frame.set(Some(self.frame()));
|
2023-08-14 21:19:57 +02:00
|
|
|
}
|
|
|
|
|
|
2024-01-14 04:44:10 +01:00
|
|
|
self.ivars().maximized.set(maximized);
|
2023-08-14 21:19:57 +02:00
|
|
|
|
2024-01-14 04:44:10 +01:00
|
|
|
if self.ivars().fullscreen.borrow().is_some() {
|
2023-08-14 21:19:57 +02:00
|
|
|
// Handle it in window_did_exit_fullscreen
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-23 23:07:55 +01:00
|
|
|
if mask_contains(self.styleMask(), NSWindowStyleMaskResizable) {
|
2023-08-14 21:19:57 +02:00
|
|
|
// Just use the native zoom if resizable
|
|
|
|
|
self.zoom(None);
|
|
|
|
|
} else {
|
|
|
|
|
// if it's not resizable, we set the frame directly
|
|
|
|
|
let new_rect = if maximized {
|
2023-12-23 20:58:38 +01:00
|
|
|
let screen = NSScreen::mainScreen(mtm).expect("no screen found");
|
2023-08-14 21:19:57 +02:00
|
|
|
screen.visibleFrame()
|
|
|
|
|
} else {
|
2024-01-14 04:44:10 +01:00
|
|
|
self.ivars()
|
|
|
|
|
.standard_frame
|
|
|
|
|
.get()
|
|
|
|
|
.unwrap_or(DEFAULT_STANDARD_FRAME)
|
2023-08-14 21:19:57 +02:00
|
|
|
};
|
|
|
|
|
self.setFrame_display(new_rect, false);
|
|
|
|
|
}
|
2017-09-07 09:33:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2022-09-21 10:04:28 +02:00
|
|
|
pub(crate) fn fullscreen(&self) -> Option<Fullscreen> {
|
2024-01-14 04:44:10 +01:00
|
|
|
self.ivars().fullscreen.borrow().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]
|
2022-09-21 10:04:28 +02:00
|
|
|
pub(crate) fn set_fullscreen(&self, fullscreen: Option<Fullscreen>) {
|
2023-12-23 20:58:38 +01:00
|
|
|
let mtm = MainThreadMarker::from(self);
|
2023-12-23 23:07:55 +01:00
|
|
|
let app = NSApplication::sharedApplication(mtm);
|
|
|
|
|
|
2024-01-14 04:44:10 +01:00
|
|
|
if self.ivars().is_simple_fullscreen.get() {
|
2019-07-29 21:16:14 +03:00
|
|
|
return;
|
|
|
|
|
}
|
2024-01-14 04:44:10 +01:00
|
|
|
if self.ivars().in_fullscreen_transition.get() {
|
2019-12-25 03:56:56 +09:00
|
|
|
// We can't set fullscreen here.
|
|
|
|
|
// Set fullscreen after transition.
|
2024-01-14 04:44:10 +01:00
|
|
|
self.ivars().target_fullscreen.replace(Some(fullscreen));
|
2019-12-25 03:56:56 +09:00
|
|
|
return;
|
|
|
|
|
}
|
2024-01-14 04:44:10 +01:00
|
|
|
let old_fullscreen = self.ivars().fullscreen.borrow().clone();
|
2019-07-29 21:16:14 +03:00
|
|
|
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
|
|
|
|
|
|
|
|
// 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 {
|
2022-09-21 10:04:28 +02:00
|
|
|
Fullscreen::Borderless(Some(monitor)) => monitor.clone(),
|
2022-11-05 10:30:39 +08:00
|
|
|
Fullscreen::Borderless(None) => {
|
|
|
|
|
if let Some(monitor) = self.current_monitor_inner() {
|
|
|
|
|
monitor
|
|
|
|
|
} else {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-09-21 10:04:28 +02:00
|
|
|
Fullscreen::Exclusive(video_mode) => video_mode.monitor(),
|
2019-07-29 21:16:14 +03:00
|
|
|
}
|
2023-12-23 20:58:38 +01:00
|
|
|
.ns_screen(mtm)
|
2019-07-29 21:16:14 +03:00
|
|
|
.unwrap();
|
|
|
|
|
|
2022-09-08 16:45:29 +02:00
|
|
|
let old_screen = self.screen().unwrap();
|
|
|
|
|
if old_screen != new_screen {
|
2023-12-24 10:12:09 +01:00
|
|
|
unsafe { self.setFrameOrigin(new_screen.frame().origin) };
|
2019-07-29 21:16:14 +03:00
|
|
|
}
|
|
|
|
|
}
|
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`..
|
|
|
|
|
|
2022-09-21 10:04:28 +02:00
|
|
|
let display_id = video_mode.monitor().native_identifier();
|
2019-07-29 21:16:14 +03:00
|
|
|
|
|
|
|
|
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(_))) {
|
2024-01-14 04:44:10 +01:00
|
|
|
self.ivars()
|
|
|
|
|
.save_presentation_opts
|
|
|
|
|
.replace(Some(app.presentationOptions()));
|
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
|
|
|
}
|
|
|
|
|
|
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,
|
2022-09-21 10:04:28 +02:00
|
|
|
video_mode.native_mode.0,
|
2019-07-29 21:16:14 +03:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-14 04:44:10 +01:00
|
|
|
self.ivars().fullscreen.replace(fullscreen.clone());
|
2019-09-16 02:59:37 +03:00
|
|
|
|
2023-08-14 21:19:57 +02:00
|
|
|
fn toggle_fullscreen(window: &WinitWindow) {
|
|
|
|
|
// Window level must be restored from `CGShieldingWindowLevel()
|
|
|
|
|
// + 1` back to normal in order for `toggleFullScreen` to do
|
|
|
|
|
// anything
|
2023-12-23 23:07:55 +01:00
|
|
|
window.setLevel(kCGNormalWindowLevel as NSWindowLevel);
|
2023-08-14 21:19:57 +02:00
|
|
|
window.toggleFullScreen(None);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match (old_fullscreen, fullscreen) {
|
|
|
|
|
(None, Some(_)) => {
|
|
|
|
|
// `toggleFullScreen` doesn't work if the `StyleMask` is none, so we
|
|
|
|
|
// set a normal style temporarily. The previous state will be
|
|
|
|
|
// restored in `WindowDelegate::window_did_exit_fullscreen`.
|
|
|
|
|
let curr_mask = self.styleMask();
|
2023-12-23 23:07:55 +01:00
|
|
|
let required = NSWindowStyleMaskTitled | NSWindowStyleMaskResizable;
|
|
|
|
|
if !mask_contains(curr_mask, required) {
|
2023-08-14 21:19:57 +02:00
|
|
|
self.set_style_mask(required);
|
2024-01-14 04:44:10 +01:00
|
|
|
self.ivars().saved_style.set(Some(curr_mask));
|
2023-08-14 21:19:57 +02:00
|
|
|
}
|
|
|
|
|
toggle_fullscreen(self);
|
2022-09-08 16:45:29 +02:00
|
|
|
}
|
2023-08-14 21:19:57 +02:00
|
|
|
(Some(Fullscreen::Borderless(_)), None) => {
|
2019-09-16 02:59:37 +03:00
|
|
|
// State is restored by `window_did_exit_fullscreen`
|
2023-08-14 21:19:57 +02:00
|
|
|
toggle_fullscreen(self);
|
2022-09-08 16:45:29 +02:00
|
|
|
}
|
2023-08-14 21:19:57 +02:00
|
|
|
(Some(Fullscreen::Exclusive(ref video_mode)), None) => {
|
2022-09-08 16:45:29 +02:00
|
|
|
unsafe {
|
2023-08-14 21:19:57 +02:00
|
|
|
ffi::CGRestorePermanentDisplayConfiguration();
|
|
|
|
|
assert_eq!(
|
|
|
|
|
ffi::CGDisplayRelease(video_mode.monitor().native_identifier()),
|
|
|
|
|
ffi::kCGErrorSuccess
|
|
|
|
|
);
|
2022-09-08 16:45:29 +02:00
|
|
|
};
|
2023-08-14 21:19:57 +02:00
|
|
|
toggle_fullscreen(self);
|
2022-09-08 16:45:29 +02:00
|
|
|
}
|
2023-08-14 21:19:57 +02:00
|
|
|
(Some(Fullscreen::Borderless(_)), Some(Fullscreen::Exclusive(_))) => {
|
2019-09-16 02:59:37 +03:00
|
|
|
// 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:`.
|
2024-01-14 04:44:10 +01:00
|
|
|
self.ivars()
|
|
|
|
|
.save_presentation_opts
|
|
|
|
|
.set(Some(app.presentationOptions()));
|
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
|
|
|
|
2023-12-23 23:07:55 +01:00
|
|
|
let presentation_options = NSApplicationPresentationFullScreen
|
|
|
|
|
| NSApplicationPresentationHideDock
|
|
|
|
|
| NSApplicationPresentationHideMenuBar;
|
2022-09-08 16:45:29 +02:00
|
|
|
app.setPresentationOptions(presentation_options);
|
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
|
|
|
|
2023-12-23 23:07:55 +01:00
|
|
|
let window_level = unsafe { ffi::CGShieldingWindowLevel() } as NSWindowLevel + 1;
|
2022-11-22 11:08:56 +01:00
|
|
|
self.setLevel(window_level);
|
2022-09-08 16:45:29 +02:00
|
|
|
}
|
2023-08-14 21:19:57 +02:00
|
|
|
(Some(Fullscreen::Exclusive(ref video_mode)), Some(Fullscreen::Borderless(_))) => {
|
2024-01-14 04:44:10 +01:00
|
|
|
let presentation_options = self.ivars().save_presentation_opts.get().unwrap_or(
|
|
|
|
|
NSApplicationPresentationFullScreen
|
|
|
|
|
| NSApplicationPresentationAutoHideDock
|
|
|
|
|
| NSApplicationPresentationAutoHideMenuBar,
|
|
|
|
|
);
|
2023-12-23 23:07:55 +01:00
|
|
|
app.setPresentationOptions(presentation_options);
|
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
|
|
|
|
2022-09-08 16:45:29 +02:00
|
|
|
unsafe {
|
2023-08-14 21:19:57 +02:00
|
|
|
ffi::CGRestorePermanentDisplayConfiguration();
|
|
|
|
|
assert_eq!(
|
|
|
|
|
ffi::CGDisplayRelease(video_mode.monitor().native_identifier()),
|
|
|
|
|
ffi::kCGErrorSuccess
|
|
|
|
|
);
|
2022-09-08 16:45:29 +02: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
|
|
|
|
|
|
|
|
// Restore the normal window level following the Borderless fullscreen
|
|
|
|
|
// `CGShieldingWindowLevel() + 1` hack.
|
2023-12-23 23:07:55 +01:00
|
|
|
self.setLevel(kCGNormalWindowLevel as NSWindowLevel);
|
2022-09-08 16:45:29 +02: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) {
|
2024-01-14 04:44:10 +01:00
|
|
|
if decorations == self.ivars().decorations.get() {
|
2023-07-08 22:36:42 +03:00
|
|
|
return;
|
|
|
|
|
}
|
2018-04-24 16:20:40 -04:00
|
|
|
|
2024-01-14 04:44:10 +01:00
|
|
|
self.ivars().decorations.set(decorations);
|
2023-07-08 22:36:42 +03:00
|
|
|
|
2024-01-14 04:44:10 +01:00
|
|
|
let fullscreen = self.ivars().fullscreen.borrow().is_some();
|
|
|
|
|
let resizable = self.ivars().resizable.get();
|
2023-07-08 22:36:42 +03:00
|
|
|
|
|
|
|
|
// If we're in fullscreen mode, we wait to apply decoration changes
|
|
|
|
|
// until we're in `window_did_exit_fullscreen`.
|
|
|
|
|
if fullscreen {
|
|
|
|
|
return;
|
2018-04-18 02:07:54 +08:00
|
|
|
}
|
2023-07-08 22:36:42 +03:00
|
|
|
|
|
|
|
|
let new_mask = {
|
|
|
|
|
let mut new_mask = if decorations {
|
2023-12-23 23:07:55 +01:00
|
|
|
NSWindowStyleMaskClosable
|
|
|
|
|
| NSWindowStyleMaskMiniaturizable
|
|
|
|
|
| NSWindowStyleMaskResizable
|
|
|
|
|
| NSWindowStyleMaskTitled
|
2023-07-08 22:36:42 +03:00
|
|
|
} else {
|
2023-12-23 23:07:55 +01:00
|
|
|
NSWindowStyleMaskBorderless | NSWindowStyleMaskResizable
|
2023-07-08 22:36:42 +03:00
|
|
|
};
|
|
|
|
|
if !resizable {
|
2023-12-23 23:07:55 +01:00
|
|
|
new_mask &= !NSWindowStyleMaskResizable;
|
2023-07-08 22:36:42 +03:00
|
|
|
}
|
|
|
|
|
new_mask
|
|
|
|
|
};
|
2023-08-14 21:19:57 +02:00
|
|
|
self.set_style_mask(new_mask);
|
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 {
|
2024-01-14 04:44:10 +01:00
|
|
|
self.ivars().decorations.get()
|
2022-02-17 15:31:13 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-01 17:03:30 -06:00
|
|
|
#[inline]
|
2022-11-26 03:50:58 +02:00
|
|
|
pub fn set_window_level(&self, level: WindowLevel) {
|
|
|
|
|
let level = match level {
|
2023-12-23 23:07:55 +01:00
|
|
|
WindowLevel::AlwaysOnTop => kCGFloatingWindowLevel as NSWindowLevel,
|
|
|
|
|
WindowLevel::AlwaysOnBottom => (kCGNormalWindowLevel - 1) as NSWindowLevel,
|
|
|
|
|
WindowLevel::Normal => kCGNormalWindowLevel as NSWindowLevel,
|
2019-05-01 17:03:30 -06:00
|
|
|
};
|
2023-08-14 21:19:57 +02:00
|
|
|
self.setLevel(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]
|
2023-06-22 19:12:14 +00:00
|
|
|
pub fn set_ime_cursor_area(&self, spot: Position, size: Size) {
|
2020-02-13 20:41:41 +01:00
|
|
|
let scale_factor = self.scale_factor();
|
|
|
|
|
let logical_spot = spot.to_logical(scale_factor);
|
2023-12-24 10:12:09 +01:00
|
|
|
let logical_spot = NSPoint::new(logical_spot.x, logical_spot.y);
|
|
|
|
|
|
2023-06-22 19:12:14 +00:00
|
|
|
let size = size.to_logical(scale_factor);
|
2023-12-24 10:12:09 +01:00
|
|
|
let size = NSSize::new(size.width, size.height);
|
|
|
|
|
|
2023-08-14 21:19:57 +02:00
|
|
|
self.view().set_ime_cursor_area(logical_spot, size);
|
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) {
|
2023-07-08 22:36:42 +03:00
|
|
|
self.view().set_ime_allowed(allowed);
|
2022-05-07 05:29:25 +03:00
|
|
|
}
|
|
|
|
|
|
2023-01-29 16:46:46 +01:00
|
|
|
#[inline]
|
|
|
|
|
pub fn set_ime_purpose(&self, _purpose: ImePurpose) {}
|
|
|
|
|
|
2021-05-19 18:39:53 +02:00
|
|
|
#[inline]
|
|
|
|
|
pub fn focus_window(&self) {
|
2023-12-23 23:07:55 +01:00
|
|
|
let mtm = MainThreadMarker::from(self);
|
2022-09-08 16:45:29 +02:00
|
|
|
let is_minimized = self.isMiniaturized();
|
|
|
|
|
let is_visible = self.isVisible();
|
2021-05-19 18:39:53 +02:00
|
|
|
|
|
|
|
|
if !is_minimized && is_visible {
|
2023-12-23 23:07:55 +01:00
|
|
|
#[allow(deprecated)]
|
|
|
|
|
NSApplication::sharedApplication(mtm).activateIgnoringOtherApps(true);
|
2023-08-14 21:19:57 +02:00
|
|
|
self.makeKeyAndOrderFront(None);
|
2021-05-19 18:39:53 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-27 03:03:08 +01:00
|
|
|
#[inline]
|
|
|
|
|
pub fn request_user_attention(&self, request_type: Option<UserAttentionType>) {
|
2023-12-23 23:07:55 +01:00
|
|
|
let mtm = MainThreadMarker::from(self);
|
2020-11-27 03:03:08 +01:00
|
|
|
let ns_request_type = request_type.map(|ty| match ty {
|
2023-12-23 23:07:55 +01:00
|
|
|
UserAttentionType::Critical => NSCriticalRequest,
|
|
|
|
|
UserAttentionType::Informational => NSInformationalRequest,
|
2020-11-27 03:03:08 +01:00
|
|
|
});
|
2022-09-08 16:45:29 +02:00
|
|
|
if let Some(ty) = ns_request_type {
|
2023-12-23 23:07:55 +01:00
|
|
|
NSApplication::sharedApplication(mtm).requestUserAttention(ty);
|
2020-11-27 03:03:08 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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.
|
2022-11-05 10:30:39 +08:00
|
|
|
pub(crate) fn current_monitor_inner(&self) -> Option<MonitorHandle> {
|
2023-12-23 20:58:38 +01:00
|
|
|
let display_id = get_display_id(&*self.screen()?);
|
2022-11-05 10:30:39 +08:00
|
|
|
Some(MonitorHandle::new(display_id))
|
2018-05-20 10:24:05 -04:00
|
|
|
}
|
|
|
|
|
|
2020-09-07 20:09:24 +03:00
|
|
|
#[inline]
|
2022-09-21 10:04:28 +02:00
|
|
|
pub fn current_monitor(&self) -> Option<MonitorHandle> {
|
2022-11-05 10:30:39 +08:00
|
|
|
self.current_monitor_inner()
|
2020-09-07 20:09:24 +03:00
|
|
|
}
|
|
|
|
|
|
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]
|
2022-09-21 10:04:28 +02:00
|
|
|
pub fn primary_monitor(&self) -> Option<MonitorHandle> {
|
2020-09-07 20:20:47 +03:00
|
|
|
let monitor = monitor::primary_monitor();
|
2022-09-21 10:04:28 +02:00
|
|
|
Some(monitor)
|
2018-05-17 21:28:30 -04:00
|
|
|
}
|
2019-08-14 07:57:16 -04:00
|
|
|
|
2023-10-14 19:07:39 -07:00
|
|
|
#[cfg(feature = "rwh_04")]
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn raw_window_handle_rwh_04(&self) -> rwh_04::RawWindowHandle {
|
|
|
|
|
let mut window_handle = rwh_04::AppKitHandle::empty();
|
|
|
|
|
window_handle.ns_window = self as *const Self as *mut _;
|
2023-12-23 23:07:55 +01:00
|
|
|
window_handle.ns_view = Id::as_ptr(&self.contentView().unwrap()) as *mut _;
|
2023-10-14 19:07:39 -07:00
|
|
|
rwh_04::RawWindowHandle::AppKit(window_handle)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "rwh_05")]
|
2019-08-14 07:57:16 -04:00
|
|
|
#[inline]
|
2023-10-14 19:07:39 -07:00
|
|
|
pub fn raw_window_handle_rwh_05(&self) -> rwh_05::RawWindowHandle {
|
|
|
|
|
let mut window_handle = rwh_05::AppKitWindowHandle::empty();
|
2023-08-05 20:56:22 +02:00
|
|
|
window_handle.ns_window = self as *const Self as *mut _;
|
2023-12-23 23:07:55 +01:00
|
|
|
window_handle.ns_view = Id::as_ptr(&self.contentView().unwrap()) as *mut _;
|
2023-10-14 19:07:39 -07:00
|
|
|
rwh_05::RawWindowHandle::AppKit(window_handle)
|
2022-07-21 22:22:36 +03:00
|
|
|
}
|
|
|
|
|
|
2023-10-14 19:07:39 -07:00
|
|
|
#[cfg(feature = "rwh_05")]
|
2022-07-21 22:22:36 +03:00
|
|
|
#[inline]
|
2023-10-14 19:07:39 -07:00
|
|
|
pub fn raw_display_handle_rwh_05(&self) -> rwh_05::RawDisplayHandle {
|
|
|
|
|
rwh_05::RawDisplayHandle::AppKit(rwh_05::AppKitDisplayHandle::empty())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "rwh_06")]
|
|
|
|
|
#[inline]
|
2023-12-22 22:33:50 +01:00
|
|
|
pub fn raw_window_handle_rwh_06(&self) -> rwh_06::RawWindowHandle {
|
2023-10-14 19:07:39 -07:00
|
|
|
let window_handle = rwh_06::AppKitWindowHandle::new({
|
2023-12-23 23:07:55 +01:00
|
|
|
let ptr = Id::as_ptr(&self.contentView().unwrap()) as *mut _;
|
2023-10-14 19:07:39 -07:00
|
|
|
std::ptr::NonNull::new(ptr).expect("Id<T> should never be null")
|
|
|
|
|
});
|
2023-12-22 22:33:50 +01:00
|
|
|
rwh_06::RawWindowHandle::AppKit(window_handle)
|
2023-10-14 19:07:39 -07:00
|
|
|
}
|
|
|
|
|
|
2022-09-08 16:45:29 +02:00
|
|
|
fn toggle_style_mask(&self, mask: NSWindowStyleMask, on: bool) {
|
|
|
|
|
let current_style_mask = self.styleMask();
|
|
|
|
|
if on {
|
2023-08-14 21:19:57 +02:00
|
|
|
self.set_style_mask(current_style_mask | mask);
|
2022-09-08 16:45:29 +02:00
|
|
|
} else {
|
2023-08-14 21:19:57 +02:00
|
|
|
self.set_style_mask(current_style_mask & (!mask));
|
2022-09-08 16:45:29 +02:00
|
|
|
}
|
|
|
|
|
}
|
2022-10-19 03:34:36 +09:00
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn theme(&self) -> Option<Theme> {
|
2024-01-14 04:44:10 +01:00
|
|
|
self.ivars().current_theme.get()
|
2022-10-19 03:34:36 +09:00
|
|
|
}
|
2022-11-03 10:11:37 -07:00
|
|
|
|
2022-11-29 11:05:51 +02:00
|
|
|
#[inline]
|
2023-01-17 03:30:14 +02:00
|
|
|
pub fn has_focus(&self) -> bool {
|
|
|
|
|
self.isKeyWindow()
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-29 11:05:51 +02:00
|
|
|
pub fn set_theme(&self, theme: Option<Theme>) {
|
2023-12-23 23:07:55 +01:00
|
|
|
let mtm = MainThreadMarker::from(self);
|
|
|
|
|
set_ns_theme(theme, mtm);
|
2024-01-14 04:44:10 +01:00
|
|
|
self.ivars()
|
|
|
|
|
.current_theme
|
|
|
|
|
.set(theme.or_else(|| Some(get_ns_theme(mtm))));
|
2022-11-29 11:05:51 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-03 10:11:37 -07:00
|
|
|
#[inline]
|
2022-11-23 15:51:34 +02:00
|
|
|
pub fn set_content_protected(&self, protected: bool) {
|
|
|
|
|
self.setSharingType(if protected {
|
2023-12-23 23:07:55 +01:00
|
|
|
NSWindowSharingNone
|
2022-11-23 15:51:34 +02:00
|
|
|
} else {
|
2023-12-23 23:07:55 +01:00
|
|
|
NSWindowSharingReadOnly
|
2022-11-23 15:51:34 +02:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-03 10:11:37 -07:00
|
|
|
pub fn title(&self) -> String {
|
2023-12-23 23:07:55 +01:00
|
|
|
let window: &NSWindow = self;
|
|
|
|
|
window.title().to_string()
|
2022-11-03 10:11:37 -07:00
|
|
|
}
|
2023-05-28 20:02:59 +02:00
|
|
|
|
|
|
|
|
pub fn reset_dead_keys(&self) {
|
|
|
|
|
// (Artur) I couldn't find a way to implement this.
|
|
|
|
|
}
|
2019-05-01 17:03:30 -06:00
|
|
|
}
|
2018-05-17 21:28:30 -04:00
|
|
|
|
2022-09-08 16:45:29 +02:00
|
|
|
impl WindowExtMacOS for WinitWindow {
|
2019-05-16 00:26:59 -04:00
|
|
|
#[inline]
|
2019-05-29 21:29:54 -04:00
|
|
|
fn simple_fullscreen(&self) -> bool {
|
2024-01-14 04:44:10 +01:00
|
|
|
self.ivars().is_simple_fullscreen.get()
|
2019-05-16 00:26:59 -04:00
|
|
|
}
|
|
|
|
|
|
2019-05-01 17:03:30 -06:00
|
|
|
#[inline]
|
|
|
|
|
fn set_simple_fullscreen(&self, fullscreen: bool) -> bool {
|
2023-12-23 23:07:55 +01:00
|
|
|
let mtm = MainThreadMarker::from(self);
|
2019-05-01 17:03:30 -06:00
|
|
|
|
2023-12-23 23:07:55 +01:00
|
|
|
let app = NSApplication::sharedApplication(mtm);
|
2024-01-14 04:44:10 +01:00
|
|
|
let is_native_fullscreen = self.ivars().fullscreen.borrow().is_some();
|
|
|
|
|
let is_simple_fullscreen = self.ivars().is_simple_fullscreen.get();
|
2019-05-01 17:03:30 -06:00
|
|
|
|
2022-09-08 16:45:29 +02:00
|
|
|
// Do nothing if native fullscreen is active.
|
|
|
|
|
if is_native_fullscreen
|
|
|
|
|
|| (fullscreen && is_simple_fullscreen)
|
|
|
|
|
|| (!fullscreen && !is_simple_fullscreen)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-05-01 17:03:30 -06:00
|
|
|
|
2022-09-08 16:45:29 +02:00
|
|
|
if fullscreen {
|
|
|
|
|
// Remember the original window's settings
|
|
|
|
|
// Exclude title bar
|
2024-01-14 04:44:10 +01:00
|
|
|
self.ivars()
|
|
|
|
|
.standard_frame
|
|
|
|
|
.set(Some(self.contentRectForFrameRect(self.frame())));
|
|
|
|
|
self.ivars().saved_style.set(Some(self.styleMask()));
|
|
|
|
|
self.ivars()
|
|
|
|
|
.save_presentation_opts
|
|
|
|
|
.set(Some(app.presentationOptions()));
|
2019-05-01 17:03:30 -06:00
|
|
|
|
2022-09-08 16:45:29 +02:00
|
|
|
// Tell our window's state that we're in fullscreen
|
2024-01-14 04:44:10 +01:00
|
|
|
self.ivars().is_simple_fullscreen.set(true);
|
2023-03-01 14:47:45 -08:00
|
|
|
|
2022-09-08 16:45:29 +02:00
|
|
|
// Simulate pre-Lion fullscreen by hiding the dock and menu bar
|
|
|
|
|
let presentation_options =
|
2023-12-23 23:07:55 +01:00
|
|
|
NSApplicationPresentationAutoHideDock | NSApplicationPresentationAutoHideMenuBar;
|
2022-09-08 16:45:29 +02:00
|
|
|
app.setPresentationOptions(presentation_options);
|
2019-05-01 17:03:30 -06:00
|
|
|
|
2022-09-08 16:45:29 +02:00
|
|
|
// Hide the titlebar
|
2023-12-23 23:07:55 +01:00
|
|
|
self.toggle_style_mask(NSWindowStyleMaskTitled, false);
|
2022-09-08 16:45:29 +02:00
|
|
|
|
|
|
|
|
// Set the window frame to the screen frame size
|
|
|
|
|
let screen = self.screen().expect("expected screen to be available");
|
|
|
|
|
self.setFrame_display(screen.frame(), true);
|
2019-05-01 17:03:30 -06:00
|
|
|
|
2022-09-08 16:45:29 +02:00
|
|
|
// Fullscreen windows can't be resized, minimized, or moved
|
2023-12-23 23:07:55 +01:00
|
|
|
self.toggle_style_mask(NSWindowStyleMaskMiniaturizable, false);
|
|
|
|
|
self.toggle_style_mask(NSWindowStyleMaskResizable, false);
|
2022-09-08 16:45:29 +02:00
|
|
|
self.setMovable(false);
|
2019-05-01 17:03:30 -06:00
|
|
|
|
2022-09-08 16:45:29 +02:00
|
|
|
true
|
|
|
|
|
} else {
|
2024-01-14 04:44:10 +01:00
|
|
|
let new_mask = self.saved_style();
|
2023-08-14 21:19:57 +02:00
|
|
|
self.set_style_mask(new_mask);
|
2024-01-14 04:44:10 +01:00
|
|
|
self.ivars().is_simple_fullscreen.set(false);
|
2022-09-08 16:45:29 +02:00
|
|
|
|
2024-01-14 04:44:10 +01:00
|
|
|
let save_presentation_opts = self.ivars().save_presentation_opts.get();
|
|
|
|
|
let frame = self
|
|
|
|
|
.ivars()
|
|
|
|
|
.standard_frame
|
|
|
|
|
.get()
|
|
|
|
|
.unwrap_or(DEFAULT_STANDARD_FRAME);
|
2023-03-01 14:47:45 -08:00
|
|
|
|
|
|
|
|
if let Some(presentation_opts) = save_presentation_opts {
|
2022-09-08 16:45:29 +02:00
|
|
|
app.setPresentationOptions(presentation_opts);
|
2019-05-01 17:03:30 -06:00
|
|
|
}
|
2022-09-08 16:45:29 +02:00
|
|
|
|
|
|
|
|
self.setFrame_display(frame, true);
|
|
|
|
|
self.setMovable(true);
|
|
|
|
|
|
|
|
|
|
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 {
|
2022-09-08 16:45:29 +02:00
|
|
|
self.hasShadow()
|
2020-08-14 02:10:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn set_has_shadow(&self, has_shadow: bool) {
|
2022-09-08 16:45:29 +02:00
|
|
|
self.setHasShadow(has_shadow)
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
2022-11-23 17:07:41 +02:00
|
|
|
|
2023-07-13 06:52:34 +00:00
|
|
|
#[inline]
|
|
|
|
|
fn set_tabbing_identifier(&self, identifier: &str) {
|
|
|
|
|
self.setTabbingIdentifier(&NSString::from_str(identifier))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn tabbing_identifier(&self) -> String {
|
|
|
|
|
self.tabbingIdentifier().to_string()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn select_next_tab(&self) {
|
2023-12-23 23:07:55 +01:00
|
|
|
self.selectNextTab(None)
|
2023-07-13 06:52:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn select_previous_tab(&self) {
|
2023-12-23 23:07:55 +01:00
|
|
|
unsafe { self.selectPreviousTab(None) }
|
2023-07-13 06:52:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2023-07-14 08:01:40 +00:00
|
|
|
fn select_tab_at_index(&self, index: usize) {
|
2023-10-20 14:05:57 +04:00
|
|
|
if let Some(group) = self.tabGroup() {
|
2023-12-23 23:07:55 +01:00
|
|
|
if let Some(windows) = unsafe { self.tabbedWindows() } {
|
2023-10-20 14:05:57 +04:00
|
|
|
if index < windows.len() {
|
2023-12-23 23:07:55 +01:00
|
|
|
group.setSelectedWindow(Some(&windows[index]));
|
2023-10-20 14:05:57 +04:00
|
|
|
}
|
|
|
|
|
}
|
2023-07-13 06:52:34 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-14 08:01:40 +00:00
|
|
|
#[inline]
|
|
|
|
|
fn num_tabs(&self) -> usize {
|
2023-12-23 23:07:55 +01:00
|
|
|
unsafe { self.tabbedWindows() }
|
2023-10-20 14:05:57 +04:00
|
|
|
.map(|windows| windows.len())
|
|
|
|
|
.unwrap_or(1)
|
2023-07-14 08:01:40 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-23 17:07:41 +02:00
|
|
|
fn is_document_edited(&self) -> bool {
|
|
|
|
|
self.isDocumentEdited()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn set_document_edited(&self, edited: bool) {
|
|
|
|
|
self.setDocumentEdited(edited)
|
|
|
|
|
}
|
2023-06-20 19:07:49 +00:00
|
|
|
|
|
|
|
|
fn set_option_as_alt(&self, option_as_alt: OptionAsAlt) {
|
2024-01-14 04:44:10 +01:00
|
|
|
self.ivars().option_as_alt.set(option_as_alt);
|
2023-06-20 19:07:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn option_as_alt(&self) -> OptionAsAlt {
|
2024-01-14 04:44:10 +01:00
|
|
|
self.ivars().option_as_alt.get()
|
2023-06-20 19:07:49 +00:00
|
|
|
}
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
2022-10-19 03:34:36 +09:00
|
|
|
|
2023-12-23 23:07:55 +01:00
|
|
|
fn mask_contains(mask: NSWindowStyleMask, value: NSWindowStyleMask) -> bool {
|
|
|
|
|
mask & value == value
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(super) fn get_ns_theme(mtm: MainThreadMarker) -> Theme {
|
|
|
|
|
let app = NSApplication::sharedApplication(mtm);
|
2022-10-19 03:34:36 +09:00
|
|
|
let has_theme: bool = unsafe { msg_send![&app, respondsToSelector: sel!(effectiveAppearance)] };
|
|
|
|
|
if !has_theme {
|
|
|
|
|
return Theme::Light;
|
|
|
|
|
}
|
|
|
|
|
let appearance = app.effectiveAppearance();
|
2023-12-23 20:58:38 +01:00
|
|
|
let name = appearance
|
|
|
|
|
.bestMatchFromAppearancesWithNames(&NSArray::from_id_slice(&[
|
|
|
|
|
NSString::from_str("NSAppearanceNameAqua"),
|
|
|
|
|
NSString::from_str("NSAppearanceNameDarkAqua"),
|
|
|
|
|
]))
|
|
|
|
|
.unwrap();
|
2022-10-19 03:34:36 +09:00
|
|
|
match &*name.to_string() {
|
|
|
|
|
"NSAppearanceNameDarkAqua" => Theme::Dark,
|
|
|
|
|
_ => Theme::Light,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-23 23:07:55 +01:00
|
|
|
fn set_ns_theme(theme: Option<Theme>, mtm: MainThreadMarker) {
|
|
|
|
|
let app = NSApplication::sharedApplication(mtm);
|
2022-10-19 03:34:36 +09:00
|
|
|
let has_theme: bool = unsafe { msg_send![&app, respondsToSelector: sel!(effectiveAppearance)] };
|
|
|
|
|
if has_theme {
|
2022-11-29 11:05:51 +02:00
|
|
|
let appearance = theme.map(|t| {
|
|
|
|
|
let name = match t {
|
|
|
|
|
Theme::Dark => NSString::from_str("NSAppearanceNameDarkAqua"),
|
|
|
|
|
Theme::Light => NSString::from_str("NSAppearanceNameAqua"),
|
|
|
|
|
};
|
2023-12-23 20:58:38 +01:00
|
|
|
NSAppearance::appearanceNamed(&name).unwrap()
|
2022-11-29 11:05:51 +02:00
|
|
|
});
|
|
|
|
|
app.setAppearance(appearance.as_ref().map(|a| a.as_ref()));
|
2022-10-19 03:34:36 +09:00
|
|
|
}
|
|
|
|
|
}
|
2024-01-14 04:44:10 +01:00
|
|
|
|
|
|
|
|
const DEFAULT_STANDARD_FRAME: NSRect =
|
|
|
|
|
NSRect::new(NSPoint::new(50.0, 50.0), NSSize::new(800.0, 600.0));
|