mirror of
https://github.com/coteditor/CotEditor.git
synced 2024-09-19 23:27:42 +03:00
Refactor AddRemoveButton
This commit is contained in:
parent
e0651173b0
commit
2fd6873a57
@ -25,13 +25,7 @@
|
||||
|
||||
import SwiftUI
|
||||
|
||||
protocol EmptyInitializable {
|
||||
|
||||
init()
|
||||
}
|
||||
|
||||
|
||||
struct AddRemoveButton<Item: Identifiable & EmptyInitializable>: View {
|
||||
struct AddRemoveButton<Item: Identifiable>: View {
|
||||
|
||||
@Binding private var items: [Item]
|
||||
@Binding private var selection: Set<Item.ID>
|
||||
@ -39,6 +33,8 @@ struct AddRemoveButton<Item: Identifiable & EmptyInitializable>: View {
|
||||
private var focus: FocusState<Item.ID?>.Binding?
|
||||
@State private var added: Item.ID?
|
||||
|
||||
private var newItem: () -> Item
|
||||
|
||||
|
||||
/// Creates a segmented add/remove control.
|
||||
///
|
||||
@ -46,11 +42,13 @@ struct AddRemoveButton<Item: Identifiable & EmptyInitializable>: View {
|
||||
/// - items: The identifiable data array where adding/removing items.
|
||||
/// - selection: A binding to a set that identifies selected items IDs.
|
||||
/// - focus: A binding to the focus state in the window.
|
||||
init(_ items: Binding<[Item]>, selection: Binding<Set<Item.ID>>, focus: FocusState<Item.ID?>.Binding? = nil) {
|
||||
/// - newItem: A closure to return an item for when adding a new item from the button.
|
||||
init(_ items: Binding<[Item]>, selection: Binding<Set<Item.ID>>, focus: FocusState<Item.ID?>.Binding? = nil, newItem: @escaping () -> Item) {
|
||||
|
||||
self._items = items
|
||||
self._selection = selection
|
||||
self.focus = focus
|
||||
self.newItem = newItem
|
||||
}
|
||||
|
||||
|
||||
@ -58,7 +56,7 @@ struct AddRemoveButton<Item: Identifiable & EmptyInitializable>: View {
|
||||
|
||||
ControlGroup {
|
||||
Button(String(localized: "Add", table: "AddRemoveButton", comment: "button label"), systemImage: "plus") {
|
||||
let item = Item()
|
||||
let item = self.newItem()
|
||||
let index = self.items.lastIndex { self.selection.contains($0.id) } ?? self.items.endIndex - 1
|
||||
|
||||
self.selection.removeAll()
|
||||
|
@ -59,7 +59,7 @@ struct SyntaxCompletionEditView: View {
|
||||
.border(Color(nsColor: .gridColor))
|
||||
|
||||
HStack {
|
||||
AddRemoveButton($items, selection: $selection, focus: $focusedField)
|
||||
AddRemoveButton($items, selection: $selection, focus: $focusedField, newItem: Item.init)
|
||||
Spacer()
|
||||
HelpButton(anchor: "syntax_highlight_settings")
|
||||
}
|
||||
|
@ -112,7 +112,7 @@ struct SyntaxFileMappingEditView: View {
|
||||
.alternatingRowBackgrounds()
|
||||
.border(Color(nsColor: .gridColor))
|
||||
|
||||
AddRemoveButton($items, selection: $selection, focus: $focusedField)
|
||||
AddRemoveButton($items, selection: $selection, focus: $focusedField, newItem: Item.init)
|
||||
}.accessibilityElement(children: .contain)
|
||||
}
|
||||
}
|
||||
|
@ -104,7 +104,7 @@ struct SyntaxHighlightEditView: View {
|
||||
.border(Color(nsColor: .gridColor))
|
||||
|
||||
HStack {
|
||||
AddRemoveButton($items, selection: $selection, focus: $focusedField)
|
||||
AddRemoveButton($items, selection: $selection, focus: $focusedField, newItem: Item.init)
|
||||
Spacer()
|
||||
HelpButton(anchor: self.helpAnchor)
|
||||
}
|
||||
|
@ -77,7 +77,7 @@ import Observation
|
||||
}
|
||||
|
||||
|
||||
struct SyntaxObjectHighlight: Identifiable, EmptyInitializable {
|
||||
struct SyntaxObjectHighlight: Identifiable {
|
||||
|
||||
let id = UUID()
|
||||
|
||||
@ -89,7 +89,7 @@ struct SyntaxObjectHighlight: Identifiable, EmptyInitializable {
|
||||
}
|
||||
|
||||
|
||||
struct SyntaxObjectOutline: Identifiable, EmptyInitializable {
|
||||
struct SyntaxObjectOutline: Identifiable {
|
||||
|
||||
let id = UUID()
|
||||
|
||||
@ -103,7 +103,7 @@ struct SyntaxObjectOutline: Identifiable, EmptyInitializable {
|
||||
}
|
||||
|
||||
|
||||
struct SyntaxObjectKeyString: Identifiable, EmptyInitializable {
|
||||
struct SyntaxObjectKeyString: Identifiable {
|
||||
|
||||
let id = UUID()
|
||||
|
||||
|
@ -64,7 +64,7 @@ struct SyntaxOutlineEditView: View {
|
||||
.tableStyle(.bordered)
|
||||
.border(Color(nsColor: .gridColor))
|
||||
|
||||
AddRemoveButton($items, selection: $selection, focus: $focusedField)
|
||||
AddRemoveButton($items, selection: $selection, focus: $focusedField, newItem: Item.init)
|
||||
.padding(.bottom, 8)
|
||||
|
||||
if self.selection.count > 1 {
|
||||
|
Loading…
Reference in New Issue
Block a user