Skip to content

Commit 4f6662a

Browse files
committed
Code clean
1 parent 35901e2 commit 4f6662a

12 files changed

+153
-154
lines changed

src/Examples/Avalonia/App.axaml.cs

+34-35
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,46 @@
1-
using System;
2-
using System.Reactive.Linq;
31
using Avalonia;
42
using Avalonia.Controls.ApplicationLifetimes;
53
using Avalonia.Markup.Xaml;
64
using GlobalHotKeys;
5+
using System;
6+
using System.Reactive.Linq;
77

88
namespace AvaloniaApp
99
{
10-
public class App : Application
11-
{
12-
public override void Initialize()
13-
{
14-
AvaloniaXamlLoader.Load(this);
15-
}
16-
17-
public override void OnFrameworkInitializationCompleted()
10+
public class App : Application
1811
{
19-
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
20-
{
21-
var hotKeyManager = new HotKeyManager();
22-
var hotKeySubscription = hotKeyManager.Register(VirtualKeyCode.KEY_1, Modifiers.Shift);
23-
24-
desktop.Exit += (sender, args) =>
12+
public override void Initialize()
2513
{
26-
hotKeySubscription.Dispose();
27-
hotKeyManager.Dispose();
28-
};
29-
30-
var mainViewModel = new MainViewModel();
31-
32-
hotKeyManager.HotKeyPressed
33-
.ObserveOn(Avalonia.Threading.AvaloniaScheduler.Instance)
34-
.Subscribe(hotKey => mainViewModel.Text += $"HotKey: Id={hotKey.Id}, Key={hotKey.Key}, Modifiers={hotKey.Modifiers}{Environment.NewLine}");
14+
AvaloniaXamlLoader.Load(this);
15+
}
3516

36-
desktop.MainWindow =
37-
new MainWindow
38-
{
39-
DataContext = mainViewModel
40-
};
41-
}
42-
43-
44-
base.OnFrameworkInitializationCompleted();
17+
public override void OnFrameworkInitializationCompleted()
18+
{
19+
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
20+
{
21+
var hotKeyManager = new HotKeyManager();
22+
var hotKeySubscription = hotKeyManager.Register(VirtualKeyCode.KEY_1, Modifiers.Shift);
23+
24+
desktop.Exit += (sender, args) =>
25+
{
26+
hotKeySubscription.Dispose();
27+
hotKeyManager.Dispose();
28+
};
29+
30+
var mainViewModel = new MainViewModel();
31+
32+
hotKeyManager.HotKeyPressed
33+
.ObserveOn(Avalonia.Threading.AvaloniaScheduler.Instance)
34+
.Subscribe(hotKey => mainViewModel.Text += $"HotKey: Id={hotKey.Id}, Key={hotKey.Key}, Modifiers={hotKey.Modifiers}{Environment.NewLine}");
35+
36+
desktop.MainWindow =
37+
new MainWindow
38+
{
39+
DataContext = mainViewModel
40+
};
41+
}
42+
43+
base.OnFrameworkInitializationCompleted();
44+
}
4545
}
46-
}
4746
}

src/Examples/Avalonia/MainViewModel.cs

+22-19
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,30 @@
33

44
namespace AvaloniaApp
55
{
6-
public class MainViewModel : INotifyPropertyChanged
7-
{
8-
#region Text
9-
private string _text = string.Empty;
10-
public string Text
6+
public class MainViewModel : INotifyPropertyChanged
117
{
12-
get => _text;
13-
set
14-
{
15-
if (_text != value)
8+
#region Text
9+
10+
private string _text = string.Empty;
11+
12+
public string Text
1613
{
17-
_text = value;
18-
OnPropertyChanged(nameof(Text));
14+
get => _text;
15+
set
16+
{
17+
if (_text != value)
18+
{
19+
_text = value;
20+
OnPropertyChanged(nameof(Text));
21+
}
22+
}
1923
}
20-
}
21-
}
22-
#endregion
2324

25+
#endregion Text
2426

25-
public event PropertyChangedEventHandler? PropertyChanged;
26-
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) =>
27-
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
28-
}
29-
}
27+
public event PropertyChangedEventHandler? PropertyChanged;
28+
29+
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) =>
30+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
31+
}
32+
}

src/Examples/Avalonia/Program.cs

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
using System;
2-
using Avalonia;
3-
using Avalonia.Controls;
4-
using Avalonia.Controls.ApplicationLifetimes;
1+
using Avalonia;
52

