2018-06-15 09:42:18 +10:00
|
|
|
use std;
|
2018-07-27 07:14:16 +10:00
|
|
|
use std::cell::{Cell, RefCell};
|
2018-11-06 06:34:54 +11:00
|
|
|
use std::f64;
|
2018-06-15 09:42:18 +10:00
|
|
|
use std::ops::Deref;
|
|
|
|
use std::os::raw::c_void;
|
|
|
|
use std::sync::Weak;
|
2018-07-27 07:14:16 +10:00
|
|
|
use std::sync::atomic::{Ordering, AtomicBool};
|
2017-02-03 23:05:57 +11:00
|
|
|
|
2018-06-15 09:42:18 +10:00
|
|
|
use cocoa::appkit::{
|
|
|
|
self,
|
|
|
|
CGFloat,
|
|
|
|
NSApplication,
|
|
|
|
NSColor,
|
|
|
|
NSScreen,
|
|
|
|
NSView,
|
|
|
|
NSWindow,
|
|
|
|
NSWindowButton,
|
|
|
|
NSWindowStyleMask,
|
2018-11-06 10:54:22 +11:00
|
|
|
NSApplicationActivationPolicy,
|
2018-06-15 09:42:18 +10:00
|
|
|
};
|
2017-02-03 23:05:57 +11:00
|
|
|
use cocoa::base::{id, nil};
|
2018-06-15 09:42:18 +10:00
|
|
|
use cocoa::foundation::{NSAutoreleasePool, NSDictionary, NSPoint, NSRect, NSSize, NSString};
|
2017-02-03 23:05:57 +11:00
|
|
|
|
2017-10-31 21:03:18 +11:00
|
|
|
use core_graphics::display::CGDisplay;
|
2017-02-03 23:05:57 +11:00
|
|
|
|
2018-06-15 09:42:18 +10:00
|
|
|
use objc;
|
|
|
|
use objc::runtime::{Class, Object, Sel, BOOL, YES, NO};
|
|
|
|
use objc::declare::ClassDecl;
|
2017-02-03 23:05:57 +11:00
|
|
|
|
2018-06-15 09:42:18 +10:00
|
|
|
use {
|
|
|
|
CreationError,
|
|
|
|
Event,
|
|
|
|
LogicalPosition,
|
|
|
|
LogicalSize,
|
|
|
|
MouseCursor,
|
|
|
|
WindowAttributes,
|
|
|
|
WindowEvent,
|
|
|
|
WindowId,
|
|
|
|
};
|
|
|
|
use CreationError::OsError;
|
|
|
|
use os::macos::{ActivationPolicy, WindowExt};
|
|
|
|
use platform::platform::{ffi, util};
|
|
|
|
use platform::platform::events_loop::{EventsLoop, Shared};
|
2018-05-18 11:28:30 +10:00
|
|
|
use platform::platform::view::{new_view, set_ime_spot};
|
2017-09-07 18:33:46 +10:00
|
|
|
use window::MonitorId as RootMonitorId;
|
2017-02-03 23:05:57 +11:00
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
|
|
pub struct Id(pub usize);
|
|
|
|
|
2018-07-27 07:14:16 +10:00
|
|
|
// TODO: It's possible for delegate methods to be called asynchronously, causing data races / `RefCell` panics.
|
2018-06-15 09:42:18 +10:00
|
|
|
pub struct DelegateState {
|
2017-02-03 23:05:57 +11:00
|
|
|
view: IdRef,
|
|
|
|
window: IdRef,
|
2017-06-09 22:13:30 +10:00
|
|
|
shared: Weak<Shared>,
|
2018-04-18 04:07:54 +10:00
|
|
|
|
|
|
|
win_attribs: RefCell<WindowAttributes>,
|
|
|
|
standard_frame: Cell<Option<NSRect>>,
|
|
|
|
save_style_mask: Cell<Option<NSWindowStyleMask>>,
|
|
|
|
|
|
|
|
// This is set when WindowBuilder::with_fullscreen was set,
|
|
|
|
// see comments of `window_did_fail_to_enter_fullscreen`
|
|
|
|
handle_with_fullscreen: bool,
|
2018-04-28 10:46:20 +10:00
|
|
|
|
2018-06-15 09:42:18 +10:00
|
|
|
// During `windowDidResize`, we use this to only send Moved if the position changed.
|
|
|
|
previous_position: Option<(f64, f64)>,
|
|
|
|
|
|
|
|
// Used to prevent redundant events.
|
|
|
|
previous_dpi_factor: f64,
|
2018-04-18 04:07:54 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DelegateState {
|
|
|
|
fn is_zoomed(&self) -> bool {
|
|
|
|
unsafe {
|
|
|
|
// Because isZoomed do not work in Borderless mode, we set it
|
|
|
|
// resizable temporality
|
|
|
|
let curr_mask = self.window.styleMask();
|
|
|
|
|
2018-06-08 03:29:23 +10:00
|
|
|
let required = NSWindowStyleMask::NSTitledWindowMask | NSWindowStyleMask::NSResizableWindowMask;
|
|
|
|
let needs_temp_mask = !curr_mask.contains(required);
|
|
|
|
if needs_temp_mask {
|
|
|
|
util::set_style_mask(*self.window, *self.view, required);
|
2018-04-18 04:07:54 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
let is_zoomed: BOOL = msg_send![*self.window, isZoomed];
|
|
|
|
|
|
|
|
// Roll back temp styles
|
2018-06-08 03:29:23 +10:00
|
|
|
if needs_temp_mask {
|
2018-05-18 11:28:30 +10:00
|
|
|
util::set_style_mask(*self.window, *self.view, curr_mask);
|
2018-04-18 04:07:54 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
is_zoomed != 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn restore_state_from_fullscreen(&mut self) {
|
|
|
|
let maximized = unsafe {
|
|
|
|
let mut win_attribs = self.win_attribs.borrow_mut();
|
|
|
|
win_attribs.fullscreen = None;
|
|
|
|
|
2018-06-08 03:29:23 +10:00
|
|
|
let mask = {
|
|
|
|
let base_mask = self.save_style_mask
|
|
|
|
.take()
|
|
|
|
.unwrap_or_else(|| self.window.styleMask());
|
|
|
|
if win_attribs.resizable {
|
|
|
|
base_mask | NSWindowStyleMask::NSResizableWindowMask
|
|
|
|
} else {
|
|
|
|
base_mask & !NSWindowStyleMask::NSResizableWindowMask
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
util::set_style_mask(*self.window, *self.view, mask);
|
2018-04-18 04:07:54 +10:00
|
|
|
|
|
|
|
win_attribs.maximized
|
|
|
|
};
|
|
|
|
|
|
|
|
self.perform_maximized(maximized);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn perform_maximized(&self, maximized: bool) {
|
|
|
|
let is_zoomed = self.is_zoomed();
|
|
|
|
|
|
|
|
if is_zoomed == maximized {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the standard frame sized if it is not zoomed
|
|
|
|
if !is_zoomed {
|
|
|
|
unsafe {
|
|
|
|
self.standard_frame.set(Some(NSWindow::frame(*self.window)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut win_attribs = self.win_attribs.borrow_mut();
|
|
|
|
win_attribs.maximized = maximized;
|
|
|
|
|
2018-06-08 03:29:23 +10:00
|
|
|
let curr_mask = unsafe { self.window.styleMask() };
|
2018-04-18 04:07:54 +10:00
|
|
|
if win_attribs.fullscreen.is_some() {
|
|
|
|
// Handle it in window_did_exit_fullscreen
|
|
|
|
return;
|
2018-06-08 03:29:23 +10:00
|
|
|
} else if curr_mask.contains(NSWindowStyleMask::NSResizableWindowMask) {
|
|
|
|
// Just use the native zoom if resizable
|
2018-04-18 04:07:54 +10:00
|
|
|
unsafe {
|
|
|
|
self.window.zoom_(nil);
|
|
|
|
}
|
|
|
|
} else {
|
2018-06-08 03:29:23 +10:00
|
|
|
// if it's not resizable, we set the frame directly
|
2018-04-18 04:07:54 +10:00
|
|
|
unsafe {
|
|
|
|
let new_rect = if maximized {
|
|
|
|
let screen = NSScreen::mainScreen(nil);
|
|
|
|
NSScreen::visibleFrame(screen)
|
|
|
|
} else {
|
|
|
|
self.standard_frame.get().unwrap_or(NSRect::new(
|
|
|
|
NSPoint::new(50.0, 50.0),
|
|
|
|
NSSize::new(800.0, 600.0),
|
|
|
|
))
|
|
|
|
};
|
|
|
|
|
|
|
|
self.window.setFrame_display_(new_rect, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct WindowDelegate {
|
|
|
|
state: Box<DelegateState>,
|
|
|
|
_this: IdRef,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WindowDelegate {
|
2018-06-15 09:42:18 +10:00
|
|
|
// Emits an event via the `EventsLoop`'s callback or stores it in the pending queue.
|
|
|
|
pub fn emit_event(state: &mut DelegateState, window_event: WindowEvent) {
|
|
|
|
let window_id = get_window_id(*state.window);
|
|
|
|
let event = Event::WindowEvent {
|
|
|
|
window_id: WindowId(window_id),
|
|
|
|
event: window_event,
|
|
|
|
};
|
|
|
|
if let Some(shared) = state.shared.upgrade() {
|
|
|
|
shared.call_user_callback_with_event_or_store_in_pending(event);
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
2018-06-15 09:42:18 +10:00
|
|
|
}
|
2017-02-03 23:05:57 +11:00
|
|
|
|
2018-06-15 09:42:18 +10:00
|
|
|
pub fn emit_resize_event(state: &mut DelegateState) {
|
|
|
|
let rect = unsafe { NSView::frame(*state.view) };
|
|
|
|
let size = LogicalSize::new(rect.size.width as f64, rect.size.height as f64);
|
|
|
|
WindowDelegate::emit_event(state, WindowEvent::Resized(size));
|
|
|
|
}
|
2017-03-19 20:30:54 +11:00
|
|
|
|
2018-06-15 09:42:18 +10:00
|
|
|
pub fn emit_move_event(state: &mut DelegateState) {
|
|
|
|
let rect = unsafe { NSWindow::frame(*state.window) };
|
|
|
|
let x = rect.origin.x as f64;
|
|
|
|
let y = util::bottom_left_to_top_left(rect);
|
|
|
|
let moved = state.previous_position != Some((x, y));
|
|
|
|
if moved {
|
|
|
|
state.previous_position = Some((x, y));
|
|
|
|
WindowDelegate::emit_event(state, WindowEvent::Moved((x, y).into()));
|
2018-04-28 10:46:20 +10:00
|
|
|
}
|
2018-06-15 09:42:18 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the delegate class, initiailizing it neccessary
|
|
|
|
fn class() -> *const Class {
|
|
|
|
use std::os::raw::c_void;
|
2018-04-28 10:46:20 +10:00
|
|
|
|
2017-02-03 23:05:57 +11:00
|
|
|
extern fn window_should_close(this: &Object, _: Sel, _: id) -> BOOL {
|
|
|
|
unsafe {
|
2017-02-05 13:28:56 +11:00
|
|
|
let state: *mut c_void = *this.get_ivar("winitState");
|
2017-02-03 23:05:57 +11:00
|
|
|
let state = &mut *(state as *mut DelegateState);
|
2018-06-15 09:42:18 +10:00
|
|
|
WindowDelegate::emit_event(state, WindowEvent::CloseRequested);
|
2018-04-25 06:20:40 +10:00
|
|
|
}
|
|
|
|
NO
|
|
|
|
}
|
|
|
|
|
|
|
|
extern fn window_will_close(this: &Object, _: Sel, _: id) {
|
|
|
|
unsafe {
|
|
|
|
let state: *mut c_void = *this.get_ivar("winitState");
|
|
|
|
let state = &mut *(state as *mut DelegateState);
|
|
|
|
|
2018-06-15 09:42:18 +10:00
|
|
|
WindowDelegate::emit_event(state, WindowEvent::Destroyed);
|
2017-03-19 19:20:38 +11:00
|
|
|
|
2017-06-09 22:13:30 +10:00
|
|
|
// Remove the window from the shared state.
|
|
|
|
if let Some(shared) = state.shared.upgrade() {
|
2017-03-19 19:20:38 +11:00
|
|
|
let window_id = get_window_id(*state.window);
|
2017-06-09 22:13:30 +10:00
|
|
|
shared.find_and_remove_window(window_id);
|
2017-03-19 19:20:38 +11:00
|
|
|
}
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern fn window_did_resize(this: &Object, _: Sel, _: id) {
|
|
|
|
unsafe {
|
2017-02-05 13:28:56 +11:00
|
|
|
let state: *mut c_void = *this.get_ivar("winitState");
|
2017-02-03 23:05:57 +11:00
|
|
|
let state = &mut *(state as *mut DelegateState);
|
2018-06-15 09:42:18 +10:00
|
|
|
WindowDelegate::emit_resize_event(state);
|
|
|
|
WindowDelegate::emit_move_event(state);
|
2018-04-28 10:46:20 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This won't be triggered if the move was part of a resize.
|
|
|
|
extern fn window_did_move(this: &Object, _: Sel, _: id) {
|
|
|
|
unsafe {
|
|
|
|
let state: *mut c_void = *this.get_ivar("winitState");
|
|
|
|
let state = &mut *(state as *mut DelegateState);
|
2018-06-15 09:42:18 +10:00
|
|
|
WindowDelegate::emit_move_event(state);
|
2017-03-19 20:30:54 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern fn window_did_change_screen(this: &Object, _: Sel, _: id) {
|
|
|
|
unsafe {
|
|
|
|
let state: *mut c_void = *this.get_ivar("winitState");
|
|
|
|
let state = &mut *(state as *mut DelegateState);
|
2018-06-15 09:42:18 +10:00
|
|
|
let dpi_factor = NSWindow::backingScaleFactor(*state.window) as f64;
|
|
|
|
if state.previous_dpi_factor != dpi_factor {
|
|
|
|
state.previous_dpi_factor = dpi_factor;
|
|
|
|
WindowDelegate::emit_event(state, WindowEvent::HiDpiFactorChanged(dpi_factor));
|
|
|
|
WindowDelegate::emit_resize_event(state);
|
|
|
|
}
|
2018-04-11 11:42:22 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-15 09:42:18 +10:00
|
|
|
// This will always be called before `window_did_change_screen`.
|
2018-04-11 11:42:22 +10:00
|
|
|
extern fn window_did_change_backing_properties(this: &Object, _:Sel, _:id) {
|
|
|
|
unsafe {
|
|
|
|
let state: *mut c_void = *this.get_ivar("winitState");
|
|
|
|
let state = &mut *(state as *mut DelegateState);
|
2018-06-15 09:42:18 +10:00
|
|
|
let dpi_factor = NSWindow::backingScaleFactor(*state.window) as f64;
|
|
|
|
if state.previous_dpi_factor != dpi_factor {
|
|
|
|
state.previous_dpi_factor = dpi_factor;
|
|
|
|
WindowDelegate::emit_event(state, WindowEvent::HiDpiFactorChanged(dpi_factor));
|
|
|
|
WindowDelegate::emit_resize_event(state);
|
|
|
|
}
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern fn window_did_become_key(this: &Object, _: Sel, _: id) {
|
|
|
|
unsafe {
|
|
|
|
// TODO: center the cursor if the window had mouse grab when it
|
|
|
|
// lost focus
|
2017-02-05 13:28:56 +11:00
|
|
|
let state: *mut c_void = *this.get_ivar("winitState");
|
2017-02-03 23:05:57 +11:00
|
|
|
let state = &mut *(state as *mut DelegateState);
|
2018-06-15 09:42:18 +10:00
|
|
|
WindowDelegate::emit_event(state, WindowEvent::Focused(true));
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern fn window_did_resign_key(this: &Object, _: Sel, _: id) {
|
|
|
|
unsafe {
|
2017-02-05 13:28:56 +11:00
|
|
|
let state: *mut c_void = *this.get_ivar("winitState");
|
2017-02-03 23:05:57 +11:00
|
|
|
let state = &mut *(state as *mut DelegateState);
|
2018-06-15 09:42:18 +10:00
|
|
|
WindowDelegate::emit_event(state, WindowEvent::Focused(false));
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-27 02:59:42 +10:00
|
|
|
/// Invoked when the dragged image enters destination bounds or frame
|
2017-07-27 12:51:00 +10:00
|
|
|
extern fn dragging_entered(this: &Object, _: Sel, sender: id) -> BOOL {
|
|
|
|
use cocoa::appkit::NSPasteboard;
|
|
|
|
use cocoa::foundation::NSFastEnumeration;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
let pb: id = unsafe { msg_send![sender, draggingPasteboard] };
|
|
|
|
let filenames = unsafe { NSPasteboard::propertyListForType(pb, appkit::NSFilenamesPboardType) };
|
|
|
|
|
|
|
|
for file in unsafe { filenames.iter() } {
|
|
|
|
use cocoa::foundation::NSString;
|
|
|
|
use std::ffi::CStr;
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let f = NSString::UTF8String(file);
|
|
|
|
let path = CStr::from_ptr(f).to_string_lossy().into_owned();
|
|
|
|
|
|
|
|
let state: *mut c_void = *this.get_ivar("winitState");
|
|
|
|
let state = &mut *(state as *mut DelegateState);
|
2018-06-15 09:42:18 +10:00
|
|
|
WindowDelegate::emit_event(state, WindowEvent::HoveredFile(PathBuf::from(path)));
|
2017-07-27 12:51:00 +10:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
YES
|
2017-07-27 02:59:42 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Invoked when the image is released
|
2017-07-27 12:56:34 +10:00
|
|
|
extern fn prepare_for_drag_operation(_: &Object, _: Sel, _: id) {}
|
2017-07-27 02:59:42 +10:00
|
|
|
|
|
|
|
/// Invoked after the released image has been removed from the screen
|
2017-07-27 12:51:00 +10:00
|
|
|
extern fn perform_drag_operation(this: &Object, _: Sel, sender: id) -> BOOL {
|
2017-07-27 02:59:42 +10:00
|
|
|
use cocoa::appkit::NSPasteboard;
|
2017-07-27 12:51:00 +10:00
|
|
|
use cocoa::foundation::NSFastEnumeration;
|
|
|
|
use std::path::PathBuf;
|
2017-07-27 02:59:42 +10:00
|
|
|
|
|
|
|
let pb: id = unsafe { msg_send![sender, draggingPasteboard] };
|
|
|
|
let filenames = unsafe { NSPasteboard::propertyListForType(pb, appkit::NSFilenamesPboardType) };
|
|
|
|
|
|
|
|
for file in unsafe { filenames.iter() } {
|
|
|
|
use cocoa::foundation::NSString;
|
2017-07-27 12:51:00 +10:00
|
|
|
use std::ffi::CStr;
|
|
|
|
|
2017-07-27 02:59:42 +10:00
|
|
|
unsafe {
|
|
|
|
let f = NSString::UTF8String(file);
|
2017-07-27 12:51:00 +10:00
|
|
|
let path = CStr::from_ptr(f).to_string_lossy().into_owned();
|
2017-07-27 02:59:42 +10:00
|
|
|
|
2017-07-27 12:51:00 +10:00
|
|
|
let state: *mut c_void = *this.get_ivar("winitState");
|
|
|
|
let state = &mut *(state as *mut DelegateState);
|
2018-06-15 09:42:18 +10:00
|
|
|
WindowDelegate::emit_event(state, WindowEvent::DroppedFile(PathBuf::from(path)));
|
2017-07-27 02:59:42 +10:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
YES
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Invoked when the dragging operation is complete
|
2017-07-27 12:56:34 +10:00
|
|
|
extern fn conclude_drag_operation(_: &Object, _: Sel, _: id) {}
|
2017-07-27 02:59:42 +10:00
|
|
|
|
|
|
|
/// Invoked when the dragging operation is cancelled
|
2017-07-27 12:56:34 +10:00
|
|
|
extern fn dragging_exited(this: &Object, _: Sel, _: id) {
|
|
|
|
unsafe {
|
|
|
|
let state: *mut c_void = *this.get_ivar("winitState");
|
|
|
|
let state = &mut *(state as *mut DelegateState);
|
2018-06-15 09:42:18 +10:00
|
|
|
WindowDelegate::emit_event(state, WindowEvent::HoveredFileCancelled);
|
2017-07-27 12:56:34 +10:00
|
|
|
}
|
2017-07-27 02:59:42 +10:00
|
|
|
}
|
|
|
|
|
2018-04-18 04:07:54 +10:00
|
|
|
/// Invoked when entered fullscreen
|
|
|
|
extern fn window_did_enter_fullscreen(this: &Object, _: Sel, _: id){
|
|
|
|
unsafe {
|
|
|
|
let state: *mut c_void = *this.get_ivar("winitState");
|
|
|
|
let state = &mut *(state as *mut DelegateState);
|
2018-05-16 23:41:45 +10:00
|
|
|
state.win_attribs.borrow_mut().fullscreen = Some(get_current_monitor(*state.window));
|
2018-04-25 06:20:40 +10:00
|
|
|
|
2018-04-18 04:07:54 +10:00
|
|
|
state.handle_with_fullscreen = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Invoked when before enter fullscreen
|
|
|
|
extern fn window_will_enter_fullscreen(this: &Object, _: Sel, _: id) {
|
|
|
|
unsafe {
|
|
|
|
let state: *mut c_void = *this.get_ivar("winitState");
|
|
|
|
let state = &mut *(state as *mut DelegateState);
|
|
|
|
let is_zoomed = state.is_zoomed();
|
|
|
|
|
|
|
|
state.win_attribs.borrow_mut().maximized = is_zoomed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Invoked when exited fullscreen
|
|
|
|
extern fn window_did_exit_fullscreen(this: &Object, _: Sel, _: id){
|
|
|
|
let state = unsafe {
|
|
|
|
let state: *mut c_void = *this.get_ivar("winitState");
|
|
|
|
&mut *(state as *mut DelegateState)
|
|
|
|
};
|
|
|
|
|
|
|
|
state.restore_state_from_fullscreen();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Invoked when fail to enter fullscreen
|
|
|
|
///
|
|
|
|
/// When this window launch from a fullscreen app (e.g. launch from VS Code
|
2018-04-25 06:20:40 +10:00
|
|
|
/// terminal), it creates a new virtual destkop and a transition animation.
|
2018-04-18 04:07:54 +10:00
|
|
|
/// This animation takes one second and cannot be disable without
|
|
|
|
/// elevated privileges. In this animation time, all toggleFullscreen events
|
|
|
|
/// will be failed. In this implementation, we will try again by using
|
|
|
|
/// performSelector:withObject:afterDelay: until window_did_enter_fullscreen.
|
|
|
|
/// It should be fine as we only do this at initialzation (i.e with_fullscreen
|
|
|
|
/// was set).
|
|
|
|
///
|
|
|
|
/// From Apple doc:
|
|
|
|
/// In some cases, the transition to enter full-screen mode can fail,
|
|
|
|
/// due to being in the midst of handling some other animation or user gesture.
|
|
|
|
/// This method indicates that there was an error, and you should clean up any
|
|
|
|
/// work you may have done to prepare to enter full-screen mode.
|
|
|
|
extern fn window_did_fail_to_enter_fullscreen(this: &Object, _: Sel, _: id) {
|
|
|
|
unsafe {
|
|
|
|
let state: *mut c_void = *this.get_ivar("winitState");
|
|
|
|
let state = &mut *(state as *mut DelegateState);
|
|
|
|
|
|
|
|
if state.handle_with_fullscreen {
|
2018-04-25 06:20:40 +10:00
|
|
|
let _: () = msg_send![*state.window,
|
2018-04-18 04:07:54 +10:00
|
|
|
performSelector:sel!(toggleFullScreen:)
|
|
|
|
withObject:nil
|
2018-04-25 06:20:40 +10:00
|
|
|
afterDelay: 0.5
|
2018-04-18 04:07:54 +10:00
|
|
|
];
|
|
|
|
} else {
|
|
|
|
state.restore_state_from_fullscreen();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-03 23:05:57 +11:00
|
|
|
static mut DELEGATE_CLASS: *const Class = 0 as *const Class;
|
|
|
|
static INIT: std::sync::Once = std::sync::ONCE_INIT;
|
|
|
|
|
|
|
|
INIT.call_once(|| unsafe {
|
|
|
|
// Create new NSWindowDelegate
|
2018-07-20 02:02:33 +10:00
|
|
|
let superclass = class!(NSObject);
|
2017-02-05 13:28:56 +11:00
|
|
|
let mut decl = ClassDecl::new("WinitWindowDelegate", superclass).unwrap();
|
2017-02-03 23:05:57 +11:00
|
|
|
|
|
|
|
// Add callback methods
|
|
|
|
decl.add_method(sel!(windowShouldClose:),
|
|
|
|
window_should_close as extern fn(&Object, Sel, id) -> BOOL);
|
2018-04-25 06:20:40 +10:00
|
|
|
decl.add_method(sel!(windowWillClose:),
|
|
|
|
window_will_close as extern fn(&Object, Sel, id));
|
2017-02-03 23:05:57 +11:00
|
|
|
decl.add_method(sel!(windowDidResize:),
|
|
|
|
window_did_resize as extern fn(&Object, Sel, id));
|
2018-04-28 10:46:20 +10:00
|
|
|
decl.add_method(sel!(windowDidMove:),
|
|
|
|
window_did_move as extern fn(&Object, Sel, id));
|
2017-03-19 20:30:54 +11:00
|
|
|
decl.add_method(sel!(windowDidChangeScreen:),
|
|
|
|
window_did_change_screen as extern fn(&Object, Sel, id));
|
2018-04-11 11:42:22 +10:00
|
|
|
decl.add_method(sel!(windowDidChangeBackingProperties:),
|
|
|
|
window_did_change_backing_properties as extern fn(&Object, Sel, id));
|
2017-02-03 23:05:57 +11:00
|
|
|
decl.add_method(sel!(windowDidBecomeKey:),
|
|
|
|
window_did_become_key as extern fn(&Object, Sel, id));
|
|
|
|
decl.add_method(sel!(windowDidResignKey:),
|
|
|
|
window_did_resign_key as extern fn(&Object, Sel, id));
|
|
|
|
|
2017-07-27 12:56:34 +10:00
|
|
|
// callbacks for drag and drop events
|
2017-07-27 02:59:42 +10:00
|
|
|
decl.add_method(sel!(draggingEntered:),
|
2017-07-27 12:51:00 +10:00
|
|
|
dragging_entered as extern fn(&Object, Sel, id) -> BOOL);
|
2018-05-13 12:10:57 +10:00
|
|
|
decl.add_method(sel!(prepareForDragOperation:),
|
2017-07-27 02:59:42 +10:00
|
|
|
prepare_for_drag_operation as extern fn(&Object, Sel, id));
|
2018-05-13 12:10:57 +10:00
|
|
|
decl.add_method(sel!(performDragOperation:),
|
2017-07-27 02:59:42 +10:00
|
|
|
perform_drag_operation as extern fn(&Object, Sel, id) -> BOOL);
|
2018-05-13 12:10:57 +10:00
|
|
|
decl.add_method(sel!(concludeDragOperation:),
|
2017-07-27 02:59:42 +10:00
|
|
|
conclude_drag_operation as extern fn(&Object, Sel, id));
|
2018-05-13 12:10:57 +10:00
|
|
|
decl.add_method(sel!(draggingExited:),
|
2017-07-27 02:59:42 +10:00
|
|
|
dragging_exited as extern fn(&Object, Sel, id));
|
|
|
|
|
2018-04-18 04:07:54 +10:00
|
|
|
// callbacks for fullscreen events
|
|
|
|
decl.add_method(sel!(windowDidEnterFullScreen:),
|
|
|
|
window_did_enter_fullscreen as extern fn(&Object, Sel, id));
|
|
|
|
decl.add_method(sel!(windowWillEnterFullScreen:),
|
|
|
|
window_will_enter_fullscreen as extern fn(&Object, Sel, id));
|
|
|
|
decl.add_method(sel!(windowDidExitFullScreen:),
|
|
|
|
window_did_exit_fullscreen as extern fn(&Object, Sel, id));
|
|
|
|
decl.add_method(sel!(windowDidFailToEnterFullScreen:),
|
|
|
|
window_did_fail_to_enter_fullscreen as extern fn(&Object, Sel, id));
|
2018-04-25 06:20:40 +10:00
|
|
|
|
2017-02-03 23:05:57 +11:00
|
|
|
// Store internal state as user data
|
2017-02-05 13:28:56 +11:00
|
|
|
decl.add_ivar::<*mut c_void>("winitState");
|
2017-02-03 23:05:57 +11:00
|
|
|
|
|
|
|
DELEGATE_CLASS = decl.register();
|
|
|
|
});
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
DELEGATE_CLASS
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn new(state: DelegateState) -> WindowDelegate {
|
|
|
|
// Box the state so we can give a pointer to it
|
|
|
|
let mut state = Box::new(state);
|
|
|
|
let state_ptr: *mut DelegateState = &mut *state;
|
|
|
|
unsafe {
|
|
|
|
let delegate = IdRef::new(msg_send![WindowDelegate::class(), new]);
|
|
|
|
|
2018-04-29 02:10:06 +10:00
|
|
|
// setDelegate uses autorelease on objects,
|
|
|
|
// so need autorelease
|
|
|
|
let autoreleasepool = NSAutoreleasePool::new(nil);
|
|
|
|
|
2017-02-05 13:28:56 +11:00
|
|
|
(&mut **delegate).set_ivar("winitState", state_ptr as *mut ::std::os::raw::c_void);
|
2017-02-03 23:05:57 +11:00
|
|
|
let _: () = msg_send![*state.window, setDelegate:*delegate];
|
|
|
|
|
2018-04-29 02:10:06 +10:00
|
|
|
let _: () = msg_send![autoreleasepool, drain];
|
|
|
|
|
2017-02-03 23:05:57 +11:00
|
|
|
WindowDelegate { state: state, _this: delegate }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for WindowDelegate {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe {
|
|
|
|
// Nil the window's delegate so it doesn't still reference us
|
2018-04-29 02:10:06 +10:00
|
|
|
// NOTE: setDelegate:nil at first retains the previous value,
|
|
|
|
// and then autoreleases it, so autorelease pool is needed
|
|
|
|
let autoreleasepool = NSAutoreleasePool::new(nil);
|
2017-02-03 23:05:57 +11:00
|
|
|
let _: () = msg_send![*self.state.window, setDelegate:nil];
|
2018-04-29 02:10:06 +10:00
|
|
|
let _: () = msg_send![autoreleasepool, drain];
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Default)]
|
|
|
|
pub struct PlatformSpecificWindowBuilderAttributes {
|
|
|
|
pub activation_policy: ActivationPolicy,
|
2018-01-23 05:07:51 +11:00
|
|
|
pub movable_by_window_background: bool,
|
2018-03-23 03:57:35 +11:00
|
|
|
pub titlebar_transparent: bool,
|
|
|
|
pub title_hidden: bool,
|
|
|
|
pub titlebar_hidden: bool,
|
|
|
|
pub titlebar_buttons_hidden: bool,
|
|
|
|
pub fullsize_content_view: bool,
|
2018-06-15 09:42:18 +10:00
|
|
|
pub resize_increments: Option<LogicalSize>,
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
2017-09-07 01:32:24 +10:00
|
|
|
pub struct Window2 {
|
2017-02-03 23:05:57 +11:00
|
|
|
pub view: IdRef,
|
|
|
|
pub window: IdRef,
|
|
|
|
pub delegate: WindowDelegate,
|
2018-05-18 11:28:30 +10:00
|
|
|
pub input_context: IdRef,
|
2018-07-27 07:14:16 +10:00
|
|
|
cursor_hidden: AtomicBool,
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
2017-09-07 01:32:24 +10:00
|
|
|
unsafe impl Send for Window2 {}
|
|
|
|
unsafe impl Sync for Window2 {}
|
2017-02-03 23:05:57 +11:00
|
|
|
|
2018-05-16 23:41:45 +10:00
|
|
|
unsafe fn get_current_monitor(window: id) -> RootMonitorId {
|
|
|
|
let screen: id = msg_send![window, screen];
|
2018-04-18 04:07:54 +10:00
|
|
|
let desc = NSScreen::deviceDescription(screen);
|
|
|
|
let key = IdRef::new(NSString::alloc(nil).init_str("NSScreenNumber"));
|
|
|
|
let value = NSDictionary::valueForKey_(desc, *key);
|
|
|
|
let display_id = msg_send![value, unsignedIntegerValue];
|
2018-05-16 23:41:45 +10:00
|
|
|
RootMonitorId { inner: EventsLoop::make_monitor_from_display(display_id) }
|
2018-04-18 04:07:54 +10:00
|
|
|
}
|
|
|
|
|
2017-09-07 01:32:24 +10:00
|
|
|
impl Drop for Window2 {
|
2017-02-03 23:05:57 +11:00
|
|
|
fn drop(&mut self) {
|
2018-04-29 02:10:06 +10:00
|
|
|
// Remove this window from the `EventLoop`s list of windows.
|
|
|
|
// The destructor order is:
|
|
|
|
// Window ->
|
|
|
|
// Rc<Window2> (makes Weak<..> in shared.windows None) ->
|
|
|
|
// Window2
|
|
|
|
// needed to remove the element from array
|
|
|
|
let id = self.id();
|
|
|
|
if let Some(shared) = self.delegate.state.shared.upgrade() {
|
|
|
|
shared.find_and_remove_window(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
// nswindow::close uses autorelease
|
|
|
|
// so autorelease pool
|
|
|
|
let autoreleasepool = unsafe {
|
|
|
|
NSAutoreleasePool::new(nil)
|
|
|
|
};
|
|
|
|
|
2017-03-19 19:32:24 +11:00
|
|
|
// Close the window if it has not yet been closed.
|
|
|
|
let nswindow = *self.window;
|
|
|
|
if nswindow != nil {
|
|
|
|
unsafe {
|
2018-03-22 06:36:06 +11:00
|
|
|
let () = msg_send![nswindow, close];
|
2017-03-19 19:32:24 +11:00
|
|
|
}
|
|
|
|
}
|
2018-04-29 02:10:06 +10:00
|
|
|
|
|
|
|
let _: () = unsafe { msg_send![autoreleasepool, drain] };
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-07 01:32:24 +10:00
|
|
|
impl WindowExt for Window2 {
|
2017-02-03 23:05:57 +11:00
|
|
|
#[inline]
|
|
|
|
fn get_nswindow(&self) -> *mut c_void {
|
|
|
|
*self.window as *mut c_void
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn get_nsview(&self) -> *mut c_void {
|
|
|
|
*self.view as *mut c_void
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-07 01:32:24 +10:00
|
|
|
impl Window2 {
|
2018-04-18 04:07:54 +10:00
|
|
|
pub fn new(
|
|
|
|
shared: Weak<Shared>,
|
2018-06-08 03:29:23 +10:00
|
|
|
mut win_attribs: WindowAttributes,
|
2018-05-08 07:36:21 +10:00
|
|
|
pl_attribs: PlatformSpecificWindowBuilderAttributes,
|
2018-04-18 04:07:54 +10:00
|
|
|
) -> Result<Window2, CreationError> {
|
2017-02-05 19:53:13 +11:00
|
|
|
unsafe {
|
2018-07-20 02:02:33 +10:00
|
|
|
if !msg_send![class!(NSThread), isMainThread] {
|
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
|
|
|
}
|
2018-06-08 03:29:23 +10:00
|
|
|
|
|
|
|
// Might as well save some RAM...
|
|
|
|
win_attribs.window_icon.take();
|
|
|
|
|
2018-04-29 02:10:06 +10:00
|
|
|
let autoreleasepool = unsafe {
|
|
|
|
NSAutoreleasePool::new(nil)
|
|
|
|
};
|
2017-02-05 19:47:39 +11:00
|
|
|
|
2017-09-07 01:32:24 +10:00
|
|
|
let app = match Window2::create_app(pl_attribs.activation_policy) {
|
2017-02-03 23:05:57 +11:00
|
|
|
Some(app) => app,
|
2018-05-16 22:51:56 +10:00
|
|
|
None => {
|
2018-04-29 02:10:06 +10:00
|
|
|
let _: () = unsafe { msg_send![autoreleasepool, drain] };
|
|
|
|
return Err(OsError(format!("Couldn't create NSApplication")));
|
|
|
|
},
|
2017-02-03 23:05:57 +11:00
|
|
|
};
|
|
|
|
|
2018-05-16 22:51:56 +10:00
|
|
|
let window = match Window2::create_window(&win_attribs, &pl_attribs) {
|
|
|
|
Some(res) => res,
|
|
|
|
None => {
|
2018-04-29 02:10:06 +10:00
|
|
|
let _: () = unsafe { msg_send![autoreleasepool, drain] };
|
|
|
|
return Err(OsError(format!("Couldn't create NSWindow")));
|
|
|
|
},
|
2017-02-03 23:05:57 +11:00
|
|
|
};
|
2018-05-18 11:28:30 +10:00
|
|
|
let view = match Window2::create_view(*window, Weak::clone(&shared)) {
|
2017-02-03 23:05:57 +11:00
|
|
|
Some(view) => view,
|
2018-05-16 22:51:56 +10:00
|
|
|
None => {
|
2018-04-29 02:10:06 +10:00
|
|
|
let _: () = unsafe { msg_send![autoreleasepool, drain] };
|
|
|
|
return Err(OsError(format!("Couldn't create NSView")));
|
|
|
|
},
|
2017-02-03 23:05:57 +11:00
|
|
|
};
|
|
|
|
|
2018-05-18 11:28:30 +10:00
|
|
|
let input_context = unsafe { util::create_input_context(*view) };
|
|
|
|
|
2017-02-03 23:05:57 +11:00
|
|
|
unsafe {
|
|
|
|
if win_attribs.transparent {
|
|
|
|
(*window as id).setOpaque_(NO);
|
|
|
|
(*window as id).setBackgroundColor_(NSColor::clearColor(nil));
|
|
|
|
}
|
|
|
|
|
|
|
|
app.activateIgnoringOtherApps_(YES);
|
|
|
|
|
2018-06-15 09:42:18 +10:00
|
|
|
if let Some(dimensions) = win_attribs.min_dimensions {
|
|
|
|
nswindow_set_min_dimensions(window.0, dimensions);
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
2018-06-15 09:42:18 +10:00
|
|
|
if let Some(dimensions) = win_attribs.max_dimensions {
|
|
|
|
nswindow_set_max_dimensions(window.0, dimensions);
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
2017-07-27 02:59:42 +10:00
|
|
|
|
|
|
|
use cocoa::foundation::NSArray;
|
|
|
|
// register for drag and drop operations.
|
2018-03-22 06:36:06 +11:00
|
|
|
let () = msg_send![(*window as id),
|
2017-07-27 02:59:42 +10:00
|
|
|
registerForDraggedTypes:NSArray::arrayWithObject(nil, appkit::NSFilenamesPboardType)];
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
2018-06-15 09:42:18 +10:00
|
|
|
let dpi_factor = unsafe { NSWindow::backingScaleFactor(*window) as f64 };
|
|
|
|
|
|
|
|
let mut delegate_state = DelegateState {
|
2017-02-03 23:05:57 +11:00
|
|
|
view: view.clone(),
|
|
|
|
window: window.clone(),
|
2018-04-28 10:46:20 +10:00
|
|
|
shared,
|
2018-04-18 04:07:54 +10:00
|
|
|
win_attribs: RefCell::new(win_attribs.clone()),
|
|
|
|
standard_frame: Cell::new(None),
|
|
|
|
save_style_mask: Cell::new(None),
|
|
|
|
handle_with_fullscreen: win_attribs.fullscreen.is_some(),
|
2018-04-28 10:46:20 +10:00
|
|
|
previous_position: None,
|
2018-06-15 09:42:18 +10:00
|
|
|
previous_dpi_factor: dpi_factor,
|
2017-02-03 23:05:57 +11:00
|
|
|
};
|
2018-06-15 09:42:18 +10:00
|
|
|
delegate_state.win_attribs.borrow_mut().fullscreen = None;
|
|
|
|
|
|
|
|
if dpi_factor != 1.0 {
|
|
|
|
WindowDelegate::emit_event(&mut delegate_state, WindowEvent::HiDpiFactorChanged(dpi_factor));
|
|
|
|
WindowDelegate::emit_resize_event(&mut delegate_state);
|
|
|
|
}
|
2017-02-03 23:05:57 +11:00
|
|
|
|
2017-09-07 01:32:24 +10:00
|
|
|
let window = Window2 {
|
2017-02-03 23:05:57 +11:00
|
|
|
view: view,
|
|
|
|
window: window,
|
2018-06-15 09:42:18 +10:00
|
|
|
delegate: WindowDelegate::new(delegate_state),
|
2018-05-18 11:28:30 +10:00
|
|
|
input_context,
|
2018-06-19 02:32:18 +10:00
|
|
|
cursor_hidden: Default::default(),
|
2017-02-03 23:05:57 +11:00
|
|
|
};
|
|
|
|
|
2018-04-18 04:07:54 +10:00
|
|
|
// Set fullscreen mode after we setup everything
|
|
|
|
if let Some(ref monitor) = win_attribs.fullscreen {
|
|
|
|
unsafe {
|
2018-05-16 23:41:45 +10:00
|
|
|
if monitor.inner != get_current_monitor(*window.window).inner {
|
2018-04-18 04:07:54 +10:00
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
window.set_fullscreen(Some(monitor.clone()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make key have to be after set fullscreen
|
|
|
|
// to prevent normal size window brefly appears
|
|
|
|
unsafe {
|
|
|
|
if win_attribs.visible {
|
|
|
|
window.window.makeKeyAndOrderFront_(nil);
|
|
|
|
} else {
|
|
|
|
window.window.makeKeyWindow();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if win_attribs.maximized {
|
|
|
|
window.delegate.state.perform_maximized(win_attribs.maximized);
|
|
|
|
}
|
|
|
|
|
2018-04-29 02:10:06 +10:00
|
|
|
let _: () = unsafe { msg_send![autoreleasepool, drain] };
|
|
|
|
|
2017-02-03 23:05:57 +11:00
|
|
|
Ok(window)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn id(&self) -> Id {
|
|
|
|
get_window_id(*self.window)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_app(activation_policy: ActivationPolicy) -> Option<id> {
|
|
|
|
unsafe {
|
|
|
|
let app = appkit::NSApp();
|
|
|
|
if app == nil {
|
|
|
|
None
|
|
|
|
} else {
|
2018-11-06 10:54:22 +11:00
|
|
|
let ns_activation_policy = match activation_policy {
|
|
|
|
ActivationPolicy::Regular =>
|
|
|
|
NSApplicationActivationPolicy::NSApplicationActivationPolicyRegular,
|
|
|
|
ActivationPolicy::Accessory =>
|
|
|
|
NSApplicationActivationPolicy::NSApplicationActivationPolicyAccessory,
|
|
|
|
ActivationPolicy::Prohibited =>
|
|
|
|
NSApplicationActivationPolicy::NSApplicationActivationPolicyProhibited,
|
|
|
|
};
|
|
|
|
app.setActivationPolicy_(ns_activation_policy);
|
2017-02-03 23:05:57 +11:00
|
|
|
app.finishLaunching();
|
|
|
|
Some(app)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-29 02:10:06 +10:00
|
|
|
fn class() -> *const Class {
|
|
|
|
static mut WINDOW2_CLASS: *const Class = 0 as *const Class;
|
|
|
|
static INIT: std::sync::Once = std::sync::ONCE_INIT;
|
|
|
|
|
|
|
|
INIT.call_once(|| unsafe {
|
2018-07-20 02:02:33 +10:00
|
|
|
let window_superclass = class!(NSWindow);
|
2018-04-29 02:10:06 +10:00
|
|
|
let mut decl = ClassDecl::new("WinitWindow", window_superclass).unwrap();
|
|
|
|
decl.add_method(sel!(canBecomeMainWindow), yes as extern fn(&Object, Sel) -> BOOL);
|
|
|
|
decl.add_method(sel!(canBecomeKeyWindow), yes as extern fn(&Object, Sel) -> BOOL);
|
|
|
|
WINDOW2_CLASS = decl.register();
|
|
|
|
});
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
WINDOW2_CLASS
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-23 05:07:51 +11:00
|
|
|
fn create_window(
|
|
|
|
attrs: &WindowAttributes,
|
2018-05-08 07:36:21 +10:00
|
|
|
pl_attrs: &PlatformSpecificWindowBuilderAttributes
|
|
|
|
) -> Option<IdRef> {
|
2017-02-03 23:05:57 +11:00
|
|
|
unsafe {
|
2018-04-29 02:10:06 +10:00
|
|
|
let autoreleasepool = NSAutoreleasePool::new(nil);
|
2017-08-29 11:08:12 +10:00
|
|
|
let screen = match attrs.fullscreen {
|
2017-09-07 18:33:46 +10:00
|
|
|
Some(ref monitor_id) => {
|
2018-03-06 19:35:04 +11:00
|
|
|
let monitor_screen = monitor_id.inner.get_nsscreen();
|
|
|
|
Some(monitor_screen.unwrap_or(appkit::NSScreen::mainScreen(nil)))
|
2017-02-03 23:05:57 +11:00
|
|
|
},
|
2017-08-29 11:08:12 +10:00
|
|
|
_ => None,
|
2017-02-03 23:05:57 +11:00
|
|
|
};
|
|
|
|
let frame = match screen {
|
|
|
|
Some(screen) => appkit::NSScreen::frame(screen),
|
|
|
|
None => {
|
2018-06-15 09:42:18 +10:00
|
|
|
let (width, height) = attrs.dimensions
|
|
|
|
.map(|logical| (logical.width, logical.height))
|
|
|
|
.unwrap_or((800.0, 600.0));
|
|
|
|
NSRect::new(NSPoint::new(0.0, 0.0), NSSize::new(width, height))
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-04-30 08:51:57 +10:00
|
|
|
let mut masks = if !attrs.decorations && !screen.is_some() {
|
2018-06-08 03:29:23 +10:00
|
|
|
// Resizable Window2 without a titlebar or borders
|
2018-04-30 08:51:57 +10:00
|
|
|
// if decorations is set to false, ignore pl_attrs
|
|
|
|
NSWindowStyleMask::NSBorderlessWindowMask
|
2018-06-08 03:29:23 +10:00
|
|
|
| NSWindowStyleMask::NSResizableWindowMask
|
2018-04-30 08:51:57 +10:00
|
|
|
} else if pl_attrs.titlebar_hidden {
|
|
|
|
// if the titlebar is hidden, ignore other pl_attrs
|
2018-03-23 03:57:35 +11:00
|
|
|
NSWindowStyleMask::NSBorderlessWindowMask |
|
|
|
|
NSWindowStyleMask::NSResizableWindowMask
|
|
|
|
} else {
|
2018-04-30 08:51:57 +10:00
|
|
|
// default case, resizable window with titlebar and titlebar buttons
|
2018-04-28 14:12:50 +10:00
|
|
|
NSWindowStyleMask::NSClosableWindowMask |
|
|
|
|
NSWindowStyleMask::NSMiniaturizableWindowMask |
|
|
|
|
NSWindowStyleMask::NSResizableWindowMask |
|
|
|
|
NSWindowStyleMask::NSTitledWindowMask
|
2017-02-03 23:05:57 +11:00
|
|
|
};
|
|
|
|
|
2018-06-08 03:29:23 +10:00
|
|
|
if !attrs.resizable {
|
|
|
|
masks &= !NSWindowStyleMask::NSResizableWindowMask;
|
|
|
|
}
|
|
|
|
|
2018-04-30 08:51:57 +10:00
|
|
|
if pl_attrs.fullsize_content_view {
|
2018-06-08 03:29:23 +10:00
|
|
|
masks |= NSWindowStyleMask::NSFullSizeContentViewWindowMask;
|
2018-04-30 08:51:57 +10:00
|
|
|
}
|
|
|
|
|
2018-04-29 02:10:06 +10:00
|
|
|
let winit_window = Window2::class();
|
2018-04-03 09:12:38 +10:00
|
|
|
|
|
|
|
let window: id = msg_send![winit_window, alloc];
|
|
|
|
|
|
|
|
let window = IdRef::new(window.initWithContentRect_styleMask_backing_defer_(
|
2017-02-03 23:05:57 +11:00
|
|
|
frame,
|
|
|
|
masks,
|
|
|
|
appkit::NSBackingStoreBuffered,
|
|
|
|
NO,
|
|
|
|
));
|
2018-04-29 02:10:06 +10:00
|
|
|
let res = window.non_nil().map(|window| {
|
2017-02-03 23:05:57 +11:00
|
|
|
let title = IdRef::new(NSString::alloc(nil).init_str(&attrs.title));
|
|
|
|
window.setReleasedWhenClosed_(NO);
|
|
|
|
window.setTitle_(*title);
|
|
|
|
window.setAcceptsMouseMovedEvents_(YES);
|
|
|
|
|
2018-03-23 03:57:35 +11:00
|
|
|
if pl_attrs.titlebar_transparent {
|
2017-02-03 23:05:57 +11:00
|
|
|
window.setTitlebarAppearsTransparent_(YES);
|
|
|
|
}
|
2018-03-23 03:57:35 +11:00
|
|
|
if pl_attrs.title_hidden {
|
|
|
|
window.setTitleVisibility_(appkit::NSWindowTitleVisibility::NSWindowTitleHidden);
|
|
|
|
}
|
|
|
|
if pl_attrs.titlebar_buttons_hidden {
|
|
|
|
let button = window.standardWindowButton_(NSWindowButton::NSWindowFullScreenButton);
|
2018-04-10 05:58:47 +10:00
|
|
|
let () = msg_send![button, setHidden:YES];
|
2018-03-23 03:57:35 +11:00
|
|
|
let button = window.standardWindowButton_(NSWindowButton::NSWindowMiniaturizeButton);
|
2018-04-10 05:58:47 +10:00
|
|
|
let () = msg_send![button, setHidden:YES];
|
2018-03-23 03:57:35 +11:00
|
|
|
let button = window.standardWindowButton_(NSWindowButton::NSWindowCloseButton);
|
2018-04-10 05:58:47 +10:00
|
|
|
let () = msg_send![button, setHidden:YES];
|
2018-03-23 03:57:35 +11:00
|
|
|
let button = window.standardWindowButton_(NSWindowButton::NSWindowZoomButton);
|
2018-04-10 05:58:47 +10:00
|
|
|
let () = msg_send![button, setHidden:YES];
|
2018-03-23 03:57:35 +11:00
|
|
|
}
|
2018-01-23 05:07:51 +11:00
|
|
|
if pl_attrs.movable_by_window_background {
|
|
|
|
window.setMovableByWindowBackground_(YES);
|
|
|
|
}
|
2018-05-21 00:24:05 +10:00
|
|
|
|
|
|
|
if attrs.always_on_top {
|
|
|
|
let _: () = msg_send![*window, setLevel:ffi::NSWindowLevel::NSFloatingWindowLevel];
|
|
|
|
}
|
2018-06-08 03:29:23 +10:00
|
|
|
|
2018-06-15 09:42:18 +10: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);
|
2018-05-17 00:16:36 +10:00
|
|
|
window.setResizeIncrements_(size);
|
|
|
|
}
|
|
|
|
}
|
2018-01-23 05:07:51 +11:00
|
|
|
|
2018-04-18 04:07:54 +10:00
|
|
|
window.center();
|
2017-02-03 23:05:57 +11:00
|
|
|
window
|
2018-04-29 02:10:06 +10:00
|
|
|
});
|
|
|
|
let _: () = msg_send![autoreleasepool, drain];
|
|
|
|
res
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-18 11:28:30 +10:00
|
|
|
fn create_view(window: id, shared: Weak<Shared>) -> Option<IdRef> {
|
2017-02-03 23:05:57 +11:00
|
|
|
unsafe {
|
2018-05-18 11:28:30 +10:00
|
|
|
let view = new_view(window, shared);
|
2017-02-03 23:05:57 +11:00
|
|
|
view.non_nil().map(|view| {
|
|
|
|
view.setWantsBestResolutionOpenGLSurface_(YES);
|
2018-11-06 06:34:54 +11: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 {
|
|
|
|
view.setWantsLayer(YES);
|
|
|
|
}
|
|
|
|
|
2017-02-03 23:05:57 +11:00
|
|
|
window.setContentView_(*view);
|
2018-05-18 11:28:30 +10:00
|
|
|
window.makeFirstResponder_(*view);
|
2017-02-03 23:05:57 +11:00
|
|
|
view
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_title(&self, title: &str) {
|
|
|
|
unsafe {
|
|
|
|
let title = IdRef::new(NSString::alloc(nil).init_str(title));
|
|
|
|
self.window.setTitle_(*title);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn show(&self) {
|
|
|
|
unsafe { NSWindow::makeKeyAndOrderFront_(*self.window, nil); }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn hide(&self) {
|
|
|
|
unsafe { NSWindow::orderOut_(*self.window, nil); }
|
|
|
|
}
|
|
|
|
|
2018-06-15 09:42:18 +10:00
|
|
|
pub fn get_position(&self) -> Option<LogicalPosition> {
|
2018-04-17 11:40:30 +10:00
|
|
|
let frame_rect = unsafe { NSWindow::frame(*self.window) };
|
|
|
|
Some((
|
2018-06-15 09:42:18 +10:00
|
|
|
frame_rect.origin.x as f64,
|
2018-05-18 11:28:30 +10:00
|
|
|
util::bottom_left_to_top_left(frame_rect),
|
2018-06-15 09:42:18 +10:00
|
|
|
).into())
|
2018-04-17 11:40:30 +10:00
|
|
|
}
|
2017-02-03 23:05:57 +11:00
|
|
|
|
2018-06-15 09:42:18 +10:00
|
|
|
pub fn get_inner_position(&self) -> Option<LogicalPosition> {
|
2018-04-17 11:40:30 +10:00
|
|
|
let content_rect = unsafe {
|
|
|
|
NSWindow::contentRectForFrameRect_(
|
|
|
|
*self.window,
|
|
|
|
NSWindow::frame(*self.window),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
Some((
|
2018-06-15 09:42:18 +10:00
|
|
|
content_rect.origin.x as f64,
|
2018-05-18 11:28:30 +10:00
|
|
|
util::bottom_left_to_top_left(content_rect),
|
2018-06-15 09:42:18 +10:00
|
|
|
).into())
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
2018-06-15 09:42:18 +10:00
|
|
|
pub fn set_position(&self, position: LogicalPosition) {
|
2018-04-17 11:40:30 +10:00
|
|
|
let dummy = NSRect::new(
|
|
|
|
NSPoint::new(
|
2018-06-15 09:42:18 +10:00
|
|
|
position.x,
|
2018-04-17 11:40:30 +10:00
|
|
|
// While it's true that we're setting the top-left position, it still needs to be
|
|
|
|
// in a bottom-left coordinate system.
|
2018-06-15 09:42:18 +10:00
|
|
|
CGDisplay::main().pixels_high() as f64 - position.y,
|
2018-04-17 11:40:30 +10:00
|
|
|
),
|
|
|
|
NSSize::new(0f64, 0f64),
|
|
|
|
);
|
2017-02-03 23:05:57 +11:00
|
|
|
unsafe {
|
2018-04-17 11:40:30 +10:00
|
|
|
NSWindow::setFrameTopLeftPoint_(*self.window, dummy.origin);
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-06-15 09:42:18 +10:00
|
|
|
pub fn get_inner_size(&self) -> Option<LogicalSize> {
|
|
|
|
let view_frame = unsafe { NSView::frame(*self.view) };
|
|
|
|
Some((view_frame.size.width as f64, view_frame.size.height as f64).into())
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-06-15 09:42:18 +10:00
|
|
|
pub fn get_outer_size(&self) -> Option<LogicalSize> {
|
|
|
|
let view_frame = unsafe { NSWindow::frame(*self.window) };
|
|
|
|
Some((view_frame.size.width as f64, view_frame.size.height as f64).into())
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-06-15 09:42:18 +10:00
|
|
|
pub fn set_inner_size(&self, size: LogicalSize) {
|
2017-02-03 23:05:57 +11:00
|
|
|
unsafe {
|
2018-06-15 09:42:18 +10:00
|
|
|
NSWindow::setContentSize_(*self.window, NSSize::new(size.width as CGFloat, size.height as CGFloat));
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-15 09:42:18 +10:00
|
|
|
pub fn set_min_dimensions(&self, dimensions: Option<LogicalSize>) {
|
2018-03-23 20:35:35 +11:00
|
|
|
unsafe {
|
2018-06-15 09:42:18 +10:00
|
|
|
let dimensions = dimensions.unwrap_or_else(|| (0, 0).into());
|
|
|
|
nswindow_set_min_dimensions(self.window.0, dimensions);
|
2018-03-23 20:35:35 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-15 09:42:18 +10:00
|
|
|
pub fn set_max_dimensions(&self, dimensions: Option<LogicalSize>) {
|
2018-03-23 20:35:35 +11:00
|
|
|
unsafe {
|
2018-06-15 09:42:18 +10:00
|
|
|
let dimensions = dimensions.unwrap_or_else(|| (!0, !0).into());
|
|
|
|
nswindow_set_max_dimensions(self.window.0, dimensions);
|
2018-03-23 20:35:35 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-08 03:29:23 +10:00
|
|
|
#[inline]
|
|
|
|
pub fn set_resizable(&self, resizable: bool) {
|
|
|
|
let mut win_attribs = self.delegate.state.win_attribs.borrow_mut();
|
|
|
|
win_attribs.resizable = resizable;
|
|
|
|
if win_attribs.fullscreen.is_none() {
|
|
|
|
let mut mask = unsafe { self.window.styleMask() };
|
|
|
|
if resizable {
|
|
|
|
mask |= NSWindowStyleMask::NSResizableWindowMask;
|
|
|
|
} else {
|
|
|
|
mask &= !NSWindowStyleMask::NSResizableWindowMask;
|
|
|
|
}
|
|
|
|
unsafe { util::set_style_mask(*self.window, *self.view, mask) };
|
|
|
|
} // Otherwise, we don't change the mask until we exit fullscreen.
|
|
|
|
}
|
|
|
|
|
2017-02-03 23:05:57 +11:00
|
|
|
pub fn set_cursor(&self, cursor: MouseCursor) {
|
|
|
|
let cursor_name = match cursor {
|
|
|
|
MouseCursor::Arrow | MouseCursor::Default => "arrowCursor",
|
|
|
|
MouseCursor::Hand => "pointingHandCursor",
|
|
|
|
MouseCursor::Grabbing | MouseCursor::Grab => "closedHandCursor",
|
|
|
|
MouseCursor::Text => "IBeamCursor",
|
|
|
|
MouseCursor::VerticalText => "IBeamCursorForVerticalLayout",
|
|
|
|
MouseCursor::Copy => "dragCopyCursor",
|
|
|
|
MouseCursor::Alias => "dragLinkCursor",
|
|
|
|
MouseCursor::NotAllowed | MouseCursor::NoDrop => "operationNotAllowedCursor",
|
|
|
|
MouseCursor::ContextMenu => "contextualMenuCursor",
|
|
|
|
MouseCursor::Crosshair => "crosshairCursor",
|
|
|
|
MouseCursor::EResize => "resizeRightCursor",
|
|
|
|
MouseCursor::NResize => "resizeUpCursor",
|
|
|
|
MouseCursor::WResize => "resizeLeftCursor",
|
|
|
|
MouseCursor::SResize => "resizeDownCursor",
|
|
|
|
MouseCursor::EwResize | MouseCursor::ColResize => "resizeLeftRightCursor",
|
|
|
|
MouseCursor::NsResize | MouseCursor::RowResize => "resizeUpDownCursor",
|
|
|
|
|
2017-10-31 21:03:18 +11:00
|
|
|
// TODO: Find appropriate OSX cursors
|
2017-02-03 23:05:57 +11:00
|
|
|
MouseCursor::NeResize | MouseCursor::NwResize |
|
|
|
|
MouseCursor::SeResize | MouseCursor::SwResize |
|
|
|
|
MouseCursor::NwseResize | MouseCursor::NeswResize |
|
|
|
|
|
2018-06-19 02:32:18 +10:00
|
|
|
MouseCursor::Cell |
|
2017-02-03 23:05:57 +11:00
|
|
|
MouseCursor::Wait | MouseCursor::Progress | MouseCursor::Help |
|
|
|
|
MouseCursor::Move | MouseCursor::AllScroll | MouseCursor::ZoomIn |
|
|
|
|
MouseCursor::ZoomOut => "arrowCursor",
|
|
|
|
};
|
|
|
|
let sel = Sel::register(cursor_name);
|
2018-07-20 02:02:33 +10:00
|
|
|
let cls = class!(NSCursor);
|
2017-02-03 23:05:57 +11:00
|
|
|
unsafe {
|
|
|
|
use objc::Message;
|
|
|
|
let cursor: id = cls.send_message(sel, ()).unwrap();
|
|
|
|
let _: () = msg_send![cursor, set];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-19 02:32:18 +10:00
|
|
|
#[inline]
|
|
|
|
pub fn grab_cursor(&self, grab: bool) -> Result<(), String> {
|
|
|
|
// TODO: Do this for real https://stackoverflow.com/a/40922095/5435443
|
|
|
|
CGDisplay::associate_mouse_and_mouse_cursor_position(!grab)
|
|
|
|
.map_err(|status| format!("Failed to grab cursor: `CGError` {:?}", status))
|
|
|
|
}
|
2017-02-03 23:05:57 +11:00
|
|
|
|
2018-06-19 02:32:18 +10:00
|
|
|
#[inline]
|
|
|
|
pub fn hide_cursor(&self, hide: bool) {
|
2018-07-20 02:02:33 +10:00
|
|
|
let cursor_class = class!(NSCursor);
|
2018-06-19 02:32:18 +10:00
|
|
|
// macOS uses a "hide counter" like Windows does, so we avoid incrementing it more than once.
|
|
|
|
// (otherwise, `hide_cursor(false)` would need to be called n times!)
|
2018-07-27 07:14:16 +10:00
|
|
|
if hide != self.cursor_hidden.load(Ordering::Acquire) {
|
2018-06-19 02:32:18 +10:00
|
|
|
if hide {
|
|
|
|
let _: () = unsafe { msg_send![cursor_class, hide] };
|
|
|
|
} else {
|
|
|
|
let _: () = unsafe { msg_send![cursor_class, unhide] };
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
2018-07-27 07:14:16 +10:00
|
|
|
self.cursor_hidden.store(hide, Ordering::Release);
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-06-15 09:42:18 +10:00
|
|
|
pub fn get_hidpi_factor(&self) -> f64 {
|
2017-02-03 23:05:57 +11:00
|
|
|
unsafe {
|
2018-06-15 09:42:18 +10:00
|
|
|
NSWindow::backingScaleFactor(*self.window) as f64
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-06-20 00:30:15 +10:00
|
|
|
pub fn set_cursor_position(&self, cursor_position: LogicalPosition) -> Result<(), String> {
|
2018-06-15 09:42:18 +10:00
|
|
|
let window_position = self.get_inner_position()
|
2018-06-20 00:30:15 +10:00
|
|
|
.ok_or("`get_inner_position` failed".to_owned())?;
|
2018-06-15 09:42:18 +10:00
|
|
|
let point = appkit::CGPoint {
|
|
|
|
x: (cursor_position.x + window_position.x) as CGFloat,
|
|
|
|
y: (cursor_position.y + window_position.y) as CGFloat,
|
|
|
|
};
|
|
|
|
CGDisplay::warp_mouse_cursor_position(point)
|
2018-06-20 00:30:15 +10:00
|
|
|
.map_err(|e| format!("`CGWarpMouseCursorPosition` failed: {:?}", e))?;
|
2018-06-15 09:42:18 +10:00
|
|
|
CGDisplay::associate_mouse_and_mouse_cursor_position(true)
|
2018-06-20 00:30:15 +10:00
|
|
|
.map_err(|e| format!("`CGAssociateMouseAndMouseCursorPosition` failed: {:?}", e))?;
|
|
|
|
|
2017-02-03 23:05:57 +11:00
|
|
|
Ok(())
|
|
|
|
}
|
2017-08-28 10:43:34 +10:00
|
|
|
|
|
|
|
#[inline]
|
2018-04-18 04:07:54 +10:00
|
|
|
pub fn set_maximized(&self, maximized: bool) {
|
|
|
|
self.delegate.state.perform_maximized(maximized)
|
2017-09-07 18:33:46 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-04-18 04:07:54 +10:00
|
|
|
/// TODO: Right now set_fullscreen do not work on switching monitors
|
|
|
|
/// in fullscreen mode
|
|
|
|
pub fn set_fullscreen(&self, monitor: Option<RootMonitorId>) {
|
|
|
|
let state = &self.delegate.state;
|
|
|
|
let current = {
|
|
|
|
let win_attribs = state.win_attribs.borrow_mut();
|
|
|
|
|
|
|
|
let current = win_attribs.fullscreen.clone();
|
|
|
|
match (¤t, monitor) {
|
|
|
|
(&None, None) => {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
(&Some(ref a), Some(ref b)) if a.inner != b.inner => {
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
(&Some(_), Some(_)) => {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
current
|
|
|
|
};
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
// Because toggleFullScreen will not work if the StyleMask is none,
|
|
|
|
// We set a normal style to it temporary.
|
|
|
|
// It will clean up at window_did_exit_fullscreen.
|
|
|
|
if current.is_none() {
|
|
|
|
let curr_mask = state.window.styleMask();
|
2018-06-08 03:29:23 +10:00
|
|
|
let required = NSWindowStyleMask::NSTitledWindowMask | NSWindowStyleMask::NSResizableWindowMask;
|
|
|
|
if !curr_mask.contains(required) {
|
|
|
|
util::set_style_mask(*self.window, *self.view, required);
|
2018-04-18 04:07:54 +10:00
|
|
|
state.save_style_mask.set(Some(curr_mask));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.window.toggleFullScreen_(nil);
|
|
|
|
}
|
2017-08-28 10:43:34 +10:00
|
|
|
}
|
|
|
|
|
2017-12-22 23:50:46 +11:00
|
|
|
#[inline]
|
2018-04-18 04:07:54 +10:00
|
|
|
pub fn set_decorations(&self, decorations: bool) {
|
|
|
|
let state = &self.delegate.state;
|
|
|
|
let mut win_attribs = state.win_attribs.borrow_mut();
|
2018-04-25 06:20:40 +10:00
|
|
|
|
2018-04-18 04:07:54 +10:00
|
|
|
if win_attribs.decorations == decorations {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
win_attribs.decorations = decorations;
|
|
|
|
|
2018-04-25 06:20:40 +10:00
|
|
|
// Skip modifiy if we are in fullscreen mode,
|
2018-04-18 04:07:54 +10:00
|
|
|
// window_did_exit_fullscreen will handle it
|
|
|
|
if win_attribs.fullscreen.is_some() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
2018-06-08 03:29:23 +10:00
|
|
|
let mut new_mask = if decorations {
|
2018-04-18 04:07:54 +10:00
|
|
|
NSWindowStyleMask::NSClosableWindowMask
|
|
|
|
| NSWindowStyleMask::NSMiniaturizableWindowMask
|
|
|
|
| NSWindowStyleMask::NSResizableWindowMask
|
|
|
|
| NSWindowStyleMask::NSTitledWindowMask
|
|
|
|
} else {
|
|
|
|
NSWindowStyleMask::NSBorderlessWindowMask
|
2018-06-08 03:29:23 +10:00
|
|
|
| NSWindowStyleMask::NSResizableWindowMask
|
2018-04-18 04:07:54 +10:00
|
|
|
};
|
2018-06-08 03:29:23 +10:00
|
|
|
if !win_attribs.resizable {
|
|
|
|
new_mask &= !NSWindowStyleMask::NSResizableWindowMask;
|
|
|
|
}
|
2018-05-18 11:28:30 +10:00
|
|
|
util::set_style_mask(*state.window, *state.view, new_mask);
|
2018-04-18 04:07:54 +10:00
|
|
|
}
|
2017-12-22 23:50:46 +11:00
|
|
|
}
|
|
|
|
|
2018-05-21 00:24:05 +10:00
|
|
|
#[inline]
|
|
|
|
pub fn set_always_on_top(&self, always_on_top: bool) {
|
|
|
|
unsafe {
|
|
|
|
let level = if always_on_top {
|
|
|
|
ffi::NSWindowLevel::NSFloatingWindowLevel
|
|
|
|
} else {
|
|
|
|
ffi::NSWindowLevel::NSNormalWindowLevel
|
|
|
|
};
|
|
|
|
let _: () = msg_send![*self.window, setLevel:level];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-08 07:36:21 +10:00
|
|
|
#[inline]
|
|
|
|
pub fn set_window_icon(&self, _icon: Option<::Icon>) {
|
|
|
|
// macOS doesn't have window icons. Though, there is `setRepresentedFilename`, but that's
|
2018-05-18 11:28:30 +10:00
|
|
|
// 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`.
|
2018-05-08 07:36:21 +10:00
|
|
|
// https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/WinPanel/Tasks/SettingWindowTitle.html
|
|
|
|
}
|
|
|
|
|
2018-05-18 11:28:30 +10:00
|
|
|
#[inline]
|
2018-06-15 09:42:18 +10:00
|
|
|
pub fn set_ime_spot(&self, logical_spot: LogicalPosition) {
|
|
|
|
set_ime_spot(*self.view, *self.input_context, logical_spot.x, logical_spot.y);
|
2018-05-18 11:28:30 +10:00
|
|
|
}
|
|
|
|
|
2017-08-28 10:43:34 +10:00
|
|
|
#[inline]
|
2017-09-07 18:33:46 +10:00
|
|
|
pub fn get_current_monitor(&self) -> RootMonitorId {
|
2018-04-18 04:07:54 +10:00
|
|
|
unsafe {
|
2018-05-16 23:41:45 +10:00
|
|
|
self::get_current_monitor(*self.window)
|
2018-04-18 04:07:54 +10:00
|
|
|
}
|
2017-08-28 10:43:34 +10:00
|
|
|
}
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the `cocoa::base::id` associated with a window to a usize to use as a unique identifier
|
|
|
|
// for the window.
|
2018-04-29 02:10:06 +10:00
|
|
|
pub fn get_window_id(window_cocoa_id: id) -> Id {
|
2017-02-03 23:05:57 +11:00
|
|
|
Id(window_cocoa_id as *const objc::runtime::Object as usize)
|
|
|
|
}
|
|
|
|
|
2018-06-15 09:42:18 +10:00
|
|
|
unsafe fn nswindow_set_min_dimensions<V: NSWindow + Copy>(window: V, mut min_size: LogicalSize) {
|
|
|
|
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-15 09:42:18 +10: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-15 09:42:18 +10:00
|
|
|
if current_rect.size.width < min_size.width {
|
|
|
|
current_rect.size.width = min_size.width;
|
2017-02-03 23:05:57 +11:00
|
|
|
window.setFrame_display_(current_rect, 0)
|
|
|
|
}
|
2018-06-15 09:42:18 +10: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;
|
2017-02-03 23:05:57 +11:00
|
|
|
window.setFrame_display_(current_rect, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-15 09:42:18 +10:00
|
|
|
unsafe fn nswindow_set_max_dimensions<V: NSWindow + Copy>(window: V, mut max_size: LogicalSize) {
|
|
|
|
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-15 09:42:18 +10: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-15 09:42:18 +10:00
|
|
|
if current_rect.size.width > max_size.width {
|
|
|
|
current_rect.size.width = max_size.width;
|
2017-02-03 23:05:57 +11:00
|
|
|
window.setFrame_display_(current_rect, 0)
|
|
|
|
}
|
2018-06-15 09:42:18 +10: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;
|
2017-02-03 23:05:57 +11:00
|
|
|
window.setFrame_display_(current_rect, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct IdRef(id);
|
|
|
|
|
|
|
|
impl IdRef {
|
2018-03-06 19:35:04 +11:00
|
|
|
pub fn new(i: id) -> IdRef {
|
2017-02-03 23:05:57 +11:00
|
|
|
IdRef(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
2018-03-06 19:35:04 +11:00
|
|
|
pub fn retain(i: id) -> IdRef {
|
2017-02-03 23:05:57 +11:00
|
|
|
if i != nil {
|
|
|
|
let _: id = unsafe { msg_send![i, retain] };
|
|
|
|
}
|
|
|
|
IdRef(i)
|
|
|
|
}
|
|
|
|
|
2018-03-06 19:35:04 +11:00
|
|
|
pub fn non_nil(self) -> Option<IdRef> {
|
2017-02-03 23:05:57 +11:00
|
|
|
if self.0 == nil { None } else { Some(self) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for IdRef {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if self.0 != nil {
|
2018-04-29 02:10:06 +10:00
|
|
|
unsafe {
|
2018-05-18 11:28:30 +10:00
|
|
|
let autoreleasepool = NSAutoreleasePool::new(nil);
|
2018-04-29 02:10:06 +10:00
|
|
|
let _ : () = msg_send![self.0, release];
|
|
|
|
let _ : () = msg_send![autoreleasepool, release];
|
|
|
|
};
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for IdRef {
|
|
|
|
type Target = id;
|
|
|
|
fn deref<'a>(&'a self) -> &'a id {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Clone for IdRef {
|
|
|
|
fn clone(&self) -> IdRef {
|
|
|
|
if self.0 != nil {
|
|
|
|
let _: id = unsafe { msg_send![self.0, retain] };
|
|
|
|
}
|
|
|
|
IdRef(self.0)
|
|
|
|
}
|
|
|
|
}
|
2018-04-03 09:12:38 +10:00
|
|
|
|
|
|
|
extern fn yes(_: &Object, _: Sel) -> BOOL {
|
|
|
|
YES
|
|
|
|
}
|