chore(rustfmt): use nightly (#2325)
Stable rustfmt lacks a lot of features resulting in worse formatted code, thus use nightly formatter.
This commit is contained in:
parent
7006c7ceca
commit
7b0c7b6cb2
154 changed files with 3439 additions and 5891 deletions
|
|
@ -58,37 +58,29 @@ fn maybe_dispatch_device_event(delegate: &ApplicationDelegate, event: &NSEvent)
|
|||
let delta_y = unsafe { event.deltaY() } as f64;
|
||||
|
||||
if delta_x != 0.0 {
|
||||
delegate.queue_device_event(DeviceEvent::Motion {
|
||||
axis: 0,
|
||||
value: delta_x,
|
||||
});
|
||||
delegate.queue_device_event(DeviceEvent::Motion { axis: 0, value: delta_x });
|
||||
}
|
||||
|
||||
if delta_y != 0.0 {
|
||||
delegate.queue_device_event(DeviceEvent::Motion {
|
||||
axis: 1,
|
||||
value: delta_y,
|
||||
})
|
||||
delegate.queue_device_event(DeviceEvent::Motion { axis: 1, value: delta_y })
|
||||
}
|
||||
|
||||
if delta_x != 0.0 || delta_y != 0.0 {
|
||||
delegate.queue_device_event(DeviceEvent::MouseMotion {
|
||||
delta: (delta_x, delta_y),
|
||||
});
|
||||
delegate.queue_device_event(DeviceEvent::MouseMotion { delta: (delta_x, delta_y) });
|
||||
}
|
||||
}
|
||||
},
|
||||
NSEventType::LeftMouseDown | NSEventType::RightMouseDown | NSEventType::OtherMouseDown => {
|
||||
delegate.queue_device_event(DeviceEvent::Button {
|
||||
button: unsafe { event.buttonNumber() } as u32,
|
||||
state: ElementState::Pressed,
|
||||
});
|
||||
}
|
||||
},
|
||||
NSEventType::LeftMouseUp | NSEventType::RightMouseUp | NSEventType::OtherMouseUp => {
|
||||
delegate.queue_device_event(DeviceEvent::Button {
|
||||
button: unsafe { event.buttonNumber() } as u32,
|
||||
state: ElementState::Released,
|
||||
});
|
||||
}
|
||||
},
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -240,10 +240,7 @@ impl ApplicationDelegate {
|
|||
}
|
||||
|
||||
pub fn queue_device_event(&self, event: DeviceEvent) {
|
||||
self.ivars()
|
||||
.pending_events
|
||||
.borrow_mut()
|
||||
.push_back(QueuedEvent::DeviceEvent(event));
|
||||
self.ivars().pending_events.borrow_mut().push_back(QueuedEvent::DeviceEvent(event));
|
||||
}
|
||||
|
||||
pub fn queue_static_scale_factor_changed_event(
|
||||
|
|
@ -252,14 +249,11 @@ impl ApplicationDelegate {
|
|||
suggested_size: PhysicalSize<u32>,
|
||||
scale_factor: f64,
|
||||
) {
|
||||
self.ivars()
|
||||
.pending_events
|
||||
.borrow_mut()
|
||||
.push_back(QueuedEvent::ScaleFactorChanged {
|
||||
window,
|
||||
suggested_size,
|
||||
scale_factor,
|
||||
});
|
||||
self.ivars().pending_events.borrow_mut().push_back(QueuedEvent::ScaleFactorChanged {
|
||||
window,
|
||||
suggested_size,
|
||||
scale_factor,
|
||||
});
|
||||
}
|
||||
|
||||
pub fn handle_redraw(&self, window_id: WindowId) {
|
||||
|
|
@ -272,8 +266,9 @@ impl ApplicationDelegate {
|
|||
event: WindowEvent::RedrawRequested,
|
||||
});
|
||||
|
||||
// `pump_events` will request to stop immediately _after_ dispatching RedrawRequested events
|
||||
// as a way to ensure that `pump_events` can't block an external loop indefinitely
|
||||
// `pump_events` will request to stop immediately _after_ dispatching RedrawRequested
|
||||
// events as a way to ensure that `pump_events` can't block an external loop
|
||||
// indefinitely
|
||||
if self.ivars().stop_on_redraw.get() {
|
||||
let app = NSApplication::sharedApplication(mtm);
|
||||
stop_app_immediately(&app);
|
||||
|
|
@ -290,9 +285,7 @@ impl ApplicationDelegate {
|
|||
}
|
||||
|
||||
fn handle_event(&self, event: Event<HandlePendingUserEvents>) {
|
||||
self.ivars()
|
||||
.event_handler
|
||||
.handle_event(event, &ActiveEventLoop::new_root(self.retain()))
|
||||
self.ivars().event_handler.handle_event(event, &ActiveEventLoop::new_root(self.retain()))
|
||||
}
|
||||
|
||||
/// dispatch `NewEvents(Init)` + `Resumed`
|
||||
|
|
@ -323,23 +316,14 @@ impl ApplicationDelegate {
|
|||
let start = self.ivars().start_time.get().unwrap();
|
||||
let cause = match self.control_flow() {
|
||||
ControlFlow::Poll => StartCause::Poll,
|
||||
ControlFlow::Wait => StartCause::WaitCancelled {
|
||||
start,
|
||||
requested_resume: None,
|
||||
},
|
||||
ControlFlow::Wait => StartCause::WaitCancelled { start, requested_resume: None },
|
||||
ControlFlow::WaitUntil(requested_resume) => {
|
||||
if Instant::now() >= requested_resume {
|
||||
StartCause::ResumeTimeReached {
|
||||
start,
|
||||
requested_resume,
|
||||
}
|
||||
StartCause::ResumeTimeReached { start, requested_resume }
|
||||
} else {
|
||||
StartCause::WaitCancelled {
|
||||
start,
|
||||
requested_resume: Some(requested_resume),
|
||||
}
|
||||
StartCause::WaitCancelled { start, requested_resume: Some(requested_resume) }
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
self.handle_event(Event::NewEvents(cause));
|
||||
|
|
@ -369,18 +353,11 @@ impl ApplicationDelegate {
|
|||
window_id: RootWindowId(window_id),
|
||||
event,
|
||||
});
|
||||
}
|
||||
},
|
||||
QueuedEvent::DeviceEvent(event) => {
|
||||
self.handle_event(Event::DeviceEvent {
|
||||
device_id: DEVICE_ID,
|
||||
event,
|
||||
});
|
||||
}
|
||||
QueuedEvent::ScaleFactorChanged {
|
||||
window,
|
||||
suggested_size,
|
||||
scale_factor,
|
||||
} => {
|
||||
self.handle_event(Event::DeviceEvent { device_id: DEVICE_ID, event });
|
||||
},
|
||||
QueuedEvent::ScaleFactorChanged { window, suggested_size, scale_factor } => {
|
||||
let new_inner_size = Arc::new(Mutex::new(suggested_size));
|
||||
let scale_factor_changed_event = Event::WindowEvent {
|
||||
window_id: RootWindowId(window.id()),
|
||||
|
|
@ -405,7 +382,7 @@ impl ApplicationDelegate {
|
|||
event: WindowEvent::Resized(physical_size),
|
||||
};
|
||||
self.handle_event(resized_event);
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -435,10 +412,7 @@ impl ApplicationDelegate {
|
|||
ControlFlow::Poll => Some(Instant::now()),
|
||||
ControlFlow::WaitUntil(instant) => Some(instant),
|
||||
};
|
||||
self.ivars()
|
||||
.waker
|
||||
.borrow_mut()
|
||||
.start_at(min_timeout(wait_timeout, app_timeout));
|
||||
self.ivars().waker.borrow_mut().start_at(min_timeout(wait_timeout, app_timeout));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -460,9 +434,7 @@ pub(crate) struct HandlePendingUserEvents;
|
|||
/// equates to an infinite timeout, not a zero timeout (so can't just use
|
||||
/// `Option::min`)
|
||||
fn min_timeout(a: Option<Instant>, b: Option<Instant>) -> Option<Instant> {
|
||||
a.map_or(b, |a_timeout| {
|
||||
b.map_or(Some(a_timeout), |b_timeout| Some(a_timeout.min(b_timeout)))
|
||||
})
|
||||
a.map_or(b, |a_timeout| b.map_or(Some(a_timeout), |b_timeout| Some(a_timeout.min(b_timeout))))
|
||||
}
|
||||
|
||||
/// A hack to make activation of multiple windows work when creating them before
|
||||
|
|
|
|||
|
|
@ -11,8 +11,7 @@ use objc2_foundation::{
|
|||
NSString,
|
||||
};
|
||||
|
||||
use crate::cursor::CursorImage;
|
||||
use crate::cursor::OnlyCursorImageSource;
|
||||
use crate::cursor::{CursorImage, OnlyCursorImageSource};
|
||||
use crate::window::CursorIcon;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||
|
|
@ -118,7 +117,10 @@ unsafe fn load_webkit_cursor(name: &NSString) -> Id<NSCursor> {
|
|||
// cursors, and will seem completely standard to macOS users.
|
||||
//
|
||||
// https://stackoverflow.com/a/21786835/5435443
|
||||
let root = ns_string!("/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/HIServices.framework/Versions/A/Resources/cursors");
|
||||
let root = ns_string!(
|
||||
"/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/\
|
||||
HIServices.framework/Versions/A/Resources/cursors"
|
||||
);
|
||||
let cursor_path = root.stringByAppendingPathComponent(name);
|
||||
|
||||
let pdf_path = cursor_path.stringByAppendingPathComponent(ns_string!("cursor.pdf"));
|
||||
|
|
@ -166,10 +168,10 @@ pub(crate) fn invisible_cursor() -> Id<NSCursor> {
|
|||
// You can reproduce this via ImageMagick.
|
||||
// $ convert -size 16x16 xc:none cursor.gif
|
||||
static CURSOR_BYTES: &[u8] = &[
|
||||
0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x10, 0x00, 0x10, 0x00, 0xF0, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x21, 0xF9, 0x04, 0x01, 0x00, 0x00, 0x00, 0x00, 0x2C, 0x00, 0x00,
|
||||
0x00, 0x00, 0x10, 0x00, 0x10, 0x00, 0x00, 0x02, 0x0E, 0x84, 0x8F, 0xA9, 0xCB, 0xED, 0x0F,
|
||||
0xA3, 0x9C, 0xB4, 0xDA, 0x8B, 0xB3, 0x3E, 0x05, 0x00, 0x3B,
|
||||
0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x10, 0x00, 0x10, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x21, 0xf9, 0x04, 0x01, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x00,
|
||||
0x00, 0x00, 0x10, 0x00, 0x10, 0x00, 0x00, 0x02, 0x0e, 0x84, 0x8f, 0xa9, 0xcb, 0xed, 0x0f,
|
||||
0xa3, 0x9c, 0xb4, 0xda, 0x8b, 0xb3, 0x3e, 0x05, 0x00, 0x3b,
|
||||
];
|
||||
|
||||
fn new_invisible() -> Id<NSCursor> {
|
||||
|
|
@ -182,10 +184,7 @@ pub(crate) fn invisible_cursor() -> Id<NSCursor> {
|
|||
|
||||
// Cache this for efficiency
|
||||
static CURSOR: OnceLock<CustomCursor> = OnceLock::new();
|
||||
CURSOR
|
||||
.get_or_init(|| CustomCursor(new_invisible()))
|
||||
.0
|
||||
.clone()
|
||||
CURSOR.get_or_init(|| CustomCursor(new_invisible())).0.clone()
|
||||
}
|
||||
|
||||
pub(crate) fn cursor_from_icon(icon: CursorIcon) -> Id<NSCursor> {
|
||||
|
|
|
|||
|
|
@ -1,22 +1,18 @@
|
|||
use std::ffi::c_void;
|
||||
|
||||
use core_foundation::{
|
||||
base::CFRelease,
|
||||
data::{CFDataGetBytePtr, CFDataRef},
|
||||
};
|
||||
use core_foundation::base::CFRelease;
|
||||
use core_foundation::data::{CFDataGetBytePtr, CFDataRef};
|
||||
use objc2::rc::Id;
|
||||
use objc2_app_kit::{NSEvent, NSEventModifierFlags, NSEventSubtype, NSEventType};
|
||||
use objc2_foundation::{run_on_main, NSPoint};
|
||||
use smol_str::SmolStr;
|
||||
|
||||
use crate::{
|
||||
event::{ElementState, KeyEvent, Modifiers},
|
||||
keyboard::{
|
||||
Key, KeyCode, KeyLocation, ModifiersKeys, ModifiersState, NamedKey, NativeKey,
|
||||
NativeKeyCode, PhysicalKey,
|
||||
},
|
||||
platform_impl::platform::ffi,
|
||||
use crate::event::{ElementState, KeyEvent, Modifiers};
|
||||
use crate::keyboard::{
|
||||
Key, KeyCode, KeyLocation, ModifiersKeys, ModifiersState, NamedKey, NativeKey, NativeKeyCode,
|
||||
PhysicalKey,
|
||||
};
|
||||
use crate::platform_impl::platform::ffi;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct KeyEventExtra {
|
||||
|
|
@ -67,10 +63,7 @@ pub fn get_modifierless_char(scancode: u16) -> Key {
|
|||
CFRelease(input_source as *mut c_void);
|
||||
}
|
||||
if translate_result != 0 {
|
||||
tracing::error!(
|
||||
"`UCKeyTranslate` returned with the non-zero value: {}",
|
||||
translate_result
|
||||
);
|
||||
tracing::error!("`UCKeyTranslate` returned with the non-zero value: {}", translate_result);
|
||||
return Key::Unidentified(NativeKey::MacOS(scancode));
|
||||
}
|
||||
if result_len == 0 {
|
||||
|
|
@ -121,9 +114,8 @@ pub(crate) fn create_key_event(
|
|||
let text_with_all_modifiers: Option<SmolStr> = if key_override.is_some() {
|
||||
None
|
||||
} else {
|
||||
let characters = unsafe { ns_event.characters() }
|
||||
.map(|s| s.to_string())
|
||||
.unwrap_or_default();
|
||||
let characters =
|
||||
unsafe { ns_event.characters() }.map(|s| s.to_string()).unwrap_or_default();
|
||||
if characters.is_empty() {
|
||||
None
|
||||
} else {
|
||||
|
|
@ -153,7 +145,7 @@ pub(crate) fn create_key_event(
|
|||
Some(text) if !has_ctrl && !has_cmd => {
|
||||
// Character heeding both SHIFT and ALT.
|
||||
Key::Character(text.clone())
|
||||
}
|
||||
},
|
||||
|
||||
_ => match key_without_modifiers.as_ref() {
|
||||
// Character heeding just SHIFT, ignoring ALT.
|
||||
|
|
@ -169,11 +161,7 @@ pub(crate) fn create_key_event(
|
|||
(key_from_code.clone(), key_from_code)
|
||||
};
|
||||
|
||||
let text = if is_press {
|
||||
logical_key.to_text().map(SmolStr::new)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let text = if is_press { logical_key.to_text().map(SmolStr::new) } else { None };
|
||||
|
||||
let location = code_to_location(physical_key);
|
||||
|
||||
|
|
@ -184,10 +172,7 @@ pub(crate) fn create_key_event(
|
|||
repeat: is_repeat,
|
||||
state,
|
||||
text,
|
||||
platform_specific: KeyEventExtra {
|
||||
text_with_all_modifiers,
|
||||
key_without_modifiers,
|
||||
},
|
||||
platform_specific: KeyEventExtra { text_with_all_modifiers, key_without_modifiers },
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -341,58 +326,31 @@ pub(super) fn event_mods(event: &NSEvent) -> Modifiers {
|
|||
ModifiersState::SHIFT,
|
||||
flags_contains(flags, NSEventModifierFlags::NSEventModifierFlagShift),
|
||||
);
|
||||
pressed_mods.set(
|
||||
ModifiersKeys::LSHIFT,
|
||||
flags_contains(flags, NX_DEVICELSHIFTKEYMASK),
|
||||
);
|
||||
pressed_mods.set(
|
||||
ModifiersKeys::RSHIFT,
|
||||
flags_contains(flags, NX_DEVICERSHIFTKEYMASK),
|
||||
);
|
||||
pressed_mods.set(ModifiersKeys::LSHIFT, flags_contains(flags, NX_DEVICELSHIFTKEYMASK));
|
||||
pressed_mods.set(ModifiersKeys::RSHIFT, flags_contains(flags, NX_DEVICERSHIFTKEYMASK));
|
||||
|
||||
state.set(
|
||||
ModifiersState::CONTROL,
|
||||
flags_contains(flags, NSEventModifierFlags::NSEventModifierFlagControl),
|
||||
);
|
||||
pressed_mods.set(
|
||||
ModifiersKeys::LCONTROL,
|
||||
flags_contains(flags, NX_DEVICELCTLKEYMASK),
|
||||
);
|
||||
pressed_mods.set(
|
||||
ModifiersKeys::RCONTROL,
|
||||
flags_contains(flags, NX_DEVICERCTLKEYMASK),
|
||||
);
|
||||
pressed_mods.set(ModifiersKeys::LCONTROL, flags_contains(flags, NX_DEVICELCTLKEYMASK));
|
||||
pressed_mods.set(ModifiersKeys::RCONTROL, flags_contains(flags, NX_DEVICERCTLKEYMASK));
|
||||
|
||||
state.set(
|
||||
ModifiersState::ALT,
|
||||
flags_contains(flags, NSEventModifierFlags::NSEventModifierFlagOption),
|
||||
);
|
||||
pressed_mods.set(
|
||||
ModifiersKeys::LALT,
|
||||
flags_contains(flags, NX_DEVICELALTKEYMASK),
|
||||
);
|
||||
pressed_mods.set(
|
||||
ModifiersKeys::RALT,
|
||||
flags_contains(flags, NX_DEVICERALTKEYMASK),
|
||||
);
|
||||
pressed_mods.set(ModifiersKeys::LALT, flags_contains(flags, NX_DEVICELALTKEYMASK));
|
||||
pressed_mods.set(ModifiersKeys::RALT, flags_contains(flags, NX_DEVICERALTKEYMASK));
|
||||
|
||||
state.set(
|
||||
ModifiersState::SUPER,
|
||||
flags_contains(flags, NSEventModifierFlags::NSEventModifierFlagCommand),
|
||||
);
|
||||
pressed_mods.set(
|
||||
ModifiersKeys::LSUPER,
|
||||
flags_contains(flags, NX_DEVICELCMDKEYMASK),
|
||||
);
|
||||
pressed_mods.set(
|
||||
ModifiersKeys::RSUPER,
|
||||
flags_contains(flags, NX_DEVICERCMDKEYMASK),
|
||||
);
|
||||
pressed_mods.set(ModifiersKeys::LSUPER, flags_contains(flags, NX_DEVICELCMDKEYMASK));
|
||||
pressed_mods.set(ModifiersKeys::RSUPER, flags_contains(flags, NX_DEVICERCMDKEYMASK));
|
||||
|
||||
Modifiers {
|
||||
state,
|
||||
pressed_mods,
|
||||
}
|
||||
Modifiers { state, pressed_mods }
|
||||
}
|
||||
|
||||
pub(super) fn dummy_event() -> Option<Id<NSEvent>> {
|
||||
|
|
@ -545,7 +503,7 @@ pub(crate) fn scancode_to_physicalkey(scancode: u32) -> PhysicalKey {
|
|||
0x07 => KeyCode::KeyX,
|
||||
0x08 => KeyCode::KeyC,
|
||||
0x09 => KeyCode::KeyV,
|
||||
//0x0a => World 1,
|
||||
// 0x0a => World 1,
|
||||
0x0b => KeyCode::KeyB,
|
||||
0x0c => KeyCode::KeyQ,
|
||||
0x0d => KeyCode::KeyW,
|
||||
|
|
@ -587,7 +545,7 @@ pub(crate) fn scancode_to_physicalkey(scancode: u32) -> PhysicalKey {
|
|||
0x31 => KeyCode::Space,
|
||||
0x32 => KeyCode::Backquote,
|
||||
0x33 => KeyCode::Backspace,
|
||||
//0x34 => unknown,
|
||||
// 0x34 => unknown,
|
||||
0x35 => KeyCode::Escape,
|
||||
0x36 => KeyCode::SuperRight,
|
||||
0x37 => KeyCode::SuperLeft,
|
||||
|
|
@ -601,22 +559,23 @@ pub(crate) fn scancode_to_physicalkey(scancode: u32) -> PhysicalKey {
|
|||
0x3f => KeyCode::Fn,
|
||||
0x40 => KeyCode::F17,
|
||||
0x41 => KeyCode::NumpadDecimal,
|
||||
//0x42 -> unknown,
|
||||
// 0x42 -> unknown,
|
||||
0x43 => KeyCode::NumpadMultiply,
|
||||
//0x44 => unknown,
|
||||
// 0x44 => unknown,
|
||||
0x45 => KeyCode::NumpadAdd,
|
||||
//0x46 => unknown,
|
||||
// 0x46 => unknown,
|
||||
0x47 => KeyCode::NumLock,
|
||||
//0x48 => KeyCode::NumpadClear,
|
||||
// 0x48 => KeyCode::NumpadClear,
|
||||
|
||||
// TODO: (Artur) for me, kVK_VolumeUp is 0x48
|
||||
// macOS 10.11
|
||||
// /System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/HIToolbox.framework/Versions/A/Headers/Events.h
|
||||
// /System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/HIToolbox.framework/
|
||||
// Versions/A/Headers/Events.h
|
||||
0x49 => KeyCode::AudioVolumeUp,
|
||||
0x4a => KeyCode::AudioVolumeDown,
|
||||
0x4b => KeyCode::NumpadDivide,
|
||||
0x4c => KeyCode::NumpadEnter,
|
||||
//0x4d => unknown,
|
||||
// 0x4d => unknown,
|
||||
0x4e => KeyCode::NumpadSubtract,
|
||||
0x4f => KeyCode::F18,
|
||||
0x50 => KeyCode::F19,
|
||||
|
|
@ -633,25 +592,25 @@ pub(crate) fn scancode_to_physicalkey(scancode: u32) -> PhysicalKey {
|
|||
0x5b => KeyCode::Numpad8,
|
||||
0x5c => KeyCode::Numpad9,
|
||||
0x5d => KeyCode::IntlYen,
|
||||
//0x5e => JIS Ro,
|
||||
//0x5f => unknown,
|
||||
// 0x5e => JIS Ro,
|
||||
// 0x5f => unknown,
|
||||
0x60 => KeyCode::F5,
|
||||
0x61 => KeyCode::F6,
|
||||
0x62 => KeyCode::F7,
|
||||
0x63 => KeyCode::F3,
|
||||
0x64 => KeyCode::F8,
|
||||
0x65 => KeyCode::F9,
|
||||
//0x66 => JIS Eisuu (macOS),
|
||||
// 0x66 => JIS Eisuu (macOS),
|
||||
0x67 => KeyCode::F11,
|
||||
//0x68 => JIS Kanna (macOS),
|
||||
// 0x68 => JIS Kanna (macOS),
|
||||
0x69 => KeyCode::F13,
|
||||
0x6a => KeyCode::F16,
|
||||
0x6b => KeyCode::F14,
|
||||
//0x6c => unknown,
|
||||
// 0x6c => unknown,
|
||||
0x6d => KeyCode::F10,
|
||||
//0x6e => unknown,
|
||||
// 0x6e => unknown,
|
||||
0x6f => KeyCode::F12,
|
||||
//0x70 => unknown,
|
||||
// 0x70 => unknown,
|
||||
0x71 => KeyCode::F15,
|
||||
0x72 => KeyCode::Insert,
|
||||
0x73 => KeyCode::Home,
|
||||
|
|
@ -666,7 +625,7 @@ pub(crate) fn scancode_to_physicalkey(scancode: u32) -> PhysicalKey {
|
|||
0x7c => KeyCode::ArrowRight,
|
||||
0x7d => KeyCode::ArrowDown,
|
||||
0x7e => KeyCode::ArrowUp,
|
||||
//0x7f => unknown,
|
||||
// 0x7f => unknown,
|
||||
|
||||
// 0xA is the caret (^) an macOS's German QERTZ layout. This key is at the same location as
|
||||
// backquote (`) on Windows' US layout.
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
use std::cell::RefCell;
|
||||
use std::fmt;
|
||||
use std::mem;
|
||||
use std::{fmt, mem};
|
||||
|
||||
use super::app_delegate::HandlePendingUserEvents;
|
||||
use crate::event::Event;
|
||||
|
|
@ -53,13 +52,13 @@ impl EventHandler {
|
|||
match self.inner.try_borrow_mut().as_deref_mut() {
|
||||
Ok(Some(_)) => {
|
||||
unreachable!("tried to set handler while another was already set");
|
||||
}
|
||||
},
|
||||
Ok(data @ None) => {
|
||||
*data = Some(EventHandlerData { handler });
|
||||
}
|
||||
},
|
||||
Err(_) => {
|
||||
unreachable!("tried to set handler that is currently in use");
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
struct ClearOnDrop<'a>(&'a EventHandler);
|
||||
|
|
@ -69,10 +68,10 @@ impl EventHandler {
|
|||
match self.0.inner.try_borrow_mut().as_deref_mut() {
|
||||
Ok(data @ Some(_)) => {
|
||||
*data = None;
|
||||
}
|
||||
},
|
||||
Ok(None) => {
|
||||
tracing::error!("tried to clear handler, but no handler was set");
|
||||
}
|
||||
},
|
||||
Err(_) => {
|
||||
// Note: This is not expected to ever happen, this
|
||||
// module generally controls the `RefCell`, and
|
||||
|
|
@ -83,7 +82,7 @@ impl EventHandler {
|
|||
// weren't able to unset the handler.
|
||||
eprintln!("tried to clear handler that is currently in use");
|
||||
std::process::abort();
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -120,17 +119,17 @@ impl EventHandler {
|
|||
// If the handler unwinds, the `RefMut` will ensure that the
|
||||
// handler is no longer borrowed.
|
||||
(handler)(event, event_loop);
|
||||
}
|
||||
},
|
||||
Ok(None) => {
|
||||
// `NSApplication`, our app delegate and this handler are all
|
||||
// global state and so it's not impossible that we could get
|
||||
// an event after the application has exited the `EventLoop`.
|
||||
tracing::error!("tried to run event handler, but no handler was set");
|
||||
}
|
||||
},
|
||||
Err(_) => {
|
||||
// Prevent re-entrancy.
|
||||
panic!("tried to handle event while another event is currently being handled");
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,44 +1,39 @@
|
|||
use std::{
|
||||
any::Any,
|
||||
cell::Cell,
|
||||
collections::VecDeque,
|
||||
marker::PhantomData,
|
||||
os::raw::c_void,
|
||||
panic::{catch_unwind, resume_unwind, RefUnwindSafe, UnwindSafe},
|
||||
ptr,
|
||||
rc::{Rc, Weak},
|
||||
sync::mpsc,
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
use std::any::Any;
|
||||
use std::cell::Cell;
|
||||
use std::collections::VecDeque;
|
||||
use std::marker::PhantomData;
|
||||
use std::os::raw::c_void;
|
||||
use std::panic::{catch_unwind, resume_unwind, RefUnwindSafe, UnwindSafe};
|
||||
use std::ptr;
|
||||
use std::rc::{Rc, Weak};
|
||||
use std::sync::mpsc;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use core_foundation::base::{CFIndex, CFRelease};
|
||||
use core_foundation::runloop::{
|
||||
kCFRunLoopCommonModes, CFRunLoopAddSource, CFRunLoopGetMain, CFRunLoopSourceContext,
|
||||
CFRunLoopSourceCreate, CFRunLoopSourceRef, CFRunLoopSourceSignal, CFRunLoopWakeUp,
|
||||
};
|
||||
use objc2::rc::{autoreleasepool, Id};
|
||||
use objc2::runtime::ProtocolObject;
|
||||
use objc2::{msg_send_id, ClassType};
|
||||
use objc2::{
|
||||
rc::{autoreleasepool, Id},
|
||||
runtime::ProtocolObject,
|
||||
};
|
||||
use objc2_app_kit::{NSApplication, NSApplicationActivationPolicy, NSWindow};
|
||||
use objc2_foundation::{MainThreadMarker, NSObjectProtocol};
|
||||
|
||||
use super::app::WinitApplication;
|
||||
use super::app_delegate::{ApplicationDelegate, HandlePendingUserEvents};
|
||||
use super::event::dummy_event;
|
||||
use super::{
|
||||
app::WinitApplication,
|
||||
app_delegate::{ApplicationDelegate, HandlePendingUserEvents},
|
||||
monitor::{self, MonitorHandle},
|
||||
observer::setup_control_flow_observers,
|
||||
use super::monitor::{self, MonitorHandle};
|
||||
use super::observer::setup_control_flow_observers;
|
||||
use crate::error::EventLoopError;
|
||||
use crate::event::Event;
|
||||
use crate::event_loop::{
|
||||
ActiveEventLoop as RootWindowTarget, ControlFlow, DeviceEvents, EventLoopClosed,
|
||||
};
|
||||
use crate::platform::macos::ActivationPolicy;
|
||||
use crate::platform::pump_events::PumpStatus;
|
||||
use crate::platform_impl::platform::cursor::CustomCursor;
|
||||
use crate::window::{CustomCursor as RootCustomCursor, CustomCursorSource};
|
||||
use crate::{
|
||||
error::EventLoopError,
|
||||
event::Event,
|
||||
event_loop::{ActiveEventLoop as RootWindowTarget, ControlFlow, DeviceEvents, EventLoopClosed},
|
||||
platform::{macos::ActivationPolicy, pump_events::PumpStatus},
|
||||
};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct PanicInfo {
|
||||
|
|
@ -57,12 +52,14 @@ impl PanicInfo {
|
|||
self.inner.set(inner);
|
||||
result
|
||||
}
|
||||
|
||||
/// Overwrites the curret state if the current state is not panicking
|
||||
pub fn set_panic(&self, p: Box<dyn Any + Send + 'static>) {
|
||||
if !self.is_panicking() {
|
||||
self.inner.set(Some(p));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn take(&self) -> Option<Box<dyn Any + Send + 'static>> {
|
||||
self.inner.take()
|
||||
}
|
||||
|
|
@ -78,16 +75,11 @@ impl ActiveEventLoop {
|
|||
pub(super) fn new_root(delegate: Id<ApplicationDelegate>) -> RootWindowTarget {
|
||||
let mtm = MainThreadMarker::from(&*delegate);
|
||||
let p = Self { delegate, mtm };
|
||||
RootWindowTarget {
|
||||
p,
|
||||
_marker: PhantomData,
|
||||
}
|
||||
RootWindowTarget { p, _marker: PhantomData }
|
||||
}
|
||||
|
||||
pub fn create_custom_cursor(&self, source: CustomCursorSource) -> RootCustomCursor {
|
||||
RootCustomCursor {
|
||||
inner: CustomCursor::new(source.inner),
|
||||
}
|
||||
RootCustomCursor { inner: CustomCursor::new(source.inner) }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
|
@ -115,9 +107,7 @@ impl ActiveEventLoop {
|
|||
pub fn raw_display_handle_rwh_06(
|
||||
&self,
|
||||
) -> Result<rwh_06::RawDisplayHandle, rwh_06::HandleError> {
|
||||
Ok(rwh_06::RawDisplayHandle::AppKit(
|
||||
rwh_06::AppKitDisplayHandle::new(),
|
||||
))
|
||||
Ok(rwh_06::RawDisplayHandle::AppKit(rwh_06::AppKitDisplayHandle::new()))
|
||||
}
|
||||
|
||||
pub(crate) fn set_control_flow(&self, control_flow: ControlFlow) {
|
||||
|
|
@ -173,7 +163,7 @@ fn map_user_event<T: 'static>(
|
|||
for event in receiver.try_iter() {
|
||||
(handler)(Event::UserEvent(event), window_target);
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -225,7 +215,10 @@ impl<T> EventLoop<T> {
|
|||
unsafe { msg_send_id![WinitApplication::class(), sharedApplication] };
|
||||
|
||||
if !app.is_kind_of::<WinitApplication>() {
|
||||
panic!("`winit` requires control over the principal class. You must create the event loop before other parts of your application initialize NSApplication");
|
||||
panic!(
|
||||
"`winit` requires control over the principal class. You must create the event \
|
||||
loop before other parts of your application initialize NSApplication"
|
||||
);
|
||||
}
|
||||
|
||||
let activation_policy = match attributes.activation_policy {
|
||||
|
|
@ -323,8 +316,8 @@ impl<T> EventLoop<T> {
|
|||
|
||||
self.delegate.set_event_handler(handler, || {
|
||||
autoreleasepool(|_| {
|
||||
// As a special case, if the application hasn't been launched yet then we at least run
|
||||
// the loop until it has fully launched.
|
||||
// As a special case, if the application hasn't been launched yet then we at least
|
||||
// run the loop until it has fully launched.
|
||||
if !self.delegate.is_launched() {
|
||||
debug_assert!(!self.delegate.is_running());
|
||||
|
||||
|
|
@ -332,31 +325,34 @@ impl<T> EventLoop<T> {
|
|||
// SAFETY: We do not run the application re-entrantly
|
||||
unsafe { self.app.run() };
|
||||
|
||||
// Note: we dispatch `NewEvents(Init)` + `Resumed` events after the application has launched
|
||||
// Note: we dispatch `NewEvents(Init)` + `Resumed` events after the application
|
||||
// has launched
|
||||
} else if !self.delegate.is_running() {
|
||||
// Even though the application may have been launched, it's possible we aren't running
|
||||
// if the `EventLoop` was run before and has since exited. This indicates that
|
||||
// we just starting to re-run the same `EventLoop` again.
|
||||
// Even though the application may have been launched, it's possible we aren't
|
||||
// running if the `EventLoop` was run before and has since
|
||||
// exited. This indicates that we just starting to re-run
|
||||
// the same `EventLoop` again.
|
||||
self.delegate.set_is_running(true);
|
||||
self.delegate.dispatch_init_events();
|
||||
} else {
|
||||
// Only run for as long as the given `Duration` allows so we don't block the external loop.
|
||||
// Only run for as long as the given `Duration` allows so we don't block the
|
||||
// external loop.
|
||||
match timeout {
|
||||
Some(Duration::ZERO) => {
|
||||
self.delegate.set_wait_timeout(None);
|
||||
self.delegate.set_stop_before_wait(true);
|
||||
}
|
||||
},
|
||||
Some(duration) => {
|
||||
self.delegate.set_stop_before_wait(false);
|
||||
let timeout = Instant::now() + duration;
|
||||
self.delegate.set_wait_timeout(Some(timeout));
|
||||
self.delegate.set_stop_after_wait(true);
|
||||
}
|
||||
},
|
||||
None => {
|
||||
self.delegate.set_wait_timeout(None);
|
||||
self.delegate.set_stop_before_wait(false);
|
||||
self.delegate.set_stop_after_wait(true);
|
||||
}
|
||||
},
|
||||
}
|
||||
self.delegate.set_stop_on_redraw(true);
|
||||
// SAFETY: We do not run the application re-entrantly
|
||||
|
|
@ -437,7 +433,7 @@ pub fn stop_app_on_panic<F: FnOnce() -> R + UnwindSafe, R>(
|
|||
let app = NSApplication::sharedApplication(mtm);
|
||||
stop_app_immediately(&app);
|
||||
None
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -494,9 +490,7 @@ impl<T> EventLoopProxy<T> {
|
|||
}
|
||||
|
||||
pub fn send_event(&self, event: T) -> Result<(), EventLoopClosed<T>> {
|
||||
self.sender
|
||||
.send(event)
|
||||
.map_err(|mpsc::SendError(x)| EventLoopClosed(x))?;
|
||||
self.sender.send(event).map_err(|mpsc::SendError(x)| EventLoopClosed(x))?;
|
||||
unsafe {
|
||||
// let the main thread know there's a new event
|
||||
CFRunLoopSourceSignal(self.source);
|
||||
|
|
|
|||
|
|
@ -4,14 +4,14 @@
|
|||
|
||||
use std::ffi::c_void;
|
||||
|
||||
use core_foundation::{
|
||||
array::CFArrayRef, dictionary::CFDictionaryRef, string::CFStringRef, uuid::CFUUIDRef,
|
||||
};
|
||||
use core_graphics::{
|
||||
base::CGError,
|
||||
display::{CGDirectDisplayID, CGDisplayConfigRef},
|
||||
};
|
||||
use objc2::{ffi::NSInteger, runtime::AnyObject};
|
||||
use core_foundation::array::CFArrayRef;
|
||||
use core_foundation::dictionary::CFDictionaryRef;
|
||||
use core_foundation::string::CFStringRef;
|
||||
use core_foundation::uuid::CFUUIDRef;
|
||||
use core_graphics::base::CGError;
|
||||
use core_graphics::display::{CGDirectDisplayID, CGDisplayConfigRef};
|
||||
use objc2::ffi::NSInteger;
|
||||
use objc2::runtime::AnyObject;
|
||||
|
||||
pub type CGDisplayFadeInterval = f32;
|
||||
pub type CGDisplayReservationInterval = f32;
|
||||
|
|
|
|||
|
|
@ -20,12 +20,8 @@ pub fn initialize(app: &NSApplication) {
|
|||
|
||||
// About menu item
|
||||
let about_item_title = ns_string!("About ").stringByAppendingString(&process_name);
|
||||
let about_item = menu_item(
|
||||
mtm,
|
||||
&about_item_title,
|
||||
Some(sel!(orderFrontStandardAboutPanel:)),
|
||||
None,
|
||||
);
|
||||
let about_item =
|
||||
menu_item(mtm, &about_item_title, Some(sel!(orderFrontStandardAboutPanel:)), None);
|
||||
|
||||
// Services menu item
|
||||
let services_menu = NSMenu::new(mtm);
|
||||
|
|
@ -41,10 +37,7 @@ pub fn initialize(app: &NSApplication) {
|
|||
mtm,
|
||||
&hide_item_title,
|
||||
Some(sel!(hide:)),
|
||||
Some(KeyEquivalent {
|
||||
key: ns_string!("h"),
|
||||
masks: None,
|
||||
}),
|
||||
Some(KeyEquivalent { key: ns_string!("h"), masks: None }),
|
||||
);
|
||||
|
||||
// Hide other applications menu item
|
||||
|
|
@ -64,12 +57,8 @@ pub fn initialize(app: &NSApplication) {
|
|||
|
||||
// Show applications menu item
|
||||
let show_all_item_title = ns_string!("Show All");
|
||||
let show_all_item = menu_item(
|
||||
mtm,
|
||||
show_all_item_title,
|
||||
Some(sel!(unhideAllApplications:)),
|
||||
None,
|
||||
);
|
||||
let show_all_item =
|
||||
menu_item(mtm, show_all_item_title, Some(sel!(unhideAllApplications:)), None);
|
||||
|
||||
// Separator menu item
|
||||
let sep = NSMenuItem::separatorItem(mtm);
|
||||
|
|
@ -80,10 +69,7 @@ pub fn initialize(app: &NSApplication) {
|
|||
mtm,
|
||||
&quit_item_title,
|
||||
Some(sel!(terminate:)),
|
||||
Some(KeyEquivalent {
|
||||
key: ns_string!("q"),
|
||||
masks: None,
|
||||
}),
|
||||
Some(KeyEquivalent { key: ns_string!("q"), masks: None }),
|
||||
);
|
||||
|
||||
app_menu.addItem(&about_item);
|
||||
|
|
|
|||
|
|
@ -17,16 +17,14 @@ mod window_delegate;
|
|||
|
||||
use std::fmt;
|
||||
|
||||
pub(crate) use self::{
|
||||
event::{physicalkey_to_scancode, scancode_to_physicalkey, KeyEventExtra},
|
||||
event_loop::{
|
||||
ActiveEventLoop, EventLoop, EventLoopProxy, OwnedDisplayHandle,
|
||||
PlatformSpecificEventLoopAttributes,
|
||||
},
|
||||
monitor::{MonitorHandle, VideoModeHandle},
|
||||
window::WindowId,
|
||||
window_delegate::PlatformSpecificWindowAttributes,
|
||||
pub(crate) use self::event::{physicalkey_to_scancode, scancode_to_physicalkey, KeyEventExtra};
|
||||
pub(crate) use self::event_loop::{
|
||||
ActiveEventLoop, EventLoop, EventLoopProxy, OwnedDisplayHandle,
|
||||
PlatformSpecificEventLoopAttributes,
|
||||
};
|
||||
pub(crate) use self::monitor::{MonitorHandle, VideoModeHandle};
|
||||
pub(crate) use self::window::WindowId;
|
||||
pub(crate) use self::window_delegate::PlatformSpecificWindowAttributes;
|
||||
use crate::event::DeviceId as RootDeviceId;
|
||||
|
||||
pub(crate) use self::cursor::CustomCursor as PlatformCustomCursor;
|
||||
|
|
|
|||
|
|
@ -1,16 +1,16 @@
|
|||
#![allow(clippy::unnecessary_cast)]
|
||||
|
||||
use std::{collections::VecDeque, fmt};
|
||||
use std::collections::VecDeque;
|
||||
use std::fmt;
|
||||
|
||||
use core_foundation::{
|
||||
array::{CFArrayGetCount, CFArrayGetValueAtIndex},
|
||||
base::{CFRelease, TCFType},
|
||||
string::CFString,
|
||||
};
|
||||
use core_foundation::array::{CFArrayGetCount, CFArrayGetValueAtIndex};
|
||||
use core_foundation::base::{CFRelease, TCFType};
|
||||
use core_foundation::string::CFString;
|
||||
use core_graphics::display::{
|
||||
CGDirectDisplayID, CGDisplay, CGDisplayBounds, CGDisplayCopyDisplayMode,
|
||||
};
|
||||
use objc2::{rc::Id, runtime::AnyObject};
|
||||
use objc2::rc::Id;
|
||||
use objc2::runtime::AnyObject;
|
||||
use objc2_app_kit::NSScreen;
|
||||
use objc2_foundation::{ns_string, run_on_main, MainThreadMarker, NSNumber, NSPoint, NSRect};
|
||||
|
||||
|
|
@ -233,9 +233,7 @@ impl MonitorHandle {
|
|||
return None;
|
||||
}
|
||||
|
||||
(time.time_scale as i64)
|
||||
.checked_div(time.time_value)
|
||||
.map(|v| (v * 1000) as u32)
|
||||
(time.time_scale as i64).checked_div(time.time_value).map(|v| (v * 1000) as u32)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,8 @@
|
|||
use std::{
|
||||
ffi::c_void,
|
||||
panic::{AssertUnwindSafe, UnwindSafe},
|
||||
ptr,
|
||||
rc::Weak,
|
||||
time::Instant,
|
||||
};
|
||||
use std::ffi::c_void;
|
||||
use std::panic::{AssertUnwindSafe, UnwindSafe};
|
||||
use std::ptr;
|
||||
use std::rc::Weak;
|
||||
use std::time::Instant;
|
||||
|
||||
use core_foundation::base::{CFIndex, CFOptionFlags, CFRelease};
|
||||
use core_foundation::date::CFAbsoluteTimeGetCurrent;
|
||||
|
|
@ -17,11 +15,9 @@ use core_foundation::runloop::{
|
|||
};
|
||||
use objc2_foundation::MainThreadMarker;
|
||||
|
||||
use super::app_delegate::ApplicationDelegate;
|
||||
use super::event_loop::{stop_app_on_panic, PanicInfo};
|
||||
use super::ffi;
|
||||
use super::{
|
||||
app_delegate::ApplicationDelegate,
|
||||
event_loop::{stop_app_on_panic, PanicInfo},
|
||||
};
|
||||
|
||||
unsafe fn control_flow_handler<F>(panic_info: *mut c_void, f: F)
|
||||
where
|
||||
|
|
@ -55,10 +51,10 @@ extern "C" fn control_flow_begin_handler(
|
|||
#[allow(non_upper_case_globals)]
|
||||
match activity {
|
||||
kCFRunLoopAfterWaiting => {
|
||||
//trace!("Triggered `CFRunLoopAfterWaiting`");
|
||||
// trace!("Triggered `CFRunLoopAfterWaiting`");
|
||||
ApplicationDelegate::get(MainThreadMarker::new().unwrap()).wakeup(panic_info);
|
||||
//trace!("Completed `CFRunLoopAfterWaiting`");
|
||||
}
|
||||
// trace!("Completed `CFRunLoopAfterWaiting`");
|
||||
},
|
||||
_ => unreachable!(),
|
||||
}
|
||||
});
|
||||
|
|
@ -77,11 +73,11 @@ extern "C" fn control_flow_end_handler(
|
|||
#[allow(non_upper_case_globals)]
|
||||
match activity {
|
||||
kCFRunLoopBeforeWaiting => {
|
||||
//trace!("Triggered `CFRunLoopBeforeWaiting`");
|
||||
// trace!("Triggered `CFRunLoopBeforeWaiting`");
|
||||
ApplicationDelegate::get(MainThreadMarker::new().unwrap()).cleared(panic_info);
|
||||
//trace!("Completed `CFRunLoopBeforeWaiting`");
|
||||
}
|
||||
kCFRunLoopExit => (), //unimplemented!(), // not expected to ever happen
|
||||
// trace!("Completed `CFRunLoopBeforeWaiting`");
|
||||
},
|
||||
kCFRunLoopExit => (), // unimplemented!(), // not expected to ever happen
|
||||
_ => unreachable!(),
|
||||
}
|
||||
});
|
||||
|
|
@ -186,11 +182,7 @@ impl Default for EventLoopWaker {
|
|||
ptr::null_mut(),
|
||||
);
|
||||
CFRunLoopAddTimer(CFRunLoopGetMain(), timer, kCFRunLoopCommonModes);
|
||||
EventLoopWaker {
|
||||
timer,
|
||||
start_instant: Instant::now(),
|
||||
next_fire_date: None,
|
||||
}
|
||||
EventLoopWaker { timer, start_instant: Instant::now(), next_fire_date: None }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -215,7 +207,7 @@ impl EventLoopWaker {
|
|||
match instant {
|
||||
Some(instant) if now >= instant => {
|
||||
self.start();
|
||||
}
|
||||
},
|
||||
Some(instant) => {
|
||||
if self.next_fire_date != Some(instant) {
|
||||
self.next_fire_date = Some(instant);
|
||||
|
|
@ -227,10 +219,10 @@ impl EventLoopWaker {
|
|||
CFRunLoopTimerSetNextFireDate(self.timer, current + fsecs)
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
None => {
|
||||
self.stop();
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,7 @@
|
|||
use objc2_foundation::{NSNotFound, NSRange, NSUInteger};
|
||||
use tracing::trace;
|
||||
|
||||
pub static EMPTY_RANGE: NSRange = NSRange {
|
||||
location: NSNotFound as NSUInteger,
|
||||
length: 0,
|
||||
};
|
||||
pub static EMPTY_RANGE: NSRange = NSRange { location: NSNotFound as NSUInteger, length: 0 };
|
||||
|
||||
macro_rules! trace_scope {
|
||||
($s:literal) => {
|
||||
|
|
@ -21,20 +18,13 @@ impl TraceGuard {
|
|||
#[inline]
|
||||
pub(crate) fn new(module_path: &'static str, called_from_fn: &'static str) -> Self {
|
||||
trace!(target = module_path, "Triggered `{}`", called_from_fn);
|
||||
Self {
|
||||
module_path,
|
||||
called_from_fn,
|
||||
}
|
||||
Self { module_path, called_from_fn }
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for TraceGuard {
|
||||
#[inline]
|
||||
fn drop(&mut self) {
|
||||
trace!(
|
||||
target = self.module_path,
|
||||
"Completed `{}`",
|
||||
self.called_from_fn
|
||||
);
|
||||
trace!(target = self.module_path, "Completed `{}`", self.called_from_fn);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,15 +26,13 @@ use super::event::{
|
|||
};
|
||||
use super::window::WinitWindow;
|
||||
use super::{util, DEVICE_ID};
|
||||
use crate::{
|
||||
dpi::{LogicalPosition, LogicalSize},
|
||||
event::{
|
||||
DeviceEvent, ElementState, Ime, Modifiers, MouseButton, MouseScrollDelta, TouchPhase,
|
||||
WindowEvent,
|
||||
},
|
||||
keyboard::{Key, KeyCode, KeyLocation, ModifiersState, NamedKey},
|
||||
platform::macos::OptionAsAlt,
|
||||
use crate::dpi::{LogicalPosition, LogicalSize};
|
||||
use crate::event::{
|
||||
DeviceEvent, ElementState, Ime, Modifiers, MouseButton, MouseScrollDelta, TouchPhase,
|
||||
WindowEvent,
|
||||
};
|
||||
use crate::keyboard::{Key, KeyCode, KeyLocation, ModifiersState, NamedKey};
|
||||
use crate::platform::macos::OptionAsAlt;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct CursorState {
|
||||
|
|
@ -44,10 +42,7 @@ struct CursorState {
|
|||
|
||||
impl Default for CursorState {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
visible: true,
|
||||
cursor: default_cursor(),
|
||||
}
|
||||
Self { visible: true, cursor: default_cursor() }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -819,10 +814,7 @@ impl WinitView {
|
|||
// (which is incompatible with `frameDidChange:`)
|
||||
//
|
||||
// unsafe { msg_send_id![self, window] }
|
||||
self.ivars()
|
||||
._ns_window
|
||||
.load()
|
||||
.expect("view to have a window")
|
||||
self.ivars()._ns_window.load().expect("view to have a window")
|
||||
}
|
||||
|
||||
fn queue_event(&self, event: WindowEvent) {
|
||||
|
|
@ -950,9 +942,7 @@ impl WinitView {
|
|||
let location_mask = ModLocationMask::from_location(event.location);
|
||||
|
||||
let mut phys_mod_state = self.ivars().phys_modifiers.borrow_mut();
|
||||
let phys_mod = phys_mod_state
|
||||
.entry(key)
|
||||
.or_insert(ModLocationMask::empty());
|
||||
let phys_mod = phys_mod_state.entry(key).or_insert(ModLocationMask::empty());
|
||||
|
||||
let is_active = current_modifiers.state().contains(event_modifier);
|
||||
let mut events = VecDeque::with_capacity(2);
|
||||
|
|
@ -1102,9 +1092,7 @@ fn replace_event(event: &NSEvent, option_as_alt: OptionAsAlt) -> Id<NSEvent> {
|
|||
|
||||
if ignore_alt_characters {
|
||||
let ns_chars = unsafe {
|
||||
event
|
||||
.charactersIgnoringModifiers()
|
||||
.expect("expected characters to be non-null")
|
||||
event.charactersIgnoringModifiers().expect("expected characters to be non-null")
|
||||
};
|
||||
|
||||
unsafe {
|
||||
|
|
|
|||
|
|
@ -18,8 +18,7 @@ pub(crate) struct Window {
|
|||
|
||||
impl Drop for Window {
|
||||
fn drop(&mut self) {
|
||||
self.window
|
||||
.get_on_main(|window| autoreleasepool(|_| window.close()))
|
||||
self.window.get_on_main(|window| autoreleasepool(|_| window.close()))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -65,9 +64,7 @@ impl Window {
|
|||
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(),
|
||||
))
|
||||
Ok(rwh_06::RawDisplayHandle::AppKit(rwh_06::AppKitDisplayHandle::new()))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -448,7 +448,7 @@ fn new_window(attrs: &WindowAttributes, mtm: MainThreadMarker) -> Option<Id<Wini
|
|||
Some(Fullscreen::Borderless(Some(monitor)))
|
||||
| Some(Fullscreen::Exclusive(VideoModeHandle { monitor, .. })) => {
|
||||
monitor.ns_screen(mtm).or_else(|| NSScreen::mainScreen(mtm))
|
||||
}
|
||||
},
|
||||
Some(Fullscreen::Borderless(None)) => NSScreen::mainScreen(mtm),
|
||||
None => None,
|
||||
};
|
||||
|
|
@ -462,7 +462,7 @@ fn new_window(attrs: &WindowAttributes, mtm: MainThreadMarker) -> Option<Id<Wini
|
|||
Some(size) => {
|
||||
let size = size.to_logical(scale_factor);
|
||||
NSSize::new(size.width, size.height)
|
||||
}
|
||||
},
|
||||
None => NSSize::new(800.0, 600.0),
|
||||
};
|
||||
let position = match attrs.position {
|
||||
|
|
@ -472,12 +472,12 @@ fn new_window(attrs: &WindowAttributes, mtm: MainThreadMarker) -> Option<Id<Wini
|
|||
NSPoint::new(position.x, position.y),
|
||||
size,
|
||||
))
|
||||
}
|
||||
},
|
||||
// This value is ignored by calling win.center() below
|
||||
None => NSPoint::new(0.0, 0.0),
|
||||
};
|
||||
NSRect::new(position, size)
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
let mut masks = if (!attrs.decorations && screen.is_none())
|
||||
|
|
@ -631,30 +631,27 @@ impl WindowDelegate {
|
|||
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"
|
||||
))
|
||||
os_error!(OsError::CreationError("parent view should be installed in a window"))
|
||||
})?;
|
||||
|
||||
// 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.
|
||||
// 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.
|
||||
unsafe {
|
||||
parent.addChildWindow_ordered(&window, NSWindowOrderingMode::NSWindowAbove)
|
||||
};
|
||||
}
|
||||
},
|
||||
Some(raw) => panic!("invalid raw window handle {raw:?} on macOS"),
|
||||
None => (),
|
||||
}
|
||||
|
||||
let resize_increments = match attrs
|
||||
.resize_increments
|
||||
.map(|i| i.to_logical(window.backingScaleFactor() as _))
|
||||
{
|
||||
Some(LogicalSize { width, height }) if width >= 1. && height >= 1. => {
|
||||
NSSize::new(width, height)
|
||||
}
|
||||
_ => NSSize::new(1., 1.),
|
||||
};
|
||||
let resize_increments =
|
||||
match attrs.resize_increments.map(|i| i.to_logical(window.backingScaleFactor() as _)) {
|
||||
Some(LogicalSize { width, height }) if width >= 1. && height >= 1. => {
|
||||
NSSize::new(width, height)
|
||||
},
|
||||
_ => NSSize::new(1., 1.),
|
||||
};
|
||||
|
||||
let scale_factor = window.backingScaleFactor() as _;
|
||||
|
||||
|
|
@ -771,10 +768,7 @@ impl WindowDelegate {
|
|||
};
|
||||
|
||||
self.ivars().previous_scale_factor.set(scale_factor);
|
||||
let content_size = self
|
||||
.window()
|
||||
.contentRectForFrameRect(self.window().frame())
|
||||
.size;
|
||||
let content_size = self.window().contentRectForFrameRect(self.window().frame()).size;
|
||||
let content_size = LogicalSize::new(content_size.width, content_size.height);
|
||||
|
||||
let app_delegate = ApplicationDelegate::get(MainThreadMarker::from(self));
|
||||
|
|
@ -885,26 +879,20 @@ impl WindowDelegate {
|
|||
pub fn request_inner_size(&self, size: Size) -> Option<PhysicalSize<u32>> {
|
||||
let scale_factor = self.scale_factor();
|
||||
let size = size.to_logical(scale_factor);
|
||||
self.window()
|
||||
.setContentSize(NSSize::new(size.width, size.height));
|
||||
self.window().setContentSize(NSSize::new(size.width, size.height));
|
||||
None
|
||||
}
|
||||
|
||||
pub fn set_min_inner_size(&self, dimensions: Option<Size>) {
|
||||
let dimensions = dimensions.unwrap_or(Size::Logical(LogicalSize {
|
||||
width: 0.0,
|
||||
height: 0.0,
|
||||
}));
|
||||
let dimensions =
|
||||
dimensions.unwrap_or(Size::Logical(LogicalSize { width: 0.0, height: 0.0 }));
|
||||
let min_size = dimensions.to_logical::<CGFloat>(self.scale_factor());
|
||||
|
||||
let min_size = NSSize::new(min_size.width, min_size.height);
|
||||
unsafe { self.window().setContentMinSize(min_size) };
|
||||
|
||||
// If necessary, resize the window to match constraint
|
||||
let mut current_size = self
|
||||
.window()
|
||||
.contentRectForFrameRect(self.window().frame())
|
||||
.size;
|
||||
let mut current_size = self.window().contentRectForFrameRect(self.window().frame()).size;
|
||||
if current_size.width < min_size.width {
|
||||
current_size.width = min_size.width;
|
||||
}
|
||||
|
|
@ -926,10 +914,7 @@ impl WindowDelegate {
|
|||
unsafe { self.window().setContentMaxSize(max_size) };
|
||||
|
||||
// If necessary, resize the window to match constraint
|
||||
let mut current_size = self
|
||||
.window()
|
||||
.contentRectForFrameRect(self.window().frame())
|
||||
.size;
|
||||
let mut current_size = self.window().contentRectForFrameRect(self.window().frame()).size;
|
||||
if max_size.width < current_size.width {
|
||||
current_size.width = max_size.width;
|
||||
}
|
||||
|
|
@ -1013,9 +998,7 @@ impl WindowDelegate {
|
|||
// 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`).
|
||||
if let Some(button) = self
|
||||
.window()
|
||||
.standardWindowButton(NSWindowButton::NSWindowZoomButton)
|
||||
if let Some(button) = self.window().standardWindowButton(NSWindowButton::NSWindowZoomButton)
|
||||
{
|
||||
button.setEnabled(buttons.contains(WindowButtons::MAXIMIZE));
|
||||
}
|
||||
|
|
@ -1064,7 +1047,7 @@ impl WindowDelegate {
|
|||
CursorGrabMode::None => true,
|
||||
CursorGrabMode::Confined => {
|
||||
return Err(ExternalError::NotSupported(NotSupportedError::new()))
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
// TODO: Do this for real https://stackoverflow.com/a/40922095/5435443
|
||||
|
|
@ -1107,9 +1090,7 @@ impl WindowDelegate {
|
|||
#[inline]
|
||||
pub fn drag_window(&self) -> Result<(), ExternalError> {
|
||||
let mtm = MainThreadMarker::from(self);
|
||||
let event = NSApplication::sharedApplication(mtm)
|
||||
.currentEvent()
|
||||
.unwrap();
|
||||
let event = NSApplication::sharedApplication(mtm).currentEvent().unwrap();
|
||||
self.window().performWindowDragWithEvent(&event);
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -1151,12 +1132,8 @@ impl WindowDelegate {
|
|||
}
|
||||
|
||||
fn saved_style(&self) -> NSWindowStyleMask {
|
||||
let base_mask = self
|
||||
.ivars()
|
||||
.saved_style
|
||||
.take()
|
||||
.unwrap_or_else(|| self.window().styleMask())
|
||||
.0;
|
||||
let base_mask =
|
||||
self.ivars().saved_style.take().unwrap_or_else(|| self.window().styleMask()).0;
|
||||
NSWindowStyleMask(if self.ivars().resizable.get() {
|
||||
base_mask | NSWindowStyleMask::Resizable.0
|
||||
} else {
|
||||
|
|
@ -1225,10 +1202,7 @@ impl WindowDelegate {
|
|||
let screen = NSScreen::mainScreen(mtm).expect("no screen found");
|
||||
screen.visibleFrame()
|
||||
} else {
|
||||
self.ivars()
|
||||
.standard_frame
|
||||
.get()
|
||||
.unwrap_or(DEFAULT_STANDARD_FRAME)
|
||||
self.ivars().standard_frame.get().unwrap_or(DEFAULT_STANDARD_FRAME)
|
||||
};
|
||||
self.window().setFrame_display(new_rect, false);
|
||||
}
|
||||
|
|
@ -1275,7 +1249,7 @@ impl WindowDelegate {
|
|||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
},
|
||||
Fullscreen::Exclusive(video_mode) => video_mode.monitor(),
|
||||
}
|
||||
.ns_screen(mtm)
|
||||
|
|
@ -1305,9 +1279,7 @@ impl WindowDelegate {
|
|||
let mut fade_token = ffi::kCGDisplayFadeReservationInvalidToken;
|
||||
|
||||
if matches!(old_fullscreen, Some(Fullscreen::Borderless(_))) {
|
||||
self.ivars()
|
||||
.save_presentation_opts
|
||||
.replace(Some(app.presentationOptions()));
|
||||
self.ivars().save_presentation_opts.replace(Some(app.presentationOptions()));
|
||||
}
|
||||
|
||||
unsafe {
|
||||
|
|
@ -1380,11 +1352,11 @@ impl WindowDelegate {
|
|||
self.ivars().saved_style.set(Some(curr_mask));
|
||||
}
|
||||
toggle_fullscreen(self.window());
|
||||
}
|
||||
},
|
||||
(Some(Fullscreen::Borderless(_)), None) => {
|
||||
// State is restored by `window_did_exit_fullscreen`
|
||||
toggle_fullscreen(self.window());
|
||||
}
|
||||
},
|
||||
(Some(Fullscreen::Exclusive(ref video_mode)), None) => {
|
||||
unsafe {
|
||||
ffi::CGRestorePermanentDisplayConfiguration();
|
||||
|
|
@ -1394,7 +1366,7 @@ impl WindowDelegate {
|
|||
);
|
||||
};
|
||||
toggle_fullscreen(self.window());
|
||||
}
|
||||
},
|
||||
(Some(Fullscreen::Borderless(_)), Some(Fullscreen::Exclusive(_))) => {
|
||||
// If we're already in fullscreen mode, calling
|
||||
// `CGDisplayCapture` will place the shielding window on top of
|
||||
|
|
@ -1404,9 +1376,7 @@ impl WindowDelegate {
|
|||
// 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:`.
|
||||
self.ivars()
|
||||
.save_presentation_opts
|
||||
.set(Some(app.presentationOptions()));
|
||||
self.ivars().save_presentation_opts.set(Some(app.presentationOptions()));
|
||||
|
||||
let presentation_options = NSApplicationPresentationOptions(
|
||||
NSApplicationPresentationOptions::NSApplicationPresentationFullScreen.0
|
||||
|
|
@ -1417,7 +1387,7 @@ impl WindowDelegate {
|
|||
|
||||
let window_level = unsafe { ffi::CGShieldingWindowLevel() } as NSWindowLevel + 1;
|
||||
self.window().setLevel(window_level);
|
||||
}
|
||||
},
|
||||
(Some(Fullscreen::Exclusive(ref video_mode)), Some(Fullscreen::Borderless(_))) => {
|
||||
let presentation_options = self.ivars().save_presentation_opts.get().unwrap_or(
|
||||
NSApplicationPresentationOptions(NSApplicationPresentationOptions::NSApplicationPresentationFullScreen.0
|
||||
|
|
@ -1436,10 +1406,9 @@ impl WindowDelegate {
|
|||
|
||||
// Restore the normal window level following the Borderless fullscreen
|
||||
// `CGShieldingWindowLevel() + 1` hack.
|
||||
self.window()
|
||||
.setLevel(ffi::kCGNormalWindowLevel as NSWindowLevel);
|
||||
}
|
||||
_ => {}
|
||||
self.window().setLevel(ffi::kCGNormalWindowLevel as NSWindowLevel);
|
||||
},
|
||||
_ => {},
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -1628,9 +1597,7 @@ impl WindowDelegate {
|
|||
pub fn set_theme(&self, theme: Option<Theme>) {
|
||||
let mtm = MainThreadMarker::from(self);
|
||||
set_ns_theme(theme, mtm);
|
||||
self.ivars()
|
||||
.current_theme
|
||||
.set(theme.or_else(|| Some(get_ns_theme(mtm))));
|
||||
self.ivars().current_theme.set(theme.or_else(|| Some(get_ns_theme(mtm))));
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
|
@ -1676,15 +1643,11 @@ impl WindowExtMacOS for WindowDelegate {
|
|||
if fullscreen {
|
||||
// Remember the original window's settings
|
||||
// Exclude title bar
|
||||
self.ivars().standard_frame.set(Some(
|
||||
self.window().contentRectForFrameRect(self.window().frame()),
|
||||
));
|
||||
self.ivars()
|
||||
.saved_style
|
||||
.set(Some(self.window().styleMask()));
|
||||
self.ivars()
|
||||
.save_presentation_opts
|
||||
.set(Some(app.presentationOptions()));
|
||||
.standard_frame
|
||||
.set(Some(self.window().contentRectForFrameRect(self.window().frame())));
|
||||
self.ivars().saved_style.set(Some(self.window().styleMask()));
|
||||
self.ivars().save_presentation_opts.set(Some(app.presentationOptions()));
|
||||
|
||||
// Tell our window's state that we're in fullscreen
|
||||
self.ivars().is_simple_fullscreen.set(true);
|
||||
|
|
@ -1700,10 +1663,7 @@ impl WindowExtMacOS for WindowDelegate {
|
|||
self.toggle_style_mask(NSWindowStyleMask::Titled, false);
|
||||
|
||||
// Set the window frame to the screen frame size
|
||||
let screen = self
|
||||
.window()
|
||||
.screen()
|
||||
.expect("expected screen to be available");
|
||||
let screen = self.window().screen().expect("expected screen to be available");
|
||||
self.window().setFrame_display(screen.frame(), true);
|
||||
|
||||
// Fullscreen windows can't be resized, minimized, or moved
|
||||
|
|
@ -1718,11 +1678,7 @@ impl WindowExtMacOS for WindowDelegate {
|
|||
self.ivars().is_simple_fullscreen.set(false);
|
||||
|
||||
let save_presentation_opts = self.ivars().save_presentation_opts.get();
|
||||
let frame = self
|
||||
.ivars()
|
||||
.standard_frame
|
||||
.get()
|
||||
.unwrap_or(DEFAULT_STANDARD_FRAME);
|
||||
let frame = self.ivars().standard_frame.get().unwrap_or(DEFAULT_STANDARD_FRAME);
|
||||
|
||||
if let Some(presentation_opts) = save_presentation_opts {
|
||||
app.setPresentationOptions(presentation_opts);
|
||||
|
|
@ -1747,8 +1703,7 @@ impl WindowExtMacOS for WindowDelegate {
|
|||
|
||||
#[inline]
|
||||
fn set_tabbing_identifier(&self, identifier: &str) {
|
||||
self.window()
|
||||
.setTabbingIdentifier(&NSString::from_str(identifier))
|
||||
self.window().setTabbingIdentifier(&NSString::from_str(identifier))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
|
@ -1779,9 +1734,7 @@ impl WindowExtMacOS for WindowDelegate {
|
|||
|
||||
#[inline]
|
||||
fn num_tabs(&self) -> usize {
|
||||
unsafe { self.window().tabbedWindows() }
|
||||
.map(|windows| windows.len())
|
||||
.unwrap_or(1)
|
||||
unsafe { self.window().tabbedWindows() }.map(|windows| windows.len()).unwrap_or(1)
|
||||
}
|
||||
|
||||
fn is_document_edited(&self) -> bool {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue