No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

transparent.py 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import math
  2. import sys
  3. import gi
  4. gi.require_version("Gtk", "3.0")
  5. from gi.repository import Gtk # noqa
  6. import cairo # noqa
  7. from OpenGL.GL import glClearColor, glClear, glFlush, GL_COLOR_BUFFER_BIT
  8. class TestWin(Gtk.Window):
  9. WINDOW_POPUP = 1
  10. def __init__(self):
  11. Gtk.Window.__init__(self, type=self.WINDOW_POPUP)
  12. self._default_monitor = 0
  13. self.set_window_geometry()
  14. self.layout = Gtk.Box()
  15. self.layout.show()
  16. self.add(self.layout)
  17. self.gl = Gtk.GLArea()
  18. self.gl.connect("render", self.gl_render)
  19. self.gl.connect("realize", self.gl_realize)
  20. # self.gl.connect('create-context', self.gl_context)
  21. self.gl.show()
  22. self.layout.pack_end(self.gl, True, True, 0)
  23. self.make_rgba()
  24. self.set_app_paintable(True)
  25. self.connect("draw", self.on_draw)
  26. self.show()
  27. self.make_untouchable()
  28. def make_rgba(self):
  29. self.screen = self.get_screen()
  30. self.visual = self.screen.get_rgba_visual()
  31. if self.visual is None or not self.screen.is_composited():
  32. print(
  33. "WARNING: You do not appear to be running a composite manager"
  34. ", transparency not available"
  35. )
  36. else:
  37. self.set_visual(self.visual)
  38. def set_window_geometry(self):
  39. geo = self.get_screen_size(self._default_monitor)
  40. scale = (1.05, 6)
  41. x, y, w, h = (
  42. geo.x + geo.width / 2 - geo.width / scale[0] / 2,
  43. geo.y,
  44. geo.width / scale[0],
  45. geo.height / scale[1],
  46. )
  47. self.move(x, y)
  48. self.resize(w, h)
  49. print(f"geometry: @{x:.0f},{y:.0f} {w:.0f}x{h:.0f}")
  50. def get_screen_size(self, monitor):
  51. return self.get_screen().get_monitor_geometry(monitor)
  52. def on_draw(self, widget, cr):
  53. # make everything transparent
  54. cr.set_source_rgba(1.0, 1.0, 1.0, 0.1)
  55. # cr.set_operator(cairo.OPERATOR_SOURCE)
  56. cr.paint()
  57. cr.set_operator(cairo.OPERATOR_OVER)
  58. # draw the circle
  59. w, h = self.get_size()
  60. cr.translate(w / 2, h / 2)
  61. cr.set_source_rgba(0.7, 0.3, 0.0, 0.6)
  62. cr.arc(0, 0, 10, 0, 2 * math.pi)
  63. cr.stroke_preserve()
  64. cr.fill()
  65. def make_untouchable(self):
  66. self.input_shape_combine_region(cairo.Region(cairo.RectangleInt(0, 0, 0, 0)))
  67. def gl_realize(self, gl_area):
  68. error = gl_area.get_error()
  69. if error != None:
  70. print(f"ERROR: No OpenGL ({error})")
  71. def gl_context(self, gl_area):
  72. c = gl_area.get_context()
  73. print(c, "context")
  74. return c
  75. def gl_render(self, area, context):
  76. glClearColor(1.0, 0.5, 0.5, 1.0)
  77. glClear(GL_COLOR_BUFFER_BIT)
  78. glFlush()
  79. print("rendering... done")
  80. return True
  81. def run(self):
  82. Gtk.main()
  83. return 0
  84. if __name__ == "__main__":
  85. app = TestWin()
  86. sys.exit(app.run())