Mark van Renswoude
5e0513fc81
Fixed ExceptionEvent not invoking to main thread Bit more exception handling while shutting down
98 lines
2.9 KiB
C#
98 lines
2.9 KiB
C#
using System;
|
|
using System.Diagnostics.Contracts;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Windows.Forms;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace IPCamAppBar
|
|
{
|
|
public partial class MainForm : Form
|
|
{
|
|
private readonly Config config;
|
|
private readonly AppBar appBar;
|
|
|
|
|
|
public MainForm()
|
|
{
|
|
InitializeComponent();
|
|
|
|
// ReSharper disable once PossibleNullReferenceException
|
|
// ReSharper disable once AssignNullToNotNullAttribute
|
|
using (var file = File.OpenText(Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "config.json")))
|
|
{
|
|
var serializer = new JsonSerializer();
|
|
config = (Config)serializer.Deserialize(file, typeof(Config));
|
|
}
|
|
|
|
var monitor = GetMonitor(config.AppBar.Monitor);
|
|
var position = GetAppBarPosition(config.AppBar.Side);
|
|
|
|
appBar = new AppBar(Handle);
|
|
appBar.SetPosition(monitor, position, config.AppBar.Size);
|
|
|
|
config.Cameras?.ForEach(AddCamera);
|
|
}
|
|
|
|
|
|
protected override void WndProc(ref Message m)
|
|
{
|
|
appBar?.HandleMessage(m);
|
|
base.WndProc(ref m);
|
|
}
|
|
|
|
|
|
private static Screen GetMonitor(int number)
|
|
{
|
|
// Since I could not find any way to get the numbers as assigned in the
|
|
// display control panel, we'll just order them left-to-right.
|
|
var orderedScreens = Screen.AllScreens.OrderBy(s => s.Bounds.Left).ToList();
|
|
|
|
// 0 means primary. Yeah. Totally not losing geek-creds for using a 1-based
|
|
// index here. No sir. 'tis a feature!
|
|
return number > 0 && number <= orderedScreens.Count
|
|
? orderedScreens[number - 1]
|
|
: Screen.PrimaryScreen;
|
|
}
|
|
|
|
|
|
private static AppBarPosition GetAppBarPosition(ConfigSide side)
|
|
{
|
|
switch (side)
|
|
{
|
|
case ConfigSide.Top: return AppBarPosition.Top;
|
|
case ConfigSide.Bottom: return AppBarPosition.Bottom;
|
|
case ConfigSide.Left: return AppBarPosition.Left;
|
|
case ConfigSide.Right: return AppBarPosition.Right;
|
|
default:
|
|
throw new ArgumentOutOfRangeException(nameof(side), side, @"Invalid side value");
|
|
}
|
|
}
|
|
|
|
|
|
private void AddCamera(ConfigCamera camera)
|
|
{
|
|
var view = new CameraView(camera.URL)
|
|
{
|
|
Width = camera.Width,
|
|
Height = camera.Height
|
|
};
|
|
|
|
CameraViewContainer.Controls.Add(view);
|
|
}
|
|
|
|
|
|
private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
|
|
{
|
|
appBar.Dispose();
|
|
}
|
|
|
|
|
|
private void CloseToolStripMenuItem_Click(object sender, EventArgs e)
|
|
{
|
|
Close();
|
|
}
|
|
}
|
|
}
|