fix restarting after refresh

This commit is contained in:
Michael Hudson-Doyle 2020-07-31 10:48:17 +12:00
parent f5f8a8a75c
commit f6757af8d1
2 changed files with 29 additions and 26 deletions

View File

@ -122,13 +122,7 @@ class Application:
# EventLoop ------------------------------------------------------------------- # EventLoop -------------------------------------------------------------------
def _remove_last_screen(self):
last_screen = self.state_path('last-screen')
if os.path.exists(last_screen):
os.unlink(last_screen)
def exit(self): def exit(self):
self._remove_last_screen()
self.aio_loop.stop() self.aio_loop.stop()
def start_controllers(self): def start_controllers(self):
@ -145,24 +139,11 @@ class Application:
with open(state_path) as fp: with open(state_path) as fp:
controller.deserialize(json.load(fp)) controller.deserialize(json.load(fp))
last_screen = None
state_path = self.state_path('last-screen')
if os.path.exists(state_path):
with open(state_path) as fp:
last_screen = fp.read().strip()
controller_index = 0
for i, controller in enumerate(self.controllers.instances):
if controller.name == last_screen:
controller_index = i
# Screens that have already been seen should be marked as configured.
for controller in self.controllers.instances[:controller_index]:
controller.configured()
return controller_index
def run(self): def run(self):
self.base_model = self.make_model() self.base_model = self.make_model()
try: try:
self.controllers.load_all() self.controllers.load_all()
self.load_serialized_state()
self._connect_base_signals() self._connect_base_signals()
self.start_controllers() self.start_controllers()
self.aio_loop.run_forever() self.aio_loop.run_forever()

View File

@ -14,6 +14,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import logging import logging
import os
import yaml import yaml
import urwid import urwid
@ -68,6 +69,15 @@ class TuiApplication(Application):
self.urwid_loop = None self.urwid_loop = None
def _remove_last_screen(self):
last_screen = self.state_path('last-screen')
if os.path.exists(last_screen):
os.unlink(last_screen)
def exit(self):
self._remove_last_screen()
super().exit()
def run_command_in_foreground(self, cmd, before_hook=None, after_hook=None, def run_command_in_foreground(self, cmd, before_hook=None, after_hook=None,
**kw): **kw):
screen = self.urwid_loop.screen screen = self.urwid_loop.screen
@ -129,6 +139,8 @@ class TuiApplication(Application):
self._move_screen(-1) self._move_screen(-1)
def select_initial_screen(self, controller_index): def select_initial_screen(self, controller_index):
for controller in self.controllers.instances[:controller_index]:
controller.configured()
self.controllers.index = controller_index - 1 self.controllers.index = controller_index - 1
self.next_screen() self.next_screen()
@ -237,17 +249,27 @@ class TuiApplication(Application):
self.toggle_rich() self.toggle_rich()
self.urwid_loop.start() self.urwid_loop.start()
def initial_controller_index(self):
if not self.updated:
return 0
state_path = self.state_path('last-screen')
if not os.path.exists(state_path):
return 0
with open(state_path) as fp:
last_screen = fp.read().strip()
controller_index = 0
for i, controller in enumerate(self.controllers.instances):
if controller.name == last_screen:
controller_index = i
return controller_index
def run(self): def run(self):
if self.opts.scripts: if self.opts.scripts:
self.run_scripts(self.opts.scripts) self.run_scripts(self.opts.scripts)
self.aio_loop.call_soon(self.start_urwid) self.aio_loop.call_soon(self.start_urwid)
initial_controller_index = 0
if self.updated:
initial_controller_index = self.load_serialized_state()
self.aio_loop.call_soon( self.aio_loop.call_soon(
self.select_initial_screen, initial_controller_index) lambda: self.select_initial_screen(
self.initial_controller_index()))
try: try:
super().run() super().run()
finally: finally: