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

Commit

Permalink
Add type safe style classes
Browse files Browse the repository at this point in the history
  • Loading branch information
david-swift committed May 18, 2024
1 parent c7eb212 commit be9cf99
Show file tree
Hide file tree
Showing 25 changed files with 293 additions and 39 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct Counter: View {
count -= 1
}
Text("\(count)")
.style("title-1")
.title1()
.frame(minWidth: 100)
Button(icon: .default(icon: .goNext)) {
count += 1
Expand Down
18 changes: 18 additions & 0 deletions Sources/Adwaita/View/CheckButton+.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// CheckButton+.swift
// Adwaita
//
// Created by david-swift on 18.05.24.
//

/// A button widget.
extension CheckButton {

/// Apply the selection mode style class.
/// - Parameter active: Whether it is applied.
/// - Returns: A view.
public func selectionMode(_ active: Bool = true) -> View {
style("selection-mode", active: active)
}

}
7 changes: 7 additions & 0 deletions Sources/Adwaita/View/Forms/ActionRow+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,11 @@ extension ActionRow {
self = self.title(title)
}

/// Deemphasize the row title and emphasize the subtitle.
/// - Parameter active: Whether the style is currently applied.
/// - Returns: A view.
public func property(_ active: Bool = true) -> View {
style("property", active: active)
}

}
10 changes: 10 additions & 0 deletions Sources/Adwaita/View/HStack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ public struct HStack: View {

/// The content.
var content: () -> Body
/// Whether the linked style should be used.
var linked = false

/// The view's body.
public var view: Body {
VStack(horizontal: true, content: content)
.linked(linked)
}

/// Initialize a `HStack`.
Expand All @@ -22,4 +25,11 @@ public struct HStack: View {
self.content = content
}

/// Link the children.
public func linked(_ active: Bool = true) -> Self {
var newSelf = self
newSelf.linked = active
return newSelf
}

}
13 changes: 11 additions & 2 deletions Sources/Adwaita/View/List.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,17 @@ extension List {
}

/// Add the "navigation-sidebar" style class.
public func sidebarStyle() -> View {
style("navigation-sidebar")
/// - Parameter active: Whether the style is applied.
/// - Returns: A view.
public func sidebarStyle(_ active: Bool = true) -> View {
style("navigation-sidebar", active: active)
}

/// Apply the boxed list style class.
/// - Parameter active: Whether the style is applied.
/// - Returns: A view.
public func boxedList(_ active: Bool = true) -> View {
style("boxed-list", active: active)
}

}
198 changes: 198 additions & 0 deletions Sources/Adwaita/View/Modifiers/InspectorWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,204 @@ extension View {
}
}

/// Make a button or similar widget use accent colors.
/// - Parameter active: Whether the style is currently applied.
/// - Returns: A view.
public func suggested(_ active: Bool = true) -> View {
style("suggested-action", active: active)
}

/// Make a button or similar widget use destructive colors.
/// - Parameter active: Whether the style is currently applied.
/// - Returns: A view.
public func destructive(_ active: Bool = true) -> View {
style("destructive-action", active: active)
}

/// Make a button or similar widget use flat appearance.
/// - Parameter active: Whether the style is currently applied.
/// - Returns: A view.
public func flat(_ active: Bool = true) -> View {
style("flat", active: active)
}

/// Make a button or similar widget use the regular appearance instead of the flat one.
/// - Parameter active: Whether the style is currently applied.
/// - Returns: A view.
public func raised(_ active: Bool = true) -> View {
style("raised", active: active)
}

/// Make a button or similar widget round.
/// - Parameter active: Whether the style is currently applied.
/// - Returns: A view.
public func circular(_ active: Bool = true) -> View {
style("circular", active: active)
}

/// Make a button or similar widget appear as a pill.
/// - Parameter active: Whether the style is currently applied.
/// - Returns: A view.
public func pill(_ active: Bool = true) -> View {
style("pill", active: active)
}

/// Make the view partially transparent.
/// - Parameter active: Whether the style is currently applied.
/// - Returns: A view.
public func dimLabel(_ active: Bool = true) -> View {
style("dim-label", active: active)
}

/// Use a title typography style.
/// - Parameter active: Whether the style is currently applied.
/// - Returns: A view.
public func title1(_ active: Bool = true) -> View {
style("title-1", active: active)
}

/// Use a title typography style.
/// - Parameter active: Whether the style is currently applied.
/// - Returns: A view.
public func title2(_ active: Bool = true) -> View {
style("title-2", active: active)
}

/// Use a title typography style.
/// - Parameter active: Whether the style is currently applied.
/// - Returns: A view.
public func title3(_ active: Bool = true) -> View {
style("title-3", active: active)
}

/// Use a title typography style.
/// - Parameter active: Whether the style is currently applied.
/// - Returns: A view.
public func title4(_ active: Bool = true) -> View {
style("title-4", active: active)
}

/// Use the heading typography style.
/// - Parameter active: Whether the style is currently applied.
/// - Returns: A view.
public func heading(_ active: Bool = true) -> View {
style("heading", active: active)
}

/// Use the body typography style.
/// - Parameter active: Whether the style is currently applied.
/// - Returns: A view.
public func body(_ active: Bool = true) -> View {
style("body", active: active)
}

/// Use the caption heading typography style.
/// - Parameter active: Whether the style is currently applied.
/// - Returns: A view.
public func captionHeading(_ active: Bool = true) -> View {
style("caption-heading", active: active)
}

