Skip to content

Commit

Permalink
ensure closed windows are garbage collected in a timely manner
Browse files Browse the repository at this point in the history
  • Loading branch information
samschott committed Mar 16, 2024
1 parent 78a018a commit 4a50ae2
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 58 deletions.
4 changes: 2 additions & 2 deletions src/maestral_cocoa/activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ def clear(self) -> None:


class ActivityWindow(Window):
def __init__(self, mdbx: MaestralProxy, app: toga.App) -> None:
super().__init__(title="Maestral Activity", app=app)
def __init__(self, mdbx: MaestralProxy) -> None:
super().__init__(title="Maestral Activity")
self.size = WINDOW_SIZE

self._refresh = False
Expand Down
31 changes: 15 additions & 16 deletions src/maestral_cocoa/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# system imports
import os
import sys
import gc
from traceback import format_exception
from subprocess import Popen
from datetime import datetime, timedelta
Expand Down Expand Up @@ -128,14 +129,14 @@ def startup(self) -> None:
pending_folder = self.mdbx.pending_dropbox_folder

if pending_link or pending_folder:
self.setup_dialog = SetupDialog(mdbx=self.mdbx, app=self)
self.setup_dialog.raise_()
self.setup_dialog.on_success = self.on_setup_completed
self.setup_dialog.on_failure = self.exit_and_stop_daemon
setup_dialog = SetupDialog(mdbx=self.mdbx)
setup_dialog.raise_()
setup_dialog.on_success = self.on_setup_completed
setup_dialog.on_failure = self.exit_and_stop_daemon

if not pending_link:
# Skip link page programmatically.
self.setup_dialog.goto_page(2)
setup_dialog.goto_page(2)

else:
self.add_background_task(self.on_setup_completed)
Expand Down Expand Up @@ -309,16 +310,13 @@ def on_start_stop_clicked(self, interface, *args, **kwargs) -> None:
self.add_background_task(self.restart)

def on_settings_clicked(self, interface, *args, **kwargs) -> None:
self.settings_window = SettingsWindow(mdbx=self.mdbx, app=self)
self.settings_window.raise_()
SettingsWindow(mdbx=self.mdbx).raise_()

def on_sync_issues_clicked(self, interface, *args, **kwargs) -> None:
self.sync_issues_window = SyncIssuesWindow(mdbx=self.mdbx, app=self)
self.sync_issues_window.raise_()
sync_issues_window = SyncIssuesWindow(mdbx=self.mdbx).raise_()

def on_activity_clicked(self, interface, *args, **kwargs) -> None:
self.activity_window = ActivityWindow(mdbx=self.mdbx, app=self)
self.activity_window.raise_()
activity_window = ActivityWindow(mdbx=self.mdbx).raise_()

def on_rebuild_clicked(self, interface, *args, **kwargs) -> None:
choice = self.alert(
Expand All @@ -343,6 +341,7 @@ async def periodic_refresh_gui(self, interface, *args, **kwargs) -> None:
await self.update_status(self)
await self.update_error(self)
await call_async_maestral(self.config_name, "status_change_longpoll")
gc.collect()
except CommunicationError:
super().exit()

Expand Down Expand Up @@ -427,13 +426,13 @@ async def _exec_dbx_location_dialog(self, interface, *args, **kwargs) -> None:
def start_sync_callback(a, *aa, **kwa):
self.mdbx.start_sync()

self.location_dialog = DbxLocationDialog(mdbx=self.mdbx, app=self)
self.location_dialog.raise_()
self.location_dialog.on_success = start_sync_callback
self.location_dialog.on_failure = self.exit_and_stop_daemon
location_dialog = DbxLocationDialog(mdbx=self.mdbx)
location_dialog.raise_()
location_dialog.on_success = start_sync_callback
location_dialog.on_failure = self.exit_and_stop_daemon

async def _exec_relink_dialog(self, reason: int) -> None:
self.rld = RelinkDialog(self.mdbx, self, reason).raise_()
RelinkDialog(self.mdbx, reason).raise_()

async def _exec_error_dialog(self, err: Exception) -> None:
title = "An unexpected error occurred"
Expand Down
3 changes: 1 addition & 2 deletions src/maestral_cocoa/dbx_location_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class DbxLocationDialog(Dialog):
ACCEPTED = 0
REJECTED = 1

def __init__(self, mdbx: MaestralProxy, app: toga.App) -> None:
def __init__(self, mdbx: MaestralProxy) -> None:
self.mdbx = mdbx
self.config_name = self.mdbx.config_name

Expand Down Expand Up @@ -72,7 +72,6 @@ def __init__(self, mdbx: MaestralProxy, app: toga.App) -> None:
default="Select",
accessory_view=self.combobox_dbx_location,
callback=self.on_dialog_pressed,
app=app,
)

self.msg_content.style.width = 450
Expand Down
11 changes: 1 addition & 10 deletions src/maestral_cocoa/dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,13 @@ def __init__(
accessory_view: toga.Widget = toga.Box(),
icon: toga.Icon | None = None,
callback: Callable | None = None,
app: toga.App | None = None,
):
super().__init__(
resizable=True,
closable=False,
minimizable=False,
title=" ",
is_dialog=True,
app=app,
)

