Debian Codegraph @

src/platform/mac/foregroundbackgroundfilter.mm

raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #include "foregroundbackgroundfilter.h"
4
5 #include "common/log.h"
6
7 #include <QEvent>
8 #include <QGuiApplication>
9 #include <QWindow>
10
11 #include <Cocoa/Cocoa.h>
12 #include <Carbon/Carbon.h>
13
14 namespace {
15
16 bool isNormalApp()
17 {
18 return [NSApp activationPolicy] == NSApplicationActivationPolicyRegular;
19 }
20
21 void becomeNormalApp()
22 {
23 if (!isNormalApp()) {
24 COPYQ_LOG("Become normal app");
25 if (![NSApp setActivationPolicy:NSApplicationActivationPolicyRegular])
26 COPYQ_LOG("Unable to become a normal app");
27 }
28 }
29
30 void becomeAccessoryApp()
31 {
32 if ([NSApp activationPolicy] != NSApplicationActivationPolicyAccessory) {
33 COPYQ_LOG("Become accessory app");
34 if (![NSApp setActivationPolicy:NSApplicationActivationPolicyAccessory])
35 COPYQ_LOG("Unable to become a accessory app");
36 }
37 }
38
39 void becomeBackgroundApp()
40 {
41 if ([NSApp activationPolicy] != NSApplicationActivationPolicyProhibited) {
42 COPYQ_LOG("Become background app");
43 if (![NSApp setActivationPolicy:NSApplicationActivationPolicyProhibited])
44 COPYQ_LOG("Unable to become a background app");
45 }
46 }
47
48 bool isNormalAppWindow(QWindow *window)
49 {
50 const auto type = window->type();
51 return (type == Qt::Dialog || type == Qt::Window)
52 && !window->flags().testFlag(Qt::FramelessWindowHint);
53 }
54
55 } // namespace
56
57 void ForegroundBackgroundFilter::installFilter(QObject *parent)
58 {
59 auto filter = new ForegroundBackgroundFilter(parent);
60 parent->installEventFilter(filter);
61 }
62
63 ForegroundBackgroundFilter::ForegroundBackgroundFilter(QObject *parent)
64 : QObject(parent)
65 {
66 }
67
68 bool ForegroundBackgroundFilter::eventFilter(QObject *obj, QEvent *ev)
69 {
70 auto window = qobject_cast<QWindow*>(obj);
71 if ( !window || !window->isTopLevel() )
72 return false;
73
74 const auto eventType = ev->type();
75 if (eventType == QEvent::Show) {
76 // WORKAROUND: Transform menu to frameless dialog to fix keyboard focus.
77 if (window->type() == Qt::Popup && QGuiApplication::focusWindow() == nullptr) {
78 COPYQ_LOG("Fixing focus for popup");
79 window->setFlags(window->flags() | Qt::Dialog | Qt::FramelessWindowHint);
80 }
81
82 // Allow windows to pop up on current space.
83 const auto wid = window->winId();
84 auto nsView = reinterpret_cast<NSView*>(wid);
85 auto nsWindow = [nsView window];
86 if (nsWindow)
87 [nsWindow setCollectionBehavior: NSWindowCollectionBehaviorMoveToActiveSpace];
88
89 if ( isNormalAppWindow(window) )
90 becomeNormalApp();
91 else if (!isNormalApp())
92 becomeAccessoryApp();
93 } else if (eventType == QEvent::Hide) {
94 // Don't background if there are any other visible windows..
95 auto windows = QGuiApplication::topLevelWindows();
96 windows.removeOne(window);
97
98 const bool hasFocusableWindow = std::any_of(
99 std::begin(windows), std::end(windows),
100 [](QWindow *aWindow) {
101 return aWindow->isVisible() && isNormalAppWindow(aWindow);
102 });
103
104 if (hasFocusableWindow) {
105 becomeNormalApp();
106 } else {
107 const bool hasAccessoryWindow = std::any_of(
108 std::begin(windows), std::end(windows),
109 [](QWindow *aWindow) {
110 return aWindow->isVisible();
111 });
112
113 if (hasAccessoryWindow)
114 becomeAccessoryApp();
115 else
116 becomeBackgroundApp(); // This also focuses the previous app.
117 }
118 }
119
120 return false;
121 }