-
Notifications
You must be signed in to change notification settings - Fork 947
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
implement focusable attribute for Window #4124
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -132,6 +132,7 @@ pub struct ViewState { | |
|
||
marked_text: RefCell<Retained<NSMutableAttributedString>>, | ||
accepts_first_mouse: bool, | ||
focusable: Cell<bool>, | ||
|
||
/// The state of the `Option` as `Alt`. | ||
option_as_alt: Cell<OptionAsAlt>, | ||
|
@@ -777,13 +778,20 @@ define_class!( | |
trace_scope!("acceptsFirstMouse:"); | ||
self.ivars().accepts_first_mouse | ||
} | ||
|
||
#[method(shouldDelayWindowOrderingForEvent:)] | ||
fn should_delay_window_ordering_for_event(&self, _event: &NSEvent) -> bool { | ||
trace_scope!("shouldDelayWindowOrderingForEvent:"); | ||
!self.ivars().focusable.get() | ||
} | ||
} | ||
); | ||
|
||
impl WinitView { | ||
pub(super) fn new( | ||
app_state: &Rc<AppState>, | ||
accepts_first_mouse: bool, | ||
focusable: bool, | ||
option_as_alt: OptionAsAlt, | ||
mtm: MainThreadMarker, | ||
) -> Retained<Self> { | ||
|
@@ -801,6 +809,7 @@ impl WinitView { | |
forward_key_to_app: Default::default(), | ||
marked_text: Default::default(), | ||
accepts_first_mouse, | ||
focusable: focusable.into(), | ||
option_as_alt: Cell::new(option_as_alt), | ||
}); | ||
let this: Retained<Self> = unsafe { msg_send![super(this), init] }; | ||
|
@@ -884,6 +893,10 @@ impl WinitView { | |
input_context.invalidateCharacterCoordinates(); | ||
} | ||
|
||
pub(super) fn set_focusable(&self, focusable: bool) { | ||
self.ivars().focusable.set(focusable); | ||
} | ||
|
||
/// Reset modifiers and emit a synthetic ModifiersChanged event if deemed necessary. | ||
pub(super) fn reset_modifiers(&self) { | ||
if !self.ivars().modifiers.get().state().is_empty() { | ||
|
@@ -1029,6 +1042,12 @@ impl WinitView { | |
} | ||
|
||
fn mouse_click(&self, event: &NSEvent, button_state: ElementState) { | ||
if !self.ivars().focusable.get() { | ||
let mtm = MainThreadMarker::from(self); | ||
unsafe { | ||
NSApplication::sharedApplication(mtm).preventWindowOrdering(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: It'd be nice with a comment here detailing why |
||
} | ||
} | ||
let position = self.mouse_view_point(event).to_physical(self.scale_factor()); | ||
let button = mouse_button(event); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,6 +116,7 @@ pub(crate) struct State { | |
decorations: Cell<bool>, | ||
resizable: Cell<bool>, | ||
maximized: Cell<bool>, | ||
focusable: Cell<bool>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid this state, query it from view.ivars().focusable instead. |
||
|
||
/// Presentation options saved before entering `set_simple_fullscreen`, and | ||
/// restored upon exiting it. Also used when transitioning from Borderless to | ||
|
@@ -723,6 +724,7 @@ fn new_window( | |
let view = WinitView::new( | ||
app_state, | ||
attrs.platform_specific.accepts_first_mouse, | ||
attrs.focusable, | ||
attrs.platform_specific.option_as_alt, | ||
mtm, | ||
); | ||
|
@@ -829,6 +831,7 @@ impl WindowDelegate { | |
decorations: Cell::new(attrs.decorations), | ||
resizable: Cell::new(attrs.resizable), | ||
maximized: Cell::new(attrs.maximized), | ||
focusable: Cell::new(attrs.focusable), | ||
save_presentation_opts: Cell::new(None), | ||
initial_fullscreen: Cell::new(attrs.fullscreen.is_some()), | ||
fullscreen: RefCell::new(None), | ||
|
@@ -877,7 +880,7 @@ impl WindowDelegate { | |
// state, since otherwise we'll briefly see the window at normal size | ||
// before it transitions. | ||
if attrs.visible { | ||
if attrs.active { | ||
if attrs.active && attrs.focusable { | ||
// Tightly linked with `app_state::window_activation_hack` | ||
window.makeKeyAndOrderFront(None); | ||
} else { | ||
|
@@ -996,9 +999,14 @@ impl WindowDelegate { | |
} | ||
|
||
pub fn set_visible(&self, visible: bool) { | ||
match visible { | ||
true => self.window().makeKeyAndOrderFront(None), | ||
false => self.window().orderOut(None), | ||
if visible { | ||
if self.is_focusable() == Some(true) { | ||
self.window().makeKeyAndOrderFront(None); | ||
} else { | ||
self.window().orderFront(None); | ||
} | ||
} else { | ||
self.window().orderOut(None); | ||
} | ||
} | ||
|
||
|
@@ -1007,6 +1015,17 @@ impl WindowDelegate { | |
Some(self.window().isVisible()) | ||
} | ||
|
||
#[inline] | ||
pub fn set_focusable(&self, focusable: bool) { | ||
self.ivars().focusable.set(focusable); | ||
self.view().set_focusable(focusable); | ||
} | ||
|
||
#[inline] | ||
pub fn is_focusable(&self) -> Option<bool> { | ||
Some(self.ivars().focusable.get()) | ||
} | ||
|
||
pub fn request_redraw(&self) { | ||
self.ivars().app_state.queue_redraw(window_id(self.window())); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: I prefer
Cell::new
explicitly.