|
| 1 | +import Foundation |
| 2 | +import SwiftCrossUI |
| 3 | +import TermKit |
| 4 | + |
| 5 | +public struct CursesBackend: AppBackend { |
| 6 | + public typealias Widget = TermKit.View |
| 7 | + |
| 8 | + public init(appIdentifier: String) {} |
| 9 | + |
| 10 | + public func run<AppRoot: App>( |
| 11 | + _ app: AppRoot, |
| 12 | + _ setViewGraph: @escaping (ViewGraph<AppRoot>) -> Void |
| 13 | + ) where AppRoot.Backend == Self { |
| 14 | + let viewGraph = ViewGraph(for: app, backend: self) |
| 15 | + setViewGraph(viewGraph) |
| 16 | + |
| 17 | + Application.prepare() |
| 18 | + let root = RootView() |
| 19 | + root.addSubview(viewGraph.rootNode.widget) |
| 20 | + Application.top.addSubview(root) |
| 21 | + Application.run() |
| 22 | + } |
| 23 | + |
| 24 | + public func runInMainThread(action: @escaping () -> Void) { |
| 25 | + DispatchQueue.main.async { |
| 26 | + action() |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + public func show(_ widget: Widget) { |
| 31 | + widget.setNeedsDisplay() |
| 32 | + } |
| 33 | + |
| 34 | + public func createVStack(spacing: Int) -> Widget { |
| 35 | + return View() |
| 36 | + } |
| 37 | + |
| 38 | + public func addChild(_ child: Widget, toVStack container: Widget) { |
| 39 | + // TODO: Properly calculate layout |
| 40 | + child.y = Pos.at(container.subviews.count) |
| 41 | + container.addSubview(child) |
| 42 | + } |
| 43 | + |
| 44 | + public func setSpacing(ofVStack container: Widget, to spacing: Int) {} |
| 45 | + |
| 46 | + public func createHStack(spacing: Int) -> Widget { |
| 47 | + return View() |
| 48 | + } |
| 49 | + |
| 50 | + public func addChild(_ child: Widget, toHStack container: Widget) { |
| 51 | + // TODO: Properly calculate layout |
| 52 | + child.y = Pos.at(container.subviews.count) |
| 53 | + container.addSubview(child) |
| 54 | + } |
| 55 | + |
| 56 | + public func setSpacing(ofHStack container: Widget, to spacing: Int) {} |
| 57 | + |
| 58 | + public func createTextView(content: String, shouldWrap: Bool) -> Widget { |
| 59 | + let label = Label(content) |
| 60 | + label.width = Dim.fill() |
| 61 | + return label |
| 62 | + } |
| 63 | + |
| 64 | + public func setContent(ofTextView textView: Widget, to content: String) { |
| 65 | + let label = textView as! Label |
| 66 | + label.text = content |
| 67 | + } |
| 68 | + |
| 69 | + public func setWrap(ofTextView textView: Widget, to shouldWrap: Bool) {} |
| 70 | + |
| 71 | + public func createButton(label: String, action: @escaping () -> Void) -> Widget { |
| 72 | + let button = TermKit.Button(label, clicked: action) |
| 73 | + button.height = Dim.sized(1) |
| 74 | + return button |
| 75 | + } |
| 76 | + |
| 77 | + public func setLabel(ofButton button: Widget, to label: String) { |
| 78 | + (button as! TermKit.Button).text = label |
| 79 | + } |
| 80 | + |
| 81 | + public func setAction(ofButton button: Widget, to action: @escaping () -> Void) { |
| 82 | + (button as! TermKit.Button).clicked = { _ in |
| 83 | + action() |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + // TODO: Properly implement padding container. Perhaps use a conversion factor to |
| 88 | + // convert the pixel values to 'characters' of padding |
| 89 | + public func createPaddingContainer(for child: Widget) -> Widget { |
| 90 | + return child |
| 91 | + } |
| 92 | + |
| 93 | + public func getChild(ofPaddingContainer container: Widget) -> Widget { |
| 94 | + return container |
| 95 | + } |
| 96 | + |
| 97 | + public func setPadding( |
| 98 | + ofPaddingContainer container: Widget, |
| 99 | + top: Int, |
| 100 | + bottom: Int, |
| 101 | + leading: Int, |
| 102 | + trailing: Int |
| 103 | + ) {} |
| 104 | +} |
| 105 | + |
| 106 | +class RootView: TermKit.View { |
| 107 | + override func processKey(event: KeyEvent) -> Bool { |
| 108 | + if super.processKey(event: event) { |
| 109 | + return true |
| 110 | + } |
| 111 | + |
| 112 | + switch event.key { |
| 113 | + case .controlC, .esc: |
| 114 | + Application.requestStop() |
| 115 | + return true |
| 116 | + default: |
| 117 | + return false |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments