1
1
mirror of https://github.com/ryujinx-mirror/ryujinx.git synced 2025-04-23 14:20:16 -05:00
ryujinx/src/Ryujinx/Ui/Applet/GtkHostUiTheme.cs
TSRBerry 0684b00b3c
[Ryujinx] Address dotnet-format issues (#5395)
* dotnet format style --severity info

Some changes were manually reverted.

* dotnet format analyzers --serverity info

Some changes have been minimally adapted.

* Restore a few unused methods and variables

* Address dotnet format CA1816 warnings

* Address or silence dotnet format CA2208 warnings

* Address or silence dotnet format CA1806 and a few CA1854 warnings

* Address dotnet format CA1822 warnings

* Make dotnet format succeed in style mode

* Address dotnet format CA2208 warnings properly

* Address most dotnet format whitespace warnings

* Apply dotnet format whitespace formatting

A few of them have been manually reverted and the corresponding warning was silenced

* Format if-blocks correctly

* Another rebase, another dotnet format run

* Run dotnet format whitespace after rebase

* Run dotnet format after rebase and remove unused usings

- analyzers
- style
- whitespace

* Add comments to disabled warnings

* Simplify properties and array initialization, Use const when possible, Remove trailing commas

* Revert "Simplify properties and array initialization, Use const when possible, Remove trailing commas"

This reverts commit 9462e4136c0a2100dc28b20cf9542e06790aa67e.

* dotnet format whitespace after rebase

* First dotnet format pass

* Fix build issues

* Apply suggestions from code review

Co-authored-by: Ac_K <Acoustik666@gmail.com>

* Second dotnet format pass

* Update src/Ryujinx/Modules/Updater/Updater.cs

Co-authored-by: Ac_K <Acoustik666@gmail.com>

* Add trailing commas and improve formatting

* Fix formatting and naming issues

* Rename nvStutterWorkaround to nvidiaStutterWorkaround

* Use using declarations and extend resource lifetimes

* Fix GTK issues

* Add formatting for generated files

* Add trailing commas

---------

Co-authored-by: Ac_K <Acoustik666@gmail.com>
2023-07-02 00:25:07 +02:00

91 lines
3.9 KiB
C#

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);
}
}
}