-
Notifications
You must be signed in to change notification settings - Fork 319
/
NavigatorWindow.cs
491 lines (414 loc) · 15.6 KB
/
NavigatorWindow.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*************************************************************************************
Extended WPF Toolkit
Copyright (C) 2007-2013 Xceed Software Inc.
This program is provided to you under the terms of the Microsoft Public
License (Ms-PL) as published at http://wpftoolkit.codeplex.com/license
For more features, controls, and fast professional support,
pick up the Plus Edition at http://xceed.com/wpf_toolkit
Stay informed: follow @datagrid on Twitter or Like http://facebook.com/datagrids
***********************************************************************************/
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using Xceed.Wpf.AvalonDock.Layout;
using Xceed.Wpf.AvalonDock.Themes;
using System.Windows.Controls;
namespace Xceed.Wpf.AvalonDock.Controls
{
[TemplatePart( Name = PART_AnchorableListBox, Type = typeof( ListBox ) )]
[TemplatePart( Name = PART_DocumentListBox, Type = typeof( ListBox ) )]
public class NavigatorWindow : Window
{
#region Members
private const string PART_AnchorableListBox = "PART_AnchorableListBox";
private const string PART_DocumentListBox = "PART_DocumentListBox";
private ResourceDictionary currentThemeResourceDictionary; // = null
private DockingManager _manager;
private bool _isSelectingDocument;
private ListBox _anchorableListBox;
private ListBox _documentListBox;
private bool _internalSetSelectedDocument = false;
private bool _internalSetSelectedAnchorable = false;
#endregion
#region Constructors
static NavigatorWindow()
{
DefaultStyleKeyProperty.OverrideMetadata( typeof( NavigatorWindow ), new FrameworkPropertyMetadata( typeof( NavigatorWindow ) ) );
ShowActivatedProperty.OverrideMetadata( typeof( NavigatorWindow ), new FrameworkPropertyMetadata( false ) );
ShowInTaskbarProperty.OverrideMetadata( typeof( NavigatorWindow ), new FrameworkPropertyMetadata( false ) );
}
internal NavigatorWindow( DockingManager manager )
{
_manager = manager;
_internalSetSelectedDocument = true;
this.SetAnchorables( _manager.Layout.Descendents().OfType<LayoutAnchorable>().Where( a => a.IsVisible ).Select( d => ( LayoutAnchorableItem )_manager.GetLayoutItemFromModel( d ) ).ToArray() );
this.SetDocuments( _manager.Layout.Descendents().OfType<LayoutDocument>().OrderByDescending( d => d.LastActivationTimeStamp.GetValueOrDefault() ).Select( d => ( LayoutDocumentItem )_manager.GetLayoutItemFromModel( d ) ).ToArray() );
_internalSetSelectedDocument = false;
if( this.Documents.Length > 1 )
{
this.InternalSetSelectedDocument( this.Documents[ 1 ] );
_isSelectingDocument = true;
}
else if( this.Anchorables.Count() > 1 )
{
this.InternalSetSelectedAnchorable( this.Anchorables.ToArray()[ 1 ] );
_isSelectingDocument = false;
}
this.DataContext = this;
this.Loaded += new RoutedEventHandler( OnLoaded );
this.Unloaded += new RoutedEventHandler( OnUnloaded );
this.UpdateThemeResources();
}
#endregion
#region Properties
#region Documents
/// <summary>
/// Documents Read-Only Dependency Property
/// </summary>
private static readonly DependencyPropertyKey DocumentsPropertyKey = DependencyProperty.RegisterReadOnly( "Documents", typeof( IEnumerable<LayoutDocumentItem> ), typeof( NavigatorWindow ),
new FrameworkPropertyMetadata( null ) );
public static readonly DependencyProperty DocumentsProperty = DocumentsPropertyKey.DependencyProperty;
/// <summary>
/// Gets the Documents property. This dependency property
/// indicates the list of documents.
/// </summary>
public LayoutDocumentItem[] Documents
{
get
{
return ( LayoutDocumentItem[] )GetValue( DocumentsProperty );
}
}
#endregion
#region Anchorables
/// <summary>
/// Anchorables Read-Only Dependency Property
/// </summary>
private static readonly DependencyPropertyKey AnchorablesPropertyKey = DependencyProperty.RegisterReadOnly( "Anchorables", typeof( IEnumerable<LayoutAnchorableItem> ), typeof( NavigatorWindow ),
new FrameworkPropertyMetadata( ( IEnumerable<LayoutAnchorableItem> )null ) );
public static readonly DependencyProperty AnchorablesProperty = AnchorablesPropertyKey.DependencyProperty;
/// <summary>
/// Gets the Anchorables property. This dependency property
/// indicates the list of anchorables.
/// </summary>
public IEnumerable<LayoutAnchorableItem> Anchorables
{
get
{
return ( IEnumerable<LayoutAnchorableItem> )GetValue( AnchorablesProperty );
}
}
#endregion
#region SelectedDocument
/// <summary>
/// SelectedDocument Dependency Property
/// </summary>
public static readonly DependencyProperty SelectedDocumentProperty = DependencyProperty.Register( "SelectedDocument", typeof( LayoutDocumentItem ), typeof( NavigatorWindow ),
new FrameworkPropertyMetadata( ( LayoutDocumentItem )null, new PropertyChangedCallback( OnSelectedDocumentChanged ) ) );
/// <summary>
/// Gets or sets the SelectedDocument property. This dependency property
/// indicates the selected document.
/// </summary>
public LayoutDocumentItem SelectedDocument
{
get
{
return ( LayoutDocumentItem )GetValue( SelectedDocumentProperty );
}
set
{
SetValue( SelectedDocumentProperty, value );
}
}
/// <summary>
/// Handles changes to the SelectedDocument property.
/// </summary>
private static void OnSelectedDocumentChanged( DependencyObject d, DependencyPropertyChangedEventArgs e )
{
( ( NavigatorWindow )d ).OnSelectedDocumentChanged( e );
}
/// <summary>
/// Provides derived classes an opportunity to handle changes to the SelectedDocument property.
/// </summary>
protected virtual void OnSelectedDocumentChanged( DependencyPropertyChangedEventArgs e )
{
if( _internalSetSelectedDocument )
return;
if( this.SelectedDocument != null &&
this.SelectedDocument.ActivateCommand.CanExecute( null ) )
{
this.Hide();
this.SelectedDocument.ActivateCommand.Execute( null );
}
}
#endregion
#region SelectedAnchorable
/// <summary>
/// SelectedAnchorable Dependency Property
/// </summary>
public static readonly DependencyProperty SelectedAnchorableProperty = DependencyProperty.Register( "SelectedAnchorable", typeof( LayoutAnchorableItem ), typeof( NavigatorWindow ),
new FrameworkPropertyMetadata( ( LayoutAnchorableItem )null, new PropertyChangedCallback( OnSelectedAnchorableChanged ) ) );
/// <summary>
/// Gets or sets the SelectedAnchorable property. This dependency property
/// indicates the selected anchorable.
/// </summary>
public LayoutAnchorableItem SelectedAnchorable
{
get
{
return ( LayoutAnchorableItem )GetValue( SelectedAnchorableProperty );
}
set
{
SetValue( SelectedAnchorableProperty, value );
}
}
/// <summary>
/// Handles changes to the SelectedAnchorable property.
/// </summary>
private static void OnSelectedAnchorableChanged( DependencyObject d, DependencyPropertyChangedEventArgs e )
{
( ( NavigatorWindow )d ).OnSelectedAnchorableChanged( e );
}
/// <summary>
/// Provides derived classes an opportunity to handle changes to the SelectedAnchorable property.
/// </summary>
protected virtual void OnSelectedAnchorableChanged( DependencyPropertyChangedEventArgs e )
{
if( _internalSetSelectedAnchorable )
return;
var selectedAnchorable = e.NewValue as LayoutAnchorableItem;
if( this.SelectedAnchorable != null &&
this.SelectedAnchorable.ActivateCommand.CanExecute( null ) )
{
this.Close();
this.SelectedAnchorable.ActivateCommand.Execute( null );
}
}
#endregion
#endregion
#region Overrides
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
_anchorableListBox = this.GetTemplateChild( PART_AnchorableListBox ) as ListBox;
_documentListBox = this.GetTemplateChild( PART_DocumentListBox ) as ListBox;
}
protected override void OnPreviewKeyDown( System.Windows.Input.KeyEventArgs e )
{
bool shouldHandle = false;
// Press Tab to switch Selected LayoutContent.
if( e.Key == System.Windows.Input.Key.Tab)
{
// Selecting LayoutDocuments
if( _isSelectingDocument )
{
if( this.SelectedDocument != null )
{
// Jump to next LayoutDocument
var docIndex = this.Documents.IndexOf<LayoutDocumentItem>( this.SelectedDocument );
if( docIndex < (this.Documents.Length - 1) )
{
this.SelectNextDocument();
shouldHandle = true;
}
// Jump to first LayoutAnchorable
else if( this.Anchorables.Count() > 0 )
{
_isSelectingDocument = false;
this.InternalSetSelectedDocument( null );
this.InternalSetSelectedAnchorable( this.Anchorables.First() );
shouldHandle = true;
}
}
// There is no SelectedDocument, select the first one.
else
{
if( this.Documents.Length > 0 )
{
this.InternalSetSelectedDocument( this.Documents[ 0 ] );
shouldHandle = true;
}
}
}
// Selecting LayoutAnchorables
else
{
if( this.SelectedAnchorable != null )
{
// Jump to next LayoutAnchorable
var anchorableIndex = this.Anchorables.ToArray().IndexOf<LayoutAnchorableItem>( this.SelectedAnchorable );
if( anchorableIndex < (this.Anchorables.Count() - 1) )
{
this.SelectNextAnchorable();
shouldHandle = true;
}
// Jump to first LayoutDocument
else if( this.Documents.Length > 0 )
{
_isSelectingDocument = true;
this.InternalSetSelectedAnchorable( null );
this.InternalSetSelectedDocument( this.Documents[ 0 ] );
shouldHandle = true;
}
}
// There is no SelectedAnchorable, select the first one.
else
{
if( this.Anchorables.Count() > 0 )
{
this.InternalSetSelectedAnchorable( this.Anchorables.ToArray()[ 0 ] );
shouldHandle = true;
}
}
}
}
if( shouldHandle )
{
e.Handled = true;
}
base.OnPreviewKeyDown( e );
}
protected override void OnPreviewKeyUp( System.Windows.Input.KeyEventArgs e )
{
if( e.Key != System.Windows.Input.Key.Tab )
{
this.Close();
if( this.SelectedDocument != null &&
this.SelectedDocument.ActivateCommand.CanExecute( null ) )
{
this.SelectedDocument.ActivateCommand.Execute( null );
}
if( this.SelectedDocument == null &&
this.SelectedAnchorable != null &&
this.SelectedAnchorable.ActivateCommand.CanExecute( null ) )
{
this.SelectedAnchorable.ActivateCommand.Execute( null );
}
e.Handled = true;
}
base.OnPreviewKeyUp( e );
}
#endregion
#region Internal Methods
/// <summary>
/// Provides a secure method for setting the Anchorables property.
/// This dependency property indicates the list of anchorables.
/// </summary>
/// <param name="value">The new value for the property.</param>
protected void SetAnchorables( IEnumerable<LayoutAnchorableItem> value )
{
this.SetValue( AnchorablesPropertyKey, value );
}
/// <summary>
/// Provides a secure method for setting the Documents property.
/// This dependency property indicates the list of documents.
/// </summary>
/// <param name="value">The new value for the property.</param>
protected void SetDocuments( LayoutDocumentItem[] value )
{
this.SetValue( DocumentsPropertyKey, value );
}
internal void UpdateThemeResources( Theme oldTheme = null )
{
if( oldTheme != null )
{
if( oldTheme is DictionaryTheme )
{
if( currentThemeResourceDictionary != null )
{
this.Resources.MergedDictionaries.Remove( currentThemeResourceDictionary );
currentThemeResourceDictionary = null;
}
}
else
{
var resourceDictionaryToRemove = this.Resources.MergedDictionaries.FirstOrDefault( r => r.Source == oldTheme.GetResourceUri() );
if( resourceDictionaryToRemove != null )
{
this.Resources.MergedDictionaries.Remove( resourceDictionaryToRemove );
}
}
}
if( _manager.Theme != null )
{
if( _manager.Theme is DictionaryTheme )
{
currentThemeResourceDictionary = ( ( DictionaryTheme )_manager.Theme ).ThemeResourceDictionary;
this.Resources.MergedDictionaries.Add( currentThemeResourceDictionary );
}
else
{
this.Resources.MergedDictionaries.Add( new ResourceDictionary() { Source = _manager.Theme.GetResourceUri() } );
}
}
}
internal void SelectNextDocument()
{
if( this.SelectedDocument != null )
{
int docIndex = this.Documents.IndexOf<LayoutDocumentItem>( this.SelectedDocument );
docIndex++;
if( docIndex == this.Documents.Length )
{
docIndex = 0;
}
this.InternalSetSelectedDocument( this.Documents[ docIndex ] );
}
}
internal void SelectNextAnchorable()
{
if( this.SelectedAnchorable != null )
{
var anchorablesArray = this.Anchorables.ToArray();
int anchorableIndex = anchorablesArray.IndexOf<LayoutAnchorableItem>( this.SelectedAnchorable );
anchorableIndex++;
if( anchorableIndex == this.Anchorables.Count() )
{
anchorableIndex = 0;
}
this.InternalSetSelectedAnchorable( anchorablesArray[ anchorableIndex ] );
}
}
#endregion
#region Private Methods
private void InternalSetSelectedAnchorable( LayoutAnchorableItem anchorableToSelect )
{
_internalSetSelectedAnchorable = true;
this.SelectedAnchorable = anchorableToSelect;
_internalSetSelectedAnchorable = false;
if( _anchorableListBox != null )
{
_anchorableListBox.Focus();
}
}
private void InternalSetSelectedDocument( LayoutDocumentItem documentToSelect )
{
_internalSetSelectedDocument = true;
this.SelectedDocument = documentToSelect;
_internalSetSelectedDocument = false;
if( ( _documentListBox != null ) && ( documentToSelect != null ) )
{
_documentListBox.Focus();
}
}
private void OnLoaded( object sender, RoutedEventArgs e )
{
this.Loaded -= new RoutedEventHandler( OnLoaded );
if( ( _documentListBox != null ) && (this.SelectedDocument != null) )
{
_documentListBox.Focus();
}
else if( ( _anchorableListBox != null ) && (this.SelectedAnchorable != null) )
{
_anchorableListBox.Focus();
}
WindowStartupLocation = WindowStartupLocation.CenterOwner;
}
private void OnUnloaded( object sender, RoutedEventArgs e )
{
this.Unloaded -= new RoutedEventHandler( OnUnloaded );
}
#endregion
}
}