Created
October 26, 2023 08:08
-
-
Save AllanChain/123b77e3415332f63a434db0eff01644 to your computer and use it in GitHub Desktop.
Pin image on desktop after spectacle screenshot. Works on Wayland.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import sys | |
import subprocess | |
from PySide6 import QtCore, QtWidgets, QtGui | |
class MyWidget(QtWidgets.QLabel): | |
def __init__(self): | |
super().__init__() | |
self.window().setWindowFlag(QtCore.Qt.Window | |
| QtCore.Qt.FramelessWindowHint | |
| QtCore.Qt.WindowStaysOnTopHint) | |
self.image: QtGui.QPixmap | None = None | |
def load_image(self) -> None: | |
clipboard = QtGui.QGuiApplication.clipboard() | |
clipboard_image = clipboard.pixmap() | |
if clipboard_image.isNull(): | |
self.close() | |
self.image = clipboard_image | |
self.dpr = self.window().devicePixelRatio() | |
clipboard_image.setDevicePixelRatio(self.dpr) | |
self.resize(int(clipboard_image.width() / self.dpr), | |
int(clipboard_image.height() / self.dpr)) | |
self.setPixmap(clipboard_image) | |
def mouseDoubleClickEvent(self, _) -> None: | |
self.close() | |
def mouseMoveEvent(self, _): | |
self.window().windowHandle().startSystemMove() | |
def wheelEvent(self, event: QtGui.QWheelEvent) -> None: | |
if event.angleDelta().y() > 0: | |
clipboard_image = self.image.scaledToWidth( | |
int(self.pixmap().width() * 1.1)) | |
elif event.angleDelta().y() < 0: | |
clipboard_image = self.image.scaledToWidth( | |
int(self.pixmap().width() / 1.1)) | |
self.resize(int(clipboard_image.width() / self.dpr), | |
int(clipboard_image.height() / self.dpr)) | |
self.setPixmap(clipboard_image) | |
def main(): | |
ret = subprocess.run(["spectacle", "--background", | |
"--region", "--copy-image", "--nonotify"]) | |
if ret.returncode != 0: | |
return | |
app = QtWidgets.QApplication([]) | |
widget = MyWidget() | |
widget.show() | |
widget.window().setWindowTitle("Pimg") | |
QtCore.QTimer.singleShot(100, widget.load_image) | |
sys.exit(app.exec()) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment