Skip to content

Commit

Permalink
Fix NoneType error when switching between widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
Liam Masters committed Apr 29, 2022
1 parent 64a58be commit 12604ed
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions picotui/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"WAutoComplete",
)


class Dialog(Widget):

finish_on_esc = True
Expand Down Expand Up @@ -108,6 +109,10 @@ def change_focus(self, widget):
widget.set_cursor()

def move_focus(self, direction):
if self.focus_idx is None:
self.focus_idx = 0
if direction is None:
direction = 0
prev_idx = (self.focus_idx + direction) % len(self.childs)
self.focus_idx, new_w = self.find_focusable_by_idx(prev_idx, direction)
self.change_focus(new_w)
Expand Down Expand Up @@ -137,14 +142,13 @@ def handle_mouse(self, x, y):
# Work in absolute coordinates
if self.inside(x, y):
self.focus_idx, w = self.find_focusable_by_xy(x, y)
# print(w)
# print(w)
if w:
self.change_focus(w)
return w.handle_mouse(x, y)


class WLabel(Widget):

def __init__(self, text, w=0):
self.t = text
self.h = 1
Expand All @@ -158,7 +162,6 @@ def redraw(self):


class WFrame(Widget):

def __init__(self, w, h, title=""):
self.w = w
self.h = h
Expand All @@ -173,7 +176,6 @@ def redraw(self):


class WButton(FocusableWidget):

def __init__(self, w, text):
Widget.__init__(self)
self.t = text
Expand Down Expand Up @@ -217,7 +219,6 @@ def on_click(self):


class WCheckbox(ChoiceWidget):

def __init__(self, title, choice=False):
super().__init__(choice)
self.t = title
Expand Down Expand Up @@ -251,7 +252,6 @@ def handle_key(self, key):


class WRadioButton(ItemSelWidget):

def __init__(self, items):
super().__init__(items)
self.h = len(items)
Expand Down Expand Up @@ -282,7 +282,6 @@ def handle_key(self, key):


class WListBox(EditorExt, ChoiceWidget):

def __init__(self, w, h, items):
EditorExt.__init__(self)
ChoiceWidget.__init__(self, 0)
Expand Down Expand Up @@ -310,7 +309,7 @@ def show_line(self, l, i):
else:
self.attr_color(C_BLACK, C_GREEN)
if i != -1:
l = self.render_line(l)[:self.width]
l = self.render_line(l)[: self.width]
self.wr(l)
self.clear_num_pos(self.width - len(l))
if hlite:
Expand Down Expand Up @@ -342,9 +341,7 @@ def cursor(self, state):


class WPopupList(Dialog):

class OneShotList(WListBox):

def handle_key(self, key):
if key == KEY_ENTER:
return ACTION_OK
Expand Down Expand Up @@ -378,7 +375,6 @@ def get_selected_value(self):


class WDropDown(ChoiceWidget):

def __init__(self, w, items, *, dropdown_h=5):
super().__init__(0)
self.items = items
Expand All @@ -398,7 +394,9 @@ def redraw(self):
self.wr(DOWN_ARROW)

def handle_mouse(self, x, y):
popup = WPopupList(self.x, self.y + 1, self.w, self.dropdown_h, self.items, self.choice)
popup = WPopupList(
self.x, self.y + 1, self.w, self.dropdown_h, self.items, self.choice
)
res = popup.loop()
if res == ACTION_OK:
self.choice = popup.get_choice()
Expand All @@ -410,7 +408,6 @@ def handle_key(self, key):


class WTextEntry(EditorExt, EditableWidget):

def __init__(self, w, text):
EditorExt.__init__(self, width=w, height=1)
self.t = text
Expand Down Expand Up @@ -466,13 +463,11 @@ def show_line(self, l, i):


class WPasswdEntry(WTextEntry):

def show_line(self, l, i):
super().show_line("*" * len(l), i)


class WMultiEntry(EditorExt, EditableWidget):

def __init__(self, w, h, lines):
EditorExt.__init__(self, width=w, height=h)
self.h = h
Expand Down Expand Up @@ -514,7 +509,9 @@ def get_choices(self, substr):

def show_popup(self):
choices = self.get_choices(self.get())
popup = self.popup_class(self.x, self.y + 1, self.longest(choices) + 2, self.popup_h, choices)
popup = self.popup_class(
self.x, self.y + 1, self.longest(choices) + 2, self.popup_h, choices
)
popup.main_widget = self
res = popup.loop()
if res == ACTION_OK:
Expand All @@ -541,12 +538,12 @@ def handle_mouse(self, x, y):


class WCompletionList(WPopupList):

def __init__(self, x, y, w, h, items):
Dialog.__init__(self, x, y, w, h)
self.list = self.OneShotList(w - 2, h - 2, items)
self.add(1, 1, self.list)
chk = WCheckbox("Prefix")

def is_prefix_changed(wid):
main = self.main_widget
choices = main.get_choices(main.get(), wid.choice)
Expand All @@ -555,6 +552,7 @@ def is_prefix_changed(wid):
self.list.cur_line = 0
self.list.row = 0
self.list.redraw()

chk.on("changed", is_prefix_changed)
self.add(1, h - 1, chk)

Expand Down

0 comments on commit 12604ed

Please sign in to comment.