Skip to content

Commit bf32918

Browse files
committed
修复 Flyout 在 mac 下无法使用中文输入法的问题
1 parent 583c1b6 commit bf32918

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

qfluentwidgets/components/widgets/flyout.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# coding:utf-8
22
from enum import Enum
3+
import sys
34
from typing import Union
45

56
from PyQt5.QtCore import (Qt, QPropertyAnimation, QPoint, QParallelAnimationGroup, QEasingCurve, QMargins,
6-
QRectF, QObject, QSize, pyqtSignal)
7+
QRectF, QObject, QSize, pyqtSignal, QEvent)
78
from PyQt5.QtGui import QPixmap, QPainter, QColor, QCursor, QIcon, QImage, QPainterPath, QBrush, QMovie, QImageReader
89
from PyQt5.QtWidgets import QWidget, QGraphicsDropShadowEffect, QLabel, QHBoxLayout, QVBoxLayout, QApplication
910

@@ -207,20 +208,34 @@ class Flyout(QWidget):
207208

208209
closed = pyqtSignal()
209210

210-
def __init__(self, view: FlyoutViewBase, parent=None, isDeleteOnClose=True):
211+
def __init__(self, view: FlyoutViewBase, parent=None, isDeleteOnClose=True, isMacInputMethodEnabled=False):
211212
super().__init__(parent=parent)
212213
self.view = view
213214
self.hBoxLayout = QHBoxLayout(self)
214215
self.aniManager = None # type: FlyoutAnimationManager
215216
self.isDeleteOnClose = isDeleteOnClose
217+
self.isMacInputMethodEnabled = isMacInputMethodEnabled
216218

217219
self.hBoxLayout.setContentsMargins(15, 8, 15, 20)
218220
self.hBoxLayout.addWidget(self.view)
219221
self.setShadowEffect()
220222

221223
self.setAttribute(Qt.WA_TranslucentBackground)
222-
self.setWindowFlags(Qt.Popup | Qt.FramelessWindowHint |
223-
Qt.NoDropShadowWindowHint)
224+
225+
if sys.platform != "darwin" or not isMacInputMethodEnabled:
226+
self.setWindowFlags(Qt.Popup | Qt.FramelessWindowHint |
227+
Qt.NoDropShadowWindowHint)
228+
else:
229+
self.setWindowFlags(Qt.Dialog | Qt.FramelessWindowHint | Qt.NoDropShadowWindowHint)
230+
QApplication.instance().installEventFilter(self)
231+
232+
def eventFilter(self, watched, event):
233+
if sys.platform == "darwin" and self.isMacInputMethodEnabled:
234+
if self.isVisible() and event.type() == QEvent.MouseButtonPress:
235+
if not self.rect().contains(self.mapFromGlobal(event.globalPos())):
236+
self.close()
237+
238+
return super().eventFilter(watched, event)
224239

225240
def setShadowEffect(self, blurRadius=35, offset=(0, 8)):
226241
""" add shadow to dialog """
@@ -252,7 +267,7 @@ def exec(self, pos: QPoint, aniType=FlyoutAnimationType.PULL_UP):
252267

253268
@classmethod
254269
def make(cls, view: FlyoutViewBase, target: Union[QWidget, QPoint] = None, parent=None,
255-
aniType=FlyoutAnimationType.PULL_UP, isDeleteOnClose=True):
270+
aniType=FlyoutAnimationType.PULL_UP, isDeleteOnClose=True, isMacInputMethodEnabled=False):
256271
""" create and show a flyout
257272
258273
Parameters
@@ -272,7 +287,7 @@ def make(cls, view: FlyoutViewBase, target: Union[QWidget, QPoint] = None, paren
272287
isDeleteOnClose: bool
273288
whether delete flyout automatically when flyout is closed
274289
"""
275-
w = cls(view, parent, isDeleteOnClose)
290+
w = cls(view, parent, isDeleteOnClose, isMacInputMethodEnabled)
276291

277292
if target is None:
278293
return w
@@ -290,7 +305,7 @@ def make(cls, view: FlyoutViewBase, target: Union[QWidget, QPoint] = None, paren
290305
@classmethod
291306
def create(cls, title: str, content: str, icon: Union[FluentIconBase, QIcon, str] = None,
292307
image: Union[str, QPixmap, QImage] = None, isClosable=False, target: Union[QWidget, QPoint] = None,
293-
parent=None, aniType=FlyoutAnimationType.PULL_UP, isDeleteOnClose=True):
308+
parent=None, aniType=FlyoutAnimationType.PULL_UP, isDeleteOnClose=True, isMacInputMethodEnabled=False):
294309
""" create and show a flyout using the default view
295310
296311
Parameters
@@ -323,7 +338,7 @@ def create(cls, title: str, content: str, icon: Union[FluentIconBase, QIcon, str
323338
whether delete flyout automatically when flyout is closed
324339
"""
325340
view = FlyoutView(title, content, icon, image, isClosable)
326-
w = cls.make(view, target, parent, aniType, isDeleteOnClose)
341+
w = cls.make(view, target, parent, aniType, isDeleteOnClose, isMacInputMethodEnabled)
327342
view.closed.connect(w.close)
328343
return w
329344

0 commit comments

Comments
 (0)