PrivacyGlass/PrivacyGlass/TrayApp.cs
Trevor Hall 4ff988deaf finished
2026-03-04 01:20:50 -05:00

100 lines
2.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace PrivacyGlass
{
public class TrayApp : ApplicationContext
{
const uint MOD_ALT = 0x0001;
const uint MOD_CONTROL = 0x0002;
const uint HOTKEY_ID = 1;
private NotifyIcon tray;
private List<BlurOverlay> overlays = new List<BlurOverlay>();
//private const int HOTKEY_ID = 100;
[DllImport("user32.dll")]
static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
[DllImport("user32.dll")]
static extern bool UnregisterHotKey(IntPtr hWnd, int id);
private void RegisterGlobalHotKey()
{
// Ctrl + Alt + Space
RegisterHotKey(
IntPtr.Zero,
(int)HOTKEY_ID,
MOD_CONTROL | MOD_ALT,
(uint)Keys.Space
);
}
public TrayApp()
{
CreateTrayIcon();
CreateOverlays();
RegisterGlobalHotKey();
Application.AddMessageFilter(new HotkeyFilter(Toggle));
}
private void CreateTrayIcon()
{
var assembly = Assembly.GetExecutingAssembly();
using (Stream s = assembly.GetManifestResourceStream("PrivacyGlass.Resources.privacyglass.ico"))
{
Icon trayicon = new Icon(s);
tray = new NotifyIcon()
{
Text = "PrivacyCurtain",
Icon = trayicon,
Visible = true,
ContextMenuStrip = BuildMenu()
};
}
}
private ContextMenuStrip BuildMenu()
{
var menu = new ContextMenuStrip();
menu.Items.Add("Toggle Now", null, (_, __) => Toggle());
menu.Items.Add("Exit", null, Exit);
return menu;
}
private void CreateOverlays()
{
foreach (var screen in Screen.AllScreens)
{
BlurOverlay ov = new BlurOverlay(screen.Bounds);
ov.TintColor = Color.Black;
ov.TintOpacity = 0; // 0255, higher = darker
overlays.Add(ov);
}
}
private void Toggle() =>
overlays.ForEach(o => o.Toggle());
private void Exit(object sender, EventArgs e)
{
UnregisterHotKey(IntPtr.Zero, (int)HOTKEY_ID);
tray.Visible = false;
Application.Exit();
}
}
}