Skip to content

Commit 6e5295f

Browse files
committed
Allow to press and hold keys
1 parent 7471c77 commit 6e5295f

5 files changed

Lines changed: 81 additions & 19 deletions

File tree

daemon/OSK/InputManager.vala

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ public class Gala.Daemon.InputManager : Object {
1717
service.keyval_released (keyval);
1818
}
1919

20-
public void erase () {
21-
service.keyval_pressed (Gdk.Key.BackSpace);
22-
service.keyval_released (Gdk.Key.BackSpace);
20+
public void press_keyval (uint keyval) {
21+
service.keyval_pressed (keyval);
22+
}
23+
24+
public void release_keyval (uint keyval) {
25+
service.keyval_released (keyval);
2326
}
2427

2528
public void request_hide () {

daemon/OSK/KeyboardModel/Key.vala

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
public class Gala.Daemon.Key : Object {
99
public const string ACTION_GROUP_PREFIX = "keyboard";
1010
public const string ACTION_PREFIX = ACTION_GROUP_PREFIX + ".";
11-
/* Types the keyval given as the action target */
11+
/* Types the keyval given as the action target in a single combination of pressed + released */
1212
public const string ACTION_TYPE_KEY_VAL = "keyval";
13-
/* Erases the last character */
14-
public const string ACTION_ERASE = "erase";
13+
/* Sends a keyval pressed event */
14+
public const string ACTION_PRESS_KEY_VAL = "keyval-press";
15+
/* Sends a keyval released event */
16+
public const string ACTION_RELEASE_KEY_VAL = "keyval-release";
1517
/* Latches the keyboard view with the name given as the action target */
1618
public const string ACTION_LATCH_VIEW = "latch-view";
1719
/* Sets the keyboard view with the name given as the action target */
@@ -23,19 +25,28 @@ public class Gala.Daemon.Key : Object {
2325
public double width { get; construct; default = 1.0; }
2426
public double height { get; construct; default = 1.0; }
2527

28+
/**
29+
* Action triggered on release.
30+
*/
2631
public string detailed_action_name { get; construct; }
2732

33+
/**
34+
* Additional optional action triggered on press.
35+
*/
36+
public string? press_detailed_action_name { get; construct; }
37+
2838
public ListModel popup_keys { get; construct; }
2939

3040
public string? label { get; construct; }
3141
public Icon? icon { get; construct; }
3242

33-
public Key (double left_offset, double width, double height, string detailed_action_name, ListModel popup_keys, string? label, Icon? icon) {
43+
public Key (double left_offset, double width, double height, string detailed_action_name, string? press_detailed_action_name, ListModel popup_keys, string? label, Icon? icon) {
3444
Object (
3545
left_offset: left_offset,
3646
width: width,
3747
height: height,
3848
detailed_action_name: detailed_action_name,
49+
press_detailed_action_name: press_detailed_action_name,
3950
popup_keys: popup_keys,
4051
label: label,
4152
icon: icon

daemon/OSK/KeyboardModel/KeyboardModelBuilder.vala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class Gala.Daemon.KeyboardModelBuilder : Object {
1919
private double current_key_width = 1.0;
2020
private double current_key_height = 1.0;
2121
private string? current_key_detailed_action_name; // Mandatory to set
22+
private string? current_key_press_detailed_action_name; // Additional action to trigger on press
2223
private ListStore? current_key_popup_keys;
2324
private string? current_key_label;
2425
private Icon? current_key_icon;
@@ -77,11 +78,12 @@ public class Gala.Daemon.KeyboardModelBuilder : Object {
7778
}
7879

7980
public void set_key_val_action (uint val) {
80-
current_key_detailed_action_name = Action.print_detailed_name (Key.ACTION_PREFIX + Key.ACTION_TYPE_KEY_VAL, new Variant.uint32 (val));
81+
current_key_press_detailed_action_name = Action.print_detailed_name (Key.ACTION_PREFIX + Key.ACTION_PRESS_KEY_VAL, new Variant.uint32 (val));
82+
current_key_detailed_action_name = Action.print_detailed_name (Key.ACTION_PREFIX + Key.ACTION_RELEASE_KEY_VAL, new Variant.uint32 (val));
8183
}
8284

8385
public void set_erase_action () {
84-
current_key_detailed_action_name = Action.print_detailed_name (Key.ACTION_PREFIX + Key.ACTION_ERASE, null);
86+
set_key_val_action (Gdk.Key.BackSpace);
8587
}
8688

8789
public void set_latch_view_action (string view_name) {
@@ -130,6 +132,7 @@ public class Gala.Daemon.KeyboardModelBuilder : Object {
130132
current_key_width,
131133
current_key_height,
132134
current_key_detailed_action_name ?? "none",
135+
current_key_press_detailed_action_name,
133136
current_key_popup_keys,
134137
current_key_label,
135138
current_key_icon
@@ -140,6 +143,7 @@ public class Gala.Daemon.KeyboardModelBuilder : Object {
140143
current_key_width = 1.0;
141144
current_key_height = 1.0;
142145
current_key_detailed_action_name = null;
146+
current_key_press_detailed_action_name = null;
143147
current_key_popup_keys = null;
144148
current_key_label = null;
145149
current_key_icon = null;

daemon/OSK/Window/Keyboard/KeyButton.vala

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,53 @@ public class Gala.Daemon.KeyButton : Granite.Bin {
99
public Key key {
1010
set {
1111
if (value.label != null) {
12-
button.label = value.label;
12+
child = new Gtk.Label (value.label);
1313
} else if (value.icon != null) {
14-
button.child = new Gtk.Image.from_gicon (value.icon);
14+
child = new Gtk.Image.from_gicon (value.icon);
1515
} else {
16-
button.label = _("Unknown Key");
16+
child = new Gtk.Label (_("Unknown Key"));
1717
}
1818

19-
button.set_detailed_action_name (value.detailed_action_name);
19+
_key = value;
2020
}
2121
}
2222

23-
private Gtk.Button button;
23+
private Key? _key;
24+
25+
class construct {
26+
// TODO: Style as a keycap? Though button style looks fitting already
27+
set_css_name ("button");
28+
}
2429

2530
construct {
26-
button = new Gtk.Button ();
27-
child = button;
31+
var click_gesture = new Gtk.GestureClick ();
32+
click_gesture.pressed.connect (on_pressed);
33+
click_gesture.released.connect (on_released);
34+
add_controller (click_gesture);
35+
}
36+
37+
private void on_pressed () {
38+
if (_key?.press_detailed_action_name == null) {
39+
return;
40+
}
41+
42+
activate_detailed_action (_key.press_detailed_action_name);
43+
}
44+
45+
private void on_released () {
46+
activate_detailed_action (_key.detailed_action_name);
47+
}
48+
49+
private void activate_detailed_action (string detailed_action) {
50+
string action_name;
51+
Variant? target;
52+
try {
53+
Action.parse_detailed_name (detailed_action, out action_name, out target);
54+
} catch (Error e) {
55+
warning ("Failed to parse action name %s: %s", detailed_action, e.message);
56+
return;
57+
}
58+
59+
activate_action_variant (action_name, target);
2860
}
2961
}

daemon/OSK/Window/Keyboard/Keyboard.vala

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
public class Gala.Daemon.Keyboard : Granite.Bin {
99
private const ActionEntry [] ACTIONS = {
1010
{ Key.ACTION_TYPE_KEY_VAL, on_type_key_val, "u" },
11-
{ Key.ACTION_ERASE, on_erase },
11+
{ Key.ACTION_PRESS_KEY_VAL, on_press_key_val, "u" },
12+
{ Key.ACTION_RELEASE_KEY_VAL, on_release_key_val, "u" },
1213
{ Key.ACTION_SET_VIEW, on_set_view, "s" },
1314
{ Key.ACTION_LATCH_VIEW, on_latch_view, "s" },
1415
{ Key.ACTION_HIDE, on_hide },
@@ -64,8 +65,19 @@ public class Gala.Daemon.Keyboard : Granite.Bin {
6465
}
6566
}
6667

67-
private void on_erase () {
68-
input_manager.erase ();
68+
private void on_press_key_val (SimpleAction action, Variant? param) {
69+
var keyval = (uint) param.get_uint32 ();
70+
input_manager.press_keyval (keyval);
71+
}
72+
73+
private void on_release_key_val (SimpleAction action, Variant? param) {
74+
var keyval = (uint) param.get_uint32 ();
75+
input_manager.release_keyval (keyval);
76+
77+
if (active_view != current_view) {
78+
/* Reset a latched view */
79+
active_view = current_view;
80+
}
6981
}
7082

7183
private void on_set_view (SimpleAction action, Variant? param) {

0 commit comments

Comments
 (0)