Skip to content

Commit

Permalink
macOS: Merge window and delegate state (#3391)
Browse files Browse the repository at this point in the history
Previously we had a sort of artificial split between these, but both were accessing each other's state, since it's really the same state!
It was especially difficult to follow what happens to the fullscreen state.
So instead, we basically merge the window and the delegate files.

This does unfortunately screw a bit with the git history, apologies to whoever reads this in the future!
  • Loading branch information
madsmtm authored Jan 14, 2024
1 parent c86b0da commit 14b418a
Show file tree
Hide file tree
Showing 5 changed files with 1,516 additions and 1,520 deletions.
2 changes: 1 addition & 1 deletion src/platform_impl/macos/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl PanicInfo {
#[derive(Debug)]
pub struct EventLoopWindowTarget {
delegate: Id<ApplicationDelegate>,
mtm: MainThreadMarker,
pub(super) mtm: MainThreadMarker,
}

impl EventLoopWindowTarget {
Expand Down
3 changes: 2 additions & 1 deletion src/platform_impl/macos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ pub(crate) use self::{
EventLoop, EventLoopProxy, EventLoopWindowTarget, PlatformSpecificEventLoopAttributes,
},
monitor::{MonitorHandle, VideoModeHandle},
window::{PlatformSpecificWindowBuilderAttributes, WindowId},
window::WindowId,
window_delegate::PlatformSpecificWindowBuilderAttributes,
};
use crate::event::DeviceId as RootDeviceId;

Expand Down
24 changes: 20 additions & 4 deletions src/platform_impl/macos/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use crate::{
WindowEvent,
},
keyboard::{Key, KeyCode, KeyLocation, ModifiersState, NamedKey},
platform::macos::{OptionAsAlt, WindowExtMacOS},
platform::macos::OptionAsAlt,
};

#[derive(Debug)]
Expand Down Expand Up @@ -141,6 +141,9 @@ pub struct ViewState {

// Weak reference because the window keeps a strong reference to the view
_ns_window: WeakId<WinitWindow>,

/// The state of the `Option` as `Alt`.
option_as_alt: Cell<OptionAsAlt>,
}

declare_class!(
Expand Down Expand Up @@ -437,7 +440,7 @@ declare_class!(
// Get the characters from the event.
let old_ime_state = self.ivars().ime_state.get();
self.ivars().forward_key_to_app.set(false);
let event = replace_event(event, self.window().option_as_alt());
let event = replace_event(event, self.option_as_alt());

// The `interpretKeyEvents` function might call
// `setMarkedText`, `insertText`, and `doCommandBySelector`.
Expand Down Expand Up @@ -483,7 +486,7 @@ declare_class!(
fn key_up(&self, event: &NSEvent) {
trace_scope!("keyUp:");

let event = replace_event(event, self.window().option_as_alt());
let event = replace_event(event, self.option_as_alt());
self.update_modifiers(&event, false);

// We want to send keyboard input when we are currently in the ground state.
Expand Down Expand Up @@ -759,11 +762,16 @@ declare_class!(
);

impl WinitView {
pub(super) fn new(window: &WinitWindow, accepts_first_mouse: bool) -> Id<Self> {
pub(super) fn new(
window: &WinitWindow,
accepts_first_mouse: bool,
option_as_alt: OptionAsAlt,
) -> Id<Self> {
let mtm = MainThreadMarker::from(window);
let this = mtm.alloc().set_ivars(ViewState {
accepts_first_mouse,
_ns_window: WeakId::new(&window.retain()),
option_as_alt: Cell::new(option_as_alt),
..Default::default()
});
let this: Id<Self> = unsafe { msg_send_id![super(this), init] };
Expand Down Expand Up @@ -883,6 +891,14 @@ impl WinitView {
}
}

pub(super) fn set_option_as_alt(&self, value: OptionAsAlt) {
self.ivars().option_as_alt.set(value)
}

pub(super) fn option_as_alt(&self) -> OptionAsAlt {
self.ivars().option_as_alt.get()
}

/// Update modifiers if `event` has something different
fn update_modifiers(&self, ns_event: &NSEvent, is_flags_changed_event: bool) {
use ElementState::{Pressed, Released};
Expand Down
Loading

0 comments on commit 14b418a

Please sign in to comment.