mirror of
https://github.com/Stone-Red-Code/DesktopMagic.git
synced 2026-09-04 08:56:15 +02:00
Pass theme properties to web plugin
This commit is contained in:
@@ -6,16 +6,26 @@ namespace DesktopMagic.Helpers;
|
|||||||
|
|
||||||
internal static partial class MultiColorConverter
|
internal static partial class MultiColorConverter
|
||||||
{
|
{
|
||||||
public static string ConvertToHex(System.Drawing.Color color)
|
public static string ConvertToHexArgb(System.Drawing.Color color)
|
||||||
{
|
{
|
||||||
return $"#{color.A:X2}{color.R:X2}{color.G:X2}{color.B:X2}";
|
return $"#{color.A:X2}{color.R:X2}{color.G:X2}{color.B:X2}";
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string ConvertToHex(System.Windows.Media.Color color)
|
public static string ConvertToHexArgb(System.Windows.Media.Color color)
|
||||||
{
|
{
|
||||||
return $"#{color.A:X2}{color.R:X2}{color.G:X2}{color.B:X2}";
|
return $"#{color.A:X2}{color.R:X2}{color.G:X2}{color.B:X2}";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static string ConvertToHexRgba(System.Drawing.Color color)
|
||||||
|
{
|
||||||
|
return $"#{color.R:X2}{color.G:X2}{color.B:X2}{color.A:X2}";
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string ConvertToHexRgba(System.Windows.Media.Color color)
|
||||||
|
{
|
||||||
|
return $"#{color.R:X2}{color.G:X2}{color.B:X2}{color.A:X2}";
|
||||||
|
}
|
||||||
|
|
||||||
public static bool TryConvertToSystemColor(string hex, out System.Drawing.Color color)
|
public static bool TryConvertToSystemColor(string hex, out System.Drawing.Color color)
|
||||||
{
|
{
|
||||||
hex = hex.Replace("#", "");
|
hex = hex.Replace("#", "");
|
||||||
|
|||||||
@@ -21,7 +21,7 @@
|
|||||||
|
|
||||||
<Border x:Name="border" Background="Transparent" CornerRadius="10">
|
<Border x:Name="border" Background="Transparent" CornerRadius="10">
|
||||||
<Grid>
|
<Grid>
|
||||||
<wv2:WebView2 x:Name="webView" DefaultBackgroundColor="Transparent" />
|
<wv2:WebView2 x:Name="webView" DefaultBackgroundColor="Transparent" CoreWebView2InitializationCompleted="webView_CoreWebView2InitializationCompleted" />
|
||||||
<Rectangle Fill="Transparent" IsHitTestVisible="True" />
|
<Rectangle Fill="Transparent" IsHitTestVisible="True" />
|
||||||
</Grid>
|
</Grid>
|
||||||
</Border>
|
</Border>
|
||||||
|
|||||||
@@ -129,8 +129,35 @@ public partial class WebPluginWindow : Window, IPluginWindow
|
|||||||
|
|
||||||
private void ThemeChanged()
|
private void ThemeChanged()
|
||||||
{
|
{
|
||||||
|
webView.Margin = new Thickness(settings.Theme.Margin);
|
||||||
border.Background = new SolidColorBrush(MultiColorConverter.ConvertToMediaColor(settings.Theme.BackgroundColor));
|
border.Background = new SolidColorBrush(MultiColorConverter.ConvertToMediaColor(settings.Theme.BackgroundColor));
|
||||||
border.CornerRadius = new CornerRadius(settings.Theme.CornerRadius);
|
border.CornerRadius = new CornerRadius(settings.Theme.CornerRadius);
|
||||||
|
|
||||||
|
string cssVariables = $@"
|
||||||
|
:root {{
|
||||||
|
--background-color: {MultiColorConverter.ConvertToHexRgba(settings.Theme.BackgroundColor)};
|
||||||
|
--primary-color: {MultiColorConverter.ConvertToHexRgba(settings.Theme.PrimaryColor)};
|
||||||
|
--secondary-color: {MultiColorConverter.ConvertToHexRgba(settings.Theme.SecondaryColor)};
|
||||||
|
--font-family: {settings.Theme.Font};
|
||||||
|
|
||||||
|
font-family: {settings.Theme.Font};
|
||||||
|
color: {MultiColorConverter.ConvertToHexRgba(settings.Theme.PrimaryColor)};
|
||||||
|
}}
|
||||||
|
";
|
||||||
|
|
||||||
|
string script = $@"
|
||||||
|
(function() {{
|
||||||
|
let style = document.getElementById('desktop-magic-theme');
|
||||||
|
if (!style) {{
|
||||||
|
style = document.createElement('style');
|
||||||
|
style.id = 'desktop-magic-theme';
|
||||||
|
document.head.appendChild(style);
|
||||||
|
}}
|
||||||
|
style.textContent = `{cssVariables}`;
|
||||||
|
}})();
|
||||||
|
";
|
||||||
|
|
||||||
|
_ = webView.ExecuteScriptAsync(script);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async System.Threading.Tasks.Task InitializeWebView()
|
private async System.Threading.Tasks.Task InitializeWebView()
|
||||||
@@ -202,4 +229,9 @@ public partial class WebPluginWindow : Window, IPluginWindow
|
|||||||
|
|
||||||
tileBar.CaptionHeight = ActualHeight - 10;
|
tileBar.CaptionHeight = ActualHeight - 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void webView_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
|
||||||
|
{
|
||||||
|
webView.CoreWebView2.DOMContentLoaded += (_, _) => ThemeChanged();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user