Using a switch instead of a long if else statement

This commit is contained in:
Erik Reider
2021-10-13 22:54:24 +02:00
parent ec7d3945cc
commit ababdd9807

View File

@@ -114,49 +114,62 @@ namespace SwayNotificatonCenter {
this.key_press_event.connect ((w, event_key) => {
if (event_key.type == Gdk.EventType.KEY_PRESS) {
if (event_key.keyval == Gdk.keyval_from_name ("Escape") ||
event_key.keyval == Gdk.keyval_from_name ("Caps_Lock")) {
var children = list_box.get_children ();
Notification noti = (Notification)
list_box.get_focus_child ();
switch (Gdk.keyval_name (event_key.keyval)) {
case "Escape":
case "Caps_Lock":
toggle_visibility ();
return true;
} else if (event_key.keyval == Gdk.keyval_from_name ("Return")) {
Notification noti = (Notification) list_box.get_focus_child ();
case "Return":
if (noti != null) noti.click_default_action ();
} else if (event_key.keyval == Gdk.keyval_from_name ("Delete") ||
event_key.keyval == Gdk.keyval_from_name ("BackSpace")) {
Notification noti = (Notification) list_box.get_focus_child ();
break;
case "Delete":
case "BackSpace":
if (noti != null) {
if (children.length () == 0) break;
if (list_reverse &&
list_box.get_children ().first ().data != noti) {
children.first ().data != noti) {
list_position--;
} else if (list_box.get_children ().last ().data == noti) {
} else if (children.last ().data == noti) {
if (list_position > 0) list_position--;
}
close_notification (noti.param.applied_id);
}
} else if (event_key.keyval == Gdk.keyval_from_name ("C")) {
break;
case "C":
close_all_notifications ();
} else if (event_key.keyval == Gdk.keyval_from_name ("D")) {
set_switch_dnd_state (!this.dnd_button.get_state ());
} else if (event_key.keyval == Gdk.keyval_from_name ("Down")) {
if (list_position + 1 < list_box.get_children ().length ()) {
break;
case "D":
set_switch_dnd_state (!dnd_button.get_state ());
break;
case "Down":
if (list_position + 1 < children.length ()) {
++list_position;
}
} else if (event_key.keyval == Gdk.keyval_from_name ("Up")) {
break;
case "Up":
if (list_position > 0) --list_position;
} else if (event_key.keyval == Gdk.keyval_from_name ("Home")) {
break;
case "Home":
list_position = 0;
} else if (event_key.keyval == Gdk.keyval_from_name ("End")) {
list_position = list_box.get_children ().length () - 1;
break;
case "End":
list_position = children.length () - 1;
if (list_position == uint.MAX) list_position = 0;
} else {
break;
default:
// Pressing 1-9 to activate a notification action
for (int i = 0; i < 9; i++) {
uint keyval = Gdk.keyval_from_name ((i + 1).to_string ());
uint keyval = Gdk.keyval_from_name (
(i + 1).to_string ());
if (event_key.keyval == keyval) {
Notification noti = (Notification) list_box.get_focus_child ();
if (noti != null) noti.click_alt_action (i);
break;
}
}
break;
}
navigate_list (list_position);
}