diff --git a/CotEditor/Sources/AddRemoveButton.swift b/CotEditor/Sources/AddRemoveButton.swift index 79b577eb2..c155c0f76 100644 --- a/CotEditor/Sources/AddRemoveButton.swift +++ b/CotEditor/Sources/AddRemoveButton.swift @@ -25,13 +25,7 @@ import SwiftUI -protocol EmptyInitializable { - - init() -} - - -struct AddRemoveButton: View { +struct AddRemoveButton: View { @Binding private var items: [Item] @Binding private var selection: Set @@ -39,6 +33,8 @@ struct AddRemoveButton: View { private var focus: FocusState.Binding? @State private var added: Item.ID? + private var newItem: () -> Item + /// Creates a segmented add/remove control. /// @@ -46,11 +42,13 @@ struct AddRemoveButton: 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>, focus: FocusState.Binding? = nil) { + /// - newItem: A closure to return an item for when adding a new item from the button. + init(_ items: Binding<[Item]>, selection: Binding>, focus: FocusState.Binding? = nil, newItem: @escaping () -> Item) { self._items = items self._selection = selection self.focus = focus + self.newItem = newItem } @@ -58,7 +56,7 @@ struct AddRemoveButton: 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() diff --git a/CotEditor/Sources/SyntaxCompletionEditView.swift b/CotEditor/Sources/SyntaxCompletionEditView.swift index 66cf96011..8c4d36a4e 100644 --- a/CotEditor/Sources/SyntaxCompletionEditView.swift +++ b/CotEditor/Sources/SyntaxCompletionEditView.swift @@ -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") } diff --git a/CotEditor/Sources/SyntaxFileMappingEditView.swift b/CotEditor/Sources/SyntaxFileMappingEditView.swift index 863273f41..f7408fe74 100644 --- a/CotEditor/Sources/SyntaxFileMappingEditView.swift +++ b/CotEditor/Sources/SyntaxFileMappingEditView.swift @@ -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) } } diff --git a/CotEditor/Sources/SyntaxHighlightEditView.swift b/CotEditor/Sources/SyntaxHighlightEditView.swift index c0fa8fd5b..b25ff4e5e 100644 --- a/CotEditor/Sources/SyntaxHighlightEditView.swift +++ b/CotEditor/Sources/SyntaxHighlightEditView.swift @@ -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) } diff --git a/CotEditor/Sources/SyntaxObject.swift b/CotEditor/Sources/SyntaxObject.swift index e91723035..f2ae8c49b 100644 --- a/CotEditor/Sources/SyntaxObject.swift +++ b/CotEditor/Sources/SyntaxObject.swift @@ -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() diff --git a/CotEditor/Sources/SyntaxOutlineEditView.swift b/CotEditor/Sources/SyntaxOutlineEditView.swift index fedf288ad..c5602e576 100644 --- a/CotEditor/Sources/SyntaxOutlineEditView.swift +++ b/CotEditor/Sources/SyntaxOutlineEditView.swift @@ -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 {