From 4df66d139f8128486b3e34e1c206413abd1babd5 Mon Sep 17 00:00:00 2001 From: "E.Z. Hart" Date: Fri, 2 Dec 2016 13:48:42 -0700 Subject: [PATCH] Allow CommandBar to expand and show command labels (#594) * Allow CommandBar to expand and show command labels Consolidate command bar placement code * Make title text wrapping consistent between NavigationPage and MasterDetailPage * Align toolbar/navigation bar behavior with other platforms --- .nuspec/Xamarin.Forms.nuspec | 1 + .../NavigationPageWindows.cs | 2 + Xamarin.Forms.Platform.UAP/FormsCommandBar.cs | 79 +- .../FormsCommandBarStyle.xaml | 800 +++++++++++++++++++++ Xamarin.Forms.Platform.UAP/MasterDetailControl.cs | 28 +- .../MasterDetailControlStyle.xaml | 33 +- Xamarin.Forms.Platform.UAP/PageControlStyle.xaml | 25 +- Xamarin.Forms.Platform.UAP/Resources.xaml | 1 + Xamarin.Forms.Platform.UAP/TabbedPageStyle.xaml | 25 +- .../ToolbarPlacementHelper.cs | 56 +- .../Xamarin.Forms.Platform.UAP.csproj | 5 + Xamarin.Forms.Platform.WinRT.Phone/FormsPivot.cs | 22 +- Xamarin.Forms.Platform.WinRT/PageControl.xaml.cs | 25 +- 13 files changed, 984 insertions(+), 118 deletions(-) create mode 100644 Xamarin.Forms.Platform.UAP/FormsCommandBarStyle.xaml diff --git a/.nuspec/Xamarin.Forms.nuspec b/.nuspec/Xamarin.Forms.nuspec index c82a6ee..b87a140 100644 --- a/.nuspec/Xamarin.Forms.nuspec +++ b/.nuspec/Xamarin.Forms.nuspec @@ -213,6 +213,7 @@ + diff --git a/Xamarin.Forms.Controls/GalleryPages/PlatformSpecificsGalleries/NavigationPageWindows.cs b/Xamarin.Forms.Controls/GalleryPages/PlatformSpecificsGalleries/NavigationPageWindows.cs index 1143678..2816075 100644 --- a/Xamarin.Forms.Controls/GalleryPages/PlatformSpecificsGalleries/NavigationPageWindows.cs +++ b/Xamarin.Forms.Controls/GalleryPages/PlatformSpecificsGalleries/NavigationPageWindows.cs @@ -8,6 +8,8 @@ namespace Xamarin.Forms.Controls.GalleryPages.PlatformSpecificsGalleries { PushAsync(CreateRoot(restore)); WindowsPlatformSpecificsGalleryHelpers.AddToolBarItems(this); + + BarBackgroundColor = Color.CornflowerBlue; } ContentPage CreateRoot(ICommand restore) diff --git a/Xamarin.Forms.Platform.UAP/FormsCommandBar.cs b/Xamarin.Forms.Platform.UAP/FormsCommandBar.cs index 21e9ca1..4145186 100644 --- a/Xamarin.Forms.Platform.UAP/FormsCommandBar.cs +++ b/Xamarin.Forms.Platform.UAP/FormsCommandBar.cs @@ -1,18 +1,30 @@ -using Windows.Foundation.Collections; +using System; +using Windows.Foundation.Collections; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; +using Windows.UI.Xaml.Data; namespace Xamarin.Forms.Platform.UWP { public class FormsCommandBar : CommandBar { - // TODO Once 10.0.14393.0 is available, enable dynamic overflow: https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.commandbar.isdynamicoverflowenabled.aspx + // TODO Once 10.0.14393.0 is available (and we don't have to support lower versions), enable dynamic overflow: https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.commandbar.isdynamicoverflowenabled.aspx + Windows.UI.Xaml.Controls.Button _moreButton; + public FormsCommandBar() { PrimaryCommands.VectorChanged += OnCommandsChanged; SecondaryCommands.VectorChanged += OnCommandsChanged; UpdateVisibility(); + WatchForContentChanges(); + } + + protected override void OnApplyTemplate() + { + base.OnApplyTemplate(); + + _moreButton = GetTemplateChild("MoreButton") as Windows.UI.Xaml.Controls.Button; } void OnCommandsChanged(IObservableVector sender, IVectorChangedEventArgs args) @@ -22,7 +34,68 @@ namespace Xamarin.Forms.Platform.UWP void UpdateVisibility() { - Visibility = PrimaryCommands.Count + SecondaryCommands.Count > 0 ? Visibility.Visible : Visibility.Collapsed; + var visibility = PrimaryCommands.Count + SecondaryCommands.Count > 0 ? Visibility.Visible : Visibility.Collapsed; + + if (_moreButton != null) + { + // The "..." button should only be visible if we have commands to display + _moreButton.Visibility = visibility; + + // There *is* an OverflowButtonVisibility property that does more or less the same thing, + // but it became available in 10.0.14393.0 and we have to support 10.0.10240 + } + + // If we have a title (or some other content) inside this command bar + // and that content is not collapsed + var frameworkElement = Content as FrameworkElement; + + // Temporarily tie the visibility of the toolbar to the visibility of the Title + // to be consistent with the old style / other platforms + if (frameworkElement != null && frameworkElement.Visibility == Visibility.Collapsed) + { + Visibility = Visibility.Collapsed; + return; + } + + if (frameworkElement != null && frameworkElement.Visibility != Visibility.Collapsed) + { + Visibility = Visibility.Visible; + } + else + { + // Otherwise, collapse it if there are no commands + Visibility = visibility; + } + } + + void WatchForContentChanges() + { + // If the content of the command bar changes while it's collapsed, we need to + // react and update the visibility (e.g., if the bar is placed at the bottom and + // has no commands, then is moved to the top and now includes the title) + + // There's no event on CommandBar when the content changes, so we'll bind our own + // dependency property to Content and update our visibility when it changes + var binding = new Windows.UI.Xaml.Data.Binding + { + Source = this, + Path = new PropertyPath(nameof(Content)), + Mode = Windows.UI.Xaml.Data.BindingMode.OneWay + }; + + BindingOperations.SetBinding(this, s_contentChangeWatcher, binding); + } + + static readonly DependencyProperty s_contentChangeWatcher = + DependencyProperty.Register( + "ContentChangeWatcher", + typeof(object), + typeof(object), + new PropertyMetadata(null, ContentChanged)); + + static void ContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + (d as FormsCommandBar)?.UpdateVisibility(); } } } \ No newline at end of file diff --git a/Xamarin.Forms.Platform.UAP/FormsCommandBarStyle.xaml b/Xamarin.Forms.Platform.UAP/FormsCommandBarStyle.xaml new file mode 100644 index 0000000..31b800f --- /dev/null +++ b/Xamarin.Forms.Platform.UAP/FormsCommandBarStyle.xaml @@ -0,0 +1,800 @@ + + + + + + + + + + + + + + + + + + diff --git a/Xamarin.Forms.Platform.UAP/MasterDetailControl.cs b/Xamarin.Forms.Platform.UAP/MasterDetailControl.cs index dddd0ba..2357232 100644 --- a/Xamarin.Forms.Platform.UAP/MasterDetailControl.cs +++ b/Xamarin.Forms.Platform.UAP/MasterDetailControl.cs @@ -49,8 +49,7 @@ namespace Xamarin.Forms.Platform.UWP new PropertyMetadata(default(Visibility))); CommandBar _commandBar; - Border _bottomCommandBarArea; - Border _topCommandBarArea; + readonly ToolbarPlacementHelper _toolbarPlacementHelper = new ToolbarPlacementHelper(); TaskCompletionSource _commandBarTcs; FrameworkElement _masterPresenter; @@ -174,7 +173,7 @@ namespace Xamarin.Forms.Platform.UWP set { _toolbarPlacement = value; - UpdateToolbarPlacement(); + _toolbarPlacementHelper.UpdateToolbarPlacement(); } } @@ -236,17 +235,8 @@ namespace Xamarin.Forms.Platform.UWP _detailPresenter = GetTemplateChild("DetailPresenter") as FrameworkElement; _commandBar = GetTemplateChild("CommandBar") as CommandBar; - _bottomCommandBarArea = GetTemplateChild("BottomCommandBarArea") as Border; - _topCommandBarArea = GetTemplateChild("TopCommandBarArea") as Border; - - if (_commandBar != null && _bottomCommandBarArea != null && _topCommandBarArea != null) - { - // We have to wait for the command bar to load so that it'll be in the control hierarchy - // otherwise we can't properly move it to wherever the toolbar is supposed to be - _commandBar.Loaded += (sender, args) => UpdateToolbarPlacement(); - } - - UpdateToolbarPlacement(); + _toolbarPlacementHelper.Initialize(_commandBar, () => ToolbarPlacement, GetTemplateChild); + UpdateMode(); if (_commandBarTcs != null) @@ -263,11 +253,6 @@ namespace Xamarin.Forms.Platform.UWP ((MasterDetailControl)dependencyObject).UpdateMode(); } - static void ToolbarPlacementChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args) - { - ((MasterDetailControl)dependencyObject).UpdateToolbarPlacement(); - } - static void CollapsedPaneWidthChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs) { ((MasterDetailControl)dependencyObject).UpdateMode(); @@ -304,10 +289,5 @@ namespace Xamarin.Forms.Platform.UWP ? Visibility.Visible : Visibility.Collapsed; } - - void UpdateToolbarPlacement() - { - ToolbarPlacementHelper.UpdateToolbarPlacement(_commandBar, ToolbarPlacement, _bottomCommandBarArea, _topCommandBarArea); - } } } \ No newline at end of file diff --git a/Xamarin.Forms.Platform.UAP/MasterDetailControlStyle.xaml b/Xamarin.Forms.Platform.UAP/MasterDetailControlStyle.xaml index d82f78a..3f13f76 100644 --- a/Xamarin.Forms.Platform.UAP/MasterDetailControlStyle.xaml +++ b/Xamarin.Forms.Platform.UAP/MasterDetailControlStyle.xaml @@ -29,29 +29,26 @@ - - - - - + + + + + - - -