Change isInFocus() check to be OS-agnostic

isInFocus() can utilize the platform-dependent GetCurrentWindowTitle() instead of re-implementing the same functionality.

Tested on Linux and macOS without issues
This commit is contained in:
Myned 2020-05-18 00:10:23 -04:00
parent ac051a42a8
commit a4de8c0c59
No known key found for this signature in database
GPG Key ID: 24318A323F309244
2 changed files with 19 additions and 29 deletions

View File

@ -126,21 +126,15 @@ void GetProcessList(QStringList& list)
}
}
bool isInFocus(QString const& appQName)
bool isInFocus(const QString &exeToCheck)
{
QByteArray ba = appQName.toLocal8Bit();
const char * appName = ba.data();
@autoreleasepool {
NSWorkspace *ws = [NSWorkspace sharedWorkspace];
NSRunningApplication *app = [ws frontmostApplication];
if (app) {
NSString *name = app.localizedName;
if (!name)
return false;
const char *str = name.UTF8String;
return (str && *str && strcmp(appName,str) == 0 )? true : false;
}
}
return false;
string curWindow;
GetCurrentWindowTitle(curWindow);
// True if executable switch equals current window
bool equals = (exeToCheck.toStdString() == curWindow);
// True if executable switch matches current window
bool matches = QString::fromStdString(curWindow).contains(QRegularExpression(exeToCheck));
return (equals || matches);
}

View File

@ -131,21 +131,17 @@ void GetProcessList(QStringList &processes) {
CloseHandle(procSnapshot);
}
bool isInFocus(const QString &exeToCheck) {
// only checks if the current foreground window is from the same executable,
// may return true for incorrent not meant windows from a program
HWND foregroundWindow = GetForegroundWindow();
DWORD processId = 0;
GetWindowThreadProcessId(foregroundWindow, &processId);
bool isInFocus(const QString &exeToCheck)
{
string curWindow;
GetCurrentWindowTitle(curWindow);
HANDLE process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processId);
if (process == NULL) return false;
// True if executable switch equals current window
bool equals = (exeToCheck.toStdString() == curWindow);
// True if executable switch matches current window
bool matches = QString::fromStdString(curWindow).contains(QRegularExpression(exeToCheck));
WCHAR executablePath[600];
GetModuleFileNameEx(process, 0, executablePath, 600);
CloseHandle(process);
return exeToCheck == QString::fromWCharArray(executablePath).split(QRegExp("(/|\\\\)")).back();
return (equals || matches);
}
int getLastInputTime()