master
yutent 2023-08-30 09:33:21 +08:00
parent 360cd9a5a8
commit 2ba819474b
1 changed files with 52 additions and 34 deletions

View File

@ -4,7 +4,7 @@ from Xlib import display
# from pprint import pprint as print # from pprint import pprint as print
import gi, cairo import gi, cairo
gi.require_version('Gtk', '3.0') gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk from gi.repository import Gtk, Gdk, GdkPixbuf
# 获取默认显示器 # 获取默认显示器
dp = Gdk.Display.get_default() dp = Gdk.Display.get_default()
@ -45,17 +45,19 @@ class App(Gtk.Window):
def __init__(self): def __init__(self):
Gtk.Window.__init__(self, type=Gtk.WindowType.POPUP) Gtk.Window.__init__(self, type=Gtk.WindowType.POPUP)
self.set_name('Xshot')
self.set_default_size(total_width, total_height) self.set_default_size(total_width, total_height)
self.set_type_hint(Gdk.WindowTypeHint.DESKTOP) self.set_type_hint(Gdk.WindowTypeHint.DESKTOP)
# 设置透明背景 # 设置透明背景
screen = self.get_screen() screen = self.get_screen()
visual = screen.get_rgba_visual() visual = screen.get_rgba_visual()
if visual and screen.is_composited(): # if visual and screen.is_composited():
self.set_visual(visual) # self.set_visual(visual)
self.set_app_paintable(True)
# self.set_app_paintable(True)
self.set_events(Gdk.EventMask.POINTER_MOTION_MASK) self.set_events(Gdk.EventMask.POINTER_MOTION_MASK)
@ -71,15 +73,20 @@ class App(Gtk.Window):
# layout.put(btn, 0, 0) # layout.put(btn, 0, 0)
# self.add(layout) # self.add(layout)
layout = Gtk.Fixed()
self.drawing_area = Gtk.DrawingArea() self.drawing_area = Gtk.DrawingArea()
self.drawing_area.set_size_request(total_width, total_height)
self.drawing_area.add_events( self.drawing_area.add_events(
Gdk.EventMask.BUTTON_PRESS_MASK | Gdk.EventMask.BUTTON_RELEASE_MASK | Gdk.EventMask.POINTER_MOTION_MASK) Gdk.EventMask.BUTTON_PRESS_MASK | Gdk.EventMask.BUTTON_RELEASE_MASK | Gdk.EventMask.POINTER_MOTION_MASK)
self.drawing_area.connect("draw", self.on_draw) self.drawing_area.connect("draw", self.on_draw)
self.drawing_area.connect("button-press-event", self.on_button_press) self.drawing_area.connect("button-press-event", self.on_button_press)
self.drawing_area.connect("button-release-event", self.on_button_release) self.drawing_area.connect("button-release-event", self.on_button_release)
self.drawing_area.connect("motion-notify-event", self.on_motion_notify) self.drawing_area.connect("motion-notify-event", self.on_motion_notify)
self.add(self.drawing_area) layout.add(self.drawing_area)
self.add(layout)
# Initialize rectangle and control point size # Initialize rectangle and control point size
self.start_x = None self.start_x = None
self.start_y = None self.start_y = None
@ -91,6 +98,8 @@ class App(Gtk.Window):
self.connect("draw", on_draw) self.connect("draw", on_draw)
def on_draw(self, widget, cr): def on_draw(self, widget, cr):
if self.start_x is not None and self.start_y is not None: if self.start_x is not None and self.start_y is not None:
width = self.current_x - self.start_x width = self.current_x - self.start_x
@ -103,6 +112,8 @@ class App(Gtk.Window):
for y in [self.start_y, self.start_y + height / 2, self.current_y]: for y in [self.start_y, self.start_y + height / 2, self.current_y]:
cr.arc(x, y, 5, 0, 2 * 3.14) cr.arc(x, y, 5, 0, 2 * 3.14)
cr.fill() cr.fill()
def on_button_press(self, widget, event): def on_button_press(self, widget, event):
if event.button == 1: # left mouse button if event.button == 1: # left mouse button
self.start_x = event.x self.start_x = event.x
@ -110,10 +121,14 @@ class App(Gtk.Window):
self.current_x = event.x self.current_x = event.x
self.current_y = event.y self.current_y = event.y
self.dragging = True self.dragging = True
def on_button_release(self, widget, event): def on_button_release(self, widget, event):
if event.button == 1: # left mouse button if event.button == 1: # left mouse button
self.dragging = False self.dragging = False
self.queue_draw() self.queue_draw()
def on_motion_notify(self, widget, event): def on_motion_notify(self, widget, event):
if self.dragging: if self.dragging:
self.current_x = event.x self.current_x = event.x
@ -123,8 +138,11 @@ class App(Gtk.Window):
def run(self): def run(self):
try:
self.show_all() self.show_all()
Gtk.main() Gtk.main()
except:
pass