misu

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 6  →  ?path2? @ 7
/trunk/misu/MisuMainForm.cs
@@ -1,23 +1,18 @@
using System;
using System.ComponentModel;
using System.Configuration;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks.Dataflow;
using System.Windows.Forms;
using AutoUpdaterDotNET;
using Configuration;
using Gma.System.MouseKeyHook;
using misu.Properties;
 
namespace misu
{
public partial class MisuMainForm : Form
{
#region Public Enums, Properties and Fields
 
public Settings Settings { get; set; }
 
#endregion
 
#region Private Delegates, Events, Enums, Properties, Indexers and Fields
 
private BufferBlock<KeyEventArgs> PressedKeysBufferBlock { get; }
@@ -39,23 +34,19 @@
InitializeComponent();
AutoUpdater.Start("http://misu.grimore.org/update/update.xml");
 
PressedKeysBufferBlock = new BufferBlock<KeyEventArgs>(new DataflowBlockOptions {EnsureOrdered = true});
 
// Load the settings.
var serialization = Serialization.Deserialize(@"Settings.xml");
switch (serialization)
// Upgrade settings if required.
if (!ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).HasFile)
{
case SerializationSuccess<Settings> serializationSuccess:
Settings = serializationSuccess.Result;
break;
case SerializationFailure _:
Settings = new Settings();
break;
Settings.Default.Upgrade();
}
 
Settings.PropertyChanged += Settings_PropertyChanged;
Settings.Binding.PropertyChanged += Binding_PropertyChanged;
// Bind to settings changed event.
Settings.Default.SettingsLoaded += Default_SettingsLoaded;
Settings.Default.SettingsSaving += Default_SettingsSaving;
Settings.Default.PropertyChanged += Default_PropertyChanged;
 
PressedKeysBufferBlock = new BufferBlock<KeyEventArgs>(new DataflowBlockOptions {EnsureOrdered = true});
 
// Hook to global events.
GlobalHook = Hook.GlobalEvents();
 
@@ -70,10 +61,10 @@
GlobalHook.MouseMoveExt += GlobalHook_BlockMouse;
GlobalHook.MouseDownExt += GlobalHook_BlockMouse;
 
if (Settings.Binding.Keys.Any())
if (Settings.Default.Keys != null)
{
Notify("Shortcut is bound.",
$"Press {Settings.Binding} to toggle locking.");
$"Press {string.Join(" ", Settings.Default.Keys.OfType<string>())} to toggle locking.");
}
}
 
@@ -81,11 +72,19 @@
 
#region Event Handlers
 
private async void Binding_PropertyChanged(object sender, PropertyChangedEventArgs e)
private static void Default_SettingsSaving(object sender, CancelEventArgs e)
{
await Settings.Serialize(@"Settings.xml");
}
 
private static void Default_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
Settings.Default.Save();
}
 
private static void Default_SettingsLoaded(object sender, SettingsLoadedEventArgs e)
{
}
 
private void GlobalHook_BlockMouse(object sender, MouseEventExtArgs e)
{
// Do not perform any operations while the binding form is visible.
@@ -94,18 +93,12 @@
return;
}
 
if (_lock && Settings.Mouse)
if (_lock && Settings.Default.Mouse)
{
e.Handled = true;
}
}
 
private async void Settings_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
// Serialize the settings to the configuration file on form closing.
await Settings.Serialize(@"Settings.xml");
}
 
private void GlobalHook_KeyUp(object sender, KeyEventArgs e)
{
// Do not perform any operations while the binding form is visible.
@@ -119,33 +112,35 @@
return;
}
 
if (Settings.Binding == null)
if (Settings.Default.Keys == null)
{
return;
}
 
if (Settings.Binding.Keys.SequenceContains(keys.Select(key => key.KeyCode)))
if (!Settings.Default.Keys.OfType<string>().SequenceEqual(keys.Select(key => key.KeyCode.ToString())))
{
_lock = !_lock;
return;
}
 
