@@ -63,8 +63,11 @@ public Rectangle DragArea
63
63
/// <summary> Starting time when dragging a tab. </summary>
64
64
private DateTimeOffset dragStartTime ;
65
65
66
- /// <summary> Cancel the tag drag event. </summary>
67
- private bool cancelTagDrag ;
66
+ /// <summary>
67
+ /// Indicates if drag operation should be canceled.
68
+ /// This value gets reset at the start of the drag operation
69
+ /// </summary>
70
+ private bool isCancelingDragOperation ;
68
71
69
72
public TabBar ( )
70
73
{
@@ -86,9 +89,7 @@ public TabBar()
86
89
private void TabView_TabItemsChanged ( TabView sender , Windows . Foundation . Collections . IVectorChangedEventArgs args )
87
90
{
88
91
if ( args . CollectionChange == Windows . Foundation . Collections . CollectionChange . ItemRemoved )
89
- {
90
92
App . AppModel . TabStripSelectedIndex = Items . IndexOf ( HorizontalTabView . SelectedItem as TabBarItem ) ;
91
- }
92
93
93
94
if ( App . AppModel . TabStripSelectedIndex >= 0 && App . AppModel . TabStripSelectedIndex < Items . Count )
94
95
{
@@ -136,30 +137,34 @@ private void TabHoverSelected(object sender, object e)
136
137
{
137
138
tabHoverTimer . Stop ( ) ;
138
139
if ( hoveredTabViewItem is not null )
139
- {
140
140
App . AppModel . TabStripSelectedIndex = Items . IndexOf ( hoveredTabViewItem . DataContext as TabBarItem ) ;
141
- }
142
141
}
143
142
144
143
private void TabView_TabDragStarting ( TabView sender , TabViewTabDragStartingEventArgs args )
145
144
{
145
+ // Reset value
146
+ isCancelingDragOperation = false ;
147
+
146
148
var tabViewItemArgs = ( args . Item as TabBarItem ) . NavigationParameter ;
147
149
args . Data . Properties . Add ( TabPathIdentifier , tabViewItemArgs . Serialize ( ) ) ;
148
150
args . Data . RequestedOperation = DataPackageOperation . Move ;
149
151
152
+ // Get cursor position & time to track how far the tab was dragged.
150
153
InteropHelpers . GetCursorPos ( out dragStartPoint ) ;
151
154
dragStartTime = DateTimeOffset . UtcNow ;
152
155
153
- // Force focus in order that the keydown event works
156
+ // Focus the UI Element, without this the focus sometimes changes
157
+ // and the PreviewKeyDown event won't trigger.
154
158
Focus ( FocusState . Programmatic ) ;
155
- cancelTagDrag = false ;
156
- PreviewKeyDown += DraggingKeyDown ;
159
+ PreviewKeyDown += TabDragging_PreviewKeyDown ;
157
160
}
158
161
159
- private void DraggingKeyDown ( object sender , KeyRoutedEventArgs e )
162
+ private void TabDragging_PreviewKeyDown ( object sender , KeyRoutedEventArgs e )
160
163
{
164
+ // Pressing escape will automatically complete the drag event but we need to set the
165
+ // isCancelingDragOperation field in order to detect if escape was pressed.
161
166
if ( e . Key is Windows . System . VirtualKey . Escape )
162
- cancelTagDrag = true ;
167
+ isCancelingDragOperation = true ;
163
168
}
164
169
165
170
private void TabView_TabStripDragOver ( object sender , DragEventArgs e )
@@ -193,9 +198,7 @@ private async void TabView_TabStripDrop(object sender, DragEventArgs e)
193
198
194
199
if ( ! e . DataView . Properties . TryGetValue ( TabPathIdentifier , out object tabViewItemPathObj ) ||
195
200
! ( tabViewItemPathObj is string tabViewItemString ) )
196
- {
197
201
return ;
198
- }
199
202
200
203
var index = - 1 ;
201
204
@@ -217,29 +220,25 @@ private async void TabView_TabStripDrop(object sender, DragEventArgs e)
217
220
218
221
private void TabView_TabDragCompleted ( TabView sender , TabViewTabDragCompletedEventArgs args )
219
222
{
223
+ // Unsubscribe from the key down event, it's only needed when a tag is actively being dragged
224
+ PreviewKeyDown -= TabDragging_PreviewKeyDown ;
225
+
220
226
if ( ApplicationData . Current . LocalSettings . Values . ContainsKey ( TabDropHandledIdentifier ) &&
221
227
( bool ) ApplicationData . Current . LocalSettings . Values [ TabDropHandledIdentifier ] )
222
- {
223
228
CloseTab ( args . Item as TabBarItem ) ;
224
- }
225
229
else
226
- {
227
230
HorizontalTabView . SelectedItem = args . Tab ;
228
- }
229
231
230
232
if ( ApplicationData . Current . LocalSettings . Values . ContainsKey ( TabDropHandledIdentifier ) )
231
- {
232
233
ApplicationData . Current . LocalSettings . Values . Remove ( TabDropHandledIdentifier ) ;
233
- }
234
-
235
- PreviewKeyDown -= DraggingKeyDown ;
236
234
}
237
235
238
236
private async void TabView_TabDroppedOutside ( TabView sender , TabViewTabDroppedOutsideEventArgs args )
239
237
{
240
- PreviewKeyDown -= DraggingKeyDown ;
238
+ // Unsubscribe from the key down event, it's only needed when a tag is actively being dragged
239
+ PreviewKeyDown -= TabDragging_PreviewKeyDown ;
241
240
242
- if ( cancelTagDrag )
241
+ if ( isCancelingDragOperation )
243
242
return ;
244
243
245
244
InteropHelpers . GetCursorPos ( out var droppedPoint ) ;
@@ -263,10 +262,8 @@ private async void TabView_TabDroppedOutside(TabView sender, TabViewTabDroppedOu
263
262
sender . SelectedIndex = selectedTabViewItemIndex ;
264
263
}
265
264
else
266
- {
267
265
// Dispose tab arguments
268
266
( args . Item as TabBarItem ) ? . Unload ( ) ;
269
- }
270
267
}
271
268
272
269
private void TabItemContextMenu_Opening ( object sender , object e )
0 commit comments