Add support for dynamic CSS #61
Some checks failed
Deploy Docs / publish (push) Has been cancelled
SwiftLint / SwiftLint (push) Has been cancelled

This commit is contained in:
david-swift 2025-05-05 13:11:35 +02:00
parent 7eeec9e0fd
commit a04bd9025d
2 changed files with 20 additions and 14 deletions

View File

@ -339,19 +339,24 @@ extension AnyView {
/// - Parameter getString: Get the CSS. /// - Parameter getString: Get the CSS.
/// - Returns: A view. /// - Returns: A view.
public func css(_ getString: @escaping () -> String) -> AnyView { public func css(_ getString: @escaping () -> String) -> AnyView {
inspectOnAppear { _ in inspect { storage, updateProperties in
let provider = gtk_css_provider_new() let cssID = "internal-css"
gtk_css_provider_load_from_string( let previous = storage.fields[cssID] as? String
provider, let string = getString()
getString() if updateProperties, string != previous {
) let provider = gtk_css_provider_new()
let display = gdk_display_get_default() gtk_css_provider_load_from_string(
gtk_style_context_add_provider_for_display( provider,
display, string
provider?.opaque(), )
.init(GTK_STYLE_PROVIDER_PRIORITY_APPLICATION) let display = gdk_display_get_default()
) gtk_style_context_add_provider_for_display(
g_object_unref(provider) display,
provider?.opaque(),
.init(GTK_STYLE_PROVIDER_PRIORITY_APPLICATION)
)
g_object_unref(provider)
}
} }
} }

View File

@ -32,7 +32,8 @@ struct DiceDemo: View {
.css { .css {
""" """
.dice-button { .dice-button {
background-color: @green_5; background-color: \(number == nil ? "@blue_5" : "@green_5");
transition: 0.2s;
} }
""" """
} }