-
Notifications
You must be signed in to change notification settings - Fork 259
/
_label.py
94 lines (75 loc) · 2.64 KB
/
_label.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
""" Label
.. UIExample:: 50
from flexx import app, event, ui
class Example(ui.Widget):
def init(self):
with ui.HBox():
self.but = ui.Button(text='Push me')
self.label = ui.Label(flex=1, wrap=True, text='This is a label. ')
@event.reaction('but.pointer_down')
def _add_label_text(self, *events):
self.label.set_text(self.label.text + 'Yes it is. ')
"""
from ... import event
from . import Widget
class Label(Widget):
""" Widget to show text/html.
The ``node`` of this widget is a
`<div> <https://developer.mozilla.org/docs/Web/HTML/Element/div>`_ with
CSS ``word-wrap`` and ``white-space`` set appropriately.
"""
DEFAULT_MIN_SIZE = 10, 24
CSS = """
.flx-Label {
border: 0px solid #454;
user-select: text;
-moz-user-select: text;
-webkit-user-select: text;
-ms-user-select: text;
}"""
text = event.StringProp('', doc="""
The text shown in the label (HTML is shown verbatim).
""")
html = event.StringProp('', doc="""
The html shown in the label.
Warning: there is a risk of introducing openings for XSS attacks
when html is introduced that you do not control (e.g. from user input).
""")
wrap = event.IntProp(0, settable=True, doc="""
Whether the content is allowed to be wrapped on multiple
lines. Set to 0/False for no wrap (default), 1/True for word-wrap,
2 for character wrap.
""")
def init(self):
if self.text:
self.set_text(self.text)
elif self.html:
self.set_html(self.html)
@event.action
def set_text(self, text):
""" Setter for the text property.
"""
if not self.node:
self._mutate_text(text)
return
self.node.textContent = text
self._mutate_text(self.node.textContent)
self._mutate_html(self.node.innerHTML)
@event.action
def set_html(self, html):
""" Setter for the html property. Use with care.
"""
if not self.node:
self._mutate_html(html)
return
self.node.innerHTML = html
self._mutate_text(self.node.textContent)
self._mutate_html(self.node.innerHTML)
@event.reaction('wrap')
def _wrap_changed(self, *events):
wrap = self.wrap
if wrap < 0 or wrap > 2:
wrap = 0
self.node.style['word-wrap'] = ['normal', 'normal', 'break-word'][wrap]
self.node.style['white-space'] = ['nowrap', 'normal', 'normal'][wrap]
self.check_real_size()