forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSplitButton.swift
More file actions
116 lines (95 loc) · 3.5 KB
/
SplitButton.swift
File metadata and controls
116 lines (95 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import SwiftUI
import AppKit
// MARK: - SplitButton Menu Item
public struct SplitButtonMenuItem: Identifiable {
public let id = UUID()
public let title: String
public let action: () -> Void
public init(title: String, action: @escaping () -> Void) {
self.title = title
self.action = action
}
}
// MARK: - SplitButton using NSComboButton
@available(macOS 13.0, *)
public struct SplitButton: NSViewRepresentable {
let title: String
let primaryAction: () -> Void
let isDisabled: Bool
let menuItems: [SplitButtonMenuItem]
public init(
title: String,
isDisabled: Bool = false,
primaryAction: @escaping () -> Void,
menuItems: [SplitButtonMenuItem] = []
) {
self.title = title
self.isDisabled = isDisabled
self.primaryAction = primaryAction
self.menuItems = menuItems
}
public func makeNSView(context: Context) -> NSComboButton {
let button = NSComboButton()
button.title = title
button.target = context.coordinator
button.action = #selector(Coordinator.handlePrimaryAction)
button.isEnabled = !isDisabled
context.coordinator.button = button
context.coordinator.updateMenu(with: menuItems)
return button
}
public func updateNSView(_ nsView: NSComboButton, context: Context) {
nsView.title = title
nsView.isEnabled = !isDisabled
context.coordinator.updateMenu(with: menuItems)
}
public func makeCoordinator() -> Coordinator {
Coordinator(primaryAction: primaryAction)
}
public class Coordinator: NSObject {
let primaryAction: () -> Void
weak var button: NSComboButton?
private var menuItemActions: [UUID: () -> Void] = [:]
init(primaryAction: @escaping () -> Void) {
self.primaryAction = primaryAction
}
@objc func handlePrimaryAction() {
primaryAction()
}
@objc func handleMenuItemAction(_ sender: NSMenuItem) {
if let itemId = sender.representedObject as? UUID,
let action = menuItemActions[itemId] {
action()
}
}
func updateMenu(with items: [SplitButtonMenuItem]) {
let menu = NSMenu()
menuItemActions.removeAll()
// Add fixed menu title if there are items
if !items.isEmpty {
if #available(macOS 14.0, *) {
let headerItem = NSMenuItem.sectionHeader(title: "Install Server With")
menu.addItem(headerItem)
} else {
let headerItem = NSMenuItem()
headerItem.title = "Install Server With"
headerItem.isEnabled = false
menu.addItem(headerItem)
}
// Add menu items
for item in items {
let menuItem = NSMenuItem(
title: item.title,
action: #selector(handleMenuItemAction(_:)),
keyEquivalent: ""
)
menuItem.target = self
menuItem.representedObject = item.id
menuItemActions[item.id] = item.action
menu.addItem(menuItem)
}
}
button?.menu = menu
}
}
}