Remove duplication between windowDid{Become,Resign}Key callbacks

This commit is contained in:
Max Brunsfeld 2022-04-22 15:06:50 -07:00
parent 6b9e93ac6d
commit e8d263274c

View File

@ -75,11 +75,11 @@ unsafe fn build_classes() {
); );
decl.add_method( decl.add_method(
sel!(windowDidBecomeKey:), sel!(windowDidBecomeKey:),
window_did_become_key as extern "C" fn(&Object, Sel, id), window_did_change_key_status as extern "C" fn(&Object, Sel, id),
); );
decl.add_method( decl.add_method(
sel!(windowDidResignKey:), sel!(windowDidResignKey:),
window_did_resign_key as extern "C" fn(&Object, Sel, id), window_did_change_key_status as extern "C" fn(&Object, Sel, id),
); );
decl.add_method(sel!(close), close_window as extern "C" fn(&Object, Sel)); decl.add_method(sel!(close), close_window as extern "C" fn(&Object, Sel));
decl.register() decl.register()
@ -612,42 +612,25 @@ extern "C" fn window_did_resize(this: &Object, _: Sel, _: id) {
window_state.as_ref().borrow().move_traffic_light(); window_state.as_ref().borrow().move_traffic_light();
} }
extern "C" fn window_did_become_key(this: &Object, _: Sel, _: id) { extern "C" fn window_did_change_key_status(this: &Object, selector: Sel, _: id) {
let window_state = unsafe { get_window_state(this) }; let is_active = if selector == sel!(windowDidBecomeKey:) {
window_state true
.as_ref() } else if selector == sel!(windowDidResignKey:) {
.borrow() false
.executor } else {
.spawn({ unreachable!();
let window_state = window_state.clone(); };
async move {
let mut window_state_borrow = window_state.as_ref().borrow_mut();
if let Some(mut callback) = window_state_borrow.activate_callback.take() {
drop(window_state_borrow);
callback(true);
window_state.borrow_mut().activate_callback = Some(callback);
};
}
})
.detach();
}
extern "C" fn window_did_resign_key(this: &Object, _: Sel, _: id) {
let window_state = unsafe { get_window_state(this) }; let window_state = unsafe { get_window_state(this) };
window_state let executor = window_state.as_ref().borrow().executor.clone();
.as_ref() executor
.borrow() .spawn(async move {
.executor let mut window_state_borrow = window_state.as_ref().borrow_mut();
.spawn({ if let Some(mut callback) = window_state_borrow.activate_callback.take() {
let window_state = window_state.clone(); drop(window_state_borrow);
async move { callback(is_active);
let mut window_state_borrow = window_state.as_ref().borrow_mut(); window_state.borrow_mut().activate_callback = Some(callback);
if let Some(mut callback) = window_state_borrow.activate_callback.take() { };
drop(window_state_borrow);
callback(false);
window_state.borrow_mut().activate_callback = Some(callback);
};
}
}) })
.detach(); .detach();
} }