[gnome-dictionary: 1/2] Keep the sidebar size up to date




commit 70bc7e0d2dda316a0692cef46d5a8448eea8d3cf
Author: Guido Trentalancia <guido trentalancia com>
Date:   Thu Sep 13 23:58:14 2018 +0200

    Keep the sidebar size up to date
    
    If it is not possible to load the window state (for example, it is
    the first time that the application runs), set a default value (33%)
    for the sidebar width.
    
    This fixes Gitlab issue #3 (bugzilla #788621) which was causing the
    sidebar to extend to the whole window (sidebar_width = 0, i.e. 100%),
    thus hiding the main panel and rendering the application basically
    unusable when the sidebar was enabled.
    
    The second version of this patch introduced further fixes to the
    window resizing code (the sidebar panel is correctly resized upon
    window resizing or window minimization/maximization).
    
    This third version includes support for the correct migration from
    the old window state format to the new format (sidebar width as a
    percentage instead of pixels).
    
    Fixes: #3
    
    Signed-off-by: Guido Trentalancia <guido trentalancia com>

 src/gdict-window.c | 56 ++++++++++++++++++++++++++++++++++--------------------
 src/gdict-window.h |  2 +-
 2 files changed, 36 insertions(+), 22 deletions(-)
---
diff --git a/src/gdict-window.c b/src/gdict-window.c
index 02be31c..012912b 100644
--- a/src/gdict-window.c
+++ b/src/gdict-window.c
@@ -701,7 +701,7 @@ gdict_window_store_state (GdictWindow *window)
   g_key_file_set_integer (state_key, "WindowState", "Height", window->current_height);
   g_key_file_set_boolean (state_key, "WindowState", "IsMaximized", window->is_maximized);
   g_key_file_set_boolean (state_key, "WindowState", "SidebarVisible", window->sidebar_visible);
-  g_key_file_set_integer (state_key, "WindowState", "SidebarWidth", window->sidebar_width);
+  g_key_file_set_double (state_key, "WindowState", "SidebarWidthPercentage", 
window->sidebar_width_percentage);
 
   page_id = gdict_sidebar_current_page (GDICT_SIDEBAR (window->sidebar));
   if (page_id == NULL)
@@ -785,11 +785,11 @@ gdict_window_load_state (GdictWindow *window)
       window->sidebar_visible = FALSE;
     }
 
-  window->sidebar_width = g_key_file_get_integer (state_key, "WindowState", "SidebarWidth", &error);
+  window->sidebar_width_percentage = g_key_file_get_double (state_key, "WindowState", 
"SidebarWidthPercentage", &error);
   if (error != NULL)
     {
       g_clear_error (&error);
-      window->sidebar_width = -1;
+      window->sidebar_width_percentage = -1;
     }
 
   window->sidebar_page = g_key_file_get_string (state_key, "WindowState", "SidebarPage", &error);
@@ -1111,12 +1111,20 @@ gdict_window_state_event_cb (GtkWidget           *widget,
                             gpointer             user_data)
 {
   GdictWindow *window = GDICT_WINDOW (widget);
-  
+
   if (event->new_window_state & GDK_WINDOW_STATE_MAXIMIZED)
     window->is_maximized = TRUE;
   else
     window->is_maximized = FALSE;
-  
+
+  gtk_window_get_size (GTK_WINDOW (window),
+                       &window->current_width,
+                       &window->current_height);
+
+  /* Resize the panel as the window is minimised/maximised */
+  gtk_paned_set_position (GTK_PANED (window->main_pane),
+                          window->current_width * (1.0 - window->sidebar_width_percentage));
+
   return FALSE;
 }
 
