mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-01-10 23:40:58 +00:00
core/ui: Add text_center_trim_left() and text_center_trim_right().
This commit is contained in:
parent
84674a7463
commit
1f58ee7ae9
@ -208,3 +208,51 @@ class Label(ui.Component):
|
|||||||
tx, ty, self.content, self.style, ui.FG, ui.BG, aw
|
tx, ty, self.content, self.style, ui.FG, ui.BG, aw
|
||||||
)
|
)
|
||||||
self.repaint = False
|
self.repaint = False
|
||||||
|
|
||||||
|
|
||||||
|
def text_center_trim_left(
|
||||||
|
x: int, y: int, text: str, font: int = ui.NORMAL, width: int = ui.WIDTH - 16
|
||||||
|
) -> None:
|
||||||
|
if ui.display.text_width(text, font) <= width:
|
||||||
|
ui.display.text_center(x, y, text, font, ui.FG, ui.BG)
|
||||||
|
return
|
||||||
|
|
||||||
|
ELLIPSIS_WIDTH = ui.display.text_width("...", ui.BOLD)
|
||||||
|
if width < ELLIPSIS_WIDTH:
|
||||||
|
return
|
||||||
|
|
||||||
|
text_length = 0
|
||||||
|
for i in range(1, len(text)):
|
||||||
|
if ui.display.text_width(text[-i:], font) + ELLIPSIS_WIDTH > width:
|
||||||
|
text_length = i - 1
|
||||||
|
break
|
||||||
|
|
||||||
|
text_width = ui.display.text_width(text[-text_length:], font)
|
||||||
|
x -= (text_width + ELLIPSIS_WIDTH) // 2
|
||||||
|
ui.display.text(x, y, "...", ui.BOLD, ui.GREY, ui.BG)
|
||||||
|
x += ELLIPSIS_WIDTH
|
||||||
|
ui.display.text(x, y, text[-text_length:], font, ui.FG, ui.BG)
|
||||||
|
|
||||||
|
|
||||||
|
def text_center_trim_right(
|
||||||
|
x: int, y: int, text: str, font: int = ui.NORMAL, width: int = ui.WIDTH - 16
|
||||||
|
) -> None:
|
||||||
|
if ui.display.text_width(text, font) <= width:
|
||||||
|
ui.display.text_center(x, y, text, font, ui.FG, ui.BG)
|
||||||
|
return
|
||||||
|
|
||||||
|
ELLIPSIS_WIDTH = ui.display.text_width("...", ui.BOLD)
|
||||||
|
if width < ELLIPSIS_WIDTH:
|
||||||
|
return
|
||||||
|
|
||||||
|
text_length = 0
|
||||||
|
for i in range(1, len(text)):
|
||||||
|
if ui.display.text_width(text[:i], font) + ELLIPSIS_WIDTH > width:
|
||||||
|
text_length = i - 1
|
||||||
|
break
|
||||||
|
|
||||||
|
text_width = ui.display.text_width(text[:text_length], font)
|
||||||
|
x -= (text_width + ELLIPSIS_WIDTH) // 2
|
||||||
|
ui.display.text(x, y, text[:text_length], font, ui.FG, ui.BG)
|
||||||
|
x += text_width
|
||||||
|
ui.display.text(x, y, "...", ui.BOLD, ui.GREY, ui.BG)
|
||||||
|
Loading…
Reference in New Issue
Block a user