Skip to content Skip to sidebar Skip to footer

Detecting When A Gtk Window Is Done Moving/resizing By The User

I want to detect when the user finished re-sizing or moving the GTK window. Basically an equivalent of WM_EXITSIZEMOVE in windows. I have looked at GTK detecting window resize fro

Solution 1:

You could use a timeout function that gets called once the resizing is done. The timeout is in ms, you might want to play with the value to get a balance between the delay in calling resize_done and triggering it before the resize is really done.

#define TIMEOUT 250

gboolean resize_done (gpointer data)
{
  guint *id = data;
  *id = 0;
  /* call your existing code here */returnFALSE;
}

gboolean on_configure_event (GtkWidget *window, GdkEvent *event, gpointer data)
{
  static guint id = 0;
  if (id)
    g_source_remove (id);
  id = g_timeout_add (TIMEOUT, resize_done, &id);
  returnFALSE;
}

Solution 2:

I implemented a solution (some would call it workaround) with PyGObject and Python 3. As Phillip Wood in this quesiton and ratchet freak in another question mentioned I also used a timer based solution.

In the example the size-allocate event is used to detected the beginning of the window resize. I assume the user drag the window border with its mouse here. There are other ways to detect a window resize event: see discussion here.

Next the event is disconnected and as a surrogate a timer event (GLib.timeout_add() with 500 milliseconds) is created handling the next stuff.

Here is the example code:

#!/usr/bin/env python3# -*- coding: utf-8 -*-import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import GLib


classMyWindow(Gtk.Window):
    def__init__(self):
        Gtk.Window.__init__(self)

        # close event
        self.connect('delete-event', self._on_delete_event)

        # "resize" event
        self._connect_resize_event()

    def_connect_resize_event(self):
        self._timer_id = None
        eid = self.connect('size-allocate', self._on_size_allocated)
        self._event_id_size_allocate = eid

    def_on_delete_event(self, a, b):
        Gtk.main_quit()

    def_on_size_allocated(self, widget, alloc):
        print('EVENT: size allocated')

        # don't install a second timerif self._timer_id:
            return# remember new size
        self._remembered_size = alloc

        # disconnect the 'size-allocate' event
        self.disconnect(self._event_id_size_allocate)

        # create a 500ms timer
        tid = GLib.timeout_add(interval=500, function=self._on_size_timer)
        # ...and remember its id
        self._timer_id = tid

    def_on_size_timer(self):
        # current window size
        curr = self.get_allocated_size().allocation

        # was the size changed in the last 500ms?# NO changes anymoreif self._remembered_size.equal(curr):  # == doesn't work hereprint('RESIZING FINISHED')
            # reconnect the 'size-allocate' event
            self._connect_resize_event()
            # stop the timer
            self._timer_id = NonereturnFalse# YES size was changed# remember the new size for the next check after 500ms
        self._remembered_size = curr
        # repeat timerreturnTrueif __name__ == '__main__':
    window = MyWindow()
    window.show_all()
    Gtk.main()

Post a Comment for "Detecting When A Gtk Window Is Done Moving/resizing By The User"