popupMenu: Replace event handlers with click action

Actions provide a higher-level API than event handlers, not unlike
GTK's event controllers (albeit less complete). Allowing them to
take care of the low-level bits where possible is generally a good
idea.

Menu items are a very straight-forward case, with a good amount
of code saving, so port them over.

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2742>
This commit is contained in:
Florian Müllner 2023-04-19 14:11:09 +02:00 committed by Marge Bot
parent cd862aa53e
commit a9ba9b7a47

View File

@ -97,6 +97,19 @@ var PopupBaseMenuItem = GObject.registerClass({
this._activatable = params.reactive && params.activate;
this._sensitive = true;
this._clickAction = new Clutter.ClickAction({
enabled: this._activatable,
});
this._clickAction.connect('clicked',
() => this.activate(Clutter.get_current_event()));
this._clickAction.connect('notify::pressed', () => {
if (this._clickAction.pressed)
this.add_style_pseudo_class('active');
else
this.remove_style_pseudo_class('active');
});
this.add_action(this._clickAction);
if (!this._activatable)
this.add_style_class_name('popup-inactive-menu-item');
@ -124,44 +137,6 @@ var PopupBaseMenuItem = GObject.registerClass({
this._parent = parent;
}
vfunc_button_press_event() {
if (!this._activatable)
return Clutter.EVENT_PROPAGATE;
// This is the CSS active state
this.add_style_pseudo_class('active');
return Clutter.EVENT_PROPAGATE;
}
vfunc_button_release_event() {
if (!this._activatable)
return Clutter.EVENT_PROPAGATE;
this.remove_style_pseudo_class('active');
// Pointer left the item during the grab
if (!this.hover)
return Clutter.EVENT_PROPAGATE;
this.activate(Clutter.get_current_event());
return Clutter.EVENT_STOP;
}
vfunc_touch_event(touchEvent) {
if (!this._activatable)
return Clutter.EVENT_PROPAGATE;
if (touchEvent.type == Clutter.EventType.TOUCH_END) {
this.remove_style_pseudo_class('active');
this.activate(Clutter.get_current_event());
return Clutter.EVENT_STOP;
} else if (touchEvent.type == Clutter.EventType.TOUCH_BEGIN) {
// This is the CSS active state
this.add_style_pseudo_class('active');
}
return Clutter.EVENT_PROPAGATE;
}
vfunc_key_press_event(keyEvent) {
if (global.focus_manager.navigate_from_event(Clutter.get_current_event()))
return Clutter.EVENT_STOP;
@ -1267,25 +1242,7 @@ class PopupSubMenuMenuItem extends PopupBaseMenuItem {
}
activate(_event) {
this._setOpenState(true);
}
vfunc_button_release_event() {
// Since we override the parent, we need to manage what the parent does
// with the active style class
this.remove_style_pseudo_class('active');
this._setOpenState(!this._getOpenState());
return Clutter.EVENT_PROPAGATE;
}
vfunc_touch_event(touchEvent) {
if (touchEvent.type == Clutter.EventType.TOUCH_END) {
// Since we override the parent, we need to manage what the parent does
// with the active style class
this.remove_style_pseudo_class('active');
this._setOpenState(!this._getOpenState());
}
return Clutter.EVENT_PROPAGATE;
}
});