term-kit-backend/Sources/TermKitBackend/Scene/MenuBar.swift

63 lines
1.8 KiB
Swift
Raw Normal View History

2024-07-10 14:41:11 +02:00
//
// MenuBar.swift
// TermKitBackend
//
// Created by david-swift on 01.07.2024.
//
import TermKit
/// The menu bar scene element adds a menu bar to the top of the app.
2024-07-19 23:41:19 +02:00
struct MenuBar: TermKitSceneElement {
2024-07-10 14:41:11 +02:00
/// The identifier of the menu bar.
2024-07-19 23:41:19 +02:00
var id: String
2024-07-10 14:41:11 +02:00
/// The menu bar's content.
2024-07-18 16:07:37 +02:00
var content: Body
2024-07-10 14:41:11 +02:00
/// Initialize the menu bar.
/// - Parameters:
/// - id: The identifier of the menu bar.
/// - content: The menu bar's content.
2024-07-19 23:41:19 +02:00
init(id: String = "main-menu", @ViewBuilder content: () -> Body) {
2024-07-10 14:41:11 +02:00
self.id = id
self.content = content()
}
/// Set up the initial scene storages.
/// - Parameter app: The app storage.
2024-07-19 23:41:19 +02:00
func setupInitialContainers<Storage>(app: Storage) where Storage: AppStorage {
2024-07-10 14:41:11 +02:00
app.storage.sceneStorage.append(container(app: app))
}
/// The scene storage.
/// - Parameter app: The app storage.
2024-07-19 23:41:19 +02:00
func container<Storage>(app: Storage) -> SceneStorage where Storage: AppStorage {
2024-07-18 16:07:37 +02:00
let items = MenuCollection { content }.container(modifiers: [], type: MenuContext.self)
2024-07-10 14:41:11 +02:00
let menubar = TermKit.MenuBar(
2024-07-18 16:07:37 +02:00
menus: items.pointer as? [TermKit.MenuBarItem] ?? []
2024-07-10 14:41:11 +02:00
)
2024-07-19 23:41:19 +02:00
for element in Application.top.subviews {
element.y = .bottom(of: menubar)
2024-07-10 14:41:11 +02:00
}
2024-07-19 23:41:19 +02:00
Application.top.addSubview(menubar)
2024-07-10 14:41:11 +02:00
return .init(id: id, pointer: menubar) {
menubar.ensureFocus()
}
}
/// Update the stored content.
/// - Parameters:
/// - storage: The storage to update.
2024-07-12 21:56:21 +02:00
/// - app: The app storage.
2024-07-10 14:41:11 +02:00
/// - updateProperties: Whether to update the view's properties.
2024-07-19 23:41:19 +02:00
func update<Storage>(
2024-07-10 14:41:11 +02:00
_ storage: SceneStorage,
app: Storage,
updateProperties: Bool
) where Storage: AppStorage {
Application.refresh()
}
}