from PySide6 import QtGui
from PySide6.QtCore import Qt
from PySide6.QtWidgets import QMainWindow


class WindowDragger:
    # Permet de déplacer une fenêtre sans barre de titre.

    def __init__(self, window: QMainWindow):
        self._window   = window
        self._drag_pos = None

    def mouse_press(self, event: QtGui.QMouseEvent) -> None:
        if event.button() == Qt.MouseButton.LeftButton:
            self._drag_pos = (
                event.globalPosition().toPoint()
                - self._window.frameGeometry().topLeft()
            )

    def mouse_move(self, event: QtGui.QMouseEvent) -> None:
        if event.buttons() & Qt.MouseButton.LeftButton and self._drag_pos is not None:
            self._window.move(event.globalPosition().toPoint() - self._drag_pos)

    def mouse_release(self, _event) -> None:
        self._drag_pos = None
