-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
251 additions
and
164 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from __future__ import annotations | ||
|
||
|
||
# external imports | ||
import toga | ||
from maestral.daemon import MaestralProxy | ||
|
||
# local imports | ||
from .bandwidth_gui import BandwidthGui | ||
from .private.widgets import Window | ||
|
||
|
||
MB_2_BYTES = 10**6 | ||
|
||
|
||
class BandwidthDialog(BandwidthGui): | ||
def __init__(self, mdbx: MaestralProxy, app: toga.App): | ||
super().__init__(app=app, is_dialog=True) | ||
|
||
self.mdbx = mdbx | ||
self.dialog_buttons.on_press = self.on_dialog_pressed | ||
|
||
self.radio_button_unlimited_down.on_change = self.on_limit_downloads_toggled | ||
self.radio_button_limited_down.on_change = self.on_limit_downloads_toggled | ||
self.radio_button_unlimited_up.on_change = self.on_limit_uploads_toggled | ||
self.radio_button_limited_up.on_change = self.on_limit_uploads_toggled | ||
|
||
def refresh_gui(self) -> None: | ||
if self.mdbx.bandwidth_limit_down == 0: | ||
self.radio_button_unlimited_down.value = True | ||
self.number_input_limit_down.enabled = False | ||
else: | ||
self.radio_button_limited_down.value = True | ||
self.number_input_limit_down.value = ( | ||
self.mdbx.bandwidth_limit_down / MB_2_BYTES | ||
) | ||
self.number_input_limit_down.enabled = True | ||
|
||
if self.mdbx.bandwidth_limit_up == 0: | ||
self.radio_button_unlimited_up.value = True | ||
self.number_input_limit_up.enabled = False | ||
else: | ||
self.radio_button_limited_up.value = True | ||
self.number_input_limit_up.value = self.mdbx.bandwidth_limit_up / MB_2_BYTES | ||
self.number_input_limit_up.enabled = True | ||
|
||
def update_settings(self): | ||
if self.radio_button_unlimited_down.value is True: | ||
self.mdbx.bandwidth_limit_down = 0.0 | ||
else: | ||
self.mdbx.bandwidth_limit_down = ( | ||
float(self.number_input_limit_down.value) * MB_2_BYTES | ||
) | ||
|
||
if self.radio_button_unlimited_up.value is True: | ||
self.mdbx.bandwidth_limit_up = 0.0 | ||
else: | ||
self.mdbx.bandwidth_limit_up = ( | ||
float(self.number_input_limit_up.value) * MB_2_BYTES | ||
) | ||
|
||
async def on_limit_downloads_toggled(self, widget: toga.Selection) -> None: | ||
if widget is self.radio_button_unlimited_down: | ||
self.number_input_limit_down.enabled = False | ||
elif widget is self.radio_button_limited_down: | ||
self.number_input_limit_down.enabled = True | ||
else: | ||
raise RuntimeError(f"Unexpected widget {widget}") | ||
|
||
async def on_limit_uploads_toggled(self, widget: toga.Selection) -> None: | ||
if widget is self.radio_button_unlimited_up: | ||
self.number_input_limit_up.enabled = False | ||
elif widget is self.radio_button_limited_up: | ||
self.number_input_limit_up.enabled = True | ||
else: | ||
raise RuntimeError(f"Unexpected widget {widget}") | ||
|
||
async def on_dialog_pressed(self, btn_name: str) -> None: | ||
try: | ||
if btn_name == "Update": | ||
self.update_settings() | ||
finally: | ||
self.close() | ||
|
||
def show_as_sheet(self, window: Window) -> None: | ||
self.refresh_gui() | ||
super().show_as_sheet(window) | ||
|
||
def show(self) -> None: | ||
self.refresh_gui() | ||
super().show() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
# external imports | ||
import toga | ||
from toga.style.pack import Pack | ||
from toga.constants import COLUMN, TOP, RIGHT, LEFT | ||
|
||
# local imports | ||
from .private.widgets import Label, RadioButton, Window, DialogButtons | ||
|
||
|
||
class BandwidthGui(Window): | ||
|
||
COLUMN_WIDTH_LEFT = 100 | ||
COLUMN_WIDTH_RIGHT = 250 | ||
|
||
ELEMENT_PADDING = 10 | ||
COLUMN_PADDING = 10 | ||
|
||
def __init__(self, **kwargs) -> None: | ||
super().__init__(title="Bandwidth Settings", size=(400, 250), **kwargs) | ||
|
||
self._label_download_rate = Label( | ||
"Download rate:", | ||
style=Pack(text_align=RIGHT, width=BandwidthGui.COLUMN_WIDTH_LEFT), | ||
) | ||
|
||
self.radio_button_unlimited_down = RadioButton( | ||
"Don't limit", group=RadioButton.Group.A | ||
) | ||
self.radio_button_limited_down = RadioButton( | ||
"Limit to:", group=RadioButton.Group.A | ||
) | ||
self.number_input_limit_down = toga.NumberInput( | ||
value=1, | ||
min_value=0.005, | ||
style=Pack(padding_left=BandwidthGui.COLUMN_PADDING, width=50), | ||
) | ||
self._unit_label_down = toga.Label( | ||
"MB/s", | ||
style=Pack(padding_left=BandwidthGui.COLUMN_PADDING, width=50), | ||
) | ||
|
||
self._label_upload_rate = Label( | ||
"Upload rate:", | ||
style=Pack(text_align=RIGHT, width=BandwidthGui.COLUMN_WIDTH_LEFT), | ||
) | ||
|
||
self.radio_button_unlimited_up = RadioButton( | ||
"Don't limit", group=RadioButton.Group.B | ||
) | ||
self.radio_button_limited_up = RadioButton( | ||
"Limit to:", group=RadioButton.Group.B | ||
) | ||
self.number_input_limit_up = toga.NumberInput( | ||
value=1, | ||
min_value=0.005, | ||
style=Pack(padding_left=BandwidthGui.COLUMN_PADDING, width=50), | ||
) | ||
self._unit_label_up = toga.Label( | ||
"MB/s", | ||
style=Pack(padding_left=BandwidthGui.COLUMN_PADDING, width=50), | ||
) | ||
|
||
self.dialog_buttons = DialogButtons( | ||
labels=["Update", "Cancel"], | ||
style=Pack(padding=(20, 20, 20, 20), flex=1), | ||
) | ||
|
||
children = [ | ||
toga.Box( | ||
children=[ | ||
self._label_download_rate, | ||
toga.Box( | ||
children=[ | ||
self.radio_button_unlimited_down, | ||
toga.Box( | ||
children=[ | ||
self.radio_button_limited_down, | ||
self.number_input_limit_down, | ||
self._unit_label_down, | ||
], | ||
), | ||
], | ||
style=Pack( | ||
alignment=TOP, | ||
direction=COLUMN, | ||
padding_left=BandwidthGui.COLUMN_PADDING, | ||
), | ||
), | ||
], | ||
), | ||
toga.Box( | ||
children=[ | ||
self._label_upload_rate, | ||
toga.Box( | ||
children=[ | ||
self.radio_button_unlimited_up, | ||
toga.Box( | ||
children=[ | ||
self.radio_button_limited_up, | ||
self.number_input_limit_up, | ||
self._unit_label_up, | ||
], | ||
), | ||
], | ||
style=Pack( | ||
alignment=TOP, | ||
direction=COLUMN, | ||
padding_left=BandwidthGui.COLUMN_PADDING, | ||
), | ||
), | ||
], | ||
style=Pack(padding_top=BandwidthGui.ELEMENT_PADDING), | ||
), | ||
self.dialog_buttons, | ||
] | ||
|
||
main_box = toga.Box( | ||
children=children, | ||
style=Pack( | ||
direction=COLUMN, | ||
padding=30, | ||
width=BandwidthGui.COLUMN_WIDTH_LEFT + BandwidthGui.COLUMN_WIDTH_RIGHT, | ||
flex=1, | ||
alignment=LEFT, | ||
), | ||
) | ||
|
||
self.content = main_box |
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
Oops, something went wrong.