Skip to content

Commit e354389

Browse files
authored
MainWindow: Use EventControllerKey (#716)
1 parent 894b9ab commit e354389

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

src/MainWindow.vala

+16-12
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,13 @@ public class Greeter.MainWindow : Gtk.ApplicationWindow {
3737

3838
private bool installer_mode = false;
3939

40+
private Gtk.EventControllerKey key_controller;
41+
4042
private const uint[] NAVIGATION_KEYS = {
4143
Gdk.Key.Up,
4244
Gdk.Key.Down,
4345
Gdk.Key.Left,
4446
Gdk.Key.Right,
45-
Gdk.Key.Return,
4647
Gdk.Key.Tab
4748
};
4849

@@ -178,11 +179,16 @@ public class Greeter.MainWindow : Gtk.ApplicationWindow {
178179
manual_card.do_connect_username.connect (do_connect_username);
179180
manual_card.do_connect.connect (do_connect);
180181

181-
key_press_event.connect ((event) => {
182-
if (!(event.keyval in NAVIGATION_KEYS)) {
182+
key_controller = new Gtk.EventControllerKey (this) {
183+
propagation_phase = CAPTURE
184+
};
185+
key_controller.key_pressed.connect ((keyval, keycode, state) => {
186+
var mods = state & Gtk.accelerator_get_default_mod_mask ();
187+
188+
if (!(keyval in NAVIGATION_KEYS)) {
183189
// Don't focus if it is a modifier or if search_box is already focused
184190
unowned var current_focus = get_focus ();
185-
if ((event.is_modifier == 0) && (current_focus == null || !current_focus.is_ancestor (current_card))) {
191+
if ((mods == 0) && (current_focus == null || !current_focus.is_ancestor (current_card))) {
186192
current_card.grab_focus ();
187193
}
188194

@@ -194,15 +200,15 @@ public class Greeter.MainWindow : Gtk.ApplicationWindow {
194200
unowned var focused_entry = (Gtk.Entry) get_focus ();
195201
if (focused_entry != null && focused_entry.is_ancestor (current_card)) {
196202
if (focused_entry.text == "") {
197-
if (event.keyval == Gdk.Key.Left) {
198-
if (get_style_context ().direction == LTR) {
203+
if (keyval == Gdk.Key.Left) {
204+
if (Gtk.StateFlags.DIR_LTR in get_state_flags ()) {
199205
go_previous ();
200206
} else {
201207
go_next ();
202208
}
203209
return Gdk.EVENT_STOP;
204-
} else if (event.keyval == Gdk.Key.Right) {
205-
if (get_style_context ().direction == LTR) {
210+
} else if (keyval == Gdk.Key.Right) {
211+
if (Gtk.StateFlags.DIR_LTR in get_state_flags ()) {
206212
go_next ();
207213
} else {
208214
go_previous ();
@@ -212,8 +218,6 @@ public class Greeter.MainWindow : Gtk.ApplicationWindow {
212218
}
213219
}
214220
}
215-
216-
return Gdk.EVENT_PROPAGATE;
217221
});
218222

219223
carousel.page_changed.connect ((index) => {
@@ -497,15 +501,15 @@ public class Greeter.MainWindow : Gtk.ApplicationWindow {
497501
});
498502

499503
user_card.go_left.connect (() => {
500-
if (get_style_context ().direction == LTR) {
504+
if (Gtk.StateFlags.DIR_LTR in get_state_flags ()) {
501505
go_previous ();
502506
} else {
503507
go_next ();
504508
}
505509
});
506510

507511
user_card.go_right.connect (() => {
508-
if (get_style_context ().direction == LTR) {
512+
if (Gtk.StateFlags.DIR_LTR in get_state_flags ()) {
509513
go_next ();
510514
} else {
511515
go_previous ();

0 commit comments

Comments
 (0)