mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2026-03-11 08:54:48 +00:00
Introduce macUtils()->isDarkMode() function to detect Mojave dark mode and correct reference entry text color. Fixes #860.
138 lines
3.7 KiB
Text
138 lines
3.7 KiB
Text
/*
|
|
* Copyright (C) 2016 Lennart Glauer <mail@lennart-glauer.de>
|
|
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 2 or (at your option)
|
|
* version 3 of the License.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#import "AppKitImpl.h"
|
|
|
|
#import <AppKit/NSWorkspace.h>
|
|
|
|
@implementation AppKitImpl
|
|
|
|
AppKit::AppKit()
|
|
{
|
|
self = [[AppKitImpl alloc] init];
|
|
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:static_cast<id>(self)
|
|
selector:@selector(didDeactivateApplicationObserver:)
|
|
name:NSWorkspaceDidDeactivateApplicationNotification
|
|
object:nil];
|
|
}
|
|
|
|
AppKit::~AppKit()
|
|
{
|
|
[[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:static_cast<id>(self)];
|
|
[static_cast<id>(self) dealloc];
|
|
}
|
|
|
|
//
|
|
// Update last active application property
|
|
//
|
|
- (void) didDeactivateApplicationObserver:(NSNotification *) notification
|
|
{
|
|
NSDictionary *userInfo = notification.userInfo;
|
|
NSRunningApplication *app = userInfo[NSWorkspaceApplicationKey];
|
|
|
|
if (app.processIdentifier != [self ownProcessId]) {
|
|
self.lastActiveApplication = app;
|
|
}
|
|
}
|
|
|
|
//
|
|
// Get process id of frontmost application (-> keyboard input)
|
|
//
|
|
- (pid_t) activeProcessId
|
|
{
|
|
return [NSWorkspace sharedWorkspace].frontmostApplication.processIdentifier;
|
|
}
|
|
|
|
//
|
|
// Get process id of own process
|
|
//
|
|
- (pid_t) ownProcessId
|
|
{
|
|
return [NSProcessInfo processInfo].processIdentifier;
|
|
}
|
|
|
|
//
|
|
// Activate application by process id
|
|
//
|
|
- (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 -------------------------
|
|
//
|
|
|
|
pid_t AppKit::lastActiveProcessId()
|
|
{
|
|
return [static_cast<id>(self) lastActiveApplication].processIdentifier;
|
|
}
|
|
|
|
pid_t AppKit::activeProcessId()
|
|
{
|
|
return [static_cast<id>(self) activeProcessId];
|
|
}
|
|
|
|
pid_t AppKit::ownProcessId()
|
|
{
|
|
return [static_cast<id>(self) ownProcessId];
|
|
}
|
|
|
|
bool AppKit::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];
|
|
}
|
|
|
|
bool AppKit::isDarkMode()
|
|
{
|
|
NSDictionary *dict = [[NSUserDefaults standardUserDefaults] persistentDomainForName:NSGlobalDomain];
|
|
id style = [dict objectForKey:@"AppleInterfaceStyle"];
|
|
return ( style && [style isKindOfClass:[NSString class]]
|
|
&& NSOrderedSame == [style caseInsensitiveCompare:@"dark"] );
|
|
}
|
|
|
|
@end
|