misu – Diff between revs 6 and 7

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 6 Rev 7
Line 1... Line 1...
1 using System; 1 using System;
2 using System.ComponentModel; 2 using System.ComponentModel;
-   3 using System.Configuration;
3 using System.Drawing; 4 using System.Drawing;
4 using System.Linq; 5 using System.Linq;
5 using System.Threading.Tasks.Dataflow; 6 using System.Threading.Tasks.Dataflow;
6 using System.Windows.Forms; 7 using System.Windows.Forms;
7 using AutoUpdaterDotNET; 8 using AutoUpdaterDotNET;
8 using Configuration; -  
9 using Gma.System.MouseKeyHook; 9 using Gma.System.MouseKeyHook;
-   10 using misu.Properties;
Line 10... Line 11...
10   11  
11 namespace misu 12 namespace misu
12 { 13 {
13 public partial class MisuMainForm : Form 14 public partial class MisuMainForm : Form
14 { -  
15 #region Public Enums, Properties and Fields -  
16   -  
17 public Settings Settings { get; set; } -  
18   -  
19 #endregion -  
20   15 {
Line 21... Line 16...
21 #region Private Delegates, Events, Enums, Properties, Indexers and Fields 16 #region Private Delegates, Events, Enums, Properties, Indexers and Fields
Line 22... Line 17...
22   17  
Line 37... Line 32...
37 public MisuMainForm() 32 public MisuMainForm()
38 { 33 {
39 InitializeComponent(); 34 InitializeComponent();
40 AutoUpdater.Start("http://misu.grimore.org/update/update.xml"); 35 AutoUpdater.Start("http://misu.grimore.org/update/update.xml");
Line 41... Line -...
41   -  
42 PressedKeysBufferBlock = new BufferBlock<KeyEventArgs>(new DataflowBlockOptions {EnsureOrdered = true}); -  
43   36  
44 // Load the settings. 37 // Upgrade settings if required.
45 var serialization = Serialization.Deserialize(@"Settings.xml"); -  
46 switch (serialization) 38 if (!ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).HasFile)
47 { -  
48 case SerializationSuccess<Settings> serializationSuccess: -  
49 Settings = serializationSuccess.Result; -  
50 break; -  
51 case SerializationFailure _: 39 {
52 Settings = new Settings(); -  
53 break; 40 Settings.Default.Upgrade();
Line -... Line 41...
-   41 }
54 } 42  
-   43 // Bind to settings changed event.
55   44 Settings.Default.SettingsLoaded += Default_SettingsLoaded;
-   45 Settings.Default.SettingsSaving += Default_SettingsSaving;
-   46 Settings.Default.PropertyChanged += Default_PropertyChanged;
Line 56... Line 47...
56 Settings.PropertyChanged += Settings_PropertyChanged; 47  
57 Settings.Binding.PropertyChanged += Binding_PropertyChanged; 48 PressedKeysBufferBlock = new BufferBlock<KeyEventArgs>(new DataflowBlockOptions {EnsureOrdered = true});
Line 58... Line 49...
58   49  
Line 68... Line 59...
68 GlobalHook.MouseDragStartedExt += GlobalHook_BlockMouse; 59 GlobalHook.MouseDragStartedExt += GlobalHook_BlockMouse;
69 GlobalHook.MouseWheelExt += GlobalHook_BlockMouse; 60 GlobalHook.MouseWheelExt += GlobalHook_BlockMouse;
70 GlobalHook.MouseMoveExt += GlobalHook_BlockMouse; 61 GlobalHook.MouseMoveExt += GlobalHook_BlockMouse;
71 GlobalHook.MouseDownExt += GlobalHook_BlockMouse; 62 GlobalHook.MouseDownExt += GlobalHook_BlockMouse;
Line 72... Line 63...
72   63  
73 if (Settings.Binding.Keys.Any()) 64 if (Settings.Default.Keys != null)
74 { 65 {
75 Notify("Shortcut is bound.", 66 Notify("Shortcut is bound.",
76 $"Press {Settings.Binding} to toggle locking."); 67 $"Press {string.Join(" ", Settings.Default.Keys.OfType<string>())} to toggle locking.");
77 } 68 }
Line 78... Line 69...
78 } 69 }
Line 79... Line 70...
79   70  
Line -... Line 71...
-   71 #endregion
-   72  
-   73 #region Event Handlers
-   74  
80 #endregion 75 private static void Default_SettingsSaving(object sender, CancelEventArgs e)
-   76 {
-   77 }
-   78  
-   79 private static void Default_PropertyChanged(object sender, PropertyChangedEventArgs e)
-   80 {
81   81 Settings.Default.Save();
82 #region Event Handlers -  
83   82 }
Line 84... Line 83...
84 private async void Binding_PropertyChanged(object sender, PropertyChangedEventArgs e) 83  
85 { 84 private static void Default_SettingsLoaded(object sender, SettingsLoadedEventArgs e)
86 await Settings.Serialize(@"Settings.xml"); 85 {
87 } 86 }
88   87  
89 private void GlobalHook_BlockMouse(object sender, MouseEventExtArgs e) 88 private void GlobalHook_BlockMouse(object sender, MouseEventExtArgs e)
90 { 89 {
Line 91... Line 90...
91 // Do not perform any operations while the binding form is visible. 90 // Do not perform any operations while the binding form is visible.
92 if (BindKeyForm != null && BindKeyForm.Visible) 91 if (BindKeyForm != null && BindKeyForm.Visible)
93 { 92 {
94 return; 93 return;
95 } 94 }
Line 96... Line -...
96   -  
97 if (_lock && Settings.Mouse) -  
98 { -  
99 e.Handled = true; -  
100 } -  
101 } -  
102   95  
103 private async void Settings_PropertyChanged(object sender, PropertyChangedEventArgs e) 96 if (_lock && Settings.Default.Mouse)
104 { 97 {
105 // Serialize the settings to the configuration file on form closing. 98 e.Handled = true;
106 await Settings.Serialize(@"Settings.xml"); 99 }
Line 117... Line 110...
117 if (!PressedKeysBufferBlock.TryReceiveAll(out var keys)) 110 if (!PressedKeysBufferBlock.TryReceiveAll(out var keys))
118 { 111 {
119 return; 112 return;
120 } 113 }
Line 121... Line 114...
121   114  
122 if (Settings.Binding == null) 115 if (Settings.Default.Keys == null)
123 { 116 {
124 return; 117 return;
Line 125... Line 118...
125 } 118 }
126   119  
127 if (Settings.Binding.Keys.SequenceContains(keys.Select(key => key.KeyCode))) -  
128 { -  
129 _lock = !_lock; -  
130   -  
131 // Lock the cursor in position. 120 if (!Settings.Default.Keys.OfType<string>().SequenceEqual(keys.Select(key => key.KeyCode.ToString())))
132 switch (_lock) -  
133 { -  
134 case true: -  
135 Cursor.Clip = new Rectangle(Cursor.Position, new Size(1, 1)); -  
136 break; -  
137 default: -  
138 Cursor.Clip = Screen.PrimaryScreen.Bounds; 121 {
Line 139... Line -...
139 break; -  
140 } 122 return;
141   -  
Line -... Line 123...
-   123 }
-   124  
-   125 _lock = !_lock;
-   126  
142 // Dismiss the current keypress to avoid typing the key. 127 // Lock the cursor in position.
-   128 switch (_lock)
-   129 {
143 e.Handled = true; 130 case true:
-   131 Cursor.Clip = new Rectangle(Cursor.Position, new Size(1, 1));
144 e.SuppressKeyPress = true; 132 break;
-   133 default:
-   134 Cursor.Clip = Screen.PrimaryScreen.Bounds;
-   135 break;
-   136 }
-   137  
-   138 // Dismiss the current keypress to avoid typing the key.
-   139 e.Handled = true;
145   140 e.SuppressKeyPress = true;
Line 146... Line 141...
146 Notify($"Mouse and Keyboard are {(_lock ? "locked" : "unlocked")}.", 141  
147 $"Press {Settings.Binding} to toggle mouse and keyboard locking."); 142 Notify($"Mouse and Keyboard are {(_lock ? "locked" : "unlocked")}.",
148 } 143 $"Press {string.Join(" ", Settings.Default.Keys.OfType<string>())} to toggle mouse and keyboard locking.");
149 } 144 }
150   145  
151 private async void GlobalHook_KeyDown(object sender, KeyEventArgs e) 146 private async void GlobalHook_KeyDown(object sender, KeyEventArgs e)
152 { 147 {
Line 153... Line 148...
153 // Do not perform any operations while the binding form is visible. 148 // Do not perform any operations while the binding form is visible.
154 if (BindKeyForm != null && BindKeyForm.Visible) 149 if (BindKeyForm != null && BindKeyForm.Visible)
155 { 150 {
156 return; 151 return;
157 } 152 }
Line 177... Line 172...
177 Application.Exit(); 172 Application.Exit();
178 } 173 }
Line 179... Line 174...
179   174  
180 private void OnContextMenuLaunchOnBootChanged(object sender, EventArgs e) 175 private void OnContextMenuLaunchOnBootChanged(object sender, EventArgs e)
181 { 176 {
Line 182... Line 177...
182 Settings.LaunchOnBoot = ((ToolStripMenuItem) sender).Checked; 177 Settings.Default.LaunchOnBoot = ((ToolStripMenuItem) sender).Checked;
183   178  
Line 184... Line 179...
184 Utilities.LaunchOnBootSet(Settings.LaunchOnBoot); 179 Utilities.LaunchOnBootSet(Settings.Default.LaunchOnBoot);
185 } 180 }
186   181  
187 private void OnContextMenuBindKeyClick(object sender, EventArgs e) 182 private void OnContextMenuBindKeyClick(object sender, EventArgs e)
188 { 183 {
189 BindKeyForm = new BindKeyForm(GlobalHook, Settings); 184 BindKeyForm = new BindKeyForm(GlobalHook);
190 BindKeyForm.KeyBound += BindKeyForm_KeyBound; 185 BindKeyForm.KeyBound += BindKeyForm_KeyBound;
Line 199... Line 194...
199 } 194 }
Line 200... Line 195...
200   195  
201 private async void BindKeyForm_KeyBound(object sender, KeyBoundEventArgs e) 196 private async void BindKeyForm_KeyBound(object sender, KeyBoundEventArgs e)
202 { 197 {
203 Notify("Shortcut key is bound.", 198 Notify("Shortcut key is bound.",
204 $"Press {e.Binding} to toggle between locked and unlocked keyboard and mouse."); -  
205   -  
206 Settings.Binding.Keys.AddRange(e.Binding.Keys); -  
207   -  
208 await Settings.Serialize(@"Settings.xml"); 199 $"Press {string.Join(" ", Settings.Default.Keys.OfType<string>())} to toggle between locked and unlocked keyboard and mouse.");
Line 209... Line 200...
209 } 200 }
210   201  
211 private void KeyboardToolStripMenuItem_CheckedChanged(object sender, EventArgs e) 202 private void KeyboardToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
Line 212... Line 203...
212 { 203 {
213 var item = (ToolStripMenuItem) sender; 204 var item = (ToolStripMenuItem) sender;
Line 214... Line 205...
214   205  
215 Settings.Keyboard = item.Checked; 206 Settings.Default.Keyboard = item.Checked;
216 } 207 }
Line 217... Line 208...
217   208  
218 private void MouseToolStripMenuItem_CheckedChanged(object sender, EventArgs e) 209 private void MouseToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
Line 219... Line 210...
219 { 210 {
220 var item = (ToolStripMenuItem) sender; 211 var item = (ToolStripMenuItem) sender;
221   212  
222 Settings.Mouse = item.Checked; 213 Settings.Default.Mouse = item.Checked;
223 } 214 }
Line 224... Line 215...
224   215  
225 private void ContextMenuStrip_Opening(object sender, CancelEventArgs e) 216 private void ContextMenuStrip_Opening(object sender, CancelEventArgs e)
226 { 217 {
Line 227... Line 218...
227 // Apply the settings to the menu items. 218 // Apply the settings to the menu items.
Line 228... Line 219...
228 launchOnBootToolStripMenuItem.Checked = Settings.LaunchOnBoot; 219 launchOnBootToolStripMenuItem.Checked = Settings.Default.LaunchOnBoot;