<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:bpa="clr-namespace:BPA.UIControl">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="pack://application:,,,/BPA.UIControl;component/Themes/ScrollViewer.xaml" />
    </ResourceDictionary.MergedDictionaries>

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

    <!--  Separator  -->
    <Style x:Key="{x:Static MenuItem.SeparatorStyleKey}" TargetType="{x:Type Separator}">
        <Setter Property="Margin" Value="{Binding Path=(bpa:ItemsControlHelper.ItemPadding), RelativeSource={RelativeSource AncestorType=MenuBase}}" />
        <Setter Property="BorderBrush" Value="{DynamicResource Border}" />
        <Setter Property="BorderThickness" Value="1 1 0 0" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Separator}">
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <!--  TopLevelHeader  -->
    <ControlTemplate x:Key="{x:Static MenuItem.TopLevelHeaderTemplateKey}" TargetType="{x:Type MenuItem}">
        <Border Background="{Binding Path=(bpa:HeaderHelper.Background), RelativeSource={RelativeSource AncestorType=MenuBase}}"
                BorderBrush="{TemplateBinding BorderBrush}"
                BorderThickness="{TemplateBinding BorderThickness}"
                CornerRadius="{Binding Path=(bpa:ControlHelper.CornerRadius), RelativeSource={RelativeSource Mode=TemplatedParent}}">
            <Grid>
                <bpa:ControlMask Background="{Binding Path=(bpa:ControlHelper.FocusedBrush), RelativeSource={RelativeSource Mode=TemplatedParent}}"
                                 CornerRadius="{Binding Path=(bpa:ControlHelper.CornerRadius), RelativeSource={RelativeSource Mode=TemplatedParent}}"
                                 IsActive="{TemplateBinding IsHighlighted}"
                                 MaskOpacity="{Binding Path=(bpa:ControlHelper.MaskOpacity), RelativeSource={RelativeSource Mode=TemplatedParent}}" />

                <ContentPresenter Margin="{TemplateBinding Padding}"
                                  ContentSource="Header"
                                  RecognizesAccessKey="True"
                                  TextBlock.Foreground="{Binding Path=(bpa:HeaderHelper.Foreground), RelativeSource={RelativeSource AncestorType=MenuBase}}" />

                <Popup x:Name="Popup"
                       AllowsTransparency="True"
                       Focusable="False"
                       HorizontalOffset="-5"
                       IsOpen="{TemplateBinding IsSubmenuOpen}"
                       Placement="Bottom"
                       PopupAnimation="Fade">
                    <Grid>
                        <Border Margin="5"
                                Padding="0 5"
                                Background="{DynamicResource FloatBackground}"
                                CornerRadius="{Binding Path=(bpa:ControlHelper.CornerRadius), RelativeSource={RelativeSource Mode=TemplatedParent}}"
                                Effect="{DynamicResource AllDirectionEffect}" />
                        <Border x:Name="SubmenuBorder"
                                Margin="5"
                                Padding="0 5"
                                Background="{DynamicResource FloatBackground}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                CornerRadius="{Binding Path=(bpa:ControlHelper.CornerRadius), RelativeSource={RelativeSource Mode=TemplatedParent}}"
                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
                            <ScrollViewer Style="{StaticResource MenuScrollViewer}" CanContentScroll="True">
                                <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle" />
                            </ScrollViewer>
                        </Border>
                    </Grid>
                </Popup>
            </Grid>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsSuspendingPopupAnimation" Value="True">
                <Setter TargetName="Popup" Property="PopupAnimation" Value="None" />
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Opacity" Value="{DynamicResource UnenableOpcity}" />
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <!--  TopLevelItem  -->
    <ControlTemplate x:Key="{x:Static MenuItem.TopLevelItemTemplateKey}" TargetType="{x:Type MenuItem}">
        <Border Background="{Binding Path=(bpa:HeaderHelper.Background), RelativeSource={RelativeSource AncestorType=MenuBase}}"
                BorderBrush="{TemplateBinding BorderBrush}"
                BorderThickness="{TemplateBinding BorderThickness}"
                CornerRadius="{Binding Path=(bpa:ControlHelper.CornerRadius), RelativeSource={RelativeSource Mode=TemplatedParent}}">
            <Grid>
                <bpa:ControlMask Background="{Binding Path=(bpa:ControlHelper.FocusedBrush), RelativeSource={RelativeSource Mode=TemplatedParent}}"
                                 CornerRadius="{Binding Path=(bpa:ControlHelper.CornerRadius), RelativeSource={RelativeSource Mode=TemplatedParent}}"
                                 IsActive="{TemplateBinding IsHighlighted}"
                                 MaskOpacity="{Binding Path=(bpa:ControlHelper.MaskOpacity), RelativeSource={RelativeSource Mode=TemplatedParent}}" />
                <ContentPresenter Margin="{TemplateBinding Padding}"
                                  ContentSource="Header"
                                  RecognizesAccessKey="True"
                                  TextBlock.Foreground="{Binding Path=(bpa:HeaderHelper.Foreground), RelativeSource={RelativeSource AncestorType=MenuBase}}" />
            </Grid>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Opacity" Value="{DynamicResource UnenableOpcity}" />
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <!--  SubmenuHeade  -->
    <ControlTemplate x:Key="{x:Static MenuItem.SubmenuHeaderTemplateKey}" TargetType="{x:Type MenuItem}">
        <Grid Background="Transparent">
            <bpa:ControlMask Background="{Binding Path=(bpa:ControlHelper.FocusedBrush), RelativeSource={RelativeSource Mode=TemplatedParent}}"
                             CornerRadius="{Binding Path=(bpa:ControlHelper.CornerRadius), RelativeSource={RelativeSource Mode=TemplatedParent}}"
                             IsActive="{TemplateBinding IsHighlighted}"
                             MaskOpacity="{Binding Path=(bpa:ControlHelper.MaskOpacity), RelativeSource={RelativeSource Mode=TemplatedParent}}" />

            <Border Padding="{TemplateBinding Padding}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="{Binding Path=(bpa:MenuHelper.IconWidth), RelativeSource={RelativeSource Mode=TemplatedParent}}" SharedSizeGroup="Icon" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" SharedSizeGroup="Shortcut" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>

                    <ContentPresenter x:Name="Icon"
                                      HorizontalAlignment="Left"
                                      VerticalAlignment="Center"
                                      ContentSource="Icon" />

                    <ContentPresenter x:Name="HeaderHost"
                                      Grid.Column="1"
                                      VerticalAlignment="Center"
                                      ContentSource="Header"
                                      RecognizesAccessKey="True" />

                    <TextBlock x:Name="InputGestureText"
                               Grid.Column="2"
                               Margin="5 0 0 0"
                               HorizontalAlignment="Right"
                               VerticalAlignment="Center"
                               Text="{TemplateBinding InputGestureText}" />

                    <bpa:Icon Grid.Column="3"
                              Margin="20 0 0 0"
                              HorizontalAlignment="Center"
                              VerticalAlignment="Center"
                              Type="ArrowRightSLine" />

                    <Popup x:Name="Popup"
                           AllowsTransparency="True"
                           Focusable="False"
                           HorizontalOffset="5"
                           IsOpen="{TemplateBinding IsSubmenuOpen}"
                           Placement="Right"
                           PopupAnimation="Fade">
                        <Grid>
                            <Border Margin="5"
                                    Padding="0 5"
                                    Background="{DynamicResource FloatBackground}"
                                    CornerRadius="{Binding Path=(bpa:ControlHelper.CornerRadius), RelativeSource={RelativeSource Mode=TemplatedParent}}"
                                    Effect="{DynamicResource AllDirectionEffect}" />
                            <Border x:Name="SubmenuBorder"
                                    Margin="5"
                                    Padding="0 5"
                                    Background="{DynamicResource FloatBackground}"
                                    BorderBrush="{TemplateBinding BorderBrush}"
                                    BorderThickness="{TemplateBinding BorderThickness}"
                                    CornerRadius="{Binding Path=(bpa:ControlHelper.CornerRadius), RelativeSource={RelativeSource Mode=TemplatedParent}}"
                                    Effect="{DynamicResource AllDirectionEffect}"
                                    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
                                <ScrollViewer Style="{StaticResource MenuScrollViewer}" CanContentScroll="True">
                                    <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle" />
                                </ScrollViewer>
                            </Border>
                        </Grid>
                    </Popup>
                </Grid>
            </Border>
        </Grid>

        <ControlTemplate.Triggers>
            <Trigger Property="Icon" Value="{x:Null}">
                <Setter TargetName="Icon" Property="Visibility" Value="Hidden" />
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Opacity" Value="{DynamicResource UnenableOpcity}" />
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <!--  SubmenuItem  -->
    <ControlTemplate x:Key="{x:Static MenuItem.SubmenuItemTemplateKey}" TargetType="{x:Type MenuItem}">
        <Grid Background="Transparent">
            <bpa:ControlMask Background="{Binding Path=(bpa:ControlHelper.FocusedBrush), RelativeSource={RelativeSource Mode=TemplatedParent}}"
                             CornerRadius="{Binding Path=(bpa:ControlHelper.CornerRadius), RelativeSource={RelativeSource Mode=TemplatedParent}}"
                             IsActive="{TemplateBinding IsHighlighted}"
                             MaskOpacity="{Binding Path=(bpa:ControlHelper.MaskOpacity), RelativeSource={RelativeSource Mode=TemplatedParent}}" />

            <Border Padding="{TemplateBinding Padding}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="{Binding Path=(bpa:MenuHelper.IconWidth), RelativeSource={RelativeSource Mode=TemplatedParent}}" SharedSizeGroup="Icon" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" SharedSizeGroup="Shortcut" />
                        <ColumnDefinition Width="{Binding Path=(bpa:MenuHelper.IconWidth), RelativeSource={RelativeSource Mode=TemplatedParent}}" />
                    </Grid.ColumnDefinitions>

                    <ContentPresenter x:Name="Icon"
                                      HorizontalAlignment="Left"
                                      VerticalAlignment="Center"
                                      ContentSource="Icon" />

                    <bpa:Icon x:Name="Check"
                              HorizontalAlignment="Left"
                              VerticalAlignment="Center"
                              Type="CheckFill"
                              Visibility="Hidden" />

                    <ContentPresenter x:Name="HeaderHost"
                                      Grid.Column="1"
                                      HorizontalAlignment="Left"
                                      VerticalAlignment="Center"
                                      ContentSource="Header"
                                      RecognizesAccessKey="True" />

                    <TextBlock x:Name="InputGestureText"
                               Grid.Column="2"
                               Margin="5 0 0 0"
                               DockPanel.Dock="Right"
                               Text="{TemplateBinding InputGestureText}" />
                </Grid>
            </Border>
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="Icon" Value="{x:Null}">
                <Setter TargetName="Icon" Property="Visibility" Value="Hidden" />
            </Trigger>
            <Trigger Property="IsChecked" Value="True">
                <Setter TargetName="Check" Property="Visibility" Value="Visible" />
            </Trigger>
            <Trigger Property="IsCheckable" Value="True">
                <Setter TargetName="Icon" Property="Visibility" Value="Hidden" />
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Opacity" Value="{DynamicResource UnenableOpcity}" />
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <!--  MenuItem Style  -->
    <Style x:Key="RubyerMenuItem" TargetType="{x:Type MenuItem}">
        <Setter Property="OverridesDefaultStyle" Value="True" />
        <Setter Property="Background" Value="{Binding Background, RelativeSource={RelativeSource AncestorType=MenuBase}}" />
        <Setter Property="BorderBrush" Value="{Binding BorderBrush, RelativeSource={RelativeSource AncestorType=MenuBase}}" />
        <Setter Property="BorderThickness" Value="{Binding BorderThickness, RelativeSource={RelativeSource AncestorType=MenuBase}}" />
        <Setter Property="bpa:ControlHelper.CornerRadius" Value="{Binding Path=(bpa:ControlHelper.CornerRadius), RelativeSource={RelativeSource AncestorType=MenuBase}}" />
        <Setter Property="bpa:ControlHelper.MaskOpacity" Value="{Binding Path=(bpa:ControlHelper.MaskOpacity), RelativeSource={RelativeSource AncestorType=MenuBase}}" />
        <Setter Property="bpa:ControlHelper.FocusedBrush" Value="{Binding Path=(bpa:ControlHelper.FocusedBrush), RelativeSource={RelativeSource AncestorType=MenuBase}}" />
        <Setter Property="Padding" Value="{Binding Path=(bpa:ItemsControlHelper.ItemPadding), RelativeSource={RelativeSource AncestorType=MenuBase}}" />
        <Setter Property="Margin" Value="{Binding Path=(bpa:ItemsControlHelper.ItemMargin), RelativeSource={RelativeSource AncestorType=MenuBase}}" />
        <Setter Property="bpa:MenuHelper.IconWidth" Value="{Binding Path=(bpa:MenuHelper.IconWidth), RelativeSource={RelativeSource AncestorType=MenuBase}}" />
        <Style.Triggers>
            <Trigger Property="Role" Value="TopLevelHeader">
                <Setter Property="Grid.IsSharedSizeScope" Value="True" />
                <Setter Property="Template" Value="{StaticResource {x:Static MenuItem.TopLevelHeaderTemplateKey}}" />
                <Setter Property="Margin" Value="0" />
            </Trigger>
            <Trigger Property="Role" Value="TopLevelItem">
                <Setter Property="Template" Value="{StaticResource {x:Static MenuItem.TopLevelItemTemplateKey}}" />
                <Setter Property="Margin" Value="0" />
            </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 x:Key="RubyerMenu" TargetType="{x:Type Menu}">
        <Setter Property="Background" Value="{DynamicResource DefaultBackground}" />
        <Setter Property="BorderThickness" Value="0" />
        <Setter Property="BorderBrush" Value="{DynamicResource Border}" />
        <Setter Property="OverridesDefaultStyle" Value="True" />
        <Setter Property="SnapsToDevicePixels" Value="True" />
        <Setter Property="bpa:ControlHelper.CornerRadius" Value="{DynamicResource AllControlCornerRadius}" />
        <Setter Property="bpa:ControlHelper.FocusedBrush" Value="{DynamicResource Mask}" />
        <Setter Property="bpa:ItemsControlHelper.ItemMargin" Value="5 1" />
        <Setter Property="bpa:ItemsControlHelper.ItemPadding" Value="10 5" />
        <Setter Property="bpa:HeaderHelper.Background" Value="Transparent" />
        <Setter Property="bpa:HeaderHelper.Foreground" Value="{DynamicResource DefaultForeground}" />
        <Setter Property="bpa:MenuHelper.IconWidth" Value="24" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Menu}">
                    <Border Background="Transparent" CornerRadius="{DynamicResource AllControlCornerRadius}">
                        <StackPanel ClipToBounds="True"
                                    IsItemsHost="True"
                                    Orientation="Horizontal" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>