<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:controls="clr-namespace:AIStudio.Wpf.Mind.Controls"
                    xmlns:dd="https://gitee.com/akwkevin/aistudio.-wpf.-diagram"
                    xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="pack://application:,,,/AIStudio.Wpf.Mind;component/Controls/ColorCanvas.xaml"/>
    </ResourceDictionary.MergedDictionaries>

    <Geometry x:Key="DownArrowGeometry">M7.41,8.58L12,13.17L16.59,8.58L18,10L12,16L6,10L7.41,8.58Z</Geometry>

    <Style x:Key="ColorPickerToggleButtonStyle" TargetType="ToggleButton">
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ToggleButton">
                    <Grid Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>

                            <ContentPresenter Content="{TemplateBinding Content}"
                                          ContentTemplate="{TemplateBinding ContentTemplate}"
                                          ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" />

                            <Grid x:Name="arrowGlyph" Grid.Column="1">
                                <Path x:Name="Arrow"
                                 Width="9"
                                 Height="6"
                                 Margin="0 0 3 0"
                                 Stretch="Fill"
                                 Data="{StaticResource DownArrowGeometry}" 
                                 Fill="#FF000000"/>
                            </Grid>
                        </Grid>

                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Fill" TargetName="Arrow" Value="#AFAFAF" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <DrawingBrush  x:Key="CheckerBrush" Viewport="0,0,10,10" ViewportUnits="Absolute" TileMode="Tile">
        <DrawingBrush.Drawing>
            <DrawingGroup>
                <GeometryDrawing Brush="White">
                    <GeometryDrawing.Geometry>
                        <RectangleGeometry Rect="0,0 100,100" />
                    </GeometryDrawing.Geometry>
                </GeometryDrawing>
                <GeometryDrawing Brush="LightGray">
                    <GeometryDrawing.Geometry>
                        <GeometryGroup>
                            <RectangleGeometry Rect="0,0 50,50" />
                            <RectangleGeometry Rect="50,50 50,50" />
                        </GeometryGroup>
                    </GeometryDrawing.Geometry>
                </GeometryDrawing>
            </DrawingGroup>
        </DrawingBrush.Drawing>
    </DrawingBrush>

    <!-- =============================================================================== -->
    <!-- ColorPicker                                                                     -->
    <!-- =============================================================================== -->

    <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
    <dd:BoolInverseConverter x:Key="BoolInverseConverter" />
    <dd:ColorBrushConverter x:Key="ColorToSolidColorBrushConverter" />
    <dd:AdditionConverter x:Key="AdditionConverter" />
    <controls:ColorModeToTabItemSelectedConverter x:Key="ColorModeToTabItemSelectedConverter" />
    <dd:GridLengthConverter x:Key="GridLengthConverter"/>

    <Style x:Key="ColorItemContainerStyle" TargetType="{x:Type ListBoxItem}">
        <Setter Property="ToolTip" Value="{Binding Name}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                    <Grid x:Name="mainGrid"
                     ToolTip="{Binding Name}">
                        <Grid.Resources>
                            <Style TargetType="ToolTip">
                                <Style.Triggers>
                                    <Trigger Property="Content"
                                    Value="{x:Static sys:String.Empty}">
                                        <Setter Property="Visibility"
                                      Value="Collapsed" />
                                    </Trigger>
                                </Style.Triggers>
                            </Style>
                        </Grid.Resources>
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                        <Border BorderThickness="1" Background="Transparent" BorderBrush="Transparent" x:Name="_outerBorder" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                            <Border Background="Transparent" BorderThickness="1" BorderBrush="Transparent" x:Name="_innerBorder" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
                        </Border>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="_outerBorder" Property="BorderBrush" Value="#FFFF0000" />
                            <Setter TargetName="_innerBorder" Property="BorderBrush" Value="#FFFFFF00" />
                        </Trigger>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter TargetName="_outerBorder" Property="BorderBrush" Value="#FFFF0000" />
                            <Setter TargetName="_innerBorder" Property="BorderBrush" Value="#FFFFFF00" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <DataTemplate x:Key="ColorItemTemplate">
        <Grid>
            <Border Background="{StaticResource CheckerBrush}" BorderBrush="Black" BorderThickness="1" Margin="2,2,2,2" >
                <Rectangle Width="16" Height="16">
                    <Rectangle.Style>
                        <Style TargetType="Rectangle">
                            <Setter Property="Fill" Value="{Binding Color, Converter={StaticResource ColorToSolidColorBrushConverter}}" />
                        </Style>
                    </Rectangle.Style>
                </Rectangle>
            </Border>
        </Grid>
    </DataTemplate>

    <Style x:Key="ColorDisplayStyle" TargetType="ContentControl">
        <Setter Property="Focusable"
              Value="False" />
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <Border Background="{StaticResource CheckerBrush}">
                        <Rectangle Fill="{Binding SelectedColor, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=controls:ColorPicker}, Converter={StaticResource ColorToSolidColorBrushConverter}}" />
                    </Border>
                </DataTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <DataTrigger Binding="{Binding SelectedColor, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=controls:ColorPicker}}"
                      Value="{x:Null}">
                <Setter Property="Visibility"
                    Value="Collapsed" />
            </DataTrigger>
        </Style.Triggers>
    </Style>

    <Style x:Key="ColorListStyle" TargetType="ListBox">
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="BorderThickness" Value="0" />
        <Setter Property="MaxHeight" Value="500" />
        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <!-- ConverterParameter is margin/Padding from Popup-->
                    <WrapPanel Width="{Binding MaxDropDownWidth, RelativeSource={RelativeSource AncestorType={x:Type controls:ColorPicker}}}" />
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="ItemContainerStyle" Value="{StaticResource ColorItemContainerStyle}" />
        <Setter Property="ItemTemplate" Value="{StaticResource ColorItemTemplate}" />
        <Setter Property="SelectionMode" Value="Single" />
    </Style>

    <ControlTemplate x:Key="TabItemTemplate"
                    TargetType="{x:Type controls:ColorPickerTabItem}">
        <Grid x:Name="templateRoot"
            SnapsToDevicePixels="true">
            <Border x:Name="mainBorder"
                 BorderBrush="{TemplateBinding BorderBrush}"
                 BorderThickness="1,1,1,0"
                 Background="#FFF0F0F0"
                 Margin="0">
                <Border x:Name="innerBorder"
                    BorderBrush="#ACACAC"
                    BorderThickness="1,1,1,0"
                    Background="#FFF0F0F0"
                    Margin="-1"
                    Opacity="0" />
            </Border>
            <ContentPresenter x:Name="contentPresenter"
                           ContentSource="Header"
                           Focusable="False"
                           Margin="{TemplateBinding Padding}"
                           RecognizesAccessKey="True"
                           SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
        </Grid>
        <ControlTemplate.Triggers>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Self}}"
                          Value="true" />
                </MultiDataTrigger.Conditions>
                <Setter Property="Panel.ZIndex"
                    Value="1" />
                <Setter Property="Margin"
                    Value="-2,-2,-2,0" />
                <Setter Property="Opacity"
                    TargetName="innerBorder"
                    Value="1" />
            </MultiDataTrigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <Style x:Key="ColorPickerStyle" TargetType="{x:Type controls:ColorPicker}">
        <Setter Property="Foreground" Value="Black" />
        <Setter Property="Background" Value="White" />
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="BorderThickness" Value="0" />
        <Setter Property="BorderBrush" Value="Gray" />
        <Setter Property="SnapsToDevicePixels" Value="True" />
        <Setter Property="MaxDropDownWidth" Value="218" />
        <Setter Property="ScrollViewer.CanContentScroll" Value="False" />
        <Setter Property="Padding" Value="0" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type controls:ColorPicker}">
                    <Grid x:Name="PART_Root">
                        <Border x:Name="Bg" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                BorderBrush="{TemplateBinding BorderBrush}" 
                                BorderThickness="{TemplateBinding BorderThickness}" 
                                Background="{TemplateBinding Background}"/>

                        <Grid x:Name="PART_InnerGrid" Margin="{TemplateBinding Padding}">
                            <!--下拉按钮-->
                            <ToggleButton x:Name="PART_ColorPickerToggleButton" Panel.ZIndex="1" IsTabStop="False" 
                                        Style="{StaticResource ColorPickerToggleButtonStyle}" 
                                        IsChecked="{Binding IsOpen, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
                                        Foreground="{TemplateBinding BorderBrush}">
                                <!--<ContentControl x:Name="ColorOnly" Style="{StaticResource ColorDisplayStyle}" />-->
                                <ContentControl x:Name="ColorOnly"
                                                              HorizontalAlignment="Center"
                                                              VerticalAlignment="Center"
                                                              HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                              VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                                                              Content="{Binding Content, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"                                                             
                                                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                                              UseLayoutRounding="False" Margin="0,0,0,0" />
                            </ToggleButton>

                        </Grid>

                        <Popup x:Name="PART_ColorPickerPalettePopup" 
                             VerticalAlignment="Bottom" 
                             IsOpen="{Binding ElementName=PART_ColorPickerToggleButton, Path=IsChecked}" 
                             StaysOpen="False" 
                             AllowsTransparency="True" 
                             Focusable="False" 
                             HorizontalOffset="1"
                             VerticalOffset="1" 
                             PopupAnimation="Slide"
                             ToolTip="{x:Static sys:String.Empty}">
                            <Popup.Resources>
                                <Style TargetType="ToolTip">
                                    <Style.Triggers>
                                        <Trigger Property="Content"
                                                Value="{x:Static sys:String.Empty}">
                                            <Setter Property="Visibility"
                                                  Value="Collapsed" />
                                        </Trigger>
                                    </Style.Triggers>
                                </Style>
                            </Popup.Resources>
                            <Border BorderThickness="1" Background="White" BorderBrush="{TemplateBinding BorderBrush}" Padding="3">
                                <TabControl x:Name="ColorPickerTabControl"
                                    Background="Transparent"
                                    SelectedIndex="{Binding ColorMode, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource ColorModeToTabItemSelectedConverter}}">
                                    <controls:ColorPickerTabItem x:Name="StandardTabItem"
                                                     Header="{Binding StandardButtonHeader, RelativeSource={RelativeSource TemplatedParent}}"
                                                     Template="{StaticResource TabItemTemplate}">
                                        <Grid x:Name="_colorPaletteHost" Margin="4">
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="Auto" />
                                                <RowDefinition Height="Auto" />
                                                <RowDefinition Height="Auto" />
                                                <RowDefinition Height="Auto" />
                                            </Grid.RowDefinitions>

                                            <!-- Available Colors -->
                                            <Grid Grid.Row="1" Visibility="{TemplateBinding ShowAvailableColors, Converter={StaticResource BooleanToVisibilityConverter}}">
                                                <Grid>
                                                    <Grid.RowDefinitions>
                                                        <RowDefinition Height="Auto" />
                                                        <RowDefinition />
                                                    </Grid.RowDefinitions>
                                                    <TextBlock Text="{TemplateBinding AvailableColorsHeader}" Background="AliceBlue" Padding="2" Margin="0,0,0,1" />
                                                    <ListBox x:Name="PART_AvailableColors"
                                             Grid.Row="1"
                                             ItemsSource="{Binding AvailableColors, RelativeSource={RelativeSource TemplatedParent}}"
                                             Style="{StaticResource ColorListStyle}" />
                                                </Grid>
                                            </Grid>

                                            <!-- Standard Colors-->
                                            <Grid Grid.Row="2" Visibility="{TemplateBinding ShowStandardColors, Converter={StaticResource BooleanToVisibilityConverter}}">
                                                <Grid>
                                                    <Grid.RowDefinitions>
                                                        <RowDefinition Height="Auto" />
                                                        <RowDefinition Height="Auto" />
                                                    </Grid.RowDefinitions>
                                                    <TextBlock Text="{TemplateBinding StandardColorsHeader}" Background="AliceBlue" Padding="2" Margin="0,1,0,1" />
                                                    <ListBox x:Name="PART_StandardColors"
                                             Grid.Row="1"
                                             ItemsSource="{Binding StandardColors, RelativeSource={RelativeSource TemplatedParent}}"                                                      
                                             Style="{StaticResource ColorListStyle}" />
                                                </Grid>
                                            </Grid>

                                            <!-- Recent Colors-->
                                            <Grid Grid.Row="3" Margin="0,1,0,1" Visibility="{TemplateBinding ShowRecentColors, Converter={StaticResource BooleanToVisibilityConverter}}">
                                                <Grid>
                                                    <Grid.RowDefinitions>
                                                        <RowDefinition Height="Auto" />
                                                        <RowDefinition Height="22" />
                                                    </Grid.RowDefinitions>
                                                    <TextBlock Text="{TemplateBinding RecentColorsHeader}" Background="AliceBlue" Padding="2" Margin="0,1,0,1" />
                                                    <ListBox x:Name="PART_RecentColors"
                                             Grid.Row="1"
                                             ItemsSource="{Binding RecentColors, RelativeSource={RelativeSource TemplatedParent}}"
                                             Style="{StaticResource ColorListStyle}" />
                                                </Grid>
                                            </Grid>
                                        </Grid>
                                    </controls:ColorPickerTabItem>
                                    <controls:ColorPickerTabItem x:Name="AdvancedTabItem"
                                                     Header="{Binding AdvancedButtonHeader, RelativeSource={RelativeSource TemplatedParent}}"
                                                     Template="{StaticResource TabItemTemplate}">
                                        <!-- ColorCanvas -->
                                        <Grid x:Name="_colorCanvasHost">
                                            <controls:ColorCanvas Background="Transparent"
                                                    BorderThickness="0"
                                                    UsingAlphaChannel="{Binding UsingAlphaChannel, RelativeSource={RelativeSource TemplatedParent}}"
                                                    SelectedColor="{Binding SelectedColor, RelativeSource={RelativeSource TemplatedParent}}"
                                                    Width="{Binding MaxDropDownWidth, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource AdditionConverter}, ConverterParameter=-18}" />
                                        </Grid>
                                    </controls:ColorPickerTabItem>
                                </TabControl>
                            </Border>
                        </Popup>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="ShowTabHeaders"
                           Value="False">
                            <Setter Property="Visibility"
                             Value="Collapsed"
                             TargetName="StandardTabItem" />
                            <Setter Property="Visibility"
                             Value="Collapsed"
                             TargetName="AdvancedTabItem" />
                        </Trigger>

                        <Trigger Property="ShowDropDownButton" Value="False">
                            <Setter Property="BorderThickness" Value="1" />
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="BorderBrush" Value="Gray"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter TargetName="PART_Root" Property="Opacity" Value="0.5"></Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style BasedOn="{StaticResource ColorPickerStyle}" TargetType="{x:Type controls:ColorPicker}"/>

</ResourceDictionary>
