[polari] js: Use event controllers where possible
- From: Marge Bot <marge-bot src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [polari] js: Use event controllers where possible
- Date: Fri, 3 Sep 2021 09:45:12 +0000 (UTC)
commit 2cdaec27dd42ea1167cd894deafce0a4dceb8f6a
Author: Florian Müllner <fmuellner gnome org>
Date: Thu Sep 24 00:52:16 2020 +0200
js: Use event controllers where possible
GTK4 removes all *-event signals and replaces them with event controllers
and gestures. Many of those are already available in GTK3, so start using
them where possible.
Part-of: <https://gitlab.gnome.org/GNOME/polari/-/merge_requests/225>
src/chatView.js | 42 +++++++++++++++++++++++-------------------
src/entryArea.js | 28 +++++++++++++++-------------
src/roomList.js | 28 ++++++++++++++--------------
src/tabCompletion.js | 10 ++++++----
4 files changed, 58 insertions(+), 50 deletions(-)
---
diff --git a/src/chatView.js b/src/chatView.js
index ba218f27..17d6d9fa 100644
--- a/src/chatView.js
+++ b/src/chatView.js
@@ -325,12 +325,19 @@ export default GObject.registerClass({
this.vadjustment.connect('notify::upper',
this._onUpperChanged.bind(this));
- this._view.connect('key-press-event', this._onKeyPress.bind(this));
- this._view.connect('motion-notify-event',
+ this._keyController = new Gtk.EventControllerKey({
+ widget: this._view,
+ });
+ this._keyController.connect('key-pressed', this._onKeyPressed.bind(this));
+
+ this._motionController = new Gtk.EventControllerMotion({
+ widget: this._view,
+ });
+ this._motionController.connect('motion',
this._handleButtonTagsHover.bind(this));
- this._view.connect('enter-notify-event',
+ this._motionController.connect('enter',
this._handleButtonTagsHover.bind(this));
- this._view.connect('leave-notify-event',
+ this._motionController.connect('leave',
this._handleButtonTagsHover.bind(this));
/* pick up DPI changes (e.g. via the 'text-scaling-factor' setting):
the default handler calls pango_cairo_context_set_resolution(), so
@@ -707,13 +714,8 @@ export default GObject.registerClass({
}
}
- _onScroll(w, event) {
- let [hasDir, dir] = event.get_scroll_direction();
- if (hasDir && dir !== Gdk.ScrollDirection.UP)
- return Gdk.EVENT_PROPAGATE;
-
- let [hasDeltas, dx_, dy] = event.get_scroll_deltas();
- if (hasDeltas && dy >= 0)
+ _onScroll(w, dx, dy) {
+ if (dy >= 0)
return Gdk.EVENT_PROPAGATE;
this._autoscroll = false;
@@ -721,9 +723,7 @@ export default GObject.registerClass({
return this._fetchBacklog();
}
- _onKeyPress(w, event) {
- let [, keyval] = event.get_keyval();
-
+ _onKeyPressed(c, keyval) {
if (keyval === Gdk.KEY_Home ||
keyval === Gdk.KEY_KP_Home) {
this._view.emit('move-cursor',
@@ -826,11 +826,15 @@ export default GObject.registerClass({
menu.popup_at_pointer(null);
}
- _handleButtonTagsHover(view, event) {
- let [, eventX, eventY] = event.get_coords();
- let [x, y] = view.window_to_buffer_coords(Gtk.TextWindowType.WIDGET,
- eventX, eventY);
- let [inside, iter] = view.get_iter_at_location(x, y);
+ _handleButtonTagsHover(controller, ...coords) {
+ let inside, iter;
+
+ if (coords.length > 0) {
+ const [eventX, eventY] = coords;
+ const [x, y] = this._view.window_to_buffer_coords(
+ Gtk.TextWindowType.WIDGET, eventX, eventY);
+ [inside, iter] = this._view.get_iter_at_location(x, y);
+ }
let hoveredButtonTags;
if (inside)
diff --git a/src/entryArea.js b/src/entryArea.js
index 1a2670c8..45948b8d 100644
--- a/src/entryArea.js
+++ b/src/entryArea.js
@@ -196,9 +196,12 @@ export default GObject.registerClass({
this.connect('destroy', this._onDestroy.bind(this));
this.connect('notify::sensitive', this._onSensitiveChanged.bind(this));
this.connect('realize', () => {
- this._toplevel = this.get_toplevel();
- this._keyPressId = this._toplevel.connect('key-press-event',
- this._onKeyPressEvent.bind(this));
+ this._toplevelKeyController = new Gtk.EventControllerKey({
+ widget: this.get_toplevel(),
+ propagation_phase: Gtk.PropagationPhase.CAPTURE,
+ });
+ this._toplevelKeyController.connect('key-pressed',
+ this._onKeyPressed.bind(this));
});
this.connect('map', () => {
this._nickButton.popover = EntryArea._nickPopover;
@@ -268,9 +271,10 @@ export default GObject.registerClass({
this._cancelButton.connect('clicked', this._onCancelClicked.bind(this));
this._pasteButton.connect('clicked', this._onPasteClicked.bind(this));
- this._pasteBox.connect_after('key-press-event', (w, event) => {
- let [, keyval] = event.get_keyval();
- let [, mods] = event.get_state();
+ this._pasteController = new Gtk.EventControllerKey({
+ widget: this._pasteBox,
+ });
+ this._pasteController.connect_after('key-pressed', (c, keyval, code, mods) => {
if (keyval === Gdk.KEY_Escape ||
keyval === Gdk.KEY_BackSpace ||
keyval === Gdk.KEY_Delete ||
@@ -327,15 +331,13 @@ export default GObject.registerClass({
!(toplevelFocus instanceof Gtk.Entry);
}
- _onKeyPressEvent(w, event) {
+ _onKeyPressed(controller, keyval, keycode, state) {
if (!this._canFocusChatEntry())
return Gdk.EVENT_PROPAGATE;
- let [, keyval] = event.get_keyval();
if (Gdk.keyval_to_unicode(keyval) === 0)
return Gdk.EVENT_PROPAGATE;
- let [, state] = event.get_state();
if (state !== 0 && state !== Gdk.ModifierType.SHIFT_MASK)
return Gdk.EVENT_PROPAGATE;
@@ -349,7 +351,7 @@ export default GObject.registerClass({
return Gdk.EVENT_PROPAGATE;
this._chatEntry.grab_focus_without_selecting();
- this._chatEntry.event(event);
+ this._chatEntry.event(Gtk.get_current_event());
return Gdk.EVENT_STOP;
}
@@ -513,11 +515,11 @@ export default GObject.registerClass({
if (this._channelChangedId)
this._room.disconnect(this._channelChangedId);
this._channelChangedId = 0;
- if (this._keyPressId)
- this._toplevel.disconnect(this._keyPressId);
- this._keyPressId = 0;
if (this._nickChangedId)
EntryArea._nickPopover.disconnect(this._nickChangedId);
this._nickChangedId = 0;
+ if (this._toplevelKeyController)
+ this._toplevelKeyController.run_dispose();
+ this._toplevelKeyController = null;
}
});
diff --git a/src/roomList.js b/src/roomList.js
index 5e03e628..174ede3d 100644
--- a/src/roomList.js
+++ b/src/roomList.js
@@ -49,10 +49,17 @@ const RoomRow = GObject.registerClass({
this._icon.visible = room.icon !== null;
- this._eventBox.connect('button-release-event',
- this._onButtonRelease.bind(this));
- this.connect('key-press-event',
- this._onKeyPress.bind(this));
+ this._keyController = new Gtk.EventControllerKey({
+ widget: this,
+ });
+ this._keyController.connect('key-pressed', this._onKeyPressed.bind(this));
+
+ this._clickGesture = new Gtk.GestureMultiPress({
+ widget: this._eventBox,
+ button: Gdk.BUTTON_SECONDARY,
+ });
+ this._clickGesture.connect('released',
+ this._onButtonReleased.bind(this));
room.bind_property('display-name',
this._roomLabel, 'label',
@@ -194,19 +201,12 @@ const RoomRow = GObject.registerClass({
this._updatePending();
}
- _onButtonRelease(w, event) {
- let [, button] = event.get_button();
- if (button !== Gdk.BUTTON_SECONDARY)
- return Gdk.EVENT_PROPAGATE;
-
+ _onButtonReleased(controller) {
+ controller.set_state(Gtk.EventSequenceState.CLAIMED);
this._showPopover();
-
- return Gdk.EVENT_STOP;
}
- _onKeyPress(w, event) {
- let [, keyval] = event.get_keyval();
- let [, mods] = event.get_state();
+ _onKeyPressed(controller, keyval, keycode, mods) {
if (keyval !== Gdk.KEY_Menu &&
!(keyval === Gdk.KEY_F10 &&
mods & Gdk.ModifierType.SHIFT_MASK))
diff --git a/src/tabCompletion.js b/src/tabCompletion.js
index 63bb626c..26386366 100644
--- a/src/tabCompletion.js
+++ b/src/tabCompletion.js
@@ -10,8 +10,12 @@ export default class TabCompletion {
this._canComplete = false;
this._key = '';
- this._entry.connect('key-press-event', this._onKeyPress.bind(this));
+ this._keyController = new Gtk.EventControllerKey({
+ widget: this._entry,
+ });
+ this._keyController.connect('key-pressed', this._onKeyPressed.bind(this));
this._entry.connect('focus-out-event', this._cancel.bind(this));
+
this._entry.connect('unmap', this._cancel.bind(this));
this._popup = new Gtk.Popover({
@@ -114,9 +118,7 @@ export default class TabCompletion {
this._canComplete = completions.length > 0;
}
- _onKeyPress(w, event) {
- let [, keyval] = event.get_keyval();
-
+ _onKeyPressed(controller, keyval) {
if (this._key.length === 0) {
if (keyval === Gdk.KEY_Tab) {
this._start();
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]