<!--
    This Source Code Form is subject to the terms of the MIT License.
    If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
    Copyright (C) Leszek Pomianowski and WPF UI Contributors.
    All Rights Reserved.
-->

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:system="clr-namespace:System;assembly=System.Runtime">

    <system:String x:Key="MenuItemChevronRightGlyph">&#xE76C;</system:String>
    <system:String x:Key="MenuItemCheckedGlyph">&#xE73E;</system:String>

    <Style x:Key="MenuItemScrollViewerStyle"
        BasedOn="{StaticResource {x:Type ScrollViewer}}"
        TargetType="{x:Type ScrollViewer}">
        <Setter Property="HorizontalScrollBarVisibility" Value="Disabled" />
        <Setter Property="VerticalScrollBarVisibility" Value="Auto" />
    </Style>

    <Style x:Key="{x:Static MenuItem.SeparatorStyleKey}" TargetType="{x:Type Separator}">
        <Setter Property="BorderBrush" Value="{DynamicResource MenuBarItemBorderBrush}" />
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="Margin" Value="0,1,0,1" />
        <Setter Property="BorderThickness" Value="1,1,0,0" />
        <Setter Property="SnapsToDevicePixels" Value="True" />
        <Setter Property="OverridesDefaultStyle" Value="True" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Separator}">
                    <Border
                        Width="{TemplateBinding Width}"
                        Margin="{TemplateBinding Margin}"
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <!--  TopLevelHeader  -->
    <ControlTemplate x:Key="{x:Static MenuItem.TopLevelHeaderTemplateKey}" TargetType="{x:Type MenuItem}">
        <Border
            x:Name="Border"
            Margin="4"
            BorderThickness="{TemplateBinding BorderThickness}"
            Background="{TemplateBinding Background}"
            BorderBrush="{TemplateBinding BorderBrush}"
            CornerRadius="6">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>

                <Grid Margin="10">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <ContentPresenter
                        x:Name="Icon"
                        Grid.Column="0"
                        Margin="0,0,6,0"
                        VerticalAlignment="Center"
                        SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                        Content="{TemplateBinding Icon}" />
                    <ContentPresenter
                        x:Name="HeaderPresenter"
                        Grid.Column="1"
                        VerticalAlignment="Center"
                        ContentSource="Header"
                        RecognizesAccessKey="True"
                        Margin="{TemplateBinding Padding}"
                        SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                        TextElement.Foreground="{TemplateBinding Foreground}" />
                </Grid>

                <Popup
                    x:Name="PART_Popup"
                    Grid.Row="1"
                    Grid.Column="0"
                    AllowsTransparency="True"
                    Focusable="False"
                    HorizontalOffset="-12"
                    IsOpen="{TemplateBinding IsSubmenuOpen}"
                    Placement="Bottom"
                    PlacementTarget="{Binding ElementName=Border}"
                    PopupAnimation="None"
                    VerticalOffset="1">
                    <Grid>
                        <Border
                            x:Name="SubmenuBorder"
                            Margin="12,0,12,18"
                            Padding="0,3,0,3"
                            Background="{DynamicResource FlyoutBackground}"
                            BorderBrush="{DynamicResource FlyoutBorderBrush}"
                            BorderThickness="1"
                            CornerRadius="8"
                            SnapsToDevicePixels="True">
                            <Border.RenderTransform>
                                <TranslateTransform />
                            </Border.RenderTransform>
                            <ScrollViewer CanContentScroll="True" Style="{StaticResource MenuItemScrollViewerStyle}">
                                <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle" />
                            </ScrollViewer>
                            <Border.Effect>
                                <DropShadowEffect
                                    BlurRadius="20"
                                    Direction="270"
                                    Opacity="0.25"
                                    ShadowDepth="6" />
                            </Border.Effect>
                        </Border>
                    </Grid>
                </Popup>
            </Grid>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="Icon" Value="{x:Null}">
                <Setter TargetName="Icon" Property="Visibility" Value="Collapsed" />
            </Trigger>
            <Trigger Property="Header" Value="{x:Null}">
                <Setter TargetName="Icon" Property="Margin" Value="0" />
                <Setter TargetName="HeaderPresenter" Property="Visibility" Value="Collapsed" />
            </Trigger>
            <Trigger Property="IsHighlighted" Value="True">
                <Setter TargetName="Border" Property="Background" Value="{DynamicResource MenuBarItemBackgroundSelected}" />
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Foreground">
                    <Setter.Value>
                        <SolidColorBrush Color="{DynamicResource TextFillColorDisabled}" />
                    </Setter.Value>
                </Setter>
            </Trigger>
            <Trigger Property="IsSubmenuOpen" Value="True">
                <Trigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation
                                Storyboard.TargetName="SubmenuBorder"
                                Storyboard.TargetProperty="(Border.RenderTransform).(TranslateTransform.Y)"
                                From="-90"
                                To="0"
                                Duration="00:00:00.167">
                                <DoubleAnimation.EasingFunction>
                                    <CircleEase EasingMode="EaseOut" />
                                </DoubleAnimation.EasingFunction>
                            </DoubleAnimation>
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.EnterActions>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <!--  TopLevelItem  -->
    <ControlTemplate x:Key="{x:Static MenuItem.TopLevelItemTemplateKey}" TargetType="{x:Type MenuItem}">
        <Border
            x:Name="Border"
            Margin="4"
            BorderThickness="{TemplateBinding BorderThickness}"
            Background="{TemplateBinding Background}"
            BorderBrush="{TemplateBinding BorderBrush}"
            CornerRadius="6">
            <Grid Margin="10">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <ContentPresenter
                    x:Name="Icon"
                    Grid.Column="0"
                    Margin="0,0,6,0"
                    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                    VerticalAlignment="Center"
                    Content="{TemplateBinding Icon}" />
                <ContentPresenter
                    x:Name="HeaderPresenter"
                    Grid.Column="1"
                    VerticalAlignment="Center"
                    ContentSource="Header"
                    Margin="{TemplateBinding Padding}"
                    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                    RecognizesAccessKey="True"
                    TextElement.Foreground="{TemplateBinding Foreground}" />
            </Grid>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsHighlighted" Value="True">
                <Setter TargetName="Border" Property="Background" Value="{DynamicResource MenuBarItemBackgroundSelected}" />
            </Trigger>
            <Trigger Property="Icon" Value="{x:Null}">
                <Setter TargetName="Icon" Property="Visibility" Value="Collapsed" />
            </Trigger>
            <Trigger Property="Header" Value="{x:Null}">
                <Setter TargetName="Icon" Property="Margin" Value="0" />
                <Setter TargetName="HeaderPresenter" Property="Visibility" Value="Collapsed" />
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Foreground">
                    <Setter.Value>
                        <SolidColorBrush Color="{DynamicResource TextFillColorDisabled}" />
                    </Setter.Value>
                </Setter>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <!--  SubmenuItem  -->
    <ControlTemplate x:Key="{x:Static MenuItem.SubmenuItemTemplateKey}" TargetType="{x:Type MenuItem}">
        <Border
            x:Name="Border"
            Margin="4,1,4,1"
            BorderThickness="{TemplateBinding BorderThickness}"
            Background="{TemplateBinding Background}"
            BorderBrush="{TemplateBinding BorderBrush}"
            CornerRadius="4">
            <Grid Margin="8,6,8,6">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="Auto" SharedSizeGroup="Shortcut" />
                </Grid.ColumnDefinitions>

                <Border
                    x:Name="CheckBoxIconBorder"
                    Grid.Column="0"
                    Width="20"
                    Height="20"
                    Margin="0,0,6,0"
                    VerticalAlignment="Center"
                    Background="{DynamicResource CheckBoxBackground}"
                    BorderBrush="{DynamicResource CheckBoxBorderBrush}"
                    BorderThickness="1"
                    CornerRadius="4"
                    Visibility="Collapsed">
                    <TextBlock
                        x:Name="CheckBoxIcon"
                        HorizontalAlignment="Center"
                        VerticalAlignment="Center"
                        FontFamily="{DynamicResource SymbolThemeFontFamily}"
                        FontSize="16"
                        Text=""
                        TextAlignment="Center" />
                </Border>

                <ContentPresenter
                    x:Name="Icon"
                    Grid.Column="1"
                    Margin="0,0,6,0"
                    VerticalAlignment="Center"
                    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                    Content="{TemplateBinding Icon}" />

                <ContentPresenter
                    Grid.Column="2"
                    ContentSource="Header"
                    RecognizesAccessKey="True"
                    Margin="{TemplateBinding Padding}"
                    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                    TextElement.Foreground="{TemplateBinding Foreground}" />

                <TextBlock
                    x:Name="InputGestureText"
                    Grid.Column="3"
                    Margin="25,0,0,0"
                    VerticalAlignment="Bottom"
                    DockPanel.Dock="Right"
                    FontSize="11"
                    Foreground="{DynamicResource TextFillColorDisabledBrush}"
                    Text="{TemplateBinding InputGestureText}" />
            </Grid>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsHighlighted" Value="True">
                <Setter TargetName="Border" Property="Background" Value="{DynamicResource MenuBarItemBackgroundSelected}" />
            </Trigger>
            <Trigger Property="Icon" Value="{x:Null}">
                <Setter TargetName="Icon" Property="Visibility" Value="Collapsed" />
            </Trigger>
            <Trigger Property="IsCheckable" Value="True">
                <Setter TargetName="CheckBoxIconBorder" Property="Visibility" Value="Visible" />
            </Trigger>
            <Trigger Property="IsChecked" Value="True">
                <Setter TargetName="CheckBoxIcon" Property="Text" Value="{StaticResource MenuItemCheckedGlyph}" />
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Foreground">
                    <Setter.Value>
                        <SolidColorBrush Color="{DynamicResource TextFillColorDisabled}" />
                    </Setter.Value>
                </Setter>
            </Trigger>
            <Trigger Property="InputGestureText" Value="">
                <Setter TargetName="InputGestureText" Property="Visibility" Value="Collapsed" />
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <!--  SubItem with Subitems  -->
    <ControlTemplate x:Key="{x:Static MenuItem.SubmenuHeaderTemplateKey}" TargetType="{x:Type MenuItem}">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <Border
                x:Name="Border"
                Grid.Row="1"
                Margin="4,1,4,1"
                BorderThickness="{TemplateBinding BorderThickness}"
                Background="{TemplateBinding Background}"
                BorderBrush="{TemplateBinding BorderBrush}"
                CornerRadius="4">
                <Grid x:Name="MenuItemContent" Margin="8,6,8,6">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <ContentPresenter
                        x:Name="Icon"
                        Grid.Column="0"
                        Margin="0,0,6,0"
                        VerticalAlignment="Center"
                        SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                        Content="{TemplateBinding Icon}" />

                    <ContentPresenter
                        x:Name="HeaderHost"
                        Grid.Column="1"
                        SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                        Margin="{TemplateBinding Padding}"
                        ContentSource="Header"
                        RecognizesAccessKey="True" />

                    <Grid Grid.Column="2">
                        <TextBlock
                            x:Name="Chevron"
                            Margin="0,3,0,0"
                            VerticalAlignment="Center"
                            FontFamily="{DynamicResource SymbolThemeFontFamily}"
                            FontSize="{TemplateBinding FontSize}"
                            Text="{StaticResource MenuItemChevronRightGlyph}" />
                    </Grid>
                </Grid>
            </Border>

            <Popup
                x:Name="PART_Popup"
                Grid.Row="1"
                AllowsTransparency="True"
                Focusable="False"
                IsOpen="{TemplateBinding IsSubmenuOpen}"
                Placement="Right"
                PlacementTarget="{Binding ElementName=MenuItemContent}"
                PopupAnimation="None"
                VerticalOffset="-20">
                <Grid>
                    <Border
                        x:Name="SubmenuBorder"
                        Margin="12,10,12,18"
                        Padding="0,3,0,3"
                        Background="{DynamicResource FlyoutBackground}"
                        BorderBrush="{DynamicResource FlyoutBorderBrush}"
                        BorderThickness="1"
                        CornerRadius="8"
                        SnapsToDevicePixels="True">
                        <Border.RenderTransform>
                            <TranslateTransform />
                        </Border.RenderTransform>
                        <ScrollViewer CanContentScroll="True" Style="{StaticResource MenuItemScrollViewerStyle}">
                            <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle" />
                        </ScrollViewer>
                        <Border.Effect>
                            <DropShadowEffect
                                BlurRadius="20"
                                Direction="270"
                                Opacity="0.5"
                                ShadowDepth="6" />
                        </Border.Effect>
                    </Border>
                </Grid>
            </Popup>
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="Icon" Value="{x:Null}">
                <Setter TargetName="Icon" Property="Visibility" Value="Collapsed" />
                <Setter TargetName="Icon" Property="Margin" Value="0" />
            </Trigger>
            <Trigger Property="IsHighlighted" Value="true">
                <Setter TargetName="Border" Property="Background" Value="{DynamicResource MenuBarItemBackgroundSelected}" />
            </Trigger>
            <!--<Trigger SourceName="Popup" Property="AllowsTransparency" Value="True">
                <Setter TargetName="SubmenuBorder" Property="CornerRadius" Value="4" />
                <Setter TargetName="SubmenuBorder" Property="Padding" Value="0,3,0,3" />
            </Trigger>-->
            <Trigger Property="IsEnabled" Value="false">
                <Setter Property="Foreground">
                    <Setter.Value>
                        <SolidColorBrush Color="{DynamicResource TextFillColorDisabled}" />
                    </Setter.Value>
                </Setter>
                <Setter TargetName="Chevron" Property="Foreground">
                    <Setter.Value>
                        <SolidColorBrush Color="{DynamicResource TextFillColorDisabled}" />
                    </Setter.Value>
                </Setter>
            </Trigger>
            <Trigger Property="IsSubmenuOpen" Value="True">
                <Trigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation
                                Storyboard.TargetName="SubmenuBorder"
                                Storyboard.TargetProperty="(Border.RenderTransform).(TranslateTransform.Y)"
                                From="-90"
                                To="0"
                                Duration="00:00:00.167">
                                <DoubleAnimation.EasingFunction>
                                    <CircleEase EasingMode="EaseOut" />
                                </DoubleAnimation.EasingFunction>
                            </DoubleAnimation>
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.EnterActions>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <Style x:Key="DefaultMenuItemStyle" TargetType="{x:Type MenuItem}">
        <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
        <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
        <Setter Property="FocusVisualStyle" Value="{DynamicResource DefaultCollectionFocusVisualStyle}" />
        <Setter Property="KeyboardNavigation.IsTabStop" Value="True" />
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="BorderBrush" Value="Transparent" />
        <Setter Property="BorderThickness" Value="1" />
        <Setter Property="Focusable" Value="True" />
        <Setter Property="OverridesDefaultStyle" Value="True" />
        <Style.Triggers>
            <Trigger Property="Role" Value="TopLevelHeader">
                <Setter Property="Template" Value="{StaticResource {x:Static MenuItem.TopLevelHeaderTemplateKey}}" />
                <Setter Property="Grid.IsSharedSizeScope" Value="True" />
            </Trigger>
            <Trigger Property="Role" Value="TopLevelItem">
                <Setter Property="Template" Value="{StaticResource {x:Static MenuItem.TopLevelItemTemplateKey}}" />
            </Trigger>
            <Trigger Property="Role" Value="SubmenuHeader">
                <Setter Property="Template" Value="{StaticResource {x:Static MenuItem.SubmenuHeaderTemplateKey}}" />
            </Trigger>
            <Trigger Property="Role" Value="SubmenuItem">
                <Setter Property="Template" Value="{StaticResource {x:Static MenuItem.SubmenuItemTemplateKey}}" />
            </Trigger>
        </Style.Triggers>
    </Style>

    <Style BasedOn="{StaticResource DefaultMenuItemStyle}" TargetType="{x:Type MenuItem}" />

</ResourceDictionary>
