1

Consider having a simple window with a Gtk.textView

#!/usr/bin/python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class myWindow(Gtk.Window):
    def __init__(self):
        ...
        self.txtBuffer = Gtk.TextBuffer()
        self.txtView = Gtk.TextView(buffer = self.txtBuffer)
        ...

    def autosave(self):
        # code to save txtBuffer content to a file

I want the autosave method to run when txtView is idle (after the writing is stopped for an amount of time, say 5 seconds) but I don't know what event(s) to use.

Aboud Zakaria
  • 567
  • 9
  • 25
  • 1
    Have you considered `Glib.timeout_add()` ? Or another signal like `focus-out` ? I don't think there is an idle signal. You will have to create one yourself. – theGtknerd May 31 '17 at 11:35
  • Do you want to contemplate copy-paste with mouse? – José Fonte May 31 '17 at 11:48
  • yeah, I have tried the `GLib.timeout_add` but it seemed to run only once for the first time, it turns out that I was missing the `return True` part that keeps `autosave` method running periodically. – Aboud Zakaria May 31 '17 at 11:51
  • 1
    @AboudZakariaI've edited the answer. The TextBuffer **changed** signal maybe a better approach instead of the **key-release-event**. – José Fonte May 31 '17 at 11:57

1 Answers1

1

Add a callback to the key-release-event which will update a boolean flag indicating that the key on the keyboard was pressed then add a timeout function at regular intervals (defined as 5 seconds) that checks the keypressed flag, if so then autosave.

#!/usr/bin/python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class myWindow(Gtk.Window):
    def __init__(self):
        ...
        self.txtBuffer = Gtk.TextBuffer()
        self.txtView = Gtk.TextView(buffer = self.txtBuffer)
        #####################################################################
        self.txtView.connect ("key-release-event", self.on_key_release_event)
        GLib.timeout_add_seconds(5, self.check_autosave_timer)
        #####################################################################
        ...

    def autosave(self):
        # code to save txtBuffer content to a file

    def check_autosave_timer(self):
        if self.keypressed:
            self.autosave(self)
            self.keypressed = False
        return True 

    def on_key_release_event(self, event, user_data):
        self.keypressed = True

EDIT:

Just checked that you also may want to contemplate the mouse copy-paste. Gtk.TextBuffer has the changed signal. You can use the same approach on that, eg. on_textbuffer_changed you set the flag and then on glib.timeout you would save. Maybe it's a better approach and includes keystrokes and mouse events.

Also note that this solution will save after 5 seconds of a keystroke/textbuffer change. To do it, only after, 5 seconds of being idle, a timer must be used and every time the textbuffer changes the timer is reset so that the timeout function will only save if the timer elapses more than the defined time for idle save (the timeout_add_seconds timed callback must be set at a lower frequency than that chosen for the idle autosave timer).

José Fonte
  • 4,016
  • 2
  • 16
  • 28