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