From ec6a086a0498b898c24262ee769356775dea176a Mon Sep 17 00:00:00 2001 From: peterhinch Date: Mon, 19 Dec 2022 13:49:03 +0000 Subject: [PATCH] Document label alignment. Simplify logic. --- README.md | 12 +++++++++++- gui/widgets/label.py | 21 +++++++++------------ 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index cfbed86..96351d4 100644 --- a/README.md +++ b/README.md @@ -496,6 +496,9 @@ Constructor args: 7. `bgcolor=None` 8. `bdcolor=False` If `False` no border is displayed. If `None` a border is shown in the `Writer` foreground color. If a color is passed, it is used. + 9. `align=ALIGN_LEFT` By default text in labels is left aligned. Options are + `ALIGN_RIGHT` and `ALIGN_CENTER`. These options can only take effect if a + large enough field width is passed to `text`. The constructor displays the string at the required location. @@ -506,10 +509,17 @@ Methods: * `fgcolor=None` Foreground color: if `None` the `Writer` default is used. * `bgcolor=None` Background color, as per foreground. * `bdcolor=None` Border color. As per above except that if `False` is - passed, no border is displayed. This clears a previously drawn border. + passed, no border is displayed. This clears a previously drawn border. + * `align=None` Use alignment specified in constructor unless one of the + module constants is passed. Returns the current text string. 2. `show` No args. (Re)draws the label. Primarily for internal use by GUI. +Module Constants: + * `ALIGN_LEFT=0` + * `ALIGN_RIGHT=1` + * `ALIGN_CENTER=2` + If populating a label would cause it to extend beyond the screen boundary a warning is printed at the console. The label may appear at an unexpected place. The following is a complete "Hello world" script. diff --git a/gui/widgets/label.py b/gui/widgets/label.py index 0664007..7c8164e 100644 --- a/gui/widgets/label.py +++ b/gui/widgets/label.py @@ -1,7 +1,7 @@ # label.py Label class for nano-gui # Released under the MIT License (MIT). See LICENSE. -# Copyright (c) 2018-2020 Peter Hinch +# Copyright (c) 2018-2022 Peter Hinch from micropython import const from gui.core.nanogui import DObject @@ -26,7 +26,7 @@ class Label(DObject): if text is not None: self.value(text, invert) - def value(self, text=None, invert=False, fgcolor=None, bgcolor=None, bdcolor=None): + def value(self, text=None, invert=False, fgcolor=None, bgcolor=None, bdcolor=None, align=None): txt = super().value(text) # Redraw even if no text supplied: colors may have changed. self.invert = invert @@ -35,6 +35,8 @@ class Label(DObject): if bdcolor is False: self.def_bdcolor = False self.bdcolor = self.def_bdcolor if bdcolor is None else bdcolor + if align is not None: + self.align = align self.show() return txt @@ -45,17 +47,12 @@ class Label(DObject): super().show() # Draw or erase border wri = self.writer dev = self.device - if self.align == ALIGN_LEFT: - Writer.set_textpos(dev, self.row, self.col) - else: + rcol = 0 # Relative column of LHS of text + if self.align: txt_width = wri.stringlen(txt) - if self.width <= txt_width: - Writer.set_textpos(dev, self.row, self.col) - else: - if self.align == ALIGN_RIGHT: - Writer.set_textpos(dev, self.row, self.col + self.width - txt_width) - else: - Writer.set_textpos(dev, self.row, self.col + self.width // 2 - txt_width // 2) + if self.width > txt_width: + rcol = self.width - txt_width if self.align == ALIGN_RIGHT else self.width // 2 - txt_width // 2 + Writer.set_textpos(dev, self.row, self.col + rcol) wri.setcolor(self.fgcolor, self.bgcolor) wri.printstring(txt, self.invert) wri.setcolor() # Restore defaults