117 lines
4.4 KiB
C#
117 lines
4.4 KiB
C#
using System;
|
|
using System.Drawing;
|
|
using System.Windows.Forms;
|
|
|
|
namespace Kniip.Infrastructure;
|
|
|
|
internal sealed class TrayIcon : IDisposable
|
|
{
|
|
private readonly NotifyIcon _icon;
|
|
private const string RecordText = "Screencast opnemen (MP4)...";
|
|
|
|
private readonly ToolStripMenuItem _clipboardItem;
|
|
private readonly ToolStripMenuItem _pauseItem;
|
|
private readonly ToolStripMenuItem _snipItem;
|
|
private readonly ToolStripMenuItem _windowItem;
|
|
private readonly ToolStripMenuItem _recordItem;
|
|
private readonly ToolStripMenuItem _gifItem;
|
|
|
|
public event Action? OpenFromClipboardRequested;
|
|
public event Action? SnipRequested;
|
|
public event Action? WindowSnipRequested;
|
|
public event Action? RecordRequested;
|
|
public event Action? GifRequested;
|
|
public event Action? OpenFileRequested;
|
|
public event Action? SettingsRequested;
|
|
public event Action? ExitRequested;
|
|
public event Action<bool>? PauseChanged;
|
|
|
|
public TrayIcon()
|
|
{
|
|
var menu = new ContextMenuStrip();
|
|
_clipboardItem = new ToolStripMenuItem("Bewerken vanaf klembord", null, (_, _) => OpenFromClipboardRequested?.Invoke())
|
|
{
|
|
Font = new Font(SystemFonts.MenuFont ?? Control.DefaultFont, FontStyle.Bold)
|
|
};
|
|
menu.Items.Add(_clipboardItem);
|
|
menu.Items.Add("Afbeelding openen...", null, (_, _) => OpenFileRequested?.Invoke());
|
|
menu.Items.Add(new ToolStripSeparator());
|
|
_snipItem = new ToolStripMenuItem("Rechthoek knippen", null, (_, _) => SnipRequested?.Invoke());
|
|
menu.Items.Add(_snipItem);
|
|
_windowItem = new ToolStripMenuItem("Venster knippen", null, (_, _) => WindowSnipRequested?.Invoke());
|
|
menu.Items.Add(_windowItem);
|
|
_recordItem = new ToolStripMenuItem(RecordText, null, (_, _) => RecordRequested?.Invoke());
|
|
menu.Items.Add(_recordItem);
|
|
_gifItem = new ToolStripMenuItem("Screencast opnemen (GIF)...", null, (_, _) => GifRequested?.Invoke());
|
|
menu.Items.Add(_gifItem);
|
|
menu.Items.Add(new ToolStripSeparator());
|
|
|
|
_pauseItem = new ToolStripMenuItem("Knipsels niet automatisch oppikken") { CheckOnClick = true };
|
|
_pauseItem.CheckedChanged += (_, _) => PauseChanged?.Invoke(_pauseItem.Checked);
|
|
menu.Items.Add(_pauseItem);
|
|
menu.Items.Add("Instellingen...", null, (_, _) => SettingsRequested?.Invoke());
|
|
menu.Items.Add(new ToolStripSeparator());
|
|
menu.Items.Add("Afsluiten", null, (_, _) => ExitRequested?.Invoke());
|
|
|
|
_icon = new NotifyIcon
|
|
{
|
|
Icon = LoadIcon(),
|
|
Text = IdleText,
|
|
ContextMenuStrip = menu,
|
|
Visible = true
|
|
};
|
|
_icon.MouseClick += (_, e) =>
|
|
{
|
|
if (e.Button == MouseButtons.Left)
|
|
{
|
|
OpenFromClipboardRequested?.Invoke();
|
|
}
|
|
};
|
|
}
|
|
|
|
public bool IsPaused => _pauseItem.Checked;
|
|
|
|
/// <summary>Toont de sneltoetsen naast de menu-items; null = geen sneltoets.</summary>
|
|
public void SetHotkeys(string? snip, string? window, string? record, string? gif, string? clipboard)
|
|
{
|
|
_clipboardItem.ShortcutKeyDisplayString = clipboard;
|
|
_snipItem.ShortcutKeyDisplayString = snip;
|
|
_windowItem.ShortcutKeyDisplayString = window;
|
|
_recordItem.ShortcutKeyDisplayString = record;
|
|
_gifItem.ShortcutKeyDisplayString = gif;
|
|
}
|
|
|
|
/// <summary>Menutekst voor de opnameknop; null zet de standaardtekst terug.</summary>
|
|
public void SetRecordItem(string? text, bool recording)
|
|
{
|
|
_recordItem.Text = text ?? RecordText;
|
|
_gifItem.Visible = text == null;
|
|
_icon.Text = recording ? $"{AppIdentity.DisplayName} - opname loopt" : IdleText;
|
|
}
|
|
|
|
private static string IdleText => AppIdentity.IsTest
|
|
? $"{AppIdentity.DisplayName} {Kniip.Update.UpdateChecker.CurrentVersion}"
|
|
: "kniip - annotaties voor het Knipprogramma";
|
|
|
|
public void ShowBalloon(string title, string text) =>
|
|
_icon.ShowBalloonTip(4000, title, text, ToolTipIcon.Info);
|
|
|
|
private static Icon LoadIcon()
|
|
{
|
|
var info = System.Windows.Application.GetResourceStream(new Uri("pack://application:,,,/AppIcon.ico"));
|
|
if (info == null)
|
|
{
|
|
return SystemIcons.Application;
|
|
}
|
|
|
|
using var stream = info.Stream;
|
|
return new Icon(stream, SystemInformation.SmallIconSize);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_icon.Visible = false;
|
|
_icon.Dispose();
|
|
}
|
|
}
|