-
-
Notifications
You must be signed in to change notification settings - Fork 501
/
MainWindow.xaml.cs
175 lines (149 loc) · 5.62 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using EarTrumpet.Extensions;
using EarTrumpet.Services;
using EarTrumpet.ViewModels;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace EarTrumpet
{
public partial class MainWindow
{
private readonly AudioMixerViewModel _viewModel;
private readonly TrayIcon _trayIcon;
public MainWindow()
{
InitializeComponent();
_viewModel = new AudioMixerViewModel();
_trayIcon = new TrayIcon();
_trayIcon.Invoked += TrayIcon_Invoked;
DataContext = _viewModel;
CreateAndHideWindow();
// Move keyboard focus to the first element. Disabled this since it is ugly but not sure invisible
// visuals are preferrable.
// Activated += (s,e) => MoveFocus(new TraversalRequest(FocusNavigationDirection.First));
SourceInitialized += (s, e) => UpdateTheme();
}
private void CreateAndHideWindow()
{
// Ensure the Win32 and WPF windows are created to fix first show issues with DPI Scaling
Opacity = 0;
Show();
Hide();
Opacity = 1;
}
void TrayIcon_Invoked()
{
if (this.IsWindowVisible())
{
this.HideWithAnimation();
}
else
{
_viewModel.Refresh();
UpdateTheme();
UpdateWindowPosition();
this.ShowwithAnimation();
}
}
private void Window_Deactivated(object sender, EventArgs e)
{
this.HideWithAnimation();
}
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape)
{
this.HideWithAnimation();
}
}
private void Slider_TouchDown(object sender, TouchEventArgs e)
{
VisualStateManager.GoToState((FrameworkElement)sender, "Pressed", true);
var slider = (Slider)sender;
slider.SetPositionByControlPoint(e.GetTouchPoint(slider).Position);
slider.CaptureTouch(e.TouchDevice);
e.Handled = true;
}
private void Slider_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
VisualStateManager.GoToState((FrameworkElement)sender, "Pressed", true);
var slider = (Slider)sender;
slider.SetPositionByControlPoint(e.GetPosition(slider));
slider.CaptureMouse();
e.Handled = true;
}
}
private void Slider_TouchUp(object sender, TouchEventArgs e)
{
VisualStateManager.GoToState((FrameworkElement)sender, "Normal", true);
var slider = (Slider)sender;
slider.ReleaseTouchCapture(e.TouchDevice);
e.Handled = true;
}
private void Slider_MouseUp(object sender, MouseButtonEventArgs e)
{
var slider = (Slider)sender;
if (slider.IsMouseCaptured)
{
// If the point is outside of the control, clear the hover state.
Rect rcSlider = new Rect(0, 0, slider.ActualWidth, slider.ActualHeight);
if (!rcSlider.Contains(e.GetPosition(slider)))
{
VisualStateManager.GoToState((FrameworkElement)sender, "Normal", true);
}
((Slider)sender).ReleaseMouseCapture();
e.Handled = true;
}
}
private void Slider_TouchMove(object sender, TouchEventArgs e)
{
var slider = (Slider)sender;
if (slider.AreAnyTouchesCaptured)
{
slider.SetPositionByControlPoint(e.GetTouchPoint(slider).Position);
e.Handled = true;
}
}
private void Slider_MouseMove(object sender, MouseEventArgs e)
{
var slider = (Slider)sender;
if (slider.IsMouseCaptured)
{
slider.SetPositionByControlPoint(e.GetPosition(slider));
}
}
private void Slider_MouseWheel(object sender, MouseWheelEventArgs e)
{
var slider = (Slider)sender;
var amount = Math.Sign(e.Delta) * 2.0;
slider.ChangePositionByAmount(amount);
e.Handled = true;
}
private void UpdateTheme()
{
// Call UpdateTheme before UpdateWindowPosition in case sizes change with the theme.
ThemeService.UpdateThemeResources(Resources);
if (ThemeService.IsWindowTransparencyEnabled)
{
this.EnableBlur();
}
else
{
this.DisableBlur();
}
}
private void UpdateWindowPosition()
{
LayoutRoot.UpdateLayout();
LayoutRoot.Measure(new Size(double.PositiveInfinity, MaxHeight));
Height = LayoutRoot.DesiredSize.Height;
var taskbarScreenWorkArea = TaskbarService.TaskbarScreen.WorkingArea;
var taskbarPosition = TaskbarService.TaskbarPosition;
Left = (taskbarPosition == TaskbarPosition.Left) ? (taskbarScreenWorkArea.Left / this.DpiWidthFactor()) : (taskbarScreenWorkArea.Right / this.DpiWidthFactor()) - Width;
Top = (taskbarPosition == TaskbarPosition.Top) ? (taskbarScreenWorkArea.Top / this.DpiHeightFactor()) : (taskbarScreenWorkArea.Bottom / this.DpiHeightFactor()) - Height;
}
}
}