Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

Commit

Permalink
Add support for custom CSS
Browse files Browse the repository at this point in the history
  • Loading branch information
david-swift committed May 18, 2024
1 parent f53c194 commit 239f025
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Sources/Adwaita/View/Modifiers/AppearObserver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
}
}

}
8 changes: 8 additions & 0 deletions Tests/DiceDemo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ struct DiceDemo: View {
}
.pill()
.suggested()
.style("dice-button")
.css {
"""
.dice-button {
background-color: @green_5;
}
"""
}
.frame(maxWidth: 100)
}
.valign(.center)
Expand Down

0 comments on commit 239f025

Please sign in to comment.