[gnome-builder/wip/gtk4-port] libide/gui: abstract page/pane helpers



commit e875949ebbc8256470df11322aecd4ee3f7356db
Author: Christian Hergert <chergert redhat com>
Date:   Wed Apr 6 22:34:37 2022 -0700

    libide/gui: abstract page/pane helpers
    
    This could probably become public at some point if we like how it works.

 src/libide/editor/ide-editor-workspace.c | 102 ++++------------------
 src/libide/gui/ide-primary-workspace.c   | 143 ++++---------------------------
 src/libide/gui/ide-workspace-private.h   |   9 ++
 src/libide/gui/ide-workspace.c           | 135 +++++++++++++++++++++++++++++
 4 files changed, 176 insertions(+), 213 deletions(-)
---
diff --git a/src/libide/editor/ide-editor-workspace.c b/src/libide/editor/ide-editor-workspace.c
index fb68df80a..97281cd90 100644
--- a/src/libide/editor/ide-editor-workspace.c
+++ b/src/libide/editor/ide-editor-workspace.c
@@ -23,6 +23,7 @@
 #include "config.h"
 
 #include "ide-editor-workspace.h"
+#include "ide-workspace-private.h"
 
 /**
  * SECTION:ide-editor-workspace
@@ -109,41 +110,16 @@ ide_editor_workspace_add_page (IdeWorkspace     *workspace,
                                IdePanelPosition *position)
 {
   IdeEditorWorkspace *self = (IdeEditorWorkspace *)workspace;
-  PanelFrame *frame;
-  PanelDockPosition edge;
-  guint column;
-  guint row;
 
   g_assert (IDE_IS_EDITOR_WORKSPACE (self));
-  g_assert (IDE_IS_PAGE (page));
-  g_assert (position != NULL);
 
-  ide_panel_position_get_edge (position, &edge);
-
-  switch (edge)
-    {
-    case PANEL_DOCK_POSITION_START:
-    case PANEL_DOCK_POSITION_END:
-    case PANEL_DOCK_POSITION_BOTTOM:
-    case PANEL_DOCK_POSITION_TOP:
-    default:
-      g_warning ("Editor workspace only supports center position");
-      return;
-
-    case PANEL_DOCK_POSITION_CENTER:
-      break;
-    }
-
-  if (!ide_panel_position_get_column (position, &column))
-    column = 0;
-
-  if (!ide_panel_position_get_row (position, &row))
-    row = 0;
-
-  frame = panel_grid_column_get_row (panel_grid_get_column (PANEL_GRID (self->grid), column), row);
-
-  /* TODO: Handle depth */
-  panel_frame_add (frame, PANEL_WIDGET (page));
+  _ide_workspace_add_widget (workspace,
+                             PANEL_WIDGET (page),
+                             position,
+                             self->edge_start,
+                             self->edge_end,
+                             self->edge_bottom,
+                             self->grid);
 }
 
 static void
@@ -152,64 +128,16 @@ ide_editor_workspace_add_pane (IdeWorkspace     *workspace,
                                IdePanelPosition *position)
 {
   IdeEditorWorkspace *self = (IdeEditorWorkspace *)workspace;
-  PanelDockPosition edge;
-  PanelPaned *paned;
-  GtkWidget *parent;
-  guint depth;
-  guint nth = 0;
 
   g_assert (IDE_IS_EDITOR_WORKSPACE (self));
-  g_assert (IDE_IS_PANE (pane));
-  g_assert (position != NULL);
-
-  ide_panel_position_get_edge (position, &edge);
-
-  switch (edge)
-    {
-    case PANEL_DOCK_POSITION_START:
-      paned = self->edge_start;
-      ide_panel_position_get_row (position, &nth);
-      break;
-
-    case PANEL_DOCK_POSITION_END:
-      paned = self->edge_end;
-      ide_panel_position_get_row (position, &nth);
-      break;
-
-    case PANEL_DOCK_POSITION_BOTTOM:
-      paned = self->edge_bottom;
-      ide_panel_position_get_column (position, &nth);
-      break;
-
-    case PANEL_DOCK_POSITION_TOP:
-    case PANEL_DOCK_POSITION_CENTER:
-    default:
-      g_warning ("Editor workspace only supports left/right/bottom edges");
-      return;
-    }
 
-  while (!(parent = panel_paned_get_nth_child (paned, nth)))
-    {
-      parent = panel_frame_new ();
-
-      if (edge == PANEL_DOCK_POSITION_START ||
-          edge == PANEL_DOCK_POSITION_END)
-        gtk_orientable_set_orientation (GTK_ORIENTABLE (parent), GTK_ORIENTATION_VERTICAL);
-      else
-        gtk_orientable_set_orientation (GTK_ORIENTABLE (parent), GTK_ORIENTATION_HORIZONTAL);
-
-      panel_paned_append (paned, parent);
-    }
-
-  if (ide_panel_position_get_depth (position, &depth))
-    {
-      /* TODO: setup position */
-      panel_frame_add (PANEL_FRAME (parent), PANEL_WIDGET (pane));
-    }
-  else
-    {
-      panel_frame_add (PANEL_FRAME (parent), PANEL_WIDGET (pane));
-    }
+  _ide_workspace_add_widget (workspace,
+                             PANEL_WIDGET (pane),
+                             position,
+                             self->edge_start,
+                             self->edge_end,
+                             self->edge_bottom,
+                             self->grid);
 }
 
 static IdeFrame *
