2017-11-27 18:14:01 +03:00
|
|
|
|
using System.Threading.Tasks;
|
2016-03-22 23:02:25 +03:00
|
|
|
|
using Windows.UI.Xaml;
|
|
|
|
|
using Windows.UI.Xaml.Controls;
|
|
|
|
|
using Windows.UI.Xaml.Media;
|
2016-08-30 20:46:14 +03:00
|
|
|
|
using Xamarin.Forms.PlatformConfiguration.WindowsSpecific;
|
2016-03-22 23:02:25 +03:00
|
|
|
|
|
|
|
|
|
namespace Xamarin.Forms.Platform.UWP
|
|
|
|
|
{
|
|
|
|
|
public class FormsPivot : Pivot, IToolbarProvider
|
|
|
|
|
{
|
2017-11-27 18:14:01 +03:00
|
|
|
|
public static readonly DependencyProperty TitleVisibilityProperty = DependencyProperty.Register(nameof(TitleVisibility), typeof(Visibility), typeof(FormsPivot), new PropertyMetadata(Visibility.Collapsed));
|
2016-03-22 23:02:25 +03:00
|
|
|
|
|
2016-07-19 02:33:41 +03:00
|
|
|
|
public static readonly DependencyProperty ToolbarForegroundProperty = DependencyProperty.Register(nameof(ToolbarForeground), typeof(Brush), typeof(FormsPivot), new PropertyMetadata(default(Brush)));
|
2016-03-22 23:02:25 +03:00
|
|
|
|
|
2016-07-19 02:33:41 +03:00
|
|
|
|
public static readonly DependencyProperty ToolbarBackgroundProperty = DependencyProperty.Register(nameof(ToolbarBackground), typeof(Brush), typeof(FormsPivot), new PropertyMetadata(default(Brush)));
|
2016-03-22 23:02:25 +03:00
|
|
|
|
|
|
|
|
|
CommandBar _commandBar;
|
2017-11-27 18:14:01 +03:00
|
|
|
|
|
2016-12-02 23:48:42 +03:00
|
|
|
|
readonly ToolbarPlacementHelper _toolbarPlacementHelper = new ToolbarPlacementHelper();
|
2017-01-03 14:25:24 +03:00
|
|
|
|
|
|
|
|
|
public bool ShouldShowToolbar
|
|
|
|
|
{
|
|
|
|
|
get { return _toolbarPlacementHelper.ShouldShowToolBar; }
|
|
|
|
|
set { _toolbarPlacementHelper.ShouldShowToolBar = value; }
|
|
|
|
|
}
|
2017-11-27 18:14:01 +03:00
|
|
|
|
|
2016-03-22 23:02:25 +03:00
|
|
|
|
TaskCompletionSource<CommandBar> _commandBarTcs;
|
2016-08-30 20:46:14 +03:00
|
|
|
|
ToolbarPlacement _toolbarPlacement;
|
2018-10-23 19:27:28 +03:00
|
|
|
|
bool _toolbarDynamicOverflowEnabled = true;
|
2016-03-22 23:02:25 +03:00
|
|
|
|
|
2017-01-03 14:25:24 +03:00
|
|
|
|
public Brush ToolbarBackground
|
2016-03-22 23:02:25 +03:00
|
|
|
|
{
|
|
|
|
|
get { return (Brush)GetValue(ToolbarBackgroundProperty); }
|
|
|
|
|
set { SetValue(ToolbarBackgroundProperty, value); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Brush ToolbarForeground
|
|
|
|
|
{
|
|
|
|
|
get { return (Brush)GetValue(ToolbarForegroundProperty); }
|
|
|
|
|
set { SetValue(ToolbarForegroundProperty, value); }
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-30 20:46:14 +03:00
|
|
|
|
public Visibility TitleVisibility
|
2016-03-22 23:02:25 +03:00
|
|
|
|
{
|
2016-08-30 20:46:14 +03:00
|
|
|
|
get { return (Visibility)GetValue(TitleVisibilityProperty); }
|
|
|
|
|
set { SetValue(TitleVisibilityProperty, value); }
|
2016-03-22 23:02:25 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-30 20:46:14 +03:00
|
|
|
|
public ToolbarPlacement ToolbarPlacement
|
|
|
|
|
{
|
|
|
|
|
get { return _toolbarPlacement; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_toolbarPlacement = value;
|
2017-11-27 18:14:01 +03:00
|
|
|
|
|
2016-12-02 23:48:42 +03:00
|
|
|
|
_toolbarPlacementHelper. UpdateToolbarPlacement();
|
2016-08-30 20:46:14 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-23 19:27:28 +03:00
|
|
|
|
public bool ToolbarDynamicOverflowEnabled
|
|
|
|
|
{
|
|
|
|
|
get { return _toolbarDynamicOverflowEnabled; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_toolbarDynamicOverflowEnabled = value;
|
|
|
|
|
UpdateToolbarDynamicOverflowEnabled();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-22 23:02:25 +03:00
|
|
|
|
Task<CommandBar> IToolbarProvider.GetCommandBarAsync()
|
|
|
|
|
{
|
|
|
|
|
if (_commandBar != null)
|
|
|
|
|
return Task.FromResult(_commandBar);
|
|
|
|
|
|
|
|
|
|
_commandBarTcs = new TaskCompletionSource<CommandBar>();
|
|
|
|
|
ApplyTemplate();
|
|
|
|
|
return _commandBarTcs.Task;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnApplyTemplate()
|
|
|
|
|
{
|
|
|
|
|
base.OnApplyTemplate();
|
2018-10-23 19:27:28 +03:00
|
|
|
|
|
2016-03-22 23:02:25 +03:00
|
|
|
|
_commandBar = GetTemplateChild("CommandBar") as CommandBar;
|
2016-08-30 20:46:14 +03:00
|
|
|
|
|
2017-11-27 18:14:01 +03:00
|
|
|
|
|
2016-12-02 23:48:42 +03:00
|
|
|
|
_toolbarPlacementHelper.Initialize(_commandBar, () => ToolbarPlacement, GetTemplateChild);
|
2018-10-23 19:27:28 +03:00
|
|
|
|
UpdateToolbarDynamicOverflowEnabled();
|
2016-08-30 20:46:14 +03:00
|
|
|
|
|
2016-03-22 23:02:25 +03:00
|
|
|
|
TaskCompletionSource<CommandBar> tcs = _commandBarTcs;
|
2018-10-23 19:27:28 +03:00
|
|
|
|
tcs?.SetResult(_commandBar);
|
2017-11-10 13:52:25 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override DependencyObject GetContainerForItemOverride()
|
|
|
|
|
{
|
|
|
|
|
var containerItem = base.GetContainerForItemOverride();
|
|
|
|
|
|
|
|
|
|
var pivotItem = containerItem as PivotItem;
|
|
|
|
|
|
|
|
|
|
if (pivotItem != null)
|
|
|
|
|
{
|
|
|
|
|
// We need to know when the data context changes so we can set the automation name to the page title
|
|
|
|
|
pivotItem.DataContextChanged += SetPivotItemAutomationName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return containerItem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void SetPivotItemAutomationName(FrameworkElement frameworkElement,
|
|
|
|
|
DataContextChangedEventArgs dataContextChangedEventArgs)
|
|
|
|
|
{
|
|
|
|
|
var pivotItem = frameworkElement as PivotItem;
|
|
|
|
|
var page = dataContextChangedEventArgs.NewValue as Page;
|
|
|
|
|
|
|
|
|
|
if (pivotItem != null && page?.Title != null)
|
|
|
|
|
{
|
|
|
|
|
// This way we can find tabs with automation (for testing, etc.)
|
|
|
|
|
Windows.UI.Xaml.Automation.AutomationProperties.SetName(pivotItem, page.Title);
|
|
|
|
|
}
|
2016-03-22 23:02:25 +03:00
|
|
|
|
}
|
2018-10-23 19:27:28 +03:00
|
|
|
|
|
|
|
|
|
void UpdateToolbarDynamicOverflowEnabled()
|
|
|
|
|
{
|
|
|
|
|
if (_commandBar != null)
|
|
|
|
|
{
|
|
|
|
|
_commandBar.IsDynamicOverflowEnabled = ToolbarDynamicOverflowEnabled;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-22 23:02:25 +03:00
|
|
|
|
}
|
|
|
|
|
}
|