@@ -17,36 +17,13 @@ import SwiftUI
1717
1818/// The `GeometryEditorToolbar` component allows users to perform common actions on a
1919/// `GeometryEditor`.
20- ///
21- /// **Features**
22- ///
23- /// - Displays controls for performing common geometry editor actions:
24- /// - Changing the tool.
25- /// - Deleting the selected element.
26- /// - Undoing the last action on the geometry.
27- /// - Redoing the last undone action.
28- /// - Configuring snap settings.
29- /// - Supports styled vertical and horizontal layouts, or no built-in layout or styling.
30- ///
31- /// **Behavior**
32- ///
33- /// The toolbar is shown only while the geometry editor is started.
34- ///
35- /// By default, the toolbar is displayed in a vertical layout. Pass `nil` for `style` to display
36- /// the toolbar without built-in layout or styling, so you can show it in a system toolbar or apply
37- /// your own layout and styling to the controls.
38- ///
39- /// The settings button is only shown when the geometry editor has snap settings with non-empty
40- /// source settings.
41- ///
42- /// **Associated Types**
43- ///
44- /// - ``Style``
4520struct GeometryEditorToolbar : View {
4621 /// The geometry editor that this toolbar controls.
4722 private let geometryEditor : GeometryEditor
4823 /// The style to apply to the toolbar's controls.
4924 private let style : Style ?
25+ /// The allowed snap source types for snap settings UI.
26+ private var snapSources : SnapSources
5027
5128 /// The spacing to apply between the controls in the stacks.
5229 /// This is hardcoded to match the system styling for toolbar groups on iOS.
@@ -68,6 +45,7 @@ struct GeometryEditorToolbar: View {
6845 geometryEditor. snapSettings. isEnabled = true
6946 self . geometryEditor = geometryEditor
7047 self . style = style
48+ self . snapSources = . all
7149
7250 let model = GeometryEditorToolbarModel ( geometryEditor: geometryEditor)
7351 self . _model = . init( wrappedValue: model)
@@ -109,7 +87,19 @@ struct GeometryEditorToolbar: View {
10987 DeleteButton ( )
11088 UndoButton ( )
11189 RedoButton ( )
112- SnapSettingsButton ( )
90+ SnapSettingsButton ( snapSources: snapSources)
91+ }
92+ }
93+
94+ extension GeometryEditorToolbar {
95+ /// Limits the snap source types available in the snap settings UI.
96+ /// Disallowed snap sources are hidden in the UI and disabled in the underlying `SnapSettings`.
97+ /// - Parameter snapSources: The allowed snap source types.
98+ /// - Returns: A toolbar with snap settings restricted to `snapSources`.
99+ func snapSources( _ snapSources: SnapSources ) -> Self {
100+ var copy = self
101+ copy. snapSources = snapSources
102+ return copy
113103 }
114104}
115105
@@ -154,6 +144,53 @@ extension GeometryEditorToolbar {
154144 /// Displays the toolbar in a styled vertical layout.
155145 case vertical
156146 }
147+
148+ /// A set of `SnapSource` types allowed in the snap settings UI.
149+ struct SnapSources : OptionSet {
150+ let rawValue : Int
151+
152+ /// Allows `FeatureLayer` snap sources.
153+ static let featureLayer = SnapSources ( rawValue: 1 << 0 )
154+ /// Allows `GraphicsOverlay` snap sources.
155+ static let graphicsOverlay = SnapSources ( rawValue: 1 << 1 )
156+ /// Allows `SubtypeFeatureLayer` snap sources.
157+ static let subtypeFeatureLayer = SnapSources ( rawValue: 1 << 2 )
158+ /// Allows `SubtypeSublayer` snap sources.
159+ static let subtypeSublayer = SnapSources ( rawValue: 1 << 3 )
160+
161+ /// Allows all supported `SnapSource` types.
162+ static let all : SnapSources = [
163+ . featureLayer,
164+ . graphicsOverlay,
165+ . subtypeFeatureLayer,
166+ . subtypeSublayer
167+ ]
168+ /// Allows `SnapSource` types that are layers.
169+ static let layers : SnapSources = [
170+ . featureLayer,
171+ . subtypeFeatureLayer,
172+ . subtypeSublayer
173+ ]
174+
175+ /// Returns a Boolean value indicating whether a snap source type is
176+ /// allowed by the set.
177+ /// - Parameter source: A snap source to check for allowance by the set.
178+ /// - Returns: `true` if the snap source type is in the set, otherwise `false`.
179+ func contains( source: some SnapSource ) -> Bool {
180+ switch source {
181+ case is FeatureLayer :
182+ contains ( . featureLayer)
183+ case is GraphicsOverlay :
184+ contains ( . graphicsOverlay)
185+ case is SubtypeFeatureLayer :
186+ contains ( . subtypeFeatureLayer)
187+ case is SubtypeSublayer :
188+ contains ( . subtypeSublayer)
189+ default :
190+ false
191+ }
192+ }
193+ }
157194}
158195
159196// MARK: - Controls
@@ -249,6 +286,8 @@ private struct UndoButton: View {
249286private struct SnapSettingsButton : View {
250287 /// The model for the parent geometry editor toolbar.
251288 @Environment ( GeometryEditorToolbarModel . self) private var model
289+ /// The allowed snap source types shown in settings.
290+ let snapSources : GeometryEditorToolbar . SnapSources
252291
253292 /// A Boolean value indicating whether the settings view is presented.
254293 @State private var isShowingSettings = false
@@ -269,9 +308,12 @@ private struct SnapSettingsButton: View {
269308 }
270309 }
271310 . sheet ( isPresented: $isShowingSettings) {
272- SnapSettingsView ( settings: model. geometryEditor. snapSettings)
311+ SnapSettingsView (
312+ settings: model. geometryEditor. snapSettings,
313+ snapSources: snapSources
314+ )
273315 // Needed to override the font set in toolbarStackStyle.
274- . font ( nil )
316+ . font ( nil )
275317 }
276318 }
277319 }
0 commit comments