unify "action menu row" creation

This commit is contained in:
Michael Hudson-Doyle 2018-07-06 13:57:55 +12:00
parent 7d15727b74
commit 830d89c735
4 changed files with 43 additions and 56 deletions

View File

@ -24,7 +24,6 @@ import logging
import attr
from urwid import (
AttrMap,
connect_signal,
Text,
)
@ -57,7 +56,7 @@ from subiquitycore.ui.table import (
from subiquitycore.ui.utils import (
button_pile,
Color,
CursorOverride,
make_action_menu_row,
Padding,
screen,
)
@ -110,26 +109,6 @@ class FilesystemConfirmation(Stretchy):
self.parent.remove_overlay()
def add_menu_row_focus_behaviour(menu, row, attr_map, focus_map, cursor_x=2):
"""Configure focus behaviour of row (which contains menu)
The desired behaviour is that:
1) The cursor appears at the left of the row rather than where the
menu is.
2) The row is highlighted when focused and retains that focus even
when the popup is open.
"""
if not isinstance(attr_map, dict):
attr_map = {None: attr_map}
if not isinstance(focus_map, dict):
focus_map = {None: focus_map}
am = AttrMap(CursorOverride(row, cursor_x=cursor_x), attr_map, focus_map)
connect_signal(menu, 'open', lambda menu: am.set_attr_map(focus_map))
connect_signal(menu, 'close', lambda menu: am.set_attr_map(attr_map))
return am
@attr.s
class MountInfo:
mount = attr.ib(default=None)
@ -228,10 +207,9 @@ class MountList(WidgetWrap):
"/".join(mi.split_path[1:]),
]
actions = [(_("Unmount"), mi.mount.can_delete(), 'unmount')]
menu = ActionMenu(
actions, "\N{BLACK RIGHT-POINTING SMALL TRIANGLE}")
menu = ActionMenu(actions)
connect_signal(menu, 'action', self._mount_action, mi.mount)
row = TableRow([
cells = [
Text("["),
Text(path_markup),
Text(mi.size, align='right'),
@ -239,12 +217,15 @@ class MountList(WidgetWrap):
Text(mi.desc),
menu,
Text("]"),
])
row = add_menu_row_focus_behaviour(
]
row = make_action_menu_row(
cells,
menu,
row,
'menu_button',
{None: 'menu_button focus', 'info_minor': 'menu_button focus'})
attr_map='menu_button',
focus_map={
None: 'menu_button focus',
'info_minor': 'menu_button focus',
})
rows.append(row)
self.table.set_contents(rows)
if self.table._w.focus_position >= len(rows):
@ -360,8 +341,7 @@ class DeviceList(WidgetWrap):
enabled=enabled,
value=(action, meth),
opens_dialog=getattr(meth, 'opens_dialog', False)))
menu = ActionMenu(
device_actions, "\N{BLACK RIGHT-POINTING SMALL TRIANGLE}")
menu = ActionMenu(device_actions)
connect_signal(menu, 'action', self._action, device)
return menu
@ -407,16 +387,15 @@ class DeviceList(WidgetWrap):
]]))
for device in devices:
menu = self._action_menu_for_device(device)
row = TableRow([
cells = [
Text("["),
Text(device.label),
Text("{:>9}".format(humanize_size(device.size))),
Text(device.desc()),
menu,
Text("]"),
])
row = add_menu_row_focus_behaviour(
menu, row, 'menu_button', 'menu_button focus')
]
row = make_action_menu_row(cells, menu)
rows.append(row)
if not device.partitions():
@ -434,16 +413,14 @@ class DeviceList(WidgetWrap):
part_size = "{:>9} ({}%)".format(
humanize_size(part.size),
int(100 * part.size / device.size))
row = TableRow([
cells = [
Text("["),
Text(" " + _("partition {}").format(part._number)),
(2, Text(part_size)),
menu,
Text("]"),
])
row = add_menu_row_focus_behaviour(
menu, row, 'menu_button', 'menu_button focus',
cursor_x=4)
]
row = make_action_menu_row(cells, menu, cursor_x=4)
rows.append(row)
if part.flag == "bios_grub":
label = "bios_grub"

View File

@ -120,7 +120,7 @@ class ActionMenu(PopUpLauncher):
signals = ['action', 'open', 'close']
def __init__(self, opts,
icon="\N{BLACK RIGHT-POINTING SMALL TRIANGLE} ]"):
icon="\N{BLACK RIGHT-POINTING SMALL TRIANGLE}"):
self._actions = []
for opt in opts:
if not isinstance(opt, Action):

View File

@ -302,11 +302,11 @@ class ClickableIcon(SelectableIcon):
def make_action_menu_row(
cells, menu,
cells,
menu,
attr_map='menu_button', focus_map='menu_button focus',
cursor_x=2):
cells[0].set_text('[ ' + cells[0].text)
row = TableRow(cells + [menu])
row = TableRow(cells)
if not isinstance(attr_map, dict):
attr_map = {None: attr_map}
if not isinstance(focus_map, dict):

View File

@ -144,7 +144,12 @@ class NetworkView(BaseView):
self.controller = controller
self.items = []
self.error = Text("", align='center')
self.device_table = TablePile(self._build_model_inputs(), spacing=2, colspecs={3:ColSpec(can_shrink=True)})
self.device_table = TablePile(
self._build_model_inputs(),
spacing=2, colspecs={
0: ColSpec(rpad=1),
4: ColSpec(can_shrink=True, rpad=1),
})
self.listbox = ListBox([self.device_table] + [
Padding.line_break(""),
])
@ -187,7 +192,9 @@ class NetworkView(BaseView):
def _build_model_inputs(self):
netdevs = self.model.get_all_netdevs()
rows = []
rows.append(TableRow([Color.info_minor(header) for header in [Text(" NAME"), Text("TYPE"), Text("DHCP"), Text("ADDRESSES")]]))
rows.append(TableRow([
Color.info_minor(Text(header))
for header in ["", "NAME", "TYPE", "DHCP", "ADDRESSES", ""]]))
for dev in netdevs:
dhcp = []
if dev.dhcp4:
@ -226,17 +233,20 @@ class NetworkView(BaseView):
]
menu = ActionMenu(actions)
connect_signal(menu, 'action', self._action, dev)
rows.append(make_action_menu_row(
[
Text(dev.name),
Text(dev.type),
Text(dhcp),
Text(addresses, wrap='clip'),
],
rows.append(make_action_menu_row([
Text("["),
Text(dev.name),
Text(dev.type),
Text(dhcp),
Text(addresses, wrap='clip'),
menu,
))
Text("]"),
], menu))
info = " / ".join([dev.hwaddr, dev.vendor, dev.model])
rows.append(Color.info_minor(TableRow([(4, Text(" " + info))])))
rows.append(Color.info_minor(TableRow([
Text(""),
(4, Text(info)),
Text("")])))
rows.append(Color.info_minor(TableRow([(4, Text(""))])))
return rows