From 5e553f6195e66e48688b8ab324f1bab1e9251f0a Mon Sep 17 00:00:00 2001 From: Samantha Houts Date: Tue, 30 Aug 2016 10:46:14 -0700 Subject: Platform Specifics (#301) * Playing around with how the platform specifics interfaces etc. might work * Sample implementation of iOS navigation translucency * Very slightly reduced code * Better vendor stuff * Drop single-implemenation interfaces * Generics on NavigationPage * On-demand vendor stuff * Remove functionally duplicate classes and make ControlGallery work again * Namespace all the things. XAML test. * Can use Effect to attach platform specific * Attach Effect on PropertyChanging for XAML support! * Rename IConfigPlatform interfaces for readability * Some renaming to match the documents * Split class files * Clear out test-only code * Re-namespace * Added On method to rendered Elements * Allow for removal of platform suffix, convenience methods on specific platforms * Creating a gallery page for specifics * Add rudimentary Platform Specifics gallery; make CollapseStyle work on UWP; Add CollapsedPaneWidth specific property * Toolbar now working with both collapse styles * MDP now displaying Content title; toolbar routing around title * Add a gallery for the iOS NavigationPage stuff * Add Navigation Page as detail page to verify it works with new Toolbar options * Make titlebar/toolbar background colors consistent * ToolbarPlacement now working on NavigationPage * Toolbar Placement working for tabbed and nav pages * Fix bug where phone doesn't get default toolbar placement on start * [Core] Add PS WindowSoftInputModeAdjust [Core] Make Application extendable * Toolbar placement now working on Nav, Tabbed, and Master pages on desktop/phone Remove unnecessary style indirection Fix build errors * [A] Add PlatformConfigurationExtensions * SetSoftInputMode test page * [A] SetSoftInputMode Known issue: Status bar color does not work in AdjustResize mode * [Core] Add PS Blur * [iOS] Configure renderer for blur * Add test page * Move to blur VisualElement for broader support * Move test pages to gallery * Update docs * Use lazy initializer for PlatformConfigurationRegistry --- Xamarin.Forms.Platform.UAP/FormsCommandBar.cs | 19 +- Xamarin.Forms.Platform.UAP/MasterDetailControl.cs | 100 +++- .../MasterDetailControlStyle.xaml | 64 +++ .../MasterDetailPageRenderer.cs | 36 +- Xamarin.Forms.Platform.UAP/PageControlStyle.xaml | 42 ++ Xamarin.Forms.Platform.UAP/Resources.xaml | 261 +-------- Xamarin.Forms.Platform.UAP/TabbedPageRenderer.cs | 603 +++++++++++---------- Xamarin.Forms.Platform.UAP/TabbedPageStyle.xaml | 199 +++++++ .../ToolbarPlacementHelper.cs | 46 ++ .../Xamarin.Forms.Platform.UAP.csproj | 16 + 10 files changed, 806 insertions(+), 580 deletions(-) create mode 100644 Xamarin.Forms.Platform.UAP/MasterDetailControlStyle.xaml create mode 100644 Xamarin.Forms.Platform.UAP/PageControlStyle.xaml create mode 100644 Xamarin.Forms.Platform.UAP/TabbedPageStyle.xaml create mode 100644 Xamarin.Forms.Platform.UAP/ToolbarPlacementHelper.cs (limited to 'Xamarin.Forms.Platform.UAP') diff --git a/Xamarin.Forms.Platform.UAP/FormsCommandBar.cs b/Xamarin.Forms.Platform.UAP/FormsCommandBar.cs index 2ca665af..21e9ca16 100644 --- a/Xamarin.Forms.Platform.UAP/FormsCommandBar.cs +++ b/Xamarin.Forms.Platform.UAP/FormsCommandBar.cs @@ -6,32 +6,23 @@ namespace Xamarin.Forms.Platform.UWP { public class FormsCommandBar : CommandBar { - Windows.UI.Xaml.Controls.Button _moreButton; + // 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 public FormsCommandBar() { PrimaryCommands.VectorChanged += OnCommandsChanged; SecondaryCommands.VectorChanged += OnCommandsChanged; - } - - protected override void OnApplyTemplate() - { - base.OnApplyTemplate(); - _moreButton = GetTemplateChild("MoreButton") as Windows.UI.Xaml.Controls.Button; - UpdateMore(); + UpdateVisibility(); } void OnCommandsChanged(IObservableVector sender, IVectorChangedEventArgs args) { - UpdateMore(); + UpdateVisibility(); } - void UpdateMore() + void UpdateVisibility() { - if (_moreButton == null) - return; - - _moreButton.Visibility = PrimaryCommands.Count > 0 || SecondaryCommands.Count > 0 ? Visibility.Visible : Visibility.Collapsed; + Visibility = PrimaryCommands.Count + SecondaryCommands.Count > 0 ? Visibility.Visible : Visibility.Collapsed; } } } \ No newline at end of file diff --git a/Xamarin.Forms.Platform.UAP/MasterDetailControl.cs b/Xamarin.Forms.Platform.UAP/MasterDetailControl.cs index 6787ccec..51f737f8 100644 --- a/Xamarin.Forms.Platform.UAP/MasterDetailControl.cs +++ b/Xamarin.Forms.Platform.UAP/MasterDetailControl.cs @@ -1,7 +1,9 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media; +using Xamarin.Forms.PlatformConfiguration.WindowsSpecific; namespace Xamarin.Forms.Platform.UWP { @@ -17,9 +19,15 @@ namespace Xamarin.Forms.Platform.UWP public static readonly DependencyProperty IsPaneOpenProperty = DependencyProperty.Register("IsPaneOpen", typeof(bool), typeof(MasterDetailControl), new PropertyMetadata(default(bool))); - public static readonly DependencyProperty ShouldShowSplitModeProperty = DependencyProperty.Register("ShouldShowSplitMode", typeof(bool), typeof(MasterDetailControl), + public static readonly DependencyProperty ShouldShowSplitModeProperty = DependencyProperty.Register(nameof(ShouldShowSplitMode), typeof(bool), typeof(MasterDetailControl), new PropertyMetadata(default(bool), OnShouldShowSplitModeChanged)); + public static readonly DependencyProperty CollapseStyleProperty = DependencyProperty.Register(nameof(CollapseStyle), typeof(CollapseStyle), + typeof(MasterDetailControl), new PropertyMetadata(CollapseStyle.Full, CollapseStyleChanged)); + + public static readonly DependencyProperty CollapsedPaneWidthProperty = DependencyProperty.Register(nameof(CollapsedPaneWidth), typeof(double), typeof(MasterDetailControl), + new PropertyMetadata(48d, CollapsedPaneWidthChanged)); + public static readonly DependencyProperty DetailTitleProperty = DependencyProperty.Register("DetailTitle", typeof(string), typeof(MasterDetailControl), new PropertyMetadata(default(string))); public static readonly DependencyProperty ToolbarForegroundProperty = DependencyProperty.Register("ToolbarForeground", typeof(Brush), typeof(MasterDetailControl), @@ -37,20 +45,26 @@ namespace Xamarin.Forms.Platform.UWP public static readonly DependencyProperty MasterToolbarVisibilityProperty = DependencyProperty.Register("MasterToolbarVisibility", typeof(Visibility), typeof(MasterDetailControl), new PropertyMetadata(default(Visibility))); + public static readonly DependencyProperty ContentTogglePaneButtonVisibilityProperty = DependencyProperty.Register(nameof(ContentTogglePaneButtonVisibility), typeof(Visibility), typeof(MasterDetailControl), + new PropertyMetadata(default(Visibility))); + CommandBar _commandBar; + Border _bottomCommandBarArea; + Border _topCommandBarArea; TaskCompletionSource _commandBarTcs; FrameworkElement _masterPresenter; FrameworkElement _detailPresenter; SplitView _split; + ToolbarPlacement _toolbarPlacement; - public MasterDetailControl() + public MasterDetailControl() { DefaultStyleKey = typeof(MasterDetailControl); - MasterTitleVisibility = Visibility.Collapsed; + DetailTitleVisibility = Visibility.Collapsed; - if (Device.Idiom != TargetIdiom.Phone) - MasterToolbarVisibility = Visibility.Collapsed; + + CollapseStyle = CollapseStyle.Full; } public FrameworkElement Detail @@ -148,6 +162,34 @@ namespace Xamarin.Forms.Platform.UWP set { SetValue(ShouldShowSplitModeProperty, value); } } + public CollapseStyle CollapseStyle + { + get { return (CollapseStyle)GetValue(CollapseStyleProperty); } + set { SetValue(CollapseStyleProperty, value); } + } + + public ToolbarPlacement ToolbarPlacement + { + get { return _toolbarPlacement; } + set + { + _toolbarPlacement = value; + UpdateToolbarPlacement(); + } + } + + public Visibility ContentTogglePaneButtonVisibility + { + get { return (Visibility)GetValue(ContentTogglePaneButtonVisibilityProperty); } + set { SetValue(ContentTogglePaneButtonVisibilityProperty, value); } + } + + public double CollapsedPaneWidth + { + get { return (double)GetValue(CollapsedPaneWidthProperty); } + set { SetValue(CollapsedPaneWidthProperty, value); } + } + public Brush ToolbarBackground { get { return (Brush)GetValue(ToolbarBackgroundProperty); } @@ -194,8 +236,11 @@ 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; - UpdateMode(); + UpdateToolbarPlacement(); + UpdateMode(); if (_commandBarTcs != null) _commandBarTcs.SetResult(_commandBar); @@ -206,6 +251,21 @@ namespace Xamarin.Forms.Platform.UWP ((MasterDetailControl)dependencyObject).UpdateMode(); } + static void CollapseStyleChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args) + { + ((MasterDetailControl)dependencyObject).UpdateMode(); + } + + static void ToolbarPlacementChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args) + { + ((MasterDetailControl)dependencyObject).UpdateToolbarPlacement(); + } + + static void CollapsedPaneWidthChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs) + { + ((MasterDetailControl)dependencyObject).UpdateMode(); + } + void OnToggleClicked(object sender, RoutedEventArgs args) { IsPaneOpen = !IsPaneOpen; @@ -214,9 +274,33 @@ namespace Xamarin.Forms.Platform.UWP void UpdateMode() { if (_split == null) + { return; + } - _split.DisplayMode = ShouldShowSplitMode ? SplitViewDisplayMode.Inline : SplitViewDisplayMode.Overlay; + _split.DisplayMode = ShouldShowSplitMode + ? SplitViewDisplayMode.Inline + : CollapseStyle == CollapseStyle.Full ? SplitViewDisplayMode.Overlay : SplitViewDisplayMode.CompactOverlay; + + _split.CompactPaneLength = CollapsedPaneWidth; + + if (_split.DisplayMode == SplitViewDisplayMode.Inline) + { + // If we've determined that the pane will always be open, then there's no + // reason to display the show/hide pane button in the master + MasterToolbarVisibility = Visibility.Collapsed; + } + + // If we're in compact mode or the pane is always open, + // we don't need to display the content pane's toggle button + ContentTogglePaneButtonVisibility = _split.DisplayMode == SplitViewDisplayMode.Overlay + ? 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 new file mode 100644 index 00000000..d82f78af --- /dev/null +++ b/Xamarin.Forms.Platform.UAP/MasterDetailControlStyle.xaml @@ -0,0 +1,64 @@ + + + \ No newline at end of file diff --git a/Xamarin.Forms.Platform.UAP/MasterDetailPageRenderer.cs b/Xamarin.Forms.Platform.UAP/MasterDetailPageRenderer.cs index c424c7fe..f0048154 100644 --- a/Xamarin.Forms.Platform.UAP/MasterDetailPageRenderer.cs +++ b/Xamarin.Forms.Platform.UAP/MasterDetailPageRenderer.cs @@ -6,6 +6,8 @@ using Windows.UI.Xaml.Automation; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Media; +using Xamarin.Forms.PlatformConfiguration.WindowsSpecific; +using Specifics = Xamarin.Forms.PlatformConfiguration.WindowsSpecific.MasterDetailPage; namespace Xamarin.Forms.Platform.UWP { @@ -56,7 +58,7 @@ namespace Xamarin.Forms.Platform.UWP set { Control.ToolbarForeground = value; } } -IPageController PageController => Element as IPageController; + IPageController PageController => Element as IPageController; IMasterDetailPageController MasterDetailPageController => Element as IMasterDetailPageController; @@ -68,11 +70,9 @@ IPageController PageController => Element as IPageController; { if (_showTitle == value) return; + _showTitle = value; - if (_showTitle) - Control.DetailTitleVisibility = Visibility.Visible; - else - Control.DetailTitleVisibility = Visibility.Collapsed; + Control.DetailTitleVisibility = _showTitle ? Visibility.Visible : Visibility.Collapsed; } } @@ -132,7 +132,7 @@ IPageController PageController => Element as IPageController; { Control = new MasterDetailControl(); Control.Loaded += OnControlLoaded; - Control.Unloaded += OnControlUnlaoded; + Control.Unloaded += OnControlUnloaded; Control.SizeChanged += OnNativeSizeChanged; Control.RegisterPropertyChangedCallback(MasterDetailControl.IsPaneOpenProperty, OnIsPaneOpenChanged); @@ -148,7 +148,12 @@ IPageController PageController => Element as IPageController; if (!string.IsNullOrEmpty(e.NewElement.AutomationId)) Control.SetValue(AutomationProperties.AutomationIdProperty, e.NewElement.AutomationId); - } + +#if WINDOWS_UWP + UpdateToolbarPlacement(); +#endif + + } } protected virtual void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) @@ -159,8 +164,12 @@ IPageController PageController => Element as IPageController; UpdateMaster(); else if (e.PropertyName == "Detail") UpdateDetail(); - else if (e.PropertyName == "ShouldShowSplitMode") + else if (e.PropertyName == nameof(MasterDetailControl.ShouldShowSplitMode) + || e.PropertyName == Specifics.CollapseStyleProperty.PropertyName + || e.PropertyName == Specifics.CollapsedPaneWidthProperty.PropertyName) UpdateMode(); + else if(e.PropertyName == PlatformConfiguration.WindowsSpecific.Page.ToolbarPlacementProperty.PropertyName) + UpdateToolbarPlacement(); } void ClearDetail() @@ -202,7 +211,7 @@ IPageController PageController => Element as IPageController; UpdateBounds(); } - void OnControlUnlaoded(object sender, RoutedEventArgs routedEventArgs) + void OnControlUnloaded(object sender, RoutedEventArgs routedEventArgs) { PageController?.SendDisappearing(); } @@ -264,6 +273,7 @@ IPageController PageController => Element as IPageController; return; Control.DetailTitle = (_detail as NavigationPage)?.CurrentPage?.Title ?? _detail.Title ?? Element?.Title; + (this as ITitleProvider).ShowTitle = !string.IsNullOrEmpty(Control.DetailTitle); } void UpdateIsPresented() @@ -291,10 +301,18 @@ IPageController PageController => Element as IPageController; void UpdateMode() { + Control.CollapseStyle = Element.OnThisPlatform().GetCollapseStyle(); + Control.CollapsedPaneWidth = Element.OnThisPlatform().CollapsedPaneWidth(); Control.ShouldShowSplitMode = MasterDetailPageController.ShouldShowSplitMode; } #if WINDOWS_UWP + + void UpdateToolbarPlacement() + { + Control.ToolbarPlacement = Element.OnThisPlatform().GetToolbarPlacement(); + } + public void BindForegroundColor(AppBar appBar) { SetAppBarForegroundBinding(appBar); diff --git a/Xamarin.Forms.Platform.UAP/PageControlStyle.xaml b/Xamarin.Forms.Platform.UAP/PageControlStyle.xaml new file mode 100644 index 00000000..bd199487 --- /dev/null +++ b/Xamarin.Forms.Platform.UAP/PageControlStyle.xaml @@ -0,0 +1,42 @@ + + + \ No newline at end of file diff --git a/Xamarin.Forms.Platform.UAP/Resources.xaml b/Xamarin.Forms.Platform.UAP/Resources.xaml index 5f3a4999..732283e8 100644 --- a/Xamarin.Forms.Platform.UAP/Resources.xaml +++ b/Xamarin.Forms.Platform.UAP/Resources.xaml @@ -5,8 +5,11 @@ x:Class="Xamarin.Forms.Platform.UWP.Resources"> + + + @@ -247,52 +250,7 @@ - + - - - - diff --git a/Xamarin.Forms.Platform.UAP/TabbedPageRenderer.cs b/Xamarin.Forms.Platform.UAP/TabbedPageRenderer.cs index aa988b80..e9bbd4a1 100644 --- a/Xamarin.Forms.Platform.UAP/TabbedPageRenderer.cs +++ b/Xamarin.Forms.Platform.UAP/TabbedPageRenderer.cs @@ -7,6 +7,7 @@ using Windows.UI.Xaml.Automation; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media; using Xamarin.Forms.Internals; +using Xamarin.Forms.PlatformConfiguration.WindowsSpecific; using WGrid = Windows.UI.Xaml.Controls.Grid; namespace Xamarin.Forms.Platform.UWP @@ -40,302 +41,320 @@ namespace Xamarin.Forms.Platform.UWP } } - public class TabbedPageRenderer : IVisualElementRenderer, ITitleProvider, IToolbarProvider - { - const string TabBarHeaderTextBlockName = "TabbedPageHeaderTextBlock"; - const string TabBarHeaderGridName = "TabbedPageHeaderGrid"; - - Color _barBackgroundColor; - Color _barTextColor; - bool _disposed; - bool _showTitle; - VisualElementTracker _tracker; - - ITitleProvider TitleProvider => this; - - public FormsPivot Control { get; private set; } - - public TabbedPage Element { get; private set; } - - protected VisualElementTracker Tracker - { - get { return _tracker; } - set - { - if (_tracker == value) - return; - - if (_tracker != null) - _tracker.Dispose(); - - _tracker = value; - } - } - - public void Dispose() - { - Dispose(true); - } - - Brush ITitleProvider.BarBackgroundBrush - { - set { Control.ToolbarBackground = value; } - } - - Brush ITitleProvider.BarForegroundBrush - { - set { Control.ToolbarForeground = value; } - } - - IPageController PageController => Element as IPageController; - - bool ITitleProvider.ShowTitle - { - get { return _showTitle; } - - set - { - if (_showTitle == value) - return; - _showTitle = value; - - UpdateBarVisibility(); - } - } - - string ITitleProvider.Title - { - get { return (string)Control?.Title; } - - set - { - if (Control != null && _showTitle) - Control.Title = value; - } - } - - public Task GetCommandBarAsync() - { - return (Control as IToolbarProvider)?.GetCommandBarAsync(); - } - - public FrameworkElement ContainerElement - { - get { return Control; } - } - - VisualElement IVisualElementRenderer.Element - { - get { return Element; } - } - - public event EventHandler ElementChanged; - - public SizeRequest GetDesiredSize(double widthConstraint, double heightConstraint) - { - var constraint = new Windows.Foundation.Size(widthConstraint, heightConstraint); - - double oldWidth = Control.Width; - double oldHeight = Control.Height; - - Control.Height = double.NaN; - Control.Width = double.NaN; - - Control.Measure(constraint); - var result = new Size(Math.Ceiling(Control.DesiredSize.Width), Math.Ceiling(Control.DesiredSize.Height)); - - Control.Width = oldWidth; - Control.Height = oldHeight; - - return new SizeRequest(result); - } - - public void SetElement(VisualElement element) - { - if (element != null && !(element is TabbedPage)) - throw new ArgumentException("Element must be a TabbedPage", "element"); - - TabbedPage oldElement = Element; - Element = (TabbedPage)element; - - if (oldElement != null) - { - oldElement.PropertyChanged -= OnElementPropertyChanged; - ((INotifyCollectionChanged)oldElement.Children).CollectionChanged -= OnPagesChanged; - } - - if (element != null) - { - if (Control == null) - { - Control = new FormsPivot { Style = (Windows.UI.Xaml.Style)Windows.UI.Xaml.Application.Current.Resources["TabbedPageStyle"], }; - - Control.SelectionChanged += OnSelectionChanged; - - Tracker = new BackgroundTracker(Windows.UI.Xaml.Controls.Control.BackgroundProperty) { Element = (Page)element, Control = Control, Container = Control }; + public class TabbedPageRenderer : IVisualElementRenderer, ITitleProvider, IToolbarProvider + { + const string TabBarHeaderTextBlockName = "TabbedPageHeaderTextBlock"; + const string TabBarHeaderGridName = "TabbedPageHeaderGrid"; - Control.Loaded += OnLoaded; - Control.Unloaded += OnUnloaded; - } - - Control.DataContext = Element; - OnPagesChanged(Element.Children, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); - - UpdateCurrentPage(); - - ((INotifyCollectionChanged)Element.Children).CollectionChanged += OnPagesChanged; - element.PropertyChanged += OnElementPropertyChanged; - - if (!string.IsNullOrEmpty(element.AutomationId)) - Control.SetValue(AutomationProperties.AutomationIdProperty, element.AutomationId); - } - - OnElementChanged(new VisualElementChangedEventArgs(oldElement, element)); - } - - protected virtual void Dispose(bool disposing) - { - if (!disposing || _disposed) - return; - - _disposed = true; - PageController?.SendDisappearing(); - SetElement(null); - Tracker = null; - } - - protected virtual void OnElementChanged(VisualElementChangedEventArgs e) - { - EventHandler changed = ElementChanged; - if (changed != null) - changed(this, e); - } - - void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) - { - if (e.PropertyName == nameof(TabbedPage.CurrentPage)) - { - UpdateCurrentPage(); - UpdateBarTextColor(); - UpdateBarBackgroundColor(); - } - else if (e.PropertyName == TabbedPage.BarTextColorProperty.PropertyName) - UpdateBarTextColor(); - else if (e.PropertyName == TabbedPage.BarBackgroundColorProperty.PropertyName) - UpdateBarBackgroundColor(); - } - - void OnLoaded(object sender, RoutedEventArgs args) - { - PageController?.SendAppearing(); - UpdateBarTextColor(); - UpdateBarBackgroundColor(); - } - - void OnPagesChanged(object sender, NotifyCollectionChangedEventArgs e) - { - e.Apply(Element.Children, Control.Items); - - // Potential performance issue, UpdateLayout () is called for every page change - Control.UpdateLayout(); - } - - void OnSelectionChanged(object sender, SelectionChangedEventArgs e) - { - if (Element == null) - return; - - Page page = e.AddedItems.Count > 0 ? (Page)e.AddedItems[0] : null; - Page currentPage = Element.CurrentPage; - if (currentPage == page) - return; - ((IPageController)currentPage)?.SendDisappearing(); - Element.CurrentPage = page; - ((IPageController)page)?.SendAppearing(); - } - - void OnUnloaded(object sender, RoutedEventArgs args) - { - PageController?.SendDisappearing(); - } - - Brush GetBarBackgroundBrush() - { - object defaultColor = Windows.UI.Xaml.Application.Current.Resources["SystemControlBackgroundChromeMediumLowBrush"]; - if (Element.BarBackgroundColor.IsDefault && defaultColor != null) - return (Brush)defaultColor; - return Element.BarBackgroundColor.ToBrush(); - } - - Brush GetBarForegroundBrush() - { - object defaultColor = Windows.UI.Xaml.Application.Current.Resources["ApplicationForegroundThemeBrush"]; - if (Element.BarTextColor.IsDefault && defaultColor != null) - return (Brush)defaultColor; - return Element.BarTextColor.ToBrush(); - } - - void UpdateBarBackgroundColor() - { - if (Element == null) return; - var barBackgroundColor = Element.BarBackgroundColor; - - if (barBackgroundColor == _barBackgroundColor) return; - _barBackgroundColor = barBackgroundColor; - - var controlToolbarBackground = Control.ToolbarBackground; - if (controlToolbarBackground == null && barBackgroundColor.IsDefault) return; - - var brush = GetBarBackgroundBrush(); - if (brush == controlToolbarBackground) return; - - TitleProvider.BarBackgroundBrush = brush; - - foreach (WGrid tabBarGrid in Control.GetDescendantsByName(TabBarHeaderGridName)) - { - tabBarGrid.Background = brush; - } - } - - void UpdateBarTextColor() - { - if (Element == null) return; - var barTextColor = Element.BarTextColor; - - if (barTextColor == _barTextColor) return; - _barTextColor = barTextColor; + Color _barBackgroundColor; + Color _barTextColor; + bool _disposed; + bool _showTitle; + VisualElementTracker _tracker; - var controlToolbarForeground = Control.ToolbarForeground; - if (controlToolbarForeground == null && barTextColor.IsDefault) return; + ITitleProvider TitleProvider => this; - var brush = GetBarForegroundBrush(); - if (brush == controlToolbarForeground) - return; + public FormsPivot Control { get; private set; } - TitleProvider.BarForegroundBrush = brush; + public TabbedPage Element { get; private set; } - foreach (TextBlock tabBarTextBlock in Control.GetDescendantsByName(TabBarHeaderTextBlockName)) - { - tabBarTextBlock.Foreground = brush; - } - } + protected VisualElementTracker Tracker + { + get { return _tracker; } + set + { + if (_tracker == value) + return; - void UpdateBarVisibility() - { - Control.ToolbarVisibility = _showTitle ? Visibility.Visible : Visibility.Collapsed; - } - - void UpdateCurrentPage() - { - Page page = Element.CurrentPage; - - var nav = page as NavigationPage; - TitleProvider.ShowTitle = nav != null; - - if (page == null) - return; - - Control.SelectedItem = page; - } - } + if (_tracker != null) + _tracker.Dispose(); + + _tracker = value; + } + } + + public void Dispose() + { + Dispose(true); + } + + Brush ITitleProvider.BarBackgroundBrush + { + set { Control.ToolbarBackground = value; } + } + + Brush ITitleProvider.BarForegroundBrush + { + set { Control.ToolbarForeground = value; } + } + + IPageController PageController => Element as IPageController; + + bool ITitleProvider.ShowTitle + { + get { return _showTitle; } + + set + { + if (_showTitle == value) + return; + _showTitle = value; + + UpdateTitleVisibility(); + } + } + + string ITitleProvider.Title + { + get { return (string)Control?.Title; } + + set + { + if (Control != null && _showTitle) + Control.Title = value; + } + } + + public Task GetCommandBarAsync() + { + return (Control as IToolbarProvider)?.GetCommandBarAsync(); + } + + public FrameworkElement ContainerElement + { + get { return Control; } + } + + VisualElement IVisualElementRenderer.Element + { + get { return Element; } + } + + public event EventHandler ElementChanged; + + public SizeRequest GetDesiredSize(double widthConstraint, double heightConstraint) + { + var constraint = new Windows.Foundation.Size(widthConstraint, heightConstraint); + + double oldWidth = Control.Width; + double oldHeight = Control.Height; + + Control.Height = double.NaN; + Control.Width = double.NaN; + + Control.Measure(constraint); + var result = new Size(Math.Ceiling(Control.DesiredSize.Width), Math.Ceiling(Control.DesiredSize.Height)); + + Control.Width = oldWidth; + Control.Height = oldHeight; + + return new SizeRequest(result); + } + + public void SetElement(VisualElement element) + { + if (element != null && !(element is TabbedPage)) + throw new ArgumentException("Element must be a TabbedPage", "element"); + + TabbedPage oldElement = Element; + Element = (TabbedPage)element; + + if (oldElement != null) + { + oldElement.PropertyChanged -= OnElementPropertyChanged; + ((INotifyCollectionChanged)oldElement.Children).CollectionChanged -= OnPagesChanged; + } + + if (element != null) + { + if (Control == null) + { + Control = new FormsPivot + { + Style = (Windows.UI.Xaml.Style)Windows.UI.Xaml.Application.Current.Resources["TabbedPageStyle"], + }; + + Control.SelectionChanged += OnSelectionChanged; + + Tracker = new BackgroundTracker(Windows.UI.Xaml.Controls.Control.BackgroundProperty) + { + Element = (Page)element, + Control = Control, + Container = Control + }; + + Control.Loaded += OnLoaded; + Control.Unloaded += OnUnloaded; + } + + Control.DataContext = Element; + OnPagesChanged(Element.Children, + new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); + + UpdateCurrentPage(); + UpdateToolbarPlacement(); + + ((INotifyCollectionChanged)Element.Children).CollectionChanged += OnPagesChanged; + element.PropertyChanged += OnElementPropertyChanged; + + if (!string.IsNullOrEmpty(element.AutomationId)) + Control.SetValue(AutomationProperties.AutomationIdProperty, element.AutomationId); + } + + OnElementChanged(new VisualElementChangedEventArgs(oldElement, element)); + } + + protected virtual void Dispose(bool disposing) + { + if (!disposing || _disposed) + return; + + _disposed = true; + PageController?.SendDisappearing(); + SetElement(null); + Tracker = null; + } + + protected virtual void OnElementChanged(VisualElementChangedEventArgs e) + { + EventHandler changed = ElementChanged; + if (changed != null) + changed(this, e); + } + + void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) + { + if (e.PropertyName == nameof(TabbedPage.CurrentPage)) + { + UpdateCurrentPage(); + UpdateBarTextColor(); + UpdateBarBackgroundColor(); + } + else if (e.PropertyName == TabbedPage.BarTextColorProperty.PropertyName) + UpdateBarTextColor(); + else if (e.PropertyName == TabbedPage.BarBackgroundColorProperty.PropertyName) + UpdateBarBackgroundColor(); + else if (e.PropertyName == PlatformConfiguration.WindowsSpecific.Page.ToolbarPlacementProperty.PropertyName) + UpdateToolbarPlacement(); + } + + void OnLoaded(object sender, RoutedEventArgs args) + { + PageController?.SendAppearing(); + UpdateBarTextColor(); + UpdateBarBackgroundColor(); + } + + void OnPagesChanged(object sender, NotifyCollectionChangedEventArgs e) + { + e.Apply(Element.Children, Control.Items); + + // Potential performance issue, UpdateLayout () is called for every page change + Control.UpdateLayout(); + } + + void OnSelectionChanged(object sender, SelectionChangedEventArgs e) + { + if (Element == null) + return; + + Page page = e.AddedItems.Count > 0 ? (Page)e.AddedItems[0] : null; + Page currentPage = Element.CurrentPage; + if (currentPage == page) + return; + ((IPageController)currentPage)?.SendDisappearing(); + Element.CurrentPage = page; + ((IPageController)page)?.SendAppearing(); + } + + void OnUnloaded(object sender, RoutedEventArgs args) + { + PageController?.SendDisappearing(); + } + + Brush GetBarBackgroundBrush() + { + object defaultColor = + Windows.UI.Xaml.Application.Current.Resources["SystemControlBackgroundChromeMediumLowBrush"]; + if (Element.BarBackgroundColor.IsDefault && defaultColor != null) + return (Brush)defaultColor; + return Element.BarBackgroundColor.ToBrush(); + } + + Brush GetBarForegroundBrush() + { + object defaultColor = Windows.UI.Xaml.Application.Current.Resources["ApplicationForegroundThemeBrush"]; + if (Element.BarTextColor.IsDefault && defaultColor != null) + return (Brush)defaultColor; + return Element.BarTextColor.ToBrush(); + } + + void UpdateBarBackgroundColor() + { + if (Element == null) return; + var barBackgroundColor = Element.BarBackgroundColor; + + if (barBackgroundColor == _barBackgroundColor) return; + _barBackgroundColor = barBackgroundColor; + + var controlToolbarBackground = Control.ToolbarBackground; + if (controlToolbarBackground == null && barBackgroundColor.IsDefault) return; + + var brush = GetBarBackgroundBrush(); + if (brush == controlToolbarBackground) return; + + TitleProvider.BarBackgroundBrush = brush; + + foreach (WGrid tabBarGrid in Control.GetDescendantsByName(TabBarHeaderGridName)) + { + tabBarGrid.Background = brush; + } + } + + void UpdateBarTextColor() + { + if (Element == null) return; + var barTextColor = Element.BarTextColor; + + if (barTextColor == _barTextColor) return; + _barTextColor = barTextColor; + + var controlToolbarForeground = Control.ToolbarForeground; + if (controlToolbarForeground == null && barTextColor.IsDefault) return; + + var brush = GetBarForegroundBrush(); + if (brush == controlToolbarForeground) + return; + + TitleProvider.BarForegroundBrush = brush; + + foreach (TextBlock tabBarTextBlock in Control.GetDescendantsByName(TabBarHeaderTextBlockName)) + { + tabBarTextBlock.Foreground = brush; + } + } + + void UpdateTitleVisibility() + { + Control.TitleVisibility = _showTitle ? Visibility.Visible : Visibility.Collapsed; + } + + void UpdateCurrentPage() + { + Page page = Element.CurrentPage; + + var nav = page as NavigationPage; + TitleProvider.ShowTitle = nav != null; + + if (page == null) + return; + + Control.SelectedItem = page; + } + + void UpdateToolbarPlacement() + { + Control.ToolbarPlacement = Element.OnThisPlatform().GetToolbarPlacement(); + } + } } \ No newline at end of file diff --git a/Xamarin.Forms.Platform.UAP/TabbedPageStyle.xaml b/Xamarin.Forms.Platform.UAP/TabbedPageStyle.xaml new file mode 100644 index 00000000..6d54a073 --- /dev/null +++ b/Xamarin.Forms.Platform.UAP/TabbedPageStyle.xaml @@ -0,0 +1,199 @@ + + + \ No newline at end of file diff --git a/Xamarin.Forms.Platform.UAP/ToolbarPlacementHelper.cs b/Xamarin.Forms.Platform.UAP/ToolbarPlacementHelper.cs new file mode 100644 index 00000000..24feecbc --- /dev/null +++ b/Xamarin.Forms.Platform.UAP/ToolbarPlacementHelper.cs @@ -0,0 +1,46 @@ +using Windows.UI.Xaml.Controls; +using Xamarin.Forms.PlatformConfiguration.WindowsSpecific; + +namespace Xamarin.Forms.Platform.UWP +{ + internal class ToolbarPlacementHelper + { + public static void UpdateToolbarPlacement(CommandBar toolbar, ToolbarPlacement toolbarPlacement, Border bottomCommandBarArea, Border topCommandBarArea) + { + if (toolbar == null || bottomCommandBarArea == null || topCommandBarArea == null) + { + // Haven't applied the template yet, so we're not ready to do this + return; + } + + // Figure out what's hosting the command bar right now + var current = toolbar.Parent as Border; + + // And figure out where it should be + Border target; + + switch (toolbarPlacement) + { + case ToolbarPlacement.Top: + target = topCommandBarArea; + break; + case ToolbarPlacement.Bottom: + target = bottomCommandBarArea; + break; + case ToolbarPlacement.Default: + default: + target = Device.Idiom == TargetIdiom.Phone ? bottomCommandBarArea : topCommandBarArea; + break; + } + + if (current == null || target == null || current == target) + { + return; + } + + // Remove the command bar from its current host and add it to the new one + current.Child = null; + target.Child = toolbar; + } + } +} \ No newline at end of file diff --git a/Xamarin.Forms.Platform.UAP/Xamarin.Forms.Platform.UAP.csproj b/Xamarin.Forms.Platform.UAP/Xamarin.Forms.Platform.UAP.csproj index 4e804819..ce1b1066 100644 --- a/Xamarin.Forms.Platform.UAP/Xamarin.Forms.Platform.UAP.csproj +++ b/Xamarin.Forms.Platform.UAP/Xamarin.Forms.Platform.UAP.csproj @@ -164,6 +164,9 @@ NativeViewWrapperRenderer.cs + + PlatformConfigurationExtensions.cs + PlatformEffect.cs @@ -183,6 +186,7 @@ + Properties\GlobalAssemblyInfo.cs @@ -432,14 +436,26 @@ Designer MSBuild:Compile + + Designer + MSBuild:Compile + Designer MSBuild:Compile + + Designer + MSBuild:Compile + MSBuild:Compile Designer + + Designer + MSBuild:Compile + -- cgit v1.2.3