Add support for specifying the CSS scheme to load
Some checks are pending
Deploy Docs / publish (push) Waiting to run
SwiftLint / SwiftLint (push) Waiting to run

This commit is contained in:
david-swift 2026-02-02 19:42:31 +01:00
parent 07d6a38edc
commit 54619c69f4
3 changed files with 40 additions and 2 deletions

View File

@ -0,0 +1,23 @@
//
// ColorScheme.swift
// Adwaita
//
// Created by david-swift on 01.02.26.
//
import CAdw
/// The system color scheme.
public enum ColorScheme: UInt32 {
/// The dark color scheme.
case dark = 2
/// The light color scheme.
case light = 3
/// The ColorScheme value as GtkInterfaceColorScheme.
var gtkValue: GtkInterfaceColorScheme {
.init(rawValue)
}
}

View File

@ -336,9 +336,11 @@ extension AnyView {
} }
/// Add CSS classes to the app as soon as the view appears. /// Add CSS classes to the app as soon as the view appears.
/// - Parameter getString: Get the CSS. /// - Parameters:
/// - scheme: The color scheme.
/// - getString: Get the CSS.
/// - Returns: A view. /// - Returns: A view.
public func css(_ getString: @escaping () -> String) -> AnyView { public func css(scheme: ColorScheme? = nil, getString: @escaping () -> String) -> AnyView {
inspect { storage, updateProperties in inspect { storage, updateProperties in
let cssID = "internal-css" let cssID = "internal-css"
let providerID = "internal-css-provider" let providerID = "internal-css-provider"
@ -346,6 +348,9 @@ extension AnyView {
let string = getString() let string = getString()
if updateProperties, string != previous { if updateProperties, string != previous {
let provider = gtk_css_provider_new() let provider = gtk_css_provider_new()
if let scheme {
gtui_cssprovider_set_prefers_color_scheme(.init(Int(bitPattern: provider)), scheme.gtkValue)
}
gtk_css_provider_load_from_string( gtk_css_provider_load_from_string(
provider, provider,
string string

View File

@ -85,3 +85,13 @@ gtui_initialize_boolean (gboolean boolean)
g_value_set_boolean(&val, boolean); g_value_set_boolean(&val, boolean);
return val; return val;
} }
static void
gtui_cssprovider_set_prefers_color_scheme (uint64_t provider, GtkInterfaceColorScheme scheme)
{
GValue val = G_VALUE_INIT;
g_value_init(&val, G_TYPE_ENUM);
g_value_set_enum(&val, scheme);
g_object_set_property(provider, "prefers-color-scheme", &val);
g_value_unset(&val);
}