Skip to content

Commit c9d0bdc

Browse files
committed
Preventing tab drag gesture when close button is clicked.
1 parent 2cd89f0 commit c9d0bdc

3 files changed

Lines changed: 27 additions & 7 deletions

File tree

CodeEdit/Features/TabBar/Views/TabBarItemCloseButton.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ struct TabBarItemCloseButton: View {
1313
var isDragging: Bool
1414
var closeAction: () -> Void
1515

16+
@Binding
17+
var closeButtonGestureActive: Bool
18+
1619
@Environment(\.colorScheme)
1720
var colorScheme
1821

@@ -63,11 +66,11 @@ struct TabBarItemCloseButton: View {
6366
.foregroundColor(isPressingClose ? .primary : .secondary)
6467
.cornerRadius(2)
6568
.contentShape(Rectangle())
66-
.highPriorityGesture(
69+
.gesture(
6770
DragGesture(minimumDistance: 0)
6871
.onChanged({ _ in
69-
print("pressed")
7072
isPressingClose = true
73+
closeButtonGestureActive = true
7174
})
7275
.onEnded({ value in
7376
if value.location.x > 0
@@ -77,8 +80,8 @@ struct TabBarItemCloseButton: View {
7780
closeAction()
7881
}
7982
isPressingClose = false
83+
closeButtonGestureActive = false
8084
})
81-
, including: .gesture
8285
)
8386
.onHover { hover in
8487
isHoveringClose = hover
@@ -92,12 +95,15 @@ struct TabBarItemCloseButton: View {
9295
}
9396

9497
struct TabBarItemCloseButton_Previews: PreviewProvider {
98+
@State static var closeButtonGestureActive = true
99+
95100
static var previews: some View {
96101
TabBarItemCloseButton(
97102
isActive: false,
98103
isHoveringTab: false,
99104
isDragging: false,
100-
closeAction: { print("Close tab") }
105+
closeAction: { print("Close tab") },
106+
closeButtonGestureActive: $closeButtonGestureActive
101107
)
102108
}
103109
}

CodeEdit/Features/TabBar/Views/TabBarItemView.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ struct TabBarItemView: View {
5656
@Binding
5757
private var onDragTabId: TabBarItemID?
5858

59+
@Binding
60+
private var closeButtonGestureActive: Bool
61+
5962
/// The current WorkspaceDocument object.
6063
///
6164
/// It contains the workspace-related information like selection states.
@@ -112,12 +115,14 @@ struct TabBarItemView: View {
112115
expectedWidth: Binding<CGFloat>,
113116
item: TabBarItemRepresentable,
114117
draggingTabId: Binding<TabBarItemID?>,
115-
onDragTabId: Binding<TabBarItemID?>
118+
onDragTabId: Binding<TabBarItemID?>,
119+
closeButtonGestureActive: Binding<Bool>
116120
) {
117121
self._expectedWidth = expectedWidth
118122
self.item = item
119123
self._draggingTabId = draggingTabId
120124
self._onDragTabId = onDragTabId
125+
self._closeButtonGestureActive = closeButtonGestureActive
121126
}
122127

123128
@ViewBuilder
@@ -189,7 +194,8 @@ struct TabBarItemView: View {
189194
isActive: isActive,
190195
isHoveringTab: isHovering,
191196
isDragging: draggingTabId != nil || onDragTabId != nil,
192-
closeAction: closeAction
197+
closeAction: closeAction,
198+
closeButtonGestureActive: $closeButtonGestureActive
193199
)
194200
}
195201
.frame(maxWidth: .infinity, alignment: .leading)

CodeEdit/Features/TabBar/Views/TabBarView.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ struct TabBarView: View {
114114
@State
115115
private var onDragLastLocation: CGPoint?
116116

117+
@State
118+
private var closeButtonGestureActive: Bool = false
119+
117120
/// Update the expected tab width when corresponding UI state is updated.
118121
///
119122
/// This function will be called when the number of tabs or the parent size is changed.
@@ -132,6 +135,10 @@ struct TabBarView: View {
132135
private func makeTabDragGesture(id: TabBarItemID) -> some Gesture {
133136
return DragGesture(minimumDistance: 2, coordinateSpace: .global)
134137
.onChanged({ value in
138+
if closeButtonGestureActive {
139+
return
140+
}
141+
135142
if draggingTabId != id {
136143
shouldOnDrag = false
137144
draggingTabId = id
@@ -294,7 +301,8 @@ struct TabBarView: View {
294301
expectedWidth: $expectedTabWidth,
295302
item: item,
296303
draggingTabId: $draggingTabId,
297-
onDragTabId: $onDragTabId
304+
onDragTabId: $onDragTabId,
305+
closeButtonGestureActive: $closeButtonGestureActive
298306
)
299307
.frame(height: TabBarView.height)
300308
.background(makeTabItemGeometryReader(id: id))

0 commit comments

Comments
 (0)