Skip to content

Commit

Permalink
Simplify the AppBackend API to reduce the number of methods and improve
Browse files Browse the repository at this point in the history
consistency

Before there were a lot of atomic methods, but SwiftCrossUI never needs to call them
separately, so it's simpler to combine them into a single 'update' method for each
widget type
  • Loading branch information
stackotter committed Dec 11, 2023
1 parent 26aa77c commit ad16aa4
Show file tree
Hide file tree
Showing 29 changed files with 614 additions and 841 deletions.
32 changes: 9 additions & 23 deletions Sources/AppKitBackend/AppKitBackend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,18 @@ public struct AppKitBackend: AppBackend {

public func show(widget: Widget) {}

public func createTextView(content: String, shouldWrap: Bool) -> Widget {
if shouldWrap {
return NSTextField(wrappingLabelWithString: content)
} else {
return NSTextField(labelWithString: content)
}
public func createTextView() -> Widget {
return NSTextField(wrappingLabelWithString: "")
}

public func setContent(ofTextView textView: Widget, to content: String) {
public func updateTextView(_ textView: Widget, content: String, shouldWrap: Bool) {
// TODO: Implement text wrap handling
(textView as! NSTextField).stringValue = content
}

public func setWrap(ofTextView textView: Widget, to shouldWrap: Bool) {}

public func createVStack(spacing: Int) -> Widget {
public func createVStack() -> Widget {
let view = NSStackView()
view.orientation = .vertical
view.spacing = CGFloat(spacing)
return view
}

Expand All @@ -88,10 +82,9 @@ public struct AppKitBackend: AppBackend {
(widget as! NSStackView).spacing = CGFloat(spacing)
}

public func createHStack(spacing: Int) -> Widget {
public func createHStack() -> Widget {
let view = NSStackView()
view.orientation = .horizontal
view.spacing = CGFloat(spacing)
return view
}

Expand All @@ -103,19 +96,12 @@ public struct AppKitBackend: AppBackend {
(widget as! NSStackView).spacing = CGFloat(spacing)
}

public func createButton(label: String, action: @escaping () -> Void) -> Widget {
let button = NSButton(title: label, target: nil, action: nil)
button.onAction = { _ in
action()
}
return button
public func createButton() -> Widget {
return NSButton(title: "", target: nil, action: nil)
}

public func setLabel(ofButton button: Widget, to label: String) {
public func updateButton(_ button: Widget, label: String, action: @escaping () -> Void) {
(button as! NSButton).title = label
}

public func setAction(ofButton button: Widget, to action: @escaping () -> Void) {
(button as! NSButton).onAction = { _ in
action()
}
Expand Down
22 changes: 9 additions & 13 deletions Sources/CursesBackend/CursesBackend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public final class CursesBackend: AppBackend {
widget.setNeedsDisplay()
}

public func createVStack(spacing: Int) -> Widget {
public func createVStack() -> Widget {
return View()
}

Expand All @@ -64,7 +64,7 @@ public final class CursesBackend: AppBackend {

public func setSpacing(ofVStack container: Widget, to spacing: Int) {}

public func createHStack(spacing: Int) -> Widget {
public func createHStack() -> Widget {
return View()
}

Expand All @@ -76,30 +76,26 @@ public final class CursesBackend: AppBackend {

public func setSpacing(ofHStack container: Widget, to spacing: Int) {}

public func createTextView(content: String, shouldWrap: Bool) -> Widget {
let label = Label(content)
public func createTextView() -> Widget {
let label = Label("")
label.width = Dim.fill()
return label
}

public func setContent(ofTextView textView: Widget, to content: String) {
public func updateTextView(_ textView: Widget, content: String, shouldWrap: Bool) {
// TODO: Implement text wrap handling
let label = textView as! Label
label.text = content
}

public func setWrap(ofTextView textView: Widget, to shouldWrap: Bool) {}

public func createButton(label: String, action: @escaping () -> Void) -> Widget {
let button = TermKit.Button(label, clicked: action)
public func createButton() -> Widget {
let button = TermKit.Button("")
button.height = Dim.sized(1)
return button
}

public func setLabel(ofButton button: Widget, to label: String) {
public func updateButton(_ button: Widget, label: String, action: @escaping () -> Void) {
(button as! TermKit.Button).text = label
}

public func setAction(ofButton button: Widget, to action: @escaping () -> Void) {
(button as! TermKit.Button).clicked = { _ in
action()
}
Expand Down
Loading

0 comments on commit ad16aa4

Please sign in to comment.