Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys, arrow, signal, os, re, gi as gir, cairo
- gir.require_version("GLib", "2.0")
- gir.require_version("Wnck", "3.0")
- gir.require_version("Pango", "1.0")
- gir.require_version("Gdk", "3.0")
- gir.require_version("Gtk", "3.0")
- from gi.repository import GLib, Wnck, Pango, Gdk, Gtk
- color = dict(fg = "#B57614", bg = "#FABD2F", hc = "#076678", bc = "#B57614")
- #color = dict(fg = "#FFFFFF", bg = "#171717CC", hc = "#FFFFFF", bc = "#171717CC")
- def scan_alpha(alpha, rgba):
- if rgba[0] == '#' and len(rgba) == 9:
- return float(int(rgba[7:9], 16)) / 255.0
- else:
- return alpha
- class MenuItem(Gtk.ListBoxRow):
- def __init__(self, wind):
- super(Gtk.ListBoxRow, self).__init__()
- self.wind = wind
- self.wind.connect("name-changed", self.name_changed)
- self.wind.connect("icon-changed", self.icon_changed)
- self.cont = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, 3)
- self.icon = Gtk.Image.new_from_pixbuf(wind.get_icon())
- self.text = Gtk.Label.new(wind.get_name())
- self.text.set_markup("<b>%s</b>" % wind.get_name())
- self.text.set_width_chars(100)
- self.text.set_ellipsize(Pango.EllipsizeMode.START)
- self.cont.add(self.icon)
- self.cont.add(self.text)
- self.add(self.cont)
- def raise_window(self):
- self.wind.activate(Gtk.get_current_event_time())
- def name_changed(self, w):
- self.text.set_markup("<b>%s</b>" % w.get_name())
- def icon_changed(self, w):
- self.icon.set_text(w.get_icon())
- class Menu(Gtk.ListBox):
- def __init__(self):
- super(Gtk.ListBox, self).__init__()
- self.pattern = "\\s*{0}\\s*".format(os.path.basename(sys.argv[0]))
- self.screen = Wnck.Screen.get_default()
- self.screen.force_update()
- self.connect("destroy", self.destroy)
- self.screen.connect("window-opened", self.new_window)
- self.screen.connect("window-closed", self.del_window)
- self.screen.connect("active-window-changed", self.sel_window)
- for w in self.screen.get_windows():
- if not re.match(self.pattern, w.get_name()):
- label = MenuItem(w)
- self.add(label)
- self.set_activate_on_single_click(True)
- self.set_selection_mode(Gtk.SelectionMode.BROWSE)
- self.connect("row-activated", self.activate_row)
- def sel_window(self, s, w):
- w = s.get_active_window()
- for i in self.get_children():
- if w == i.wind:
- self.select_row(i)
- return None
- def activate_row(self, w, r):
- r.raise_window()
- w.unselect_all()
- def show_all(self):
- height = 0
- width = 0
- for i in self.get_children():
- a = i.get_allocation()
- height += a.height
- width = a.width if a.width > width else width
- self.get_toplevel().resize(width, height)
- super().show_all()
- def new_window(self, s, w):
- if not re.match(self.pattern, w.get_name()):
- label = MenuItem(w)
- self.add(label)
- self.show_all()
- def del_window(self, s, w):
- for i in self.get_children():
- if i.wind == w:
- self.remove(i)
- self.show_all()
- return None
- def destroy(self, w):
- self.screen = None
- Wnck.shutdown()
- class MenuShell(Gtk.EventBox):
- def __init__(self):
- super(Gtk.EventBox, self).__init__()
- Wnck.set_default_icon_size(16)
- self.add_events(Gdk.EventMask.BUTTON_PRESS_MASK)
- self.wind = Gtk.Window.new(Gtk.WindowType.POPUP)
- self.wind.add_events(Gdk.EventMask.FOCUS_CHANGE_MASK)
- self.wind.set_type_hint(Gdk.WindowTypeHint.MENU)
- self.menu = Menu()
- self.wind.add(self.menu)
- self.wind.show_all()
- self.wind.hide()
- self.wind.connect("focus-out-event", self.unfocus)
- self.text = Gtk.Label.new()
- self.text.set_markup("<span color='{0}' weight='bold'>\u21b4</span>".format(color["fg"][0:7]))
- self.text.set_property("margin-left", 5)
- self.text.set_property("margin-right", 8)
- self.add(self.text)
- self.connect("button-press-event", self.click)
- self.show_all()
- def click(self, w, e):
- if e.button == 1:
- if w.wind.is_visible():
- self.wind.hide()
- else:
- x = self.get_screen().get_width() - self.wind.get_window().get_width()
- self.wind.move(x, 18)
- self.wind.show()
- def unfocus(self, w, e):
- print("focus")
- self.wind.hide()
- return True
- class DT(object):
- def __init__(self):
- self.time = arrow.now()
- def __format__(self, fmt):
- return self.time.format(fmt)
- class Clock(Gtk.Label):
- def __init__(self):
- super(Gtk.Label, self).__init__()
- GLib.timeout_add(1000, self.spin)
- def spin(self):
- self.set_markup("<b><span color='{0}'>{2:ddd, MMM DD YYYY}</span><span color='{1}'> — {2:HH:mm:ss A ZZZ (ZZ})</span></b>".format(color["hc"][0:7], color["fg"][0:7], DT()))
- return True
- class BarBG(Gtk.DrawingArea):
- def __init__(self):
- super(Gtk.DrawingArea, self).__init__()
- self.color1 = Gdk.RGBA()
- self.color2 = Gdk.RGBA()
- self.color1.parse(color["bg"][0:7])
- self.color1.alpha = scan_alpha(self.color1.alpha, color["bg"])
- self.color2.parse(color["bc"][0:7])
- self.color2.alpha = scan_alpha(self.color2.alpha, color["bc"])
- self.set_hexpand(True)
- self.set_vexpand(True)
- self.connect("draw", self.render_bg)
- def render_bg(self, w, context):
- area = self.get_allocation()
- Gdk.cairo_set_source_rgba(context, self.color1)
- context.rectangle(area.x, area.y, area.width, area.height)
- context.fill()
- Gdk.cairo_set_source_rgba(context, self.color2)
- context.rectangle(area.x, area.y, area.width, area.height)
- context.stroke()
- return True
- class Main(Gtk.Window):
- def __init__(self):
- super(Gtk.Window, self).__init__()
- signal.signal(signal.SIGINT, lambda s, f: Gtk.main_quit())
- self.cont = Gtk.Overlay.new()
- self.area = BarBG()
- self.cont.add(self.area)
- self.time = Clock()
- self.time.set_halign(Gtk.Align.CENTER)
- self.cont.add_overlay(self.time)
- self.menu = MenuShell()
- self.menu.set_halign(Gtk.Align.END)
- self.cont.add_overlay(self.menu)
- self.add(self.cont)
- self.set_type_hint(Gdk.WindowTypeHint.DESKTOP)
- self.move(0, 0)
- self.set_default_size(self.screen_width(), 18)
- self.connect("destroy", self.on_destroy)
- self.if_has_compositor()
- def screen_width(self):
- return self.get_screen().get_width()
- def screen_composited(self):
- return self.get_screen().is_composited()
- def if_has_compositor(self):
- if self.screen_composited():
- self.set_app_paintable(True)
- self.set_visual(self.get_screen().get_rgba_visual())
- self.connect("draw", self.__draw)
- def __draw(self, w, context):
- ax, ay, aw, ah = context.clip_extents()
- context.set_source_rgba(0.0, 0.0, 0.0, 0.0)
- context.set_operator(cairo.OPERATOR_SOURCE)
- context.rectangle(ax, ay, aw, ah)
- context.fill()
- def on_destroy(self, w):
- print("\r\n")
- Gtk.main_quit()
- main = Main()
- main.show_all()
- Gtk.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement