Refactoring, magic method support, menu generator support
In addition to Entry sub-menues, menu generators are now supported.
This commit is contained in:
parent
eb7692ac9d
commit
6212ebed5b
1 changed files with 40 additions and 11 deletions
|
@ -1,5 +1,8 @@
|
||||||
|
FUNCTION = type(lambda: 1)
|
||||||
|
|
||||||
class Input:
|
class Input:
|
||||||
def __init__(self, caption, initial_text="", on_done=None, on_change=None, on_cancel=None):
|
def __init__(self, caption, initial_text="", on_done=None, on_change=None,
|
||||||
|
on_cancel=None):
|
||||||
self.caption = caption
|
self.caption = caption
|
||||||
self.initial_text = initial_text
|
self.initial_text = initial_text
|
||||||
self.on_done = on_done
|
self.on_done = on_done
|
||||||
|
@ -13,7 +16,8 @@ class Input:
|
||||||
|
|
||||||
|
|
||||||
class Entry:
|
class Entry:
|
||||||
def __init__(self, name, description="", action=None, kwargs={}, sub_menu=None, input=None):
|
def __init__(self, name, description="", action=None, kwargs={},
|
||||||
|
sub_menu=None, input=None):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.description = description
|
self.description = description
|
||||||
self.action = action
|
self.action = action
|
||||||
|
@ -61,9 +65,12 @@ class Menu:
|
||||||
entry.action(**entry.kwargs)
|
entry.action(**entry.kwargs)
|
||||||
if entry.input != None:
|
if entry.input != None:
|
||||||
entry.input.invoke(self.window)
|
entry.input.invoke(self.window)
|
||||||
if entry.sub_menu != None:
|
if type(entry.sub_menu) is FUNCTION:
|
||||||
|
entry.sub_menu(entry).invoke(self.window, back=self)
|
||||||
|
elif entry.sub_menu != None
|
||||||
entry.sub_menu.invoke(self.window, back=self)
|
entry.sub_menu.invoke(self.window, back=self)
|
||||||
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def menu_entries(self):
|
def menu_entries(self):
|
||||||
entries = []
|
entries = []
|
||||||
|
@ -72,16 +79,38 @@ class Menu:
|
||||||
return entries
|
return entries
|
||||||
|
|
||||||
|
|
||||||
|
def __add__(self, other):
|
||||||
|
try:
|
||||||
|
self.add(other)
|
||||||
|
except TypeError:
|
||||||
|
return NotImplemented
|
||||||
|
return self
|
||||||
|
|
||||||
|
|
||||||
|
def __sub__(self, other):
|
||||||
|
try:
|
||||||
|
self.remove(other)
|
||||||
|
except TypeError:
|
||||||
|
return NotImplemented
|
||||||
|
return self
|
||||||
|
|
||||||
|
|
||||||
def add(self, entry):
|
def add(self, entry):
|
||||||
if len(self.entries) > 0:
|
if entry.__class__ == Entry:
|
||||||
entry_id = tuple(self.entries.keys())[-1] + 1
|
if len(self.entries) > 0:
|
||||||
|
entry_id = tuple(self.entries.keys())[-1] + 1
|
||||||
|
else:
|
||||||
|
entry_id = 0
|
||||||
|
self.entries[entry_id] = entry
|
||||||
else:
|
else:
|
||||||
entry_id = 0
|
raise TypeError("invalid type supplied")
|
||||||
self.entries[entry_id] = entry
|
|
||||||
|
|
||||||
|
|
||||||
def remove(self, entry):
|
def remove(self, entry):
|
||||||
if entry in self.entries.values():
|
if entry.__class__ == Entry:
|
||||||
for entry_id in self.entries:
|
if entry in self.entries.values():
|
||||||
if self.entries[entry_id] == entry:
|
for entry_id in self.entries:
|
||||||
del self.entries[entry_id]
|
if self.entries[entry_id] == entry:
|
||||||
|
del self.entries[entry_id]
|
||||||
|
else:
|
||||||
|
raise TypeError("invalid type supplied")
|
||||||
|
|
Reference in a new issue