diff --git a/src/libide/gui/ide-primary-workspace.c b/src/libide/gui/ide-primary-workspace.c
index 0c1573bae..6c13b9ad0 100644
--- a/src/libide/gui/ide-primary-workspace.c
+++ b/src/libide/gui/ide-primary-workspace.c
@@ -30,9 +30,7 @@
 #include "ide-omni-bar.h"
 #include "ide-primary-workspace-private.h"
 #include "ide-run-button.h"
-
-#define GET_PRIORITY(w)   GPOINTER_TO_INT(g_object_get_data(G_OBJECT(w),"PRIORITY"))
-#define SET_PRIORITY(w,i) g_object_set_data(G_OBJECT(w),"PRIORITY",GINT_TO_POINTER(i))
+#include "ide-workspace-private.h"
 
 /**
  * SECTION:ide-primary-workspace
@@ -88,87 +86,22 @@ ide_primary_workspace_context_set (IdeWorkspace *workspace,
                             G_BINDING_SYNC_CREATE);
 }
 
-static void
-add_to_frame_with_depth (PanelFrame  *frame,
-                         PanelWidget *widget,
-                         guint        depth,
-                         gboolean     depth_set)
-{
-  PanelWidget *previous_page;
-  guint n_pages;
-
-  g_assert (PANEL_IS_FRAME (frame));
-  g_assert (PANEL_IS_WIDGET (widget));
-
-  previous_page = panel_frame_get_visible_child (frame);
-
-  if (!depth_set || depth > G_MAXINT)
-    depth = G_MAXINT;
-
-  SET_PRIORITY (widget, depth);
-
-  n_pages = panel_frame_get_n_pages (frame);
-
-  for (guint i = 0; i < n_pages; i++)
-    {
-      PanelWidget *child = panel_frame_get_page (frame, i);
-
-      if ((int)depth < GET_PRIORITY (child))
-        {
-          panel_frame_add_before (frame, widget, child);
-          panel_frame_set_visible_child (frame, widget);
-          return;
-        }
-    }
-
-  panel_frame_add (frame, widget);
-
-  if (previous_page != NULL)
-    panel_frame_set_visible_child (frame, previous_page);
-}
-
 static void
 ide_primary_workspace_add_page (IdeWorkspace     *workspace,
                                 IdePage          *page,
                                 IdePanelPosition *position)
 {
   IdePrimaryWorkspace *self = (IdePrimaryWorkspace *)workspace;
-  PanelFrame *frame;
-  PanelDockPosition edge;
-  gboolean depth_set;
-  guint depth = 0;
-  guint column;
-  guint row;
 
   g_assert (IDE_IS_PRIMARY_WORKSPACE (self));
-  g_assert (IDE_IS_PAGE (page));
-  g_assert (position != NULL);
-
-  ide_panel_position_get_edge (position, &edge);
-
-  switch (edge)
-    {
-    case PANEL_DOCK_POSITION_START:
-    case PANEL_DOCK_POSITION_END:
-    case PANEL_DOCK_POSITION_BOTTOM:
-    case PANEL_DOCK_POSITION_TOP:
-    default:
-      g_warning ("Primary workspace only supports center position");
-      return;
-
-    case PANEL_DOCK_POSITION_CENTER:
-      break;
-    }
-
-  if (!ide_panel_position_get_column (position, &column))
-    column = 0;
-
-  if (!ide_panel_position_get_row (position, &row))
-    row = 0;
-
-  depth_set = ide_panel_position_get_depth (position, &depth);
-  frame = panel_grid_column_get_row (panel_grid_get_column (PANEL_GRID (self->grid), column), row);
-  add_to_frame_with_depth (frame, PANEL_WIDGET (page), depth, depth_set);
+
+  _ide_workspace_add_widget (workspace,
+                             PANEL_WIDGET (page),
+                             position,
+                             self->edge_start,
+                             self->edge_end,
+                             self->edge_bottom,
+                             self->grid);
 }
 
 static void
@@ -177,58 +110,16 @@ ide_primary_workspace_add_pane (IdeWorkspace     *workspace,
                                 IdePanelPosition *position)
 {
   IdePrimaryWorkspace *self = (IdePrimaryWorkspace *)workspace;
-  PanelDockPosition edge;
-  PanelPaned *paned;
-  GtkWidget *parent;
-  gboolean depth_set;
-  guint depth;
-  guint nth = 0;
 
   g_assert (IDE_IS_PRIMARY_WORKSPACE (self));
-  g_assert (IDE_IS_PANE (pane));
-  g_assert (position != NULL);
-
-  ide_panel_position_get_edge (position, &edge);
-
-  switch (edge)
-    {
-    case PANEL_DOCK_POSITION_START:
-      paned = self->edge_start;
-      ide_panel_position_get_row (position, &nth);
-      break;
-
-    case PANEL_DOCK_POSITION_END:
-      paned = self->edge_end;
-      ide_panel_position_get_row (position, &nth);
-      break;
-
-    case PANEL_DOCK_POSITION_BOTTOM:
-      paned = self->edge_bottom;
-      ide_panel_position_get_column (position, &nth);
-      break;
-
-    case PANEL_DOCK_POSITION_TOP:
-    case PANEL_DOCK_POSITION_CENTER:
-    default:
-      g_warning ("Primary workspace only supports left/right/bottom edges");
-      return;
-    }
-
-  while (!(parent = panel_paned_get_nth_child (paned, nth)))
-    {
-      parent = panel_frame_new ();
-
-      if (edge == PANEL_DOCK_POSITION_START ||
-          edge == PANEL_DOCK_POSITION_END)
-        gtk_orientable_set_orientation (GTK_ORIENTABLE (parent), GTK_ORIENTATION_VERTICAL);
-      else
-        gtk_orientable_set_orientation (GTK_ORIENTABLE (parent), GTK_ORIENTATION_HORIZONTAL);
-
-      panel_paned_append (paned, parent);
-    }
-
-  depth_set = ide_panel_position_get_depth (position, &depth);
-  add_to_frame_with_depth (PANEL_FRAME (parent), PANEL_WIDGET (pane), depth, depth_set);
+
+  _ide_workspace_add_widget (workspace,
+                             PANEL_WIDGET (pane),
+                             position,
+                             self->edge_start,
+                             self->edge_end,
+                             self->edge_bottom,
+                             self->grid);
 }
 
 static IdeFrame *
diff --git a/src/libide/gui/ide-workspace-private.h b/src/libide/gui/ide-workspace-private.h
index 60f859656..9ea8ac973 100644
--- a/src/libide/gui/ide-workspace-private.h
+++ b/src/libide/gui/ide-workspace-private.h
@@ -20,6 +20,8 @@
 
 #pragma once
 
+#include "ide-grid.h"
+#include "ide-panel-position.h"
 #include "ide-workspace.h"
 
 G_BEGIN_DECLS
@@ -34,5 +36,12 @@ void   _ide_workspace_move_front_page_mru    (IdeWorkspace        *workspace,
                                               GList               *mru_link);
 void   _ide_workspace_set_context            (IdeWorkspace        *workspace,
                                               IdeContext          *context);
+void   _ide_workspace_add_widget             (IdeWorkspace        *workspace,
+                                              PanelWidget         *widget,
+                                              IdePanelPosition    *position,
+                                              PanelPaned          *dock_start,
+                                              PanelPaned          *dock_end,
+                                              PanelPaned          *dock_bottom,
+                                              IdeGrid             *grid);
 
 G_END_DECLS
diff --git a/src/libide/gui/ide-workspace.c b/src/libide/gui/ide-workspace.c
index e7c97aeed..20fcc404a 100644
--- a/src/libide/gui/ide-workspace.c
+++ b/src/libide/gui/ide-workspace.c
@@ -30,6 +30,9 @@
 #include "ide-workbench-private.h"
 
 #define MUX_ACTIONS_KEY "IDE_WORKSPACE_MUX_ACTIONS"
+#define GET_PRIORITY(w)   GPOINTER_TO_INT(g_object_get_data(G_OBJECT(w),"PRIORITY"))
+#define SET_PRIORITY(w,i) g_object_set_data(G_OBJECT(w),"PRIORITY",GINT_TO_POINTER(i))
+
 
 typedef struct
 {
@@ -927,3 +930,135 @@ ide_workspace_get_statusbar (IdeWorkspace *self)
 
   return priv->statusbar;
 }
+
+static void
+add_to_frame_with_depth (PanelFrame  *frame,
+                         PanelWidget *widget,
+                         guint        depth,
+                         gboolean     depth_set)
+{
+  PanelWidget *previous_page;
+  guint n_pages;
+
+  g_assert (PANEL_IS_FRAME (frame));
+  g_assert (PANEL_IS_WIDGET (widget));
+
+  previous_page = panel_frame_get_visible_child (frame);
+
+  if (!depth_set || depth > G_MAXINT)
+    depth = G_MAXINT;
+
+  SET_PRIORITY (widget, depth);
+
+  n_pages = panel_frame_get_n_pages (frame);
+
+  for (guint i = 0; i < n_pages; i++)
+    {
+      PanelWidget *child = panel_frame_get_page (frame, i);
+
+      if ((int)depth < GET_PRIORITY (child))
+        {
+          panel_frame_add_before (frame, widget, child);
+          panel_frame_set_visible_child (frame, widget);
+          return;
+        }
+    }
+
+  panel_frame_add (frame, widget);
+
+  if (previous_page != NULL)
+    panel_frame_set_visible_child (frame, previous_page);
+}
+
+void
+_ide_workspace_add_widget (IdeWorkspace     *self,
+                           PanelWidget      *widget,
+                           IdePanelPosition *position,
+                           PanelPaned       *dock_start,
+                           PanelPaned       *dock_end,
+                           PanelPaned       *dock_bottom,
+                           IdeGrid          *grid)
+{
+  PanelDockPosition edge;
+  PanelPaned *paned = NULL;
+  GtkWidget *parent;
+  gboolean depth_set;
+  guint depth;
+  guint column;
+  guint row;
+  guint nth = 0;
+
+  IDE_ENTRY;
+
+  g_return_if_fail (IDE_IS_WORKSPACE (self));
+  g_return_if_fail (PANEL_IS_WIDGET (widget));
+  g_return_if_fail (position != NULL);
+  g_return_if_fail (!dock_start || PANEL_IS_PANED (dock_start));
+  g_return_if_fail (!dock_end || PANEL_IS_PANED (dock_end));
+  g_return_if_fail (!dock_bottom || PANEL_IS_PANED (dock_bottom));
+  g_return_if_fail (IDE_IS_GRID (grid));
+
+  if (!ide_panel_position_get_edge (position, &edge))
+    edge = PANEL_DOCK_POSITION_CENTER;
+
+  if (edge == PANEL_DOCK_POSITION_CENTER)
+    {
+      PanelFrame *frame;
+
+      if (!ide_panel_position_get_column (position, &column))
+        column = 0;
+
+      if (!ide_panel_position_get_row (position, &row))
+        row = 0;
+
+      depth_set = ide_panel_position_get_depth (position, &depth);
+      frame = panel_grid_column_get_row (panel_grid_get_column (PANEL_GRID (grid), column), row);
+      add_to_frame_with_depth (frame, widget, depth, depth_set);
+
+      IDE_EXIT;
+    }
+
+  switch (edge)
+    {
+    case PANEL_DOCK_POSITION_START:
+      paned = dock_start;
+      ide_panel_position_get_row (position, &nth);
+      break;
+
+    case PANEL_DOCK_POSITION_END:
+      paned = dock_end;
+      ide_panel_position_get_row (position, &nth);
+      break;
+
+    case PANEL_DOCK_POSITION_BOTTOM:
+      paned = dock_bottom;
+      ide_panel_position_get_column (position, &nth);
+      break;
+
+    case PANEL_DOCK_POSITION_TOP:
+      g_warning ("Top panel is not supported");
+      return;
+
+    case PANEL_DOCK_POSITION_CENTER:
+    default:
+      return;
+    }
+
+  while (!(parent = panel_paned_get_nth_child (paned, nth)))
+    {
+      parent = panel_frame_new ();
+
+      if (edge == PANEL_DOCK_POSITION_START ||
+          edge == PANEL_DOCK_POSITION_END)
+        gtk_orientable_set_orientation (GTK_ORIENTABLE (parent), GTK_ORIENTATION_VERTICAL);
+      else
+        gtk_orientable_set_orientation (GTK_ORIENTABLE (parent), GTK_ORIENTATION_HORIZONTAL);
+
+      panel_paned_append (paned, parent);
+    }
+
+  depth_set = ide_panel_position_get_depth (position, &depth);
+  add_to_frame_with_depth (PANEL_FRAME (parent), widget, depth, depth_set);
+
+  IDE_EXIT;
+}


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