/// Use the caption typography style.
/// - Parameter active: Whether the style is currently applied.
/// - Returns: A view.
public func caption(_ active: Bool = true) -> View {
style("caption", active: active)
}

/// Use the monospace typography style.
/// - Parameter active: Whether the style is currently applied.
/// - Returns: A view.
public func monospace(_ active: Bool = true) -> View {
style("monospace", active: active)
}

/// Use the numeric typography style.
/// - Parameter active: Whether the style is currently applied.
/// - Returns: A view.
public func numeric(_ active: Bool = true) -> View {
style("numeric", active: active)
}

/// Apply the accent color.
/// - Parameter active: Whether the style is currently applied.
/// - Returns: A view.
public func accent(_ active: Bool = true) -> View {
style("accent", active: active)
}

/// Apply the success color.
/// - Parameter active: Whether the style is currently applied.
/// - Returns: A view.
public func success(_ active: Bool = true) -> View {
style("success", active: active)
}

/// Apply the warning color.
/// - Parameter active: Whether the style is currently applied.
/// - Returns: A view.
public func warning(_ active: Bool = true) -> View {
style("warning", active: active)
}

/// Apply the error color.
/// - Parameter active: Whether the style is currently applied.
/// - Returns: A view.
public func error(_ active: Bool = true) -> View {
style("error", active: active)
}

/// Apply the card style.
/// - Parameter active: Whether the style is currently applied.
/// - Returns: A view.
public func card(_ active: Bool = true) -> View {
style("card", active: active)
}

/// Apply an icon dropshadow.
/// - Parameter active: Whether the style is currently applied.
/// - Returns: A view.
///
/// Use for icons larger than 32x32 pixels.
public func iconDropshadow(_ active: Bool = true) -> View {
style("icon-dropshadow", active: active)
}

/// Use for icons smaller than or equal to 32x32 pixels.
/// - Parameter active: Whether the style is currently applied.
/// - Returns: A view.
public func lowresIcon(_ active: Bool = true) -> View {
style("lowres-icon", active: active)
}

/// Use the OSD style class.
/// - Parameter active: Whether the style is currently applied.
/// - Returns: A view.
public func osd(_ active: Bool = true) -> View {
style("osd", active: active)
}

/// Give a view the default window background and foreground colors.
/// - Parameter active: Whether the style is currently applied.
/// - Returns: A view.
public func backgroundStyle(_ active: Bool = true) -> View {
style("background", active: active)
}

/// Give a view the default view background and foreground colors.
/// - Parameter active: Whether the style is currently applied.
/// - Returns: A view.
public func viewStyle(_ active: Bool = true) -> View {
style("view", active: active)
}

/// Give a view the default border.
/// - Parameter active: Whether the style is currently applied.
/// - Returns: A view.
public func frameStyle(_ active: Bool = true) -> View {
style("frame", active: active)
}

/// Run a function when the view gets an update.
/// - Parameter onUpdate: The function.
/// - Returns: A view.
Expand Down
7 changes: 7 additions & 0 deletions Sources/Adwaita/View/StatusPage+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,11 @@ extension StatusPage {
self = self.child(content)
}

/// Make the status page more compact.
/// - Parameter active: Whether the style is applied.
/// - Returns: A view.
public func compact(_ active: Bool = true) -> View {
style("compact", active: active)
}

}
5 changes: 5 additions & 0 deletions Sources/Adwaita/View/VStack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ extension VStack {
}
}

/// Link the children.
public func linked(_ active: Bool = true) -> View {
style("linked", active: active)
}

}
4 changes: 2 additions & 2 deletions Tests/AlertDialogDemo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ struct AlertDialogDemo: View {
Button("Show Dialog") {
dialog = true
}
.style("pill")
.style("suggested-action")
.pill()
.suggested()
.frame(maxWidth: 100)
.padding()
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/CarouselDemo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct CarouselDemo: View {
}
.vexpand()
.hexpand()
.style("card")
.card()
.onClick { print(element.id) }
.padding(20)
.frame(minWidth: 300, minHeight: 200)
Expand Down
4 changes: 2 additions & 2 deletions Tests/CounterDemo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct CounterDemo: View {
HStack {
CountButton(count: $count, icon: .goPrevious) { $0 -= 1 }
Text("\(count)")
.style("title-1")
.title1()
.frame(minWidth: 100)
CountButton(count: $count, icon: .goNext) { $0 += 1 }
}
Expand All @@ -39,7 +39,7 @@ struct CounterDemo: View {
Button(icon: .default(icon: icon)) {
action(&count)
}
.style("circular")
.circular()
}
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/DialogDemo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ struct DialogDemo: View {
Button("Show Dialog") {
dialog = true
}
.style("pill")
.style("suggested-action")
.pill()
.suggested()
.frame(maxWidth: 100)
.padding()
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/DiceDemo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ struct DiceDemo: View {
Button(label) {
number = .random(in: 1...6)
}
.style("pill")
.style("suggested-action")
.pill()
.suggested()
.frame(maxWidth: 100)
}
.valign(.center)
Expand Down
2 changes: 1 addition & 1 deletion Tests/FlowBoxDemo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ struct FlowBoxDemo: View {
selectedItem = items[safe: index]?.id ?? items[safe: index ?? 0 - 1]?.id ?? items.first?.id ?? ""
}
}
.linked()
.padding()
.style("linked")
.halign(.center)
if !items.isEmpty {
FlowBox(items, selection: $selectedItem) { item in
Expand Down
Loading

0 comments on commit be9cf99

Please sign in to comment.