-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
135 lines (113 loc) · 4.42 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using System.Windows.Forms;
using System.Collections.Generic;
using SpotlightDimmer.Models;
using System;
using System.Windows.Interop;
using System.Windows.Media.Imaging;
namespace SpotlightDimmer
{
public partial class MainWindow : Window
{
public DimmerSettings _dimmerSettings;
public WindowsEventsManager _dimmerStateManager;
public DimmerState _state;
private Dictionary<string, Window> _dimmerWindowsByScreen;
private NotifyIcon _notifyIcon;
public MainWindow()
{
InitializeComponent();
SetApplicationIcon();
BuildTheViewModel();
SetMinimizeToTrayOptions();
CreateTheDimmerWindows();
Closing += OnClosing;
}
private void BuildTheViewModel()
{
_state = new DimmerState();
_dimmerStateManager = new WindowsEventsManager(_state);
_dimmerSettings = new DimmerSettings(_state);
DataContext = _state;
}
protected void CreateTheDimmerWindows()
{
_dimmerWindowsByScreen ??= new Dictionary<string, Window>();
foreach (var dimmerWindow in _dimmerWindowsByScreen.Values)
dimmerWindow.Hide();
foreach (var screen in Screen.AllScreens)
{
if (!_dimmerWindowsByScreen.ContainsKey(screen.DeviceName))
{
var dimmerWindow = new DimmerWindow(screen, _state, this);
_dimmerWindowsByScreen.Add(screen.DeviceName, dimmerWindow);
dimmerWindow.Show();
}
else
{
_dimmerWindowsByScreen[screen.DeviceName].Show();
}
}
}
private void SetApplicationIcon()
{
var icon = GetSpotlightDimmerIcon();
var handle = icon.Handle;
this.Icon = Imaging.CreateBitmapSourceFromHIcon(handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
}
private void SetMinimizeToTrayOptions()
{
// Create the NotifyIcon object
_notifyIcon = new NotifyIcon();
_notifyIcon.Icon = GetSpotlightDimmerIcon();
_notifyIcon.Text = "Spotlight Dimmer";
_notifyIcon.BalloonTipText = "The application is still running on the system tray. Click here to open it again";
_notifyIcon.BalloonTipTitle = "Spotlight Dimmer";
_notifyIcon.BalloonTipIcon = ToolTipIcon.Info;
_notifyIcon.Visible = true;
// Handle the StateChanged event of the Window
this.StateChanged += MainWindow_StateChanged;
// Handle the DoubleClick event of the NotifyIcon
_notifyIcon.Click += NotifyIcon_Click;
}
private void SaveSettingsButton_Click(object? sender, RoutedEventArgs e)
{
_dimmerSettings.SaveSettings();
}
private void Window_Activated(object? sender, EventArgs e)
{
this.Activate();
this.Focus();
}
private void MainWindow_StateChanged(object? sender, System.EventArgs e)
{
if (WindowState == WindowState.Minimized && _state.MinimizeToTray)
{
Hide();
_notifyIcon.ShowBalloonTip((int)TimeSpan.FromSeconds(5).TotalMilliseconds);
}
}
private void NotifyIcon_Click(object? sender, System.EventArgs e)
{
Show();
this.Activate();
this.Focus();
WindowState = WindowState.Normal;
}
private void DebugInfoTextBox_TextChanged(object? sender, System.Windows.Controls.TextChangedEventArgs e)
{
DebugInfoTextBox.ScrollToEnd();
}
private void OnClosing(object? sender, CancelEventArgs e)
{
foreach (var childWindow in _dimmerWindowsByScreen.Values)
childWindow.Close();
_dimmerStateManager.Dispose();
_notifyIcon.Dispose();
}
private System.Drawing.Icon GetSpotlightDimmerIcon()
{
using var stream = typeof(MainWindow).Assembly.GetManifestResourceStream("SpotlightDimmer.ico");
return new System.Drawing.Icon(stream);
}
}
}