63
namespace AvaloniaApp
74
{
8-
class Program
5+
internal class Program
96
{
107
// Initialization code. Don't use any Avalonia, third-party APIs or any
118
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
@@ -19,4 +16,4 @@ public static AppBuilder BuildAvaloniaApp()
1916
.UsePlatformDetect()
2017
.LogToTrace();
2118
}
22-
}
19+
}

src/Examples/Console/Program.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using System;
2-
using GlobalHotKeys;
1+
using GlobalHotKeys;
2+
using System;
33

44
void HotKeyPressed(HotKey hotKey) =>
55
Console.WriteLine($"HotKey Pressed: Id = {hotKey.Id}, Key = {hotKey.Key}, Modifiers = {hotKey.Modifiers}");
@@ -10,4 +10,4 @@ void HotKeyPressed(HotKey hotKey) =>
1010
using var ctrl1 = hotKeyManager.Register(VirtualKeyCode.KEY_1, Modifiers.Control);
1111

1212
Console.WriteLine("Listening for HotKeys...");
13-
Console.ReadLine();
13+
Console.ReadLine();

src/Examples/WinForms/Form1.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
namespace WinForms
44
{
5-
public partial class Form1 : Form
6-
{
7-
public Form1()
5+
public partial class Form1 : Form
86
{
9-
InitializeComponent();
10-
}
7+
public Form1()
8+
{
9+
InitializeComponent();
10+
}
1111

12-
public void AppendText(string text) =>
13-
this.textBox1.AppendText(text);
14-
}
12+
public void AppendText(string text) =>
13+
this.textBox1.AppendText(text);
14+
}
1515
}

src/Examples/WinForms/Program.cs

+21-21
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
1+
using GlobalHotKeys;
12
using System;
23
using System.Reactive.Linq;
34
using System.Threading;
45
using System.Windows.Forms;
5-
using GlobalHotKeys;
66

77
namespace WinForms
88
{
9-
static class Program
10-
{
11-
/// <summary>
12-
/// The main entry point for the application.
13-
/// </summary>
14-
[STAThread]
15-
static void Main()
9+
internal static class Program
1610
{
17-
Application.SetHighDpiMode(HighDpiMode.SystemAware);
18-
Application.EnableVisualStyles();
19-
Application.SetCompatibleTextRenderingDefault(false);
11+
/// <summary>
12+
/// The main entry point for the application.
13+
/// </summary>
14+
[STAThread]
15+
private static void Main()
16+
{
17+
Application.SetHighDpiMode(HighDpiMode.SystemAware);
18+
Application.EnableVisualStyles();
19+
Application.SetCompatibleTextRenderingDefault(false);
2020

21-
using var hotKeyManager = new HotKeyManager();
22-
using var shift1 = hotKeyManager.Register(VirtualKeyCode.KEY_1, Modifiers.Shift);
21+
using var hotKeyManager = new HotKeyManager();
22+
using var shift1 = hotKeyManager.Register(VirtualKeyCode.KEY_1, Modifiers.Shift);
2323

24-
var form = new Form1();
25-
using var subscription = hotKeyManager.HotKeyPressed
26-
.ObserveOn(SynchronizationContext.Current)
27-
.Subscribe(hotKey =>
28-
form.AppendText($"HotKey: Id = {hotKey.Id}, Key = {hotKey.Key}, Modifiers = {hotKey.Modifiers}{Environment.NewLine}")
29-
);
24+
var form = new Form1();
25+
using var subscription = hotKeyManager.HotKeyPressed
26+
.ObserveOn(SynchronizationContext.Current)
27+
.Subscribe(hotKey =>
28+
form.AppendText($"HotKey: Id = {hotKey.Id}, Key = {hotKey.Key}, Modifiers = {hotKey.Modifiers}{Environment.NewLine}")
29+
);
3030

31-
Application.Run(form);
31+
Application.Run(form);
32+
}
3233
}
33-
}
3434
}

src/Examples/Wpf/App.xaml

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@
44
Startup="App_OnStartup"
55
Exit="App_OnExit">
66
<Application.Resources>
7-
87
</Application.Resources>
9-
</Application>
8+
</Application>

src/Examples/Wpf/App.xaml.cs

