tesses.broadcast/Tesses.Broadcast.Eto/Class1.cs

96 lines
3.3 KiB
C#

/*
A simple udp broadcast library
Copyright (C) 2024 Mike Nolan
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
I am reachable at tesses@tesses.net
*/
using System;
using System.Threading;
using Eto.Forms;
using Tesses.Broadcast;
namespace Tesses.Broadcast.Eto
{
public sealed class BroadcastDialog : Dialog<string>
{
public BroadcastDialog(string serviceName,int port=6942)
{
CancellationTokenSource src=new CancellationTokenSource();
Title="Pick a device";
ListBox listBox=new ListBox();
TextBox url=new TextBox(){PlaceholderText="Url"};
Button cancel=new Button((sender,e)=>{
if(!src.IsCancellationRequested)
{
src.Cancel();
src.Dispose();
}
Close("");
}){Text="Cancel"};
Button ok=new Button((sender,e)=>{
if(!src.IsCancellationRequested)
{
src.Cancel();
src.Dispose();
}
Close(url.Text);
}){Text="OK"};
Shown += async(sender,e)=>{
await foreach(var item in BroadcastClient.ScanAsync(serviceName,port,src.Token))
{
listBox.Items.Add(item.DeviceName,item.ServiceUrl);
}
};
Closing += (sender,e)=>{
if(!src.IsCancellationRequested)
{
src.Cancel();
src.Dispose();
}
};
DynamicLayout layout=new DynamicLayout();
layout.BeginVertical();
layout.BeginHorizontal();
layout.Add(listBox,true,true);
layout.EndBeginHorizontal();
DynamicLayout layout2=new DynamicLayout();
layout2.BeginVertical();
layout2.BeginHorizontal();
layout2.Add(new Label(){Text="Url: "});
layout2.Add(url,true);
layout2.EndHorizontal();
layout2.EndBeginVertical();
layout2.BeginHorizontal();
layout2.Add(null,true);
layout2.Add(ok);
layout2.Add(cancel);
layout2.EndHorizontal();
layout2.EndVertical();
layout.Add(layout2,true);
layout.EndHorizontal();
layout.EndVertical();
Content=layout;
listBox.SelectedKeyChanged += (sender,e)=>{
url.Text = listBox.SelectedKey;
};
Width=320;
Height=480;
}
}
}