Skip to content

Commit

Permalink
[Keyboard Manager] Cleanup header file references to improve incremen…
Browse files Browse the repository at this point in the history
…tal build time (#4880)

* Remove WinUI include in KeyboardManagerState.h

* Changed include steps

* Clean up headers in KeyboardManagerUI except XamlBridge.h

* Cleaned up headers in KeyboardManager common and test

* Cleaned up headers in KeyboardManager project

* Removed headers from XamlBridge

* Removed some headers from kbm common pch

* Added MP flag to reduce build time

* Added missing include
  • Loading branch information
arjunbalgovind committed Jul 13, 2020
1 parent 7db5d6a commit 6a9badd
Show file tree
Hide file tree
Showing 37 changed files with 290 additions and 179 deletions.
16 changes: 13 additions & 3 deletions src/modules/keyboardmanager/common/Helpers.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
#pragma once
#include <vector>
#include <winrt/Windows.System.h>
#include <winrt/Windows.Foundation.h>
namespace winrt
{
struct hstring;
namespace Windows::Foundation
{
struct IInspectable;
namespace Collections
{
template<typename T>
struct IVector;
}
}
}

namespace KeyboardManagerHelper
{
Expand Down
2 changes: 0 additions & 2 deletions src/modules/keyboardmanager/common/InputInterface.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#pragma once
#include "windows.h"
#include <string>

// Interface used to wrap keyboard input library methods
class InputInterface
Expand Down
2 changes: 1 addition & 1 deletion src/modules/keyboardmanager/common/KeyDelay.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#pragma once
#include <interface/lowlevel_keyboard_event_data.h>
#include <functional>
#include <thread>
#include <queue>
#include <mutex>
#include <interface/lowlevel_keyboard_event_data.h>

// Available states for the KeyDelay state machine.
enum class KeyDelayState
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<AdditionalOptions Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">/MP %(AdditionalOptions)</AdditionalOptions>
<AdditionalOptions Condition="'$(Configuration)|$(Platform)'=='Release|x64'">/MP %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
<Lib>
<AdditionalDependencies Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">shlwapi.lib;</AdditionalDependencies>
Expand Down
35 changes: 18 additions & 17 deletions src/modules/keyboardmanager/common/KeyboardManagerState.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "pch.h"
#include "KeyboardManagerState.h"
#include <../common/settings_helpers.h>
#include "KeyDelay.h"

// Constructor
KeyboardManagerState::KeyboardManagerState() :
Expand Down Expand Up @@ -179,15 +181,15 @@ bool KeyboardManagerState::AddAppSpecificShortcut(const std::wstring& app, const
void KeyboardManagerState::ConfigureDetectShortcutUI(const StackPanel& textBlock1, const StackPanel& textBlock2)
{
std::lock_guard<std::mutex> lock(currentShortcutUI_mutex);
currentShortcutUI1 = textBlock1;
currentShortcutUI2 = textBlock2;
currentShortcutUI1 = textBlock1.as<winrt::Windows::Foundation::IInspectable>();
currentShortcutUI2 = textBlock2.as<winrt::Windows::Foundation::IInspectable>();
}

// Function to set the textblock of the detect remap key UI so that it can be accessed by the hook
void KeyboardManagerState::ConfigureDetectSingleKeyRemapUI(const StackPanel& textBlock)
{
std::lock_guard<std::mutex> lock(currentSingleKeyUI_mutex);
currentSingleKeyUI = textBlock;
currentSingleKeyUI = textBlock.as<winrt::Windows::Foundation::IInspectable>();
}

void KeyboardManagerState::AddKeyToLayout(const StackPanel& panel, const hstring& key)
Expand Down Expand Up @@ -226,34 +228,34 @@ void KeyboardManagerState::UpdateDetectShortcutUI()
detectedShortcut_lock.unlock();

// Since this function is invoked from the back-end thread, in order to update the UI the dispatcher must be used.
currentShortcutUI1.Dispatcher().RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, [this, detectedShortcutCopy]() {
currentShortcutUI1.as<StackPanel>().Dispatcher().RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, [this, detectedShortcutCopy]() {
std::vector<hstring> shortcut = detectedShortcutCopy.GetKeyVector(keyboardMap);
currentShortcutUI1.Children().Clear();
currentShortcutUI2.Children().Clear();
currentShortcutUI1.as<StackPanel>().Children().Clear();
currentShortcutUI2.as<StackPanel>().Children().Clear();

// The second row should be hidden if there are 3 keys or lesser to avoid an extra margin
if (shortcut.size() > 3)
{
currentShortcutUI2.Visibility(Visibility::Visible);
currentShortcutUI2.as<StackPanel>().Visibility(Visibility::Visible);
}
else
{
currentShortcutUI2.Visibility(Visibility::Collapsed);
currentShortcutUI2.as<StackPanel>().Visibility(Visibility::Collapsed);
}

for (int i = 0; i < shortcut.size(); i++)
{
if (i < 3)
{
AddKeyToLayout(currentShortcutUI1, shortcut[i]);
AddKeyToLayout(currentShortcutUI1.as<StackPanel>(), shortcut[i]);
}
else
{
AddKeyToLayout(currentShortcutUI2, shortcut[i]);
AddKeyToLayout(currentShortcutUI2.as<StackPanel>(), shortcut[i]);
}
}
currentShortcutUI1.UpdateLayout();
currentShortcutUI2.UpdateLayout();
currentShortcutUI1.as<StackPanel>().UpdateLayout();
currentShortcutUI2.as<StackPanel>().UpdateLayout();
});
}

Expand All @@ -265,13 +267,12 @@ void KeyboardManagerState::UpdateDetectSingleKeyRemapUI()
{
return;
}

// Since this function is invoked from the back-end thread, in order to update the UI the dispatcher must be used.
currentSingleKeyUI.Dispatcher().RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, [this]() {
currentSingleKeyUI.Children().Clear();
currentSingleKeyUI.as<StackPanel>().Dispatcher().RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, [this]() {
currentSingleKeyUI.as<StackPanel>().Children().Clear();
hstring key = winrt::to_hstring(keyboardMap.GetKeyName(detectedRemapKey).c_str());
AddKeyToLayout(currentSingleKeyUI, key);
currentSingleKeyUI.UpdateLayout();
AddKeyToLayout(currentSingleKeyUI.as<StackPanel>(), key);
currentSingleKeyUI.as<StackPanel>().UpdateLayout();
});
}

Expand Down
27 changes: 15 additions & 12 deletions src/modules/keyboardmanager/common/KeyboardManagerState.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
#pragma once
#include "Helpers.h"
#include "../common/keyboard_layout.h"
#include "Shortcut.h"
#include "RemapShortcut.h"
#include "KeyDelay.h"
#include <mutex>
#include "KeyboardManagerConstants.h"
#include "../common/keyboard_layout.h"
#include <functional>
#include <interface/lowlevel_keyboard_event_data.h>
#include <mutex>
#include <winrt/Windows.UI.Xaml.Controls.h>
#include <../common/settings_helpers.h>

using namespace winrt::Windows::UI::Xaml::Controls;
class KeyDelay;

namespace winrt::Windows::UI::Xaml::Controls
{
struct StackPanel;
}

// Enum type to store different states of the UI
enum class KeyboardManagerUIState
Expand Down Expand Up @@ -52,12 +55,12 @@ class KeyboardManagerState
std::mutex detectedRemapKey_mutex;

// Stores the UI element which is to be updated based on the remap key entered.
StackPanel currentSingleKeyUI;
winrt::Windows::Foundation::IInspectable currentSingleKeyUI;
std::mutex currentSingleKeyUI_mutex;

// Stores the UI element which is to be updated based on the shortcut entered (each stackpanel represents a row of keys)
StackPanel currentShortcutUI1;
StackPanel currentShortcutUI2;
winrt::Windows::Foundation::IInspectable currentShortcutUI1;
winrt::Windows::Foundation::IInspectable currentShortcutUI2;
std::mutex currentShortcutUI_mutex;

// Stores the current configuration name.
Expand All @@ -75,7 +78,7 @@ class KeyboardManagerState
std::wstring activatedAppSpecificShortcutTarget;

// Display a key by appending a border Control as a child of the panel.
void AddKeyToLayout(const StackPanel& panel, const winrt::hstring& key);
void AddKeyToLayout(const winrt::Windows::UI::Xaml::Controls::StackPanel& panel, const winrt::hstring& key);

public:
// The map members and their mutexes are left as public since the maps are used extensively in dllmain.cpp.
Expand Down Expand Up @@ -136,10 +139,10 @@ class KeyboardManagerState
bool AddAppSpecificShortcut(const std::wstring& app, const Shortcut& originalSC, const Shortcut& newSC);

// Function to set the textblock of the detect shortcut UI so that it can be accessed by the hook
void ConfigureDetectShortcutUI(const StackPanel& textBlock1, const StackPanel& textBlock2);
void ConfigureDetectShortcutUI(const winrt::Windows::UI::Xaml::Controls::StackPanel& textBlock1, const winrt::Windows::UI::Xaml::Controls::StackPanel& textBlock2);

// Function to set the textblock of the detect remap key UI so that it can be accessed by the hook
void ConfigureDetectSingleKeyRemapUI(const StackPanel& textBlock);
void ConfigureDetectSingleKeyRemapUI(const winrt::Windows::UI::Xaml::Controls::StackPanel& textBlock);

// Function to update the detect shortcut UI based on the entered keys
void UpdateDetectShortcutUI();
Expand Down
17 changes: 17 additions & 0 deletions src/modules/keyboardmanager/common/Shortcut.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
#include "pch.h"
#include "Shortcut.h"
#include "../common/keyboard_layout.h"
#include "../common/shared_constants.h"
#include <interface/lowlevel_keyboard_event_data.h>
#include "Helpers.h"
#include "InputInterface.h"

// Constructor to initialize Shortcut from it's virtual key code string representation.
Shortcut::Shortcut(const std::wstring& shortcutVK) :
winKey(ModifierKey::Disabled), ctrlKey(ModifierKey::Disabled), altKey(ModifierKey::Disabled), shiftKey(ModifierKey::Disabled), actionKey(NULL)
{
auto keys = KeyboardManagerHelper::splitwstring(shortcutVK, ';');
for (auto it : keys)
{
auto vkKeyCode = std::stoul(it);
SetKey(vkKeyCode);
}
}

// Function to return the number of keys in the shortcut
int Shortcut::Size() const
Expand Down
23 changes: 8 additions & 15 deletions src/modules/keyboardmanager/common/Shortcut.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#pragma once
#include "Helpers.h"
#include "../common/keyboard_layout.h"
#include "../common/shared_constants.h"
#include <interface/lowlevel_keyboard_event_data.h>
#include "InputInterface.h"

class InputInterface;
class LayoutMap;
namespace KeyboardManagerHelper
{
enum class ErrorType;
}

// Enum type to store different states of the win key
enum class ModifierKey
Expand Down Expand Up @@ -31,16 +33,7 @@ class Shortcut
}

