-
Notifications
You must be signed in to change notification settings - Fork 3
/
ChromecastPanel.cs
143 lines (111 loc) · 4.19 KB
/
ChromecastPanel.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
using GoogleCast;
using GoogleCast.Channels;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MusicBeePlugin
{
public partial class ChromecastPanel : Form
{
private Color backgroundColor { get; set; }
public IMediaChannel ChromecastMediaChannel { get; set; } = null;
//public Sender ChromecastSender { get; set; } = null;
public bool Disconnect { get; set; } = false;
public ChromecastPanel(Color color)
{
backgroundColor = color;
InitializeComponent();
}
private async void ChromecastPanel_Load(object sender, EventArgs e)
{
var contrastColor = ContrastColor(backgroundColor);
this.BackColor = backgroundColor;
this.closeText.ForeColor = contrastColor;
this.devicesText.ForeColor = contrastColor;
IEnumerable<IReceiver> receiver = await new DeviceLocator().FindReceiversAsync();
if (receiver.Count() == 0 )
{
Button b = new Button
{
BackColor = Color.Transparent,
ForeColor = ContrastColor(backgroundColor),
FlatStyle = FlatStyle.Flat,
Text = "No Devices",
AutoSize = false,
Width = 200,
};
b.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Transparent;
b.FlatAppearance.BorderColor = backgroundColor;
flowLayoutPanel1.Controls.Add(b);
}
foreach (var x in receiver)
{
Button b = new Button
{
BackColor = Color.Transparent,
ForeColor = ContrastColor(backgroundColor),
FlatStyle = FlatStyle.Flat,
Text = x.FriendlyName,
AutoSize = false,
Width = 200,
};
b.Click +=
new EventHandler((s, e2) => MyButtonHandler(s, e, receiver));
b.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Transparent;
b.FlatAppearance.BorderColor = backgroundColor;
flowLayoutPanel1.Controls.Add(b);
}
}
async void MyButtonHandler(object sender, EventArgs e, IEnumerable<IReceiver> devices)
{
var sender2 = new Sender();
IReceiver device = null;
foreach (var x in devices)
{
if (x.FriendlyName == (sender as Button).Text)
{
device = x;
}
}
if (device != null)
{
//Connect to the device
await sender2.ConnectAsync(device);
//Launch the media reciever app
var mediaChannel = sender2.GetChannel<IMediaChannel>();
await sender2.LaunchAsync(mediaChannel);
//ChromecastSender = sender2;
ChromecastMediaChannel = mediaChannel;
this.FormClosing -= ChromecastSelection_FormClosing;
this.Close();
}
}
private void label1_Click(object sender, EventArgs e)
{
this.FormClosing -= ChromecastSelection_FormClosing;
this.Close();
}
Color ContrastColor(Color color)
{
int d = 0;
// Counting the perceptive luminance - human eye favors green color...
double luminance = (0.299 * color.R + 0.587 * color.G + 0.114 * color.B) / 255;
if (luminance > 0.5)
d = 0; // bright colors - black font
else
d = 255; // dark colors - white font
return Color.FromArgb(d, d, d);
}
private void ChromecastSelection_FormClosing(object sender, FormClosingEventArgs e)
{
this.FormClosing -= ChromecastSelection_FormClosing;
this.Close();
}
}
}