diff --git a/Sources/Adwaita/View/Modifiers/AppearObserver.swift b/Sources/Adwaita/View/Modifiers/AppearObserver.swift index e1b9f8a..fc234d1 100644 --- a/Sources/Adwaita/View/Modifiers/AppearObserver.swift +++ b/Sources/Adwaita/View/Modifiers/AppearObserver.swift @@ -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) + ) + } + } + } diff --git a/Tests/DiceDemo.swift b/Tests/DiceDemo.swift index f2676be..66926bd 100644 --- a/Tests/DiceDemo.swift +++ b/Tests/DiceDemo.swift @@ -28,6 +28,14 @@ struct DiceDemo: View { } .pill() .suggested() + .style("dice-button") + .css { + """ + .dice-button { + background-color: @green_5; + } + """ + } .frame(maxWidth: 100) } .valign(.center)