Skip to content

Commit a0854c3

Browse files
committed
Added comments
1 parent bd99b46 commit a0854c3

File tree

1 file changed

+22
-25
lines changed

1 file changed

+22
-25
lines changed

src/Files.App/UserControls/TabBar/TabBar.xaml.cs

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,11 @@ public Rectangle DragArea
6363
/// <summary> Starting time when dragging a tab. </summary>
6464
private DateTimeOffset dragStartTime;
6565

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;
6871

6972
public TabBar()
7073
{
@@ -86,9 +89,7 @@ public TabBar()
8689
private void TabView_TabItemsChanged(TabView sender, Windows.Foundation.Collections.IVectorChangedEventArgs args)
8790
{
8891
if (args.CollectionChange == Windows.Foundation.Collections.CollectionChange.ItemRemoved)
89-
{
9092
App.AppModel.TabStripSelectedIndex = Items.IndexOf(HorizontalTabView.SelectedItem as TabBarItem);
91-
}
9293

9394
if (App.AppModel.TabStripSelectedIndex >= 0 && App.AppModel.TabStripSelectedIndex < Items.Count)
9495
{
@@ -136,30 +137,34 @@ private void TabHoverSelected(object sender, object e)
136137
{
137138
tabHoverTimer.Stop();
138139
if (hoveredTabViewItem is not null)
139-
{
140140
App.AppModel.TabStripSelectedIndex = Items.IndexOf(hoveredTabViewItem.DataContext as TabBarItem);
141-
}
142141
}
143142

144143
private void TabView_TabDragStarting(TabView sender, TabViewTabDragStartingEventArgs args)
145144
{
145+
// Reset value
146+
isCancelingDragOperation = false;
147+
146148
var tabViewItemArgs = (args.Item as TabBarItem).NavigationParameter;
147149
args.Data.Properties.Add(TabPathIdentifier, tabViewItemArgs.Serialize());
148150
args.Data.RequestedOperation = DataPackageOperation.Move;
149151

152+
// Get cursor position & time to track how far the tab was dragged.
150153
InteropHelpers.GetCursorPos(out dragStartPoint);
151154
dragStartTime = DateTimeOffset.UtcNow;
152155

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.
154158
Focus(FocusState.Programmatic);
155-
cancelTagDrag = false;
156-
PreviewKeyDown += DraggingKeyDown;
159+
PreviewKeyDown += TabDragging_PreviewKeyDown;
157160
}
158161

159-
private void DraggingKeyDown(object sender, KeyRoutedEventArgs e)
162+
private void TabDragging_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
160163
{
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.
161166
if (e.Key is Windows.System.VirtualKey.Escape)
162-
cancelTagDrag = true;
167+
isCancelingDragOperation = true;
163168
}
164169

165170
private void TabView_TabStripDragOver(object sender, DragEventArgs e)
@@ -193,9 +198,7 @@ private async void TabView_TabStripDrop(object sender, DragEventArgs e)
193198

194199
if (!e.DataView.Properties.TryGetValue(TabPathIdentifier, out object tabViewItemPathObj) ||
195200
!(tabViewItemPathObj is string tabViewItemString))
196-
{
197201
return;
198-
}
199202

200203
var index = -1;
201204

@@ -217,29 +220,25 @@ private async void TabView_TabStripDrop(object sender, DragEventArgs e)
217220

218221
private void TabView_TabDragCompleted(TabView sender, TabViewTabDragCompletedEventArgs args)
219222
{
223+
// Unsubscribe from the key down event, it's only needed when a tag is actively being dragged
224+
PreviewKeyDown -= TabDragging_PreviewKeyDown;
225+
220226
if (ApplicationData.Current.LocalSettings.Values.ContainsKey(TabDropHandledIdentifier) &&
221227
(bool)ApplicationData.Current.LocalSettings.Values[TabDropHandledIdentifier])
222-
{
223228
CloseTab(args.Item as TabBarItem);
224-
}
225229
else
226-
{
227230
HorizontalTabView.SelectedItem = args.Tab;
228-
}
229231

230232
if (ApplicationData.Current.LocalSettings.Values.ContainsKey(TabDropHandledIdentifier))
231-
{
232233
ApplicationData.Current.LocalSettings.Values.Remove(TabDropHandledIdentifier);
233-
}
234-
235-
PreviewKeyDown -= DraggingKeyDown;
236234
}
237235

238236
private async void TabView_TabDroppedOutside(TabView sender, TabViewTabDroppedOutsideEventArgs args)
239237
{
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;
241240

242-
if (cancelTagDrag)
241+
if (isCancelingDragOperation)
243242
return;
244243

245244
InteropHelpers.GetCursorPos(out var droppedPoint);
@@ -263,10 +262,8 @@ private async void TabView_TabDroppedOutside(TabView sender, TabViewTabDroppedOu
263262
sender.SelectedIndex = selectedTabViewItemIndex;
264263
}
265264
else
266-
{
267265
// Dispose tab arguments
268266
(args.Item as TabBarItem)?.Unload();
269-
}
270267
}
271268

272269
private void TabItemContextMenu_Opening(object sender, object e)

0 commit comments

Comments
 (0)