Skip to content

Commit

Permalink
Update demo and fix bugs
Browse files Browse the repository at this point in the history
- Fix build optional in ViewBuilder
- Fix maximum size frame modifier
- Improve inspector wrapper
- Improve header bar
- Improve status page
- Improve the naming of some elements
  • Loading branch information
david-swift committed Oct 12, 2023
1 parent 22c10d6 commit 921f025
Show file tree
Hide file tree
Showing 29 changed files with 451 additions and 101 deletions.
2 changes: 2 additions & 0 deletions Documentation/Reference/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

- [Binding](structs/Binding.md)
- [Button](structs/Button.md)
- [Clamp](structs/Clamp.md)
- [EitherView](structs/EitherView.md)
- [HStack](structs/HStack.md)
- [HeaderBar](structs/HeaderBar.md)
Expand Down Expand Up @@ -57,6 +58,7 @@
## Typealiases

- [Body](typealiases/Body.md)
- [GTUIWindow](typealiases/GTUIWindow.md)
- [Scene](typealiases/Scene.md)
- [SceneBuilder](typealiases/SceneBuilder.md)

Expand Down
14 changes: 7 additions & 7 deletions Documentation/Reference/extensions/View.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ Update a storage to a view.
Get a storage.
- Returns: The storage.

### `frame(maxSize:)`

Set the view's maximal size.
- Parameter maxSize: The maximal size.
- Returns: A view.

### `inspect(_:)`

Modify a GTUI widget before being displayed.
Modify a GTUI widget before being displayed and when being updated.
- Parameter modify: Modify the widget.
- Returns: A view.

Expand Down Expand Up @@ -64,12 +70,6 @@ Set the view's minimal width or height.
- minHeight: The minimal height.
- Returns: A view.

### `frame(maxSize:)`

Set the view's maximal size.
- Parameter maxSize: The maximal size.
- Returns: A view.

### `transition(_:)`

Set the view's transition.
Expand Down
2 changes: 1 addition & 1 deletion Documentation/Reference/extensions/WindowScene.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
# `WindowScene`

## Properties
### `body`
### `scene`

The window scene's body is itself.
2 changes: 1 addition & 1 deletion Documentation/Reference/protocols/WindowSceneGroup.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
A structure conforming to `WindowScene` can be added to an app's `scene`.

## Properties
### `body`
### `scene`

The group's content.
25 changes: 25 additions & 0 deletions Documentation/Reference/structs/Clamp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
**STRUCT**

# `Clamp`

A horizontal AdwClamp equivalent.

## Properties
### `content`

The content.

### `maxSize`

The maximum size.

## Methods
### `update(_:)`

Update a view storage.
- Parameter storage: The view storage.

### `container()`

Get a view storage.
- Returns: The view storage.
21 changes: 20 additions & 1 deletion Documentation/Reference/structs/HeaderBar.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ The start content of the header bar.

The end content of the header bar.

### `titleButtons`

Whether the title buttons are visible.

### `headerBarTitle`

The view acting as the title of the header bar.

### `startID`

The start content's id.
Expand All @@ -21,11 +29,16 @@ The start content's id.

The end content's id.

### `titleID`

The title's id.

## Methods
### `init(start:end:)`
### `init(titleButtons:start:end:)`

Initialize a header bar.
- Parameters:
- titleButtons: Whether the title buttons (e.g. close button) are visible.
- start: The start content.
- end: The end content.

Expand Down Expand Up @@ -55,3 +68,9 @@ Update a header bar's view storage.

Get the container for a header bar.
- Returns: The view storage.

### `headerBarTitle(view:)`

Set the title widget for the header bar.
- Parameter view: The widget in the header bar.
- Returns: The header bar.
2 changes: 1 addition & 1 deletion Documentation/Reference/structs/InspectorWrapper.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# `InspectorWrapper`

A widget which executes a custom code on the GTUI widget when being created.
A widget which executes a custom code on the GTUI widget when being created and updated.

## Properties
### `modify`
Expand Down
2 changes: 1 addition & 1 deletion Documentation/Reference/structs/List.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ The identifier of the selected element.
## Methods
### `init(_:selection:content:)`

