5
5
import os
6
6
import tempfile
7
7
from PyQt6 import QtCore , QtGui
8
- from PyQt6 .QtCore import Qt , pyqtSlot , pyqtSignal , QThread
8
+ from PyQt6 .QtCore import Qt , pyqtSlot , pyqtSignal , QThread , QTimer
9
+ from PyQt6 .QtGui import QGuiApplication
9
10
from PyQt6 .QtWebEngineWidgets import QWebEngineView
10
- from PyQt6 .QtWidgets import QMainWindow , QApplication , QMessageBox , QVBoxLayout , QWidget ,\
11
+ from PyQt6 .QtWidgets import QMainWindow , QApplication , QMessageBox , QVBoxLayout , QWidget , \
11
12
QPushButton , QTextEdit , QFormLayout , QHBoxLayout , QDoubleSpinBox
12
13
from pynput .mouse import Controller
13
14
@@ -251,6 +252,25 @@ def __init__(self, parent):
251
252
252
253
self .mouse = Controller ()
253
254
255
+ # Create and start the timer
256
+ self .factor = QGuiApplication .primaryScreen ().devicePixelRatio ()
257
+ self .timer = QTimer (self )
258
+ self .timer .timeout .connect (self .update_geometry_based_on_cursor_position )
259
+ self .timer .start (500 )
260
+
261
+ def update_geometry_based_on_cursor_position (self ):
262
+ if not self .isSnipping :
263
+ return
264
+
265
+ # Update the geometry of the SnipWidget based on the current screen
266
+ mouse_pos = QtGui .QCursor .pos ()
267
+ screen = QGuiApplication .screenAt (mouse_pos )
268
+ if screen :
269
+ self .factor = screen .devicePixelRatio ()
270
+ screen_geometry = screen .geometry ()
271
+ self .setGeometry (screen_geometry )
272
+
273
+
254
274
def snip (self ):
255
275
self .isSnipping = True
256
276
self .setWindowFlags (QtCore .Qt .WindowType .WindowStaysOnTopHint )
@@ -273,7 +293,7 @@ def paintEvent(self, event):
273
293
qp .drawRect (QtCore .QRect (self .begin , self .end ))
274
294
275
295
def keyPressEvent (self , event ):
276
- if event .key () == QtCore .Qt .Key_Escape :
296
+ if event .key () == QtCore .Qt .Key . Key_Escape . value :
277
297
QApplication .restoreOverrideCursor ()
278
298
self .close ()
279
299
self .parent .show ()
@@ -296,8 +316,6 @@ def mouseReleaseEvent(self, event):
296
316
297
317
startPos = self .startPos
298
318
endPos = self .mouse .position
299
- # account for retina display. #TODO how to check if device is actually using retina display
300
- factor = 2 if sys .platform == "darwin" else 1
301
319
302
320
x1 = int (min (startPos [0 ], endPos [0 ]))
303
321
y1 = int (min (startPos [1 ], endPos [1 ]))
@@ -310,7 +328,8 @@ def mouseReleaseEvent(self, event):
310
328
img = ImageGrab .grab (bbox = (x1 , y1 , x2 , y2 ), all_screens = True )
311
329
except Exception as e :
312
330
if sys .platform == "darwin" :
313
- img = ImageGrab .grab (bbox = (x1 // factor , y1 // factor , x2 // factor , y2 // factor ), all_screens = True )
331
+ img = ImageGrab .grab (bbox = (x1 // self .factor , y1 // self .factor ,
332
+ x2 // self .factor , y2 // self .factor ), all_screens = True )
314
333
else :
315
334
raise e
316
335
QApplication .processEvents ()
0 commit comments