Commit dd592ad2 authored by Philipp Hörist's avatar Philipp Hörist
Browse files

Assistant: Add convenience mehtods to Page

- Page has now a update_page_complete() method
- Page has now a get_visible_buttons() method
- Page has now a get_get_default_button() method
parent a5fc50ca
......@@ -686,7 +686,7 @@ def _set_complete(self, *args):
self.complete = True
self._ui.visit_server_button.set_visible(True)
self.get_toplevel().update_page_complete()
self.update_page_complete()
def is_anonymous(self):
return self._ui.sign_up_anonymously.get_active()
......@@ -805,7 +805,7 @@ def _set_complete(self, *args):
port_valid = self._validate_port()
host_valid = self._validate_host()
self.complete = port_valid and host_valid
self.get_toplevel().update_page_complete()
self.update_page_complete()
class SecurityWarning(Page):
......@@ -884,7 +884,7 @@ def has_form(self):
def _on_is_valid(self, _widget, is_valid):
self.complete = is_valid
self.get_toplevel().update_page_complete()
self.update_page_complete()
def add_form(self, form):
self.remove_form()
......
......@@ -66,9 +66,6 @@ def __init__(self,
def show_all(self):
page_name = self._ui.stack.get_visible_child_name()
buttons = self._button_visible_func(self, page_name)
self._set_buttons_visible(buttons)
self.update_page_complete()
self.emit('page-changed', page_name)
Gtk.ApplicationWindow.show_all(self)
......@@ -76,7 +73,7 @@ def _on_key_press_event(self, _widget, event):
if event.keyval == Gdk.KEY_Escape:
self.destroy()
def update_page_complete(self):
def _update_page_complete(self, *args):
page_widget = self._ui.stack.get_visible_child()
for button, complete in self._buttons.values():
if complete:
......@@ -85,6 +82,33 @@ def update_page_complete(self):
def update_title(self):
self.set_title(self._ui.stack.get_visible_child().title)
def _hide_buttons(self):
for button, _ in self._buttons.values():
button.hide()
def _set_buttons_visible(self):
page_name = self._ui.stack.get_visible_child_name()
if self._button_visible_func is None:
buttons = self.get_page(page_name).get_visible_buttons()
if buttons is not None:
if len(buttons) == 1:
default = buttons[0]
else:
default = self.get_page(page_name).get_default_button()
self.set_default_button(default)
else:
buttons = self._button_visible_func(self, page_name)
self._update_page_complete()
if buttons is None:
return
for button_name in buttons:
button, _ = self._buttons[button_name]
button.show()
def set_button_visible_func(self, func):
self._button_visible_func = func
......@@ -105,6 +129,7 @@ def add_button(self, name, label, css_class=None, complete=False):
def add_pages(self, pages):
self._pages = pages
for name, widget in pages.items():
widget.connect('update-page-complete', self._update_page_complete)
self._ui.stack.add_named(widget, name)
def add_default_page(self, name):
......@@ -125,27 +150,20 @@ def get_current_page(self):
return self._ui.stack.get_visible_child_name()
def show_page(self, name, transition=Gtk.StackTransitionType.NONE):
buttons = self._button_visible_func(self, name)
self._set_buttons_visible(buttons)
if self._ui.stack.get_visible_child_name() == name:
return
self._hide_buttons()
self._ui.stack.set_visible_child_full(name, transition)
def get_page(self, name):
return self._pages[name]
def _set_buttons_visible(self, buttons):
for button, _ in self._buttons.values():
button.hide()
if buttons is None:
return
for button_name in buttons:
button, _ = self._buttons[button_name]
button.show()
def _on_visible_child_name(self, stack, _param):
if stack.get_visible_child_name() is None:
# Happens for some reason when adding the first page
return
self.update_title()
self.update_page_complete()
self._set_buttons_visible()
self.emit('page-changed', stack.get_visible_child_name())
def __on_button_clicked(self, button):
......@@ -161,6 +179,11 @@ def __on_destroy(self, *args):
class Page(Gtk.Box):
__gsignals__ = {
'update-page-complete': (GObject.SignalFlags.RUN_LAST, None, ()),
}
def __init__(self):
Gtk.Box.__init__(self, orientation=Gtk.Orientation.VERTICAL)
self.set_spacing(18)
......@@ -169,6 +192,15 @@ def __init__(self):
self.title = ''
self.complete = True
def get_visible_buttons(self):
return None
def get_default_button(self):
return None
def update_page_complete(self):
self.emit('update-page-complete')
class DefaultPage(Page):
def __init__(self, icon_name, icon_css_class):
......
......@@ -210,7 +210,7 @@ def _on_changed(self, _entry):
def _set_complete(self, state):
self.complete = state
self.get_toplevel().update_page_complete()
self.update_page_complete()
def get_password(self):
return self._password1_entry.get_text()
......@@ -228,7 +228,7 @@ def __init__(self):
def _on_is_valid(self, _widget, is_valid):
self.complete = is_valid
self.get_toplevel().update_page_complete()
self.update_page_complete()
def set_form(self, form):
if self._current_form is not None:
......
......@@ -128,7 +128,7 @@ def __init__(self):
def _on_changed(self, entry):
self.complete = bool(entry.get_text())
self.get_toplevel().update_page_complete()
self.update_page_complete()
win = TestAssistant()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment