Skip to content

Commit

Permalink
Add support for the observation framework
Browse files Browse the repository at this point in the history
  • Loading branch information
david-swift committed May 2, 2024
1 parent 53858f1 commit e0a03f1
Show file tree
Hide file tree
Showing 70 changed files with 1,022 additions and 48 deletions.
9 changes: 9 additions & 0 deletions Sources/Adwaita/Model/Data Flow/Binding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,12 @@ extension Binding where Value: MutableCollection, Value.Element: Identifiable {
}

}

extension Binding: CustomStringConvertible where Value: CustomStringConvertible {

/// A textual description of the wrapped value.
public var description: String {
wrappedValue.description
}

}
5 changes: 4 additions & 1 deletion Sources/Adwaita/Model/Enumerations/Icon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Foundation

// swiftlint:disable type_body_length file_length
/// An icon.
public enum Icon {
public enum Icon: CustomStringConvertible {

/// A preinstalled icon.
/// - Parameter icon: The default icon.
Expand All @@ -28,6 +28,9 @@ public enum Icon {
}
}

/// A textual description of the icon.
public var description: String { string }

/// A preinstalled icon.
public enum DefaultIcon: String {
// swiftlint:disable missing_docs identifier_name
Expand Down
17 changes: 17 additions & 0 deletions Sources/Adwaita/Model/Extensions/Array.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@ extension Array: AnyView where Element == AnyView {
/// The array's view body is the array itself.
public var viewContent: Body { self }

/// Get the debug tree for an array of views.
/// - Parameter parameters: Whether the widget parameters should be visible in the tree.
/// - Returns: The tree.
public func getBodyDebugTree(parameters: Bool) -> String {
var description = ""
for view in self {
let viewDescription: String
if let widget = view as? Widget {
viewDescription = widget.getViewDescription(parameters: parameters)
} else {
viewDescription = view.getDebugTree(parameters: parameters)
}
description += viewDescription
}
return description
}

/// Get a widget from a collection of views.
/// - Parameter modifiers: Modify views before being updated.
/// - Returns: A widget.
Expand Down
35 changes: 35 additions & 0 deletions Sources/Adwaita/Model/Extensions/DefaultStringInterpolation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// DefaultStringInterpolation.swift
// Adwaita
//
// Created by david-swift on 01.05.24.
//
// Thanks to Eneko Alonso, Pyry Jahkola, cukr for the comments in this Swift forum discussion:
// "Multi-line string nested indentation with interpolation"
// https://forums.swift.org/t/multi-line-string-nested-indentation-with-interpolation/36933
//

extension DefaultStringInterpolation {

/// Preserve the indentation in a multi line string.
/// - Parameter string: The string.
///
/// Use it the following way:
/// """
/// Hello
/// \(indented: "World\n Test")
/// """
public mutating func appendInterpolation(indented string: String) {
// swiftlint:disable compiler_protocol_init
let indent = String(stringInterpolation: self).reversed().prefix { " \t".contains($0) }
// swiftlint:enable compiler_protocol_init
if indent.isEmpty {
appendInterpolation(string)
} else {
appendLiteral(
string.split(separator: "\n", omittingEmptySubsequences: false).joined(separator: "\n" + indent)
)
}
}

}
15 changes: 15 additions & 0 deletions Sources/Adwaita/Model/Extensions/Optional.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// Optional.swift
// Adwaita
//
// Created by david-swift on 01.05.20204.
//

extension Optional: CustomStringConvertible where Wrapped: CustomStringConvertible {

/// A textual description of the wrapped value.
public var description: String {
self?.description ?? "nil"
}

}
14 changes: 14 additions & 0 deletions Sources/Adwaita/Model/User Interface/View/AnyView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ public protocol AnyView {

extension AnyView {

/// Get the view's debug tree.
/// - Parameter parameters: Whether the widget parameters should be included in the debug tree.
/// - Returns: A textual description.
public func getDebugTree(parameters: Bool) -> String {
if let body = self as? Body {
return body.getBodyDebugTree(parameters: parameters)
}
return """
\(Self.self) {
\(indented: viewContent.getBodyDebugTree(parameters: parameters))
}
"""
}

func getModified(modifiers: [(AnyView) -> AnyView]) -> AnyView {
var modified: AnyView = self
for modifier in modifiers {
Expand Down
29 changes: 29 additions & 0 deletions Sources/Adwaita/Model/User Interface/View/Widget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
/// A widget is a view that know about its GTUI widget.
public protocol Widget: AnyView {

/// The debug tree parameters.
var debugTreeParameters: [(String, value: CustomStringConvertible)] { get }
/// The debug tree's content.
var debugTreeContent: [(String, body: Body)] { get }
/// The view storage.
/// - Parameter modifiers: Modify views before being updated.
func container(modifiers: [(AnyView) -> AnyView]) -> ViewStorage
Expand All @@ -25,4 +29,29 @@ extension Widget {
/// A widget's view is empty.
public var viewContent: Body { [] }

/// A description of the view.
public func getViewDescription(parameters: Bool) -> String {
var content = ""
for element in debugTreeContent {
if content.isEmpty {
content += """
{
\(indented: element.body.getDebugTree(parameters: parameters))
}
"""
} else {
content += """
\(element.0): {
\(indented: element.body.getDebugTree(parameters: parameters))
}
"""
}
}
if parameters {
let parametersString = debugTreeParameters.map { "\($0.0): \($0.value)" }.joined(separator: ", ")
return "\(Self.self)(\(parametersString))\(content)"
}
return "\(Self.self)\(content)"
}

}
18 changes: 18 additions & 0 deletions Sources/Adwaita/View/Dialogs/AboutDialog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@ struct AboutDialog: Widget {
/// The link for opening issues.
var issues: URL?

/// The debug tree parameters.
var debugTreeParameters: [(String, value: CustomStringConvertible)] {
[
("visible", value: visible),
("appName", value: appName),
("developer", value: developer),
("version", value: version),
("icon", value: icon),
("website", value: website),
("issues", value: issues)
]
}

/// The debug tree's content.
var debugTreeContent: [(String, body: Body)] {
[("child", body: [child])]
}

/// The ID for the dialog's storage.
let dialogID = "dialog"

Expand Down
16 changes: 16 additions & 0 deletions Sources/Adwaita/View/Dialogs/AlertDialog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ public struct AlertDialog: Widget {
/// The child view.
var child: AnyView

/// The debug tree parameters.
public var debugTreeParameters: [(String, value: CustomStringConvertible)] {
[
("visible", value: visible),
("id", value: id),
("heading", value: heading),
("body", value: body),
("responses", value: responses)
]
}

/// The debug tree's content.
public var debugTreeContent: [(String, body: Body)] {
[("child", body: [child])]
}

/// Information about a response.
struct Response: Identifiable {

Expand Down
16 changes: 16 additions & 0 deletions Sources/Adwaita/View/Dialogs/Dialog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ struct Dialog: Widget {
/// The ID for the content's storage.
let contentID = "content"

/// The debug tree parameters.
var debugTreeParameters: [(String, value: CustomStringConvertible)] {
[
("visible", value: visible),
("id", value: id),
("title", value: title),
("width", value: width),
("height", value: height)
]
}

/// The debug tree's content.
var debugTreeContent: [(String, body: Body)] {
[("child", body: [child]), ("content", body: content)]
}

/// Get the container of the child.
/// - Parameter modifiers: Modify views before being updated.
/// - Returns: The view storage.
Expand Down
15 changes: 15 additions & 0 deletions Sources/Adwaita/View/ForEach.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@ public struct ForEach<Element>: Widget where Element: Identifiable {
/// Whether the list is horizontal.
var horizontal: Bool

/// The debug tree parameters.
public var debugTreeParameters: [(String, value: CustomStringConvertible)] {
[
("elements", value: elements),
("horizontal", value: horizontal)
]
}

/// The debug tree's content.
public var debugTreeContent: [(String, body: Body)] {
elements.map { element in
("\(element)", body: content(element))
}
}

/// Initialize `ForEach`.
public init(_ elements: [Element], horizontal: Bool = false, @ViewBuilder content: @escaping (Element) -> Body) {
self.elements = elements
Expand Down
16 changes: 15 additions & 1 deletion Sources/Adwaita/View/Generated/ActionRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// ActionRow.swift
// Adwaita
//
// Created by auto-generation on 28.04.24.
// Created by auto-generation on 02.05.24.
//

import CAdw
Expand Down Expand Up @@ -104,6 +104,20 @@ public struct ActionRow: Widget {
/// The window.
var window: GTUIApplicationWindow?

/// The debug tree parameters.
public var debugTreeParameters: [(String, value: CustomStringConvertible)] {
[("iconName", value: "\(iconName)"), ("subtitle", value: "\(subtitle)"), ("subtitleLines", value: "\(subtitleLines)"), ("subtitleSelectable", value: "\(subtitleSelectable)"), ("title", value: "\(title)"), ("titleLines", value: "\(titleLines)"), ("titleSelectable", value: "\(titleSelectable)"), ("useMarkup", value: "\(useMarkup)"), ("useUnderline", value: "\(useUnderline)"), ("activated", value: "\(activated)"), ("app", value: "\(app)"), ("window", value: "\(window)")]
}

/// The debug tree's content.
public var debugTreeContent: [(String, body: Body)] {
var content: [(String, body: Body)] = [("activatableWidget", body: self.activatableWidget?() ?? []),]

content.append(("suffix", body: self.suffix()))
content.append(("prefix", body: self.prefix()))
return content
}

/// Initialize `ActionRow`.
public init() {
}
Expand Down
14 changes: 13 additions & 1 deletion Sources/Adwaita/View/Generated/Avatar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Avatar.swift
// Adwaita
//
// Created by auto-generation on 28.04.24.
// Created by auto-generation on 02.05.24.
//

import CAdw
Expand Down Expand Up @@ -55,6 +55,18 @@ public struct Avatar: Widget {
/// The window.
var window: GTUIApplicationWindow?

/// The debug tree parameters.
public var debugTreeParameters: [(String, value: CustomStringConvertible)] {
[("iconName", value: "\(iconName)"), ("showInitials", value: "\(showInitials)"), ("size", value: "\(size)"), ("text", value: "\(text)"), ("app", value: "\(app)"), ("window", value: "\(window)")]
}

/// The debug tree's content.
public var debugTreeContent: [(String, body: Body)] {
var content: [(String, body: Body)] = []

return content
}

/// Initialize `Avatar`.
public init(showInitials: Bool, size: Int) {
self.showInitials = showInitials
Expand Down
14 changes: 13 additions & 1 deletion Sources/Adwaita/View/Generated/Banner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Banner.swift
// Adwaita
//
// Created by auto-generation on 28.04.24.
// Created by auto-generation on 02.05.24.
//

import CAdw
Expand Down Expand Up @@ -60,6 +60,18 @@ public struct Banner: Widget {
/// The window.
var window: GTUIApplicationWindow?

/// The debug tree parameters.
public var debugTreeParameters: [(String, value: CustomStringConvertible)] {
[("buttonLabel", value: "\(buttonLabel)"), ("revealed", value: "\(revealed)"), ("title", value: "\(title)"), ("useMarkup", value: "\(useMarkup)"), ("buttonClicked", value: "\(buttonClicked)"), ("app", value: "\(app)"), ("window", value: "\(window)")]
}

/// The debug tree's content.
public var debugTreeContent: [(String, body: Body)] {
var content: [(String, body: Body)] = []

return content
}

/// Initialize `Banner`.
public init(title: String) {
self.title = title
Expand Down
14 changes: 13 additions & 1 deletion Sources/Adwaita/View/Generated/Bin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Bin.swift
// Adwaita
//
// Created by auto-generation on 28.04.24.
// Created by auto-generation on 02.05.24.
//

import CAdw
Expand Down Expand Up @@ -31,6 +31,18 @@ public struct Bin: Widget {
/// The window.
var window: GTUIApplicationWindow?

/// The debug tree parameters.
public var debugTreeParameters: [(String, value: CustomStringConvertible)] {
[("app", value: "\(app)"), ("window", value: "\(window)")]
}

/// The debug tree's content.
public var debugTreeContent: [(String, body: Body)] {
var content: [(String, body: Body)] = [("child", body: self.child?() ?? []),]

return content
}

/// Initialize `Bin`.
public init() {
}
Expand Down
Loading

0 comments on commit e0a03f1

Please sign in to comment.