1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
#!/usr/bin/python3
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('XApp', '1.0')
from gi.repository import Gio, GLib, GObject, Gtk, XApp, Pango
import sys
import signal
signal.signal(signal.SIGINT, signal.SIG_DFL)
DBUS_NAME = "org.x.StatusIcon"
DBUS_PATH = "/org/x/StatusIcon"
class ListItem(Gtk.ListBoxRow):
def __init__(self, favinfo, mgr):
super(Gtk.ListBoxRow, self).__init__()
self.favinfo = favinfo
self.mgr = mgr
self.box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
self.add(self.box)
label = Gtk.Label(label=self.favinfo.display_name, xalign=0)
self.box.pack_start(label, False, False, 6)
delete = Gtk.Button.new_from_icon_name("edit-delete-symbolic", Gtk.IconSize.BUTTON)
delete.set_relief(Gtk.ReliefStyle.NONE)
self.box.pack_end(delete, False, False, 6)
delete.connect("clicked", self.on_delete_clicked)
self.show_all()
def on_delete_clicked(self, widget, data=None):
self.mgr.remove(self.favinfo.uri)
class FavoriteList(GObject.Object):
def __init__(self):
super(FavoriteList, self).__init__()
self.window = None
self.manager = XApp.Favorites.get_default()
self.manager.connect("changed", self.on_manager_changed)
self.window = Gtk.Window()
self.window.set_default_size(600, 400)
self.window.connect("destroy", self.on_window_destroy)
self.window_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
self.main_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL,
margin=6,
spacing=0)
bar = Gtk.MenuBar()
menu_item = Gtk.MenuItem.new_with_label("Favorites")
favorites = self.manager.create_menu(None, self.favorite_menu_item_activated)
menu_item.set_submenu(favorites)
bar.append(menu_item)
self.window_box.pack_start(bar, False, False, 0)
self.window_box.pack_start(self.main_box, True, True, 0)
# list stuff
sw_frame = Gtk.Frame()
sw = Gtk.ScrolledWindow(hadjustment=None, vadjustment=None)
sw_frame.add(sw)
self.list_box = Gtk.ListBox(activate_on_single_click=False)
self.list_box.connect("selected-rows-changed", self.on_selection_changed)
self.list_box.connect("row-activated", self.on_row_activated)
sw.add(self.list_box)
self.main_box.pack_start(sw_frame, True, True, 6)
self.window.add(self.window_box)
# controls
control_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
self.add_button = Gtk.Button.new_from_icon_name("list-add-symbolic", Gtk.IconSize.BUTTON)
self.add_button.connect("clicked", self.on_add_clicked)
control_box.pack_start(self.add_button, False, False, 6)
self.main_box.pack_start(control_box, False, False, 6)
self.window.show_all()
self.selected_uri = None
self.loading = False
self.load_favorites()
def favorite_menu_item_activated(self, manager, uri, data=None):
print("activated", uri)
self.manager.launch(uri, Gtk.get_current_event_time())
def load_favorites(self):
self.loading = True # this will preserve the last selection
for child in self.list_box.get_children():
self.list_box.remove(child)
previously_selected = None
favorites = self.manager.get_favorites(None)
for info in favorites:
row = ListItem(info, self.manager)
self.list_box.insert(row, -1)
if self.selected_uri == info.uri:
previously_selected = row
self.loading = False
if previously_selected:
self.list_box.select_row(previously_selected)
def on_row_activated(self, box, row, data=None):
self.manager.launch(row.favinfo.uri, Gtk.get_current_event_time())
def on_selection_changed(self, box, data=None):
if self.loading:
return
row = self.list_box.get_selected_row()
if row:
self.selected_uri = row.favinfo.uri
else:
self.selected_uri = None
def on_add_clicked(self, widget, data=None):
dialog = Gtk.FileChooserDialog(title="Add file to favorites",
parent=self.window,
action=Gtk.FileChooserAction.OPEN)
dialog.add_buttons("Add", Gtk.ResponseType.OK,
"Cancel", Gtk.ResponseType.CANCEL)
# dialog.add_shortcut_folder_uri ("favorites:///")
res = dialog.run()
if res == Gtk.ResponseType.OK:
uri = dialog.get_uri()
self.manager.add(uri)
dialog.destroy()
def on_manager_changed(self, manager, data=None):
self.load_favorites()
def on_window_destroy(self, widget, data=None):
Gtk.main_quit()
if __name__ == '__main__':
test = FavoriteList()
Gtk.main()
|