After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 624539 - Keep track of last focused window
Keep track of last focused window
Status: RESOLVED FIXED
Product: gtk+
Classification: Platform
Component: Class: GtkApplication
2.99.x
Other Linux
: Normal enhancement
: 3.2
Assigned To: gtkdev
gtkdev
Depends on:
Blocks:
 
 
Reported: 2010-07-16 13:14 UTC by Jonh Wendell
Modified: 2011-05-31 22:29 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Keep track of last focused window (2.16 KB, patch)
2010-07-16 13:14 UTC, Jonh Wendell
none Details | Review
Proposed patch (2.34 KB, patch)
2011-01-31 05:07 UTC, Matthew Barnes
reviewed Details | Review

Description Jonh Wendell 2010-07-16 13:14:10 UTC
Created attachment 166020 [details] [review]
Keep track of last focused window

Currently GtkApplication keeps track only of the first created window. If there are more than 1 window, we should keep track of the last one. Also, if the first one is destroyed, our reference is broken.
Comment 1 Matthew Barnes 2011-01-31 04:37:00 UTC
I was just going to file a very similar enhancement request, but I'll hijack this one.

I'm starting to convert Evolution's EShell class from UniqueApp to GtkApplication.  EShell has the exact same window-tracking concept as GtkApplication, except we keep the window list sorted by most-recently-focused using the same "focus-in-event" trick that Jonh's using in the patch here.  But instead of explictly trying to keep track of a default window, you can just maintain the invariant that the first window in the list returned by gtk_application_get_windows() is the most recently focused window.

This comes in handy when you need to quickly find a parent for a transient window.  The most recently focused "tracked" window is usually a safe choice.
Comment 2 Matthew Barnes 2011-01-31 05:07:01 UTC
Created attachment 179689 [details] [review]
Proposed patch
Comment 3 Matthias Clasen 2011-04-30 00:54:24 UTC
Review of attachment 179689 [details] [review]:

Makes sense to me. 
Lets see if Ryan has any comments

::: gtk/gtkapplication.c
@@ +267,3 @@
   if (g_list_find (priv->windows, window))
     {
+      if (GTK_IS_WINDOW (window))

Why is this check necessary ?

@@ +290,1 @@
  * The list that is returned should not be modified in any way.

Should probably point out here that the list will only remain valid until the next focus change.
Comment 4 Matthew Barnes 2011-04-30 04:57:19 UTC
(In reply to comment #3)
> ::: gtk/gtkapplication.c
> @@ +267,3 @@
>    if (g_list_find (priv->windows, window))
>      {
> +      if (GTK_IS_WINDOW (window))
> 
> Why is this check necessary ?

It may not be needed.  I think that was for the case where the function is called from gtk_window_finalize().  Wasn't sure if it's safe to disconnect signal handlers on an instance already being finalized, but I might have just been overly paranoid.