<UserControl x:Class="CalculatorApp.Calculator"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:automation="using:CalculatorApp.Common.Automation"
             xmlns:common="using:CalculatorApp.Common"
             xmlns:controls="using:CalculatorApp.Controls"
             xmlns:converters="using:CalculatorApp.Converters"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="using:CalculatorApp"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             Loaded="OnLoaded"
             mc:Ignorable="d">

    <UserControl.Resources>
        <!-- DataTemplates -->

        <DataTemplate x:Key="Operand" x:DataType="common:DisplayExpressionToken">
            <TextBlock Margin="2,0,0,0"
                       Foreground="{ThemeResource SystemControlPageTextBaseMediumBrush}"
                       IsTextScaleFactorEnabled="False"
                       Text="{x:Bind Token, Mode=OneWay}"/>
        </DataTemplate>

        <DataTemplate x:Key="Operator" x:DataType="common:DisplayExpressionToken">
            <TextBlock Margin="2,0,0,0"
                       Foreground="{ThemeResource SystemControlPageTextBaseMediumBrush}"
                       IsTextScaleFactorEnabled="False"
                       Text="{x:Bind Token, Mode=OneWay}"/>
        </DataTemplate>

        <DataTemplate x:Key="Separator" x:DataType="common:DisplayExpressionToken">
            <TextBlock x:Name="MainText"
                       IsTextScaleFactorEnabled="False"
                       Text="{x:Bind Token, Mode=OneWay}"/>
        </DataTemplate>

        <!-- TextBox Styles -->

        <Style x:Key="NormalStyle" TargetType="controls:OverflowTextBlock">
            <Setter Property="HorizontalAlignment" Value="Stretch"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="controls:OverflowTextBlock">
                        <Grid x:Name="TokenContainer" Background="{TemplateBinding Background}">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="12"/>
                                <ColumnDefinition/>
                                <ColumnDefinition Width="12"/>
                            </Grid.ColumnDefinitions>
                            <ScrollViewer x:Name="ExpressionContainer"
                                          Grid.Column="1"
                                          Padding="0,0,0,0"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                          Style="{StaticResource ResultsScrollerSnapped}"
                                          AutomationProperties.AccessibilityView="Raw">
                                <ItemsControl x:Name="TokenList"
                                              Padding="0"
                                              VerticalAlignment="Stretch"
                                              HorizontalContentAlignment="Right"
                                              VerticalContentAlignment="Stretch"
                                              IsTabStop="False"
                                              ItemTemplateSelector="{StaticResource ExpressionItemTemplateSelector}"
                                              ItemsSource="{Binding ExpressionTokens}">
                                    <ItemsControl.ItemsPanel>
                                        <ItemsPanelTemplate>
                                            <ItemsStackPanel HorizontalAlignment="Right"
                                                             VerticalAlignment="Stretch"
                                                             Orientation="Horizontal"/>
                                        </ItemsPanelTemplate>
                                    </ItemsControl.ItemsPanel>
                                </ItemsControl>
                            </ScrollViewer>
                            <Button x:Name="ScrollLeft"
                                    x:Uid="scrollLeft"
                                    Grid.Column="0"
                                    Margin="-4,3,-4,0"
                                    Style="{StaticResource ScrollButtonStyle}">
                                <FontIcon FontFamily="{ThemeResource SymbolThemeFontFamily}"
                                          FontSize="12"
                                          Glyph="&#xE26C;"/>
                            </Button>
                            <Button x:Name="ScrollRight"
                                    x:Uid="scrollRight"
                                    Grid.Column="2"
                                    Margin="0,3,-9,0"
                                    Style="{StaticResource ScrollButtonStyle}">
                                <FontIcon FontFamily="{ThemeResource SymbolThemeFontFamily}"
                                          FontSize="12"
                                          Glyph="&#xE26B;"/>
                            </Button>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="AlwaysOnTopStyleS" TargetType="controls:OverflowTextBlock">
            <Setter Property="HorizontalAlignment" Value="Stretch"/>
            <Setter Property="ColumnWidth" Value="14"/>
            <Setter Property="ColumnHeight" Value="14"/>
            <Setter Property="ScrollButtonFontSize" Value="12"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="controls:OverflowTextBlock">
                        <Grid x:Name="TokenContainer" Background="{TemplateBinding Background}">
                            <Grid.Resources>
                                <ResourceDictionary>
                                    <ResourceDictionary.ThemeDictionaries>
                                        <ResourceDictionary x:Key="Light">
                                            <SolidColorBrush x:Key="ButtonBackgroundPointerOver" Color="Transparent"/>
                                            <SolidColorBrush x:Key="ButtonBackgroundPressed" Color="Transparent"/>
                                            <SolidColorBrush x:Key="ButtonBackgroundDisabled" Color="Transparent"/>
                                            <SolidColorBrush x:Key="ButtonForeground" Color="#99000000"/>
                                            <SolidColorBrush x:Key="ButtonForegroundPointerOver" Color="{ThemeResource SystemAccentColor}" Opacity="0.8"/>
                                            <SolidColorBrush x:Key="ButtonForegroundPressed" Color="{ThemeResource SystemAccentColor}" Opacity="1.0"/>
                                            <SolidColorBrush x:Key="ButtonForegroundDisabled" Color="#33000000"/>
                                        </ResourceDictionary>
                                        <ResourceDictionary x:Key="HighContrast">
                                            <SolidColorBrush x:Key="ButtonBackgroundPointerOver" Color="Transparent"/>
                                            <SolidColorBrush x:Key="ButtonBackgroundPressed" Color="Transparent"/>
                                            <SolidColorBrush x:Key="ButtonBackgroundDisabled" Color="Transparent"/>
                                            <SolidColorBrush x:Key="ButtonForeground" Color="{ThemeResource SystemColorWindowTextColor}" Opacity="0.6"/>
                                            <SolidColorBrush x:Key="ButtonForegroundPointerOver" Color="{ThemeResource SystemColorHighlightColor}" Opacity="0.8"/>
                                            <SolidColorBrush x:Key="ButtonForegroundPressed" Color="{ThemeResource SystemColorHighlightColor}" Opacity="1.0"/>
                                            <SolidColorBrush x:Key="ButtonForegroundDisabled" Color="{ThemeResource SystemColorGrayTextColor}" Opacity="0.2"/>
                                        </ResourceDictionary>
                                        <ResourceDictionary x:Key="Dark">
                                            <SolidColorBrush x:Key="ButtonBackgroundPointerOver" Color="Transparent"/>
                                            <SolidColorBrush x:Key="ButtonBackgroundPressed" Color="Transparent"/>
                                            <SolidColorBrush x:Key="ButtonBackgroundDisabled" Color="Transparent"/>
                                            <SolidColorBrush x:Key="ButtonForeground" Color="#99FFFFFF"/>
                                            <SolidColorBrush x:Key="ButtonForegroundPointerOver" Color="{ThemeResource SystemAccentColorLight1}" Opacity="0.8"/>
                                            <SolidColorBrush x:Key="ButtonForegroundPressed" Color="{ThemeResource SystemAccentColorLight1}" Opacity="1.0"/>
                                            <SolidColorBrush x:Key="ButtonForegroundDisabled" Color="#33FFFFFF"/>
                                        </ResourceDictionary>
                                    </ResourceDictionary.ThemeDictionaries>
                                </ResourceDictionary>
                            </Grid.Resources>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
                            <ScrollViewer x:Name="ExpressionContainer"
                                          Grid.Column="0"
                                          Grid.ColumnSpan="3"
                                          Padding="0,0,0,0"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                          Style="{StaticResource ResultsScrollerSnapped}"
                                          AutomationProperties.AccessibilityView="Control">
                                <TextBlock x:Name="EditableToken"
                                           AutomationProperties.AccessibilityView="Raw"
                                           Text="{Binding DisplayValue, Mode=OneWay}"
                                           Margin="4,0,4,0"
                                           Padding="0"
                                           VerticalAlignment="Stretch"
                                           FontWeight="SemiBold"
                                           Grid.Row="2"
                                           HorizontalAlignment="Right"/>
                            </ScrollViewer>
                            <Border Background="{ThemeResource AppChromeAcrylicHostBackdropMediumLowBrush}"
                                    Grid.Column="0">
                                <Button x:Name="ScrollLeft"
                                    x:Uid="scrollLeft"
                                    Margin="0,3,0,0"
                                    Style="{StaticResource AlwaysOnTopScrollButtonStyleS}"
                                    MinWidth="{TemplateBinding ColumnWidth}"
                                    MinHeight="{TemplateBinding ColumnHeight}"
                                    Background="Transparent">
                                    <FontIcon FontFamily="{ThemeResource SymbolThemeFontFamily}"
                                          FontSize="{TemplateBinding ScrollButtonFontSize}"
                                          Glyph="&#xE26C;"/>
                                </Button>
                            </Border>
                            <Border Background="{ThemeResource AppChromeAcrylicHostBackdropMediumLowBrush}"
                                    Grid.Column="2">
                                <Button x:Name="ScrollRight"
                                    x:Uid="scrollRight"
                                    Margin="0,3,0,0"
                                    Style="{StaticResource AlwaysOnTopScrollButtonStyleS}"
                                    MinWidth="{TemplateBinding ColumnWidth}"
                                    MinHeight="{TemplateBinding ColumnHeight}"
                                    Background="Transparent">
                                    <FontIcon FontFamily="{ThemeResource SymbolThemeFontFamily}"
                                          FontSize="{TemplateBinding ScrollButtonFontSize}"
                                          Glyph="&#xE26B;"/>
                                </Button>
                            </Border>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="AlwaysOnTopStyleM" BasedOn="{StaticResource AlwaysOnTopStyleS}" TargetType="controls:OverflowTextBlock">
            <Setter Property="ColumnWidth" Value="28"/>
            <Setter Property="ColumnHeight" Value="280"/>
            <Setter Property="ScrollButtonFontSize" Value="28"/>
        </Style>

        <!-- Calculation Result Styles -->
        <Style x:Key="ResultsStyle"
               BasedOn="{StaticResource CalculationResultStyle}"
               TargetType="controls:CalculationResult">
            <Setter Property="HorizontalContentAlignment" Value="Right"/>
            <Setter Property="VerticalContentAlignment" Value="Top"/>
            <Setter Property="DisplayMargin" Value="0,0,0,0"/>
            <Setter Property="IsActive" Value="True"/>
            <Setter Property="MinFontSize" Value="12"/>
        </Style>

        <!-- Button Styles -->
        <Style x:Key="ScrollButtonStyle" TargetType="Button">
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="Padding" Value="0,0,0,0"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="Width" Value="20"/>
            <Setter Property="MinWidth" Value="20"/>
            <Setter Property="MinHeight" Value="24"/>
            <Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundAccentBrush}"/>
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="Visibility" Value="Collapsed"/>
        </Style>
        
        <Style x:Key="AlwaysOnTopScrollButtonStyleS" TargetType="Button">
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="Padding" Value="0,0,0,0"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="Visibility" Value="Collapsed"/>
        </Style>

        <!-- Flyout Styles -->

        <Style x:Key="MemoryFlyoutStyle" TargetType="FlyoutPresenter">
            <Setter Property="MinWidth" Value="200"/>
            <Setter Property="MaxHeight" Value="2400"/>
            <Setter Property="MaxWidth" Value="2400"/>
            <Setter Property="ContentTransitions">
                <Setter.Value>
                    <TransitionCollection>
                        <EdgeUIThemeTransition Edge="Bottom"/>
                    </TransitionCollection>
                </Setter.Value>
            </Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="FlyoutPresenter">
                        <Grid Margin="0"
                              Background="Transparent"
                              Tapped="OnMemoryFlyOutTapped">
                            <ContentPresenter HorizontalAlignment="Stretch"
                                              VerticalAlignment="Stretch"
                                              Content="{TemplateBinding Content}"
                                              ContentTemplate="{TemplateBinding ContentTemplate}"
                                              ContentTransitions="{TemplateBinding ContentTransitions}"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="HistoryFlyoutStyle" TargetType="FlyoutPresenter">
            <Setter Property="MinWidth" Value="200"/>
            <Setter Property="MaxHeight" Value="2400"/>
            <Setter Property="MaxWidth" Value="2400"/>
            <Setter Property="ContentTransitions">
                <Setter.Value>
                    <TransitionCollection>
                        <EdgeUIThemeTransition Edge="Bottom"/>
                    </TransitionCollection>
                </Setter.Value>
            </Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="FlyoutPresenter">
                        <Grid Margin="0"
                              Background="Transparent"
                              Tapped="OnHistoryFlyOutTapped">
                            <ContentPresenter HorizontalAlignment="Stretch"
                                              VerticalAlignment="Stretch"
                                              Content="{TemplateBinding Content}"
                                              ContentTemplate="{TemplateBinding ContentTemplate}"
                                              ContentTransitions="{TemplateBinding ContentTransitions}"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <!-- Storyboards -->

        <Storyboard x:Name="Animate">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="NumpadPanel" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.ScaleX)">
                <EasingDoubleKeyFrame KeyTime="0" Value="0.92">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <ExponentialEase EasingMode="EaseOut" Exponent="5"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
                <EasingDoubleKeyFrame KeyTime="0:0:0.367" Value="1">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <ExponentialEase EasingMode="EaseOut" Exponent="5"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="NumpadPanel" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.ScaleY)">
                <EasingDoubleKeyFrame KeyTime="0" Value="0.92">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <ExponentialEase EasingMode="EaseOut" Exponent="5"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
                <EasingDoubleKeyFrame KeyTime="0:0:0.367" Value="1">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <ExponentialEase EasingMode="EaseOut" Exponent="5"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>

        <Storyboard x:Name="AnimateWithoutResult">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="NumpadPanel" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.ScaleX)">
                <EasingDoubleKeyFrame KeyTime="0" Value="0.92">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <ExponentialEase EasingMode="EaseOut" Exponent="5"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
                <EasingDoubleKeyFrame KeyTime="0:0:0.367" Value="1">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <ExponentialEase EasingMode="EaseOut" Exponent="5"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="NumpadPanel" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.ScaleY)">
                <EasingDoubleKeyFrame KeyTime="0" Value="0.92">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <ExponentialEase EasingMode="EaseOut" Exponent="5"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
                <EasingDoubleKeyFrame KeyTime="0:0:0.367" Value="1">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <ExponentialEase EasingMode="EaseOut" Exponent="5"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>

        <!-- Value Converters -->

        <converters:BooleanToVisibilityNegationConverter x:Key="BooleanToVisibilityNegationConverter"/>
        <converters:BooleanNegationConverter x:Key="BooleanNegationConverter"/>

        <!-- DataTemplate Selectors and Style Selectors -->

        <converters:ExpressionItemTemplateSelector x:Key="ExpressionItemTemplateSelector"
                                                   OperandTemplate="{StaticResource Operand}"
                                                   OperatorTemplate="{StaticResource Operator}"
                                                   SeparatorTemplate="{StaticResource Separator}"/>

        <!-- Miscellaneous Resources -->

        <automation:NarratorNotifier x:Name="NarratorNotifier" Announcement="{x:Bind Model.Announcement, Mode=OneWay}"/>

        <!-- Used by hidden shortcut buttons -->
        <x:Int32 x:Key="Zero">0</x:Int32>

        <MenuFlyout x:Key="DisplayContextMenu">
            <MenuFlyoutItem x:Name="CopyMenuItem"
                            x:Uid="CopyMenuItem"
                            Command="{x:Bind Model.CopyCommand}"
                            Icon="Copy"/>
            <MenuFlyoutItem x:Name="PasteMenuItem"
                            x:Uid="PasteMenuItem"
                            Command="{x:Bind Model.PasteCommand}"
                            Icon="Paste"/>
        </MenuFlyout>
    </UserControl.Resources>

    <Grid AutomationProperties.LandmarkType="Main">
        <Grid.ColumnDefinitions>
            <ColumnDefinition x:Name="ColumnMain"/>
            <ColumnDefinition x:Name="ColumnHistory"
                              Width="0"
                              MaxWidth="320"/>
        </Grid.ColumnDefinitions>

        <VisualStateManager.VisualStateGroups>
            <!-- Error Layout specific -->
            <VisualStateGroup x:Name="ErrorVisualStates">
                <VisualState x:Name="NoErrorLayout"/>
                <VisualState x:Name="ErrorLayout">
                    <VisualState.Setters>
                        <Setter Target="ClearMemoryButton.IsEnabled" Value="False"/>
                        <Setter Target="MemRecall.IsEnabled" Value="False"/>
                        <Setter Target="MemPlus.IsEnabled" Value="False"/>
                        <Setter Target="MemMinus.IsEnabled" Value="False"/>
                        <Setter Target="MemButton.IsEnabled" Value="False"/>
                    </VisualState.Setters>
                    <Storyboard Completed="OnErrorLayoutCompleted"/>
                </VisualState>
            </VisualStateGroup>
            <!-- Always-on-Top specific -->
            <VisualStateGroup x:Name="AlwaysOnTopStates">
                <VisualState x:Name="AlwaysOnTop">
                    <VisualState.Setters>
                        <Setter Target="ClearMemoryButton.IsEnabled" Value="False"/>
                        <Setter Target="MemRecall.IsEnabled" Value="False"/>
                        <Setter Target="MemPlus.IsEnabled" Value="False"/>
                        <Setter Target="MemMinus.IsEnabled" Value="False"/>
                        <Setter Target="MemButton.IsEnabled" Value="False"/>
                        <Setter Target="RowExpression.Height" Value="0*"/>
                        <Setter Target="RowHamburger.Height" Value="0"/>
                        <Setter Target="RowMemoryControls.Height" Value="0*"/>
                    </VisualState.Setters>
                </VisualState>
                <VisualState x:Name="Normal">
                    <VisualState.Setters>
                        <Setter Target="RowExpression.Height" Value="20*"/>
                        <Setter Target="RowHamburger.Height" Value="{StaticResource HamburgerHeightGridLength}"/>
                        <Setter Target="RowMemoryControls.Height" Value="32*"/>
                    </VisualState.Setters>
                </VisualState>
            </VisualStateGroup>
            <!-- Mode specific -->
            <VisualStateGroup x:Name="Mode">
                <VisualState x:Name="Standard">
                    <Storyboard Completed="OnStoryboardCompleted"/>
                </VisualState>
                <VisualState x:Name="Scientific">
                    <VisualState.Setters>
                        <Setter Target="RowDisplayControls.Height" Value="32*"/>
                        <Setter Target="RowDisplayControls.MinHeight" Value="32"/>
                        <Setter Target="RowNumPad.Height" Value="276*"/>
                    </VisualState.Setters>
                    <Storyboard Completed="OnStoryboardCompleted"/>
                </VisualState>
                <VisualState x:Name="Programmer">
                    <VisualState.Setters>
                        <Setter Target="RowDisplayControls.Height" Value="96*"/>
                        <Setter Target="RowDisplayControls.MinHeight" Value="96"/>
                        <Setter Target="RowNumPad.Height" Value="268*"/>
                        <Setter Target="M4.Width" Value="0.01*"/>
                        <Setter Target="M4.MaxWidth" Value="99999"/>
                        <Setter Target="M5.Width" Value="1*"/>
                        <Setter Target="M5.MaxWidth" Value="80"/>
                        <Setter Target="MemButton.(Grid.Column)" Value="5"/>
                        <Setter Target="MemoryButton.(Grid.Column)" Value="6"/>
                        <Setter Target="HistoryButton.Visibility" Value="Collapsed"/>
                    </VisualState.Setters>
                    <Storyboard Completed="OnStoryboardCompleted"/>
                </VisualState>
            </VisualStateGroup>
            <!-- Layout specific -->
            <VisualStateGroup CurrentStateChanged="OnVisualStateChanged">
                <VisualState x:Name="Portrait768x1366">
                    <VisualState.StateTriggers>
                        <AdaptiveTrigger MinWindowHeight="1366" MinWindowWidth="768"/>
                    </VisualState.StateTriggers>
                    <VisualState.Setters>
                        <Setter Target="ColumnHistory.Width" Value="320"/>
                        <Setter Target="DockPanel.Visibility" Value="Visible"/>
                        <Setter Target="M6.Width" Value="0"/>
                    </VisualState.Setters>
                    <Storyboard Completed="OnLayoutStateChanged"/>
                </VisualState>
                <VisualState x:Name="LargeWideView">
                    <VisualState.StateTriggers>
                        <AdaptiveTrigger MinWindowHeight="768" MinWindowWidth="1024"/>
                    </VisualState.StateTriggers>
                    <VisualState.Setters>
                        <Setter Target="ColumnHistory.Width" Value="320"/>
                        <Setter Target="DockPanel.Visibility" Value="Visible"/>
                        <Setter Target="M6.Width" Value="0"/>
                    </VisualState.Setters>
                    <Storyboard Completed="OnLayoutStateChanged"/>
                </VisualState>
                <VisualState x:Name="DockVisible">
                    <VisualState.StateTriggers>
                        <AdaptiveTrigger MinWindowHeight="0" MinWindowWidth="560"/>
                    </VisualState.StateTriggers>
                    <VisualState.Setters>
                        <Setter Target="ColumnMain.Width" Value="320*"/>
                        <Setter Target="ColumnHistory.Width" Value="240*"/>
                        <Setter Target="DockPanel.Visibility" Value="Visible"/>
                        <Setter Target="M6.Width" Value="0"/>
                    </VisualState.Setters>
                    <Storyboard Completed="OnLayoutStateChanged"/>
                </VisualState>
                <VisualState x:Name="MinSizeLayout">
                    <VisualState.StateTriggers>
                        <AdaptiveTrigger MinWindowHeight="{StaticResource AppMinWindowHeight}" MinWindowWidth="{StaticResource AppMinWindowWidth}"/>
                    </VisualState.StateTriggers>
                    <Storyboard Completed="OnLayoutStateChanged"/>
                </VisualState>
                <VisualState x:Name="DefaultLayout">
                    <VisualState.StateTriggers>
                        <AdaptiveTrigger MinWindowHeight="0" MinWindowWidth="0"/>
                    </VisualState.StateTriggers>
                    <VisualState.Setters>
                        <Setter Target="RowMemoryControls.MinHeight" Value="0"/>

                        <Setter Target="ClearMemoryButton.Style" Value="{StaticResource CaptionButtonStyle}"/>
                        <Setter Target="MemRecall.Style" Value="{StaticResource CaptionButtonStyle}"/>
                        <Setter Target="MemPlus.Style" Value="{StaticResource CaptionButtonStyle}"/>
                        <Setter Target="MemMinus.Style" Value="{StaticResource CaptionButtonStyle}"/>
                        <Setter Target="MemButton.Style" Value="{StaticResource CaptionButtonStyle}"/>
                        <Setter Target="MemoryButton.MinHeight" Value="0"/>
                    </VisualState.Setters>
                    <Storyboard Completed="OnLayoutStateChanged"/>
                </VisualState>
            </VisualStateGroup>
            <!-- Results display specific -->
            <VisualStateGroup>
                <VisualState x:Name="RegularAlwaysOnTop">
                    <VisualState.StateTriggers>
                        <AdaptiveTrigger MinWindowHeight="260"/>
                    </VisualState.StateTriggers>
                    <VisualState.Setters>
                        <Setter Target="RowResult.MinHeight" Value="54"/>
                        <Setter Target="RowResult.Height" Value="72*"/>
                        <Setter Target="AlwaysOnTopResults.FontSize" Value="40"/>
                        <Setter Target="AlwaysOnTopResults.Style" Value="{StaticResource AlwaysOnTopStyleM}"/>
                    </VisualState.Setters>
                </VisualState>
                <VisualState x:Name="MinAlwaysOnTop">
                    <VisualState.StateTriggers>
                        <AdaptiveTrigger MinWindowHeight="0"/>
                    </VisualState.StateTriggers>
                    <VisualState.Setters>
                        <Setter Target="RowResult.MinHeight" Value="20"/>
                        <Setter Target="RowResult.Height" Value="72*"/>
                        <Setter Target="AlwaysOnTopResults.FontSize" Value="18"/>
                        <Setter Target="AlwaysOnTopResults.Style" Value="{StaticResource AlwaysOnTopStyleS}"/>
                    </VisualState.Setters>
                </VisualState>
            </VisualStateGroup>
            <VisualStateGroup>
                <VisualState x:Name="ResultsL">
                    <VisualState.StateTriggers>
                        <AdaptiveTrigger MinWindowHeight="800"/>
                    </VisualState.StateTriggers>
                    <VisualState.Setters>
                        <Setter Target="Results.MaxFontSize" Value="72"/>
                        <Setter Target="Results.MaxExpressionHistoryCharacters" Value="51"/>
                        <Setter Target="RowResult.MinHeight" Value="108"/>
                        <Setter Target="RowResult.Height" Value="72*"/>
                    </VisualState.Setters>
                </VisualState>
                <VisualState x:Name="ResultsM">
                    <VisualState.StateTriggers>
                        <AdaptiveTrigger x:Name="ResultsMVisualStateTrigger" MinWindowHeight="640"/>
                    </VisualState.StateTriggers>
                    <VisualState.Setters>
                        <Setter Target="Results.MaxFontSize" Value="46"/>
                        <Setter Target="Results.MaxExpressionHistoryCharacters" Value="30"/>
                        <Setter Target="RowResult.MinHeight" Value="72"/>
                        <Setter Target="RowResult.Height" Value="72*"/>
                    </VisualState.Setters>
                </VisualState>
                <VisualState x:Name="ResultsS">
                    <VisualState.StateTriggers>
                        <AdaptiveTrigger MinWindowHeight="0"/>
                    </VisualState.StateTriggers>
                    <VisualState.Setters>
                        <Setter Target="Results.MaxFontSize" Value="26"/>
                        <Setter Target="Results.MaxExpressionHistoryCharacters" Value="30"/>
                        <Setter Target="RowResult.MinHeight" Value="42"/>
                        <Setter Target="RowResult.Height" Value="42*"/>
                    </VisualState.Setters>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>

        <Grid FlowDirection="LeftToRight">
            <Grid.RowDefinitions>
                <RowDefinition x:Name="RowHamburger" Height="{StaticResource HamburgerHeightGridLength}"/>
                <RowDefinition x:Name="RowExpression"
                               Height="20*"
                               MinHeight="0"/>
                <RowDefinition x:Name="RowResult"
                               Height="72*"
                               MinHeight="20"/>
                <RowDefinition x:Name="RowDisplayControls" Height="0"/>
                <RowDefinition x:Name="RowMemoryControls"
                               Height="32*"
                               MinHeight="0"/>
                <RowDefinition x:Name="RowNumPad"
                               Height="308*"
                               MinHeight="0"/>
            </Grid.RowDefinitions>

            <controls:OverflowTextBlock x:Name="ExpressionText"
                                        Grid.Row="1"
                                        Margin="6,0,6,0"
                                        Style="{StaticResource NormalStyle}"
                                        VerticalAlignment="Bottom"
                                        AutomationProperties.AutomationId="CalculatorExpression"
                                        AutomationProperties.Name="{x:Bind Model.CalculationExpressionAutomationName, Mode=OneWay}"
                                        IsTabStop="False"
                                        TokensUpdated="{x:Bind Model.AreTokensUpdated, Mode=OneWay}"
                                        Visibility="{x:Bind Model.IsAlwaysOnTop, Converter={StaticResource BooleanToVisibilityNegationConverter}, Mode=OneWay}"/>
            <controls:CalculationResult x:Name="Results"
                                        x:Uid="CalculatorResults"
                                        Grid.Row="2"
                                        Margin="0,0,0,0"
                                        Style="{StaticResource ResultsStyle}"
                                        AutomationProperties.AutomationId="CalculatorResults"
                                        AutomationProperties.HeadingLevel="Level1"
                                        AutomationProperties.Name="{x:Bind Model.CalculationResultAutomationName, Mode=OneWay}"
                                        ContextCanceled="OnContextCanceled"
                                        ContextRequested="OnContextRequested"
                                        DisplayStringExpression="{x:Bind Model.DisplayStringExpression, Mode=OneWay}"
                                        DisplayValue="{x:Bind Model.DisplayValue, Mode=OneWay}"
                                        ExpressionVisibility="Visible"
                                        IsInError="{x:Bind Model.IsInError, Mode=OneWay}"
                                        IsOperatorCommand="{x:Bind Model.IsOperatorCommand, Mode=OneWay}"
                                        TabIndex="1"
                                        Visibility="{x:Bind Model.IsAlwaysOnTop, Converter={StaticResource BooleanToVisibilityNegationConverter}, Mode=OneWay}"/>
            <controls:OverflowTextBlock x:Name="AlwaysOnTopResults"
                                        x:Uid="CalculatorAlwaysOnTopResults"
                                        Grid.Row="2"
                                        Margin="6,0,6,0"
                                        AutomationProperties.AutomationId="CalculatorAlwaysOnTopResults"
                                        AutomationProperties.HeadingLevel="Level1"
                                        AutomationProperties.Name="{x:Bind Model.CalculationResultAutomationName, Mode=OneWay}"
                                        TokensUpdated="{x:Bind Model.AreAlwaysOnTopResultsUpdated, Mode=OneWay}"
                                        HorizontalContentAlignment="Right"
                                        IsActive="True"
                                        UseSystemFocusVisuals="True"
                                        Visibility="{x:Bind Model.IsAlwaysOnTop, Mode=OneWay}"/>

            <!-- Programmer display panel controls -->
            <local:CalculatorProgrammerOperators x:Name="ProgrammerOperators"
                                                 Grid.Row="3"
                                                 x:Load="False"
                                                 IsEnabled="{x:Bind Model.IsProgrammer, Mode=OneWay}"
                                                 TabIndex="6"
                                                 Visibility="{x:Bind Model.IsProgrammer, Mode=OneWay}"/>

            <local:CalculatorProgrammerDisplayPanel x:Name="ProgrammerDisplayPanel"
                                                    Grid.Row="4"
                                                    x:Load="False"
                                                    IsEnabled="{x:Bind Model.IsProgrammer, Mode=OneWay}"
                                                    TabIndex="7"
                                                    Visibility="{x:Bind Model.IsProgrammer, Mode=OneWay}"/>

            <Grid x:Name="HistoryButtonParent"
                  Visibility="{x:Bind Model.IsAlwaysOnTop, Converter={StaticResource BooleanToVisibilityNegationConverter}, Mode=OneWay}">
                <Button x:Name="HistoryButton"
                    x:Uid="HistoryButton"
                    Grid.Row="0"
                    IsEnabled="{x:Bind Model.IsAlwaysOnTop, Converter={StaticResource BooleanNegationConverter}, Mode=OneWay}"
                    Style="{StaticResource HistoryButtonStyle}"
                    AutomationProperties.AutomationId="HistoryButton"
                    Command="{x:Bind HistoryButtonPressed, Mode=OneTime}"
                    Content="&#xe81c;"
                    ExitDisplayModeOnAccessKeyInvoked="False"
                    TabIndex="2">
                    <FlyoutBase.AttachedFlyout>
                        <Flyout x:Name="HistoryFlyout"
                                AutomationProperties.AutomationId="HistoryFlyout"
                                Closed="HistoryFlyout_Closed"
                                FlyoutPresenterStyle="{StaticResource HistoryFlyoutStyle}"
                                Opened="HistoryFlyout_Opened"
                                Placement="Full"/>
                    </FlyoutBase.AttachedFlyout>
                </Button>
            </Grid>
            
            <!-- Scientific angle buttons -->
            <local:CalculatorScientificAngleButtons x:Name="ScientificAngleButtons"
                                                    Grid.Row="3"
                                                    x:Load="False"
                                                    IsEnabled="{x:Bind Model.IsScientific, Mode=OneWay}"
                                                    TabIndex="6"
                                                    Visibility="{x:Bind Model.IsScientific, Mode=OneWay}"/>

            <!-- Memory panel controls -->
            <Grid x:Name="MemoryPanel"
                  x:Uid="MemoryPanel"
                  Grid.Row="4"
                  Margin="3,0,3,0"
                  Visibility="{x:Bind Model.IsAlwaysOnTop, Converter={StaticResource BooleanToVisibilityNegationConverter}, Mode=OneWay}"
                  AutomationProperties.HeadingLevel="Level1">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="1*" MaxWidth="80"/>
                    <ColumnDefinition Width="1*" MaxWidth="80"/>
                    <ColumnDefinition Width="1*" MaxWidth="80"/>
                    <ColumnDefinition Width="1*" MaxWidth="80"/>
                    <ColumnDefinition x:Name="M4"
                                      Width="1*"
                                      MaxWidth="80"/>
                    <ColumnDefinition x:Name="M5" Width="0.01*"/>
                    <ColumnDefinition x:Name="M6"
                                      Width="1*"
                                      MaxWidth="80"/>
                </Grid.ColumnDefinitions>

                <controls:CalculatorButton x:Name="ClearMemoryButton"
                                           x:Uid="ClearMemoryButton"
                                           Style="{StaticResource CaptionButtonStyle}"
                                           AutomationProperties.AutomationId="ClearMemoryButton"
                                           Command="{x:Bind Model.ClearMemoryCommand}"
                                           Content="MC"
                                           TabIndex="10"
                                           Visibility="{Binding IsProgrammer, Converter={StaticResource BooleanToVisibilityNegationConverter}}"/>
                <controls:CalculatorButton x:Name="MemRecall"
                                           x:Uid="MemRecall"
                                           Grid.Column="1"
                                           Style="{StaticResource CaptionButtonStyle}"
                                           AutomationProperties.AutomationId="MemRecall"
                                           Command="{x:Bind Model.MemoryItemPressed}"
                                           CommandParameter="{StaticResource Zero}"
                                           Content="MR"
                                           TabIndex="11"
                                           Visibility="{Binding IsProgrammer, Converter={StaticResource BooleanToVisibilityNegationConverter}}"/>
                <controls:CalculatorButton x:Name="MemPlus"
                                           x:Uid="MemPlus"
                                           Grid.Column="2"
                                           Style="{StaticResource CaptionButtonStyle}"
                                           AutomationProperties.AutomationId="MemPlus"
                                           Command="{x:Bind Model.MemoryAdd}"
                                           CommandParameter="{StaticResource Zero}"
                                           Content="M+"
                                           TabIndex="12"
                                           Visibility="{Binding IsProgrammer, Converter={StaticResource BooleanToVisibilityNegationConverter}}"/>
                <controls:CalculatorButton x:Name="MemMinus"
                                           x:Uid="MemMinus"
                                           Grid.Column="3"
                                           Style="{StaticResource CaptionButtonStyle}"
                                           AutomationProperties.AutomationId="MemMinus"
                                           Command="{x:Bind Model.MemorySubtract}"
                                           CommandParameter="{StaticResource Zero}"
                                           Content="M-"
                                           TabIndex="13"
                                           Visibility="{Binding IsProgrammer, Converter={StaticResource BooleanToVisibilityNegationConverter}}"/>
                <controls:CalculatorButton x:Name="MemButton"
                                           x:Uid="memButton"
                                           Grid.Column="4"
                                           Style="{StaticResource CaptionButtonStyle}"
                                           AutomationProperties.AutomationId="memButton"
                                           ButtonId="Memory"
                                           Content="MS"
                                           TabIndex="14"/>
                <Button x:Name="MemoryButton"
                        x:Uid="MemoryButton"
                        Grid.Column="6"
                        MinWidth="15.5"
                        MinHeight="8.402"
                        Style="{StaticResource PathFakeButtonStyle}"
                        AutomationProperties.AutomationId="MemoryButton"
                        Click="ToggleMemoryFlyout"
                        Content="M0,9.46968e-006L1.96199,9.46968e-006L4.17099,5.59502C4.33899,6.02501 4.44899,6.34502 4.5,6.55602L4.52899,6.55602C4.67299,6.11501 4.791,5.78701 4.87999,5.57201L7.12999,9.46968e-006L9.023,9.46968e-006L9.023,8.402L7.64,8.402L7.64,2.96501C7.64,2.519 7.668,1.97401 7.722,1.33L7.69899,1.33C7.613,1.697 7.53699,1.96101 7.46999,2.12102L4.96199,8.402L4.002,8.402L1.48799,2.16801C1.418,1.98399 1.34299,1.705 1.265,1.33L1.24199,1.33C1.273,1.666 1.28899,2.21501 1.28899,2.976L1.28899,8.402L0,8.402zM10.5,0L15.5,0L12.99,2.5z"
                        ExitDisplayModeOnAccessKeyInvoked="False"
                        IsEnabled="{x:Bind Model.IsMemoryEmpty, Converter={StaticResource BooleanNegationConverter}, Mode=OneWay}"
                        TabIndex="15">
                    <FlyoutBase.AttachedFlyout>
                        <Flyout x:Name="MemoryFlyout"
                                x:Uid="MemoryFlyout"
                                AutomationProperties.AutomationId="MemoryFlyout"
                                Closed="OnMemoryFlyoutClosed"
                                FlyoutPresenterStyle="{StaticResource MemoryFlyoutStyle}"
                                Opened="OnMemoryFlyoutOpened"
                                Placement="Full"/>
                    </FlyoutBase.AttachedFlyout>
                </Button>
            </Grid>

            <Grid x:Name="NumpadPanel"
                  Grid.Row="5"
                  Margin="3,0,3,3"
                  RenderTransformOrigin="0.5,0.5">
                <Grid.RenderTransform>
                    <CompositeTransform/>
                </Grid.RenderTransform>

                <local:OperatorsPanel x:Name="OpsPanel" IsBitFlipChecked="{x:Bind Model.IsBitFlipChecked, Mode=OneWay}"/>
            </Grid>
        </Grid>

        <!-- Docked Pivot panel for history/memory -->
        <Border x:Name="DockPanel"
                x:Uid="DockPanel"
                Grid.Row="1"
                Grid.Column="1"
                AutomationProperties.HeadingLevel="Level1"
                Visibility="Collapsed">
            <Border.Resources>
                <!--
                    This is a copy/paste of the Pivot template. The only change is setting the PivotPanel's ManipulationMode=None
                    to disable swipe navigation between History and Memory to enable the SwipeControl
                -->
                <ControlTemplate x:Key="DockPanelTemplate" TargetType="Pivot">
                    <Grid x:Name="RootElement"
                          HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
                          VerticalAlignment="{TemplateBinding VerticalAlignment}"
                          Background="{TemplateBinding Background}">
                        <Grid.Resources>
                            <Style x:Key="BaseContentControlStyle" TargetType="ContentControl">
                                <Setter Property="FontFamily" Value="XamlAutoFontFamily"/>
                                <Setter Property="FontWeight" Value="SemiBold"/>
                                <Setter Property="FontSize" Value="15"/>
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="ContentControl">
                                            <ContentPresenter Margin="{TemplateBinding Padding}"
                                                              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                                              Content="{TemplateBinding Content}"
                                                              ContentTemplate="{TemplateBinding ContentTemplate}"
                                                              ContentTransitions="{TemplateBinding ContentTransitions}"
                                                              OpticalMarginAlignment="TrimSideBearings"/>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                            <Style x:Key="TitleContentControlStyle"
                                   BasedOn="{StaticResource BaseContentControlStyle}"
                                   TargetType="ContentControl">
                                <Setter Property="FontFamily" Value="{ThemeResource PivotTitleFontFamily}"/>
                                <Setter Property="FontWeight" Value="{ThemeResource PivotTitleThemeFontWeight}"/>
                                <Setter Property="FontSize" Value="{ThemeResource PivotTitleFontSize}"/>
                            </Style>
                        </Grid.Resources>

                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>

                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="Orientation">
                                <VisualState x:Name="Portrait">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="TitleContentControl" Storyboard.TargetProperty="Margin">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource PivotPortraitThemePadding}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Landscape">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="TitleContentControl" Storyboard.TargetProperty="Margin">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource PivotLandscapeThemePadding}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="NavigationButtonsVisibility">
                                <VisualState x:Name="NavigationButtonsHidden"/>
                                <VisualState x:Name="NavigationButtonsVisible">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="NextButton" Storyboard.TargetProperty="Opacity">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="1"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="NextButton" Storyboard.TargetProperty="IsEnabled">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="True"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PreviousButton" Storyboard.TargetProperty="Opacity">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="1"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PreviousButton" Storyboard.TargetProperty="IsEnabled">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="True"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="PreviousButtonVisible">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PreviousButton" Storyboard.TargetProperty="Opacity">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="1"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PreviousButton" Storyboard.TargetProperty="IsEnabled">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="True"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="NextButtonVisible">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="NextButton" Storyboard.TargetProperty="Opacity">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="1"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="NextButton" Storyboard.TargetProperty="IsEnabled">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="True"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="HeaderStates">
                                <VisualState x:Name="HeaderDynamic"/>
                                <VisualState x:Name="HeaderStatic">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Header" Storyboard.TargetProperty="Visibility">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="StaticHeader" Storyboard.TargetProperty="Visibility">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>

                        </VisualStateManager.VisualStateGroups>

                        <ContentControl x:Name="TitleContentControl"
                                        Margin="{StaticResource PivotPortraitThemePadding}"
                                        Style="{StaticResource TitleContentControlStyle}"
                                        Content="{TemplateBinding Title}"
                                        ContentTemplate="{TemplateBinding TitleTemplate}"
                                        IsTabStop="False"
                                        Visibility="Collapsed"/>

                        <Grid Grid.Row="1">
                            <Grid.Resources>
                                <ControlTemplate x:Key="NextTemplate" TargetType="Button">
                                    <Border x:Name="Root"
                                            Background="{ThemeResource PivotNextButtonBackground}"
                                            BorderBrush="{ThemeResource PivotNextButtonBorderBrush}"
                                            BorderThickness="{ThemeResource PivotNavButtonBorderThemeThickness}">
                                        <VisualStateManager.VisualStateGroups>
                                            <VisualStateGroup x:Name="CommonStates">
                                                <VisualState x:Name="Normal"/>
                                                <VisualState x:Name="PointerOver">
                                                    <Storyboard>
                                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Root" Storyboard.TargetProperty="Background">
                                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource PivotNextButtonBackgroundPointerOver}"/>
                                                        </ObjectAnimationUsingKeyFrames>
                                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Root" Storyboard.TargetProperty="BorderBrush">
                                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource PivotNextButtonBorderBrushPointerOver}"/>
                                                        </ObjectAnimationUsingKeyFrames>
                                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Arrow" Storyboard.TargetProperty="Foreground">
                                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource PivotNextButtonForegroundPointerOver}"/>
                                                        </ObjectAnimationUsingKeyFrames>
                                                    </Storyboard>
                                                </VisualState>
                                                <VisualState x:Name="Pressed">
                                                    <Storyboard>
                                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Root" Storyboard.TargetProperty="Background">
                                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource PivotNextButtonBackgroundPressed}"/>
                                                        </ObjectAnimationUsingKeyFrames>
                                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Root" Storyboard.TargetProperty="BorderBrush">
                                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource PivotNextButtonBorderBrushPressed}"/>
                                                        </ObjectAnimationUsingKeyFrames>
                                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Arrow" Storyboard.TargetProperty="Foreground">
                                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource PivotNextButtonForegroundPressed}"/>
                                                        </ObjectAnimationUsingKeyFrames>
                                                    </Storyboard>
                                                </VisualState>
                                            </VisualStateGroup>
                                        </VisualStateManager.VisualStateGroups>
                                        <FontIcon x:Name="Arrow"
                                                  HorizontalAlignment="Center"
                                                  VerticalAlignment="Center"
                                                  Foreground="{ThemeResource PivotNextButtonForeground}"
                                                  FontFamily="{ThemeResource SymbolThemeFontFamily}"
                                                  FontSize="12"
                                                  Glyph="&#xE0E3;"
                                                  MirroredWhenRightToLeft="True"
                                                  UseLayoutRounding="False"/>
                                    </Border>
                                </ControlTemplate>

                                <ControlTemplate x:Key="PreviousTemplate" TargetType="Button">
                                    <Border x:Name="Root"
                                            Background="{ThemeResource PivotPreviousButtonBackground}"
                                            BorderBrush="{ThemeResource PivotPreviousButtonBorderBrush}"
                                            BorderThickness="{ThemeResource PivotNavButtonBorderThemeThickness}">
                                        <VisualStateManager.VisualStateGroups>
                                            <VisualStateGroup x:Name="CommonStates">
                                                <VisualState x:Name="Normal"/>
                                                <VisualState x:Name="PointerOver">
                                                    <Storyboard>
                                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Root" Storyboard.TargetProperty="Background">
                                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource PivotPreviousButtonBackgroundPointerOver}"/>
                                                        </ObjectAnimationUsingKeyFrames>
                                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Root" Storyboard.TargetProperty="BorderBrush">
                                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource PivotPreviousButtonBorderBrushPointerOver}"/>
                                                        </ObjectAnimationUsingKeyFrames>
                                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Arrow" Storyboard.TargetProperty="Foreground">
                                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource PivotPreviousButtonForegroundPointerOver}"/>
                                                        </ObjectAnimationUsingKeyFrames>
                                                    </Storyboard>
                                                </VisualState>
                                                <VisualState x:Name="Pressed">
                                                    <Storyboard>
                                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Root" Storyboard.TargetProperty="Background">
                                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource PivotPreviousButtonBackgroundPressed}"/>
                                                        </ObjectAnimationUsingKeyFrames>
                                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Root" Storyboard.TargetProperty="BorderBrush">
                                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource PivotPreviousButtonBorderBrushPressed}"/>
                                                        </ObjectAnimationUsingKeyFrames>
                                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Arrow" Storyboard.TargetProperty="Foreground">
                                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource PivotPreviousButtonForegroundPressed}"/>
                                                        </ObjectAnimationUsingKeyFrames>
                                                    </Storyboard>
                                                </VisualState>
                                            </VisualStateGroup>
                                        </VisualStateManager.VisualStateGroups>
                                        <FontIcon x:Name="Arrow"
                                                  HorizontalAlignment="Center"
                                                  VerticalAlignment="Center"
                                                  Foreground="{ThemeResource PivotPreviousButtonForeground}"
                                                  FontFamily="{ThemeResource SymbolThemeFontFamily}"
                                                  FontSize="12"
                                                  Glyph="&#xE0E2;"
                                                  MirroredWhenRightToLeft="True"
                                                  UseLayoutRounding="False"/>
                                    </Border>
                                </ControlTemplate>
                            </Grid.Resources>

                            <ScrollViewer x:Name="ScrollViewer"
                                          Margin="{TemplateBinding Padding}"
                                          VerticalContentAlignment="Stretch"
                                          BringIntoViewOnFocusChange="False"
                                          HorizontalScrollBarVisibility="Hidden"
                                          HorizontalSnapPointsAlignment="Center"
                                          HorizontalSnapPointsType="MandatorySingle"
                                          Template="{StaticResource ScrollViewerScrollBarlessTemplate}"
                                          VerticalScrollBarVisibility="Disabled"
                                          VerticalScrollMode="Disabled"
                                          VerticalSnapPointsType="None"
                                          ZoomMode="Disabled">
                                <PivotPanel x:Name="Panel"
                                            VerticalAlignment="Stretch"
                                            ManipulationMode="None">
                                    <Grid x:Name="PivotLayoutElement">
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="*"/>
                                        </Grid.RowDefinitions>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto"/>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="Auto"/>
                                        </Grid.ColumnDefinitions>
                                        <Grid.RenderTransform>
                                            <CompositeTransform x:Name="PivotLayoutElementTranslateTransform"/>
                                        </Grid.RenderTransform>
                                        <ContentPresenter x:Name="LeftHeaderPresenter"
                                                          HorizontalAlignment="Stretch"
                                                          VerticalAlignment="Stretch"
                                                          Content="{TemplateBinding LeftHeader}"
                                                          ContentTemplate="{TemplateBinding LeftHeaderTemplate}"/>
                                        <ContentControl x:Name="HeaderClipper"
                                                        Grid.Column="1"
                                                        HorizontalContentAlignment="Stretch"
                                                        UseSystemFocusVisuals="{StaticResource UseSystemFocusVisuals}">
                                            <ContentControl.Clip>
                                                <RectangleGeometry x:Name="HeaderClipperGeometry"/>
                                            </ContentControl.Clip>
                                            <Grid Background="{ThemeResource PivotHeaderBackground}">
                                                <Grid.RenderTransform>
                                                    <CompositeTransform x:Name="HeaderOffsetTranslateTransform"/>
                                                </Grid.RenderTransform>
                                                <PivotHeaderPanel x:Name="StaticHeader" Visibility="Collapsed">
                                                    <PivotHeaderPanel.RenderTransform>
                                                        <CompositeTransform x:Name="StaticHeaderTranslateTransform"/>
                                                    </PivotHeaderPanel.RenderTransform>
                                                </PivotHeaderPanel>
                                                <PivotHeaderPanel x:Name="Header">
                                                    <PivotHeaderPanel.RenderTransform>
                                                        <CompositeTransform x:Name="HeaderTranslateTransform"/>
                                                    </PivotHeaderPanel.RenderTransform>
                                                </PivotHeaderPanel>
                                                <Rectangle x:Name="FocusFollower"
                                                           HorizontalAlignment="Stretch"
                                                           VerticalAlignment="Stretch"
                                                           Fill="Transparent"
                                                           Control.IsTemplateFocusTarget="True"
                                                           IsHitTestVisible="False"/>
                                            </Grid>
                                        </ContentControl>
                                        <Button x:Name="PreviousButton"
                                                Grid.Column="1"
                                                Width="20"
                                                Height="36"
                                                Margin="{ThemeResource PivotNavButtonMargin}"
                                                HorizontalAlignment="Left"
                                                VerticalAlignment="Top"
                                                Background="Transparent"
                                                Opacity="0"
                                                IsEnabled="False"
                                                IsTabStop="False"
                                                Template="{StaticResource PreviousTemplate}"
                                                UseSystemFocusVisuals="False"/>
                                        <Button x:Name="NextButton"
                                                Grid.Column="1"
                                                Width="20"
                                                Height="36"
                                                Margin="{ThemeResource PivotNavButtonMargin}"
                                                HorizontalAlignment="Right"
                                                VerticalAlignment="Top"
                                                Background="Transparent"
                                                Opacity="0"
                                                IsEnabled="False"
                                                IsTabStop="False"
                                                Template="{StaticResource NextTemplate}"
                                                UseSystemFocusVisuals="False"/>
                                        <ContentPresenter x:Name="RightHeaderPresenter"
                                                          Grid.Column="2"
                                                          HorizontalAlignment="Stretch"
                                                          VerticalAlignment="Stretch"
                                                          Content="{TemplateBinding RightHeader}"
                                                          ContentTemplate="{TemplateBinding RightHeaderTemplate}"/>
                                        <ItemsPresenter x:Name="PivotItemPresenter"
                                                        Grid.Row="1"
                                                        Grid.ColumnSpan="3">
                                            <ItemsPresenter.RenderTransform>
                                                <TransformGroup>
                                                    <TranslateTransform x:Name="ItemsPresenterTranslateTransform"/>
                                                    <CompositeTransform x:Name="ItemsPresenterCompositeTransform"/>
                                                </TransformGroup>
                                            </ItemsPresenter.RenderTransform>
                                        </ItemsPresenter>
                                    </Grid>
                                </PivotPanel>
                            </ScrollViewer>
                        </Grid>
                    </Grid>
                </ControlTemplate>
            </Border.Resources>

            <Pivot x:Name="DockPivot"
                   TabIndex="5"
                   Tapped="DockPanelTapped"
                   Template="{StaticResource DockPanelTemplate}">
                <Pivot.Resources>
                    <Style TargetType="PivotHeaderItem">
                        <Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundBaseHighBrush}"/>
                        <Setter Property="Background" Value="{ThemeResource SystemControlBackgroundTransparentBrush}"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="PivotHeaderItem">
                                    <Grid x:Name="Grid"
                                          Padding="{TemplateBinding Padding}"
                                          Background="{TemplateBinding Background}">
                                        <Grid.RenderTransform>
                                            <TranslateTransform x:Name="ContentPresenterTranslateTransform"/>
                                        </Grid.RenderTransform>
                                        <VisualStateManager.VisualStateGroups>
                                            <VisualStateGroup x:Name="SelectionStates">
                                                <VisualStateGroup.Transitions>
                                                    <VisualTransition From="Unselected"
                                                                      GeneratedDuration="0:0:0.33"
                                                                      To="UnselectedLocked"/>
                                                    <VisualTransition From="UnselectedLocked"
                                                                      GeneratedDuration="0:0:0.33"
                                                                      To="Unselected"/>
                                                </VisualStateGroup.Transitions>
                                                <VisualState x:Name="Disabled">
                                                    <VisualState.Setters>
                                                        <Setter Target="SelectedPipe.Visibility" Value="Collapsed"/>
                                                        <Setter Target="ContentPresenter.Foreground" Value="{ThemeResource PivotHeaderItemForegroundDisabled}"/>
                                                        <Setter Target="Grid.Background" Value="{ThemeResource PivotHeaderItemBackgroundDisabled}"/>
                                                    </VisualState.Setters>
                                                </VisualState>
                                                <VisualState x:Name="Unselected"/>
                                                <VisualState x:Name="UnselectedLocked">
                                                    <VisualState.Setters>
                                                        <Setter Target="SelectedPipe.Visibility" Value="Collapsed"/>
                                                        <Setter Target="ContentPresenter.(UIElement.Opacity)" Value="0"/>
                                                    </VisualState.Setters>
                                                    <Storyboard>
                                                        <DoubleAnimation Duration="0"
                                                                         Storyboard.TargetName="ContentPresenterTranslateTransform"
                                                                         Storyboard.TargetProperty="X"
                                                                         To="{ThemeResource PivotHeaderItemLockedTranslation}"/>
                                                    </Storyboard>
                                                </VisualState>
                                                <VisualState x:Name="Selected">
                                                    <!--
                                                        TODO: MSFT 13767760: Need to use storyboards here because
                                                        visualstate setters are bugged
                                                    -->
                                                    <Storyboard>
                                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="SelectedPipe" Storyboard.TargetProperty="Visibility">
                                                            <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
                                                        </ObjectAnimationUsingKeyFrames>
                                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource PivotHeaderItemForegroundSelected}"/>
                                                        </ObjectAnimationUsingKeyFrames>
                                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Grid" Storyboard.TargetProperty="Background">
                                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource PivotHeaderItemBackgroundSelected}"/>
                                                        </ObjectAnimationUsingKeyFrames>
                                                    </Storyboard>
                                                </VisualState>
                                                <VisualState x:Name="UnselectedPointerOver">
                                                    <VisualState.Setters>
                                                        <Setter Target="SelectedPipe.Visibility" Value="Collapsed"/>
                                                        <Setter Target="ContentPresenter.Foreground" Value="{ThemeResource PivotHeaderItemForegroundUnselectedPointerOver}"/>
                                                        <Setter Target="Grid.Background" Value="{ThemeResource PivotHeaderItemBackgroundUnselectedPointerOver}"/>
                                                    </VisualState.Setters>
                                                </VisualState>
                                                <VisualState x:Name="SelectedPointerOver">
                                                    <VisualState.Setters>
                                                        <Setter Target="SelectedPipe.Visibility" Value="Visible"/>
                                                        <Setter Target="ContentPresenter.Foreground" Value="{ThemeResource PivotHeaderItemForegroundSelectedPointerOver}"/>
                                                        <Setter Target="Grid.Background" Value="{ThemeResource PivotHeaderItemBackgroundSelectedPointerOver}"/>
                                                    </VisualState.Setters>
                                                </VisualState>
                                                <VisualState x:Name="UnselectedPressed">
                                                    <VisualState.Setters>
                                                        <Setter Target="SelectedPipe.Visibility" Value="Collapsed"/>
                                                        <Setter Target="ContentPresenter.Foreground" Value="{ThemeResource PivotHeaderItemForegroundUnselectedPressed}"/>
                                                        <Setter Target="Grid.Background" Value="{ThemeResource PivotHeaderItemBackgroundUnselectedPressed}"/>
                                                    </VisualState.Setters>
                                                </VisualState>
                                                <VisualState x:Name="SelectedPressed">
                                                    <VisualState.Setters>
                                                        <Setter Target="SelectedPipe.Visibility" Value="Visible"/>
                                                        <Setter Target="ContentPresenter.Foreground" Value="{ThemeResource PivotHeaderItemForegroundSelectedPressed}"/>
                                                        <Setter Target="Grid.Background" Value="{ThemeResource PivotHeaderItemBackgroundSelectedPressed}"/>
                                                    </VisualState.Setters>
                                                </VisualState>
                                            </VisualStateGroup>
                                            <VisualStateGroup x:Name="FocusStates">
                                                <VisualState x:Name="Focused">
                                                    <VisualState.Setters>
                                                        <Setter Target="FocusPipe.Visibility" Value="Visible"/>
                                                    </VisualState.Setters>
                                                </VisualState>
                                                <VisualState x:Name="Unfocused"/>
                                            </VisualStateGroup>
                                        </VisualStateManager.VisualStateGroups>
                                        <Rectangle x:Name="FocusPipe"
                                                   Stroke="{ThemeResource SystemControlFocusVisualPrimaryBrush}"
                                                   StrokeThickness="2"
                                                   Visibility="Collapsed"/>
                                        <Grid Margin="4,0,4,0">
                                            <ContentPresenter x:Name="ContentPresenter"
                                                              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                                              FontFamily="{TemplateBinding FontFamily}"
                                                              FontSize="{TemplateBinding FontSize}"
                                                              FontWeight="{TemplateBinding FontWeight}"
                                                              Content="{TemplateBinding Content}"
                                                              ContentTemplate="{TemplateBinding ContentTemplate}"
                                                              OpticalMarginAlignment="TrimSideBearings"/>
                                            <Rectangle x:Name="SelectedPipe"
                                                       Height="3"
                                                       Margin="0,0,0,6"
                                                       HorizontalAlignment="Stretch"
                                                       VerticalAlignment="Bottom"
                                                       Fill="{ThemeResource AppControlTransparentAccentColorBrush}"
                                                       Visibility="Collapsed"/>
                                        </Grid>
                                    </Grid>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </Pivot.Resources>
                <PivotItem x:Name="HistoryPivotItem"
                           x:Uid="HistoryPivotItem"
                           Margin="0,10,0,0"
                           AutomationProperties.AutomationId="HistoryLabel">
                    <PivotItem.Header>
                        <TextBlock x:Uid="HistoryLabel" AccessKeyInvoked="OnHistoryAccessKeyInvoked"/>
                    </PivotItem.Header>
                    <Border x:Name="DockHistoryHolder"/>
                </PivotItem>
                <PivotItem x:Name="MemoryPivotItem"
                           x:Uid="MemoryPivotItem"
                           Margin="0,10,0,0"
                           AutomationProperties.AutomationId="MemoryLabel">
                    <PivotItem.Header>
                        <TextBlock x:Uid="MemoryLabel" AccessKeyInvoked="OnMemoryAccessKeyInvoked"/>
                    </PivotItem.Header>
                    <Border x:Name="DockMemoryHolder"/>
                </PivotItem>
            </Pivot>
        </Border>
    </Grid>
</UserControl>
