omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Triggering code on Pythonista focus lost

    Pythonista
    app sleep focus
    5
    5
    2343
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • nfmusician
      nfmusician last edited by nfmusician

      I've got an app using a TextView for editing plaintext files that has a save button. In the case that focus is lost from the Pythonista app as a whole (not just the TextView itself) while the file is unsaved, is there a way to catch this and trigger an automatic save before the app sleeps?

      stephen mikael 2 Replies Last reply Reply Quote 0
      • stephen
        stephen @nfmusician last edited by

        @nfmusician

        Here u go!

        console.set_idle_timer_disabled(flag):

        Disable or enable the idle timer (which puts the device to sleep after a certain period of inactivity).

        console.is_in_background():

        Return True if the app is currently running in the background, False otherwise.

        1 Reply Last reply Reply Quote 1
        • JonB
          JonB last edited by

          The method using a timer to periodically check whether the app is in the background ought to work. Check once every 30 seconds, perhaps. It might not be a bad idea to just perpetually save to a file after certain periods of inactivity -- at least to a temporary file. In your delegate method, you could first call the ui.cancel_delays(), then call ui.delay(your_save_func, 30).

          That way everytime you edit, after you stop typing, 30 seconds later the file gets saved. That should work in the background as well, I think (but test it).

          As an alternative, you can register a callback for notifications when the app is sent to the background -- so that you call for function immediately.

          1 Reply Last reply Reply Quote 1
          • mikael
            mikael @nfmusician last edited by

            @nfmusician, if you want to use the ”always saved” option that @Jonb suggested, here’s a version that calls the save 1 second after you have stopped typing.

            
            from functools import partial
            import ui
            
            class Delegate:
                
                save_delay = 1
                
                def textview_did_change(self, textview):
                    ui.cancel_delays()
                    ui.delay(
                        partial(self.save, textview),
                        self.save_delay)
                    
                def save(self, textview):
                    print('Saving', textview)
                
            tv = ui.TextView(delegate=Delegate())
            tv.present('fullscreen')
            
            
            cvp 1 Reply Last reply Reply Quote 1
            • cvp
              cvp @mikael last edited by

              @mikael said:

              from functools import partial

              Didn't know this partial. I often have searched a way with ui.delay calling a function with arguments, without using globals. Thanks to remember me that I don't know a lot about Python 😢

              1 Reply Last reply Reply Quote 0
              • First post
                Last post
              Powered by NodeBB Forums | Contributors