[gnome-mines/arnaudb/wip/gtk4: 5/39] Use GestureClick.
- From: Arnaud B. <arnaudb src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-mines/arnaudb/wip/gtk4: 5/39] Use GestureClick.
- Date: Sat, 26 Sep 2020 10:14:09 +0000 (UTC)
commit a7e07b1f21fb0d45a5f02bb21a23a59c25eb1ed9
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date: Mon Apr 13 22:33:55 2020 +0200
Use GestureClick.
src/gnome-mines.vala | 19 ++++++++++---------
src/minefield-view.vala | 20 +++++++++++---------
src/tile.vala | 31 ++++++++++++++++++++-----------
3 files changed, 41 insertions(+), 29 deletions(-)
---
diff --git a/src/gnome-mines.vala b/src/gnome-mines.vala
index 1309076..7632e8d 100644
--- a/src/gnome-mines.vala
+++ b/src/gnome-mines.vala
@@ -89,6 +89,7 @@ public class Mines : Gtk.Application
private AspectFrame new_game_screen;
private AspectFrame custom_game_screen;
private CssProvider theme_provider;
+ private GestureClick view_click_controller; // for keeping in memory
private const OptionEntry[] option_entries =
{
@@ -297,7 +298,10 @@ public class Mines : Gtk.Application
paused_box = (Box) ui_builder.get_object ("paused_box");
buttons_box = (Box) ui_builder.get_object ("buttons_box");
aspect_child = (Box) ui_builder.get_object ("aspect_child");
- paused_box.button_press_event.connect (view_button_press_event);
+
+ view_click_controller = new GestureClick (); // only reacts to left-click button
+ view_click_controller.pressed.connect (view_button_press_event);
+ paused_box.add_controller (view_click_controller);
minefield_overlay.add_overlay (paused_box);
@@ -538,17 +542,14 @@ public class Mines : Gtk.Application
window.present ();
}
- private bool view_button_press_event (Widget widget, Gdk.EventButton event)
+ private inline void view_button_press_event (GestureClick _view_click_controller, int n_press, double x,
double y)
{
/* Cancel pause on click */
- if (minefield.paused)
- {
- minefield.paused = false;
- pause_requested = false;
- return true;
- }
+ if (!minefield.paused)
+ return;
- return false;
+ minefield.paused = false;
+ pause_requested = false;
}
private void quit_cb ()
diff --git a/src/minefield-view.vala b/src/minefield-view.vala
index d399b95..61595b1 100644
--- a/src/minefield-view.vala
+++ b/src/minefield-view.vala
@@ -191,9 +191,9 @@ public class MinefieldView : Gtk.Grid
{
mines[i,j] = new Tile (i, j);
mines[i,j].show ();
- mines[i,j].tile_pressed.connect ((x, y, event) => { tile_pressed_cb (x, y, event); });
- mines[i,j].tile_released.connect ((x, y, event) => { tile_released_cb (x, y, event); });
- mines[i,j].tile_long_pressed.connect ((x, y) => { tile_long_pressed_cb (x, y); });
+ mines[i,j].tile_pressed.connect (tile_pressed_cb);
+ mines[i,j].tile_released.connect (tile_released_cb);
+ mines[i,j].tile_long_pressed.connect (tile_long_pressed_cb);
add (mines[i,j], i, j);
}
}
@@ -214,10 +214,10 @@ public class MinefieldView : Gtk.Grid
}
}
- public void tile_pressed_cb (int x, int y, Gdk.EventButton event)
+ private inline void tile_pressed_cb (int x, int y, uint button, int n_press, bool ctrl)
{
/* Ignore double click events */
- if (event.type != Gdk.EventType.BUTTON_PRESS)
+ if (n_press > 1)
return;
/* Check for end cases and paused game */
@@ -237,7 +237,8 @@ public class MinefieldView : Gtk.Grid
return;
/* Right or Ctrl+Left button to toggle flags */
- if (event.button == 3 || (event.button == 1 && (event.state & Gdk.ModifierType.CONTROL_MASK) != 0))
+ if (button == Gdk.BUTTON_SECONDARY
+ || button == Gdk.BUTTON_PRIMARY && ctrl)
{
toggle_mark (selected.x, selected.y);
this.force_nolongpress = true;
@@ -252,9 +253,10 @@ public class MinefieldView : Gtk.Grid
keyboard_cursor.position = {selected.x, selected.y};
}
- public void tile_released_cb (int x, int y, Gdk.EventButton event)
+ private inline void tile_released_cb (int x, int y, uint button)
{
- if (event.button != 1) return;
+ if (button != Gdk.BUTTON_PRIMARY)
+ return;
this.force_nolongpress = false;
@@ -283,7 +285,7 @@ public class MinefieldView : Gtk.Grid
selected.is_set = false;
}
- public void tile_long_pressed_cb (int x, int y)
+ private inline void tile_long_pressed_cb (int x, int y)
{
if (this.force_nolongpress == true) return;
selected.is_set = false;
diff --git a/src/tile.vala b/src/tile.vala
index 7aa945b..898ae58 100644
--- a/src/tile.vala
+++ b/src/tile.vala
@@ -3,11 +3,12 @@ public class Tile : Gtk.Button
public int row { internal get; protected construct; }
public int column { internal get; protected construct; }
- private Gtk.GestureLongPress _gesture;
+ private Gtk.GestureLongPress _gesture; // for keeping in memory
+ private Gtk.GestureClick _click_controller; // for keeping in memory
public signal void tile_mouse_over (int x, int y);
- public signal void tile_pressed (int x, int y, Gdk.EventButton event);
- public signal void tile_released (int x, int y, Gdk.EventButton event);
+ public signal void tile_pressed (int x, int y, uint button, int n_press, bool ctrl);
+ public signal void tile_released (int x, int y, uint button);
public signal void tile_long_pressed (int x, int y);
public Tile (int prow, int pcol)
@@ -19,27 +20,35 @@ public class Tile : Gtk.Button
{
can_focus = false;
add_class ("tile");
- _gesture = new Gtk.GestureLongPress (this);
+
+ _gesture = new Gtk.GestureLongPress ();
_gesture.touch_only = true;
_gesture.pressed.connect((x, y) =>
{
tile_long_pressed (row, column);
});
- button_press_event.connect ((event) =>
+ add_controller (_gesture);
+
+ _click_controller = new Gtk.GestureClick ();
+ _click_controller.set_button (/* all buttons */ 0);
+ _click_controller.pressed.connect ((click_controller, n_press, x, y) =>
{
/* By default windows with both button press and button release
* grab Gdk events, ungrab events here for other tiles. */
- event.get_seat().ungrab ();
+// event.get_seat().ungrab (); // TODO do something instead?
// event.device.ungrab (event.time);
- tile_pressed (row, column, event);
- return false;
+ Gdk.ModifierType state = click_controller.get_current_event_state ();
+ bool ctrl = (state & Gdk.ModifierType.CONTROL_MASK) != 0;
+ uint button = click_controller.get_current_button ();
+ tile_pressed (row, column, button, n_press, ctrl);
});
- button_release_event.connect ((event) =>
+ _click_controller.released.connect ((click_controller, n_press, x, y) =>
{
- tile_released (row, column, event);
- return false;
+ uint button = click_controller.get_current_button ();
+ tile_released (row, column, button);
});
+ add_controller (_click_controller);
}
public void add_class (string style_class)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]