Initialize `ForEach`.
Initialize `List`.
- Parameters:
- elements: The elements.
- selection: The identifier of the selected element.
Expand Down
5 changes: 5 additions & 0 deletions Documentation/Reference/typealiases/GTUIWindow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
**TYPEALIAS**

# `GTUIWindow`

A GTUI window.
Binary file modified Icons/Demo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions Sources/Adwaita/Model/User Interface/GTUIWindow.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//
// GTUIWindow.swift
// Adwaita
//
// Created by david-swift on 12.10.23.
//

import GTUI

/// A GTUI window.
public typealias GTUIWindow = GTUI.Window
6 changes: 5 additions & 1 deletion Sources/Adwaita/Model/User Interface/ViewBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ public enum ViewBuilder {
/// - Parameter component: An optional component.
/// - Returns: A nonoptional component.
public static func buildOptional(_ component: Component?) -> Component {
component ?? .components([])
if let component {
return .element(EitherView(true, { buildFinalResult(component) }, else: nil))
} else {
return .element(EitherView(false, nil) { [] })
}
}

/// Enables support for `if`-`else` and `switch` statements.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Adwaita/Model/User Interface/WindowScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ public protocol WindowScene: WindowSceneGroup {
extension WindowScene {

/// The window scene's body is itself.
@SceneBuilder public var body: Scene { self }
@SceneBuilder public var scene: Scene { self }

}
4 changes: 2 additions & 2 deletions Sources/Adwaita/Model/User Interface/WindowSceneGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
public protocol WindowSceneGroup {

/// The group's content.
@SceneBuilder var body: Scene { get }
@SceneBuilder var scene: Scene { get }

}

Expand All @@ -19,7 +19,7 @@ extension WindowSceneGroup {
/// - Returns: The windows.
func windows() -> [WindowScene] {
var content: [WindowScene] = []
for element in body {
for element in scene {
if let window = element as? WindowScene {
content.append(window)
} else {
Expand Down
4 changes: 2 additions & 2 deletions Sources/Adwaita/Model/User Interface/WindowStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class WindowStorage {
/// Whether the reference to the window should disappear in the next update.
public var destroy = false
/// The GTUI window.
public var window: GTUI.Window
public var window: GTUIWindow
/// The content's storage.
public var view: ViewStorage

Expand All @@ -24,7 +24,7 @@ public class WindowStorage {
/// - id: The window's identifier.
/// - window: The GTUI window.
/// - view: The content's storage.
public init(id: String, window: GTUI.Window, view: ViewStorage) {
public init(id: String, window: GTUIWindow, view: ViewStorage) {
self.id = id
self.window = window
self.view = view
Expand Down
36 changes: 34 additions & 2 deletions Sources/Adwaita/View/HeaderBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,25 @@ public struct HeaderBar: Widget {
var start: Body
/// The end content of the header bar.
var end: Body
/// Whether the title buttons are visible.
var titleButtons: Bool
/// The view acting as the title of the header bar.
var headerBarTitle: Body?

/// The start content's id.
let startID = "start"
/// The end content's id.
let endID = "end"
/// The title's id.
let titleID = "title"

/// Initialize a header bar.
/// - Parameters:
/// - titleButtons: Whether the title buttons (e.g. close button) are visible.
/// - start: The start content.
/// - end: The end content.
public init(@ViewBuilder start: () -> Body, @ViewBuilder end: () -> Body) {
public init(titleButtons: Bool = true, @ViewBuilder start: () -> Body, @ViewBuilder end: () -> Body) {
self.titleButtons = titleButtons
self.start = start()
self.end = end()
}
Expand Down Expand Up @@ -52,8 +60,14 @@ public struct HeaderBar: Widget {
/// Update a header bar's view storage.
/// - Parameter storage: The view storage.
public func update(_ storage: ViewStorage) {
if let bar = storage.view as? GTUI.HeaderBar {
_ = bar.showTitleButtons(titleButtons)
}
start.update(storage.content[startID] ?? [])
end.update(storage.content[endID] ?? [])
if let first = storage.content[titleID]?.first {
headerBarTitle?.widget().update(first)
}
}

/// Get the container for a header bar.
Expand All @@ -72,7 +86,25 @@ public struct HeaderBar: Widget {
_ = bar.packEnd(element.view)
endContent.append(element)
}
return .init(bar, content: [startID: startContent, endID: endContent])
let title = headerBarTitle?.widget().container()
let titleStorage: [ViewStorage]
if let title {
_ = bar.titleWidget(title.view)
titleStorage = [title]
} else {
titleStorage = []
}
_ = bar.showTitleButtons(titleButtons)
return .init(bar, content: [startID: startContent, endID: endContent, titleID: titleStorage])
}

/// Set the title widget for the header bar.
/// - Parameter view: The widget in the header bar.
/// - Returns: The header bar.
public func headerBarTitle(@ViewBuilder view: () -> Body) -> Self {
var newSelf = self
newSelf.headerBarTitle = view()
return newSelf
}

}
2 changes: 1 addition & 1 deletion Sources/Adwaita/View/List.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public struct List<Element>: Widget where Element: Identifiable {
/// The identifier of the selected element.
@Binding var selection: Element.ID

/// Initialize `ForEach`.
/// Initialize `List`.
/// - Parameters:
/// - elements: The elements.
/// - selection: The identifier of the selected element.
Expand Down
49 changes: 49 additions & 0 deletions Sources/Adwaita/View/Modifiers/Clamp.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// Clamp.swift
// Adwaita
//
// Created by david-swift on 12.10.23.
//

import GTUI

/// A horizontal AdwClamp equivalent.
struct Clamp: Widget {

/// The content.
var content: View
/// The maximum size.
var maxSize: Int

/// Update a view storage.
/// - Parameter storage: The view storage.
func update(_ storage: ViewStorage) {
if let clamp = storage.view as? GTUI.Clamp {
_ = clamp.maximumSize(maxSize)
}
if let storage = storage.content[.mainContent]?[safe: 0] {
content.widget().update(storage)
}
}

/// Get a view storage.
/// - Returns: The view storage.
func container() -> ViewStorage {
let container = content.storage()
let clamp: GTUI.Clamp = .init(container.view)
_ = clamp.maximumSize(maxSize)
return .init(clamp, content: [.mainContent: [container]])
}

}

extension View {

/// Set the view's maximal size.
/// - Parameter maxSize: The maximal size.
/// - Returns: A view.
public func frame(maxSize: Int? = nil) -> View {
Clamp(content: self, maxSize: maxSize ?? -1)
}

}
12 changes: 3 additions & 9 deletions Sources/Adwaita/View/Modifiers/InspectorWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import GTUI

/// A widget which executes a custom code on the GTUI widget when being created.
/// A widget which executes a custom code on the GTUI widget when being created and updated.
struct InspectorWrapper: Widget {

/// The custom code to edit the widget.
Expand All @@ -27,13 +27,14 @@ struct InspectorWrapper: Widget {
/// - Parameter storage: The content's storage.
func update(_ storage: ViewStorage) {
content.updateStorage(storage)
modify(storage.view)
}

}

extension View {

/// Modify a GTUI widget before being displayed.
/// Modify a GTUI widget before being displayed and when being updated.
/// - Parameter modify: Modify the widget.
/// - Returns: A view.
public func inspect(_ modify: @escaping (NativeWidgetPeer?) -> Void) -> View {
Expand Down Expand Up @@ -86,13 +87,6 @@ extension View {
inspect { _ = $0?.frame(minWidth: minWidth, minHeight: minHeight) }
}

/// Set the view's maximal size.
/// - Parameter maxSize: The maximal size.
/// - Returns: A view.
public func frame(maxSize: Int? = nil) -> View {
inspect { _ = $0?.frame(maxSize: maxSize) }
}

/// Set the view's transition.
/// - Parameter transition: The transition.
/// - Returns: A view.
Expand Down

0 comments on commit 921f025

Please sign in to comment.