// Constructor to initialize Shortcut from it's virtual key code string representation.
Shortcut(const std::wstring& shortcutVK) :
winKey(ModifierKey::Disabled), ctrlKey(ModifierKey::Disabled), altKey(ModifierKey::Disabled), shiftKey(ModifierKey::Disabled), actionKey(NULL)
{
auto keys = KeyboardManagerHelper::splitwstring(shortcutVK, ';');
for (auto it : keys)
{
auto vkKeyCode = std::stoul(it);
SetKey(vkKeyCode);
}
}
Shortcut(const std::wstring& shortcutVK);

// == operator
inline bool operator==(const Shortcut& sc) const
Expand Down
10 changes: 1 addition & 9 deletions src/modules/keyboardmanager/common/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,14 @@
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Graphics.h>
#include <winrt/Windows.system.h>
#include <winrt/windows.ui.xaml.hosting.h>
#include <windows.ui.xaml.hosting.desktopwindowxamlsource.h>
#include <winrt/windows.ui.xaml.controls.h>
#include <winrt/Windows.ui.xaml.media.h>
#include <winrt/Windows.Foundation.Collections.h>
#include "winrt/Windows.Foundation.Numerics.h"
#include "winrt/Windows.UI.Xaml.Controls.Primitives.h"
#include "winrt/Windows.UI.Text.h"
#include <winrt/windows.ui.xaml.controls.h>
#include "winrt/Windows.UI.Core.h"
#include <stdlib.h>
#include <ProjectTelemetry.h>

