2020-03-24 01:44:02 +08:00
|
|
|
#include "pch.h"
|
|
|
|
#include "EditShortcutsWindow.h"
|
|
|
|
#include "ShortcutControl.h"
|
2020-04-19 07:12:26 +08:00
|
|
|
#include "KeyDropDownControl.h"
|
2020-03-24 01:44:02 +08:00
|
|
|
|
|
|
|
LRESULT CALLBACK EditShortcutsWindowProc(HWND, UINT, WPARAM, LPARAM);
|
|
|
|
|
|
|
|
// This Hwnd will be the window handler for the Xaml Island: A child window that contains Xaml.
|
|
|
|
HWND hWndXamlIslandEditShortcutsWindow = nullptr;
|
|
|
|
// This variable is used to check if window registration has been done to avoid repeated registration leading to an error.
|
|
|
|
bool isEditShortcutsWindowRegistrationCompleted = false;
|
2020-04-15 00:24:11 +08:00
|
|
|
// Holds the native window handle of EditShortcuts Window.
|
|
|
|
HWND hwndEditShortcutsNativeWindow = nullptr;
|
|
|
|
std::mutex editShortcutsWindowMutex;
|
2020-03-24 01:44:02 +08:00
|
|
|
|
|
|
|
// Function to create the Edit Shortcuts Window
|
|
|
|
void createEditShortcutsWindow(HINSTANCE hInst, KeyboardManagerState& keyboardManagerState)
|
|
|
|
{
|
|
|
|
// Window Registration
|
|
|
|
const wchar_t szWindowClass[] = L"EditShortcutsWindow";
|
|
|
|
|
|
|
|
if (!isEditShortcutsWindowRegistrationCompleted)
|
|
|
|
{
|
|
|
|
WNDCLASSEX windowClass = {};
|
|
|
|
windowClass.cbSize = sizeof(WNDCLASSEX);
|
|
|
|
windowClass.lpfnWndProc = EditShortcutsWindowProc;
|
|
|
|
windowClass.hInstance = hInst;
|
|
|
|
windowClass.lpszClassName = szWindowClass;
|
|
|
|
windowClass.hbrBackground = (HBRUSH)(COLOR_WINDOW);
|
|
|
|
windowClass.hIconSm = LoadIcon(windowClass.hInstance, IDI_APPLICATION);
|
|
|
|
if (RegisterClassEx(&windowClass) == NULL)
|
|
|
|
{
|
|
|
|
MessageBox(NULL, L"Windows registration failed!", L"Error", NULL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
isEditShortcutsWindowRegistrationCompleted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Window Creation
|
|
|
|
HWND _hWndEditShortcutsWindow = CreateWindow(
|
|
|
|
szWindowClass,
|
2020-03-31 02:05:29 +08:00
|
|
|
L"Edit Shortcuts",
|
2020-03-24 01:44:02 +08:00
|
|
|
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
|
|
|
|
CW_USEDEFAULT,
|
|
|
|
CW_USEDEFAULT,
|
|
|
|
CW_USEDEFAULT,
|
|
|
|
CW_USEDEFAULT,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
hInst,
|
|
|
|
NULL);
|
|
|
|
if (_hWndEditShortcutsWindow == NULL)
|
|
|
|
{
|
|
|
|
MessageBox(NULL, L"Call to CreateWindow failed!", L"Error", NULL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-15 00:24:11 +08:00
|
|
|
// Store the newly created Edit Shortcuts window's handle.
|
|
|
|
std::unique_lock<std::mutex> hwndLock(editShortcutsWindowMutex);
|
|
|
|
hwndEditShortcutsNativeWindow = _hWndEditShortcutsWindow;
|
|
|
|
hwndLock.unlock();
|
|
|
|
|
2020-03-24 01:44:02 +08:00
|
|
|
// This DesktopWindowXamlSource is the object that enables a non-UWP desktop application
|
|
|
|
// to host UWP controls in any UI element that is associated with a window handle (HWND).
|
|
|
|
DesktopWindowXamlSource desktopSource;
|
|
|
|
// Get handle to corewindow
|
|
|
|
auto interop = desktopSource.as<IDesktopWindowXamlSourceNative>();
|
|
|
|
// Parent the DesktopWindowXamlSource object to current window
|
|
|
|
check_hresult(interop->AttachToWindow(_hWndEditShortcutsWindow));
|
|
|
|
|
|
|
|
// Get the new child window's hwnd
|
|
|
|
interop->get_WindowHandle(&hWndXamlIslandEditShortcutsWindow);
|
|
|
|
// Update the xaml island window size becuase initially is 0,0
|
|
|
|
SetWindowPos(hWndXamlIslandEditShortcutsWindow, 0, 0, 0, 400, 400, SWP_SHOWWINDOW);
|
|
|
|
|
|
|
|
// Creating the Xaml content. xamlContainer is the parent UI element
|
|
|
|
Windows::UI::Xaml::Controls::StackPanel xamlContainer;
|
|
|
|
|
|
|
|
// Header for the window
|
2020-04-24 00:14:16 +08:00
|
|
|
Windows::UI::Xaml::Controls::RelativePanel header;
|
2020-03-24 01:44:02 +08:00
|
|
|
header.Margin({ 10, 10, 10, 30 });
|
|
|
|
|
|
|
|
// Header text
|
|
|
|
TextBlock headerText;
|
2020-04-22 05:14:50 +08:00
|
|
|
headerText.Text(L"Edit Shortcuts");
|
2020-03-24 01:44:02 +08:00
|
|
|
headerText.FontSize(30);
|
|
|
|
headerText.Margin({ 0, 0, 100, 0 });
|
2020-04-24 00:14:16 +08:00
|
|
|
header.SetAlignLeftWithPanel(headerText, true);
|
2020-03-24 01:44:02 +08:00
|
|
|
|
|
|
|
// Cancel button
|
|
|
|
Button cancelButton;
|
2020-04-22 05:14:50 +08:00
|
|
|
cancelButton.Content(winrt::box_value(L"Cancel"));
|
2020-04-24 00:14:16 +08:00
|
|
|
cancelButton.Margin({ 0, 0, 10, 0 });
|
2020-04-20 23:22:36 +08:00
|
|
|
cancelButton.Click([&](winrt::Windows::Foundation::IInspectable const& sender, RoutedEventArgs const&) {
|
2020-03-24 01:44:02 +08:00
|
|
|
// Close the window since settings do not need to be saved
|
|
|
|
PostMessage(_hWndEditShortcutsWindow, WM_CLOSE, 0, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Table to display the shortcuts
|
2020-04-24 00:14:16 +08:00
|
|
|
Windows::UI::Xaml::Controls::Grid shortcutTable;
|
|
|
|
ColumnDefinition firstColumn;
|
|
|
|
ColumnDefinition secondColumn;
|
|
|
|
ColumnDefinition thirdColumn;
|
2020-03-24 01:44:02 +08:00
|
|
|
shortcutTable.Margin({ 10, 10, 10, 20 });
|
2020-04-24 00:14:16 +08:00
|
|
|
shortcutTable.HorizontalAlignment(HorizontalAlignment::Stretch);
|
|
|
|
shortcutTable.ColumnSpacing(10);
|
|
|
|
shortcutTable.ColumnDefinitions().Append(firstColumn);
|
|
|
|
shortcutTable.ColumnDefinitions().Append(secondColumn);
|
|
|
|
shortcutTable.ColumnDefinitions().Append(thirdColumn);
|
|
|
|
shortcutTable.RowDefinitions().Append(RowDefinition());
|
2020-03-24 01:44:02 +08:00
|
|
|
|
|
|
|
// First header textblock in the header row of the shortcut table
|
|
|
|
TextBlock originalShortcutHeader;
|
2020-04-22 05:14:50 +08:00
|
|
|
originalShortcutHeader.Text(L"Original Shortcut:");
|
2020-03-24 01:44:02 +08:00
|
|
|
originalShortcutHeader.FontWeight(Text::FontWeights::Bold());
|
|
|
|
originalShortcutHeader.Margin({ 0, 0, 0, 10 });
|
|
|
|
|
2020-04-04 01:57:46 +08:00
|
|
|
// Second header textblock in the header row of the shortcut table
|
2020-03-24 01:44:02 +08:00
|
|
|
TextBlock newShortcutHeader;
|
2020-04-22 05:14:50 +08:00
|
|
|
newShortcutHeader.Text(L"New Shortcut:");
|
2020-03-24 01:44:02 +08:00
|
|
|
newShortcutHeader.FontWeight(Text::FontWeights::Bold());
|
|
|
|
newShortcutHeader.Margin({ 0, 0, 0, 10 });
|
|
|
|
|
2020-04-24 00:14:16 +08:00
|
|
|
shortcutTable.SetColumn(originalShortcutHeader, 0);
|
|
|
|
shortcutTable.SetRow(newShortcutHeader, 0);
|
|
|
|
shortcutTable.SetColumn(originalShortcutHeader, 1);
|
|
|
|
shortcutTable.SetRow(newShortcutHeader, 0);
|
|
|
|
|
|
|
|
shortcutTable.Children().Append(originalShortcutHeader);
|
|
|
|
shortcutTable.Children().Append(newShortcutHeader);
|
2020-03-24 01:44:02 +08:00
|
|
|
|
|
|
|
// Message to display success/failure of saving settings.
|
2020-04-20 23:59:35 +08:00
|
|
|
Flyout applyFlyout;
|
2020-03-24 01:44:02 +08:00
|
|
|
TextBlock settingsMessage;
|
2020-04-20 23:59:35 +08:00
|
|
|
applyFlyout.Content(settingsMessage);
|
2020-03-24 01:44:02 +08:00
|
|
|
|
2020-04-10 00:20:19 +08:00
|
|
|
// Store handle of edit shortcuts window
|
|
|
|
ShortcutControl::EditShortcutsWindowHandle = _hWndEditShortcutsWindow;
|
|
|
|
// Store keyboard manager state
|
|
|
|
ShortcutControl::keyboardManagerState = &keyboardManagerState;
|
2020-04-19 07:12:26 +08:00
|
|
|
KeyDropDownControl::keyboardManagerState = &keyboardManagerState;
|
2020-04-10 00:20:19 +08:00
|
|
|
// Clear the shortcut remap buffer
|
|
|
|
ShortcutControl::shortcutRemapBuffer.clear();
|
2020-04-19 07:12:26 +08:00
|
|
|
// Vector to store dynamically allocated control objects to avoid early destruction
|
|
|
|
std::vector<std::vector<std::unique_ptr<ShortcutControl>>> keyboardRemapControlObjects;
|
2020-04-10 00:20:19 +08:00
|
|
|
|
|
|
|
// Load existing shortcuts into UI
|
|
|
|
std::unique_lock<std::mutex> lock(keyboardManagerState.osLevelShortcutReMap_mutex);
|
|
|
|
for (const auto& it : keyboardManagerState.osLevelShortcutReMap)
|
|
|
|
{
|
2020-04-19 07:12:26 +08:00
|
|
|
ShortcutControl::AddNewShortcutControlRow(shortcutTable, keyboardRemapControlObjects, it.first, it.second.targetShortcut);
|
2020-04-10 00:20:19 +08:00
|
|
|
}
|
|
|
|
lock.unlock();
|
|
|
|
|
2020-03-24 01:44:02 +08:00
|
|
|
// Apply button
|
|
|
|
Button applyButton;
|
2020-04-22 05:14:50 +08:00
|
|
|
applyButton.Content(winrt::box_value(L"Apply"));
|
2020-04-24 00:14:16 +08:00
|
|
|
header.SetAlignRightWithPanel(applyButton, true);
|
|
|
|
header.SetLeftOf(cancelButton, applyButton);
|
2020-04-20 23:59:35 +08:00
|
|
|
applyButton.Flyout(applyFlyout);
|
2020-04-20 23:22:36 +08:00
|
|
|
applyButton.Click([&](winrt::Windows::Foundation::IInspectable const& sender, RoutedEventArgs const&) {
|
2020-03-24 01:44:02 +08:00
|
|
|
bool isSuccess = true;
|
|
|
|
// Clear existing shortcuts
|
|
|
|
keyboardManagerState.ClearOSLevelShortcuts();
|
|
|
|
|
|
|
|
// Save the shortcuts that are valid and report if any of them were invalid
|
2020-04-10 00:20:19 +08:00
|
|
|
for (int i = 0; i < ShortcutControl::shortcutRemapBuffer.size(); i++)
|
2020-03-24 01:44:02 +08:00
|
|
|
{
|
2020-04-10 00:20:19 +08:00
|
|
|
Shortcut originalShortcut = ShortcutControl::shortcutRemapBuffer[i][0];
|
|
|
|
Shortcut newShortcut = ShortcutControl::shortcutRemapBuffer[i][1];
|
2020-03-24 01:44:02 +08:00
|
|
|
|
2020-04-19 07:12:26 +08:00
|
|
|
if (originalShortcut.IsValidShortcut() && newShortcut.IsValidShortcut())
|
2020-04-10 00:20:19 +08:00
|
|
|
{
|
|
|
|
bool result = keyboardManagerState.AddOSLevelShortcut(originalShortcut, newShortcut);
|
|
|
|
if (!result)
|
2020-03-24 01:44:02 +08:00
|
|
|
{
|
|
|
|
isSuccess = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
isSuccess = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-20 23:22:36 +08:00
|
|
|
// Save the updated key remaps to file.
|
|
|
|
auto saveResult = keyboardManagerState.SaveConfigToFile();
|
|
|
|
|
|
|
|
if (isSuccess && saveResult)
|
2020-03-24 01:44:02 +08:00
|
|
|
{
|
2020-04-22 05:14:50 +08:00
|
|
|
settingsMessage.Text(L"Remapping successful!");
|
2020-03-24 01:44:02 +08:00
|
|
|
}
|
2020-04-20 23:22:36 +08:00
|
|
|
else if (!isSuccess && saveResult)
|
2020-03-24 01:44:02 +08:00
|
|
|
{
|
2020-04-22 05:14:50 +08:00
|
|
|
settingsMessage.Text(L"All remappings were not successfully applied.");
|
2020-03-24 01:44:02 +08:00
|
|
|
}
|
2020-04-20 23:22:36 +08:00
|
|
|
else
|
|
|
|
{
|
2020-04-20 23:59:35 +08:00
|
|
|
settingsMessage.Text(L"Failed to save the remappings.");
|
2020-04-20 23:22:36 +08:00
|
|
|
}
|
2020-03-24 01:44:02 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
header.Children().Append(headerText);
|
|
|
|
header.Children().Append(cancelButton);
|
|
|
|
header.Children().Append(applyButton);
|
|
|
|
|
|
|
|
// Add shortcut button
|
|
|
|
Windows::UI::Xaml::Controls::Button addShortcut;
|
|
|
|
FontIcon plusSymbol;
|
|
|
|
plusSymbol.FontFamily(Xaml::Media::FontFamily(L"Segoe MDL2 Assets"));
|
|
|
|
plusSymbol.Glyph(L"\xE109");
|
|
|
|
addShortcut.Content(plusSymbol);
|
|
|
|
addShortcut.Margin({ 10 });
|
2020-04-20 23:22:36 +08:00
|
|
|
addShortcut.Click([&](winrt::Windows::Foundation::IInspectable const& sender, RoutedEventArgs const&) {
|
2020-04-19 07:12:26 +08:00
|
|
|
ShortcutControl::AddNewShortcutControlRow(shortcutTable, keyboardRemapControlObjects);
|
2020-03-24 01:44:02 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
xamlContainer.Children().Append(header);
|
|
|
|
xamlContainer.Children().Append(shortcutTable);
|
|
|
|
xamlContainer.Children().Append(addShortcut);
|
|
|
|
xamlContainer.UpdateLayout();
|
|
|
|
desktopSource.Content(xamlContainer);
|
|
|
|
|
|
|
|
////End XAML Island section
|
|
|
|
if (_hWndEditShortcutsWindow)
|
|
|
|
{
|
|
|
|
ShowWindow(_hWndEditShortcutsWindow, SW_SHOW);
|
|
|
|
UpdateWindow(_hWndEditShortcutsWindow);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Message loop:
|
|
|
|
MSG msg = {};
|
|
|
|
while (GetMessage(&msg, NULL, 0, 0))
|
|
|
|
{
|
|
|
|
TranslateMessage(&msg);
|
|
|
|
DispatchMessage(&msg);
|
|
|
|
}
|
|
|
|
desktopSource.Close();
|
2020-04-15 00:24:11 +08:00
|
|
|
|
|
|
|
hWndXamlIslandEditShortcutsWindow = nullptr;
|
|
|
|
hwndLock.lock();
|
|
|
|
hwndEditShortcutsNativeWindow = nullptr;
|
2020-03-24 01:44:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LRESULT CALLBACK EditShortcutsWindowProc(HWND hWnd, UINT messageCode, WPARAM wParam, LPARAM lParam)
|
|
|
|
{
|
|
|
|
RECT rcClient;
|
|
|
|
switch (messageCode)
|
|
|
|
{
|
2020-04-24 00:14:16 +08:00
|
|
|
// Resize the XAML window whenever the parent window is painted or resized
|
2020-03-24 01:44:02 +08:00
|
|
|
case WM_PAINT:
|
2020-04-24 00:14:16 +08:00
|
|
|
case WM_SIZE:
|
2020-03-24 01:44:02 +08:00
|
|
|
GetClientRect(hWnd, &rcClient);
|
|
|
|
SetWindowPos(hWndXamlIslandEditShortcutsWindow, 0, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom, SWP_SHOWWINDOW);
|
|
|
|
break;
|
|
|
|
case WM_DESTROY:
|
|
|
|
PostQuitMessage(0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return DefWindowProc(hWnd, messageCode, wParam, lParam);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-04-15 00:24:11 +08:00
|
|
|
|
2020-04-20 23:59:35 +08:00
|
|
|
bool CheckEditShortcutsWindowActive()
|
2020-04-15 00:24:11 +08:00
|
|
|
{
|
|
|
|
bool result = false;
|
|
|
|
std::unique_lock<std::mutex> hwndLock(editShortcutsWindowMutex);
|
|
|
|
if (hwndEditShortcutsNativeWindow != nullptr)
|
|
|
|
{
|
|
|
|
// Check if the window is minimized if yes then restore the window.
|
|
|
|
if (IsIconic(hwndEditShortcutsNativeWindow))
|
|
|
|
{
|
|
|
|
ShowWindow(hwndEditShortcutsNativeWindow, SW_RESTORE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is an already existing window no need to create a new open bring it on foreground.
|
|
|
|
SetForegroundWindow(hwndEditShortcutsNativeWindow);
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|