Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NoneType error when switching between widgets #69

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 13 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,8 @@ def change_focus(self, widget):
widget.set_cursor()

def move_focus(self, direction):
if self.focus_idx is None:
self.focus_idx = 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 +140,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 +160,6 @@ def redraw(self):


class WFrame(Widget):

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


class WButton(FocusableWidget):

def __init__(self, w, text):
Widget.__init__(self)
self.t = text
Expand Down Expand Up @@ -217,7 +217,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 +250,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 +280,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 +307,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 +339,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 +373,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 +392,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 +406,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 +461,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 +507,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 +536,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 +550,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