Skip to content

Commit

Permalink
Merge pull request #4 from dustincjensen/customized-key-bindings
Browse files Browse the repository at this point in the history
Customized key bindings
  • Loading branch information
dustincjensen committed Oct 30, 2016
2 parents 3c7bbd4 + f1a5ae5 commit 6852275
Show file tree
Hide file tree
Showing 13 changed files with 698 additions and 77 deletions.
39 changes: 39 additions & 0 deletions Snapper/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,45 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="Snapper.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
</startup>
<userSettings>
<Snapper.Properties.Settings>
<setting name="SwapMonitor" serializeAs="String">
<value />
</setting>
<setting name="BottomLeft" serializeAs="String">
<value />
</setting>
<setting name="Bottom" serializeAs="String">
<value />
</setting>
<setting name="BottomRight" serializeAs="String">
<value />
</setting>
<setting name="Left" serializeAs="String">
<value />
</setting>
<setting name="Mid" serializeAs="String">
<value />
</setting>
<setting name="Right" serializeAs="String">
<value />
</setting>
<setting name="TopLeft" serializeAs="String">
<value />
</setting>
<setting name="Top" serializeAs="String">
<value />
</setting>
<setting name="TopRight" serializeAs="String">
<value />
</setting>
</Snapper.Properties.Settings>
</userSettings>
</configuration>
17 changes: 17 additions & 0 deletions Snapper/Common/ViewModelBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace Snapper.Common
{
public class ViewModelBase : INotifyPropertyChanged
{
protected ViewModelBase() {}

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
22 changes: 22 additions & 0 deletions Snapper/KeyBindEnum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Snapper
{
public enum KeyBindEnum
{
SwapMonitor,
BottomLeft,
Bottom,
BottomRight,
Left,
Mid,
Right,
TopLeft,
Top,
TopRight
}
}
126 changes: 126 additions & 0 deletions Snapper/KeyBindSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace Snapper
{
public static class KeyBindSettings
{
public static Tuple<ModifierKeys, Key> GetSetting(KeyBindEnum purpose)
{
var keyCombo = (string)Properties.Settings.Default[purpose.ToString()];
var keyStrings = keyCombo.Split(',');
var possibleKey = Key.None;
var modifierKeys = ModifierKeys.None;

foreach (var keyString in keyStrings)
{
Key tmpKey;
ModifierKeys tmpModifier;

Enum.TryParse(keyString, out tmpKey);
Enum.TryParse(keyString, out tmpModifier);

if (tmpKey != Key.None)
{
possibleKey = tmpKey;
}
else if (tmpModifier != ModifierKeys.None)
{
modifierKeys = modifierKeys | tmpModifier;
}
}

return (possibleKey == Key.None && modifierKeys == ModifierKeys.None)
// Return a default for this keybind.
? _GetDefaultKeyBind(purpose)
// Or the specified one in the settings file
: new Tuple<ModifierKeys, Key>(modifierKeys, possibleKey);
}

private static Tuple<ModifierKeys, Key> _GetDefaultKeyBind(KeyBindEnum purpose)
{
Key key;

switch (purpose)
{
case KeyBindEnum.BottomLeft:
key = Key.NumPad1;
break;
case KeyBindEnum.Bottom:
key = Key.NumPad2;
break;
case KeyBindEnum.BottomRight:
key = Key.NumPad3;
break;
case KeyBindEnum.Left:
key = Key.NumPad4;
break;
case KeyBindEnum.Mid:
key = Key.NumPad5;
break;
case KeyBindEnum.Right:
key = Key.NumPad6;
break;
case KeyBindEnum.TopLeft:
key = Key.NumPad7;
break;
case KeyBindEnum.Top:
key = Key.NumPad8;
break;
case KeyBindEnum.TopRight:
key = Key.NumPad9;
break;
case KeyBindEnum.SwapMonitor:
key = Key.NumPad0;
break;
default:
key = Key.NumPad0;
break;
}

return new Tuple<ModifierKeys, Key>(ModifierKeys.Control | ModifierKeys.Alt, key);
}

public static void SetSetting(string value, KeyBindEnum purpose)
{
switch (purpose)
{
case KeyBindEnum.BottomLeft:
Properties.Settings.Default.BottomLeft = value;
break;
case KeyBindEnum.Bottom:
Properties.Settings.Default.Bottom = value;
break;
case KeyBindEnum.BottomRight:
Properties.Settings.Default.BottomRight = value;
break;
case KeyBindEnum.Left:
Properties.Settings.Default.Left = value;
break;
case KeyBindEnum.Mid:
Properties.Settings.Default.Mid = value;
break;
case KeyBindEnum.Right:
Properties.Settings.Default.Right = value;
break;
case KeyBindEnum.TopLeft:
Properties.Settings.Default.TopLeft = value;
break;
case KeyBindEnum.Top:
Properties.Settings.Default.Top = value;
break;
case KeyBindEnum.TopRight:
Properties.Settings.Default.TopRight = value;
break;
case KeyBindEnum.SwapMonitor:
Properties.Settings.Default.SwapMonitor = value;
break;
}
Properties.Settings.Default.Save();
}
}
}
28 changes: 24 additions & 4 deletions Snapper/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,44 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Snapper"
xmlns:views="clr-namespace:Snapper.Views"
mc:Ignorable="d"
Title="Snapper"
Height="350" Width="525"
MinHeight="250" MinWidth="450"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Height="400" Width="600"
MinHeight="400" MinWidth="600"
Background="#222">

<Grid VerticalAlignment="Stretch" Margin="5">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>

<StackPanel Grid.Row="0" VerticalAlignment="Center">
<TextBlock Foreground="White" FontSize="32" HorizontalAlignment="Center">
Welcome to Snapper
Welcome to Snapper
</TextBlock>
<TextBlock Foreground="LightGray" HorizontalAlignment="Center">
To get started, use Ctrl + Alt and the number pad.
To get started, use Ctrl + Alt and the number pad.
</TextBlock>
</StackPanel>

<Grid Grid.Row="1" Margin="20,20">
<ItemsControl HorizontalAlignment="Stretch" ItemsSource="{Binding KeyBindingsList, UpdateSourceTrigger=PropertyChanged}" Grid.IsSharedSizeScope="True">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="2"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<ContentControl Content="{Binding}" Margin="25"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>

</Grid>
</Window>
79 changes: 41 additions & 38 deletions Snapper/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
Expand All @@ -14,14 +16,18 @@
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Snapper.Views;
using KeyEventArgs = System.Windows.Input.KeyEventArgs;

namespace Snapper
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
public partial class MainWindow : Window, INotifyPropertyChanged
{
private List<KeyBindings> _keyBindingsList;

public MainWindow()
{
InitializeComponent();
Expand All @@ -37,59 +43,49 @@ protected override void OnStateChanged(EventArgs args)
base.OnStateChanged(args);
}


private Hotkeys.GlobalHotKey _numpad1_HotKey;
private Hotkeys.GlobalHotKey _numpad2_HotKey;
private Hotkeys.GlobalHotKey _numpad3_HotKey;
private Hotkeys.GlobalHotKey _numpad4_HotKey;
private Hotkeys.GlobalHotKey _numpad5_HotKey;
private Hotkeys.GlobalHotKey _numpad6_HotKey;
private Hotkeys.GlobalHotKey _numpad7_HotKey;
private Hotkeys.GlobalHotKey _numpad8_HotKey;
private Hotkeys.GlobalHotKey _numpad9_HotKey;
private Hotkeys.GlobalHotKey _numpad0_HotKey;

private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
{
_LoadHotkeys();
_SetupNotifyIcon();

WindowState = WindowState.Minimized;
//WindowState = WindowState.Minimized;
}

private void _LoadHotkeys()
public List<KeyBindings> KeyBindingsList
{
_numpad1_HotKey = new Hotkeys.GlobalHotKey(ModifierKeys.Control | ModifierKeys.Alt, Key.NumPad1, this);
_numpad2_HotKey = new Hotkeys.GlobalHotKey(ModifierKeys.Control | ModifierKeys.Alt, Key.NumPad2, this);
_numpad3_HotKey = new Hotkeys.GlobalHotKey(ModifierKeys.Control | ModifierKeys.Alt, Key.NumPad3, this);
_numpad4_HotKey = new Hotkeys.GlobalHotKey(ModifierKeys.Control | ModifierKeys.Alt, Key.NumPad4, this);
_numpad5_HotKey = new Hotkeys.GlobalHotKey(ModifierKeys.Control | ModifierKeys.Alt, Key.NumPad5, this);
_numpad6_HotKey = new Hotkeys.GlobalHotKey(ModifierKeys.Control | ModifierKeys.Alt, Key.NumPad6, this);
_numpad7_HotKey = new Hotkeys.GlobalHotKey(ModifierKeys.Control | ModifierKeys.Alt, Key.NumPad7, this);
_numpad8_HotKey = new Hotkeys.GlobalHotKey(ModifierKeys.Control | ModifierKeys.Alt, Key.NumPad8, this);
_numpad9_HotKey = new Hotkeys.GlobalHotKey(ModifierKeys.Control | ModifierKeys.Alt, Key.NumPad9, this);
_numpad1_HotKey.HotKeyPressed += _MoveWindowOnHotKeyPressed;
_numpad2_HotKey.HotKeyPressed += _MoveWindowOnHotKeyPressed;
_numpad3_HotKey.HotKeyPressed += _MoveWindowOnHotKeyPressed;
_numpad4_HotKey.HotKeyPressed += _MoveWindowOnHotKeyPressed;
_numpad5_HotKey.HotKeyPressed += _MoveWindowOnHotKeyPressed;
_numpad6_HotKey.HotKeyPressed += _MoveWindowOnHotKeyPressed;
_numpad7_HotKey.HotKeyPressed += _MoveWindowOnHotKeyPressed;
_numpad8_HotKey.HotKeyPressed += _MoveWindowOnHotKeyPressed;
_numpad9_HotKey.HotKeyPressed += _MoveWindowOnHotKeyPressed;
get { return _keyBindingsList; }
private set
{
_keyBindingsList = value;
OnPropertyChanged();
}
}

_numpad0_HotKey = new Hotkeys.GlobalHotKey(ModifierKeys.Control | ModifierKeys.Alt, Key.NumPad0, this);
_numpad0_HotKey.HotKeyPressed += _ToggleWindowMonitorOnHotKeyPressed;
private void _LoadHotkeys()
{
KeyBindingsList = new List<KeyBindings>
{
new KeyBindings {Label = "Swap Monitors", HotKeyAction = ToggleWindowMonitorOnHotKeyPressed, Purpose = KeyBindEnum.SwapMonitor},
new KeyBindings {Label = "Bottom Left", HotKeyAction = MoveWindowOnHotKeyPressed, Purpose = KeyBindEnum.BottomLeft},
new KeyBindings {Label = "Bottom", HotKeyAction = MoveWindowOnHotKeyPressed, Purpose = KeyBindEnum.Bottom},
new KeyBindings {Label = "Bottom Right", HotKeyAction = MoveWindowOnHotKeyPressed, Purpose = KeyBindEnum.BottomRight},
new KeyBindings {Label = "Left", HotKeyAction = MoveWindowOnHotKeyPressed, Purpose = KeyBindEnum.Left},
new KeyBindings {Label = "Mid", HotKeyAction = MoveWindowOnHotKeyPressed, Purpose = KeyBindEnum.Mid},
new KeyBindings {Label = "Right", HotKeyAction = MoveWindowOnHotKeyPressed, Purpose = KeyBindEnum.Right},
new KeyBindings {Label = "Top Left", HotKeyAction = MoveWindowOnHotKeyPressed, Purpose = KeyBindEnum.TopLeft},
new KeyBindings {Label = "Top", HotKeyAction = MoveWindowOnHotKeyPressed, Purpose = KeyBindEnum.Top},
new KeyBindings {Label = "Top Right", HotKeyAction = MoveWindowOnHotKeyPressed, Purpose = KeyBindEnum.TopRight}
};
}

private void _ToggleWindowMonitorOnHotKeyPressed(Hotkeys.GlobalHotKey globalHotKey)
public void ToggleWindowMonitorOnHotKeyPressed(KeyBindEnum purpose)
{
MoveActiveWindow.ToggleWindowMonitor();
}

private void _MoveWindowOnHotKeyPressed(Hotkeys.GlobalHotKey globalHotKey)
public void MoveWindowOnHotKeyPressed(KeyBindEnum purpose)
{
MoveActiveWindow.MoveWindow(globalHotKey);
MoveActiveWindow.MoveWindow(purpose);
}

/// <summary>
Expand Down Expand Up @@ -125,5 +121,12 @@ private void _SetupNotifyIcon()
WindowState = WindowState.Normal;
};
}

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Loading

0 comments on commit 6852275

Please sign in to comment.