Skip to content
Snippets Groups Projects
Commit 4cd457af authored by Mateusz Żebrak's avatar Mateusz Żebrak Committed by Mateusz Kudela
Browse files

Refactor/fix dialogs - dismiss should be called in CliveActionDialog,...

Refactor/fix dialogs - dismiss should be called in CliveActionDialog, Confirmed message should be sent only when conditions are met and actions are done (eg. SwitchNodeAddressDialog, RemoveKeyAliasDialog)
parent 576148bc
No related branches found
No related tags found
2 merge requests!600v1.27.5.21 Release,!567Show dialogs instead of operation summary screens
...@@ -2,7 +2,6 @@ from __future__ import annotations ...@@ -2,7 +2,6 @@ from __future__ import annotations
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from textual import on
from textual.binding import Binding from textual.binding import Binding
from clive.__private.ui.dialogs.clive_base_dialogs import CliveActionDialog from clive.__private.ui.dialogs.clive_base_dialogs import CliveActionDialog
...@@ -27,8 +26,6 @@ class AddTrackedAccountDialog(CliveActionDialog): ...@@ -27,8 +26,6 @@ class AddTrackedAccountDialog(CliveActionDialog):
yield AccountManagementReference() yield AccountManagementReference()
yield self._add_account_container yield self._add_account_container
@on(CliveActionDialog.Confirmed) async def _perform_confirmation(self) -> bool:
async def save_account(self) -> None:
is_account_saved = await self._add_account_container.save_account() is_account_saved = await self._add_account_container.save_account()
if is_account_saved: return is_account_saved # noqa: RET504
self.dismiss()
...@@ -20,7 +20,6 @@ from clive.__private.ui.widgets.inputs.clive_input import CliveInput ...@@ -20,7 +20,6 @@ from clive.__private.ui.widgets.inputs.clive_input import CliveInput
if TYPE_CHECKING: if TYPE_CHECKING:
from textual.app import ComposeResult from textual.app import ComposeResult
CliveDialogVariant = Literal["default", "error"] CliveDialogVariant = Literal["default", "error"]
...@@ -123,14 +122,41 @@ class CliveActionDialog(CliveBaseDialog[ScreenResultT], ABC): ...@@ -123,14 +122,41 @@ class CliveActionDialog(CliveBaseDialog[ScreenResultT], ABC):
yield ConfirmOneLineButton(self._confirm_button_text) yield ConfirmOneLineButton(self._confirm_button_text)
yield CancelOneLineButton() yield CancelOneLineButton()
async def confirm_dialog(self) -> None:
"""Confirm the dialog which means try to perform the action and close the dialog if successful."""
is_confirmed = await self._perform_confirmation()
if is_confirmed:
self.post_message(self.Confirmed())
self._close_when_confirmed()
async def cancel_dialog(self) -> None:
"""Cancel the dialog which means close the dialog without performing any action."""
self._close_when_cancelled()
async def _perform_confirmation(self) -> bool:
"""Perform the action of the dialog and return True if it was successful."""
return True
def _close_when_confirmed(self) -> None:
self.dismiss()
def _close_when_cancelled(self) -> None:
self.dismiss()
@on(CliveInput.Submitted) @on(CliveInput.Submitted)
@on(ConfirmOneLineButton.Pressed) @on(ConfirmOneLineButton.Pressed)
async def confirm_dialog(self) -> None: async def _confirm_with_event(self) -> None:
self.post_message(self.Confirmed()) """By default, pressing the confirm button or submitting the input will confirm the dialog."""
await self.confirm_dialog()
@on(CancelOneLineButton.Pressed) @on(CancelOneLineButton.Pressed)
def action_cancel(self) -> None: async def _cancel_with_button(self) -> None:
self.dismiss() """By default, pressing the cancel button will cancel the dialog."""
await self.cancel_dialog()
async def _action_cancel(self) -> None:
"""By default, pressing the cancel key binding will cancel the dialog."""
await self.cancel_dialog()
class CliveInfoDialog(CliveBaseDialog[None], ABC): class CliveInfoDialog(CliveBaseDialog[None], ABC):
...@@ -139,6 +165,15 @@ class CliveInfoDialog(CliveBaseDialog[None], ABC): ...@@ -139,6 +165,15 @@ class CliveInfoDialog(CliveBaseDialog[None], ABC):
def create_buttons_content(self) -> ComposeResult: def create_buttons_content(self) -> ComposeResult:
yield CloseOneLineButton() yield CloseOneLineButton()
@on(CloseOneLineButton.Pressed) async def _close(self) -> None:
def action_close(self) -> None: """Close the dialog without performing any action."""
self.dismiss() self.dismiss()
@on(CloseOneLineButton.Pressed)
async def _close_with_button(self) -> None:
"""By default, pressing the close button will close the dialog."""
await self._close()
async def _action_close(self) -> None:
"""By default, pressing the close key binding will close the dialog."""
await self._close()
...@@ -2,12 +2,10 @@ from __future__ import annotations ...@@ -2,12 +2,10 @@ from __future__ import annotations
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from textual import on
from textual.widgets import Static from textual.widgets import Static
from clive.__private.ui.dialogs.clive_base_dialogs import CliveActionDialog, CliveDialogVariant from clive.__private.ui.dialogs.clive_base_dialogs import CliveActionDialog, CliveDialogVariant
from clive.__private.ui.get_css import get_relative_css_path from clive.__private.ui.get_css import get_relative_css_path
from clive.__private.ui.widgets.buttons import CancelOneLineButton, ConfirmOneLineButton
from clive.__private.ui.widgets.section import Section from clive.__private.ui.widgets.section import Section
if TYPE_CHECKING: if TYPE_CHECKING:
...@@ -39,12 +37,8 @@ class ConfirmActionDialog(CliveActionDialog[bool]): ...@@ -39,12 +37,8 @@ class ConfirmActionDialog(CliveActionDialog[bool]):
with Section(): with Section():
yield Static(self._confirm_question, id="confirm-question") yield Static(self._confirm_question, id="confirm-question")
@on(ConfirmOneLineButton.Pressed) def _close_when_confirmed(self) -> None:
async def confirm_dialog(self) -> None:
self.dismiss(result=True) self.dismiss(result=True)
@on(CancelOneLineButton.Pressed) def _close_when_cancelled(self) -> None:
def action_cancel(self, event: CancelOneLineButton.Pressed | None = None) -> None:
if event is not None:
event.prevent_default()
self.dismiss(result=False) self.dismiss(result=False)
...@@ -2,8 +2,6 @@ from __future__ import annotations ...@@ -2,8 +2,6 @@ from __future__ import annotations
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from textual import on
from clive.__private.ui.dialogs.confirm_action_dialog import ConfirmActionDialog from clive.__private.ui.dialogs.confirm_action_dialog import ConfirmActionDialog
if TYPE_CHECKING: if TYPE_CHECKING:
...@@ -26,9 +24,9 @@ class MarkAlarmAsHarmlessDialog(ConfirmActionDialog): ...@@ -26,9 +24,9 @@ class MarkAlarmAsHarmlessDialog(ConfirmActionDialog):
def alarm_info(self) -> str: def alarm_info(self) -> str:
return self._alarm.get_alarm_basic_info() return self._alarm.get_alarm_basic_info()
@on(ConfirmActionDialog.Confirmed) async def _perform_confirmation(self) -> bool:
def mark_alarm_as_harmless(self) -> None:
self._alarm.is_harmless = True self._alarm.is_harmless = True
self.notify(f"Alarm `{self.alarm_info}` was marked as harmless.") self.notify(f"Alarm `{self.alarm_info}` was marked as harmless.")
self.app.trigger_profile_watchers() self.app.trigger_profile_watchers()
self.dismiss() self.call_later(self.app.pop_screen) # pop the underlying AlarmInfoDialog
return True
...@@ -2,8 +2,6 @@ from __future__ import annotations ...@@ -2,8 +2,6 @@ from __future__ import annotations
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from textual import on
from clive.__private.ui.dialogs.confirm_action_dialog import ConfirmActionDialog from clive.__private.ui.dialogs.confirm_action_dialog import ConfirmActionDialog
if TYPE_CHECKING: if TYPE_CHECKING:
...@@ -27,8 +25,8 @@ class RemoveKeyAliasDialog(ConfirmActionDialog): ...@@ -27,8 +25,8 @@ class RemoveKeyAliasDialog(ConfirmActionDialog):
def key_alias(self) -> str: def key_alias(self) -> str:
return self._public_key.alias return self._public_key.alias
@on(ConfirmActionDialog.Confirmed) async def _perform_confirmation(self) -> bool:
def remove_key_alias(self) -> None:
self.profile.keys.remove(self._public_key) self.profile.keys.remove(self._public_key)
self.notify(f"Key alias `{self.key_alias}` was removed.") self.notify(f"Key alias `{self.key_alias}` was removed.")
self.app.trigger_profile_watchers() self.app.trigger_profile_watchers()
return True
...@@ -2,11 +2,8 @@ from __future__ import annotations ...@@ -2,11 +2,8 @@ from __future__ import annotations
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from textual import on
from clive.__private.ui.dialogs.clive_base_dialogs import CliveActionDialog from clive.__private.ui.dialogs.clive_base_dialogs import CliveActionDialog
from clive.__private.ui.get_css import get_relative_css_path from clive.__private.ui.get_css import get_relative_css_path
from clive.__private.ui.widgets.buttons import ConfirmButton
from clive.__private.ui.widgets.node_widgets import NodesList, SelectedNodeAddress from clive.__private.ui.widgets.node_widgets import NodesList, SelectedNodeAddress
from clive.__private.ui.widgets.section import Section from clive.__private.ui.widgets.section import Section
...@@ -25,11 +22,6 @@ class SwitchNodeAddressDialog(CliveActionDialog): ...@@ -25,11 +22,6 @@ class SwitchNodeAddressDialog(CliveActionDialog):
yield SelectedNodeAddress(self.node.http_endpoint) yield SelectedNodeAddress(self.node.http_endpoint)
yield NodesList() yield NodesList()
@on(ConfirmButton.Pressed) async def _perform_confirmation(self) -> bool:
async def switch_node_address(self) -> None:
await self._switch_node_address()
async def _switch_node_address(self) -> None:
change_node_succeeded = await self.query_exactly_one(NodesList).save_selected_node_address() change_node_succeeded = await self.query_exactly_one(NodesList).save_selected_node_address()
if change_node_succeeded: return change_node_succeeded # noqa: RET504
self.dismiss()
...@@ -2,7 +2,6 @@ from __future__ import annotations ...@@ -2,7 +2,6 @@ from __future__ import annotations
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from textual import on
from textual.binding import Binding from textual.binding import Binding
from clive.__private.ui.dialogs.clive_base_dialogs import CliveActionDialog from clive.__private.ui.dialogs.clive_base_dialogs import CliveActionDialog
...@@ -29,7 +28,6 @@ class SwitchWorkingAccountDialog(CliveActionDialog): ...@@ -29,7 +28,6 @@ class SwitchWorkingAccountDialog(CliveActionDialog):
yield AccountManagementReference() yield AccountManagementReference()
yield self._switch_working_account_container yield self._switch_working_account_container
@on(CliveActionDialog.Confirmed) async def _perform_confirmation(self) -> bool:
def confirm_selected_working_account(self) -> None:
self._switch_working_account_container.confirm_selected_working_account() self._switch_working_account_container.confirm_selected_working_account()
self.dismiss() return True
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment