-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWindow.cs
165 lines (144 loc) · 5.35 KB
/
Window.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
using System;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
namespace AVRTray
{
public partial class MainForm : Form
{
private readonly NotifyIcon notifyIcon;
private readonly AVR AVR;
private Size defaultSize;
private FormBorderStyle defaultStyle;
private int timeLeft = 10;
public MainForm()
{
AVR = new AVR();
InitializeComponent();
defaultSize = Size;
defaultStyle = FormBorderStyle;
notifyIcon = new()
{
ContextMenuStrip = new ContextMenuStrip(),
Icon = Properties.Resources.AppIcon,
Text = "AVRTray",
Visible = true
};
notifyIcon.ContextMenuStrip.Items.Add("Toggle Power", null, (s, e) => AVR.TogglePower());
notifyIcon.ContextMenuStrip.Items.Add(new ToolStripSeparator());
notifyIcon.ContextMenuStrip.Items.Add("Exit", null, (s, e) => Application.Exit());
notifyIcon.DoubleClick += Window_Show;
connectionTimer.Interval = 1000;
connectionTimer.Start();
}
/// <summary>
/// Minimise to notification tray
/// </summary>
void Window_Hide()
{
Size = new Size(0, 0);
FormBorderStyle = FormBorderStyle.None;
ShowInTaskbar = false;
}
/// <summary>
/// Unminimize from notification tray
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void Window_Show(object sender, EventArgs e)
{
Size = defaultSize;
FormBorderStyle = defaultStyle;
WindowState = FormWindowState.Normal;
ShowInTaskbar = true;
}
/// <summary>
/// Set the 'currentValue' property of a Control element without triggering its ValueChanged event
/// </summary>
/// <param name="control">The UI element to update</param>
/// <param name="value">The new value to set</param>
private void SetPrivateControlValue(Control control, decimal value)
{
if (control == null) throw new ArgumentNullException(nameof(control));
var currentValueField = control.GetType().GetField("currentValue", BindingFlags.Instance | BindingFlags.NonPublic);
if (currentValueField != null)
{
currentValueField.SetValue(control, value);
control.Text = value.ToString();
}
}
#region UI Events
void Form_Load(object sender, EventArgs e)
{
// Set volume bar and field to current AVR volume
// Note, this currently triggers a single send event setting the AVR volume to the
// just received volume. Not a real problem, but should be fixed at some point
volumeBar.Value = AVR.GetVolume();
SetPrivateControlValue(volumeField, volumeBar.Value);
// Load custom sources into select box and set active
sourceSelect.Items.AddRange(AVR.Settings.Sources.Keys.ToArray());
sourceSelect.SelectedItem = AVR.GetSource();
// Set IP address label
ipLabel.Text += $" {AVR.Settings.IP}:{AVR.Settings.Port}";
}
private void Form_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
Window_Hide();
} else
{
Window_Show(sender, e);
}
}
private void Form_Closing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
WindowState = FormWindowState.Minimized;
Window_Hide();
}
private void volumeBar_ValueChanged(object sender, EventArgs e)
{
AVR.Write("MV" + volumeBar.Value);
SetPrivateControlValue(volumeField, volumeBar.Value);
}
private void volumeField_ValueChanged(object sender, EventArgs e)
{
AVR.Write("MV" + (int)volumeField.Value);
SetPrivateControlValue(volumeBar, (int)volumeField.Value);
}
// Toggle buttons
private void togglePower_Click(object sender, EventArgs e) => AVR.TogglePower();
private void buttonMute_Click(object sender, EventArgs e) => AVR.ToggleMute();
private void sourceSelect_Changed(object sender, EventArgs e)
{
AVR.SetSource(sourceSelect.SelectedItem.ToString());
}
private void refreshButton_Click(object sender, EventArgs e)
{
SetPrivateControlValue(volumeBar, AVR.GetVolume());
SetPrivateControlValue(volumeField, volumeBar.Value);
sourceSelect.SelectedItem = AVR.GetSource();
}
#endregion
private void connectionTimer_Tick(object sender, EventArgs e)
{
timeLeft--;
if (timeLeft <= 0)
{
AVR.Disconnect();
connectionTimer.Stop();
timeLeft = 10;
}
}
private void MainForm_Click(object sender, EventArgs e)
{
if (!AVR.IsConnected())
{
AVR.Connect();
connectionTimer.Start();
}
}
}
}