Skip to content

Commit d4241f7

Browse files
author
ficapy
committed
fix multi-screen screenshot lukas-blecher#194
1 parent 19b332e commit d4241f7

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

pix2tex/gui.py

+25-6
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
import os
66
import tempfile
77
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
910
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, \
1112
QPushButton, QTextEdit, QFormLayout, QHBoxLayout, QDoubleSpinBox
1213
from pynput.mouse import Controller
1314

@@ -251,6 +252,25 @@ def __init__(self, parent):
251252

252253
self.mouse = Controller()
253254

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+
254274
def snip(self):
255275
self.isSnipping = True
256276
self.setWindowFlags(QtCore.Qt.WindowType.WindowStaysOnTopHint)
@@ -273,7 +293,7 @@ def paintEvent(self, event):
273293
qp.drawRect(QtCore.QRect(self.begin, self.end))
274294

275295
def keyPressEvent(self, event):
276-
if event.key() == QtCore.Qt.Key_Escape:
296+
if event.key() == QtCore.Qt.Key.Key_Escape.value:
277297
QApplication.restoreOverrideCursor()
278298
self.close()
279299
self.parent.show()
@@ -296,8 +316,6 @@ def mouseReleaseEvent(self, event):
296316

297317
startPos = self.startPos
298318
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
301319

302320
x1 = int(min(startPos[0], endPos[0]))
303321
y1 = int(min(startPos[1], endPos[1]))
@@ -310,7 +328,8 @@ def mouseReleaseEvent(self, event):
310328
img = ImageGrab.grab(bbox=(x1, y1, x2, y2), all_screens=True)
311329
except Exception as e:
312330
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)
314333
else:
315334
raise e
316335
QApplication.processEvents()

0 commit comments

Comments
 (0)