using namespace winrt;
using namespace Windows::UI;
using namespace Windows::UI::Composition;
using namespace Windows::UI::Xaml::Hosting;
using namespace Windows::Foundation::Numerics;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
3 changes: 3 additions & 0 deletions src/modules/keyboardmanager/dll/KeyboardEventHandlers.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "pch.h"
#include "KeyboardEventHandlers.h"
#include "../common/shared_constants.h"
#include <keyboardmanager/common/KeyboardManagerState.h>
#include <keyboardmanager/common/InputInterface.h>

namespace KeyboardEventHandlers
{
Expand Down
12 changes: 9 additions & 3 deletions src/modules/keyboardmanager/dll/KeyboardEventHandlers.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
#pragma once
#include <keyboardmanager/common/KeyboardManagerState.h>
#include <keyboardmanager/common/KeyboardManagerConstants.h>
#include <keyboardmanager/common/InputInterface.h>
#include <interface/lowlevel_keyboard_event_data.h>
#include <map>
#include <mutex>
#include "keyboardmanager/common/KeyboardManagerConstants.h"

class InputInterface;
class KeyboardManagerState;
class Shortcut;
class RemapShortcut;

namespace KeyboardEventHandlers
{
Expand Down
2 changes: 2 additions & 0 deletions src/modules/keyboardmanager/dll/KeyboardManager.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<AdditionalOptions Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">/MP %(AdditionalOptions)</AdditionalOptions>
<AdditionalOptions Condition="'$(Configuration)|$(Platform)'=='Release|x64'">/MP %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
</ItemDefinitionGroup>
<ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion src/modules/keyboardmanager/dll/pch.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <common/common.h>
#include <ProjectTelemetry.h>
#include <shlwapi.h>
#include <stdexcept>
Expand Down
2 changes: 2 additions & 0 deletions src/modules/keyboardmanager/test/KeyboardManagerTest.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<AdditionalOptions Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">/MP %(AdditionalOptions)</AdditionalOptions>
<AdditionalOptions Condition="'$(Configuration)|$(Platform)'=='Release|x64'">/MP %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
</ItemDefinitionGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <keyboardmanager/common/KeyboardManagerState.h>
#include <keyboardmanager/dll/KeyboardEventHandlers.h>
#include "TestHelpers.h"
#include "../common/shared_constants.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <keyboardmanager/common/KeyboardManagerState.h>
#include <keyboardmanager/dll/KeyboardEventHandlers.h>
#include "TestHelpers.h"
#include "../common/shared_constants.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

Expand Down
2 changes: 2 additions & 0 deletions src/modules/keyboardmanager/test/TestHelpers.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "pch.h"
#include "TestHelpers.h"
#include "MockedInput.h"
#include "keyboardmanager/common/KeyboardManagerState.h"

namespace TestHelpers
{
Expand Down
4 changes: 2 additions & 2 deletions src/modules/keyboardmanager/test/TestHelpers.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once
#include "MockedInput.h"
#include <keyboardmanager/common/KeyboardManagerState.h>
class MockedInput;
class KeyboardManagerState;

namespace TestHelpers
{
Expand Down
6 changes: 6 additions & 0 deletions src/modules/keyboardmanager/test/pch.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
#pragma once
#pragma comment(lib, "shlwapi.lib")
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <ProjectTelemetry.h>
#include <shlwapi.h>
#include <stdexcept>
#include <unordered_set>
2 changes: 2 additions & 0 deletions src/modules/keyboardmanager/ui/Dialog.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "pch.h"
#include "Dialog.h"

using namespace winrt::Windows::Foundation;

IAsyncOperation<bool> Dialog::PartialRemappingConfirmationDialog(XamlRoot root, std::wstring dialogTitle)
{
ContentDialog confirmationDialog;
Expand Down

0 comments on commit 6a9badd

Please sign in to comment.