mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-02-05 12:21:16 +00:00
apps: remove split_words, use Text layout functionality
This commit is contained in:
parent
f074360cd2
commit
38bbb9dd83
@ -1,9 +1,7 @@
|
||||
from ubinascii import hexlify
|
||||
|
||||
from trezor import ui
|
||||
from trezor.crypto.hashlib import sha256
|
||||
from trezor.ui.text import TEXT_MARGIN_LEFT
|
||||
from trezor.utils import HashWriter, chunks, split_words
|
||||
from trezor.utils import HashWriter
|
||||
|
||||
from apps.wallet.sign_tx.signing import write_varint
|
||||
|
||||
@ -21,13 +19,10 @@ def message_digest(coin, message):
|
||||
|
||||
|
||||
def split_message(message):
|
||||
def measure(s):
|
||||
return ui.display.text_width(s, ui.NORMAL)
|
||||
|
||||
try:
|
||||
m = bytes(message).decode()
|
||||
lines = split_words(m, ui.WIDTH - 2 * TEXT_MARGIN_LEFT, metric=measure)
|
||||
words = m.split(" ")
|
||||
except UnicodeError:
|
||||
m = hexlify(message)
|
||||
lines = chunks(m, 16)
|
||||
return lines
|
||||
words = [m]
|
||||
return words
|
||||
|
@ -34,6 +34,6 @@ async def sign_message(ctx, msg):
|
||||
|
||||
async def require_confirm_sign_message(ctx, message):
|
||||
message = split_message(message)
|
||||
text = Text("Sign ETH message")
|
||||
text = Text("Sign ETH message", new_lines=False)
|
||||
text.normal(*message)
|
||||
await require_confirm(ctx, text)
|
||||
|
@ -32,10 +32,10 @@ async def verify_message(ctx, msg):
|
||||
|
||||
|
||||
async def require_confirm_verify_message(ctx, address, message):
|
||||
text = Text("Confirm address")
|
||||
text = Text("Confirm address", new_lines=False)
|
||||
text.mono(*split_address(address))
|
||||
await require_confirm(ctx, text)
|
||||
|
||||
text = Text("Verify message")
|
||||
text = Text("Verify message", new_lines=False)
|
||||
text.mono(*split_message(message))
|
||||
await require_confirm(ctx, text)
|
||||
|
@ -39,6 +39,6 @@ async def sign_message(ctx, msg):
|
||||
|
||||
|
||||
async def require_confirm_sign_message(ctx, message):
|
||||
text = Text("Sign Lisk message")
|
||||
text = Text("Sign Lisk message", new_lines=False)
|
||||
text.normal(*split_message(message))
|
||||
await require_confirm(ctx, text)
|
||||
|
@ -1,7 +1,7 @@
|
||||
from trezor import ui
|
||||
from trezor.messages import ButtonRequestType
|
||||
from trezor.ui.text import Text
|
||||
from trezor.utils import format_amount, split_words
|
||||
from trezor.utils import format_amount
|
||||
|
||||
from .helpers import NEM_MAX_DIVISIBILITY
|
||||
|
||||
@ -9,8 +9,10 @@ from apps.common.confirm import require_confirm, require_hold_to_confirm
|
||||
|
||||
|
||||
async def require_confirm_text(ctx, action: str):
|
||||
words = split_words(action, 18)
|
||||
await require_confirm_content(ctx, "Confirm action", words)
|
||||
content = action.split(" ")
|
||||
text = Text("Confirm action", ui.ICON_SEND, icon_color=ui.GREEN, new_lines=False)
|
||||
text.normal(*content)
|
||||
await require_confirm(ctx, text, ButtonRequestType.ConfirmOutput)
|
||||
|
||||
|
||||
async def require_confirm_fee(ctx, action: str, fee: int):
|
||||
|
@ -12,7 +12,6 @@ from trezor.messages import (
|
||||
from trezor.ui.confirm import ConfirmDialog
|
||||
from trezor.ui.scroll import Scrollpage, animate_swipe, paginate
|
||||
from trezor.ui.text import Text
|
||||
from trezor.utils import split_words
|
||||
|
||||
from ..layout import (
|
||||
require_confirm_content,
|
||||
@ -97,9 +96,10 @@ def _get_mosaic_properties(definition: NEMMosaicDefinition):
|
||||
|
||||
# description
|
||||
if definition.description:
|
||||
t = Text("Confirm properties", ui.ICON_SEND)
|
||||
t = Text("Confirm properties", ui.ICON_SEND, new_lines=False)
|
||||
t.bold("Description:")
|
||||
t.normal(*split_words(definition.description, 22))
|
||||
t.br()
|
||||
t.normal(*definition.description.split(" "))
|
||||
properties.append(t)
|
||||
|
||||
# transferable
|
||||
|
@ -9,7 +9,7 @@ from trezor.messages import (
|
||||
NEMTransfer,
|
||||
)
|
||||
from trezor.ui.text import Text
|
||||
from trezor.utils import format_amount, split_words
|
||||
from trezor.utils import format_amount
|
||||
|
||||
from ..helpers import (
|
||||
NEM_LEVY_PERCENTILE_DIVISOR_ABSOLUTE,
|
||||
@ -68,11 +68,9 @@ async def ask_transfer_mosaic(
|
||||
else:
|
||||
msg = Text("Confirm mosaic", ui.ICON_SEND, icon_color=ui.RED)
|
||||
msg.bold("Unknown mosaic!")
|
||||
msg.normal(
|
||||
*split_words(
|
||||
"Divisibility and levy cannot be shown for unknown mosaics", 22
|
||||
)
|
||||
)
|
||||
msg.normal("Divisibility and levy")
|
||||
msg.normal("cannot be shown for")
|
||||
msg.normal("unknown mosaics")
|
||||
await require_confirm(ctx, msg, ButtonRequestType.ConfirmOutput)
|
||||
|
||||
msg = Text("Confirm mosaic", ui.ICON_SEND, icon_color=ui.GREEN)
|
||||
@ -136,9 +134,9 @@ async def _require_confirm_payload(ctx, payload: bytearray, encrypt=False):
|
||||
if encrypt:
|
||||
text = Text("Confirm payload", ui.ICON_SEND, icon_color=ui.GREEN)
|
||||
text.bold("Encrypted:")
|
||||
text.normal(*split_words(payload, 22))
|
||||
text.normal(*payload.split(" "))
|
||||
else:
|
||||
text = Text("Confirm payload", ui.ICON_SEND, icon_color=ui.RED)
|
||||
text.bold("Unencrypted:")
|
||||
text.normal(*split_words(payload, 22))
|
||||
text.normal(*payload.split(" "))
|
||||
await require_confirm(ctx, text, ButtonRequestType.ConfirmOutput)
|
||||
|
@ -40,6 +40,6 @@ async def sign_message(ctx, msg):
|
||||
|
||||
async def require_confirm_sign_message(ctx, message):
|
||||
message = split_message(message)
|
||||
text = Text("Sign message")
|
||||
text = Text("Sign message", new_lines=False)
|
||||
text.normal(*message)
|
||||
await require_confirm(ctx, text)
|
||||
|
@ -68,6 +68,6 @@ async def require_confirm_verify_message(ctx, address, message):
|
||||
text.mono(*split_address(address))
|
||||
await require_confirm(ctx, text)
|
||||
|
||||
text = Text("Verify message")
|
||||
text = Text("Verify message", new_lines=False)
|
||||
text.normal(*split_message(message))
|
||||
await require_confirm(ctx, text)
|
||||
|
@ -140,6 +140,9 @@ class Text(ui.LazyWidget):
|
||||
self.content.extend(content)
|
||||
self.content.append(ui.NORMAL)
|
||||
|
||||
def br(self):
|
||||
self.content.append(BR)
|
||||
|
||||
def render(self):
|
||||
ui.header(
|
||||
self.header_text, self.header_icon, ui.TITLE_GREY, ui.BG, self.icon_color
|
||||
|
@ -47,27 +47,6 @@ def chunks(items, size):
|
||||
yield items[i : i + size]
|
||||
|
||||
|
||||
def split_words(sentence, width, metric=len):
|
||||
line = []
|
||||
for w in sentence.split(" "):
|
||||
# empty word -> skip
|
||||
if not w:
|
||||
continue
|
||||
# new word will not fit -> break the line
|
||||
if metric(" ".join(line + [w])) >= width:
|
||||
yield " ".join(line)
|
||||
line = []
|
||||
# word is too wide -> split the word
|
||||
while metric(w) >= width:
|
||||
for i in range(1, len(w) + 1):
|
||||
if metric(w[:-i]) < width:
|
||||
yield w[:-i] + "-"
|
||||
w = w[-i:]
|
||||
break
|
||||
line.append(w)
|
||||
yield " ".join(line)
|
||||
|
||||
|
||||
def format_amount(amount, decimals):
|
||||
d = pow(10, decimals)
|
||||
amount = ("%d.%0*d" % (amount // d, decimals, amount % d)).rstrip("0")
|
||||
|
Loading…
Reference in New Issue
Block a user