Skip to content

Commit

Permalink
(✿◠‿◠)
Browse files Browse the repository at this point in the history
  • Loading branch information
Der-Floh committed Jun 2, 2023
1 parent d32b029 commit 9df02bb
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 4 deletions.
30 changes: 27 additions & 3 deletions Cursor-Installer-Creator/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

111 changes: 110 additions & 1 deletion Cursor-Installer-Creator/MainForm.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.Win32;
using System.Diagnostics;
using System.Security.Principal;
using System.Text.Json;
Expand All @@ -9,15 +10,99 @@ public sealed partial class MainForm : Form
private Dictionary<string, string> BoxCursorAssignment { get; set; } = new Dictionary<string, string>();
private Dictionary<string, string> CursorBoxAssignment { get; set; }
private List<CCursor>? CCursors { get; set; }
private System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
private bool isResizing;

public MainForm(List<CCursor>? cCursors = null)
{
timer.Interval = 1000;
timer.Tick += (sender, e) =>
{
Refresh();
};

InitializeComponent();

DarkModeCheckBox.Checked = IsDarkModeEnabled();
DarkModeCheckBox_CheckedChanged(DarkModeCheckBox, EventArgs.Empty);
AdminLabel.Visible = !IsRunningWithAdminPrivileges();

SetUpBoxCursorAssignment();
FillCursors(cCursors);
}

public static bool IsDarkModeEnabled()
{
const string keyPath = @"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize";
const string valueName = "AppsUseLightTheme";

using var key = Registry.CurrentUser.OpenSubKey(keyPath);
if (key is not null && key.GetValue(valueName) is int value)
return value == 0;
return false;
}

public void ChangeTheme(bool light, Control.ControlCollection container)
{
if (light)
{
BackColor = SystemColors.Control;
ForeColor = SystemColors.ControlText;
}
else
{
BackColor = SystemColors.ControlDarkDark;
ForeColor = SystemColors.Control;
}
foreach (Control component in container)
{
if (component is Panel || component is Button || component is TextBox)
{
if (component is Panel)
{
var panel = component as Panel;
if (panel is not null)
if (light)
{
if (panel.HasBorderColor())
{
panel.BorderStyle = BorderStyle.FixedSingle;
panel.RemoveBorderColor();
}
}
else if (panel.BorderStyle == BorderStyle.FixedSingle)
{
panel.BorderStyle = BorderStyle.None;
panel.SetBorderColor(SystemColors.ControlDark);
}
ChangeTheme(light, component.Controls);
}
else if (component is Button)
{
var button = component as Button;
if (button is not null)
if (light)
button.FlatStyle = FlatStyle.Standard;
else
{
button.FlatStyle = FlatStyle.Flat;
button.FlatAppearance.BorderColor = SystemColors.ControlDark;
}
}
if (light)
{
component.BackColor = SystemColors.Control;
component.ForeColor = SystemColors.ControlText;
}
else
{
component.BackColor = SystemColors.ControlDarkDark;
component.ForeColor = SystemColors.Control;
}
}
}
}

private void SetUpBoxCursorAssignment()
{
BoxCursorAssignment.Add(CursorPictureBox1.Name, "Arrow");
Expand Down Expand Up @@ -308,6 +393,29 @@ private void RestartWithAdminPrivileges()

#region EventListeners

private void MainForm_Resize(object sender, EventArgs e)
{
if (DarkModeCheckBox.Checked && !isResizing)
{
isResizing = true;
timer.Start();
}
}
private void MainForm_ResizeEnd(object sender, EventArgs e)
{
if (isResizing)
{
timer.Stop();
isResizing = false;
Refresh();
}
}

private void DarkModeCheckBox_CheckedChanged(object sender, EventArgs e)
{
ChangeTheme(!DarkModeCheckBox.Checked, this.Controls);
}

private void PackageNameTextBox_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(PackageNameTextBox.Text))
Expand Down Expand Up @@ -362,7 +470,8 @@ private void PreviewCheckBox_CheckedChanged(object sender, EventArgs e)
Panel? panel = pictureBox?.Parent as Panel;

if (panel is null)
continue;
if (panel is null)
continue;

string? cursorPath = CCursors?.Find(x => x.Name?.ToLower() == cursorName.ToLower())?.CursorPath;
if (string.IsNullOrEmpty(cursorPath))
Expand Down
36 changes: 36 additions & 0 deletions Cursor-Installer-Creator/PanelExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace Cursor_Installer_Creator;
public static class PanelExtensions
{
public static readonly string BorderColorTag = "BorderColor";
public static Color BorderColor;

public static void SetBorderColor(this Panel panel, Color color)
{
if (HasBorderColor(panel))
RemoveBorderColor(panel);

BorderColor = color;
panel.Paint += Panel_Paint;
panel.Tag = BorderColorTag;
panel.Invalidate();
}

public static void RemoveBorderColor(this Panel panel)
{
panel.Paint -= Panel_Paint;
panel.Tag = null;
panel.Invalidate();
}

public static bool HasBorderColor(this Panel panel)
{
return panel.Tag != null && panel.Tag.ToString() == BorderColorTag;
}

private static void Panel_Paint(object sender, PaintEventArgs e)
{
Panel panel = (Panel)sender;
using Pen pen = new Pen(BorderColor);
e.Graphics.DrawRectangle(pen, new Rectangle(0, 0, ((Panel)sender).Width - 1, ((Panel)sender).Height - 1));
}
}

0 comments on commit 9df02bb

Please sign in to comment.