diff --git a/src/SlingshotView.vala b/src/SlingshotView.vala index f1bf81dc..b1a051d4 100644 --- a/src/SlingshotView.vala +++ b/src/SlingshotView.vala @@ -241,41 +241,45 @@ public class Slingshot.SlingshotView : Gtk.Bin, UnityClient { return Gdk.EVENT_STOP; } } + // Alt accelerators if ((state & Gdk.ModifierType.MOD1_MASK) != 0) { switch (keyval) { case Gdk.Key.F4: close_indicator (); return Gdk.EVENT_STOP; + } - case Gdk.Key.@0: - case Gdk.Key.@1: - case Gdk.Key.@2: - case Gdk.Key.@3: - case Gdk.Key.@4: - case Gdk.Key.@5: - case Gdk.Key.@6: - case Gdk.Key.@7: - case Gdk.Key.@8: - case Gdk.Key.@9: - if (modality == Modality.NORMAL_VIEW) { + if (modality == NORMAL_VIEW) { + switch (keyval) { + case Gdk.Key.@0: + grid_view.set_page (0); + return Gdk.EVENT_STOP; + + case Gdk.Key.@9: + grid_view.last_page (); + return Gdk.EVENT_STOP; + + case Gdk.Key.@1: + case Gdk.Key.@2: + case Gdk.Key.@3: + case Gdk.Key.@4: + case Gdk.Key.@5: + case Gdk.Key.@6: + case Gdk.Key.@7: + case Gdk.Key.@8: var key = Gdk.keyval_name (keyval).replace ("KP_", ""); - int page = int.parse (key); - if (page < 0 || page == 9) { - grid_view.go_to_last (); - } else { - grid_view.go_to_number (page); - } - } + grid_view.set_page (int.parse (key) - 1); - return Gdk.EVENT_STOP; + return Gdk.EVENT_STOP; + } } } switch (keyval) { case Gdk.Key.Page_Up: if (modality == Modality.NORMAL_VIEW) { - grid_view.go_to_previous (); + grid_view.previous_page (); } else if (modality == Modality.CATEGORY_VIEW) { category_view.page_up (); } @@ -283,7 +287,7 @@ public class Slingshot.SlingshotView : Gtk.Bin, UnityClient { case Gdk.Key.Page_Down: if (modality == Modality.NORMAL_VIEW) { - grid_view.go_to_next (); + grid_view.next_page (); } else if (modality == Modality.CATEGORY_VIEW) { category_view.page_down (); } @@ -291,7 +295,7 @@ public class Slingshot.SlingshotView : Gtk.Bin, UnityClient { case Gdk.Key.End: if (modality == Modality.NORMAL_VIEW) { - grid_view.go_to_last (); + grid_view.last_page (); } break; diff --git a/src/Views/GridView.vala b/src/Views/GridView.vala index fd1e7ec7..0308cd87 100644 --- a/src/Views/GridView.vala +++ b/src/Views/GridView.vala @@ -45,25 +45,6 @@ public class Slingshot.Widgets.Grid : Gtk.Box { } } - private uint _current_grid_key = 0; - public uint current_grid_key { - get { - return _current_grid_key; - } - - set { - // Clamp to valid values for keyboard navigation - _current_grid_key = value.clamp (1, paginator.n_pages); - var grid = (Gtk.Grid) paginator.get_children ().nth_data (_current_grid_key - 1); - if (grid == null) { - return; - } - - paginator.scroll_to (grid); - refocus (); - } - } - construct { paginator = new Hdy.Carousel () { hexpand = true, @@ -96,9 +77,7 @@ public class Slingshot.Widgets.Grid : Gtk.Box { paginator.remove (child); } - _current_grid_key = 0; // Avoids clamp - var grid = add_new_grid (); // Increments current_grid_key to 1 - + var grid = add_new_grid (); // Where to insert new app button var next_row_index = 0; var next_col_index = 0; @@ -124,7 +103,7 @@ public class Slingshot.Widgets.Grid : Gtk.Box { show_all (); // Show first page after populating the carousel - current_grid_key = 1; + set_page (0); } private Gtk.Grid add_new_grid () { @@ -147,7 +126,6 @@ public class Slingshot.Widgets.Grid : Gtk.Box { } paginator.add (grid); - current_grid_key = current_grid_key + 1; return grid; } @@ -157,7 +135,7 @@ public class Slingshot.Widgets.Grid : Gtk.Box { return null; } else { var grid = (Gtk.Grid) paginator.get_children ().nth_data ((int) paginator.get_position ()); - return grid.get_child_at ((int)col - 1, (int)row - 1); + return grid.get_child_at ((int) col - 1, (int) row - 1); } } @@ -167,27 +145,11 @@ public class Slingshot.Widgets.Grid : Gtk.Box { focused_column = focused_column; } - public void go_to_next () { - current_grid_key++; - } - - public void go_to_previous () { - current_grid_key--; - } - - public void go_to_last () { - current_grid_key = paginator.n_pages; - } - - public void go_to_number (int number) { - current_grid_key = number; - } - private bool on_key_press (uint keyval, uint keycode, Gdk.ModifierType state) { switch (keyval) { case Gdk.Key.Home: case Gdk.Key.KP_Home: - current_grid_key = 1; + set_page (0); return Gdk.EVENT_STOP; case Gdk.Key.Left: @@ -230,9 +192,9 @@ public class Slingshot.Widgets.Grid : Gtk.Box { private void move_left (Gdk.ModifierType state) { if ((state & Gdk.ModifierType.SHIFT_MASK) > 0) { - current_grid_key--; - } else if (focused_column == 1 && current_grid_key > 1) { - current_grid_key--; + previous_page (); + } else if (focused_column == 1 && paginator.get_position () > 0) { + previous_page (); focused_column = PAGE_COLUMNS; } else { focused_column--; @@ -241,12 +203,34 @@ public class Slingshot.Widgets.Grid : Gtk.Box { private void move_right (Gdk.ModifierType state) { if ((state & Gdk.ModifierType.SHIFT_MASK) > 0) { - current_grid_key++; - } else if (focused_column == PAGE_COLUMNS && current_grid_key < paginator.n_pages) { - current_grid_key++; + next_page (); + } else if (focused_column == PAGE_COLUMNS && paginator.get_position () < paginator.n_pages - 1) { + next_page (); focused_column = 1; } else { focused_column++; } } + + public void next_page () { + set_page ((int) paginator.get_position () + 1); + } + + public void previous_page () { + set_page ((int) paginator.get_position () - 1); + } + + public void last_page () { + set_page (paginator.n_pages); + } + + public void set_page (uint pos) { + var grid = paginator.get_children ().nth_data (pos); + if (grid == null) { + return; + } + + paginator.scroll_to (grid); + refocus (); + } }