Sun, 04 Feb 2018 14:37:09 +0000
Various tray menu fixes
borgend/ui.py | file | annotate | diff | comparison | revisions |
--- a/borgend/ui.py Sun Feb 04 09:38:55 2018 +0000 +++ b/borgend/ui.py Sun Feb 04 14:37:09 2018 +0000 @@ -112,7 +112,7 @@ return new info=None - this_need_reconstruct=None + this_refresh_time=None if not status.errors.ok(): info=add_info(info, str(status.errors)) @@ -135,13 +135,17 @@ diff=datetime.timedelta(seconds=when-now) if twhen>tendtomorrow: + # Display date if scheduled event is after tomorrow whenday=datetime.date.fromtimestamp(when) whenstr='on %s' % twhen.date().isoformat() - this_need_reconstruct=tendtoday+datetime.timedelta(seconds=1) - elif diff.seconds>=12*60*60: # 12 hours + this_refresh_time=tendtoday+datetime.timedelta(seconds=1) + elif twhen>tendtoday and diff.seconds>=12*60*60: # 12 hours + # Display 'tomorrow' if the scheduled event is tomorrow and + # not earlier than after 12 hours whenstr='tomorrow' - this_need_reconstruct=twhen-datetime.timedelta(hours=12) + this_refresh_time=twhen-datetime.timedelta(hours=12) else: + # Otherwise, display time twhen=time.localtime(when) if twhen.tm_sec>30: # Round up minute display to avoid user confusion @@ -184,7 +188,7 @@ else: title=status.name - return title, (status.state, status.errors), this_need_reconstruct + return title, (status.state, status.errors), this_refresh_time class BorgendTray(rumps.App): def __init__(self, backups): @@ -213,12 +217,12 @@ def __rebuild_menu(self): menu=[] state=(backup.State.INACTIVE, backup.Errors.OK) - need_reconstruct=None + refresh_time=None all_paused=True for index in range(len(self.backups)): b=self.backups[index] - title, this_state, this_need_reconstruct=make_title(self.statuses[index]) + title, this_state, this_refresh_time=make_title(self.statuses[index]) # Python closures suck dog's balls... # first and the last program I write in Python until somebody # fixes this brain damage @@ -234,10 +238,10 @@ all_paused=(all_paused and this_state[0]==backup.State.PAUSED) # Do we have to automatically update menu display? - if not need_reconstruct: - need_reconstruct=this_need_reconstruct - elif this_need_reconstruct: - need_reconstruct=min(need_reconstruct, this_need_reconstruct) + if not refresh_time: + refresh_time=this_refresh_time + elif this_refresh_time: + refresh_time=min(refresh_time, this_refresh_time) menu_log=rumps.MenuItem("Show log", callback=lambda _: showlog()) menu.append(menu_log) @@ -253,23 +257,28 @@ menu_quit=rumps.MenuItem("Quit...", callback=lambda _: self.quit()) menu.append(menu_quit) - return menu, state, need_reconstruct + return menu, state, refresh_time def build_menu_and_timer(self): if self.refresh_timer: self.refresh_timer.cancel() self.refresh_timer=None self.refresh_timer_time=None + logger.debug('Rebuilding menu') - menu, state, need_reconstruct=self.__rebuild_menu() + menu, state, refresh_time=self.__rebuild_menu() title=trayname(state) - if need_reconstruct: - when=time.mktime(need_reconstruct.timetuple()) + if refresh_time: + # Need to time a refresh due to content display changing, + # e.g., 'tomorrow' changing to a more specific hour. + when=time.mktime(refresh_time.timetuple()) delay=when-time.time() - self.refresh_timer=Timer(delay, self.refresh_ui) - self.refresh_timer_time=need_reconstruct - self.refresh_timer.start() + if delay>0: + logger.debug('Timing menu refresh in %s seconds' % delay) + self.refresh_timer=Timer(delay, self.refresh_ui) + self.refresh_timer_time=refresh_time + self.refresh_timer.start() return menu, title @@ -287,7 +296,7 @@ # Time the refresh if it has not been timed, or if the timer # is timing for the "long-term" (refresh_timer_time set) if not self.refresh_timer or self.refresh_timer_time: - logger.debug("Timing refresh") + # logger.debug("Timing refresh") self.refresh_timer=Timer(refresh_interval, self.refresh_ui) # refresh_timer_time is only set for "long-term timers" self.refresh_timer_time=None