-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
WeltHideWindow.py
executable file
·154 lines (137 loc) · 4.78 KB
/
WeltHideWindow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2018年3月1日
@author: Irony
@site: https://pyqt.site , https://github.com/PyQt5
@email: [email protected]
@file: WeltHideWindow
@description: 简单的窗口贴边隐藏
"""
import os
import platform
from subprocess import getoutput
try:
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (
QApplication,
QGridLayout,
QMessageBox,
QPushButton,
QSizePolicy,
QSpacerItem,
QWidget,
)
except ImportError:
from PySide2.QtCore import Qt
from PySide2.QtWidgets import (
QApplication,
QGridLayout,
QMessageBox,
QPushButton,
QSizePolicy,
QSpacerItem,
QWidget,
)
def IsSupport():
"""判断是否支持"""
if platform.system() == "Linux":
name = os.environ.get("XDG_SESSION_DESKTOP", "") + os.environ.get(
"XDG_CURRENT_DESKTOP", ""
)
if name.lower().find("gnome") != -1:
print("gnome desktop")
return False
wid = getoutput("xprop -root _NET_SUPPORTING_WM_CHECK").split(" # ")[-1]
print("wid:", wid)
if wid:
name = getoutput("xprop -id %s _NET_WM_NAME" % wid)
print("name:", name)
if name.lower().find("gnome") != -1:
print("gnome desktop")
return False
return True
class WeltHideWindow(QWidget):
def __init__(self, *args, **kwargs):
super(WeltHideWindow, self).__init__(*args, **kwargs)
self.setWindowFlags(self.windowFlags() | Qt.FramelessWindowHint)
self.resize(400, 300)
self._width = QApplication.desktop().availableGeometry(self).width()
layout = QGridLayout(self)
layout.addItem(
QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum), 0, 0
)
self.closeBtn = QPushButton("X", self)
layout.addWidget(self.closeBtn, 0, 1)
layout.addItem(
QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding), 1, 0
)
self.closeBtn.clicked.connect(self.close)
self.closeBtn.setMinimumSize(24, 24)
self.closeBtn.setMaximumSize(24, 24)
def mousePressEvent(self, event):
"""鼠标按下事件,需要记录下坐标self._pos 和 是否可移动self._canMove"""
super(WeltHideWindow, self).mousePressEvent(event)
if event.button() == Qt.LeftButton:
self._pos = event.globalPos() - self.pos()
# 当窗口最大化或者全屏时不可移动
self._canMove = not self.isMaximized() or not self.isFullScreen()
def mouseMoveEvent(self, event):
"""鼠标移动事件,动态调整窗口位置"""
super(WeltHideWindow, self).mouseMoveEvent(event)
if event.buttons() == Qt.LeftButton and self._canMove:
self.move(event.globalPos() - self._pos)
def mouseReleaseEvent(self, event):
"""鼠标弹起事件,这个时候需要判断窗口的左边是否符合贴到左边,顶部,右边一半"""
super(WeltHideWindow, self).mouseReleaseEvent(event)
self._canMove = False
pos = self.pos()
x = pos.x()
y = pos.y()
if x < 0:
# 隐藏到左边
return self.move(1 - self.width(), y)
if y < 0:
# 隐藏到顶部
return self.move(x, 1 - self.height())
if x > self._width - self.width() / 2: # 窗口进入右边一半距离
# 隐藏到右边
return self.move(self._width - 1, y)
def enterEvent(self, event):
"""鼠标进入窗口事件,用于弹出显示窗口"""
super(WeltHideWindow, self).enterEvent(event)
pos = self.pos()
x = pos.x()
y = pos.y()
if x < 0:
return self.move(0, y)
if y < 0:
return self.move(x, 0)
if x > self._width - self.width() / 2:
return self.move(self._width - self.width(), y)
def leaveEvent(self, event):
"""鼠标离开事件,如果原先窗口已经隐藏,并暂时显示,此时离开后需要再次隐藏"""
super(WeltHideWindow, self).leaveEvent(event)
pos = self.pos()
x = pos.x()
y = pos.y()
if x == 0:
return self.move(1 - self.width(), y)
if y == 0:
return self.move(x, 1 - self.height())
if x == self._width - self.width():
return self.move(self._width - 1, y)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
if not IsSupport():
QMessageBox.warning(
None,
"Warning",
"当前桌面不支持此功能",
)
app.quit()
else:
w = WeltHideWindow()
w.show()
sys.exit(app.exec_())