[epiphany] Append new tab on the window with most tabs in the current workspace



commit c968c68cabc319896ad4d2096940c9a34d4c13cd
Author: Gustavo Noronha Silva <gns gnome org>
Date:   Sun Dec 9 15:25:37 2012 +0100

    Append new tab on the window with most tabs in the current workspace
    
    This change uses a different heuristic to decide on what the best window to add
    a new tab is from the last window which has been interacted with, which might
    even be in a different workspace, to the window with the most tabs in the
    current workspace. If no window exists on the current workspace one will be
    created.
    
    Partial fix for https://bugzilla.gnome.org/show_bug.cgi?id=685976

 configure.ac       |    1 +
 src/ephy-session.c |    2 +-
 src/ephy-shell.c   |   38 ++++++++++++++++++++++++++++++++++++++
 src/ephy-shell.h   |    2 ++
 src/ephy-window.c  |   39 +++++++++++++++++++++++++++++++++++++++
 src/ephy-window.h  |    2 ++
 6 files changed, 83 insertions(+), 1 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 591ece4..2bbb34e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -125,6 +125,7 @@ PKG_CHECK_MODULES([DEPENDENCIES], [
 		  gtk+-3.0 >= $GTK_REQUIRED
 		  gtk+-unix-print-3.0 >= $GTK_REQUIRED
 		  x11
+		  libwnck-3.0
 		  libxml-2.0 >= $LIBXML_REQUIRED
 		  libxslt >= $LIBXSLT_REQUIRED
 		  $WEBKIT_GTK_PC_NAME >= $WEBKIT_GTK_REQUIRED
diff --git a/src/ephy-session.c b/src/ephy-session.c
index d761d9d..3a0f197 100644
--- a/src/ephy-session.c
+++ b/src/ephy-session.c
@@ -261,7 +261,7 @@ session_command_open_uris (EphySession *session,
 
 	g_object_ref (shell);
 
-	window = EPHY_WINDOW (gtk_application_get_active_window (GTK_APPLICATION (shell)));
+	window = ephy_shell_get_main_window (shell);
 
 	new_windows_in_tabs = g_settings_get_boolean (EPHY_SETTINGS_MAIN,
 						      EPHY_PREFS_NEW_WINDOWS_IN_TABS);
diff --git a/src/ephy-shell.c b/src/ephy-shell.c
index eb97891..9800038 100644
--- a/src/ephy-shell.c
+++ b/src/ephy-shell.c
@@ -1061,6 +1061,44 @@ ephy_shell_get_n_windows (EphyShell *shell)
   return g_list_length (shell->priv->windows);
 }
 
+EphyWindow*
+ephy_shell_get_main_window (EphyShell *shell)
+{
+  EphyWindow *window = NULL;
+  GList *windows;
+  GList *iter;
+
+  g_return_val_if_fail (EPHY_IS_SHELL (shell), NULL);
+
+  /* Select the window with most tabs in the current workspace as the window to
+   * use for opening a new tab on, if that turns out to be the case.
+   */
+  windows = ephy_shell_get_windows (shell);
+
+  for (iter = windows; iter != NULL; iter = iter->next) {
+    EphyWindow *candidate = EPHY_WINDOW (iter->data);
+    GtkWidget *cur_notebook;
+    GtkWidget *cand_notebook;
+
+    if (!ephy_window_is_on_current_workspace (candidate))
+      continue;
+
+    if (!window) {
+      window = candidate;
+      continue;
+    }
+
+    cur_notebook = ephy_window_get_notebook (window);
+    cand_notebook =  ephy_window_get_notebook (candidate);
+    if (gtk_notebook_get_n_pages (cand_notebook) > gtk_notebook_get_n_pages (cur_notebook))
+      window = candidate;
+  }
+
+  g_list_free (windows);
+
+  return window;
+}
+
 gboolean
 ephy_shell_close_all_windows (EphyShell *shell)
 {
diff --git a/src/ephy-shell.h b/src/ephy-shell.h
index 3cf8b18..057d7cb 100644
--- a/src/ephy-shell.h
+++ b/src/ephy-shell.h
@@ -176,6 +176,8 @@ GList          *ephy_shell_get_windows                  (EphyShell *shell);
 
 guint           ephy_shell_get_n_windows                (EphyShell *shell);
 
+EphyWindow     *ephy_shell_get_main_window              (EphyShell *shell);
+
 gboolean        ephy_shell_close_all_windows            (EphyShell *shell);
 
 G_END_DECLS
diff --git a/src/ephy-window.c b/src/ephy-window.c
index 64c7be8..d1c0170 100644
--- a/src/ephy-window.c
+++ b/src/ephy-window.c
@@ -61,10 +61,15 @@
 #include "window-commands.h"
 
 #include <gdk/gdkkeysyms.h>
+#include <gdk/gdkx.h>
 #include <gio/gio.h>
 #include <glib/gi18n.h>
 #include <gtk/gtk.h>
 #include <libsoup/soup.h>
+
+#define WNCK_I_KNOW_THIS_IS_UNSTABLE
+#include <libwnck/libwnck.h>
+
 #ifdef HAVE_WEBKIT2
 #include <webkit2/webkit2.h>
 #else
@@ -4390,6 +4395,40 @@ ephy_window_get_location_controller (EphyWindow *window)
 }
 
 /**
+ * ephy_window_is_on_current_workspace:
+ * @window: an #EphyWindow
+ *
+ * Returns whether @window is on the current workspace
+ *
+ * Returns: %TRUE if the window is on the current workspace, %FALSE otherwise
+ **/
+gboolean
+ephy_window_is_on_current_workspace (EphyWindow *window)
+{
+	GdkWindow *gdk_window = NULL;
+	WnckWorkspace *workspace = NULL;
+	WnckWindow *wnck_window = NULL;
+
+	if (!gtk_widget_get_realized (GTK_WIDGET (window)))
+		return TRUE;
+
+	workspace = wnck_screen_get_active_workspace (wnck_screen_get_default ());
+
+	/* From WNCK docs:
+	 * "May return NULL sometimes, if libwnck is in a weird state due to
+	 *  the asynchronous nature of the interaction with the window manager."
+	 * In such a case we cannot really check, so assume we are.
+	 */
+	if (!workspace)
+		return TRUE;
+
+	gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
+	wnck_window = wnck_window_get (GDK_WINDOW_XID (gdk_window));
+
+	return wnck_window_is_on_workspace (wnck_window, workspace);
+}
+
+/**
  * ephy_window_close:
  * @window: an #EphyWindow
  *
diff --git a/src/ephy-window.h b/src/ephy-window.h
index 8ce845d..e284111 100644
--- a/src/ephy-window.h
+++ b/src/ephy-window.h
@@ -76,6 +76,8 @@ const char       *ephy_window_get_location        (EphyWindow *window);
 
 gboolean          ephy_window_close               (EphyWindow *window);
 
+gboolean      ephy_window_is_on_current_workspace (EphyWindow *window);
+
 G_END_DECLS
 
 #endif



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