@@ -1295,12 +1303,13 @@ gdict_window_size_allocate (GtkWidget     *widget,
   if (GTK_WIDGET_CLASS (gdict_window_parent_class)->size_allocate != NULL)
     GTK_WIDGET_CLASS (gdict_window_parent_class)->size_allocate (widget, allocation);
 
-  if (!window->is_maximized)
-    {
-      gtk_window_get_size (GTK_WINDOW (widget),
-                           &window->current_width,
-                           &window->current_height);
-    }
+  gtk_window_get_size (GTK_WINDOW (window),
+                       &window->current_width,
+                       &window->current_height);
+
+  /* Resize the panel as the window is resized */
+  int paned_pos = window->current_width * (1.0 - window->sidebar_width_percentage);
+  gtk_paned_set_position (GTK_PANED (window->main_pane), paned_pos);
 }
 
 static void
@@ -1309,14 +1318,16 @@ gdict_window_handle_notify_position_cb (GtkWidget  *widget,
                                        gpointer    user_data)
 {
   GdictWindow *window = GDICT_WINDOW (user_data);
-  gint window_width, pos;
-  GtkAllocation allocation;
 
-  pos = gtk_paned_get_position (GTK_PANED (widget));
-  gtk_widget_get_allocation (GTK_WIDGET (window), &allocation);
-  window_width = allocation.width;
+  int pos = gtk_paned_get_position (GTK_PANED (window->main_pane));
+
+  double new_sidebar_width_percentage = 1.0 - pos / (double) window->current_width;
+  double diff_sidebar_width_percentage = fabs (window->sidebar_width_percentage - 
new_sidebar_width_percentage);
+  if (pos < window->current_width && diff_sidebar_width_percentage > 1e-2)
+    window->sidebar_width_percentage = new_sidebar_width_percentage;
 
-  window->sidebar_width = window_width - pos;
+  int new_pos = window->current_width * (1.0 - window->sidebar_width_percentage);
+  gtk_paned_set_position (GTK_PANED (window->main_pane), new_pos);
 }
 
 static void
@@ -1327,7 +1338,6 @@ gdict_window_constructed (GObject *gobject)
   GtkWidget *button;
   PangoFontDescription *font_desc;
   gchar *font_name;
-  GtkAllocation allocation;
   GMenu *menu;
   
   window = GDICT_WINDOW (gobject);
@@ -1337,6 +1347,10 @@ gdict_window_constructed (GObject *gobject)
   /* recover the state */
   gdict_window_load_state (window);
 
+  /* Set a default value for the sidebar width (percentage) */
+  if (window->sidebar_width_percentage <= 0)
+    window->sidebar_width_percentage = 0.3333;
+
   /* build menus */
   g_action_map_add_action_entries (G_ACTION_MAP (window),
                                    entries, G_N_ELEMENTS (entries),
@@ -1493,7 +1507,7 @@ gdict_window_constructed (GObject *gobject)
     }
 
   pango_font_description_free (font_desc);
-  
+
   gtk_window_set_title (GTK_WINDOW (window), _("Dictionary"));
   gtk_window_set_default_size (GTK_WINDOW (window),
                                window->default_width,
@@ -1501,8 +1515,6 @@ gdict_window_constructed (GObject *gobject)
   if (window->is_maximized)
     gtk_window_maximize (GTK_WINDOW (window));
 
-  gtk_widget_get_allocation (GTK_WIDGET (window), &allocation);
-  gtk_paned_set_position (GTK_PANED (window->main_pane), allocation.width - window->sidebar_width);
   if (window->sidebar_page != NULL)
     gdict_sidebar_view_page (GDICT_SIDEBAR (window->sidebar), window->sidebar_page);
   else
@@ -1662,6 +1674,8 @@ gdict_window_init (GdictWindow *window)
 
   window->default_width = -1;
   window->default_height = -1;
+  window->current_width = -1;
+  window->current_height = -1;
   window->is_maximized = FALSE;
   window->sidebar_visible = FALSE;
   window->sidebar_page = NULL;
diff --git a/src/gdict-window.h b/src/gdict-window.h
index 9eb2100..b509d2e 100644
--- a/src/gdict-window.h
+++ b/src/gdict-window.h
@@ -99,7 +99,7 @@ struct _GdictWindow
   gint default_height;
   gint current_width;
   gint current_height;
-  gint sidebar_width;
+  gdouble sidebar_width_percentage;
 
   gchar *sidebar_page;
 


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