Skip to content

Commit

Permalink
Fix native hiding of macOS windows
Browse files Browse the repository at this point in the history
  • Loading branch information
varjolintu committed Jan 6, 2019
1 parent dd5665c commit 74be406
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 3 deletions.
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

0 comments on commit 74be406

Please sign in to comment.