Skip to content

Commit

Permalink
AppKitBackend: Add Toggle view support
Browse files Browse the repository at this point in the history
  • Loading branch information
stackotter committed Apr 7, 2024
1 parent 755936a commit 337d2b8
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 16 deletions.
30 changes: 14 additions & 16 deletions Examples/Sources/ControlsExample/ControlsApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,39 @@ class ControlsState: Observable {

@main
struct ControlsApp: App {
typealias Backend = SelectedBackend

let identifier = "dev.stackotter.Controls"

let state = ControlsState()

var body: some Scene {
WindowGroup("ControlsApp") {
HStack {
VStack {
VStack {
Text("Button")
Button("Click me!") {
state.count += 1
}
Text("Count: \(state.count)", wrap: false)
Spacer()
Text("Count: \(state.count)")
}
Spacer()
.padding(.bottom, 20)

VStack {
Text("Toggle (Button Style)")
Text("Toggle button")
Toggle("Toggle me!", active: state.$exampleButtonState)
.toggleStyle(.button)
Text("Currently enabled: \(state.exampleButtonState)")
Spacer()
.padding(.bottom, 10)
Text("Toggle (Switch Style)")
HStack {
Toggle("Toggle me:", active: state.$exampleSwitchState)
.toggleStyle(.switch)
Spacer()
.padding(.bottom, 10)
}
}
.padding(.bottom, 20)

VStack {
Text("Toggle switch")
Toggle("Toggle me:", active: state.$exampleSwitchState)
.toggleStyle(.switch)
Text("Currently enabled: \(state.exampleSwitchState)")
Spacer()
}
}
.padding(10)
}
}
}
82 changes: 82 additions & 0 deletions Sources/AppKitBackend/AppKitBackend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,88 @@ public struct AppKitBackend: AppBackend {
view.edgeInsets.left = CGFloat(leading)
view.edgeInsets.right = CGFloat(trailing)
}

public func createSpacer() -> NSView {
return NSView()
}

public func updateSpacer(
_ spacer: NSView, expandHorizontally: Bool, expandVertically: Bool, minSize: Int
) {
// TODO: Update spacer

Check warning on line 147 in Sources/AppKitBackend/AppKitBackend.swift

View workflow job for this annotation

GitHub Actions / swift-lint

Todo Violation: TODOs should be resolved (Update spacer) (todo)
}

public func createSwitch() -> NSView {
return NSSwitch()
}

public func updateSwitch(_ toggleSwitch: NSView, onChange: @escaping (Bool) -> Void) {
let toggleSwitch = toggleSwitch as! NSSwitch
toggleSwitch.onAction = { toggleSwitch in
let toggleSwitch = toggleSwitch as! NSSwitch
onChange(toggleSwitch.state == .on)
}
}

public func setState(ofSwitch toggleSwitch: NSView, to state: Bool) {
let toggleSwitch = toggleSwitch as! NSSwitch
toggleSwitch.state = state ? .on : .off
}

public func createToggle() -> NSView {
let toggle = NSButton()
toggle.setButtonType(.pushOnPushOff)
return toggle
}

public func updateToggle(_ toggle: NSView, label: String, onChange: @escaping (Bool) -> Void) {
let toggle = toggle as! NSButton
toggle.title = label
toggle.onAction = { toggle in
let toggle = toggle as! NSButton
onChange(toggle.state == .on)
}
}

public func setState(ofToggle toggle: NSView, to state: Bool) {
let toggle = toggle as! NSButton
toggle.state = state ? .on : .off
}

public func getInheritedOrientation(of widget: NSView) -> InheritedOrientation? {
guard let superview = widget.superview else {
return nil
}

if let stack = superview as? NSStackView {
switch stack.orientation {
case .horizontal:
return .horizontal
case .vertical:
return .vertical
default:
break
}
}

return getInheritedOrientation(of: superview)
}

public func createSingleChildContainer() -> NSView {
let container = NSStackView()
container.alignment = .centerY
return container
}

public func setChild(ofSingleChildContainer container: NSView, to widget: NSView?) {
let container = container as! NSStackView
for child in container.arrangedSubviews {
container.removeView(child)
}
if let widget = widget {
container.addView(widget, in: .center)
}
}
}

// Source: https://gist.github.com/sindresorhus/3580ce9426fff8fafb1677341fca4815
Expand Down

0 comments on commit 337d2b8

Please sign in to comment.