Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error from new textualize from dismiss() in call_after_refresh. #412

Merged
merged 3 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions datashuttle/tui/screens/modal_dialogs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Optional
from typing import TYPE_CHECKING, Callable, Optional

if TYPE_CHECKING:
from pathlib import Path
Expand Down Expand Up @@ -72,9 +72,10 @@ class FinishTransferScreen(ModalScreen):
taking user input ('OK' or 'Cancel').
"""

def __init__(self, message: str) -> None:
def __init__(self, message: str, transfer_func: Callable) -> None:
super().__init__()

self.transfer_func = transfer_func
self.message = message

def compose(self) -> ComposeResult:
Expand All @@ -90,15 +91,15 @@ def compose(self) -> ComposeResult:

def on_button_pressed(self, event: Button.Pressed) -> None:
if event.button.id == "confirm_ok_button":
# Update the display to 'transferring' before TUI freezes
# during data transfer.
# Update the display to 'transferring' before
# TUI freezes during data transfer.
self.query_one("#confirm_button_container").visible = False
self.query_one("#confirm_message_label").update("Transferring...")
self.query_one("#confirm_message_label").call_after_refresh(
lambda: self.dismiss(True)
lambda: self.transfer_func(True)
)
else:
self.dismiss(False)
self.transfer_func(False)


class SelectDirectoryTreeScreen(ModalScreen):
Expand Down
33 changes: 30 additions & 3 deletions datashuttle/tui/tabs/transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def __init__(
self.show_legend = self.mainwindow.load_global_settings()[
"show_transfer_tree_status"
]
self.finish_transfer_screen: Optional[FinishTransferScreen] = None

# Setup
# ----------------------------------------------------------------------------------
Expand Down Expand Up @@ -277,8 +278,22 @@ def on_button_pressed(self, event: Button.Pressed) -> None:
to confirm that the user wishes to transfer their data
(in the direction selected). If "Yes" is selected,
`self.transfer_data` (see below) is run.
"""

Notes
-----
The way that Textualize works makes it difficult (impossible?)
to render a screen but keep the main loop with another screen.
What we want to do is 1) select 'OK' to start transfer
2) show 'Transferring...' 3) transfer data 4) tear down 'transferring'
screen 5) show confirmation screen. This is not simple if we want to
manage the actual transfer in this screen, which makes a lot of
sense as all options are held here The alternative is to pass all settings
JoeZiminski marked this conversation as resolved.
Show resolved Hide resolved
to a `Transferring` modal dialog which seems even more convoluted. So,
`FinishTransferScreen` calls back to `self.transfer_data`. If 'OK' was
selected, the message is changed to 'Transferring'. Then, `self.transfer_data`
handles data transfer, teardown of the `FinishTransferScreen`
and display of the confirmation screen.
"""
if event.button.id == "transfer_transfer_button":
if not self.query_one("#transfer_switch").value:
direction = "upload"
Expand All @@ -294,9 +309,15 @@ def on_button_pressed(self, event: Button.Pressed) -> None:
" central filesystem.\n\nAre you sure you wish to proceed?\n",
)

self.mainwindow.push_screen(
FinishTransferScreen(message), self.transfer_data
# This is very convoluted. See docstring for details.
assert (
self.finish_transfer_screen is None
), "`finish_transfer_screen` should de cleaned up in `transfer_data`."

self.finish_transfer_screen = FinishTransferScreen(
message, self.transfer_data
)
self.mainwindow.push_screen(self.finish_transfer_screen)

def on_custom_directory_tree_directory_tree_special_key_press(
self, event: CustomDirectoryTree.DirectoryTreeSpecialKeyPress
Expand Down Expand Up @@ -337,6 +358,12 @@ def transfer_data(self, transfer_bool: bool) -> None:
transfer by clicking "Yes".

"""
# Teardown the screen first, whatever `transfer_bool` is.
# It will not be updated until the transfer is complete.
assert self.finish_transfer_screen is not None
self.finish_transfer_screen.dismiss()
self.finish_transfer_screen = None

if transfer_bool:
upload = not self.query_one("#transfer_switch").value

Expand Down
2 changes: 1 addition & 1 deletion tests/tests_tui/test_tui_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ async def test_show_transfer_tree_status(self, setup_project_paths):
with pytest.raises(BaseException) as e:
transfer_tab.query_one("#transfer_legend")

assert "No nodes match <DOMQuery query" in str(e)
assert "No nodes match" in str(e)
await pilot.pause()

# Go to the settings page and turn on transfer tree styling.
Expand Down
Loading