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