if not callback:
Expand Down Expand Up @@ -169,7 +167,6 @@ def __init__(
msg_title: str = "Progress",
icon: toga.Icon = None,
callback: Callable | None = None,
app: toga.App | None = None,
) -> None:
self.progress_bar = toga.ProgressBar(
max=None,
Expand All @@ -187,7 +184,6 @@ def __init__(
icon=icon,
callback=callback,
accessory_view=self.progress_bar,
app=app,
)

# save some space...
Expand Down Expand Up @@ -219,7 +215,6 @@ def __init__(
callback=None,
details_title="Traceback",
details="",
app=None,
):
label = Label(
details_title,
Expand Down Expand Up @@ -250,7 +245,6 @@ def __init__(
icon=icon,
callback=callback,
accessory_view=accessory_view,
app=app,
)


Expand All @@ -273,7 +267,6 @@ def __init__(
version: str = "",
release_notes: str = "",
icon: toga.Icon | None = None,
app: toga.App | None = None,
) -> None:
link_button = FollowLinkButton(
text="GitHub Releases",
Expand Down Expand Up @@ -330,7 +323,6 @@ def __init__(
button_labels=("Ok",),
icon=icon,
accessory_view=accessory_view,
app=app,
)
self.msg_content.style.padding_bottom = 0
self.msg_content.style.height = 40
Expand All @@ -352,7 +344,7 @@ class RelinkDialog(Dialog):

CONTENT_WIDTH = 325

def __init__(self, mdbx: MaestralProxy, app: toga.App, reason: int) -> None:
def __init__(self, mdbx: MaestralProxy, reason: int) -> None:
self.mdbx = mdbx
self.reason = reason

Expand Down Expand Up @@ -400,7 +392,6 @@ def __init__(self, mdbx: MaestralProxy, app: toga.App, reason: int) -> None:
accessory_view=token_box,
button_labels=(self.LINK_BTN, self.CANCEL_BTN, self.UNLINK_BTN),
callback=self.on_dialog_press,
app=app,
)

self.dialog_buttons[self.LINK_BTN].enabled = False
Expand Down
1 change: 0 additions & 1 deletion src/maestral_cocoa/private/implementation/cocoa/factory.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-

# system imports
import os
import os.path as osp
import platform

Expand Down
4 changes: 0 additions & 4 deletions src/maestral_cocoa/private/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,6 @@ def __init__(
closable=True,
minimizable=True,
is_dialog=False,
app=None,
on_close=lambda x: True, # See https://github.com/beeware/toga/issues/1482
):
super().__init__(
Expand Down Expand Up @@ -693,9 +692,6 @@ def raise_(self):
self.show()
self._impl.force_to_front()

def close(self):
self._impl.close()

# sheet support

def show_as_sheet(self, window):
Expand Down
27 changes: 8 additions & 19 deletions src/maestral_cocoa/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import os.path as osp
import asyncio
from pathlib import Path
from typing import Any, TYPE_CHECKING
from typing import Any

# external imports
import toga
Expand All @@ -31,9 +31,6 @@
from .autostart import AutoStart
from .constants import FROZEN

if TYPE_CHECKING:
from .app import MaestralGui


class SettingsWindow(SettingsGui):
_update_interval_mapping = {
Expand All @@ -47,12 +44,11 @@ class SettingsWindow(SettingsGui):
_cached_pic_stat = os.stat(FACEHOLDER_PATH)
_cached_dbx_location = None

def __init__(self, mdbx: MaestralProxy, app: MaestralGui) -> None:
super().__init__(app=app)
def __init__(self, mdbx: MaestralProxy) -> None:
super().__init__()

self._refresh = False
self._refresh_interval = 2
self._refresh_interval_profile_pic = 60 * 45

self.on_close = self.on_close_pressed

Expand Down Expand Up @@ -246,23 +242,17 @@ def refresh_gui(self) -> None:
self._update_cli_tool_button()

async def refresh_profile_pic(self, interface, *args, **kwargs) -> None:
path = await call_async_maestral(self.mdbx.config_name, "get_profile_pic")
self.set_profile_pic(path)
try:
path = await call_async_maestral(self.mdbx.config_name, "get_profile_pic")
self.set_profile_pic(path)
except (ConnectionError, MaestralApiError, NotLinkedError):
pass

async def periodic_refresh_gui(self, interface, *args, **kwargs) -> None:
while self._refresh:
self.refresh_gui()
await asyncio.sleep(self._refresh_interval)

async def periodic_refresh_profile_pic(self, interface, *args, **kwargs) -> None:
while self._refresh:
try:
await self.refresh_profile_pic(self)
except (ConnectionError, MaestralApiError, NotLinkedError):
await asyncio.sleep(60 * 10)
else:
await asyncio.sleep(self._refresh_interval_profile_pic)

def set_account_info_from_cache(self) -> None:
acc_display_name = self.mdbx.get_state("account", "display_name")
acc_mail = self.mdbx.get_state("account", "email")
Expand All @@ -289,5 +279,4 @@ def on_close_pressed(self, sender: Any = None) -> bool:
def show(self) -> None:
self._refresh = True
self.app.add_background_task(self.periodic_refresh_gui)
self.app.add_background_task(self.periodic_refresh_profile_pic)
super().show()
4 changes: 2 additions & 2 deletions src/maestral_cocoa/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@


class SetupDialog(SetupDialogGui):
def __init__(self, mdbx: MaestralProxy, app: toga.App) -> None:
super().__init__(app=app)
def __init__(self, mdbx: MaestralProxy) -> None:
super().__init__()

self.mdbx = mdbx
self.config_name = self.mdbx.config_name
Expand Down
4 changes: 2 additions & 2 deletions src/maestral_cocoa/syncissues.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ def __init__(self, sync_err: SyncErrorEntry) -> None:


class SyncIssuesWindow(Window):
def __init__(self, mdbx: MaestralProxy, app: toga.App) -> None:
super().__init__(title="Maestral Sync Issues", app=app)
def __init__(self, mdbx: MaestralProxy) -> None:
super().__init__(title="Maestral Sync Issues")
self.on_close = self.on_close_pressed

self.mdbx = mdbx
Expand Down

0 comments on commit 4a50ae2

Please sign in to comment.