11# coding:utf-8
22from enum import Enum
3+ import sys
34from typing import Union
45
56from PyQt5 .QtCore import (Qt , QPropertyAnimation , QPoint , QParallelAnimationGroup , QEasingCurve , QMargins ,
6- QRectF , QObject , QSize , pyqtSignal )
7+ QRectF , QObject , QSize , pyqtSignal , QEvent )
78from PyQt5 .QtGui import QPixmap , QPainter , QColor , QCursor , QIcon , QImage , QPainterPath , QBrush , QMovie , QImageReader
89from 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