From 6ec8758b0f1a2d73d45e6d976d3d3abbf3249a4b Mon Sep 17 00:00:00 2001 From: Harald Judt Date: Sat, 31 Jan 2015 11:22:14 +0100 Subject: Improve keyboard navigation for thunar-tree-view (bug #4519) Left and right arrow (keypad too) expand/collapse, up and down arrow work as usual, and the right pane follows the tree view selection. --- thunar/thunar-tree-view.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/thunar/thunar-tree-view.c b/thunar/thunar-tree-view.c index f987dfc..10448a4 100644 --- a/thunar/thunar-tree-view.c +++ b/thunar/thunar-tree-view.c @@ -97,6 +97,8 @@ static gboolean thunar_tree_view_button_press_event (G GdkEventButton *event); static gboolean thunar_tree_view_button_release_event (GtkWidget *widget, GdkEventButton *event); +static gboolean thunar_tree_view_key_press_event (GtkWidget *widget, + GdkEventKey *event); static void thunar_tree_view_drag_data_received (GtkWidget *widget, GdkDragContext *context, gint x, @@ -432,6 +434,9 @@ thunar_tree_view_init (ThunarTreeView *view) gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE); gtk_tree_selection_set_select_function (selection, thunar_tree_view_selection_func, view, NULL); + /* custom keyboard handler for better navigation */ + g_signal_connect (GTK_WIDGET (view), "key_press_event", G_CALLBACK (thunar_tree_view_key_press_event), NULL); + /* enable drop support for the tree view */ gtk_drag_dest_set (GTK_WIDGET (view), 0, drop_targets, G_N_ELEMENTS (drop_targets), GDK_ACTION_COPY | GDK_ACTION_LINK | GDK_ACTION_MOVE); @@ -766,6 +771,74 @@ thunar_tree_view_button_release_event (GtkWidget *widget, return (*GTK_WIDGET_CLASS (thunar_tree_view_parent_class)->button_release_event) (widget, event); } +static gboolean +thunar_tree_view_key_press_event(GtkWidget *widget, + GdkEventKey *event) +{ + ThunarTreeView *tree_view = THUNAR_TREE_VIEW (widget); + gboolean stopPropagation = FALSE; + + /* Get path of currently highlighted item */ + GtkTreePath *path; + GtkTreeViewColumn *column; + gtk_tree_view_get_cursor(GTK_TREE_VIEW (tree_view), &path, &column); + + switch (event->keyval) + { + case GDK_KEY_Up: + case GDK_KP_Up: + /* the default action works good, but we want to update the right pane */ + GTK_WIDGET_CLASS (thunar_tree_view_parent_class)->key_press_event (widget, event); + thunar_tree_view_action_open (tree_view); + + stopPropagation = TRUE; + break; + + case GDK_KEY_Left: + case GDK_KP_Left: + /* if branch is expanded then collapse it */ + if (gtk_tree_view_row_expanded (GTK_TREE_VIEW (tree_view), path)) + gtk_tree_view_collapse_row (GTK_TREE_VIEW (tree_view), path); + + else /* if branch is already collapsed then move to parent */ + if (gtk_tree_path_up(path)) + gtk_tree_view_set_cursor(GTK_TREE_VIEW (tree_view), path, NULL, FALSE); + thunar_tree_view_action_open (tree_view); + + stopPropagation = TRUE; + break; + + case GDK_KEY_Down: + case GDK_KP_Down: + /* the default action works good, but we want to update the right pane */ + GTK_WIDGET_CLASS (thunar_tree_view_parent_class)->key_press_event (widget, event); + thunar_tree_view_action_open (tree_view); + + stopPropagation = TRUE; + break; + + case GDK_KEY_Right: + case GDK_KP_Right: + /* if branch is not expanded then expand it */ + if (!gtk_tree_view_row_expanded (GTK_TREE_VIEW (tree_view), path)) + gtk_tree_view_expand_row (GTK_TREE_VIEW (tree_view), path, FALSE); + + else /* if branch is already expanded then move to first child */ + { + gtk_tree_path_down(path); + gtk_tree_view_set_cursor(GTK_TREE_VIEW (tree_view), path, NULL, FALSE); + } + thunar_tree_view_action_open (tree_view); + + stopPropagation = TRUE; + break; + } + + gtk_tree_path_free(path); + gtk_widget_grab_focus (widget); + + return stopPropagation; +} static void -- 2.2.2