Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix native hiding of macOS windows #2583

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/browser/BrowserService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ BrowserService::BrowserService(DatabaseTabWidget* parent)
, m_dialogActive(false)
, m_bringToFrontRequested(false)
, m_wasMinimized(false)
, m_wasHidden(false)
, m_keepassBrowserUUID(QUuid::fromRfc4122(QByteArray::fromHex("de887cc3036343b8974b5911b8816224")))
{
// Don't connect the signals when used from DatabaseSettingsWidgetBrowser (parent is nullptr)
Expand Down Expand Up @@ -96,7 +97,6 @@ bool BrowserService::openDatabase(bool triggerUnlock)

if (triggerUnlock) {
m_bringToFrontRequested = true;
m_wasMinimized = getMainWindow()->isMinimized();
raiseWindow(true);
}

Expand Down Expand Up @@ -933,7 +933,11 @@ void BrowserService::hideWindow() const
getMainWindow()->showMinimized();
} else {
#ifdef Q_OS_MACOS
macUtils()->raiseLastActiveWindow();
if (m_wasHidden) {
macUtils()->hideOwnWindow();
} else {
macUtils()->raiseLastActiveWindow();
}
#else
getMainWindow()->lower();
#endif
Expand All @@ -944,6 +948,7 @@ void BrowserService::raiseWindow(const bool force)
{
m_wasMinimized = getMainWindow()->isMinimized();
#ifdef Q_OS_MACOS
m_wasHidden = macUtils()->isHidden();
macUtils()->raiseOwnWindow();
Tools::wait(500);
#else
Expand Down
1 change: 1 addition & 0 deletions src/browser/BrowserService.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public slots:
bool m_dialogActive;
bool m_bringToFrontRequested;
bool m_wasMinimized;
bool m_wasHidden;
QUuid m_keepassBrowserUUID;
};

Expand Down
2 changes: 2 additions & 0 deletions src/gui/macutils/AppKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class AppKit
pid_t activeProcessId();
pid_t ownProcessId();
bool activateProcess(pid_t pid);
bool hideProcess(pid_t pid);
bool isHidden(pid_t pid);

private:
void *self;
Expand Down
2 changes: 2 additions & 0 deletions src/gui/macutils/AppKitImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,7 @@
- (pid_t) activeProcessId;
- (pid_t) ownProcessId;
- (bool) activateProcess:(pid_t) pid;
- (bool) hideProcess:(pid_t) pid;
- (bool) isHidden:(pid_t) pid;

@end
29 changes: 28 additions & 1 deletion src/gui/macutils/AppKitImpl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,27 @@ - (pid_t) ownProcessId
- (bool) activateProcess:(pid_t) pid
{
NSRunningApplication *app = [NSRunningApplication runningApplicationWithProcessIdentifier:pid];

return [app activateWithOptions:NSApplicationActivateIgnoringOtherApps];
}

//
// Hide application by process id
//
- (bool) hideProcess:(pid_t) pid
{
NSRunningApplication *app = [NSRunningApplication runningApplicationWithProcessIdentifier:pid];
return [app hide];
}

//
// Get application hidden state by process id
//
- (bool) isHidden:(pid_t) pid
{
NSRunningApplication *app = [NSRunningApplication runningApplicationWithProcessIdentifier:pid];
return [app isHidden];
}

//
// ------------------------- C++ Trampolines -------------------------
//
Expand All @@ -100,4 +117,14 @@ - (bool) activateProcess:(pid_t) pid
return [static_cast<id>(self) activateProcess:pid];
}

bool AppKit::hideProcess(pid_t pid)
{
return [static_cast<id>(self) hideProcess:pid];
}

bool AppKit::isHidden(pid_t pid)
{
return [static_cast<id>(self) isHidden:pid];
}

@end
10 changes: 10 additions & 0 deletions src/gui/macutils/MacUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,13 @@ bool MacUtils::raiseLastActiveWindow()
{
return m_appkit->activateProcess(m_appkit->lastActiveProcessId());
}

bool MacUtils::hideOwnWindow()
{
return m_appkit->hideProcess(m_appkit->ownProcessId());
}

bool MacUtils::isHidden()
{
return m_appkit->isHidden(m_appkit->ownProcessId());
}
2 changes: 2 additions & 0 deletions src/gui/macutils/MacUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class MacUtils : public QObject
bool raiseWindow(WId pid);
bool raiseLastActiveWindow();
bool raiseOwnWindow();
bool hideOwnWindow();
bool isHidden();

private:
explicit MacUtils(QObject* parent = nullptr);
Expand Down