FireWalletCosmos/FireWalletLite/NotifyForm.cs

249 lines
7.5 KiB
C#
Raw Normal View History

using System.Diagnostics;
2023-07-05 16:44:34 +10:00
using System.Runtime.InteropServices;
2023-07-15 22:17:49 +10:00
namespace FireWallet;
public partial class NotifyForm : Form
2023-07-05 16:44:34 +10:00
{
2023-07-15 22:17:49 +10:00
private bool allowClose = true;
private readonly string altLink;
2023-07-05 16:44:34 +10:00
2023-07-15 22:17:49 +10:00
private readonly string dir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
"\\FireWallet\\";
2023-07-05 16:44:34 +10:00
2023-07-15 22:17:49 +10:00
private readonly bool Linkcopy;
private Dictionary<string, string> theme;
2023-07-05 16:44:34 +10:00
2023-07-15 22:17:49 +10:00
public NotifyForm(string Message)
{
InitializeComponent();
labelmessage.Text = Message;
altLink = "";
}
2023-07-05 16:44:34 +10:00
2023-07-15 22:17:49 +10:00
public NotifyForm(string Message, string altText, string altLink)
{
InitializeComponent();
labelmessage.Text = Message;
buttonALT.Text = altText;
buttonALT.Visible = true;
this.altLink = altLink;
buttonOK.Focus();
Linkcopy = false;
}
2023-07-05 16:44:34 +10:00
2023-07-15 22:17:49 +10:00
public NotifyForm(string Message, bool allowClose)
{
InitializeComponent();
labelmessage.Text = Message;
buttonOK.Focus();
Linkcopy = false;
buttonOK.Visible = allowClose;
allowClose = allowClose;
}
2023-07-05 16:44:34 +10:00
2023-07-15 22:17:49 +10:00
public NotifyForm(string Message, string altText, string altLink, bool Linkcopy)
{
InitializeComponent();
labelmessage.Text = Message;
buttonALT.Text = altText;
buttonALT.Visible = true;
this.altLink = altLink;
buttonOK.Focus();
this.Linkcopy = Linkcopy;
if (Linkcopy)
// Small font to fix more data
labelmessage.Font = new Font(labelmessage.Font.FontFamily, 10);
}
2023-07-05 16:44:34 +10:00
2023-07-15 22:17:49 +10:00
public void CloseNotification()
{
Close();
}
2023-07-05 16:44:34 +10:00
2023-07-15 22:17:49 +10:00
private void NotifyForm_Load(object sender, EventArgs e)
{
UpdateTheme();
}
2023-07-05 16:44:34 +10:00
2023-07-15 22:17:49 +10:00
private void OK_Click(object sender, EventArgs e)
{
allowClose = true;
Close();
}
2023-07-05 16:44:34 +10:00
2023-07-15 22:17:49 +10:00
private void buttonALT_Click(object sender, EventArgs e)
{
if (Linkcopy)
{
// Copy link to clipboard
Clipboard.SetText(altLink);
return;
}
2023-07-05 16:44:34 +10:00
2023-07-15 22:17:49 +10:00
// Open link
var psi = new ProcessStartInfo
{
FileName = altLink,
UseShellExecute = true
};
Process.Start(psi);
}
2023-07-05 16:44:34 +10:00
2023-07-15 22:17:49 +10:00
private void NotifyForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (!allowClose) e.Cancel = true;
}
2023-07-05 16:44:34 +10:00
2023-07-15 22:17:49 +10:00
#region Theming
2023-07-05 16:44:34 +10:00
2023-07-15 22:17:49 +10:00
private void UpdateTheme()
{
// Check if file exists
if (!Directory.Exists(dir)) CreateConfig(dir);
if (!File.Exists(dir + "theme.txt")) CreateConfig(dir);
2023-07-05 16:44:34 +10:00
2023-07-15 22:17:49 +10:00
// Read file
var sr = new StreamReader(dir + "theme.txt");
theme = new Dictionary<string, string>();
while (!sr.EndOfStream)
2023-07-05 16:44:34 +10:00
{
2023-07-15 22:17:49 +10:00
var line = sr.ReadLine();
var split = line.Split(':');
theme.Add(split[0].Trim(), split[1].Trim());
2023-07-05 16:44:34 +10:00
}
2023-07-15 22:17:49 +10:00
sr.Dispose();
2023-07-05 16:44:34 +10:00
2023-07-15 22:17:49 +10:00
if (!theme.ContainsKey("background") || !theme.ContainsKey("background-alt") ||
!theme.ContainsKey("foreground") || !theme.ContainsKey("foreground-alt")) return;
2023-07-05 16:44:34 +10:00
2023-07-15 22:17:49 +10:00
// Apply theme
BackColor = ColorTranslator.FromHtml(theme["background"]);
2023-07-05 16:44:34 +10:00
2023-07-15 22:17:49 +10:00
// Foreground
ForeColor = ColorTranslator.FromHtml(theme["foreground"]);
2023-07-05 16:44:34 +10:00
2023-07-15 22:17:49 +10:00
// Need to specify this for each groupbox to override the black text
foreach (Control c in Controls) ThemeControl(c);
2023-07-05 16:44:34 +10:00
2023-07-15 22:17:49 +10:00
// Transparancy
applyTransparency(theme);
}
2023-07-05 16:44:34 +10:00
2023-07-15 22:17:49 +10:00
private void ThemeControl(Control c)
{
if (c.GetType() == typeof(GroupBox) || c.GetType() == typeof(Panel))
2023-07-05 16:44:34 +10:00
{
2023-07-15 22:17:49 +10:00
c.ForeColor = ColorTranslator.FromHtml(theme["foreground"]);
foreach (Control sub in c.Controls) ThemeControl(sub);
2023-07-05 16:44:34 +10:00
}
2023-07-15 22:17:49 +10:00
if (c.GetType() == typeof(TextBox) || c.GetType() == typeof(Button)
|| c.GetType() == typeof(ComboBox) || c.GetType() == typeof(StatusStrip))
2023-07-05 16:44:34 +10:00
{
2023-07-15 22:17:49 +10:00
c.ForeColor = ColorTranslator.FromHtml(theme["foreground-alt"]);
c.BackColor = ColorTranslator.FromHtml(theme["background-alt"]);
2023-07-05 16:44:34 +10:00
}
2023-07-15 22:17:49 +10:00
}
2023-07-05 16:44:34 +10:00
2023-07-15 22:17:49 +10:00
private void applyTransparency(Dictionary<string, string> theme)
{
if (theme.ContainsKey("transparent-mode"))
switch (theme["transparent-mode"])
2023-07-05 16:44:34 +10:00
{
2023-07-15 22:17:49 +10:00
case "mica":
var accent = new AccentPolicy { AccentState = AccentState.ACCENT_ENABLE_BLURBEHIND };
var accentStructSize = Marshal.SizeOf(accent);
var accentPtr = Marshal.AllocHGlobal(accentStructSize);
Marshal.StructureToPtr(accent, accentPtr, false);
var data = new WindowCompositionAttributeData
{
Attribute = WindowCompositionAttribute.WCA_ACCENT_POLICY,
SizeOfData = accentStructSize,
Data = accentPtr
};
User32.SetWindowCompositionAttribute(Handle, ref data);
Marshal.FreeHGlobal(accentPtr);
break;
case "key":
if (theme.ContainsKey("transparency-key"))
switch (theme["transparency-key"])
{
case "alt":
TransparencyKey = ColorTranslator.FromHtml(theme["background-alt"]);
break;
case "main":
TransparencyKey = ColorTranslator.FromHtml(theme["background"]);
break;
default:
TransparencyKey = ColorTranslator.FromHtml(theme["transparency-key"]);
break;
}
break;
case "percent":
if (theme.ContainsKey("transparency-percent"))
Opacity = Convert.ToDouble(theme["transparency-percent"]) / 100;
break;
2023-07-05 16:44:34 +10:00
}
2023-07-15 22:17:49 +10:00
}
2023-07-05 16:44:34 +10:00
2023-07-15 22:17:49 +10:00
private void CreateConfig(string dir)
{
if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
var sw = new StreamWriter(dir + "theme.txt");
sw.WriteLine("background: #000000");
sw.WriteLine("foreground: #8e05c2");
sw.WriteLine("background-alt: #3e065f");
sw.WriteLine("foreground-alt: #ffffff");
sw.WriteLine("transparent-mode: off");
sw.WriteLine("transparency-key: main");
sw.WriteLine("transparency-percent: 90");
sw.Dispose();
}
// Required for mica effect
internal enum AccentState
{
ACCENT_DISABLED = 0,
ACCENT_ENABLE_GRADIENT = 1,
ACCENT_ENABLE_TRANSPARENTGRADIENT = 2,
ACCENT_ENABLE_BLURBEHIND = 3,
ACCENT_INVALID_STATE = 4
2023-07-05 16:44:34 +10:00
}
2023-07-15 22:17:49 +10:00
internal enum WindowCompositionAttribute
{
WCA_ACCENT_POLICY = 19
}
[StructLayout(LayoutKind.Sequential)]
internal struct AccentPolicy
{
public AccentState AccentState;
public int AccentFlags;
public int GradientColor;
public int AnimationId;
}
[StructLayout(LayoutKind.Sequential)]
internal struct WindowCompositionAttributeData
{
public WindowCompositionAttribute Attribute;
public IntPtr Data;
public int SizeOfData;
}
internal static class User32
{
[DllImport("user32.dll")]
internal static extern int SetWindowCompositionAttribute(IntPtr hwnd, ref WindowCompositionAttributeData data);
}
#endregion
}