ShareX/ShareX.HelpersLib/ShareXResources.cs

335 lines
12 KiB
C#
Raw Normal View History

#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2024-01-03 12:57:14 +13:00
Copyright (c) 2007-2024 ShareX Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
2014-12-11 09:25:20 +13:00
using ShareX.HelpersLib.Properties;
2020-07-09 19:20:06 +12:00
using System.ComponentModel;
using System.Drawing;
using System.Reflection;
2018-11-06 12:40:05 +13:00
using System.Windows.Forms;
2014-12-11 09:25:20 +13:00
namespace ShareX.HelpersLib
{
public static class ShareXResources
{
2021-12-11 23:16:54 +13:00
public static string Name { get; set; } = "ShareX";
2018-11-06 12:40:05 +13:00
public static string UserAgent
{
get
{
2023-03-06 08:16:45 +13:00
return $"{Name}/{Helpers.GetApplicationVersion()}";
2018-11-06 12:40:05 +13:00
}
}
2020-02-09 04:07:17 +13:00
private static bool useCustomTheme;
2019-09-07 23:39:58 +12:00
2020-02-05 21:13:55 +13:00
public static bool UseCustomTheme
2019-09-07 23:39:58 +12:00
{
get
{
2020-02-09 04:07:17 +13:00
return useCustomTheme && Theme != null;
2019-09-07 23:39:58 +12:00
}
set
{
2020-02-09 04:07:17 +13:00
useCustomTheme = value;
2019-09-07 23:39:58 +12:00
}
}
2020-03-31 20:26:18 +13:00
public static bool IsDarkTheme => UseCustomTheme && Theme.IsDarkTheme;
2021-11-27 07:48:16 +13:00
private static bool useWhiteIcon;
2021-11-27 07:48:16 +13:00
public static bool UseWhiteIcon
{
get
{
return useWhiteIcon;
}
set
{
if (useWhiteIcon != value)
{
useWhiteIcon = value;
if (useWhiteIcon)
{
Icon = Resources.ShareX_Icon_White;
}
else
{
Icon = Resources.ShareX_Icon;
}
}
}
}
2021-11-27 08:02:18 +13:00
private static Icon icon = Resources.ShareX_Icon;
public static Icon Icon
{
get
{
return icon.CloneSafe();
}
set
{
if (icon != value)
{
icon?.Dispose();
icon = value;
}
}
}
2019-09-23 22:20:22 +12:00
2021-11-27 08:15:24 +13:00
private static Bitmap logo = Resources.ShareX_Logo;
public static Bitmap Logo
{
get
{
return logo.CloneSafe();
}
set
{
if (logo != value)
{
logo?.Dispose();
logo = value;
}
}
}
2020-12-09 18:32:46 +13:00
public static ShareXTheme Theme { get; set; } = ShareXTheme.DarkTheme;
2023-06-04 10:07:30 +12:00
public static void ApplyTheme(Form form, bool closeOnEscape = false, bool setIcon = true)
{
2023-06-04 10:07:30 +12:00
if (closeOnEscape)
{
form.CloseOnEscape();
}
2019-06-25 06:17:33 +12:00
if (setIcon)
{
form.Icon = Icon;
}
2020-05-22 17:44:02 +12:00
if (UseCustomTheme)
{
2020-02-09 04:07:17 +13:00
ApplyCustomThemeToControl(form);
IContainer components = form.GetType().GetField("components", BindingFlags.NonPublic | BindingFlags.Instance)?.GetValue(form) as IContainer;
ApplyCustomThemeToComponents(components);
if (form.IsHandleCreated)
{
NativeMethods.UseImmersiveDarkMode(form.Handle, Theme.IsDarkTheme);
}
else
{
form.HandleCreated += (s, e) => NativeMethods.UseImmersiveDarkMode(form.Handle, Theme.IsDarkTheme);
}
}
}
public static void ApplyCustomThemeToControl(Control control)
{
2019-06-25 07:33:17 +12:00
if (control.ContextMenuStrip != null)
{
2020-02-09 04:07:17 +13:00
ApplyCustomThemeToContextMenuStrip(control.ContextMenuStrip);
2019-06-25 07:33:17 +12:00
}
if (control is MenuButton mb && mb.Menu != null)
{
2020-02-09 04:07:17 +13:00
ApplyCustomThemeToContextMenuStrip(mb.Menu);
2019-06-25 07:33:17 +12:00
}
2019-06-23 23:26:53 +12:00
switch (control)
{
2022-08-01 01:08:00 +12:00
case ColorButton colorButton:
colorButton.FlatStyle = FlatStyle.Flat;
colorButton.FlatAppearance.BorderColor = Theme.BorderColor;
colorButton.ForeColor = Theme.TextColor;
colorButton.BackColor = Theme.LightBackgroundColor;
colorButton.BorderColor = Theme.BorderColor;
return;
2019-06-23 23:26:53 +12:00
case Button btn:
2019-06-26 04:12:34 +12:00
btn.FlatStyle = FlatStyle.Flat;
btn.FlatAppearance.BorderColor = Theme.BorderColor;
btn.ForeColor = Theme.TextColor;
btn.BackColor = Theme.LightBackgroundColor;
2019-06-26 04:12:34 +12:00
return;
case CheckBox cb when cb.Appearance == Appearance.Button:
cb.FlatStyle = FlatStyle.Flat;
cb.FlatAppearance.BorderColor = Theme.BorderColor;
cb.ForeColor = Theme.TextColor;
cb.BackColor = Theme.LightBackgroundColor;
2019-06-24 04:10:20 +12:00
return;
2019-06-25 09:49:34 +12:00
case TextBox tb:
tb.ForeColor = Theme.TextColor;
tb.BackColor = Theme.LightBackgroundColor;
2019-06-25 09:49:34 +12:00
tb.BorderStyle = BorderStyle.FixedSingle;
return;
case ComboBox cb:
cb.FlatStyle = FlatStyle.Flat;
cb.ForeColor = Theme.TextColor;
cb.BackColor = Theme.LightBackgroundColor;
return;
case ListBox lb:
lb.ForeColor = Theme.TextColor;
lb.BackColor = Theme.LightBackgroundColor;
2019-06-29 10:35:34 +12:00
return;
case ListView lv:
lv.ForeColor = Theme.TextColor;
lv.BackColor = Theme.LightBackgroundColor;
2020-02-09 04:07:17 +13:00
lv.SupportCustomTheme();
return;
case SplitContainerCustomSplitter sccs:
sccs.SplitterColor = Theme.BackgroundColor;
sccs.SplitterLineColor = Theme.BorderColor;
sccs.Panel1.BackColor = Theme.BackgroundColor;
sccs.Panel2.BackColor = Theme.BackgroundColor;
break;
2019-06-23 23:26:53 +12:00
case SplitContainer sc:
sc.Panel1.BackColor = Theme.BackgroundColor;
sc.Panel2.BackColor = Theme.BackgroundColor;
2019-06-24 04:10:20 +12:00
break;
2019-06-24 01:03:25 +12:00
case PropertyGrid pg:
pg.CategoryForeColor = Theme.TextColor;
pg.CategorySplitterColor = Theme.BackgroundColor;
pg.LineColor = Theme.BackgroundColor;
pg.SelectedItemWithFocusForeColor = Theme.BackgroundColor;
pg.SelectedItemWithFocusBackColor = Theme.TextColor;
pg.ViewForeColor = Theme.TextColor;
pg.ViewBackColor = Theme.LightBackgroundColor;
pg.ViewBorderColor = Theme.BorderColor;
pg.HelpForeColor = Theme.TextColor;
pg.HelpBackColor = Theme.BackgroundColor;
pg.HelpBorderColor = Theme.BorderColor;
2019-06-25 09:49:34 +12:00
return;
2019-06-24 03:56:19 +12:00
case DataGridView dgv:
dgv.BackgroundColor = Theme.LightBackgroundColor;
dgv.GridColor = Theme.BorderColor;
dgv.DefaultCellStyle.BackColor = Theme.LightBackgroundColor;
dgv.DefaultCellStyle.SelectionBackColor = Theme.LightBackgroundColor;
dgv.DefaultCellStyle.ForeColor = Theme.TextColor;
dgv.DefaultCellStyle.SelectionForeColor = Theme.TextColor;
dgv.ColumnHeadersDefaultCellStyle.BackColor = Theme.BackgroundColor;
dgv.ColumnHeadersDefaultCellStyle.SelectionBackColor = Theme.BackgroundColor;
dgv.ColumnHeadersDefaultCellStyle.ForeColor = Theme.TextColor;
dgv.ColumnHeadersDefaultCellStyle.SelectionForeColor = Theme.TextColor;
2019-06-24 03:56:19 +12:00
dgv.EnableHeadersVisualStyles = false;
2019-06-24 04:10:20 +12:00
break;
2019-12-23 00:50:06 +13:00
case ContextMenuStrip cms:
2020-02-09 04:07:17 +13:00
ApplyCustomThemeToContextMenuStrip(cms);
2019-12-23 00:50:06 +13:00
return;
2019-06-25 05:59:48 +12:00
case ToolStrip ts:
2021-08-23 15:17:35 +12:00
ts.Font = Theme.MenuFont;
2019-06-25 05:59:48 +12:00
ts.Renderer = new ToolStripDarkRenderer();
2020-02-09 04:07:17 +13:00
ApplyCustomThemeToToolStripItemCollection(ts.Items);
2019-06-25 05:59:48 +12:00
return;
2019-06-24 04:10:20 +12:00
case LinkLabel ll:
ll.LinkColor = Theme.LinkColor;
2019-06-23 23:26:53 +12:00
break;
}
control.ForeColor = Theme.TextColor;
control.BackColor = Theme.BackgroundColor;
2019-06-24 04:10:20 +12:00
foreach (Control child in control.Controls)
{
2020-02-09 04:07:17 +13:00
ApplyCustomThemeToControl(child);
}
switch (control)
{
case TabToTreeView tttv:
tttv.LeftPanelBackColor = Theme.DarkBackgroundColor;
2020-11-30 14:21:53 +13:00
tttv.SeparatorColor = Theme.SeparatorDarkColor;
break;
}
}
2019-06-25 05:59:48 +12:00
private static void ApplyCustomThemeToComponents(IContainer container)
2020-07-09 19:20:06 +12:00
{
if (container != null)
2020-07-09 19:20:06 +12:00
{
foreach (IComponent component in container.Components)
2020-07-09 19:20:06 +12:00
{
switch (component)
{
case ContextMenuStrip cms:
ApplyCustomThemeToContextMenuStrip(cms);
break;
case ToolTip tt:
tt.ForeColor = Theme.TextColor;
tt.BackColor = Theme.BackgroundColor;
tt.OwnerDraw = true;
tt.Draw -= ToolTip_Draw;
tt.Draw += ToolTip_Draw;
break;
}
2020-07-09 19:20:06 +12:00
}
}
}
private static void ToolTip_Draw(object sender, DrawToolTipEventArgs e)
{
e.DrawBackground();
e.DrawBorder();
2020-10-30 02:40:38 +13:00
e.DrawText(TextFormatFlags.VerticalCenter | TextFormatFlags.LeftAndRightPadding);
2020-07-09 19:20:06 +12:00
}
2020-02-09 04:07:17 +13:00
public static void ApplyCustomThemeToContextMenuStrip(ContextMenuStrip cms)
2019-12-23 00:50:06 +13:00
{
if (cms != null)
{
cms.Renderer = new ToolStripDarkRenderer();
cms.Font = Theme.ContextMenuFont;
cms.Opacity = Theme.ContextMenuOpacityDouble;
ApplyCustomThemeToToolStripItemCollection(cms.Items);
}
2019-12-23 00:50:06 +13:00
}
2020-02-09 04:07:17 +13:00
private static void ApplyCustomThemeToToolStripItemCollection(ToolStripItemCollection collection)
2019-06-25 05:59:48 +12:00
{
foreach (ToolStripItem tsi in collection)
{
switch (tsi)
{
case ToolStripControlHost tsch:
2020-02-09 04:07:17 +13:00
ApplyCustomThemeToControl(tsch.Control);
2019-06-25 05:59:48 +12:00
break;
case ToolStripDropDownItem tsddi:
2019-12-23 00:50:06 +13:00
if (tsddi.DropDown != null)
{
tsddi.DropDown.Opacity = Theme.ContextMenuOpacityDouble;
2020-02-09 04:07:17 +13:00
ApplyCustomThemeToToolStripItemCollection(tsddi.DropDownItems);
2019-12-23 00:50:06 +13:00
}
2019-06-25 05:59:48 +12:00
break;
}
}
}
}
}