-
Notifications
You must be signed in to change notification settings - Fork 615
/
TabViewWindowingSamplePage.xaml.cs
250 lines (210 loc) · 9.45 KB
/
TabViewWindowingSamplePage.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
using System;
using Windows.ApplicationModel.Core;
using Windows.ApplicationModel.DataTransfer;
using Windows.Foundation.Metadata;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Hosting;
using Microsoft.UI.Xaml.Navigation;
using Microsoft.UI.Windowing;
using AppUIBasics.Helper;
using System.Threading;
using Microsoft.UI.Dispatching;
using System.Threading.Tasks;
using Windows.System;
using DispatcherQueueHandler = Microsoft.UI.Dispatching.DispatcherQueueHandler;
namespace AppUIBasics.TabViewPages
{
public sealed partial class TabViewWindowingSamplePage : Page
{
private const string DataIdentifier = "MyTabItem";
public TabViewWindowingSamplePage()
{
this.InitializeComponent();
Tabs.TabItemsChanged += Tabs_TabItemsChanged;
Loaded += TabViewWindowingSamplePage_Loaded;
}
private void TabViewWindowingSamplePage_Loaded(object sender, RoutedEventArgs e)
{
var currentWindow = WindowHelper.GetWindowForElement(this);
currentWindow.ExtendsContentIntoTitleBar = true;
currentWindow.SetTitleBar(CustomDragRegion);
CustomDragRegion.MinWidth = 188;
}
private void Tabs_TabItemsChanged(TabView sender, Windows.Foundation.Collections.IVectorChangedEventArgs args)
{
// If there are no more tabs, close the window.
if (sender.TabItems.Count == 0)
{
WindowHelper.GetWindowForElement(this).Close();
}
// If there is only one tab left, disable dragging and reordering of Tabs.
else if (sender.TabItems.Count == 1)
{
sender.CanReorderTabs = false;
sender.CanDragTabs = false;
}
else
{
sender.CanReorderTabs = true;
sender.CanDragTabs = true;
}
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
SetupWindow();
}
void SetupWindow()
{
// Main Window -- add some default items
for (int i = 0; i < 3; i++)
{
Tabs.TabItems.Add(new TabViewItem() { IconSource = new Microsoft.UI.Xaml.Controls.SymbolIconSource() { Symbol = Symbol.Placeholder }, Header = $"Item {i}", Content = new MyTabContentControl() { DataContext = $"Page {i}" } });
}
Tabs.SelectedIndex = 0;
}
private void CoreTitleBar_LayoutMetricsChanged(CoreApplicationViewTitleBar sender, object args)
{
// To ensure that the tabs in the titlebar are not occluded by shell
// content, we must ensure that we account for left and right overlays.
// In LTR layouts, the right inset includes the caption buttons and the
// drag region, which is flipped in RTL.
// The SystemOverlayLeftInset and SystemOverlayRightInset values are
// in terms of physical left and right. Therefore, we need to flip
// then when our flow direction is RTL.
if (FlowDirection == FlowDirection.LeftToRight)
{
CustomDragRegion.MinWidth = sender.SystemOverlayRightInset;
ShellTitleBarInset.MinWidth = sender.SystemOverlayLeftInset;
}
else
{
CustomDragRegion.MinWidth = sender.SystemOverlayLeftInset;
ShellTitleBarInset.MinWidth = sender.SystemOverlayRightInset;
}
// Ensure that the height of the custom regions are the same as the titlebar.
CustomDragRegion.Height = ShellTitleBarInset.Height = sender.Height;
}
public void AddTabToTabs(TabViewItem tab)
{
Tabs.TabItems.Add(tab);
}
// Create a new Window once the Tab is dragged outside.
private void Tabs_TabDroppedOutside(TabView sender, TabViewTabDroppedOutsideEventArgs args)
{
var newPage = new TabViewWindowingSamplePage();
Tabs.TabItems.Remove(args.Tab);
newPage.AddTabToTabs(args.Tab);
var newWindow = WindowHelper.CreateWindow();
newWindow.ExtendsContentIntoTitleBar = true;
newWindow.Content = newPage;
newWindow.Activate();
}
private void Tabs_TabDragStarting(TabView sender, TabViewTabDragStartingEventArgs args)
{
// We can only drag one tab at a time, so grab the first one...
var firstItem = args.Tab;
// ... set the drag data to the tab...
args.Data.Properties.Add(DataIdentifier, firstItem);
// ... and indicate that we can move it
args.Data.RequestedOperation = DataPackageOperation.Move;
}
private async void Tabs_TabStripDrop(object sender, DragEventArgs e)
{
// This event is called when we're dragging between different TabViews
// It is responsible for handling the drop of the item into the second TabView
if (e.DataView.Properties.TryGetValue(DataIdentifier, out object obj))
{
// Ensure that the obj property is set before continuing.
if (obj == null)
{
return;
}
var destinationTabView = sender as TabView;
var destinationItems = destinationTabView.TabItems;
if (destinationItems != null)
{
// First we need to get the position in the List to drop to
var index = -1;
// Determine which items in the list our pointer is between.
for (int i = 0; i < destinationTabView.TabItems.Count; i++)
{
var item = destinationTabView.ContainerFromIndex(i) as TabViewItem;
if (e.GetPosition(item).X - item.ActualWidth < 0)
{
index = i;
break;
}
}
// The TabViewItem can only be in one tree at a time. Before moving it to the new TabView, remove it from the old.
// Note that this call can happen on a different thread if moving across windows. So make sure you call methods on
// the same thread as where the UI Elements were created.
object header = null;
object dataContext = null;
var element = (obj as UIElement);
var taskCompletionSource = new TaskCompletionSource();
element.DispatcherQueue.TryEnqueue(
Microsoft.UI.Dispatching.DispatcherQueuePriority.Normal,
new DispatcherQueueHandler(() =>
{
var tabItem = obj as TabViewItem;
var destinationTabViewListView = (tabItem.Parent as TabViewListView);
destinationTabViewListView.Items.Remove(obj);
header = tabItem.Header;
dataContext = (tabItem.Content as MyTabContentControl).DataContext;
taskCompletionSource.SetResult();
}));
await taskCompletionSource.Task;
var insertedItem = CreateNewTVI(header.ToString(), dataContext.ToString());
if (index < 0)
{
// We didn't find a transition point, so we're at the end of the list
destinationItems.Add(insertedItem);
}
else if (index < destinationTabView.TabItems.Count)
{
// Otherwise, insert at the provided index.
destinationItems.Insert(index, insertedItem);
}
// Select the newly dragged tab
destinationTabView.SelectedItem = insertedItem;
}
}
}
private TabViewItem CreateNewTVI(string header, string dataContext)
{
var newTab = new TabViewItem()
{
IconSource = new Microsoft.UI.Xaml.Controls.SymbolIconSource()
{
Symbol = Symbol.Placeholder
},
Header = header,
Content = new MyTabContentControl()
{
DataContext = dataContext
}
};
return newTab;
}
// This method prevents the TabView from handling things that aren't text (ie. files, images, etc.)
private void Tabs_TabStripDragOver(object sender, DragEventArgs e)
{
if (e.DataView.Properties.ContainsKey(DataIdentifier))
{
e.AcceptedOperation = DataPackageOperation.Move;
}
}
private void Tabs_AddTabButtonClick(TabView sender, object args)
{
var tab = CreateNewTVI("New Item", "New Item");
sender.TabItems.Add(tab);
}
private void Tabs_TabCloseRequested(TabView sender, TabViewTabCloseRequestedEventArgs args)
{
sender.TabItems.Remove(args.Tab);
}
}
}