[gimp] Issue #6869: Toolbox buttons should focus the canvas as side effect.



commit 3d9996f1a62d64bdffeb2e3a4cfba5edad18a846
Author: Jehan <jehan girinstud io>
Date:   Mon Jul 5 12:33:35 2021 +0200

    Issue #6869: Toolbox buttons should focus the canvas as side effect.
    
    Toolbox buttons don't require focus keeping (no further keyboard input
    needed and navigating through/selecting tools can be done in various
    other ways with a keyboard), which is why I removed focusability of
    toolbox buttons years ago (cf. commit c83ee61c07).
    
    Nevertheless this is not enough, and since toolbox is meant to work on
    the canvas anyway, let's give back canvas focus as soon as any click
    happens on dead areas of the toolbox, as well as on the Wilber (dnd)
    drawing, but also each time a toolbox button is selected (though not on
    the longer clicks to see tool groups' contents without selecting a tool
    in the end).
    This will allow to directly start working on the image without requiring
    a click first (for instance panning with Space or similar interactions
    which would not give focus to the image canvas).
    
    This can also count as an alternative to the "Esc" shortcut to get
    canvas focus back, with a mouse click instead of a keyboard key.

 app/widgets/gimptoolbox.c    | 31 +++++++++++++++++++++++++++++--
 app/widgets/gimptoolbutton.c | 20 ++++++++++++++++++++
 2 files changed, 49 insertions(+), 2 deletions(-)
---
diff --git a/app/widgets/gimptoolbox.c b/app/widgets/gimptoolbox.c
index fb5f9fc476..b20aa622b8 100644
--- a/app/widgets/gimptoolbox.c
+++ b/app/widgets/gimptoolbox.c
@@ -31,6 +31,11 @@
 
 #include "core/gimp.h"
 #include "core/gimpcontext.h"
+#include "core/gimpdisplay.h"
+
+#include "display/display-types.h"
+#include "display/gimpdisplay.h"
+#include "display/gimpdisplayshell.h"
 
 #include "file/file-open.h"
 
@@ -182,6 +187,7 @@ gimp_toolbox_constructed (GObject *object)
   GimpToolbox   *toolbox = GIMP_TOOLBOX (object);
   GimpGuiConfig *config;
   GtkWidget     *main_vbox;
+  GtkWidget     *event_box;
 
   gimp_assert (GIMP_IS_CONTEXT (toolbox->p->context));
 
@@ -216,10 +222,16 @@ gimp_toolbox_constructed (GObject *object)
                     G_CALLBACK (gimp_toolbox_drag_drop),
                     toolbox);
 
+  event_box = gtk_event_box_new ();
+  gtk_box_pack_start (GTK_BOX (toolbox->p->vbox), event_box, FALSE, FALSE, 0);
+  g_signal_connect_swapped (event_box, "button-press-event",
+                            G_CALLBACK (gimp_toolbox_button_press_event),
+                            toolbox);
+  gtk_widget_show (event_box);
+
   toolbox->p->header = gtk_frame_new (NULL);
   gtk_frame_set_shadow_type (GTK_FRAME (toolbox->p->header), GTK_SHADOW_NONE);
-  gtk_box_pack_start (GTK_BOX (toolbox->p->vbox), toolbox->p->header,
-                      FALSE, FALSE, 0);
+  gtk_container_add (GTK_CONTAINER (event_box), toolbox->p->header);
 
   g_object_bind_property (config,             "toolbox-wilber",
                           toolbox->p->header, "visible",
@@ -340,6 +352,21 @@ gimp_toolbox_button_press_event (GtkWidget      *widget,
                                  GdkEventButton *event)
 {
   GimpToolbox *toolbox = GIMP_TOOLBOX (widget);
+  GimpDisplay *display;
+
+  if ((display = gimp_context_get_display (toolbox->p->context)) &&
+      gimp_context_get_image (toolbox->p->context))
+    {
+      /* Any button event in empty spaces or the Wilber area gives focus
+       * to the top image.
+       */
+      GimpImageWindow *image_window;
+
+      image_window = gimp_display_shell_get_window (gimp_display_get_shell (display));
+
+      gimp_display_present (display);
+      gtk_widget_grab_focus (gimp_window_get_primary_focus_widget (GIMP_WINDOW (image_window)));
+    }
 
   if (event->type == GDK_BUTTON_PRESS && event->button == 2)
     {
diff --git a/app/widgets/gimptoolbutton.c b/app/widgets/gimptoolbutton.c
index db73c2e866..815f3ef9e9 100644
--- a/app/widgets/gimptoolbutton.c
+++ b/app/widgets/gimptoolbutton.c
@@ -35,9 +35,14 @@
 #include "core/gimp-gui.h"
 #include "core/gimpcontainer.h"
 #include "core/gimpcontext.h"
+#include "core/gimpdisplay.h"
 #include "core/gimptoolgroup.h"
 #include "core/gimptoolinfo.h"
 
+#include "display/display-types.h"
+#include "display/gimpdisplay.h"
+#include "display/gimpdisplayshell.h"
+
 #include "actions/tools-commands.h"
 
 #include "gimpaccellabel.h"
@@ -47,6 +52,7 @@
 #include "gimptoolbutton.h"
 #include "gimpuimanager.h"
 #include "gimpwidgets-utils.h"
+#include "gimpwindow.h"
 #include "gimpwindowstrategy.h"
 
 #include "gimp-intl.h"
@@ -599,10 +605,24 @@ gimp_tool_button_toggled (GtkToggleToolButton *toggle_tool_button)
 
   if (tool_info)
     {
+      GimpDisplay *display;
+
       if (gtk_toggle_tool_button_get_active (toggle_tool_button))
         gimp_context_set_tool (context, tool_info);
       else if (tool_info == gimp_context_get_tool (context))
         gtk_toggle_tool_button_set_active (toggle_tool_button, TRUE);
+
+      /* Give focus to the main image. */
+      if ((display = gimp_context_get_display (context)) &&
+          gimp_context_get_image (context))
+        {
+          GimpImageWindow *image_window;
+
+          image_window = gimp_display_shell_get_window (gimp_display_get_shell (display));
+
+          gimp_display_present (display);
+          gtk_widget_grab_focus (gimp_window_get_primary_focus_widget (GIMP_WINDOW (image_window)));
+        }
     }
   else
     {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]