+29-29
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
1-
using System;
1+
using GlobalHotKeys;
2+
using System;
23
using System.Reactive.Linq;
34
using System.Threading;
45
using System.Windows;
5-
using GlobalHotKeys;
66

77
namespace Wpf
88
{
9-
/// <summary>
10-
/// Interaction logic for App.xaml
11-
/// </summary>
12-
public partial class App : Application
13-
{
14-
HotKeyManager _hotKeyManager;
15-
IDisposable _shift1;
16-
IDisposable _subscription;
17-
18-
void App_OnStartup(object sender, StartupEventArgs e)
9+
/// <summary>
10+
/// Interaction logic for App.xaml
11+
/// </summary>
12+
public partial class App : Application
1913
{
20-
_hotKeyManager = new HotKeyManager();
21-
_shift1 = _hotKeyManager.Register(VirtualKeyCode.KEY_1, Modifiers.Shift);
14+
private HotKeyManager _hotKeyManager;
15+
private IDisposable _shift1;
16+
private IDisposable _subscription;
2217

23-
var mainViewModel = new MainViewModel();
24-
this.MainWindow = new MainWindow { DataContext = mainViewModel };
25-
this.MainWindow.Show();
26-
27-
_subscription = _hotKeyManager.HotKeyPressed
28-
.ObserveOn(SynchronizationContext.Current)
29-
.Subscribe(hotKey =>
30-
mainViewModel.Text += $"hotKey: Id = {hotKey.Id}, Key = {hotKey.Key}, Modifiers = {hotKey.Modifiers}{Environment.NewLine}");
31-
}
18+
private void App_OnStartup(object sender, StartupEventArgs e)
19+
{
20+
_hotKeyManager = new HotKeyManager();
21+
_shift1 = _hotKeyManager.Register(VirtualKeyCode.KEY_1, Modifiers.Shift);
3222

33-
private void App_OnExit(object sender, ExitEventArgs e)
34-
{
35-
_subscription.Dispose();
36-
_shift1.Dispose();
37-
((IDisposable)_hotKeyManager).Dispose();
23+
var mainViewModel = new MainViewModel();
24+
this.MainWindow = new MainWindow { DataContext = mainViewModel };
25+
this.MainWindow.Show();
26+
27+
_subscription = _hotKeyManager.HotKeyPressed
28+
.ObserveOn(SynchronizationContext.Current)
29+
.Subscribe(hotKey =>
30+
mainViewModel.Text += $"hotKey: Id = {hotKey.Id}, Key = {hotKey.Key}, Modifiers = {hotKey.Modifiers}{Environment.NewLine}");
31+
}
32+
33+
private void App_OnExit(object sender, ExitEventArgs e)
34+
{
35+
_subscription.Dispose();
36+
_shift1.Dispose();
37+
((IDisposable)_hotKeyManager).Dispose();
38+
}
3839
}
39-
}
4040
}

src/Examples/Wpf/AssemblyInfo.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
[assembly: ThemeInfo(
44
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
5-
//(used if a resource is not found in the page,
6-
// or application resource dictionaries)
5+
//(used if a resource is not found in the page,
6+
// or application resource dictionaries)
77
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
8-
//(used if a resource is not found in the page,
9-
// app, or any theme specific resource dictionaries)
8+
//(used if a resource is not found in the page,
9+
// app, or any theme specific resource dictionaries)
1010
)]

src/Examples/Wpf/MainViewModel.cs

+18-17
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,26 @@
33

44
namespace Wpf
55
{
6-
public class MainViewModel : INotifyPropertyChanged
7-
{
8-
string _text;
9-
public string Text
6+
public class MainViewModel : INotifyPropertyChanged
107
{
11-
get => _text;
12-
set
13-
{
14-
if (this._text != value)
8+
private string _text;
9+
10+
public string Text
1511
{
16-
this._text = value;
17-
this.OnPropertyChanged(nameof(Text));
12+
get => _text;
13+
set
14+
{
15+
if (this._text != value)
16+
{
17+
this._text = value;
18+
this.OnPropertyChanged(nameof(Text));
19+
}
20+
}
1821
}
19-
}
20-
}
21-
22-
public event PropertyChangedEventHandler PropertyChanged;
2322

24-
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) =>
25-
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
26-
}
23+
public event PropertyChangedEventHandler PropertyChanged;
24+
25+
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) =>
26+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
27+
}
2728
}

0 commit comments

Comments
 (0)