// Lock the cursor in position.
switch (_lock)
{
case true:
Cursor.Clip = new Rectangle(Cursor.Position, new Size(1, 1));
break;
default:
Cursor.Clip = Screen.PrimaryScreen.Bounds;
break;
}
_lock = !_lock;
 
// Dismiss the current keypress to avoid typing the key.
e.Handled = true;
e.SuppressKeyPress = true;
// Lock the cursor in position.
switch (_lock)
{
case true:
Cursor.Clip = new Rectangle(Cursor.Position, new Size(1, 1));
break;
default:
Cursor.Clip = Screen.PrimaryScreen.Bounds;
break;
}
 
Notify($"Mouse and Keyboard are {(_lock ? "locked" : "unlocked")}.",
$"Press {Settings.Binding} to toggle mouse and keyboard locking.");
}
// Dismiss the current keypress to avoid typing the key.
e.Handled = true;
e.SuppressKeyPress = true;
 
Notify($"Mouse and Keyboard are {(_lock ? "locked" : "unlocked")}.",
$"Press {string.Join(" ", Settings.Default.Keys.OfType<string>())} to toggle mouse and keyboard locking.");
}
 
private async void GlobalHook_KeyDown(object sender, KeyEventArgs e)
@@ -156,7 +151,7 @@
return;
}
 
if (_lock && Settings.Keyboard)
if (_lock && Settings.Default.Keyboard)
{
e.SuppressKeyPress = true;
e.Handled = true;
@@ -179,14 +174,14 @@
 
private void OnContextMenuLaunchOnBootChanged(object sender, EventArgs e)
{
Settings.LaunchOnBoot = ((ToolStripMenuItem) sender).Checked;
Settings.Default.LaunchOnBoot = ((ToolStripMenuItem) sender).Checked;
 
Utilities.LaunchOnBootSet(Settings.LaunchOnBoot);
Utilities.LaunchOnBootSet(Settings.Default.LaunchOnBoot);
}
 
private void OnContextMenuBindKeyClick(object sender, EventArgs e)
{
BindKeyForm = new BindKeyForm(GlobalHook, Settings);
BindKeyForm = new BindKeyForm(GlobalHook);
BindKeyForm.KeyBound += BindKeyForm_KeyBound;
BindKeyForm.Closing += BindKeyForm_Closing;
BindKeyForm.Show();
@@ -201,11 +196,7 @@
private async void BindKeyForm_KeyBound(object sender, KeyBoundEventArgs e)
{
Notify("Shortcut key is bound.",
$"Press {e.Binding} to toggle between locked and unlocked keyboard and mouse.");
 
Settings.Binding.Keys.AddRange(e.Binding.Keys);
 
await Settings.Serialize(@"Settings.xml");
$"Press {string.Join(" ", Settings.Default.Keys.OfType<string>())} to toggle between locked and unlocked keyboard and mouse.");
}
 
private void KeyboardToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
@@ -212,7 +203,7 @@
{
var item = (ToolStripMenuItem) sender;
 
Settings.Keyboard = item.Checked;
Settings.Default.Keyboard = item.Checked;
}
 
private void MouseToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
@@ -219,17 +210,17 @@
{
var item = (ToolStripMenuItem) sender;
 
Settings.Mouse = item.Checked;
Settings.Default.Mouse = item.Checked;
}
 
private void ContextMenuStrip_Opening(object sender, CancelEventArgs e)
{
// Apply the settings to the menu items.
launchOnBootToolStripMenuItem.Checked = Settings.LaunchOnBoot;
Utilities.LaunchOnBootSet(Settings.LaunchOnBoot);
launchOnBootToolStripMenuItem.Checked = Settings.Default.LaunchOnBoot;
Utilities.LaunchOnBootSet(Settings.Default.LaunchOnBoot);
 
mouseToolStripMenuItem.Checked = Settings.Mouse;
keyboardToolStripMenuItem.Checked = Settings.Keyboard;
mouseToolStripMenuItem.Checked = Settings.Default.Mouse;
keyboardToolStripMenuItem.Checked = Settings.Default.Keyboard;
}
 
#endregion