Skip to content

Instantly share code, notes, and snippets.

@zalito12
Last active January 16, 2020 17:43
Show Gist options
  • Save zalito12/3ad7fdfd3feddebed8df to your computer and use it in GitHub Desktop.
Save zalito12/3ad7fdfd3feddebed8df to your computer and use it in GitHub Desktop.
Gtk Searchbar with long text field and combo box
# -*- coding: utf-8 -*-
from gi.repository import Gtk, Gdk
class Searchbar(Gtk.Revealer):
__gtype_name__ = 'Searchbar'
#Combo Ids from ui file
COMBO_ALL = 0
COMBO_TITLE = 1
COMBO_AUTHOR = 2
def __init__(self, DOMAIN, window=None, search_button=None):
Gtk.Revealer.__init__(self)
builder = Gtk.Builder()
builder.add_from_file("searchBar.ui")
self._searchContainer = builder.get_object("searchBar")
self.search_entry = builder.get_object("searchEntry")
self.searchCombo = builder.get_object("searchCombo")
self.add(self._searchContainer)
self.show = False
self.set_window(window, search_button)
def set_window(self, window, search_button):
self.window = window
self.search_button = search_button
self.search_button.connect("toggled", self.open_search)
self.window.connect("key-press-event", self._on_key_press)
self.window.connect_after("key-press-event", self._after_key_press)
def open_search(self, widget):
self.show_bar(widget.get_active())
def show_bar(self, show):
self.show = show
if not self.show:
self.search_entry.set_text('')
if show:
self.search_entry.grab_focus()
self.set_reveal_child(show)
def toggle_bar(self):
self.show_bar(not self.get_child_revealed())
def _on_key_press(self, widget, event):
keyname = Gdk.keyval_name(event.keyval)
if keyname == 'Escape' and self.search_button.get_active():
if self.search_entry.is_focus():
self.search_button.set_active(False)
else:
self.search_entry.grab_focus()
return True
if event.state & Gdk.ModifierType.CONTROL_MASK:
if keyname == 'f':
self.search_button.set_active(True)
return True
return False
def _after_key_press(self, widget, event):
if (not self.search_button.get_active() or
not self.search_entry.is_focus()):
if self.search_entry.im_context_filter_keypress(event):
self.search_button.set_active(True)
self.search_entry.grab_focus()
# Text in entry is selected, deselect it
l = self.search_entry.get_text_length()
self.search_entry.select_region(l, l)
return True
return False
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk+" version="3.12"/>
<object class="GtkToolbar" id="searchBar">
<property name="visible">True</property>
<property name="can_focus">False</property>
<style>
<class name="search-bar"/>
</style>
<child>
<object class="GtkToolItem" id="tool_item">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkBox" id="tool_box">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">horizontal</property>
<property name="halign">fill</property>
<property name="hexpand">True</property>
<child>
<object class="GtkSearchEntry" id="searchEntry">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="hexpand">True</property>
<property name="caps_lock_warning">False</property>
<property name="margin_right">6</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkComboBoxText" id="searchCombo">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="active">0</property>
<items>
<item id="comboAll" translatable="yes">All</item>
<item id="comboTitle" translatable="yes">Title</item>
<item id="comboAuthor" translatable="yes">Author</item>
</items>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
<packing>
<property name="expand">True</property>
</packing>
</child>
</object>
</interface>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment