11// Copyright (c) Files Community
22// Licensed under the MIT License.
33
4+ using CommunityToolkit . WinUI ;
45using Microsoft . UI . Xaml ;
56using Microsoft . UI . Xaml . Controls ;
67using Microsoft . UI . Xaml . Media ;
@@ -17,12 +18,16 @@ namespace Files.App.Controls
1718 [ TemplatePart ( Name = "PART_ModesHostGrid" , Type = typeof ( Grid ) ) ]
1819 // Visual states
1920 [ TemplateVisualState ( Name = "Focused" , GroupName = "FocusStates" ) ]
20- [ TemplateVisualState ( Name = "Unfocused " , GroupName = "FocusStates" ) ]
21+ [ TemplateVisualState ( Name = "Normal " , GroupName = "FocusStates" ) ]
2122 public partial class Omnibar : Control
2223 {
2324 private const string ModesHostGrid = "PART_ModesHostGrid" ;
25+ private const string AutoSuggestPopup = "PART_AutoSuggestPopup" ;
26+ private const string AutoSuggestBoxBorder = "PART_AutoSuggestBoxBorder" ;
2427
2528 private Grid ? _modesHostGrid ;
29+ private Popup ? _autoSuggestPopup ;
30+ private Border ? _autoSuggestBoxBorder ;
2631 private bool _isFocused ;
2732
2833 public Omnibar ( )
@@ -36,11 +41,19 @@ protected override void OnApplyTemplate()
3641 {
3742 _modesHostGrid = GetTemplateChild ( ModesHostGrid ) as Grid
3843 ?? throw new MissingFieldException ( $ "Could not find { ModesHostGrid } in the given { nameof ( Omnibar ) } 's style.") ;
44+ _autoSuggestPopup = GetTemplateChild ( AutoSuggestPopup ) as Popup
45+ ?? throw new MissingFieldException ( $ "Could not find { AutoSuggestPopup } in the given { nameof ( Omnibar ) } 's style.") ;
46+ _autoSuggestBoxBorder = GetTemplateChild ( AutoSuggestBoxBorder ) as Border
47+ ?? throw new MissingFieldException ( $ "Could not find { AutoSuggestBoxBorder } in the given { nameof ( Omnibar ) } 's style.") ;
3948
4049 if ( Modes is null )
4150 return ;
4251
43- // Populate the modes1
52+ // Add shadow to the popup and set the proper width
53+ _autoSuggestBoxBorder ! . Translation = new ( 0 , 0 , 32 ) ;
54+ _autoSuggestBoxBorder ! . Width = _modesHostGrid ! . ActualWidth ;
55+
56+ // Populate the modes
4457 foreach ( var mode in Modes )
4558 {
4659 // Insert a divider
@@ -63,9 +76,14 @@ protected override void OnApplyTemplate()
6376 _modesHostGrid . ColumnDefinitions . Add ( new ( ) { Width = GridLength . Auto } ) ;
6477 Grid . SetColumn ( mode , _modesHostGrid . Children . Count ) ;
6578 _modesHostGrid . Children . Add ( mode ) ;
66- mode . Host = _modesHostGrid ;
79+ mode . Host = this ;
80+
81+ //if (mode.UseDefaultInactiveMode)
82+ // mode.ContentOnInactive = DefaultInactiveMode;
6783 }
6884
85+ _modesHostGrid . SizeChanged += _modesHostGrid_SizeChanged ;
86+
6987 GotFocus += Omnibar_GotFocus ;
7088 LostFocus += Omnibar_LostFocus ;
7189
@@ -74,28 +92,60 @@ protected override void OnApplyTemplate()
7492 base . OnApplyTemplate ( ) ;
7593 }
7694
77- // Private methods
95+ // Methods
96+
97+ internal void ChangeExpandedMode ( OmnibarMode modeToExpand )
98+ {
99+ if ( _modesHostGrid is null || Modes is null )
100+ throw new NullReferenceException ( ) ;
101+
102+ // Reset
103+ foreach ( var column in _modesHostGrid . ColumnDefinitions )
104+ column . Width = GridLength . Auto ;
105+ foreach ( var mode in Modes )
106+ {
107+ VisualStateManager . GoToState ( mode , "Unfocused" , true ) ;
108+ }
109+
110+ // Expand the given mode
111+ VisualStateManager . GoToState ( modeToExpand , "Focused" , true ) ;
112+ _modesHostGrid . ColumnDefinitions [ _modesHostGrid . Children . IndexOf ( modeToExpand ) ] . Width = new ( 1 , GridUnitType . Star ) ;
113+
114+ CurrentActiveMode = modeToExpand ;
115+ }
78116
79117 private void UpdateVisualStates ( )
80118 {
81119 VisualStateManager . GoToState (
82120 this ,
83121 _isFocused ? "Focused" : "Normal" ,
84122 true ) ;
123+
124+ if ( CurrentActiveMode is not null )
125+ VisualStateManager . GoToState ( CurrentActiveMode , _isFocused ? "Focused" : "CurrentUnfocused" , true ) ;
85126 }
86127
87128 // Events
88129
130+ private void _modesHostGrid_SizeChanged ( object sender , SizeChangedEventArgs e )
131+ {
132+ _autoSuggestBoxBorder ! . Width = _modesHostGrid ! . ActualWidth ;
133+ }
134+
89135 private void Omnibar_GotFocus ( object sender , RoutedEventArgs e )
90136 {
91137 _isFocused = true ;
92138 UpdateVisualStates ( ) ;
139+
140+ _autoSuggestPopup ! . IsOpen = true ;
93141 }
94142
95143 private void Omnibar_LostFocus ( object sender , RoutedEventArgs e )
96144 {
97145 _isFocused = false ;
98146 UpdateVisualStates ( ) ;
147+
148+ _autoSuggestPopup ! . IsOpen = false ;
99149 }
100150 }
101151}
0 commit comments