Skip to content

Commit

Permalink
重构标题栏
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiyiYo committed Jul 25, 2022
1 parent 87faece commit 7fda6f2
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 56 deletions.
5 changes: 2 additions & 3 deletions qframelesswindow/__init__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import sys

from .titlebar import TitleBar

if sys.platform == "win32":
from .titlebar import WindowsTitleBar as TitleBar
from .windows import AcrylicWindow
from .windows import WindowsFramelessWindow as FramelessWindow
from .windows import WindowsWindowEffect as WindowEffect
elif sys.platform == "darwin":
from .titlebar import MacTitleBar as TitleBar
from .mac import AcrylicWindow
from .mac import MacFramelessWindow as FramelessWindow
from .mac import MacWindowEffect as WindowEffect
else:
from .titlebar import LinuxTitleBar as TitleBar
from .linux import LinuxFramelessWindow as FramelessWindow
from .linux import LinuxWindowEffect as WindowEffect

Expand Down
69 changes: 16 additions & 53 deletions qframelesswindow/titlebar/__init__.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
# coding:utf-8
import sys

from PyQt5.QtCore import Qt, QEvent
from PyQt5.QtCore import QEvent, Qt
from PyQt5.QtWidgets import QHBoxLayout, QWidget

if sys.platform == "win32":
import win32con
from win32api import SendMessage
from win32gui import ReleaseCapture
elif sys.platform == "darwin":
from ..utils.mac_utils import MacMoveResize
else:
from ..utils.linux_utils import LinuxMoveResize

from ..utils import startSystemMove
from .title_bar_buttons import CloseButton, MaximizeButton, MinimizeButton


class TitleBarBase(QWidget):
""" Title bar base class """
class TitleBar(QWidget):
""" Title bar """

def __init__(self, parent):
super().__init__(parent)
Expand Down Expand Up @@ -60,6 +52,18 @@ def mouseDoubleClickEvent(self, event):

self.__toggleMaxState()

def mouseMoveEvent(self, e):
if sys.platform != "win32" or not self._isDragRegion(e.pos()):
return

startSystemMove(self.window(), e.globalPos())

def mousePressEvent(self, e):
if sys.platform == "win32" or e.button() != Qt.LeftButton or not self._isDragRegion(e.pos()):
return

startSystemMove(self.window(), e.globalPos())

def __toggleMaxState(self):
""" Toggles the maximization state of the window and change icon """
if self.window().isMaximized():
Expand All @@ -70,44 +74,3 @@ def __toggleMaxState(self):
def _isDragRegion(self, pos):
""" Check whether the pressed point belongs to the area where dragging is allowed """
return 0 < pos.x() < self.width() - 46 * 3


class WindowsTitleBar(TitleBarBase):
""" Title bar for Windows system """

def mouseMoveEvent(self, event):
if not self._isDragRegion(event.pos()):
return

ReleaseCapture()
SendMessage(self.window().winId(), win32con.WM_SYSCOMMAND,
win32con.SC_MOVE | win32con.HTCAPTION, 0)
event.ignore()


class LinuxTitleBar(TitleBarBase):
""" Title bar for Unix system """

def mousePressEvent(self, event):
if event.button() != Qt.LeftButton or not self._isDragRegion(event.pos()):
return

LinuxMoveResize.startSystemMove(self.window(), event.globalPos())


class MacTitleBar(TitleBarBase):
""" Title bar for Mac OS """

def mousePressEvent(self, event):
if event.button() != Qt.LeftButton or not self._isDragRegion(event.pos()):
return

MacMoveResize.startSystemMove(self.window(), event.globalPos())


if sys.platform == "win32":
TitleBar = WindowsTitleBar
elif sys.platform == "darwin":
TitleBar = MacTitleBar
else:
TitleBar = LinuxTitleBar
40 changes: 40 additions & 0 deletions qframelesswindow/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# coding:utf-8
import sys

if sys.platform == "win32":
from .win32_utils import WindowsMoveResize as MoveResize
elif sys.platform == "darwin":
from .mac_utils import MacMoveResize as MoveResize
else:
from .linux_utils import LinuxMoveResize as MoveResize


def startSystemMove(window, globalPos):
""" resize window
Parameters
----------
window: QWidget
window
globalPos: QPoint
the global point of mouse release event
"""
MoveResize.startSystemMove(window, globalPos)


def starSystemResize(window, globalPos, edges):
""" resize window
Parameters
----------
window: QWidget
window
globalPos: QPoint
the global point of mouse release event
edges: `Qt.Edges`
window edges
"""
MoveResize.starSystemResize(window, globalPos, edges)
41 changes: 41 additions & 0 deletions qframelesswindow/utils/win32_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,44 @@ def getPosition(cls, hWnd):
return appbarData.uEdge

return cls.NO_POSITION


class WindowsMoveResize:
""" Tool class for moving and resizing Mac OS window """

@staticmethod
def startSystemMove(window, globalPos):
""" resize window
Parameters
----------
window: QWidget
window
globalPos: QPoint
the global point of mouse release event
"""
win32gui.ReleaseCapture()
win32api.SendMessage(
int(window.winId()),
win32con.WM_SYSCOMMAND,
win32con.SC_MOVE | win32con.HTCAPTION,
0
)

@classmethod
def starSystemResize(cls, window, globalPos, edges):
""" resize window
Parameters
----------
window: QWidget
window
globalPos: QPoint
the global point of mouse release event
edges: `Qt.Edges`
window edges
"""
pass

0 comments on commit 7fda6f2

Please sign in to comment.