Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.37 KB | None | 0 0
  1. import sys, arrow, signal, os, re, gi as gir, cairo
  2. gir.require_version("GLib", "2.0")
  3. gir.require_version("Wnck", "3.0")
  4. gir.require_version("Pango", "1.0")
  5. gir.require_version("Gdk", "3.0")
  6. gir.require_version("Gtk", "3.0")
  7.  
  8. from gi.repository import GLib, Wnck, Pango, Gdk, Gtk
  9.  
  10. color = dict(fg = "#B57614", bg = "#FABD2F", hc = "#076678", bc = "#B57614")
  11. #color = dict(fg = "#FFFFFF", bg = "#171717CC", hc = "#FFFFFF", bc = "#171717CC")
  12.  
  13. def scan_alpha(alpha, rgba):
  14. if rgba[0] == '#' and len(rgba) == 9:
  15. return float(int(rgba[7:9], 16)) / 255.0
  16. else:
  17. return alpha
  18.  
  19. class MenuItem(Gtk.ListBoxRow):
  20. def __init__(self, wind):
  21. super(Gtk.ListBoxRow, self).__init__()
  22. self.wind = wind
  23. self.wind.connect("name-changed", self.name_changed)
  24. self.wind.connect("icon-changed", self.icon_changed)
  25. self.cont = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, 3)
  26. self.icon = Gtk.Image.new_from_pixbuf(wind.get_icon())
  27. self.text = Gtk.Label.new(wind.get_name())
  28. self.text.set_markup("<b>%s</b>" % wind.get_name())
  29. self.text.set_width_chars(100)
  30. self.text.set_ellipsize(Pango.EllipsizeMode.START)
  31. self.cont.add(self.icon)
  32. self.cont.add(self.text)
  33. self.add(self.cont)
  34.  
  35. def raise_window(self):
  36. self.wind.activate(Gtk.get_current_event_time())
  37.  
  38. def name_changed(self, w):
  39. self.text.set_markup("<b>%s</b>" % w.get_name())
  40.  
  41. def icon_changed(self, w):
  42. self.icon.set_text(w.get_icon())
  43.  
  44. class Menu(Gtk.ListBox):
  45. def __init__(self):
  46. super(Gtk.ListBox, self).__init__()
  47. self.pattern = "\\s*{0}\\s*".format(os.path.basename(sys.argv[0]))
  48. self.screen = Wnck.Screen.get_default()
  49. self.screen.force_update()
  50. self.connect("destroy", self.destroy)
  51. self.screen.connect("window-opened", self.new_window)
  52. self.screen.connect("window-closed", self.del_window)
  53. self.screen.connect("active-window-changed", self.sel_window)
  54. for w in self.screen.get_windows():
  55. if not re.match(self.pattern, w.get_name()):
  56. label = MenuItem(w)
  57. self.add(label)
  58. self.set_activate_on_single_click(True)
  59. self.set_selection_mode(Gtk.SelectionMode.BROWSE)
  60. self.connect("row-activated", self.activate_row)
  61.  
  62. def sel_window(self, s, w):
  63. w = s.get_active_window()
  64. for i in self.get_children():
  65. if w == i.wind:
  66. self.select_row(i)
  67. return None
  68.  
  69. def activate_row(self, w, r):
  70. r.raise_window()
  71. w.unselect_all()
  72.  
  73. def show_all(self):
  74. height = 0
  75. width = 0
  76. for i in self.get_children():
  77. a = i.get_allocation()
  78. height += a.height
  79. width = a.width if a.width > width else width
  80. self.get_toplevel().resize(width, height)
  81. super().show_all()
  82.  
  83. def new_window(self, s, w):
  84. if not re.match(self.pattern, w.get_name()):
  85. label = MenuItem(w)
  86. self.add(label)
  87. self.show_all()
  88.  
  89. def del_window(self, s, w):
  90. for i in self.get_children():
  91. if i.wind == w:
  92. self.remove(i)
  93. self.show_all()
  94. return None
  95.  
  96. def destroy(self, w):
  97. self.screen = None
  98. Wnck.shutdown()
  99.  
  100. class MenuShell(Gtk.EventBox):
  101. def __init__(self):
  102. super(Gtk.EventBox, self).__init__()
  103. Wnck.set_default_icon_size(16)
  104. self.add_events(Gdk.EventMask.BUTTON_PRESS_MASK)
  105. self.wind = Gtk.Window.new(Gtk.WindowType.POPUP)
  106. self.wind.add_events(Gdk.EventMask.FOCUS_CHANGE_MASK)
  107. self.wind.set_type_hint(Gdk.WindowTypeHint.MENU)
  108. self.menu = Menu()
  109. self.wind.add(self.menu)
  110. self.wind.show_all()
  111. self.wind.hide()
  112. self.wind.connect("focus-out-event", self.unfocus)
  113. self.text = Gtk.Label.new()
  114. self.text.set_markup("<span color='{0}' weight='bold'>\u21b4</span>".format(color["fg"][0:7]))
  115. self.text.set_property("margin-left", 5)
  116. self.text.set_property("margin-right", 8)
  117. self.add(self.text)
  118. self.connect("button-press-event", self.click)
  119. self.show_all()
  120.  
  121. def click(self, w, e):
  122. if e.button == 1:
  123. if w.wind.is_visible():
  124. self.wind.hide()
  125. else:
  126. x = self.get_screen().get_width() - self.wind.get_window().get_width()
  127. self.wind.move(x, 18)
  128. self.wind.show()
  129.  
  130. def unfocus(self, w, e):
  131. print("focus")
  132. self.wind.hide()
  133. return True
  134.  
  135. class DT(object):
  136. def __init__(self):
  137. self.time = arrow.now()
  138.  
  139. def __format__(self, fmt):
  140. return self.time.format(fmt)
  141.  
  142. class Clock(Gtk.Label):
  143. def __init__(self):
  144. super(Gtk.Label, self).__init__()
  145. GLib.timeout_add(1000, self.spin)
  146.  
  147. def spin(self):
  148. self.set_markup("<b><span color='{0}'>{2:ddd, MMM DD YYYY}</span><span color='{1}'>&#032;&#8212;&#032;{2:HH:mm:ss A ZZZ (ZZ})</span></b>".format(color["hc"][0:7], color["fg"][0:7], DT()))
  149. return True
  150.  
  151. class BarBG(Gtk.DrawingArea):
  152. def __init__(self):
  153. super(Gtk.DrawingArea, self).__init__()
  154. self.color1 = Gdk.RGBA()
  155. self.color2 = Gdk.RGBA()
  156. self.color1.parse(color["bg"][0:7])
  157. self.color1.alpha = scan_alpha(self.color1.alpha, color["bg"])
  158. self.color2.parse(color["bc"][0:7])
  159. self.color2.alpha = scan_alpha(self.color2.alpha, color["bc"])
  160. self.set_hexpand(True)
  161. self.set_vexpand(True)
  162. self.connect("draw", self.render_bg)
  163.  
  164. def render_bg(self, w, context):
  165. area = self.get_allocation()
  166. Gdk.cairo_set_source_rgba(context, self.color1)
  167. context.rectangle(area.x, area.y, area.width, area.height)
  168. context.fill()
  169. Gdk.cairo_set_source_rgba(context, self.color2)
  170. context.rectangle(area.x, area.y, area.width, area.height)
  171. context.stroke()
  172. return True
  173.  
  174. class Main(Gtk.Window):
  175. def __init__(self):
  176. super(Gtk.Window, self).__init__()
  177. signal.signal(signal.SIGINT, lambda s, f: Gtk.main_quit())
  178. self.cont = Gtk.Overlay.new()
  179. self.area = BarBG()
  180. self.cont.add(self.area)
  181. self.time = Clock()
  182. self.time.set_halign(Gtk.Align.CENTER)
  183. self.cont.add_overlay(self.time)
  184. self.menu = MenuShell()
  185. self.menu.set_halign(Gtk.Align.END)
  186. self.cont.add_overlay(self.menu)
  187. self.add(self.cont)
  188. self.set_type_hint(Gdk.WindowTypeHint.DESKTOP)
  189. self.move(0, 0)
  190. self.set_default_size(self.screen_width(), 18)
  191. self.connect("destroy", self.on_destroy)
  192. self.if_has_compositor()
  193.  
  194. def screen_width(self):
  195. return self.get_screen().get_width()
  196.  
  197. def screen_composited(self):
  198. return self.get_screen().is_composited()
  199.  
  200. def if_has_compositor(self):
  201. if self.screen_composited():
  202. self.set_app_paintable(True)
  203. self.set_visual(self.get_screen().get_rgba_visual())
  204. self.connect("draw", self.__draw)
  205.  
  206. def __draw(self, w, context):
  207. ax, ay, aw, ah = context.clip_extents()
  208. context.set_source_rgba(0.0, 0.0, 0.0, 0.0)
  209. context.set_operator(cairo.OPERATOR_SOURCE)
  210. context.rectangle(ax, ay, aw, ah)
  211. context.fill()
  212.  
  213. def on_destroy(self, w):
  214. print("\r\n")
  215. Gtk.main_quit()
  216.  
  217.  
  218. main = Main()
  219. main.show_all()
  220. Gtk.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement