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(
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(
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.register()
@ -612,42 +612,25 @@ extern "C" fn window_did_resize(this: &Object, _: Sel, _: id) {
window_state.as_ref().borrow().move_traffic_light();
}
extern "C" fn window_did_become_key(this: &Object, _: Sel, _: id) {
let window_state = unsafe { get_window_state(this) };
window_state
.as_ref()
.borrow()
.executor
.spawn({
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_change_key_status(this: &Object, selector: Sel, _: id) {
let is_active = if selector == sel!(windowDidBecomeKey:) {
true
} else if selector == sel!(windowDidResignKey:) {
false
} else {
unreachable!();
};
extern "C" fn window_did_resign_key(this: &Object, _: Sel, _: id) {
let window_state = unsafe { get_window_state(this) };
window_state
.as_ref()
.borrow()
.executor
.spawn({
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(false);
window_state.borrow_mut().activate_callback = Some(callback);
};
}
let executor = window_state.as_ref().borrow().executor.clone();
executor
.spawn(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(is_active);
window_state.borrow_mut().activate_callback = Some(callback);
};
})
.detach();
}