Skip to content

Commit

Permalink
AppKitBackend: Implement text field
Browse files Browse the repository at this point in the history
  • Loading branch information
stackotter committed Apr 7, 2024
1 parent 0bff1ce commit 81b8447
Showing 1 changed file with 58 additions and 2 deletions.
60 changes: 58 additions & 2 deletions Sources/AppKitBackend/AppKitBackend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,52 @@ public struct AppKitBackend: AppBackend {
public func setForegroundColor(ofStyleContainer container: NSView, to color: Color) {
// TODO: Implement foreground color
}

public func createTextField() -> NSView {
return NSObservableTextField()
}

public func updateTextField(
_ textField: NSView, placeholder: String, onChange: @escaping (String) -> Void
) {
let textField = textField as! NSObservableTextField
textField.placeholderString = placeholder
textField.onEdit = { textField in
onChange(textField.stringValue)
}
}

public func getContent(ofTextField textField: NSView) -> String {
let textField = textField as! NSTextField
return textField.stringValue
}

public func setContent(ofTextField textField: NSView, to content: String) {
let textField = textField as! NSTextField
textField.stringValue = content
}

public func createScrollContainer(for child: NSView) -> NSView {
let scrollView = NSScrollView()
scrollView.addSubview(child)
return scrollView
}

public func createLayoutTransparentStack() -> NSView {
return NSStackView()
}

public func updateLayoutTransparentStack(_ container: NSView) {
let stack = container as! NSStackView
// Inherit orientation of nearest oriented parent (defaulting to vertical)
stack.orientation =
getInheritedOrientation(of: stack) == .horizontal ? .horizontal : .vertical
}

public func addChild(_ child: NSView, toLayoutTransparentStack container: NSView) {
let stack = container as! NSStackView
stack.addView(child, in: .bottom)
}
}

// Source: https://gist.github.com/sindresorhus/3580ce9426fff8fafb1677341fca4815
Expand Down Expand Up @@ -323,16 +369,26 @@ final class ObjectAssociation<T: Any> {
}
}

class NSObservableTextField: NSTextField {
override func textDidChange(_ notification: Notification) {
onEdit?(self)
}

var onEdit: ((NSTextField) -> Void)?
}

// Source: https://gist.github.com/sindresorhus/3580ce9426fff8fafb1677341fca4815
extension NSControl {
typealias ActionClosure = ((NSControl) -> Void)
typealias EditClosure = ((NSTextField) -> Void)

private struct AssociatedKeys {
struct AssociatedKeys {
static let onActionClosure = ObjectAssociation<ActionClosure>()
static let onEditClosure = ObjectAssociation<EditClosure>()
}

@objc
private func callClosure(_ sender: NSControl) {
func callClosure(_ sender: NSControl) {
onAction?(sender)
}

Expand Down

0 comments on commit 81b8447

Please sign in to comment.