Last active
August 1, 2018 22:23
-
-
Save gottadiveintopython/e4a952113cdfc3abd966a8d49b42ceae to your computer and use it in GitHub Desktop.
pause resume test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
title=pause resume test | |
author=GottaDiveIntoPython | |
orientation=sensor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
from kivy.config import Config | |
Config.set('kivy', 'pause_on_minimize', 1) | |
from kivy.app import App | |
from kivy.uix.textinput import TextInput | |
from os.path import join as ospath_join, isfile as ospath_isfile | |
import io | |
import sys | |
def log(*args, **kwargs): | |
print(*args, file=sys.stderr, **kwargs) | |
class TestApp(App): | |
def build(self): | |
return TextInput() | |
def on_pause(self): | |
log('on_pause()') | |
self.save_session() | |
return True | |
def on_resume(self): | |
log('on_resume()') | |
def on_start(self): | |
log('on_start()') | |
self.restore_session() | |
def on_stop(self): | |
log('on_stop()') | |
self.save_session() | |
@property | |
def savefilepath(self): | |
return ospath_join(self.user_data_dir, 'savedata.txt') | |
def save_session(self): | |
with io.open(self.savefilepath, 'wt', encoding='utf-8') as writer: | |
writer.write(self.root.text) | |
def restore_session(self): | |
path = self.savefilepath | |
if not ospath_isfile(path): | |
return | |
with io.open(path, 'rt', encoding='utf-8') as reader: | |
self.root.text = reader.read() | |
if __name__ == '__main__': | |
TestApp().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment