disable apps for shortcut guide (#10123)

This commit is contained in:
Davide Giacometti
2021-04-02 16:29:48 +02:00
committed by GitHub
parent 6561290bde
commit bc28a3f5e3
6 changed files with 152 additions and 0 deletions

View File

@@ -14,8 +14,10 @@
#include <common/logger/logger.h>
#include <common/utils/process_path.h>
#include <common/utils/resources.h>
#include <common/utils/string_utils.h>
#include <common/utils/winapi_error.h>
#include <common/utils/window.h>
#include <Psapi.h>
// TODO: refactor singleton
OverlayWindow* instance = nullptr;
@@ -41,6 +43,7 @@ namespace
{
HWND hwnd = nullptr; // Handle to the top-level foreground window or nullptr if there is no such window
bool snappable = false; // True, if the window can react to Windows Snap keys
bool disabled = false;
};
ShortcutGuideWindowInfo GetShortcutGuideWindowInfo()
@@ -52,6 +55,18 @@ namespace
{
return result;
}
WCHAR exePath[MAX_PATH] = L"";
instance->get_exe_path(active_window, exePath);
if (wcslen(exePath) > 0)
{
result.disabled = instance->is_disabled_app(exePath);
if (result.disabled)
{
return result;
}
}
auto style = GetWindowLong(active_window, GWL_STYLE);
auto exStyle = GetWindowLong(active_window, GWL_EXSTYLE);
if ((style & WS_CHILD) == WS_CHILD ||
@@ -193,6 +208,11 @@ void OverlayWindow::set_config(const wchar_t* config)
winkey_popup->set_theme(theme.value);
}
}
if (auto val = _values.get_string_value(disabledApps.name))
{
disabledApps.value = std::move(*val);
update_disabled_apps();
}
}
}
catch (...)
@@ -343,6 +363,11 @@ intptr_t OverlayWindow::signal_event(LowlevelKeyboardEvent* event)
void OverlayWindow::on_held()
{
auto windowInfo = GetShortcutGuideWindowInfo();
if (windowInfo.disabled)
{
target_state->was_hidden();
return;
}
winkey_popup->show(windowInfo.hwnd, windowInfo.snappable);
}
@@ -391,9 +416,72 @@ void OverlayWindow::init_settings()
{
theme.value = std::move(*val);
}
if (auto val = settings.get_string_value(disabledApps.name))
{
disabledApps.value = std::move(*val);
update_disabled_apps();
}
}
catch (std::exception&)
{
// Error while loading from the settings file. Just let default values stay as they are.
}
}
bool OverlayWindow::is_disabled_app(wchar_t* exePath)
{
if (exePath == nullptr)
{
return false;
}
auto exePathUpper = std::wstring(exePath);
CharUpperBuffW(exePathUpper.data(), (DWORD)exePathUpper.length());
for (const auto& row : disabled_apps_array)
{
const auto pos = exePathUpper.rfind(row);
const auto last_slash = exePathUpper.rfind('\\');
// Check that row occurs in disabled_apps_array, and its last occurrence contains in itself the first character after the last backslash.
if (pos != std::wstring::npos && pos <= last_slash + 1 && pos + row.length() > last_slash)
{
return true;
}
}
return false;
}
void OverlayWindow::update_disabled_apps()
{
disabled_apps_array.clear();
auto disabledUppercase = disabledApps.value;
CharUpperBuffW(disabledUppercase.data(), (DWORD)disabledUppercase.length());
std::wstring_view view(disabledUppercase);
view = trim(view);
while (!view.empty())
{
auto pos = (std::min)(view.find_first_of(L"\r\n"), view.length());
disabled_apps_array.emplace_back(view.substr(0, pos));
view.remove_prefix(pos);
view = trim(view);
}
}
void OverlayWindow::get_exe_path(HWND window, wchar_t* path)
{
if (disabled_apps_array.empty())
{
return;
}
DWORD pid = 0;
GetWindowThreadProcessId(window, &pid);
if (pid != 0)
{
HANDLE processHandle = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
if (processHandle && GetProcessImageFileName(processHandle, path, MAX_PATH) > 0)
{
CloseHandle(processHandle);
}
}
}