<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:APKInstaller.Controls">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="TitleBar.ThemeResources.xaml" />
    </ResourceDictionary.MergedDictionaries>

    <Style TargetType="local:TitleBar">
        <Setter Property="VerticalAlignment" Value="Top" />
        <Setter Property="HorizontalAlignment" Value="Stretch" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:TitleBar">
                    <Grid x:Name="LayoutRoot" Background="{TemplateBinding Background}">
                        <Grid.BackgroundTransition>
                            <BrushTransition />
                        </Grid.BackgroundTransition>

                        <Grid.RowDefinitions>
                            <RowDefinition Height="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.TopPaddingColumnGridLength}" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>

                        <Grid
                            x:Name="TitleRoot"
                            Grid.Row="1"
                            Height="{ThemeResource TitleBarExpandedHeight}">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.LeftPaddingColumnGridLength}" />
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.RightPaddingColumnGridLength}" />
                            </Grid.ColumnDefinitions>

                            <Grid Grid.Column="1">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <!--  0: back button  -->
                                    <ColumnDefinition Width="Auto" />
                                    <!--  1: icon  -->
                                    <ColumnDefinition Width="*" />
                                    <!--  2: title  -->
                                    <ColumnDefinition Width="Auto" />
                                    <!--  3: custom content  -->
                                </Grid.ColumnDefinitions>

                                <Grid.ChildrenTransitions>
                                    <TransitionCollection>
                                        <ReorderThemeTransition />
                                    </TransitionCollection>
                                </Grid.ChildrenTransitions>

                                <Button
                                    x:Name="BackButton"
                                    x:Uid="/TitleBar/BackButton"
                                    Grid.Column="0"
                                    IsEnabled="{TemplateBinding IsBackEnabled}"
                                    Style="{ThemeResource TitleBarBackButtonStyle}" />

                                <Viewbox
                                    x:Name="Icon"
                                    Grid.Column="1"
                                    MaxWidth="16"
                                    MaxHeight="16"
                                    Margin="4,0,16,0"
                                    VerticalAlignment="Center"
                                    Child="{TemplateBinding IconSource}" />

                                <TextBlock
                                    x:Name="TitleText"
                                    Grid.Column="2"
                                    Margin="0,0,16,2"
                                    HorizontalAlignment="Left"
                                    VerticalAlignment="Center"
                                    FontSize="{ThemeResource ControlContentThemeFontSize}"
                                    Foreground="{ThemeResource TitleBarForegroundBrush}"
                                    Style="{StaticResource CaptionTextBlockStyle}"
                                    Text="{TemplateBinding Title}"
                                    TextTrimming="CharacterEllipsis"
                                    TextWrapping="NoWrap" />

                                <Grid
                                    x:Name="DragRegion"
                                    Grid.Column="1"
                                    Grid.ColumnSpan="3"
                                    Background="Transparent" />

                                <Grid
                                    x:Name="ContentGrid"
                                    Grid.Column="3"
                                    HorizontalAlignment="Stretch">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*" />
                                        <ColumnDefinition Width="Auto" />
                                        <ColumnDefinition Width="Auto" />
                                    </Grid.ColumnDefinitions>
                                    <Grid.ChildrenTransitions>
                                        <TransitionCollection>
                                            <ReorderThemeTransition />
                                        </TransitionCollection>
                                    </Grid.ChildrenTransitions>
                                    <ContentPresenter
                                        x:Name="CustomContentPresenter"
                                        Grid.Column="0"
                                        HorizontalAlignment="Stretch"
                                        Content="{TemplateBinding CustomContent}" />
                                    <ProgressRing
                                        x:Name="ProgressRing"
                                        Grid.Column="1"
                                        Width="20"
                                        Height="20"
                                        Margin="14,0,6,0"
                                        VerticalAlignment="Center"
                                        IsActive="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.IsProgressActive}"
                                        IsIndeterminate="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.IsProgressIndeterminate}"
                                        Visibility="Collapsed"
                                        Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.ProgressValue}" />
                                    <Button
                                        x:Name="RefreshButton"
                                        x:Uid="/TitleBar/RefreshButton"
                                        Grid.Column="2"
                                        Content="&#xE72C;"
                                        IsEnabled="{TemplateBinding IsRefreshEnabled}"
                                        Style="{StaticResource TitleBarIconButtonStyle}" />
                                </Grid>
                            </Grid>
                        </Grid>

                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="ActivationStateGroup">
                                <VisualState x:Name="Activated" />
                                <VisualState x:Name="Deactivated">
                                    <VisualState.Setters>
                                        <Setter Target="TitleText.Foreground" Value="{ThemeResource TitleBarDeactivatedForegroundBrush}" />
                                        <Setter Target="BackButton.Foreground" Value="{ThemeResource TitleBarDeactivatedForegroundBrush}" />
                                    </VisualState.Setters>
                                </VisualState>
                            </VisualStateGroup>

                            <VisualStateGroup x:Name="BackButtonVisibilityGroup">
                                <VisualState x:Name="BackButtonVisible" />
                                <VisualState x:Name="BackButtonCollapsed">
                                    <VisualState.Setters>
                                        <Setter Target="BackButton.Visibility" Value="Collapsed" />
                                        <Setter Target="Icon.Margin" Value="16,0" />
                                    </VisualState.Setters>
                                </VisualState>
                            </VisualStateGroup>

                            <VisualStateGroup x:Name="RefreshButtonVisibilityGroup">
                                <VisualState x:Name="RefreshButtonVisible" />
                                <VisualState x:Name="RefreshButtonCollapsed">
                                    <VisualState.Setters>
                                        <Setter Target="RefreshButton.Visibility" Value="Collapsed" />
                                        <Setter Target="ProgressRing.Margin" Value="14,0" />
                                    </VisualState.Setters>
                                </VisualState>
                            </VisualStateGroup>

                            <VisualStateGroup x:Name="ProgressVisibilityGroup">
                                <VisualState x:Name="ProgressVisible">
                                    <VisualState.Setters>
                                        <Setter Target="ProgressRing.Visibility" Value="Visible" />
                                    </VisualState.Setters>
                                </VisualState>
                                <VisualState x:Name="ProgressCollapsed" />
                            </VisualStateGroup>

                            <VisualStateGroup x:Name="IconVisibilityGroup">
                                <VisualState x:Name="IconVisible" />
                                <VisualState x:Name="IconCollapsed">
                                    <VisualState.Setters>
                                        <Setter Target="Icon.Visibility" Value="Collapsed" />
                                    </VisualState.Setters>
                                </VisualState>
                            </VisualStateGroup>

                            <VisualStateGroup x:Name="TitleTextVisibilityGroup">
                                <VisualState x:Name="TitleTextVisible" />
                                <VisualState x:Name="TitleTextCollapsed">
                                    <VisualState.Setters>
                                        <Setter Target="TitleText.Visibility" Value="Collapsed" />
                                    </VisualState.Setters>
                                </VisualState>
                            </VisualStateGroup>

                            <VisualStateGroup x:Name="FullScreenGroup">
                                <VisualState x:Name="TitleBarVisible" />
                                <VisualState x:Name="TitleBarCollapsed" />
                            </VisualStateGroup>

                            <VisualStateGroup x:Name="HeightGroup">
                                <VisualStateGroup.Transitions>
                                    <VisualTransition From="CompactHeight" To="ExpandedHeight">
                                        <Storyboard>
                                            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="LayoutRoot" Storyboard.TargetProperty="Height">
                                                <LinearDoubleKeyFrame KeyTime="0" Value="{ThemeResource TitleBarCompactHeight}" />
                                                <LinearDoubleKeyFrame KeyTime="0:0:0.1" Value="{ThemeResource TitleBarExpandedHeight}" />
                                            </DoubleAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualTransition>
                                    <VisualTransition From="ExpandedHeight" To="CompactHeight">
                                        <Storyboard>
                                            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="LayoutRoot" Storyboard.TargetProperty="Height">
                                                <LinearDoubleKeyFrame KeyTime="0" Value="{ThemeResource TitleBarExpandedHeight}" />
                                                <LinearDoubleKeyFrame KeyTime="0:0:0.1" Value="{ThemeResource TitleBarCompactHeight}" />
                                            </DoubleAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualTransition>
                                </VisualStateGroup.Transitions>
                                <VisualState x:Name="CompactHeight" />
                                <VisualState x:Name="ExpandedHeight" />
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                    </Grid>

                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>
