MainWindow: improve the headerbar widget

This commit is contained in:
Bilal Elmoussaoui 2019-02-14 13:01:44 +01:00
parent 0b68e4cc5a
commit 97b5fd7de3
2 changed files with 124 additions and 63 deletions

View file

@ -2,21 +2,6 @@
<!-- Generated with glade 3.22.0 -->
<interface>
<requires lib="gtk+" version="3.22"/>
<object class="GtkImage" id="add_image">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="icon_name">list-add-symbolic</property>
</object>
<object class="GtkImage" id="primary_menu_image">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="icon_name">open-menu-symbolic</property>
</object>
<object class="GtkImage" id="search_image">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="icon_name">system-search-symbolic</property>
</object>
<template class="Window" parent="GtkApplicationWindow">
<property name="width_request">350</property>
<property name="height_request">500</property>
@ -28,6 +13,7 @@
<property name="width_request">350</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="transition_type">slide-left-right</property>
<child>
<object class="GtkBox" id="empty_accounts_box">
<property name="visible">True</property>
@ -249,19 +235,95 @@
</object>
</child>
<child type="titlebar">
<object class="GtkHeaderBar" id="headerbar">
<object class="GtkHeaderBar">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="title" translatable="yes">Authenticator</property>
<property name="show_close_button">True</property>
<child>
<object class="GtkButton" id="add_btn">
<child type="title">
<object class="GtkStack" id="headerbar_stack">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="action_name">win.add-account</property>
<property name="image">add_image</property>
<signal name="clicked" handler="add_btn_clicked" swapped="no"/>
<property name="can_focus">False</property>
<property name="hexpand">True</property>
<property name="transition_type">slide-left-right</property>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="hexpand">True</property>
<child>
<object class="GtkButton" id="add_btn">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="action_name">win.add-account</property>
<property name="image">add_image</property>
<signal name="clicked" handler="add_btn_clicked" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkToggleButton" id="search_btn">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="image">search_image</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="pack_type">end</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="name">main_headerbar</property>
</packing>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="hexpand">True</property>
<child>
<object class="GtkButton" id="add_btn1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="action_name">win.add-account</property>
<property name="image">add_image1</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
</object>
<packing>
<property name="name">empty_headerbar</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">center</property>
<property name="hexpand">True</property>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="name">locked_headerbar</property>
<property name="position">2</property>
</packing>
</child>
</object>
</child>
<child>
@ -274,22 +336,29 @@
</object>
<packing>
<property name="pack_type">end</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkToggleButton" id="search_btn">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="image">search_image</property>
</object>
<packing>
<property name="pack_type">end</property>
<property name="position">2</property>
</packing>
</child>
</object>
</child>
</template>
<object class="GtkImage" id="add_image">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="icon_name">list-add-symbolic</property>
</object>
<object class="GtkImage" id="add_image1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="icon_name">list-add-symbolic</property>
</object>
<object class="GtkImage" id="primary_menu_image">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="icon_name">open-menu-symbolic</property>
</object>
<object class="GtkImage" id="search_image">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="icon_name">system-search-symbolic</property>
</object>
</interface>

View file

@ -42,13 +42,11 @@ class Window(Gtk.ApplicationWindow, GObject.GObject):
view = GObject.Property(type=int, default=0)
headerbar = Gtk.Template.Child()
add_btn = Gtk.Template.Child()
search_btn = Gtk.Template.Child()
primary_menu_btn = Gtk.Template.Child()
main_stack = Gtk.Template.Child()
headerbar_stack = Gtk.Template.Child()
search_bar = Gtk.Template.Child()
@ -57,7 +55,6 @@ class Window(Gtk.ApplicationWindow, GObject.GObject):
accounts_viewport = Gtk.Template.Child()
unlock_btn = Gtk.Template.Child()
password_entry = Gtk.Template.Child()
def __init__(self):
@ -160,18 +157,6 @@ class Window(Gtk.ApplicationWindow, GObject.GObject):
else:
self.props.view = WindowView.NORMAL
@Gtk.Template.Callback('unlock_btn_clicked')
def __unlock_btn_clicked(self, *_):
from Authenticator.models import Keyring
typed_password = self.password_entry.get_text()
if typed_password == Keyring.get_password():
self.get_application().set_property("is-locked", False)
# Reset password entry
self.password_entry.get_style_context().remove_class("error")
self.password_entry.set_text("")
else:
self.password_entry.get_style_context().add_class("error")
def __add_action(self, key, callback, prop_bind=None, bind_flag=GObject.BindingFlags.INVERT_BOOLEAN):
action = Gio.SimpleAction.new(key, None)
action.connect("activate", callback)
@ -182,27 +167,34 @@ class Window(Gtk.ApplicationWindow, GObject.GObject):
def __state_changed(self, *_):
if self.props.view == WindowView.LOCKED:
visible_child = "locked_state"
self.add_btn.set_visible(False)
self.add_btn.set_no_show_all(True)
self.search_btn.set_visible(False)
self.search_btn.set_no_show_all(True)
visible_headerbar = "locked_headerbar"
if self.key_press_signal:
self.disconnect(self.key_press_signal)
else:
if self.props.view == WindowView.EMPTY:
visible_child = "empty_state"
self.search_btn.set_visible(False)
self.search_btn.set_no_show_all(True)
visible_headerbar = "empty_headerbar"
else:
visible_child = "normal_state"
self.search_btn.set_visible(True)
self.search_btn.set_no_show_all(False)
visible_headerbar = "main_headerbar"
# Connect on type search bar
self.key_press_signal = self.connect("key-press-event", lambda x,
y: self.search_bar.handle_event(y))
self.add_btn.set_visible(True)
self.add_btn.set_no_show_all(False)
self.main_stack.set_visible_child_name(visible_child)
self.headerbar_stack.set_visible_child_name(visible_headerbar)
@Gtk.Template.Callback('unlock_btn_clicked')
def __unlock_btn_clicked(self, *_):
from Authenticator.models import Keyring
typed_password = self.password_entry.get_text()
if typed_password == Keyring.get_password():
self.get_application().set_property("is-locked", False)
# Reset password entry
self.password_entry.get_style_context().remove_class("error")
self.password_entry.set_text("")
else:
self.password_entry.get_style_context().add_class("error")
@Gtk.Template.Callback('search_changed')
def __search_changed(self, entry):