2020-03-27 23:38:58 +08:00
# include "pch.h"
# include "SingleKeyRemapControl.h"
2020-04-21 12:01:21 +08:00
# include "keyboardmanager/common/Helpers.h"
2020-03-27 23:38:58 +08:00
//Both static members are initialized to null
HWND SingleKeyRemapControl : : EditKeyboardWindowHandle = nullptr ;
KeyboardManagerState * SingleKeyRemapControl : : keyboardManagerState = nullptr ;
2020-04-10 00:20:19 +08:00
// Initialized as new vector
std : : vector < std : : vector < DWORD > > SingleKeyRemapControl : : singleKeyRemapBuffer ;
2020-03-27 23:38:58 +08:00
// Function to add a new row to the remap keys table. If the originalKey and newKey args are provided, then the displayed remap keys are set to those values.
2020-04-24 00:14:16 +08:00
void SingleKeyRemapControl : : AddNewControlKeyRemapRow ( Grid & parent , std : : vector < std : : vector < std : : unique_ptr < SingleKeyRemapControl > > > & keyboardRemapControlObjects , const DWORD originalKey , const DWORD newKey )
2020-03-27 23:38:58 +08:00
{
2020-04-19 07:12:26 +08:00
// Create new SingleKeyRemapControl objects dynamically so that we does not get destructed
std : : vector < std : : unique_ptr < SingleKeyRemapControl > > newrow ;
2020-05-12 01:10:36 +08:00
newrow . push_back ( std : : move ( std : : unique_ptr < SingleKeyRemapControl > ( new SingleKeyRemapControl ( parent , 0 ) ) ) ) ;
newrow . push_back ( std : : move ( std : : unique_ptr < SingleKeyRemapControl > ( new SingleKeyRemapControl ( parent , 1 ) ) ) ) ;
2020-04-19 07:12:26 +08:00
keyboardRemapControlObjects . push_back ( std : : move ( newrow ) ) ;
2020-04-24 00:14:16 +08:00
// Add to grid
parent . RowDefinitions ( ) . Append ( RowDefinition ( ) ) ;
2020-05-09 08:34:24 +08:00
parent . SetColumn ( keyboardRemapControlObjects [ keyboardRemapControlObjects . size ( ) - 1 ] [ 0 ] - > getSingleKeyRemapControl ( ) , KeyboardManagerConstants : : RemapTableOriginalColIndex ) ;
2020-04-24 00:14:16 +08:00
parent . SetRow ( keyboardRemapControlObjects [ keyboardRemapControlObjects . size ( ) - 1 ] [ 0 ] - > getSingleKeyRemapControl ( ) , parent . RowDefinitions ( ) . Size ( ) - 1 ) ;
2020-05-09 08:34:24 +08:00
parent . SetColumn ( keyboardRemapControlObjects [ keyboardRemapControlObjects . size ( ) - 1 ] [ 1 ] - > getSingleKeyRemapControl ( ) , KeyboardManagerConstants : : RemapTableNewColIndex ) ;
2020-04-24 00:14:16 +08:00
parent . SetRow ( keyboardRemapControlObjects [ keyboardRemapControlObjects . size ( ) - 1 ] [ 1 ] - > getSingleKeyRemapControl ( ) , parent . RowDefinitions ( ) . Size ( ) - 1 ) ;
2020-03-27 23:38:58 +08:00
// SingleKeyRemapControl for the original key.
2020-04-24 00:14:16 +08:00
parent . Children ( ) . Append ( keyboardRemapControlObjects [ keyboardRemapControlObjects . size ( ) - 1 ] [ 0 ] - > getSingleKeyRemapControl ( ) ) ;
2020-05-09 08:34:24 +08:00
// Arrow icon
FontIcon arrowIcon ;
arrowIcon . FontFamily ( Xaml : : Media : : FontFamily ( L " Segoe MDL2 Assets " ) ) ;
arrowIcon . Glyph ( L " \xE72A " ) ;
arrowIcon . VerticalAlignment ( VerticalAlignment : : Center ) ;
arrowIcon . HorizontalAlignment ( HorizontalAlignment : : Center ) ;
parent . SetColumn ( arrowIcon , KeyboardManagerConstants : : RemapTableArrowColIndex ) ;
parent . SetRow ( arrowIcon , parent . RowDefinitions ( ) . Size ( ) - 1 ) ;
parent . Children ( ) . Append ( arrowIcon ) ;
2020-04-19 07:12:26 +08:00
// SingleKeyRemapControl for the new remap key
2020-04-24 00:14:16 +08:00
parent . Children ( ) . Append ( keyboardRemapControlObjects [ keyboardRemapControlObjects . size ( ) - 1 ] [ 1 ] - > getSingleKeyRemapControl ( ) ) ;
2020-03-27 23:38:58 +08:00
// Set the key text if the two keys are not null (i.e. default args)
if ( originalKey ! = NULL & & newKey ! = NULL )
{
2020-04-10 00:20:19 +08:00
singleKeyRemapBuffer . push_back ( std : : vector < DWORD > { originalKey , newKey } ) ;
2020-04-19 07:12:26 +08:00
std : : vector < DWORD > keyCodes = keyboardManagerState - > keyboardMap . GetKeyCodeList ( ) ;
auto it = std : : find ( keyCodes . begin ( ) , keyCodes . end ( ) , originalKey ) ;
if ( it ! = keyCodes . end ( ) )
{
keyboardRemapControlObjects [ keyboardRemapControlObjects . size ( ) - 1 ] [ 0 ] - > singleKeyRemapDropDown . SetSelectedIndex ( ( int32_t ) std : : distance ( keyCodes . begin ( ) , it ) ) ;
}
it = std : : find ( keyCodes . begin ( ) , keyCodes . end ( ) , newKey ) ;
if ( it ! = keyCodes . end ( ) )
{
keyboardRemapControlObjects [ keyboardRemapControlObjects . size ( ) - 1 ] [ 1 ] - > singleKeyRemapDropDown . SetSelectedIndex ( ( int32_t ) std : : distance ( keyCodes . begin ( ) , it ) ) ;
}
2020-04-10 00:20:19 +08:00
}
else
{
// Initialize both keys to NULL
singleKeyRemapBuffer . push_back ( std : : vector < DWORD > { NULL , NULL } ) ;
2020-03-27 23:38:58 +08:00
}
// Delete row button
Windows : : UI : : Xaml : : Controls : : Button deleteRemapKeys ;
FontIcon deleteSymbol ;
deleteSymbol . FontFamily ( Xaml : : Media : : FontFamily ( L " Segoe MDL2 Assets " ) ) ;
deleteSymbol . Glyph ( L " \xE74D " ) ;
deleteRemapKeys . Content ( deleteSymbol ) ;
2020-05-06 23:34:26 +08:00
deleteRemapKeys . Background ( Media : : SolidColorBrush ( Colors : : Transparent ( ) ) ) ;
2020-05-09 08:34:24 +08:00
deleteRemapKeys . HorizontalAlignment ( HorizontalAlignment : : Center ) ;
2020-04-20 23:22:36 +08:00
deleteRemapKeys . Click ( [ & ] ( winrt : : Windows : : Foundation : : IInspectable const & sender , RoutedEventArgs const & ) {
2020-04-24 00:14:16 +08:00
Button currentButton = sender . as < Button > ( ) ;
2020-03-27 23:38:58 +08:00
uint32_t index ;
2020-04-24 00:14:16 +08:00
// Get index of delete button
UIElementCollection children = parent . Children ( ) ;
children . IndexOf ( currentButton , index ) ;
2020-05-09 08:34:24 +08:00
uint32_t lastIndexInRow = index + ( ( KeyboardManagerConstants : : RemapTableColCount - 1 ) - KeyboardManagerConstants : : RemapTableRemoveColIndex ) ;
2020-04-24 00:14:16 +08:00
// Change the row index of elements appearing after the current row, as we will delete the row definition
2020-04-27 06:09:40 +08:00
for ( uint32_t i = lastIndexInRow + 1 ; i < children . Size ( ) ; i + + )
2020-04-24 00:14:16 +08:00
{
int32_t elementRowIndex = parent . GetRow ( children . GetAt ( i ) . as < FrameworkElement > ( ) ) ;
parent . SetRow ( children . GetAt ( i ) . as < FrameworkElement > ( ) , elementRowIndex - 1 ) ;
}
2020-05-09 08:34:24 +08:00
for ( int i = 0 ; i < KeyboardManagerConstants : : RemapTableColCount ; i + + )
{
parent . Children ( ) . RemoveAt ( lastIndexInRow - i ) ;
}
2020-04-24 00:14:16 +08:00
// Calculate row index in the buffer from the grid child index (first two children are header elements and then three children in each row)
2020-05-09 08:34:24 +08:00
int bufferIndex = ( lastIndexInRow - KeyboardManagerConstants : : RemapTableHeaderCount ) / KeyboardManagerConstants : : RemapTableColCount ;
2020-04-24 00:14:16 +08:00
// Delete the row definition
parent . RowDefinitions ( ) . RemoveAt ( bufferIndex + 1 ) ;
// delete the row from the buffer.
singleKeyRemapBuffer . erase ( singleKeyRemapBuffer . begin ( ) + bufferIndex ) ;
2020-04-19 07:12:26 +08:00
// delete the SingleKeyRemapControl objects so that they get destructed
2020-04-24 00:14:16 +08:00
keyboardRemapControlObjects . erase ( keyboardRemapControlObjects . begin ( ) + bufferIndex ) ;
2020-03-27 23:38:58 +08:00
} ) ;
2020-05-09 08:34:24 +08:00
parent . SetColumn ( deleteRemapKeys , KeyboardManagerConstants : : RemapTableRemoveColIndex ) ;
2020-04-24 00:14:16 +08:00
parent . SetRow ( deleteRemapKeys , parent . RowDefinitions ( ) . Size ( ) - 1 ) ;
parent . Children ( ) . Append ( deleteRemapKeys ) ;
2020-04-27 06:09:40 +08:00
parent . UpdateLayout ( ) ;
2020-03-27 23:38:58 +08:00
}
// Function to return the stack panel element of the SingleKeyRemapControl. This is the externally visible UI element which can be used to add it to other layouts
StackPanel SingleKeyRemapControl : : getSingleKeyRemapControl ( )
{
return singleKeyRemapControlLayout ;
}
// Function to create the detect remap key UI window
2020-04-24 00:14:16 +08:00
void SingleKeyRemapControl : : createDetectKeyWindow ( winrt : : Windows : : Foundation : : IInspectable const & sender , XamlRoot xamlRoot , std : : vector < std : : vector < DWORD > > & singleKeyRemapBuffer , KeyboardManagerState & keyboardManagerState )
2020-03-27 23:38:58 +08:00
{
// ContentDialog for detecting remap key. This is the parent UI element.
ContentDialog detectRemapKeyBox ;
2020-04-17 00:16:48 +08:00
2020-03-27 23:38:58 +08:00
// ContentDialog requires manually setting the XamlRoot (https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.controls.contentdialog#contentdialog-in-appwindow-or-xaml-islands)
detectRemapKeyBox . XamlRoot ( xamlRoot ) ;
detectRemapKeyBox . Title ( box_value ( L " Press a key on selected keyboard: " ) ) ;
2020-04-17 00:16:48 +08:00
detectRemapKeyBox . IsPrimaryButtonEnabled ( false ) ;
2020-03-27 23:38:58 +08:00
detectRemapKeyBox . IsSecondaryButtonEnabled ( false ) ;
// Get the linked text block for the "Type Key" button that was clicked
2020-04-20 23:22:36 +08:00
ComboBox linkedRemapDropDown = KeyboardManagerHelper : : getSiblingElement ( sender ) . as < ComboBox > ( ) ;
2020-03-27 23:38:58 +08:00
2020-04-17 00:16:48 +08:00
auto unregisterKeys = [ & keyboardManagerState ] ( ) {
std : : thread t1 ( & KeyboardManagerState : : UnregisterKeyDelay , & keyboardManagerState , VK_ESCAPE ) ;
std : : thread t2 ( & KeyboardManagerState : : UnregisterKeyDelay , & keyboardManagerState , VK_RETURN ) ;
t1 . detach ( ) ;
t2 . detach ( ) ;
} ;
2020-04-19 07:12:26 +08:00
auto onAccept = [ linkedRemapDropDown ,
2020-04-17 00:16:48 +08:00
detectRemapKeyBox ,
& keyboardManagerState ,
& singleKeyRemapBuffer ,
2020-04-24 00:14:16 +08:00
unregisterKeys ] {
2020-03-27 23:38:58 +08:00
// Save the detected key in the linked text block
DWORD detectedKey = keyboardManagerState . GetDetectedSingleRemapKey ( ) ;
2020-04-10 00:20:19 +08:00
2020-03-27 23:38:58 +08:00
if ( detectedKey ! = NULL )
{
2020-04-19 07:12:26 +08:00
std : : vector < DWORD > keyCodeList = keyboardManagerState . keyboardMap . GetKeyCodeList ( ) ;
// Update the drop down list with the new language to ensure that the correct key is displayed
2020-04-21 12:01:21 +08:00
linkedRemapDropDown . ItemsSource ( KeyboardManagerHelper : : ToBoxValue ( keyboardManagerState . keyboardMap . GetKeyNameList ( ) ) ) ;
2020-04-19 07:12:26 +08:00
auto it = std : : find ( keyCodeList . begin ( ) , keyCodeList . end ( ) , detectedKey ) ;
if ( it ! = keyCodeList . end ( ) )
{
linkedRemapDropDown . SelectedIndex ( ( int32_t ) std : : distance ( keyCodeList . begin ( ) , it ) ) ;
}
2020-03-27 23:38:58 +08:00
}
// Reset the keyboard manager UI state
keyboardManagerState . ResetUIState ( ) ;
2020-05-05 06:49:37 +08:00
// Revert UI state back to Edit Keyboard window
keyboardManagerState . SetUIState ( KeyboardManagerUIState : : EditKeyboardWindowActivated , EditKeyboardWindowHandle ) ;
2020-04-17 00:16:48 +08:00
unregisterKeys ( ) ;
detectRemapKeyBox . Hide ( ) ;
} ;
TextBlock primaryButtonText ;
2020-04-22 05:14:50 +08:00
primaryButtonText . Text ( L " OK " ) ;
2020-04-17 00:16:48 +08:00
Button primaryButton ;
primaryButton . HorizontalAlignment ( HorizontalAlignment : : Stretch ) ;
primaryButton . Margin ( { 2 , 2 , 2 , 2 } ) ;
primaryButton . Content ( primaryButtonText ) ;
2020-04-20 23:22:36 +08:00
primaryButton . Click ( [ onAccept ] ( winrt : : Windows : : Foundation : : IInspectable const & sender , RoutedEventArgs const & ) {
2020-04-17 00:16:48 +08:00
onAccept ( ) ;
2020-03-27 23:38:58 +08:00
} ) ;
2020-04-17 00:16:48 +08:00
keyboardManagerState . RegisterKeyDelay (
VK_RETURN ,
std : : bind ( & KeyboardManagerState : : SelectDetectedRemapKey , & keyboardManagerState , std : : placeholders : : _1 ) ,
[ primaryButton , detectRemapKeyBox ] ( DWORD ) {
detectRemapKeyBox . Dispatcher ( ) . RunAsync (
Windows : : UI : : Core : : CoreDispatcherPriority : : Normal ,
[ primaryButton ] {
2020-04-22 04:42:06 +08:00
// Use the base medium low brush to be consistent with the theme
primaryButton . Background ( Windows : : UI : : Xaml : : Application : : Current ( ) . Resources ( ) . Lookup ( box_value ( L " SystemControlBackgroundBaseMediumLowBrush " ) ) . as < Windows : : UI : : Xaml : : Media : : SolidColorBrush > ( ) ) ;
2020-04-17 00:16:48 +08:00
} ) ;
} ,
[ onAccept , detectRemapKeyBox ] ( DWORD ) {
detectRemapKeyBox . Dispatcher ( ) . RunAsync (
Windows : : UI : : Core : : CoreDispatcherPriority : : Normal ,
[ onAccept ] {
onAccept ( ) ;
} ) ;
} ) ;
TextBlock cancelButtonText ;
2020-04-22 05:14:50 +08:00
cancelButtonText . Text ( L " Cancel " ) ;
2020-04-17 00:16:48 +08:00
Button cancelButton ;
cancelButton . HorizontalAlignment ( HorizontalAlignment : : Stretch ) ;
cancelButton . Margin ( { 2 , 2 , 2 , 2 } ) ;
cancelButton . Content ( cancelButtonText ) ;
2020-03-27 23:38:58 +08:00
// Cancel button
2020-04-20 23:22:36 +08:00
cancelButton . Click ( [ detectRemapKeyBox , unregisterKeys , & keyboardManagerState ] ( winrt : : Windows : : Foundation : : IInspectable const & sender , RoutedEventArgs const & ) {
2020-03-27 23:38:58 +08:00
// Reset the keyboard manager UI state
keyboardManagerState . ResetUIState ( ) ;
2020-05-05 06:49:37 +08:00
// Revert UI state back to Edit Keyboard window
keyboardManagerState . SetUIState ( KeyboardManagerUIState : : EditKeyboardWindowActivated , EditKeyboardWindowHandle ) ;
2020-04-17 00:16:48 +08:00
unregisterKeys ( ) ;
detectRemapKeyBox . Hide ( ) ;
2020-03-27 23:38:58 +08:00
} ) ;
2020-04-17 00:16:48 +08:00
keyboardManagerState . RegisterKeyDelay (
VK_ESCAPE ,
std : : bind ( & KeyboardManagerState : : SelectDetectedRemapKey , & keyboardManagerState , std : : placeholders : : _1 ) ,
[ & keyboardManagerState , detectRemapKeyBox , unregisterKeys ] ( DWORD ) {
detectRemapKeyBox . Dispatcher ( ) . RunAsync (
Windows : : UI : : Core : : CoreDispatcherPriority : : Normal ,
[ detectRemapKeyBox ] {
detectRemapKeyBox . Hide ( ) ;
} ) ;
keyboardManagerState . ResetUIState ( ) ;
2020-05-05 06:49:37 +08:00
// Revert UI state back to Edit Keyboard window
keyboardManagerState . SetUIState ( KeyboardManagerUIState : : EditKeyboardWindowActivated , EditKeyboardWindowHandle ) ;
2020-04-17 00:16:48 +08:00
unregisterKeys ( ) ;
} ,
nullptr ) ;
2020-03-27 23:38:58 +08:00
// StackPanel parent for the displayed text in the dialog
Windows : : UI : : Xaml : : Controls : : StackPanel stackPanel ;
2020-04-09 05:31:31 +08:00
detectRemapKeyBox . Content ( stackPanel ) ;
2020-03-27 23:38:58 +08:00
// Header textblock
TextBlock text ;
2020-04-22 05:14:50 +08:00
text . Text ( L " Key Pressed: " ) ;
2020-03-27 23:38:58 +08:00
text . Margin ( { 0 , 0 , 0 , 10 } ) ;
stackPanel . Children ( ) . Append ( text ) ;
2020-04-09 05:31:31 +08:00
// Target StackPanel to place the selected key
Windows : : UI : : Xaml : : Controls : : StackPanel keyStackPanel ;
keyStackPanel . Orientation ( Orientation : : Horizontal ) ;
2020-04-17 00:16:48 +08:00
stackPanel . Children ( ) . Append ( keyStackPanel ) ;
TextBlock holdEscInfo ;
2020-04-22 05:14:50 +08:00
holdEscInfo . Text ( L " Hold Esc to discard " ) ;
2020-04-17 00:16:48 +08:00
holdEscInfo . FontSize ( 12 ) ;
holdEscInfo . Margin ( { 0 , 20 , 0 , 0 } ) ;
stackPanel . Children ( ) . Append ( holdEscInfo ) ;
TextBlock holdEnterInfo ;
2020-04-22 05:14:50 +08:00
holdEnterInfo . Text ( L " Hold Enter to apply " ) ;
2020-04-17 00:16:48 +08:00
holdEnterInfo . FontSize ( 12 ) ;
holdEnterInfo . Margin ( { 0 , 0 , 0 , 0 } ) ;
stackPanel . Children ( ) . Append ( holdEnterInfo ) ;
ColumnDefinition primaryButtonColumn ;
ColumnDefinition cancelButtonColumn ;
Grid buttonPanel ;
buttonPanel . Margin ( { 0 , 20 , 0 , 0 } ) ;
buttonPanel . HorizontalAlignment ( HorizontalAlignment : : Stretch ) ;
buttonPanel . ColumnDefinitions ( ) . Append ( primaryButtonColumn ) ;
buttonPanel . ColumnDefinitions ( ) . Append ( cancelButtonColumn ) ;
buttonPanel . SetColumn ( primaryButton , 0 ) ;
buttonPanel . SetColumn ( cancelButton , 1 ) ;
buttonPanel . Children ( ) . Append ( primaryButton ) ;
buttonPanel . Children ( ) . Append ( cancelButton ) ;
stackPanel . Children ( ) . Append ( buttonPanel ) ;
2020-03-27 23:38:58 +08:00
stackPanel . UpdateLayout ( ) ;
// Configure the keyboardManagerState to store the UI information.
2020-04-09 05:31:31 +08:00
keyboardManagerState . ConfigureDetectSingleKeyRemapUI ( keyStackPanel ) ;
2020-03-27 23:38:58 +08:00
// Show the dialog
detectRemapKeyBox . ShowAsync ( ) ;
}