<!--
    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.
    
    Based on Microsoft XAML for Win UI
    Copyright (c) Microsoft Corporation. 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">

    <Thickness x:Key="TextBoxBorderThemeThickness">1,1,1,1</Thickness>
    <Thickness x:Key="TextBoxAccentBorderThemeThickness">0,0,0,1</Thickness>
    <Thickness x:Key="TextBoxLeftIconMargin">10,0,0,0</Thickness>
    <Thickness x:Key="TextBoxRightIconMargin">0,0,10,0</Thickness>
    <Thickness x:Key="TextBoxClearButtonMargin">0,0,4,0</Thickness>
    <Thickness x:Key="TextBoxClearButtonPadding">0,0,0,0</Thickness>
    <system:Double x:Key="TextBoxClearButtonHeight">24</system:Double>
    <system:String x:Key="ClearGlyph">&#xE894;</system:String>

    <Style x:Key="DefaultTextBoxBaseStyle" TargetType="{x:Type TextBoxBase}">
        <!--  Universal WPF UI focus  -->
        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
        <!--  Universal WPF UI ContextMenu  -->
        <Setter Property="ContextMenu" Value="{DynamicResource DefaultControlContextMenu}" />
        <Setter Property="Foreground" Value="{DynamicResource TextControlForeground}" />
        <Setter Property="CaretBrush" Value="{DynamicResource TextControlForeground}" />
        <Setter Property="Background" Value="{DynamicResource TextControlBackground}" />
        <Setter Property="BorderBrush" Value="{DynamicResource TextControlElevationBorderBrush}" />
        <Setter Property="BorderThickness" Value="{StaticResource TextBoxBorderThemeThickness}" />
        <Setter Property="ScrollViewer.CanContentScroll" Value="False" />
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Hidden" />
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Hidden" />
        <Setter Property="ScrollViewer.IsDeferredScrollingEnabled" Value="False" />
        <Setter Property="HorizontalAlignment" Value="Stretch" />
        <Setter Property="VerticalAlignment" Value="Stretch" />
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        <Setter Property="VerticalContentAlignment" Value="Top" />
        <Setter Property="MinHeight" Value="{DynamicResource TextControlThemeMinHeight}" />
        <Setter Property="MinWidth" Value="{DynamicResource TextControlThemeMinWidth}" />
        <Setter Property="Padding" Value="{DynamicResource TextControlThemePadding}" />
        <Setter Property="Border.CornerRadius" Value="{DynamicResource ControlCornerRadius}" />
        <Setter Property="SnapsToDevicePixels" Value="True" />
        <Setter Property="OverridesDefaultStyle" Value="True" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBoxBase}">
                    <Grid>
                        <Border
                            x:Name="ContentBorder"
                            MinWidth="{TemplateBinding MinWidth}"
                            MinHeight="{TemplateBinding MinHeight}"
                            Padding="0"
                            HorizontalAlignment="Stretch"
                            VerticalAlignment="Stretch"
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            CornerRadius="{TemplateBinding Border.CornerRadius}">
                            <Grid
                                
                                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
                                <ScrollViewer
                                    x:Name="PART_ContentHost"
                                    VerticalAlignment="Center"
                                    CanContentScroll="{TemplateBinding ScrollViewer.CanContentScroll}"
                                    HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}"
                                    IsDeferredScrollingEnabled="{TemplateBinding ScrollViewer.IsDeferredScrollingEnabled}"
                                    IsTabStop="{TemplateBinding ScrollViewer.IsTabStop}"
                                    Padding="{TemplateBinding Padding}"                                
                                    TextElement.Foreground="{TemplateBinding Foreground}"
                                    VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}" />
                            </Grid>
                        </Border>
                        <!--  The Accent Border is a separate element so that changes to the border thickness do not affect the position of the element  -->
                        <Border
                            x:Name="AccentBorder"
                            HorizontalAlignment="Stretch"
                            VerticalAlignment="Stretch"
                            BorderBrush="{DynamicResource ControlStrokeColorDefaultBrush}"
                            BorderThickness="{StaticResource TextBoxAccentBorderThemeThickness}"
                            CornerRadius="{TemplateBinding Border.CornerRadius}" />
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsFocused" Value="True">
                            <Setter TargetName="AccentBorder" Property="BorderThickness" Value="0,0,0,2" />
                            <Setter TargetName="AccentBorder" Property="BorderBrush" Value="{DynamicResource TextControlFocusedBorderBrush}" />
                            <Setter TargetName="ContentBorder" Property="Background" Value="{DynamicResource TextControlBackgroundFocused}" />
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsEnabled" Value="True" />
                                <Condition Property="IsMouseOver" Value="True" />
                                <Condition Property="IsFocused" Value="False" />
                            </MultiTrigger.Conditions>
                            <Setter TargetName="ContentBorder" Property="Background" Value="{DynamicResource TextControlBackgroundPointerOver}" />
                        </MultiTrigger>
                        <Trigger Property="IsEnabled" Value="True">
                            <Setter Property="Cursor" Value="IBeam" />
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter TargetName="ContentBorder" Property="Background" Value="{DynamicResource TextControlBackgroundDisabled}" />
                            <Setter TargetName="ContentBorder" Property="BorderBrush" Value="{DynamicResource TextControlBorderBrushDisabled}" />
                            <Setter TargetName="AccentBorder" Property="BorderBrush" Value="{DynamicResource TextControlBorderBrushDisabled}" />
                            <Setter Property="Foreground" Value="{DynamicResource TextControlForegroundDisabled}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>


    <ControlTemplate x:Key="DefaultTextBoxControlTemplate" TargetType="{x:Type TextBox}">
        <Grid>
            <Border
                x:Name="ContentBorder"
                MinWidth="{TemplateBinding MinWidth}"
                MinHeight="{TemplateBinding MinHeight}"
                Padding="0"
                HorizontalAlignment="Stretch"
                VerticalAlignment="Stretch"
                Background="{TemplateBinding Background}"
                BorderBrush="{TemplateBinding BorderBrush}"
                BorderThickness="{TemplateBinding BorderThickness}"
                CornerRadius="{TemplateBinding Border.CornerRadius}">
                <Grid HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>

                    <Grid Grid.Column="0">
                        <ScrollViewer
                            x:Name="PART_ContentHost"
                            VerticalAlignment="Center"
                            CanContentScroll="{TemplateBinding ScrollViewer.CanContentScroll}"
                            HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}"
                            IsDeferredScrollingEnabled="{TemplateBinding ScrollViewer.IsDeferredScrollingEnabled}"
                            IsTabStop="{TemplateBinding ScrollViewer.IsTabStop}"
                            Padding="{TemplateBinding Padding}"                                
                            TextElement.Foreground="{TemplateBinding Foreground}"
                            VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}" />
                    </Grid>

                    <!--  Buttons and Icons have no padding from the main element to allow absolute positions if height is larger than the text entry zone  -->
                    <Button
                        x:Name="ClearButton"
                        Grid.Column="1"
                        MinWidth="{StaticResource TextBoxClearButtonHeight}"
                        MinHeight="{StaticResource TextBoxClearButtonHeight}"
                        Margin="{StaticResource TextBoxClearButtonMargin}"
                        Padding="{StaticResource TextBoxClearButtonPadding}"
                        HorizontalAlignment="Center"
                        VerticalAlignment="Center"
                        HorizontalContentAlignment="Center"
                        VerticalContentAlignment="Center"
                        Background="Transparent"
                        BorderBrush="Transparent"
                        Command="{Binding Path=TemplateButtonCommand, RelativeSource={RelativeSource TemplatedParent}}"
                        Cursor="Arrow"
                        IsTabStop ="False"
                        Foreground="{DynamicResource TextControlButtonForeground}"
                        >
                       
                        <Button.Content>
                            <TextBlock
                                FontSize="{TemplateBinding FontSize}"
                                FontFamily="{DynamicResource SymbolThemeFontFamily}"
                                >&#xE894;</TextBlock>
                        </Button.Content>
                    </Button>
                </Grid>
            </Border>
            <!--  The Accent Border is a separate element so that changes to the border thickness do not affect the position of the element  -->
            <Border
                x:Name="AccentBorder"
                HorizontalAlignment="Stretch"
                VerticalAlignment="Stretch"
                BorderBrush="{DynamicResource ControlStrokeColorDefaultBrush}"
                BorderThickness="{StaticResource TextBoxAccentBorderThemeThickness}"
                CornerRadius="{TemplateBinding Border.CornerRadius}" />
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="Text" Value="{x:Static system:String.Empty}">
                <Setter TargetName="ClearButton" Property="Visibility" Value="Collapsed" />
                <Setter TargetName="ClearButton" Property="Margin" Value="0" />
            </Trigger>
            <Trigger Property="IsFocused" Value="True">
                <Setter TargetName="AccentBorder" Property="BorderThickness" Value="0,0,0,2" />
                <Setter TargetName="AccentBorder" Property="BorderBrush" Value="{DynamicResource TextControlFocusedBorderBrush}" />
                <Setter TargetName="ContentBorder" Property="Background" Value="{DynamicResource TextControlBackgroundFocused}" />   
            </Trigger>
            <Trigger Property="IsKeyboardFocusWithin" Value="False">
                <Setter TargetName="ClearButton" Property="Visibility" Value="Collapsed" />
            </Trigger>
            <Trigger Property="IsKeyboardFocusWithin" Value="True">
                <Setter TargetName="ClearButton" Property="Visibility" Value="Visible" />
            </Trigger>
            <Trigger Property="Text" Value="{x:Static system:String.Empty}">
                <Setter TargetName="ClearButton" Property="Visibility" Value="Collapsed" />
                <Setter TargetName="ClearButton" Property="Margin" Value="0" />
            </Trigger>
           
                        
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsEnabled" Value="True" />
                    <Condition Property="IsMouseOver" Value="True" />
                    <Condition Property="IsFocused" Value="False" />
                </MultiTrigger.Conditions>
                <Setter TargetName="ContentBorder" Property="Background" Value="{DynamicResource TextControlBackgroundPointerOver}" />
            </MultiTrigger>
            <Trigger Property="IsReadOnly" Value="True">
                <Setter TargetName="ClearButton" Property="Visibility" Value="Collapsed" />
                <Setter TargetName="ClearButton" Property="Margin" Value="0" />
            </Trigger>
            <Trigger Property="IsEnabled" Value="True">
                <Setter Property="Cursor" Value="IBeam" />
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">
                <Setter TargetName="ContentBorder" Property="Background" Value="{DynamicResource TextControlBackgroundDisabled}" />
                <Setter TargetName="ContentBorder" Property="BorderBrush" Value="{DynamicResource TextControlBorderBrushDisabled}" />
                <Setter TargetName="AccentBorder" Property="BorderBrush" Value="{DynamicResource TextControlBorderBrushDisabled}" />
                <Setter Property="Foreground" Value="{DynamicResource TextControlForegroundDisabled}" />
                <Setter TargetName="ClearButton" Property="Visibility" Value="Collapsed" />
                <Setter TargetName="ClearButton" Property="Margin" Value="0" />

            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <Style x:Key="DefaultTextBoxStyle" TargetType="{x:Type TextBox}">
        <!--  Universal WPF UI focus  -->
        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
        <!--  Universal WPF UI ContextMenu  -->
        <Setter Property="ContextMenu" Value="{DynamicResource DefaultControlContextMenu}" />
        <Setter Property="Foreground" Value="{DynamicResource TextControlForeground}" />
        <Setter Property="CaretBrush" Value="{DynamicResource TextControlForeground}" />
        <Setter Property="Background" Value="{DynamicResource TextControlBackground}" />
        <Setter Property="BorderBrush" Value="{DynamicResource TextControlElevationBorderBrush}" />
        <Setter Property="BorderThickness" Value="{StaticResource TextBoxBorderThemeThickness}" />
        <Setter Property="ScrollViewer.CanContentScroll" Value="False" />
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Hidden" />
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Hidden" />
        <Setter Property="ScrollViewer.IsDeferredScrollingEnabled" Value="False" />
        <Setter Property="HorizontalAlignment" Value="Stretch" />
        <Setter Property="VerticalAlignment" Value="Stretch" />
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        <Setter Property="VerticalContentAlignment" Value="Top" />
        <Setter Property="MinHeight" Value="{DynamicResource TextControlThemeMinHeight}" />
        <Setter Property="MinWidth" Value="{DynamicResource TextControlThemeMinWidth}" />
        <Setter Property="Padding" Value="{DynamicResource TextControlThemePadding}" />
        <Setter Property="Border.CornerRadius" Value="{DynamicResource ControlCornerRadius}" />
        <Setter Property="SnapsToDevicePixels" Value="True" />
        <Setter Property="OverridesDefaultStyle" Value="True" />
        <Setter Property="Template" Value="{StaticResource DefaultTextBoxControlTemplate}" />
    </Style>

    <Style BasedOn="{StaticResource DefaultTextBoxBaseStyle}" TargetType="{x:Type TextBoxBase}" />
    <Style BasedOn="{StaticResource DefaultTextBoxStyle}" TargetType="{x:Type TextBox}" />

</ResourceDictionary>
