Format shortcut_guide according to .clang-format

This commit is contained in:
yuyoyuppe 2019-12-06 14:07:54 +03:00 committed by yuyoyuppe
parent 946e74a918
commit f22a30ca87
10 changed files with 1395 additions and 1171 deletions

View File

@ -5,26 +5,32 @@
#include "overlay_window.h"
#include "trace.h"
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
switch (ul_reason_for_call) {
case DLL_PROCESS_ATTACH:
Trace::RegisterProvider();
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
Trace::UnregisterProvider();
break;
}
return TRUE;
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
Trace::RegisterProvider();
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
Trace::UnregisterProvider();
break;
}
return TRUE;
}
extern "C" __declspec(dllexport) PowertoyModuleIface* __cdecl powertoy_create() {
if (!instance) {
instance = new OverlayWindow();
return instance;
} else {
return nullptr;
}
extern "C" __declspec(dllexport) PowertoyModuleIface* __cdecl powertoy_create()
{
if (!instance)
{
instance = new OverlayWindow();
return instance;
}
else
{
return nullptr;
}
}

View File

@ -1,22 +1,25 @@
#include "pch.h"
#include "keyboard_state.h"
bool winkey_held() {
auto left = GetAsyncKeyState(VK_LWIN);
auto right = GetAsyncKeyState(VK_RWIN);
return (left & 0x8000) || (right & 0x8000);
bool winkey_held()
{
auto left = GetAsyncKeyState(VK_LWIN);
auto right = GetAsyncKeyState(VK_RWIN);
return (left & 0x8000) || (right & 0x8000);
}
bool only_winkey_key_held() {
/* There are situations, when some of the keys are not registered correctly by
bool only_winkey_key_held()
{
/* There are situations, when some of the keys are not registered correctly by
GetKeyboardState. The M key can get stuck as "pressed" after Win+M, and
Shift etc. keys are not always reported as expected.
*/
for (int vk = 0; vk <= VK_OEM_CLEAR; ++vk) {
if (vk == VK_LWIN || vk == VK_RWIN)
continue;
if (GetAsyncKeyState(vk) & 0x8000)
return false;
}
return true;
for (int vk = 0; vk <= VK_OEM_CLEAR; ++vk)
{
if (vk == VK_LWIN || vk == VK_RWIN)
continue;
if (GetAsyncKeyState(vk) & 0x8000)
return false;
}
return true;
}

File diff suppressed because it is too large Load Diff

View File

@ -7,85 +7,94 @@
#include "common/windows_colors.h"
#include "common/tasklist_positions.h"
struct ScaleResult {
double scale;
RECT rect;
struct ScaleResult
{
double scale;
RECT rect;
};
class D2DOverlaySVG : public D2DSVG {
class D2DOverlaySVG : public D2DSVG
{
public:
D2DOverlaySVG& load(const std::wstring& filename, ID2D1DeviceContext5* d2d_dc);
D2DOverlaySVG& resize(int x, int y, int width, int height, float fill, float max_scale = -1.0f);
D2DOverlaySVG& find_thumbnail(const std::wstring& id);
D2DOverlaySVG& find_window_group(const std::wstring& id);
ScaleResult get_thumbnail_rect_and_scale(int x_offset, int y_offset, int window_cx, int window_cy, float fill);
D2DOverlaySVG& toggle_window_group(bool active);
winrt::com_ptr<ID2D1SvgElement> find_element(const std::wstring& id);
D2D1_RECT_F get_maximize_label() const;
D2D1_RECT_F get_minimize_label() const;
D2D1_RECT_F get_snap_left() const;
D2D1_RECT_F get_snap_right() const;
D2DOverlaySVG& load(const std::wstring& filename, ID2D1DeviceContext5* d2d_dc);
D2DOverlaySVG& resize(int x, int y, int width, int height, float fill, float max_scale = -1.0f);
D2DOverlaySVG& find_thumbnail(const std::wstring& id);
D2DOverlaySVG& find_window_group(const std::wstring& id);
ScaleResult get_thumbnail_rect_and_scale(int x_offset, int y_offset, int window_cx, int window_cy, float fill);
D2DOverlaySVG& toggle_window_group(bool active);
winrt::com_ptr<ID2D1SvgElement> find_element(const std::wstring& id);
D2D1_RECT_F get_maximize_label() const;
D2D1_RECT_F get_minimize_label() const;
D2D1_RECT_F get_snap_left() const;
D2D1_RECT_F get_snap_right() const;
private:
D2D1_POINT_2F thumbnail_top_left = {};
D2D1_POINT_2F thumbnail_bottom_right = {};
RECT thumbnail_scaled_rect = {};
winrt::com_ptr<ID2D1SvgElement> window_group;
D2D1_POINT_2F thumbnail_top_left = {};
D2D1_POINT_2F thumbnail_bottom_right = {};
RECT thumbnail_scaled_rect = {};
winrt::com_ptr<ID2D1SvgElement> window_group;
};
struct AnimateKeys {
Animation animation;
D2D1_COLOR_F original;
winrt::com_ptr<ID2D1SvgElement> button;
int vk_code;
struct AnimateKeys
{
Animation animation;
D2D1_COLOR_F original;
winrt::com_ptr<ID2D1SvgElement> button;
int vk_code;
};
class D2DOverlayWindow : public D2DWindow {
class D2DOverlayWindow : public D2DWindow
{
public:
D2DOverlayWindow();
void show(HWND active_window);
void animate(int vk_code);
~D2DOverlayWindow();
void apply_overlay_opacity(float opacity);
void set_theme(const std::wstring& theme);
void quick_hide();
D2DOverlayWindow();
void show(HWND active_window);
void animate(int vk_code);
~D2DOverlayWindow();
void apply_overlay_opacity(float opacity);
void set_theme(const std::wstring& theme);
void quick_hide();
private:
void animate(int vk_code, int offset);
bool show_thumbnail(const RECT& rect, double alpha);
void hide_thumbnail();
virtual void init() override;
virtual void resize() override;
virtual void render(ID2D1DeviceContext5* d2d_dc) override;
virtual void on_show() override;
virtual void on_hide() override;
float get_overlay_opacity();
void animate(int vk_code, int offset);
bool show_thumbnail(const RECT& rect, double alpha);
void hide_thumbnail();
virtual void init() override;
virtual void resize() override;
virtual void render(ID2D1DeviceContext5* d2d_dc) override;
virtual void on_show() override;
virtual void on_hide() override;
float get_overlay_opacity();
bool running = true;
std::vector<AnimateKeys> key_animations;
std::vector<int> key_pressed;
std::vector<MonitorInfo> monitors;
ScreenSize total_screen;
int monitor_dx = 0, monitor_dy = 0;
D2DText text;
WindowsColors colors;
Animation animation;
RECT window_rect = {};
Tasklist tasklist;
std::vector<TasklistButton> tasklist_buttons;
std::thread tasklist_thread;
bool tasklist_update = false;
std::mutex tasklist_cv_mutex;
std::condition_variable tasklist_cv;
bool running = true;
std::vector<AnimateKeys> key_animations;
std::vector<int> key_pressed;
std::vector<MonitorInfo> monitors;
ScreenSize total_screen;
int monitor_dx = 0, monitor_dy = 0;
D2DText text;
WindowsColors colors;
Animation animation;
RECT window_rect = {};
Tasklist tasklist;
std::vector<TasklistButton> tasklist_buttons;
std::thread tasklist_thread;
bool tasklist_update = false;
std::mutex tasklist_cv_mutex;
std::condition_variable tasklist_cv;
HTHUMBNAIL thumbnail;
HWND active_window = nullptr;
D2DOverlaySVG landscape, portrait;
D2DOverlaySVG* use_overlay = nullptr;
D2DSVG no_active;
std::vector<D2DSVG> arrows;
std::chrono::steady_clock::time_point shown_start_time;
float overlay_opacity = 0.9f;
enum {
Light, Dark, System
} theme_setting = System;
bool light_mode = true;
HTHUMBNAIL thumbnail;
HWND active_window = nullptr;
D2DOverlaySVG landscape, portrait;
D2DOverlaySVG* use_overlay = nullptr;
D2DSVG no_active;
std::vector<D2DSVG> arrows;
std::chrono::steady_clock::time_point shown_start_time;
float overlay_opacity = 0.9f;
enum
{
Light,
Dark,
System
} theme_setting = System;
bool light_mode = true;
};

View File

@ -8,169 +8,199 @@ extern "C" IMAGE_DOS_HEADER __ImageBase;
OverlayWindow* instance = nullptr;
OverlayWindow::OverlayWindow() {
init_settings();
OverlayWindow::OverlayWindow()
{
init_settings();
}
const wchar_t * OverlayWindow::get_name() {
return L"Shortcut Guide";
const wchar_t* OverlayWindow::get_name()
{
return L"Shortcut Guide";
}
const wchar_t ** OverlayWindow::get_events() {
static const wchar_t* events[2] = { ll_keyboard, 0 };
return events;
const wchar_t** OverlayWindow::get_events()
{
static const wchar_t* events[2] = { ll_keyboard, 0 };
return events;
}
bool OverlayWindow::get_config(wchar_t* buffer, int *buffer_size) {
HINSTANCE hinstance = reinterpret_cast<HINSTANCE>(&__ImageBase);
bool OverlayWindow::get_config(wchar_t* buffer, int* buffer_size)
{
HINSTANCE hinstance = reinterpret_cast<HINSTANCE>(&__ImageBase);
PowerToysSettings::Settings settings(hinstance, get_name());
settings.set_description(L"Shows a help overlay with Windows shortcuts when the Windows key is pressed.");
settings.set_overview_link(L"https://github.com/microsoft/PowerToys/blob/master/src/modules/shortcut_guide/README.md");
settings.set_icon_key(L"pt-shortcut-guide");
PowerToysSettings::Settings settings(hinstance, get_name());
settings.set_description(L"Shows a help overlay with Windows shortcuts when the Windows key is pressed.");
settings.set_overview_link(L"https://github.com/microsoft/PowerToys/blob/master/src/modules/shortcut_guide/README.md");
settings.set_icon_key(L"pt-shortcut-guide");
settings.add_int_spinner(
pressTime.name,
pressTime.resourceId,
pressTime.value,
100,
10000,
100
);
settings.add_int_spinner(
pressTime.name,
pressTime.resourceId,
pressTime.value,
100,
10000,
100);
settings.add_int_spinner(
overlayOpacity.name,
overlayOpacity.resourceId,
overlayOpacity.value,
0,
100,
1
);
settings.add_int_spinner(
overlayOpacity.name,
overlayOpacity.resourceId,
overlayOpacity.value,
0,
100,
1);
settings.add_choice_group(
theme.name,
theme.resourceId,
theme.value,
theme.keys_and_texts
);
settings.add_choice_group(
theme.name,
theme.resourceId,
theme.value,
theme.keys_and_texts);
return settings.serialize_to_buffer(buffer, buffer_size);
return settings.serialize_to_buffer(buffer, buffer_size);
}
void OverlayWindow::set_config(const wchar_t * config) {
try {
PowerToysSettings::PowerToyValues _values =
PowerToysSettings::PowerToyValues::from_json_string(config);
if (const auto press_delay_time = _values.get_int_value(pressTime.name)) {
pressTime.value = *press_delay_time;
if (target_state) {
target_state->set_delay(*press_delay_time);
}
void OverlayWindow::set_config(const wchar_t* config)
{
try
{
PowerToysSettings::PowerToyValues _values =
PowerToysSettings::PowerToyValues::from_json_string(config);
if (const auto press_delay_time = _values.get_int_value(pressTime.name))
{
pressTime.value = *press_delay_time;
if (target_state)
{
target_state->set_delay(*press_delay_time);
}
}
if (const auto overlay_opacity = _values.get_int_value(overlayOpacity.name))
{
overlayOpacity.value = *overlay_opacity;
if (winkey_popup)
{
winkey_popup->apply_overlay_opacity(((float)overlayOpacity.value) / 100.0f);
}
}
if (auto val = _values.get_string_value(theme.name))
{
theme.value = std::move(*val);
winkey_popup->set_theme(theme.value);
}
_values.save_to_settings_file();
Trace::SettingsChanged(pressTime.value, overlayOpacity.value, theme.value);
}
if (const auto overlay_opacity = _values.get_int_value(overlayOpacity.name)) {
overlayOpacity.value = *overlay_opacity;
if (winkey_popup) {
catch (...)
{
// Improper JSON. TODO: handle the error.
}
}
void OverlayWindow::enable()
{
if (!_enabled)
{
Trace::EnableShortcutGuide(true);
winkey_popup = std::make_unique<D2DOverlayWindow>();
winkey_popup->apply_overlay_opacity(((float)overlayOpacity.value) / 100.0f);
}
winkey_popup->set_theme(theme.value);
target_state = std::make_unique<TargetState>(pressTime.value);
winkey_popup->initialize();
}
if (auto val = _values.get_string_value(theme.name)) {
theme.value = std::move(*val);
winkey_popup->set_theme(theme.value);
_enabled = true;
}
void OverlayWindow::disable(bool trace_event)
{
if (_enabled)
{
_enabled = false;
if (trace_event)
{
Trace::EnableShortcutGuide(false);
}
winkey_popup->hide();
target_state->exit();
target_state.reset();
winkey_popup.reset();
}
_values.save_to_settings_file();
Trace::SettingsChanged(pressTime.value, overlayOpacity.value, theme.value);
}
catch (...) {
// Improper JSON. TODO: handle the error.
}
}
void OverlayWindow::enable() {
if (!_enabled) {
Trace::EnableShortcutGuide(true);
winkey_popup = std::make_unique<D2DOverlayWindow>();
winkey_popup->apply_overlay_opacity(((float)overlayOpacity.value)/100.0f);
winkey_popup->set_theme(theme.value);
target_state = std::make_unique<TargetState>(pressTime.value);
winkey_popup->initialize();
}
_enabled = true;
void OverlayWindow::disable()
{
this->disable(true);
}
void OverlayWindow::disable(bool trace_event) {
if (_enabled) {
_enabled = false;
if (trace_event) {
Trace::EnableShortcutGuide(false);
bool OverlayWindow::is_enabled()
{
return _enabled;
}
intptr_t OverlayWindow::signal_event(const wchar_t* name, intptr_t data)
{
if (_enabled && wcscmp(name, ll_keyboard) == 0)
{
auto& event = *(reinterpret_cast<LowlevelKeyboardEvent*>(data));
if (event.wParam == WM_KEYDOWN ||
event.wParam == WM_SYSKEYDOWN ||
event.wParam == WM_KEYUP ||
event.wParam == WM_SYSKEYUP)
{
bool supress = target_state->signal_event(event.lParam->vkCode,
event.wParam == WM_KEYDOWN || event.wParam == WM_SYSKEYDOWN);
return supress ? 1 : 0;
}
}
winkey_popup->hide();
target_state->exit();
target_state.reset();
winkey_popup.reset();
}
return 0;
}
void OverlayWindow::disable() {
this->disable(true);
void OverlayWindow::on_held()
{
auto active_window = get_filtered_active_window();
winkey_popup->show(active_window);
}
bool OverlayWindow::is_enabled() {
return _enabled;
void OverlayWindow::on_held_press(DWORD vkCode)
{
winkey_popup->animate(vkCode);
}
intptr_t OverlayWindow::signal_event(const wchar_t * name, intptr_t data) {
if (_enabled && wcscmp(name, ll_keyboard) == 0) {
auto& event = *(reinterpret_cast<LowlevelKeyboardEvent*>(data));
if (event.wParam == WM_KEYDOWN ||
event.wParam == WM_SYSKEYDOWN ||
event.wParam == WM_KEYUP ||
event.wParam == WM_SYSKEYUP) {
bool supress = target_state->signal_event(event.lParam->vkCode,
event.wParam == WM_KEYDOWN || event.wParam == WM_SYSKEYDOWN);
return supress ? 1 : 0;
void OverlayWindow::quick_hide()
{
winkey_popup->quick_hide();
}
void OverlayWindow::was_hidden()
{
target_state->was_hiden();
}
void OverlayWindow::destroy()
{
this->disable(false);
delete this;
instance = nullptr;
}
void OverlayWindow::init_settings()
{
try
{
PowerToysSettings::PowerToyValues settings =
PowerToysSettings::PowerToyValues::load_from_settings_file(OverlayWindow::get_name());
if (const auto val = settings.get_int_value(pressTime.name))
{
pressTime.value = *val;
}
if (const auto val = settings.get_int_value(overlayOpacity.name))
{
overlayOpacity.value = *val;
}
if (auto val = settings.get_string_value(theme.name))
{
theme.value = std::move(*val);
}
}
}
return 0;
}
void OverlayWindow::on_held() {
auto active_window = get_filtered_active_window();
winkey_popup->show(active_window);
}
void OverlayWindow::on_held_press(DWORD vkCode) {
winkey_popup->animate(vkCode);
}
void OverlayWindow::quick_hide() {
winkey_popup->quick_hide();
}
void OverlayWindow::was_hidden() {
target_state->was_hiden();
}
void OverlayWindow::destroy() {
this->disable(false);
delete this;
instance = nullptr;
}
void OverlayWindow::init_settings() {
try {
PowerToysSettings::PowerToyValues settings =
PowerToysSettings::PowerToyValues::load_from_settings_file(OverlayWindow::get_name());
if (const auto val = settings.get_int_value(pressTime.name)) {
pressTime.value = *val;
catch (std::exception&)
{
// Error while loading from the settings file. Just let default values stay as they are.
}
if (const auto val = settings.get_int_value(overlayOpacity.name)) {
overlayOpacity.value = *val;
}
if (auto val = settings.get_string_value(theme.name)) {
theme.value = std::move(*val);
}
}
catch (std::exception&) {
// Error while loading from the settings file. Just let default values stay as they are.
}
}

View File

@ -8,57 +8,61 @@ extern class OverlayWindow* instance;
class TargetState;
class OverlayWindow : public PowertoyModuleIface {
class OverlayWindow : public PowertoyModuleIface
{
public:
OverlayWindow();
virtual const wchar_t* get_name() override;
virtual const wchar_t** get_events() override;
virtual bool get_config(wchar_t* buffer, int *buffer_size) override;
OverlayWindow();
virtual const wchar_t* get_name() override;
virtual const wchar_t** get_events() override;
virtual bool get_config(wchar_t* buffer, int* buffer_size) override;
virtual void set_config(const wchar_t* config) override;
virtual void enable() override;
virtual void disable() override;
virtual bool is_enabled() override;
virtual intptr_t signal_event(const wchar_t* name, intptr_t data) override;
virtual void set_config(const wchar_t* config) override;
virtual void enable() override;
virtual void disable() override;
virtual bool is_enabled() override;
virtual intptr_t signal_event(const wchar_t* name, intptr_t data) override;
virtual void register_system_menu_helper(PowertoySystemMenuIface* helper) override { }
virtual void signal_system_menu_action(const wchar_t* name) override { }
virtual void register_system_menu_helper(PowertoySystemMenuIface* helper) override {}
virtual void signal_system_menu_action(const wchar_t* name) override {}
void on_held();
void on_held_press(DWORD vkCode);
void quick_hide();
void was_hidden();
void on_held();
void on_held_press(DWORD vkCode);
void quick_hide();
void was_hidden();
virtual void destroy() override;
virtual void destroy() override;
private:
std::unique_ptr<TargetState> target_state;
std::unique_ptr<D2DOverlayWindow> winkey_popup;
bool _enabled = false;
std::unique_ptr<TargetState> target_state;
std::unique_ptr<D2DOverlayWindow> winkey_popup;
bool _enabled = false;
void init_settings();
void disable(bool trace_event);
void init_settings();
void disable(bool trace_event);
struct PressTime {
PCWSTR name = L"press_time";
int value = 900; // ms
int resourceId = IDS_SETTING_DESCRIPTION_PRESS_TIME;
} pressTime;
struct PressTime
{
PCWSTR name = L"press_time";
int value = 900; // ms
int resourceId = IDS_SETTING_DESCRIPTION_PRESS_TIME;
} pressTime;
struct OverlayOpacity {
PCWSTR name = L"overlay_opacity";
int value = 90; // percent
int resourceId = IDS_SETTING_DESCRIPTION_OVERLAY_OPACITY;
} overlayOpacity;
struct OverlayOpacity
{
PCWSTR name = L"overlay_opacity";
int value = 90; // percent
int resourceId = IDS_SETTING_DESCRIPTION_OVERLAY_OPACITY;
} overlayOpacity;
struct Theme {
PCWSTR name = L"theme";
std::wstring value = L"system";
int resourceId = IDS_SETTING_DESCRIPTION_THEME;
std::vector<std::pair<std::wstring, UINT>> keys_and_texts = {
{ L"system", IDS_SETTING_DESCRIPTION_THEME_SYSTEM },
{ L"light", IDS_SETTING_DESCRIPTION_THEME_LIGHT },
{ L"dark", IDS_SETTING_DESCRIPTION_THEME_DARK }
};
} theme;
struct Theme
{
PCWSTR name = L"theme";
std::wstring value = L"system";
int resourceId = IDS_SETTING_DESCRIPTION_THEME;
std::vector<std::pair<std::wstring, UINT>> keys_and_texts = {
{ L"system", IDS_SETTING_DESCRIPTION_THEME_SYSTEM },
{ L"light", IDS_SETTING_DESCRIPTION_THEME_LIGHT },
{ L"dark", IDS_SETTING_DESCRIPTION_THEME_DARK }
};
} theme;
};

View File

@ -3,155 +3,180 @@
#include "common/start_visible.h"
#include "keyboard_state.h"
TargetState::TargetState(int ms_delay) : delay(std::chrono::milliseconds(ms_delay)), thread(&TargetState::thread_proc, this)
{ }
bool TargetState::signal_event(unsigned vk_code, bool key_down) {
std::unique_lock lock(mutex);
if (!events.empty() && events.back().key_down == key_down && events.back().vk_code == vk_code) {
return false;
}
// Hide the overlay when WinKey + Shift + S is pressed. 0x53 is the VK code of the S key
if (key_down && state == Shown && vk_code == 0x53 && (GetKeyState(VK_LSHIFT) || GetKeyState(VK_RSHIFT))) {
// We cannot use normal hide() here, there is stuff that needs deinitialization.
// It can be safely done when the user releases the WinKey.
instance->quick_hide();
}
bool supress = false;
if (!key_down && (vk_code == VK_LWIN || vk_code == VK_RWIN) &&
state == Shown &&
std::chrono::system_clock::now() - singnal_timestamp > std::chrono::milliseconds(300) &&
!key_was_pressed) {
supress = true;
}
events.push_back({ key_down, vk_code });
lock.unlock();
cv.notify_one();
if (supress) {
// Send a fake key-stroke to prevent the start menu from appearing.
// We use 0xCF VK code, which is reserved. It still prevents the
// start menu from appearing, but should not interfere with any
// keyboard shortcuts.
INPUT input[3] = { {},{},{} };
input[0].type = INPUT_KEYBOARD;
input[0].ki.wVk = 0xCF;
input[1].type = INPUT_KEYBOARD;
input[1].ki.wVk = 0xCF;
input[1].ki.dwFlags = KEYEVENTF_KEYUP;
input[2].type = INPUT_KEYBOARD;
input[2].ki.wVk = VK_LWIN;
input[2].ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(3, input, sizeof(INPUT));
}
return supress;
TargetState::TargetState(int ms_delay) :
delay(std::chrono::milliseconds(ms_delay)), thread(&TargetState::thread_proc, this)
{
}
void TargetState::was_hiden() {
std::unique_lock<std::mutex> lock(mutex);
state = Hidden;
events.clear();
lock.unlock();
cv.notify_one();
}
void TargetState::exit() {
std::unique_lock lock(mutex);
events.clear();
state = Exiting;
lock.unlock();
cv.notify_one();
thread.join();
}
KeyEvent TargetState::next() {
auto e = events.front();
events.pop_front();
return e;
}
void TargetState::handle_hidden() {
std::unique_lock lock(mutex);
if (events.empty())
cv.wait(lock);
if (events.empty() || state == Exiting)
return;
auto event = next();
if (event.key_down && (event.vk_code == VK_LWIN || event.vk_code == VK_RWIN)) {
state = Timeout;
winkey_timestamp = std::chrono::system_clock::now();
}
}
void TargetState::handle_shown() {
std::unique_lock lock(mutex);
if (events.empty()) {
cv.wait(lock);
}
if (events.empty() || state == Exiting) {
return;
}
auto event = next();
if (event.key_down && (event.vk_code == VK_LWIN || event.vk_code == VK_RWIN)) {
return;
}
if (!event.key_down && (event.vk_code == VK_LWIN || event.vk_code == VK_RWIN) || !winkey_held()) {
state = Hidden;
lock.unlock();
return;
}
if (event.key_down) {
key_was_pressed = true;
lock.unlock();
instance->on_held_press(event.vk_code);
}
}
void TargetState::thread_proc() {
while (true) {
switch (state) {
case Hidden:
handle_hidden();
break;
case Timeout:
handle_timeout();
break;
case Shown:
handle_shown();
break;
case Exiting:
default:
return;
bool TargetState::signal_event(unsigned vk_code, bool key_down)
{
std::unique_lock lock(mutex);
if (!events.empty() && events.back().key_down == key_down && events.back().vk_code == vk_code)
{
return false;
}
}
// Hide the overlay when WinKey + Shift + S is pressed. 0x53 is the VK code of the S key
if (key_down && state == Shown && vk_code == 0x53 && (GetKeyState(VK_LSHIFT) || GetKeyState(VK_RSHIFT)))
{
// We cannot use normal hide() here, there is stuff that needs deinitialization.
// It can be safely done when the user releases the WinKey.
instance->quick_hide();
}
bool supress = false;
if (!key_down && (vk_code == VK_LWIN || vk_code == VK_RWIN) &&
state == Shown &&
std::chrono::system_clock::now() - singnal_timestamp > std::chrono::milliseconds(300) &&
!key_was_pressed)
{
supress = true;
}
events.push_back({ key_down, vk_code });
lock.unlock();
cv.notify_one();
if (supress)
{
// Send a fake key-stroke to prevent the start menu from appearing.
// We use 0xCF VK code, which is reserved. It still prevents the
// start menu from appearing, but should not interfere with any
// keyboard shortcuts.
INPUT input[3] = { {}, {}, {} };
input[0].type = INPUT_KEYBOARD;
input[0].ki.wVk = 0xCF;
input[1].type = INPUT_KEYBOARD;
input[1].ki.wVk = 0xCF;
input[1].ki.dwFlags = KEYEVENTF_KEYUP;
input[2].type = INPUT_KEYBOARD;
input[2].ki.wVk = VK_LWIN;
input[2].ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(3, input, sizeof(INPUT));
}
return supress;
}
void TargetState::handle_timeout() {
std::unique_lock lock(mutex);
auto wait_time = delay - (std::chrono::system_clock::now() - winkey_timestamp);
if (events.empty())
cv.wait_for(lock, wait_time);
if (state == Exiting)
return;
while (!events.empty()) {
auto event = events.front();
if (event.key_down && (event.vk_code == VK_LWIN || event.vk_code == VK_RWIN))
events.pop_front();
else
break;
}
if (!events.empty() || !only_winkey_key_held() || is_start_visible()) {
void TargetState::was_hiden()
{
std::unique_lock<std::mutex> lock(mutex);
state = Hidden;
return;
}
if (std::chrono::system_clock::now() - winkey_timestamp < delay)
return;
singnal_timestamp = std::chrono::system_clock::now();
key_was_pressed = false;
state = Shown;
lock.unlock();
instance->on_held();
events.clear();
lock.unlock();
cv.notify_one();
}
void TargetState::set_delay(int ms_delay) {
delay = std::chrono::milliseconds(ms_delay);
void TargetState::exit()
{
std::unique_lock lock(mutex);
events.clear();
state = Exiting;
lock.unlock();
cv.notify_one();
thread.join();
}
KeyEvent TargetState::next()
{
auto e = events.front();
events.pop_front();
return e;
}
void TargetState::handle_hidden()
{
std::unique_lock lock(mutex);
if (events.empty())
cv.wait(lock);
if (events.empty() || state == Exiting)
return;
auto event = next();
if (event.key_down && (event.vk_code == VK_LWIN || event.vk_code == VK_RWIN))
{
state = Timeout;
winkey_timestamp = std::chrono::system_clock::now();
}
}
void TargetState::handle_shown()
{
std::unique_lock lock(mutex);
if (events.empty())
{
cv.wait(lock);
}
if (events.empty() || state == Exiting)
{
return;
}
auto event = next();
if (event.key_down && (event.vk_code == VK_LWIN || event.vk_code == VK_RWIN))
{
return;
}
if (!event.key_down && (event.vk_code == VK_LWIN || event.vk_code == VK_RWIN) || !winkey_held())
{
state = Hidden;
lock.unlock();
return;
}
if (event.key_down)
{
key_was_pressed = true;
lock.unlock();
instance->on_held_press(event.vk_code);
}
}
void TargetState::thread_proc()
{
while (true)
{
switch (state)
{
case Hidden:
handle_hidden();
break;
case Timeout:
handle_timeout();
break;
case Shown:
handle_shown();
break;
case Exiting:
default:
return;
}
}
}
void TargetState::handle_timeout()
{
std::unique_lock lock(mutex);
auto wait_time = delay - (std::chrono::system_clock::now() - winkey_timestamp);
if (events.empty())
cv.wait_for(lock, wait_time);
if (state == Exiting)
return;
while (!events.empty())
{
auto event = events.front();
if (event.key_down && (event.vk_code == VK_LWIN || event.vk_code == VK_RWIN))
events.pop_front();
else
break;
}
if (!events.empty() || !only_winkey_key_held() || is_start_visible())
{
state = Hidden;
return;
}
if (std::chrono::system_clock::now() - winkey_timestamp < delay)
return;
singnal_timestamp = std::chrono::system_clock::now();
key_was_pressed = false;
state = Shown;
lock.unlock();
instance->on_held();
}
void TargetState::set_delay(int ms_delay)
{
delay = std::chrono::milliseconds(ms_delay);
}

View File

@ -6,30 +6,39 @@
#include <chrono>
#include "shortcut_guide.h"
struct KeyEvent {
bool key_down;
unsigned vk_code;
struct KeyEvent
{
bool key_down;
unsigned vk_code;
};
class TargetState {
class TargetState
{
public:
TargetState(int ms_delay);
bool signal_event(unsigned vk_code, bool key_down);
void was_hiden();
void exit();
void set_delay(int ms_delay);
TargetState(int ms_delay);
bool signal_event(unsigned vk_code, bool key_down);
void was_hiden();
void exit();
void set_delay(int ms_delay);
private:
KeyEvent next();
void handle_hidden();
void handle_timeout();
void handle_shown();
void thread_proc();
std::mutex mutex;
std::condition_variable cv;
std::chrono::system_clock::time_point winkey_timestamp, singnal_timestamp;
std::chrono::milliseconds delay;
std::deque<KeyEvent> events;
enum { Hidden, Timeout, Shown, Exiting } state = Hidden;
bool key_was_pressed = false;
std::thread thread;
KeyEvent next();
void handle_hidden();
void handle_timeout();
void handle_shown();
void thread_proc();
std::mutex mutex;
std::condition_variable cv;
std::chrono::system_clock::time_point winkey_timestamp, singnal_timestamp;
std::chrono::milliseconds delay;
std::deque<KeyEvent> events;
enum
{
Hidden,
Timeout,
Shown,
Exiting
} state = Hidden;
bool key_was_pressed = false;
std::thread thread;
};

View File

@ -2,59 +2,66 @@
#include "trace.h"
TRACELOGGING_DEFINE_PROVIDER(
g_hProvider,
"Microsoft.PowerToys",
// {38e8889b-9731-53f5-e901-e8a7c1753074}
(0x38e8889b, 0x9731, 0x53f5, 0xe9, 0x01, 0xe8, 0xa7, 0xc1, 0x75, 0x30, 0x74),
TraceLoggingOptionProjectTelemetry());
g_hProvider,
"Microsoft.PowerToys",
// {38e8889b-9731-53f5-e901-e8a7c1753074}
(0x38e8889b, 0x9731, 0x53f5, 0xe9, 0x01, 0xe8, 0xa7, 0xc1, 0x75, 0x30, 0x74),
TraceLoggingOptionProjectTelemetry());
void Trace::RegisterProvider() noexcept {
TraceLoggingRegister(g_hProvider);
void Trace::RegisterProvider() noexcept
{
TraceLoggingRegister(g_hProvider);
}
void Trace::UnregisterProvider() noexcept {
TraceLoggingUnregister(g_hProvider);
void Trace::UnregisterProvider() noexcept
{
TraceLoggingUnregister(g_hProvider);
}
void Trace::HideGuide(const __int64 duration_ms, std::vector<int> &key_pressed) noexcept {
std::string vk_codes;
std::vector<int>::iterator it;
for (it = key_pressed.begin(); it != key_pressed.end(); ) {
vk_codes += std::to_string(*it);
if (++it != key_pressed.end()) {
vk_codes += " ";
void Trace::HideGuide(const __int64 duration_ms, std::vector<int>& key_pressed) noexcept
{
std::string vk_codes;
std::vector<int>::iterator it;
for (it = key_pressed.begin(); it != key_pressed.end();)
{
vk_codes += std::to_string(*it);
if (++it != key_pressed.end())
{
vk_codes += " ";
}
}
}
TraceLoggingWrite(
g_hProvider,
"ShortcutGuide_HideGuide",
TraceLoggingInt64(duration_ms, "DurationInMs"),
TraceLoggingInt64(key_pressed.size(), "NumberOfKeysPressed"),
TraceLoggingString(vk_codes.c_str(), "ListOfKeysPressed"),
ProjectTelemetryPrivacyDataTag(ProjectTelemetryTag_ProductAndServicePerformance),
TraceLoggingBoolean(TRUE, "UTCReplace_AppSessionGuid"),
TraceLoggingKeyword(PROJECT_KEYWORD_MEASURE));
TraceLoggingWrite(
g_hProvider,
"ShortcutGuide_HideGuide",
TraceLoggingInt64(duration_ms, "DurationInMs"),
TraceLoggingInt64(key_pressed.size(), "NumberOfKeysPressed"),
TraceLoggingString(vk_codes.c_str(), "ListOfKeysPressed"),
ProjectTelemetryPrivacyDataTag(ProjectTelemetryTag_ProductAndServicePerformance),
TraceLoggingBoolean(TRUE, "UTCReplace_AppSessionGuid"),
TraceLoggingKeyword(PROJECT_KEYWORD_MEASURE));
}
void Trace::EnableShortcutGuide(const bool enabled) noexcept {
TraceLoggingWrite(
g_hProvider,
"ShortcutGuide_EnableGuide",
TraceLoggingBoolean(enabled, "Enabled"),
ProjectTelemetryPrivacyDataTag(ProjectTelemetryTag_ProductAndServicePerformance),
TraceLoggingBoolean(TRUE, "UTCReplace_AppSessionGuid"),
TraceLoggingKeyword(PROJECT_KEYWORD_MEASURE));
void Trace::EnableShortcutGuide(const bool enabled) noexcept
{
TraceLoggingWrite(
g_hProvider,
"ShortcutGuide_EnableGuide",
TraceLoggingBoolean(enabled, "Enabled"),
ProjectTelemetryPrivacyDataTag(ProjectTelemetryTag_ProductAndServicePerformance),
TraceLoggingBoolean(TRUE, "UTCReplace_AppSessionGuid"),
TraceLoggingKeyword(PROJECT_KEYWORD_MEASURE));
}
void Trace::SettingsChanged(const int press_delay_time, const int overlay_opacity, const std::wstring& theme) noexcept {
TraceLoggingWrite(
g_hProvider,
"ShortcutGuide_SettingsChanged",
TraceLoggingInt32(press_delay_time, "PressDelayTime"),
TraceLoggingInt32(overlay_opacity, "OverlayOpacity"),
TraceLoggingWideString(theme.c_str(), "Theme"),
ProjectTelemetryPrivacyDataTag(ProjectTelemetryTag_ProductAndServicePerformance),
TraceLoggingBoolean(TRUE, "UTCReplace_AppSessionGuid"),
TraceLoggingKeyword(PROJECT_KEYWORD_MEASURE));
void Trace::SettingsChanged(const int press_delay_time, const int overlay_opacity, const std::wstring& theme) noexcept
{
TraceLoggingWrite(
g_hProvider,
"ShortcutGuide_SettingsChanged",
TraceLoggingInt32(press_delay_time, "PressDelayTime"),
TraceLoggingInt32(overlay_opacity, "OverlayOpacity"),
TraceLoggingWideString(theme.c_str(), "Theme"),
ProjectTelemetryPrivacyDataTag(ProjectTelemetryTag_ProductAndServicePerformance),
TraceLoggingBoolean(TRUE, "UTCReplace_AppSessionGuid"),
TraceLoggingKeyword(PROJECT_KEYWORD_MEASURE));
}

View File

@ -1,10 +1,11 @@
#pragma once
class Trace {
class Trace
{
public:
static void RegisterProvider() noexcept;
static void UnregisterProvider() noexcept;
static void HideGuide(const __int64 duration_ms, std::vector<int> &key_pressed) noexcept;
static void EnableShortcutGuide(const bool enabled) noexcept;
static void SettingsChanged(const int press_delay_time, const int overlay_opacity, const std::wstring& theme) noexcept;
static void RegisterProvider() noexcept;
static void UnregisterProvider() noexcept;
static void HideGuide(const __int64 duration_ms, std::vector<int>& key_pressed) noexcept;
static void EnableShortcutGuide(const bool enabled) noexcept;
static void SettingsChanged(const int press_delay_time, const int overlay_opacity, const std::wstring& theme) noexcept;
};