From 7f0e0cd0bd5b625503db86950a252ffb98842f62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=BDeljan=20Rikalo?= Date: Tue, 19 Oct 2021 14:54:10 +0200 Subject: [PATCH] Qt,Qt5: fixed broken TTrayIcon popup menu after recreating popup menu handle. issue #38849 --- lcl/interfaces/qt/qtsystemtrayicon.pas | 26 ++++++++++++++++++++++++- lcl/interfaces/qt5/qtsystemtrayicon.pas | 24 +++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/lcl/interfaces/qt/qtsystemtrayicon.pas b/lcl/interfaces/qt/qtsystemtrayicon.pas index b7fa5f64d6..08cbf1b86e 100644 --- a/lcl/interfaces/qt/qtsystemtrayicon.pas +++ b/lcl/interfaces/qt/qtsystemtrayicon.pas @@ -21,7 +21,7 @@ interface uses Classes, types, Controls, ExtCtrls, Graphics, Forms, LCLType, LCLProc, LazUTF8, - qtobjects, qt4, qtproc, qtint; + qtobjects, qt4, qtint; type TSysTrayIconPaintData = record @@ -36,11 +36,13 @@ type TQtSystemTrayIcon = class(TQtObject) private FSysTrayHook: QObject_hookH; + FDestroyedMenuHook: QObject_hookH; FHook: QSystemTrayIcon_hookH; FSysTrayWidget: QWidgetH; FCanvas: TCanvas; function BeginPaintInternal(var APaintData: TSysTrayIconPaintData): hdc; procedure EndPaintInternal(var APaintData: TSysTrayIconPaintData); + procedure DestroyedMenu; cdecl; public FTrayIcon: TCustomTrayIcon; public @@ -110,6 +112,11 @@ begin QSystemTrayIcon_hook_destroy(FHook); FHook := nil; end; + if Assigned(FDestroyedMenuHook) then + begin + QObject_hook_Destroy(FDestroyedMenuHook); + FDestroyedMenuHook := nil; + end; inherited DetachEvents; end; @@ -169,6 +176,13 @@ begin APaintData.Context := nil; end; +procedure TQtSystemTrayIcon.DestroyedMenu; cdecl; +begin + setContextMenu(nil); + if not (csDestroying in FTrayIcon.ComponentState) then + FTrayIcon.InternalUpdate; +end; + function TQtSystemTrayIcon.EventFilter(Sender: QObjectH; Event: QEventH): Boolean; cdecl; var X, Y: Integer; @@ -244,6 +258,16 @@ end; procedure TQtSystemTrayIcon.setContextMenu(menu: QMenuH); begin + if menu <> nil then + begin + if Assigned(FDestroyedMenuHook) then + begin + QObject_hook_destroy(FDestroyedMenuHook); + FDestroyedMenuHook := nil; + end; + FDestroyedMenuHook := QObject_hook_create(menu); + QObject_hook_hook_destroyed(FDestroyedMenuHook, @DestroyedMenu); + end; QSystemTrayIcon_setContextMenu(QSystemTrayIconH(TheObject), menu); end; diff --git a/lcl/interfaces/qt5/qtsystemtrayicon.pas b/lcl/interfaces/qt5/qtsystemtrayicon.pas index 217c5c83f1..f45ab2c816 100644 --- a/lcl/interfaces/qt5/qtsystemtrayicon.pas +++ b/lcl/interfaces/qt5/qtsystemtrayicon.pas @@ -36,11 +36,13 @@ type TQtSystemTrayIcon = class(TQtObject) private FSysTrayHook: QObject_hookH; + FDestroyedMenuHook: QObject_hookH; FHook: QSystemTrayIcon_hookH; FSysTrayWidget: QWidgetH; FCanvas: TCanvas; function BeginPaintInternal(var APaintData: TSysTrayIconPaintData): hdc; procedure EndPaintInternal(var APaintData: TSysTrayIconPaintData); + procedure DestroyedMenu; cdecl; public FTrayIcon: TCustomTrayIcon; public @@ -110,6 +112,11 @@ begin QSystemTrayIcon_hook_destroy(FHook); FHook := nil; end; + if Assigned(FDestroyedMenuHook) then + begin + QObject_hook_Destroy(FDestroyedMenuHook); + FDestroyedMenuHook := nil; + end; inherited DetachEvents; end; @@ -168,6 +175,13 @@ begin APaintData.Context := nil; end; +procedure TQtSystemTrayIcon.DestroyedMenu; cdecl; +begin + setContextMenu(nil); + if not (csDestroying in FTrayIcon.ComponentState) then + FTrayIcon.InternalUpdate; +end; + function TQtSystemTrayIcon.EventFilter(Sender: QObjectH; Event: QEventH ): Boolean; cdecl; var @@ -246,6 +260,16 @@ end; procedure TQtSystemTrayIcon.setContextMenu(menu: QMenuH); begin + if menu <> nil then + begin + if Assigned(FDestroyedMenuHook) then + begin + QObject_hook_destroy(FDestroyedMenuHook); + FDestroyedMenuHook := nil; + end; + FDestroyedMenuHook := QObject_hook_create(menu); + QObject_hook_hook_destroyed(FDestroyedMenuHook, @DestroyedMenu); + end; QSystemTrayIcon_setContextMenu(QSystemTrayIconH(TheObject), menu); end;