Add support for specifying the CSS scheme to load
This commit is contained in:
parent
07d6a38edc
commit
54619c69f4
23
Sources/Adwaita/Model/Enumerations/ColorScheme.swift
Normal file
23
Sources/Adwaita/Model/Enumerations/ColorScheme.swift
Normal 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)
|
||||
}
|
||||
|
||||
}
|
||||
@ -336,9 +336,11 @@ extension AnyView {
|
||||
}
|
||||
|
||||
/// 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.
|
||||
public func css(_ getString: @escaping () -> String) -> AnyView {
|
||||
public func css(scheme: ColorScheme? = nil, getString: @escaping () -> String) -> AnyView {
|
||||
inspect { storage, updateProperties in
|
||||
let cssID = "internal-css"
|
||||
let providerID = "internal-css-provider"
|
||||
@ -346,6 +348,9 @@ extension AnyView {
|
||||
let string = getString()
|
||||
if updateProperties, string != previous {
|
||||
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(
|
||||
provider,
|
||||
string
|
||||
|
||||
@ -85,3 +85,13 @@ gtui_initialize_boolean (gboolean boolean)
|
||||
g_value_set_boolean(&val, boolean);
|
||||
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);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user