Skip to content

Commit

Permalink
xmr: ui - fix QR code, zoom by swipe
Browse files Browse the repository at this point in the history
  • Loading branch information
ph4r05 committed Jun 18, 2020
1 parent e630279 commit efe15fd
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 29 deletions.
8 changes: 4 additions & 4 deletions core/src/apps/monero/get_address.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from trezor.messages.MoneroAddress import MoneroAddress

from apps.common import paths
from apps.common.layout import address_n_to_str, show_qr
from apps.common.layout import address_n_to_str
from apps.common.seed import with_slip44_keychain
from apps.monero import CURVE, SLIP44_ID, misc
from apps.monero.layout import confirms
from apps.monero.layout import address as layout_address
from apps.monero.xmr import addresses, crypto, monero
from apps.monero.xmr.networks import net_version

Expand Down Expand Up @@ -45,9 +45,9 @@ async def get_address(ctx, msg, keychain):
if msg.show_display:
desc = address_n_to_str(msg.address_n)
while True:
if await confirms.show_address(ctx, addr.decode(), desc=desc):
if await layout_address.show_address(ctx, addr.decode(), desc=desc):
break
if await show_qr(ctx, "monero:" + addr.decode(), desc=desc):
if await layout_address.show_qr(ctx, "monero:" + addr.decode(), desc=desc):
break

return MoneroAddress(address=addr)
131 changes: 131 additions & 0 deletions core/src/apps/monero/layout/address.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
from micropython import const

from trezor import loop, ui, wire
from trezor.messages import ButtonRequestType
from trezor.ui.button import ButtonDefault
from trezor.ui.confirm import CONFIRMED, Confirm
from trezor.ui.container import Container
from trezor.ui.qr import Qr
from trezor.ui.scroll import Paginated
from trezor.ui.text import Text
from trezor.wire import Context

from apps.common import button_request
from apps.common.confirm import confirm
from apps.monero.layout import common

if False:
from typing import Optional, Tuple

if __debug__:
from apps.debug import swipe_signal


async def show_address(
ctx, address: str, desc: str = "Confirm address", network: str = None
):
pages = []
for lines in common.paginate_lines(common.split_address(address), 5):
text = Text(desc, ui.ICON_RECEIVE, ui.GREEN)
if network is not None:
text.normal("%s network" % network)
text.mono(*lines)
pages.append(text)

return await confirm(
ctx,
Paginated(pages),
code=ButtonRequestType.Address,
cancel="QR",
cancel_style=ButtonDefault,
)


def render_scrollbar(pages: int, page: int) -> None:
BBOX = const(220)
SIZE = const(8)

padding = 14
if pages * padding > BBOX:
padding = BBOX // pages

X = const(232)
Y = (BBOX // 2) - (pages // 2) * padding

for i in range(0, pages):
if i == page:
fg = ui.FG
else:
fg = ui.GREY
ui.display.bar_radius(X, Y + i * padding, SIZE, SIZE, fg, ui.BG, 4)


class ZoomingQrConfirm(Confirm):
def __init__(
self,
ctx: Context,
qr_data: str,
qr_text: Optional[str],
cancel: str,
qr_scales: Tuple[int] = (3, 5),
) -> None:
self.ctx = ctx
self.qr_data = qr_data
self.qr_text = qr_text
self.qr_scales = qr_scales
self.cur_scale = len(qr_scales) - 1
self.content = None # type: Optional[ui.Component]
self.gen_qr()
super().__init__(
self.content,
confirm=Confirm.DEFAULT_CONFIRM,
confirm_style=Confirm.DEFAULT_CONFIRM_STYLE,
cancel=cancel,
cancel_style=ButtonDefault,
major_confirm=False,
)

async def show(self):
await button_request(self.ctx, code=ButtonRequestType.Address)
return await self.ctx.wait(self) is CONFIRMED

def dispatch(self, event: int, x: int, y: int) -> None:
super().dispatch(event, x, y)
self.content.dispatch(event, x, y)
if event is ui.RENDER:
render_scrollbar(len(self.qr_scales), self.cur_scale)

def gen_qr(self):
QR_X = const(120)
QR_Y = const(115)
self.cur_scale = (self.cur_scale + 1) % len(self.qr_scales)
qr = Qr(self.qr_data, QR_X, QR_Y, self.qr_scales[self.cur_scale])
text = Text(self.qr_text, ui.ICON_RECEIVE, ui.GREEN) if self.qr_text else None
self.content = Container(qr, text) if text else qr

async def handle_paging(self) -> None:
from trezor.ui.swipe import SWIPE_ALL, Swipe

directions = SWIPE_ALL

if __debug__:
_ = await loop.race(Swipe(directions), swipe_signal())
else:
_ = await Swipe(directions)

self.gen_qr()
self.content.repaint = True

def create_tasks(self) -> Tuple[loop.Task, ...]:
tasks = super().create_tasks()
return tasks + (self.handle_paging(),)


async def show_qr(
ctx: wire.Context,
address: str,
desc: str = "Confirm address",
cancel: str = "Address",
) -> bool:
content = ZoomingQrConfirm(ctx, address, desc, cancel)
return await content.show()
25 changes: 0 additions & 25 deletions core/src/apps/monero/layout/confirms.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,28 +218,3 @@ async def live_refresh_step(ctx, current):
if current is None:
return
await Popup(LiveRefreshStep(current))


async def show_address(
ctx, address: str, desc: str = "Confirm address", network: str = None
):
from apps.common.confirm import confirm
from trezor.messages import ButtonRequestType
from trezor.ui.button import ButtonDefault
from trezor.ui.scroll import Paginated

pages = []
for lines in common.paginate_lines(common.split_address(address), 5):
text = Text(desc, ui.ICON_RECEIVE, ui.GREEN)
if network is not None:
text.normal("%s network" % network)
text.mono(*lines)
pages.append(text)

return await confirm(
ctx,
Paginated(pages),
code=ButtonRequestType.Address,
cancel="QR",
cancel_style=ButtonDefault,
)

0 comments on commit efe15fd

Please sign in to comment.