aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Gtk3/UI/Applet/GtkHostUITheme.cs
diff options
context:
space:
mode:
authorMary Guillemard <mary@mary.zone>2024-03-02 12:51:05 +0100
committerGitHub <noreply@github.com>2024-03-02 12:51:05 +0100
commitec6cb0abb4b7669895b6e96fd7581c93b5abd691 (patch)
tree128c862ff5faea0b219467656d4023bee7faefb5 /src/Ryujinx.Gtk3/UI/Applet/GtkHostUITheme.cs
parent53b5985da6b9d7b281d9fc25b93bfd1d1918a107 (diff)
infra: Make Avalonia the default UI (#6375)1.1.1216
* misc: Move Ryujinx project to Ryujinx.Gtk3 This breaks release CI for now but that's fine. Signed-off-by: Mary Guillemard <mary@mary.zone> * misc: Move Ryujinx.Ava project to Ryujinx This breaks CI for now, but it's fine. Signed-off-by: Mary Guillemard <mary@mary.zone> * infra: Make Avalonia the default UI Should fix CI after the previous changes. GTK3 isn't build by the release job anymore, only by PR CI. This also ensure that the test-ava update package is still generated to allow update from the old testing channel. Signed-off-by: Mary Guillemard <mary@mary.zone> * Fix missing copy in create_app_bundle.sh Signed-off-by: Mary Guillemard <mary@mary.zone> * Fix syntax error Signed-off-by: Mary Guillemard <mary@mary.zone> --------- Signed-off-by: Mary Guillemard <mary@mary.zone>
Diffstat (limited to 'src/Ryujinx.Gtk3/UI/Applet/GtkHostUITheme.cs')
-rw-r--r--src/Ryujinx.Gtk3/UI/Applet/GtkHostUITheme.cs90
1 files changed, 90 insertions, 0 deletions
diff --git a/src/Ryujinx.Gtk3/UI/Applet/GtkHostUITheme.cs b/src/Ryujinx.Gtk3/UI/Applet/GtkHostUITheme.cs
new file mode 100644
index 00000000..52d1123b
--- /dev/null
+++ b/src/Ryujinx.Gtk3/UI/Applet/GtkHostUITheme.cs
@@ -0,0 +1,90 @@
+using Gtk;
+using Ryujinx.HLE.UI;
+using System.Diagnostics;
+
+namespace Ryujinx.UI.Applet
+{
+ internal class GtkHostUITheme : IHostUITheme
+ {
+ private const int RenderSurfaceWidth = 32;
+ private const int RenderSurfaceHeight = 32;
+
+ public string FontFamily { get; private set; }
+
+ public ThemeColor DefaultBackgroundColor { get; }
+ public ThemeColor DefaultForegroundColor { get; }
+ public ThemeColor DefaultBorderColor { get; }
+ public ThemeColor SelectionBackgroundColor { get; }
+ public ThemeColor SelectionForegroundColor { get; }
+
+ public GtkHostUITheme(Window parent)
+ {
+ Entry entry = new();
+ entry.SetStateFlags(StateFlags.Selected, true);
+
+ // Get the font and some colors directly from GTK.
+ FontFamily = entry.PangoContext.FontDescription.Family;
+
+ // Get foreground colors from the style context.
+
+ var defaultForegroundColor = entry.StyleContext.GetColor(StateFlags.Normal);
+ var selectedForegroundColor = entry.StyleContext.GetColor(StateFlags.Selected);
+
+ DefaultForegroundColor = new ThemeColor((float)defaultForegroundColor.Alpha, (float)defaultForegroundColor.Red, (float)defaultForegroundColor.Green, (float)defaultForegroundColor.Blue);
+ SelectionForegroundColor = new ThemeColor((float)selectedForegroundColor.Alpha, (float)selectedForegroundColor.Red, (float)selectedForegroundColor.Green, (float)selectedForegroundColor.Blue);
+
+ ListBoxRow row = new();
+ row.SetStateFlags(StateFlags.Selected, true);
+
+ // Request the main thread to render some UI elements to an image to get an approximation for the color.
+ // NOTE (caian): This will only take the color of the top-left corner of the background, which may be incorrect
+ // if someone provides a custom style with a gradient or image.
+
+ using (var surface = new Cairo.ImageSurface(Cairo.Format.Argb32, RenderSurfaceWidth, RenderSurfaceHeight))
+ using (var context = new Cairo.Context(surface))
+ {
+ context.SetSourceRGBA(1, 1, 1, 1);
+ context.Rectangle(0, 0, RenderSurfaceWidth, RenderSurfaceHeight);
+ context.Fill();
+
+ // The background color must be from the main Window because entry uses a different color.
+ parent.StyleContext.RenderBackground(context, 0, 0, RenderSurfaceWidth, RenderSurfaceHeight);
+
+ DefaultBackgroundColor = ToThemeColor(surface.Data);
+
+ context.SetSourceRGBA(1, 1, 1, 1);
+ context.Rectangle(0, 0, RenderSurfaceWidth, RenderSurfaceHeight);
+ context.Fill();
+
+ // Use the background color of the list box row when selected as the text box frame color because they are the
+ // same in the default theme.
+ row.StyleContext.RenderBackground(context, 0, 0, RenderSurfaceWidth, RenderSurfaceHeight);
+
+ DefaultBorderColor = ToThemeColor(surface.Data);
+ }
+
+ // Use the border color as the text selection color.
+ SelectionBackgroundColor = DefaultBorderColor;
+ }
+
+ private static ThemeColor ToThemeColor(byte[] data)
+ {
+ Debug.Assert(data.Length == 4 * RenderSurfaceWidth * RenderSurfaceHeight);
+
+ // Take the center-bottom pixel of the surface.
+ int position = 4 * (RenderSurfaceWidth * (RenderSurfaceHeight - 1) + RenderSurfaceWidth / 2);
+
+ if (position + 4 > data.Length)
+ {
+ return new ThemeColor(1, 0, 0, 0);
+ }
+
+ float a = data[position + 3] / 255.0f;
+ float r = data[position + 2] / 255.0f;
+ float g = data[position + 1] / 255.0f;
+ float b = data[position + 0] / 255.0f;
+
+ return new ThemeColor(a, r, g, b);
+ }
+ }
+}