Skip to content
Snippets Groups Projects
Commit 51e69a7a authored by Jakub Ziebinski's avatar Jakub Ziebinski
Browse files

Implement generous button widget

parent 962596e8
No related branches found
No related tags found
2 merge requests!309V1.27.5.7 release,!297Implement `Power up` part of hive power management
......@@ -2,24 +2,24 @@ from __future__ import annotations
from typing import TYPE_CHECKING
from textual import on
from textual.containers import Grid, ScrollableContainer
from textual.containers import Horizontal, ScrollableContainer, Vertical
from textual.widgets import Static, TabPane
from clive.__private.ui.get_css import get_css_from_relative_path
from clive.__private.ui.operations.bindings.operation_action_bindings import OperationActionBindings
from clive.__private.ui.widgets.clive_button import CliveButton
from clive.__private.ui.widgets.generous_button import GenerousButton
from clive.__private.ui.widgets.inputs.account_name_input import AccountNameInput
from clive.__private.ui.widgets.inputs.clive_validated_input import CliveValidatedInput
from clive.__private.ui.widgets.inputs.hive_asset_amount_input import HiveAssetAmountInput
from clive.__private.ui.widgets.notice import Notice
from clive.models import Asset
from schemas.operations import TransferToVestingOperation as TransferToVesting
if TYPE_CHECKING:
from rich.text import TextType
from textual.app import ComposeResult
from clive.models import Asset
class ScrollablePart(ScrollableContainer):
pass
......@@ -46,19 +46,14 @@ class PowerUp(TabPane, OperationActionBindings):
with ScrollablePart():
yield Static("Power up corresponds to a `transfer to vesting` operation", id="operation-name-info")
yield Notice("Your governance voting power will be increased after 30 days")
with Grid(id="power-up-inputs"):
with Vertical(id="power-up-inputs"):
yield self._receiver_input
yield self._asset_input
yield CliveButton("All !", id_="clive-button-all", variant="success")
@on(CliveButton.Pressed, "#clive-button-all")
def fill_input_by_all(self) -> None:
"""If the balance is not 0, fill the amount input with the entire HIVE balance."""
hive_balance = self.app.world.profile_data.working_account.data.hive_balance
if float(hive_balance.amount) == 0:
self.notify("Zero is not enough value to make power up", severity="warning")
return
self._asset_input.input.value = Asset.pretty_amount(hive_balance)
with Horizontal(id="input-with-button"):
yield self._asset_input
yield GenerousButton(self._asset_input, self._get_hive_balance) # type: ignore[arg-type]
def _get_hive_balance(self) -> Asset.Hive:
return self.app.world.profile_data.working_account.data.hive_balance
def _create_operation(self) -> TransferToVesting | None:
if not CliveValidatedInput.validate_many(self._asset_input, self._receiver_input):
......
......@@ -14,22 +14,21 @@ Notice {
}
#power-up-inputs {
grid-size: 2;
grid-columns: 1fr 13;
grid-gutter: 1;
grid-rows: auto;
background: $panel;
padding: 2 4;
padding: 2 2;
height: auto;
}
AccountNameInput {
column-span: 2;
#input-with-button {
margin-top: 1;
height: auto;
max-height: 4;
}
CliveButton {
#input-with-button HiveAssetAmountInput {
width: 1fr;
}
#input-with-button GenerousButton {
width: 14;
height: auto;
min-width: 14;
}
from __future__ import annotations
from typing import TYPE_CHECKING
from textual import on
from clive.__private.ui.widgets.clive_button import CliveButton
from clive.models import Asset
if TYPE_CHECKING:
from collections.abc import Callable
from clive.__private.ui.widgets.inputs.clive_validated_input import CliveValidatedInput
class GenerousButton(CliveButton):
"""Button that fill the related input with the entire selected asset balance."""
def __init__(
self,
related_input: CliveValidatedInput[Asset.AnyT],
amount_callback: Callable[[], Asset.AnyT],
*,
id_: str | None = None,
classes: str | None = None,
):
super().__init__(label="All!", variant="success", id_=id_, classes=classes)
self._related_input = related_input
self._amount_callback = amount_callback
@on(CliveButton.Pressed)
def fill_input_by_all(self) -> None:
"""If the balance is not 0, fill the related input with the entire selected asset balance."""
if int(self._amount_callback().amount) == 0:
self.notify("Zero is not a enough value to perform this action", severity="warning")
return
self._related_input.input.value = Asset.pretty_amount(self._amount_callback())
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