From 21e28344c187fc9bba218919b2a90dec38a7121d Mon Sep 17 00:00:00 2001 From: david-swift Date: Thu, 18 Jul 2024 16:07:37 +0200 Subject: [PATCH] Move from renderable elements to views --- .../Button/ButtonCollection.swift | 59 +++++++++++++++ .../TermKitBackend/Button/ButtonContext.swift | 21 ++++++ Sources/TermKitBackend/Menu/Menu.swift | 36 +++++----- .../TermKitBackend/Menu/MenuCollection.swift | 72 +++++++++++++++++++ Sources/TermKitBackend/Menu/MenuContext.swift | 21 ++++++ .../Model/Extensions/Button+.swift | 47 ------------ Sources/TermKitBackend/Model/TermKitApp.swift | 6 +- .../Model/TermKitMainView.swift | 16 +++++ Sources/TermKitBackend/Scene/MenuBar.swift | 7 +- Sources/TermKitBackend/Scene/Window.swift | 5 +- Sources/TermKitBackend/View/Button.swift | 32 +++++++-- Sources/TermKitBackend/View/Checkbox.swift | 13 ++-- Sources/TermKitBackend/View/Dialogs/Box.swift | 25 +++---- Sources/TermKitBackend/View/Frame.swift | 13 ++-- Sources/TermKitBackend/View/HStack.swift | 12 ++-- Sources/TermKitBackend/View/Label.swift | 13 ++-- Sources/TermKitBackend/View/ListView.swift | 13 ++-- Sources/TermKitBackend/View/ProgressBar.swift | 13 ++-- Sources/TermKitBackend/View/ScrollView.swift | 13 ++-- Sources/TermKitBackend/View/TextField.swift | 13 ++-- Sources/TermKitBackend/View/VStack.swift | 12 ++-- Sources/TermKitBackend/View/ZStack.swift | 12 ++-- Sources/TestApp/TestApp.swift | 21 +++--- 23 files changed, 335 insertions(+), 160 deletions(-) create mode 100644 Sources/TermKitBackend/Button/ButtonCollection.swift create mode 100644 Sources/TermKitBackend/Button/ButtonContext.swift create mode 100644 Sources/TermKitBackend/Menu/MenuCollection.swift create mode 100644 Sources/TermKitBackend/Menu/MenuContext.swift delete mode 100644 Sources/TermKitBackend/Model/Extensions/Button+.swift create mode 100644 Sources/TermKitBackend/Model/TermKitMainView.swift diff --git a/Sources/TermKitBackend/Button/ButtonCollection.swift b/Sources/TermKitBackend/Button/ButtonCollection.swift new file mode 100644 index 0000000..0adf187 --- /dev/null +++ b/Sources/TermKitBackend/Button/ButtonCollection.swift @@ -0,0 +1,59 @@ +// +// ButtonCollection.swift +// TermKitBackend +// +// Created by david-swift on 18.07.2024. +// + +import TermKit + +/// A menu is an item of a ``MenuBar``. +public struct ButtonCollection: ButtonContext.Widget, Wrapper { + + /// The content of the menu. + var content: Body + + /// Initialize a menu. + /// - Parameters: + /// - label: The menu's label, displayed in the menu bar. + /// - content: The content of the menu. + public init(@ViewBuilder content: @escaping () -> Body) { + self.content = content() + } + + /// The view storage. + /// - Parameters: + /// - modifiers: Modify the views before updating. + /// - type: The type of the views. + /// - Returns: The view storage. + public func container( + modifiers: [(any AnyView) -> any AnyView], + type: Data.Type + ) -> ViewStorage where Data: ViewRenderData { + var buttons: [Button] = [] + for element in content.storages(modifiers: modifiers, type: type) { + if let button = element.pointer as? Button { + buttons.append(button) + } else if let collection = element.pointer as? [Button] { + buttons += collection + } + } + return .init(buttons, content: [.mainContent: content.storages(modifiers: modifiers, type: type)]) + } + + /// Update the stored content. + /// - Parameters: + /// - storage: The storage to update. + /// - modifiers: Modify the views before updating. + /// - updateProperties: Whether to update the properties. + /// - type: The type of the views. + public func update( + _ storage: ViewStorage, + modifiers: [(AnyView) -> AnyView], + updateProperties: Bool, + type: Data.Type + ) where Data: ViewRenderData { + // Buttons in dialogs cannot be updated + } + +} diff --git a/Sources/TermKitBackend/Button/ButtonContext.swift b/Sources/TermKitBackend/Button/ButtonContext.swift new file mode 100644 index 0000000..a47846c --- /dev/null +++ b/Sources/TermKitBackend/Button/ButtonContext.swift @@ -0,0 +1,21 @@ +// +// ButtonContext.swift +// TermKitBackend +// +// Created by david-swift on 18.07.2024. +// + +import TermKit + +/// The menu items view context. +public enum ButtonContext: ViewRenderData { + + /// The type of the widgets. + public typealias WidgetType = Widget + /// The wrapper type. + public typealias WrapperType = ButtonCollection + + /// The type of the widgets. + public protocol Widget: Meta.Widget { } + +} diff --git a/Sources/TermKitBackend/Menu/Menu.swift b/Sources/TermKitBackend/Menu/Menu.swift index 43b6082..4356469 100644 --- a/Sources/TermKitBackend/Menu/Menu.swift +++ b/Sources/TermKitBackend/Menu/Menu.swift @@ -8,48 +8,52 @@ import TermKit /// A menu is an item of a ``MenuBar``. -public struct Menu: Renderable { +public struct Menu: MenuContext.Widget { /// The menu's label, displayed in the menu bar. var label: String /// The content of the menu. - var content: [Button] + var content: Body /// Initialize a menu. /// - Parameters: /// - label: The menu's label, displayed in the menu bar. /// - content: The content of the menu. - public init(_ label: String, @Builder