Add support for custom CSS

This commit is contained in:
david-swift 2024-05-18 14:28:06 +02:00
parent f53c194045
commit 239f025508
2 changed files with 27 additions and 0 deletions

View File

@ -64,4 +64,23 @@ extension View {
}
}
/// Add CSS classes to the app as soon as the view appears.
/// - Parameter getString: Get the CSS.
/// - Returns: A view.
public func css(_ getString: @escaping () -> String) -> View {
inspectOnAppear { _ in
let provider = gtk_css_provider_new()
gtk_css_provider_load_from_string(
provider,
getString()
)
let display = gdk_display_get_default()
gtk_style_context_add_provider_for_display(
display,
provider?.opaque(),
.init(GTK_STYLE_PROVIDER_PRIORITY_APPLICATION)
)
}
}
}

View File

@ -28,6 +28,14 @@ struct DiceDemo: View {
}
.pill()
.suggested()
.style("dice-button")
.css {
"""
.dice-button {
background-color: @green_5;
}
"""
}
.frame(maxWidth: 100)
}
.valign(.center)