allow form fields to be enabled or disabled

This commit is contained in:
Michael Hudson-Doyle 2017-02-13 14:59:25 +13:00
parent 00c807dc9e
commit 1bd00a843a
2 changed files with 33 additions and 6 deletions

View File

@ -164,7 +164,7 @@ class AddPartitionView(BaseView):
fstype = self.form.fstype.value fstype = self.form.fstype.value
if fstype.is_mounted: if fstype.is_mounted:
mount = self.form.mountpoint.value mount = self.form.mount.value
else: else:
mount = None mount = None

View File

@ -82,6 +82,8 @@ class BoundFormField(object):
self.in_error = False self.in_error = False
self._help = None self._help = None
self._caption = None self._caption = None
self.pile = None
self.enabled = True
self.widget = self._make_widget() self.widget = self._make_widget()
def _make_widget(self): def _make_widget(self):
@ -96,6 +98,8 @@ class BoundFormField(object):
return value return value
def _validate(self): def _validate(self):
if not self.enabled:
return
try: try:
v = self.value v = self.value
except ValueError as e: except ValueError as e:
@ -160,23 +164,46 @@ class BoundFormField(object):
def caption(self, val): def caption(self, val):
self._caption = val self._caption = val
def as_row(self, include_help): def cols(self):
text = Text(self.caption, align="right") text = Text(self.caption, align="right")
input = _Validator(self, self.widget) if self.enabled:
input = Color.string_input(_Validator(self, self.widget))
else:
input = self.widget
cols = [ cols = [
("weight", 0.2, text), ("weight", 0.2, text),
("weight", 0.3, Color.string_input(input)), ("weight", 0.3, input),
] ]
if include_help: if self.include_help:
if self.help is not None: if self.help is not None:
help = self.help help = self.help
else: else:
help = "" help = ""
cols.append( cols.append(
("weight", 0.5, Text(help))) ("weight", 0.5, Text(help)))
self.pile = Pile([Columns(cols, dividechars=4)]) cols = Columns(cols, dividechars=4)
if self.enabled:
return cols
else:
return WidgetDisable(Color.info_minor(cols))
def as_row(self, include_help):
if self.pile is not None:
raise RuntimeError("do not call as_row more than once!")
self.include_help = include_help
self.pile = Pile([self.cols()])
return self.pile return self.pile
def enable(self):
self.enabled = True
self.pile.contents[0] = (self.cols(), self.pile.contents[0][1])
self.validate()
def disable(self):
self.enabled = False
self.pile.contents[0] = (self.cols(), self.pile.contents[0][1])
self.validate()
class BoundStringField(BoundFormField): class BoundStringField(BoundFormField):
def _make_widget(self): def _make_widget(self):