<UserControl xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:m="using:SourceGit.Models"
             xmlns:vm="using:SourceGit.ViewModels"
             xmlns:v="using:SourceGit.Views"
             xmlns:c="using:SourceGit.Converters"
             mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
             x:Class="SourceGit.Views.Repository"
             x:DataType="vm:Repository">
  <Grid>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="{Binding Source={x:Static vm:Preferences.Instance}, Path=Layout.RepositorySidebarWidth, Mode=TwoWay}" MinWidth="200" MaxWidth="500"/>
      <ColumnDefinition Width="3"/>
      <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <!-- Left Panel -->
    <Grid Grid.Column="0" RowDefinitions="Auto,*">
      <!-- Page Switcher for Left Contents (Dashboard or CommitSearch) -->
      <StackPanel Grid.Row="0" Margin="0,6" Height="24" HorizontalAlignment="Center" Orientation="Horizontal">
        <RadioButton Classes="switch_button"
                     Width="48"
                     GroupName="SearchGroup"
                     IsChecked="{Binding !IsSearchingCommits, Mode=OneWay}">
          <ToolTip.Tip>
            <TextBlock>
              <Run Text="{DynamicResource Text.Repository.Dashboard}" Foreground="{DynamicResource Brush.FG1}"/>
              <Run Text=" "/>
              <Run Text="{OnPlatform Ctrl+Shift+H, macOS=⌘+⇧+H}" FontSize="11" Foreground="{DynamicResource MenuFlyoutItemKeyboardAcceleratorTextForeground}"/>
            </TextBlock>
          </ToolTip.Tip>
          <Path Width="12" Height="12" Stretch="Fill" HorizontalAlignment="Center" Data="{StaticResource Icons.Home}"/>
        </RadioButton>

        <RadioButton Classes="switch_button"
                     Width="48"
                     GroupName="SearchGroup"
                     IsChecked="{Binding IsSearchingCommits, Mode=TwoWay}">
          <ToolTip.Tip>
            <TextBlock>
              <Run Text="{DynamicResource Text.Repository.Search}" Foreground="{DynamicResource Brush.FG1}"/>
              <Run Text=" "/>
              <Run Text="{OnPlatform Ctrl+F, macOS=⌘+F}" FontSize="11" Foreground="{DynamicResource MenuFlyoutItemKeyboardAcceleratorTextForeground}"/>
            </TextBlock>
          </ToolTip.Tip>
          <Path Width="12" Height="12" Stretch="Fill" HorizontalAlignment="Center" Data="{StaticResource Icons.Search}"/>
        </RadioButton>
      </StackPanel>

      <!-- Dashboard -->
      <Grid Grid.Row="1" Margin="0,0,0,8" RowDefinitions="Auto,Auto,*" IsVisible="{Binding !IsSearchingCommits}">
        <!-- Page Switcher for Right Panel -->
        <Border Grid.Row="0" Margin="8,0,4,0" BorderThickness="1" BorderBrush="{DynamicResource Brush.Border2}" CornerRadius="6">
          <Border CornerRadius="6" ClipToBounds="True">
            <ListBox Background="Transparent" SelectedIndex="{Binding SelectedViewIndex, Mode=TwoWay}" SelectionMode="AlwaysSelected">
              <ListBox.Styles>
                <Style Selector="Path.icon">
                  <Setter Property="Width" Value="12"/>
                  <Setter Property="Height" Value="12"/>
                  <Setter Property="Margin" Value="8,0,6,0"/>
                  <Setter Property="Fill" Value="{DynamicResource Brush.FG2}"/>
                </Style>
                <Style Selector="TextBlock.header">
                  <Setter Property="Foreground" Value="{DynamicResource Brush.FG2}"/>
                  <Setter Property="FontWeight" Value="Bold"/>
                </Style>
                <Style Selector="ListBoxItem">
                  <Setter Property="Height" Value="28"/>
                  <Setter Property="Margin" Value="0"/>
                  <Setter Property="Padding" Value="0"/>
                  <Setter Property="BorderThickness" Value="0,0,0,1"/>
                  <Setter Property="BorderBrush" Value="{DynamicResource Brush.Border2}"/>
                </Style>
                <Style Selector="ListBoxItem:nth-last-child(1)">
                  <Setter Property="BorderThickness" Value="0"/>
                </Style>
                <Style Selector="ListBoxItem:pointerover /template/ ContentPresenter#PART_ContentPresenter, ListBoxItem:selected /template/ ContentPresenter#PART_ContentPresenter">
                  <Setter Property="Background" Value="Transparent"/>
                </Style>
                <Style Selector="ListBoxItem:selected Grid.view_mode">
                  <Setter Property="Background" Value="{DynamicResource Brush.AccentHovered}"/>
                </Style>
                <Style Selector="ListBoxItem:selected TextBlock.header">
                  <Setter Property="Foreground" Value="{DynamicResource Brush.FG1}"/>
                </Style>
                <Style Selector="ListBoxItem:selected Path.icon">
                  <Setter Property="Fill" Value="{DynamicResource Brush.FG1}"/>
                </Style>
              </ListBox.Styles>

              <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                  <StackPanel Orientation="Vertical"/>
                </ItemsPanelTemplate>
              </ListBox.ItemsPanel>

              <ListBoxItem>
                <Grid Classes="view_mode" ColumnDefinitions="Auto,*,Auto,Auto,Auto">
                  <Path Grid.Column="0" Classes="icon" Data="{StaticResource Icons.Histories}"/>
                  <TextBlock Grid.Column="1" Classes="header" Text="{DynamicResource Text.Histories}"/>

                  <ToggleButton Grid.Column="2"
                                Classes="line_path"
                                Width="26" Height="26"
                                Background="Transparent"
                                IsChecked="{Binding OnlyHighlightCurrentBranchInHistories, Mode=TwoWay}"
                                ToolTip.Tip="{DynamicResource Text.Repository.OnlyHighlightCurrentBranchInGraph}">
                    <Path Width="12" Height="12" Data="{StaticResource Icons.LightOn}"/>
                  </ToggleButton>
                  <ToggleButton Grid.Column="3"
                                Classes="line_path"
                                Width="26" Height="26"
                                Background="Transparent"
                                IsChecked="{Binding Source={x:Static vm:Preferences.Instance}, Path=DisplayTimeAsPeriodInHistories, Mode=TwoWay}"
                                ToolTip.Tip="{DynamicResource Text.Repository.UseRelativeTimeInGraph}">
                    <Path Width="12" Height="12" Data="{StaticResource Icons.Stopwatch}"/>
                  </ToggleButton>
                  <Button Grid.Column="4"
                          Classes="icon_button"
                          Width="26" Height="26"
                          Click="OnOpenAdvancedHistoriesOption"
                          ToolTip.Tip="{DynamicResource Text.Repository.MoreOptions}">
                    <Path Width="12" Height="12" Data="{StaticResource Icons.More}"/>
                  </Button>
                </Grid>
              </ListBoxItem>

              <ListBoxItem IsVisible="{Binding !IsBare}">
                <Grid Classes="view_mode" ColumnDefinitions="Auto,*,Auto,Auto,Auto">
                  <Path Grid.Column="0" Classes="icon" Data="{StaticResource Icons.Changes}"/>
                  <TextBlock Grid.Column="1" Classes="header" Text="{DynamicResource Text.WorkingCopy}"/>
                  <Border Grid.Column="2"
                          Height="18"
                          CornerRadius="9"
                          Padding="9,0"
                          Margin="6,0"
                          VerticalAlignment="Center"
                          Background="{DynamicResource Brush.Badge}"
                          IsVisible="{Binding LocalChangesCount, Mode=OneWay, Converter={x:Static c:IntConverters.IsGreaterThanZero}}">
                    <TextBlock Text="{Binding LocalChangesCount, Mode=OneWay}"
                               Foreground="{DynamicResource Brush.BadgeFG}"
                               FontFamily="{DynamicResource Fonts.Monospace}"
                               FontSize="10"/>
                  </Border>
                  <Path Grid.Column="3"
                        Width="12" Height="12"
                        Margin="0,0,6,0"
                        Data="{StaticResource Icons.Info}"
                        Fill="DarkOrange"
                        IsVisible="{Binding InProgressContext, Converter={x:Static ObjectConverters.IsNotNull}}"/>
                  <Button Grid.Column="4"
                          Classes="icon_button"
                          Width="26" Height="26"
                          Command="{Binding DiscardAllChanges}"
                          ToolTip.Tip="{DynamicResource Text.Repository.DiscardAll}">
                    <Path Width="12" Height="12" Data="{StaticResource Icons.Undo}"/>
                  </Button>
                </Grid>
              </ListBoxItem>

              <ListBoxItem IsVisible="{Binding !IsBare}">
                <Grid Classes="view_mode" ColumnDefinitions="Auto,*,Auto,Auto">
                  <Path Grid.Column="0" Classes="icon" Data="{StaticResource Icons.Stashes}"/>
                  <TextBlock Grid.Column="1" Classes="header" Text="{DynamicResource Text.Stashes}"/>
                  <Border Grid.Column="2"
                          Height="18"
                          CornerRadius="9"
                          Padding="6,0"
                          Margin="6,0"
                          VerticalAlignment="Center"
                          Background="{DynamicResource Brush.Badge}"
                          IsVisible="{Binding StashesCount, Mode=OneWay, Converter={x:Static c:IntConverters.IsGreaterThanZero}}">
                    <TextBlock Text="{Binding StashesCount, Mode=OneWay}"
                               Foreground="{DynamicResource Brush.BadgeFG}"
                               FontFamily="{DynamicResource Fonts.Monospace}"
                               FontSize="10"/>
                  </Border>
                  <Button Grid.Column="3"
                          Classes="icon_button"
                          Width="26" Height="26"
                          Command="{Binding ClearStashes}"
                          IsEnabled="{Binding StashesCount, Converter={x:Static c:IntConverters.IsGreaterThanZero}}"
                          ToolTip.Tip="{DynamicResource Text.Repository.ClearStashes}">
                    <Path Width="12" Height="12" Data="{StaticResource Icons.RemoveAll}"/>
                  </Button>
                </Grid>
              </ListBoxItem>
            </ListBox>
          </Border>
        </Border>

        <!-- Filter Branches/Tags/Submodules -->
        <TextBox Grid.Row="1"
                 Height="26"
                 Margin="8,6,4,0"
                 BorderThickness="1"
                 CornerRadius="4"
                 BorderBrush="{DynamicResource Brush.Border2}"
                 Watermark="{DynamicResource Text.Repository.Filter}"
                 Text="{Binding Filter, Mode=TwoWay}"
                 VerticalContentAlignment="Center">
          <TextBox.InnerLeftContent>
            <Path Width="14" Height="14"
                  Margin="6,0,0,0"
                  Fill="{DynamicResource Brush.FG2}"
                  Data="{StaticResource Icons.Search}"/>
          </TextBox.InnerLeftContent>

          <TextBox.InnerRightContent>
            <Button Classes="icon_button"
                    Width="16"
                    Margin="0,0,6,0"
                    Command="{Binding ClearFilter}"
                    IsVisible="{Binding Filter, Converter={x:Static StringConverters.IsNotNullOrEmpty}}"
                    HorizontalAlignment="Right">
              <Path Width="14" Height="14"
                    Margin="0,1,0,0"
                    Fill="{DynamicResource Brush.FG1}"
                    Data="{StaticResource Icons.Clear}"/>
            </Button>
          </TextBox.InnerRightContent>
        </TextBox>

        <Grid Grid.Row="2" x:Name="LeftSidebarGroups" Margin="0,4,0,0" RowDefinitions="28,Auto,28,Auto,28,Auto,28,Auto,28,Auto" SizeChanged="OnLeftSidebarSizeChanged">
          <!-- Local Branches -->
          <ToggleButton Grid.Row="0" Classes="group_expander" IsChecked="{Binding IsLocalBranchGroupExpanded, Mode=TwoWay}">
            <Grid ColumnDefinitions="16,*,Auto,Auto">
              <Path Grid.Column="0" Width="11" Height="11" HorizontalAlignment="Left" Data="{StaticResource Icons.Local}" Fill="{DynamicResource Brush.FG2}"/>
              <StackPanel Grid.Column="1" Orientation="Horizontal">
                <TextBlock Classes="group_header_label" Text="{DynamicResource Text.Repository.LocalBranches}"/>
                <TextBlock Classes="group_header_label" Text="{Binding LocalBranchesCount, StringFormat='({0})'}" Margin="4,0,0,0"/>
              </StackPanel>
              <Button Grid.Column="2"
                      Classes="icon_button"
                      Width="14"
                      Margin="8,0,0,0"
                      Click="OnOpenSortLocalBranchMenu"
                      ToolTip.Tip="{DynamicResource Text.Repository.BranchSort}">
                <Grid>
                  <Path Width="12" Height="12" Data="{StaticResource Icons.OrderByName}" IsVisible="{Binding IsSortingLocalBranchByName, Mode=OneWay}"/>
                  <Path Width="12" Height="12" Data="{StaticResource Icons.OrderByTime}" IsVisible="{Binding !IsSortingLocalBranchByName, Mode=OneWay}"/>
                </Grid>
              </Button>
              <Button Grid.Column="3" Classes="icon_button" Width="14" Margin="8,0" Command="{Binding CreateNewBranch}" ToolTip.Tip="{DynamicResource Text.Repository.NewBranch}">
                <Path Width="12" Height="12" Data="{StaticResource Icons.Branch.Add}"/>
              </Button>
            </Grid>
          </ToggleButton>
          <v:BranchTree Grid.Row="1"
                        x:Name="LocalBranchTree"
                        Height="0"
                        Margin="8,0,4,0"
                        Nodes="{Binding LocalBranchTrees}"
                        IsVisible="{Binding IsLocalBranchGroupExpanded}"
                        SelectionChanged="OnLocalBranchTreeSelectionChanged"
                        RowsChanged="OnBranchTreeRowsChanged"/>

          <!-- Remotes -->
          <ToggleButton Grid.Row="2" Classes="group_expander" IsChecked="{Binding IsRemoteGroupExpanded, Mode=TwoWay}">
            <Grid ColumnDefinitions="16,*,Auto,Auto">
              <Path Grid.Column="0" Width="12" Height="12" HorizontalAlignment="Left" Data="{StaticResource Icons.Remotes}" Fill="{DynamicResource Brush.FG2}"/>
              <StackPanel Grid.Column="1" Orientation="Horizontal">
                <TextBlock Classes="group_header_label" Text="{DynamicResource Text.Repository.Remotes}"/>
                <TextBlock Classes="group_header_label" Text="{Binding Remotes, Converter={x:Static c:ListConverters.ToCount}}" Margin="4,0,0,0"/>
              </StackPanel>
              <Button Grid.Column="2"
                      Classes="icon_button"
                      Width="14"
                      Margin="8,0,0,0"
                      Click="OnOpenSortRemoteBranchMenu"
                      ToolTip.Tip="{DynamicResource Text.Repository.BranchSort}">
                <Grid>
                  <Path Width="12" Height="12" Data="{StaticResource Icons.OrderByName}" IsVisible="{Binding IsSortingRemoteBranchByName, Mode=OneWay}"/>
                  <Path Width="12" Height="12" Data="{StaticResource Icons.OrderByTime}" IsVisible="{Binding !IsSortingRemoteBranchByName, Mode=OneWay}"/>
                </Grid>
              </Button>
              <Button Grid.Column="3" Classes="icon_button" Width="14" Margin="8,0" Command="{Binding AddRemote}" ToolTip.Tip="{DynamicResource Text.Repository.Remotes.Add}">
                <Path Width="12" Height="12" Data="{StaticResource Icons.Remote.Add}"/>
              </Button>
            </Grid>
          </ToggleButton>
          <v:BranchTree Grid.Row="3"
                        x:Name="RemoteBranchTree"
                        Height="0"
                        Margin="8,0,4,0"
                        Nodes="{Binding RemoteBranchTrees}"
                        IsVisible="{Binding IsRemoteGroupExpanded}"
                        SelectionChanged="OnRemoteBranchTreeSelectionChanged"
                        RowsChanged="OnBranchTreeRowsChanged"/>

          <!-- Tags -->
          <ToggleButton Grid.Row="4" Classes="group_expander" IsChecked="{Binding IsTagGroupExpanded, Mode=TwoWay}">
            <Grid ColumnDefinitions="16,*,Auto,Auto,Auto">
              <Path Grid.Column="0" Width="11" Height="11" Margin="2,1,0,0" HorizontalAlignment="Left" Data="{StaticResource Icons.Tags}" Fill="{DynamicResource Brush.FG2}"/>
              <StackPanel Grid.Column="1" Orientation="Horizontal">
                <TextBlock Classes="group_header_label" Text="{DynamicResource Text.Repository.Tags}"/>
                <TextBlock Classes="group_header_label" Text="{Binding Tags, Converter={x:Static c:ListConverters.ToCount}}" Margin="4,0,0,0"/>
              </StackPanel>
              <ToggleButton Grid.Column="2"
                            Classes="show_as_tree"
                            Width="14"
                            IsChecked="{Binding ShowTagsAsTree, Mode=TwoWay}"
                            ToolTip.Tip="{DynamicResource Text.Repository.ShowTagsAsTree}"/>
              <Button Grid.Column="3"
                      Classes="icon_button"
                      Width="14"
                      Margin="8,0,0,0"
                      Click="OnOpenSortTagMenu"
                      ToolTip.Tip="{DynamicResource Text.Repository.Tags.Sort}">
                <Grid>
                  <Path Width="12" Height="12" Data="{StaticResource Icons.OrderByName}" IsVisible="{Binding IsSortingTagsByName, Mode=OneWay}"/>
                  <Path Width="12" Height="12" Data="{StaticResource Icons.OrderByTime}" IsVisible="{Binding !IsSortingTagsByName, Mode=OneWay}"/>
                </Grid>
              </Button>
              <Button Grid.Column="4"
                      Classes="icon_button"
                      Width="14"
                      Margin="8,0"
                      Command="{Binding CreateNewTag}"
                      ToolTip.Tip="{DynamicResource Text.Repository.Tags.Add}">
                <Path Width="12" Height="12" Data="{StaticResource Icons.Tag.Add}"/>
              </Button>
            </Grid>
          </ToggleButton>
          <v:TagsView Grid.Row="5"
                      x:Name="TagsList"
                      Height="0"
                      Margin="8,0,4,0"
                      Background="Transparent"
                      Content="{Binding VisibleTags}"
                      Focusable="False"
                      IsVisible="{Binding IsTagGroupExpanded, Mode=OneWay}"
                      SelectionChanged="OnTagsSelectionChanged"
                      RowsChanged="OnTagsRowsChanged"/>

          <!-- Submodules -->
          <ToggleButton Grid.Row="6" Classes="group_expander" IsChecked="{Binding IsSubmoduleGroupExpanded, Mode=TwoWay}">
            <Grid ColumnDefinitions="16,*,Auto,Auto,Auto">
              <Path Grid.Column="0" Width="10" Height="10" Margin="2,0,0,0" HorizontalAlignment="Left" Data="{StaticResource Icons.Submodules}" Fill="{DynamicResource Brush.FG2}"/>
              <StackPanel Grid.Column="1" Orientation="Horizontal">
                <TextBlock Classes="group_header_label" Text="{DynamicResource Text.Repository.Submodules}"/>
                <TextBlock Classes="group_header_label" Text="{Binding Submodules, Converter={x:Static c:ListConverters.ToCount}}" Margin="4,0,0,0"/>
              </StackPanel>
              <ToggleButton Grid.Column="2"
                            Classes="show_as_tree"
                            Width="14"
                            IsChecked="{Binding ShowSubmodulesAsTree, Mode=TwoWay}"
                            IsVisible="{Binding Submodules, Converter={x:Static c:ListConverters.IsNotNullOrEmpty}}"
                            ToolTip.Tip="{DynamicResource Text.Repository.ShowSubmodulesAsTree}"/>
              <Button Grid.Column="3"
                      Classes="icon_button"
                      Width="14"
                      Margin="8,0"
                      Command="{Binding UpdateSubmodules}"
                      IsVisible="{Binding Submodules, Converter={x:Static c:ListConverters.IsNotNullOrEmpty}}"
                      ToolTip.Tip="{DynamicResource Text.Repository.Submodules.Update}">
                <Path Width="12" Height="12" Data="{StaticResource Icons.Loading}"/>
              </Button>
              <Button Grid.Column="4"
                      Classes="icon_button"
                      Width="14"
                      Margin="0,0,8,0"
                      Command="{Binding AddSubmodule}"
                      ToolTip.Tip="{DynamicResource Text.Repository.Submodules.Add}">
                <Path Width="12" Height="12" Data="{StaticResource Icons.Submodule.Add}"/>
              </Button>
            </Grid>
          </ToggleButton>
          <v:SubmodulesView Grid.Row="7"
                            x:Name="SubmoduleList"
                            Height="0"
                            Margin="8,0,4,0"
                            Content="{Binding VisibleSubmodules}"
                            RowsChanged="OnSubmodulesRowsChanged"
                            Focusable="False"
                            IsVisible="{Binding IsSubmoduleGroupExpanded, Mode=OneWay}"/>

          <!-- Worktrees -->
          <ToggleButton Grid.Row="8" Classes="group_expander" IsChecked="{Binding IsWorktreeGroupExpanded, Mode=TwoWay}">
            <Grid ColumnDefinitions="16,*,Auto,Auto">
              <Path Grid.Column="0" Width="11" Height="11" Margin="1,0,0,0" HorizontalAlignment="Left" Data="{StaticResource Icons.Worktrees}" Fill="{DynamicResource Brush.FG2}"/>
              <StackPanel Grid.Column="1" Orientation="Horizontal">
                <TextBlock Classes="group_header_label" Text="{DynamicResource Text.Repository.Worktrees}"/>
                <TextBlock Classes="group_header_label" Text="{Binding Worktrees, Converter={x:Static c:ListConverters.ToCount}}" Margin="4,0,0,0"/>
              </StackPanel>
              <Button Grid.Column="2"
                      Classes="icon_button"
                      Width="14"
                      Margin="8,0"
                      Click="OnPruneWorktrees"
                      IsVisible="{Binding Worktrees, Converter={x:Static c:ListConverters.IsNotNullOrEmpty}}"
                      ToolTip.Tip="{DynamicResource Text.Repository.Worktrees.Prune}">
                <Path Width="12" Height="12" Data="{StaticResource Icons.Loading}"/>
              </Button>
              <Button Grid.Column="3"
                      Classes="icon_button"
                      Width="14"
                      Margin="0,0,9,0"
                      Command="{Binding AddWorktree}"
                      ToolTip.Tip="{DynamicResource Text.Repository.Worktrees.Add}">
                <Path Width="11" Height="11" Data="{StaticResource Icons.Worktree.Add}"/>
              </Button>
            </Grid>
          </ToggleButton>
          <ListBox Grid.Row="9"
                   x:Name="WorktreeList"
                   Height="0"
                   Margin="12,0,4,0"
                   Classes="repo_left_content_list"
                   ItemsSource="{Binding Worktrees}"
                   SelectionMode="Single"
                   ContextRequested="OnWorktreeContextRequested"
                   DoubleTapped="OnDoubleTappedWorktree"
                   PropertyChanged="OnWorktreeListPropertyChanged"
                   IsVisible="{Binding IsWorktreeGroupExpanded, Mode=OneWay}">
            <ListBox.Styles>
              <Style Selector="ListBoxItem">
                <Setter Property="CornerRadius" Value="4"/>
              </Style>
            </ListBox.Styles>
            <ListBox.ItemTemplate>
              <DataTemplate DataType="m:Worktree">
                <Grid ColumnDefinitions="Auto,*,22" Background="Transparent">
                  <Path Grid.Column="0" Width="10" Height="10" Margin="8,0,0,0" Data="{StaticResource Icons.Worktree}"/>
                  <TextBlock Grid.Column="1" Margin="8,0,0,0" Foreground="{DynamicResource Brush.FG2}" TextTrimming="CharacterEllipsis">
                    <Run Text="{Binding Name}" Foreground="{DynamicResource Brush.FG1}"/>
                    <Run Text="{Binding RelativePath}"/>
                  </TextBlock>
                  <Path Grid.Column="2" Width="10" Height="10" Margin="4,0,0,0" Data="{StaticResource Icons.Lock}" Fill="{DynamicResource Brush.FG2}" IsVisible="{Binding IsLocked}"/>
                </Grid>
              </DataTemplate>
            </ListBox.ItemTemplate>
          </ListBox>
        </Grid>
      </Grid>

      <!-- Commit Search Panel -->
      <Grid Grid.Row="1" RowDefinitions="Auto,26,*" Margin="8,0,4,8" IsVisible="{Binding IsSearchingCommits}" PropertyChanged="OnSearchCommitPanelPropertyChanged">
        <!-- Search Input Box -->
        <Grid Grid.Row="0" Height="26" Margin="0,4,0,0">
          <TextBox x:Name="TxtSearchCommitsBox"
                   Height="24"
                   BorderThickness="1"
                   BorderBrush="{DynamicResource Brush.Border2}"
                   Background="{DynamicResource Brush.Contents}"
                   CornerRadius="12"
                   Watermark="{DynamicResource Text.Repository.Search}"
                   Text="{Binding SearchCommitContext.Filter, Mode=TwoWay}"
                   VerticalContentAlignment="Center"
                   KeyDown="OnSearchKeyDown">
            <TextBox.InnerLeftContent>
              <Path Width="14" Height="14"
                    Margin="6,0,0,0"
                    Fill="{DynamicResource Brush.FG2}"
                    Data="{StaticResource Icons.Search}"/>
            </TextBox.InnerLeftContent>

            <TextBox.InnerRightContent>
              <Button Classes="icon_button"
                      Width="16"
                      Margin="0,0,6,0"
                      Click="OnClearSearchCommitFilter"
                      IsVisible="{Binding SearchCommitContext.Filter, Converter={x:Static StringConverters.IsNotNullOrEmpty}}"
                      HorizontalAlignment="Right">
                <Path Width="14" Height="14"
                      Margin="0,1,0,0"
                      Fill="{DynamicResource Brush.FG1}"
                      Data="{StaticResource Icons.Clear}"/>
              </Button>
            </TextBox.InnerRightContent>
          </TextBox>

          <Popup PlacementTarget="{Binding #TxtSearchCommitsBox}"
                 Placement="BottomEdgeAlignedLeft"
                 HorizontalOffset="-8" VerticalAlignment="-8">
            <Popup.IsOpen>
              <MultiBinding Converter="{x:Static BoolConverters.And}">
                <Binding Path="SearchCommitContext.Suggestions" Converter="{x:Static c:ListConverters.IsNotNullOrEmpty}"/>
                <Binding Path="$parent[Window].IsActive"/>
              </MultiBinding>
            </Popup.IsOpen>

            <Border Margin="8" VerticalAlignment="Top" Effect="drop-shadow(0 0 8 #80000000)">
              <Border Background="{DynamicResource Brush.Popup}" CornerRadius="4" Padding="4" BorderThickness="0.65" BorderBrush="{DynamicResource Brush.Accent}">
                <ListBox x:Name="SearchSuggestionBox"
                         Background="Transparent"
                         SelectionMode="Single"
                         ItemsSource="{Binding SearchCommitContext.Suggestions}"
                         MaxHeight="400"
                         Focusable="True"
                         KeyDown="OnSearchSuggestionBoxKeyDown">
                  <ListBox.Styles>
                    <Style Selector="ListBoxItem">
                      <Setter Property="Padding" Value="0"/>
                      <Setter Property="MinHeight" Value="26"/>
                      <Setter Property="CornerRadius" Value="4"/>
                    </Style>

                    <Style Selector="ListBox">
                      <Setter Property="FocusAdorner">
                        <FocusAdornerTemplate>
                          <Grid/>
                        </FocusAdornerTemplate>
                      </Setter>
                    </Style>
                  </ListBox.Styles>

                  <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                      <VirtualizingStackPanel Orientation="Vertical"/>
                    </ItemsPanelTemplate>
                  </ListBox.ItemsPanel>

                  <ListBox.ItemTemplate>
                    <DataTemplate DataType="x:String">
                      <StackPanel Background="Transparent" Orientation="Vertical" Margin="8,4" DoubleTapped="OnSearchSuggestionDoubleTapped">
                        <StackPanel Orientation="Horizontal">
                          <Path Width="12" Height="12" Data="{StaticResource Icons.File}"/>
                          <TextBlock Margin="6,0,0,0" Text="{Binding Converter={x:Static c:PathConverters.PureFileName}}"/>
                        </StackPanel>
                        <TextBlock FontSize="12" Margin="18,2,0,0" Foreground="{DynamicResource Brush.FG2}" Text="{Binding Converter={x:Static c:PathConverters.PureDirectoryName}}"/>
                      </StackPanel>
                    </DataTemplate>
                  </ListBox.ItemTemplate>
                </ListBox>
              </Border>
            </Border>
          </Popup>
        </Grid>

        <StackPanel Grid.Row="1" Orientation="Horizontal">
          <ComboBox MinHeight="24" Height="24"
                    Padding="4,0"
                    Background="Transparent"
                    BorderThickness="0"
                    SelectedIndex="{Binding SearchCommitContext.Method, Mode=TwoWay}">
            <ComboBox.Items>
              <TextBlock Text="{DynamicResource Text.Repository.Search.BySHA}"/>
              <TextBlock Text="{DynamicResource Text.Repository.Search.ByAuthor}"/>
              <TextBlock Text="{DynamicResource Text.Repository.Search.ByCommitter}"/>
              <TextBlock Text="{DynamicResource Text.Repository.Search.ByMessage}"/>
              <TextBlock Text="{DynamicResource Text.Repository.Search.ByPath}"/>
              <TextBlock Text="{DynamicResource Text.Repository.Search.ByContent}"/>
            </ComboBox.Items>
          </ComboBox>

          <CheckBox Height="24"
                    Margin="4,0,0,0"
                    IsChecked="{Binding SearchCommitContext.OnlySearchCurrentBranch, Mode=TwoWay}"
                    IsVisible="{Binding SearchCommitContext.Method, Converter={x:Static c:IntConverters.IsGreaterThanZero}}">
            <TextBlock Text="{DynamicResource Text.Repository.Search.InCurrentBranch}"/>
          </CheckBox>
        </StackPanel>

        <ListBox Grid.Row="2"
                 Margin="0,8,0,0"
                 Padding="4"
                 ItemsSource="{Binding SearchCommitContext.Results}"
                 SelectionMode="Single"
                 SelectedItem="{Binding SearchCommitContext.Selected, Mode=TwoWay}"
                 BorderThickness="1"
                 BorderBrush="{DynamicResource Brush.Border2}"
                 Background="{DynamicResource Brush.Contents}"
                 CornerRadius="4"
                 ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                 ScrollViewer.VerticalScrollBarVisibility="Auto"
                 Grid.IsSharedSizeScope="True">
          <ListBox.Styles>
            <Style Selector="ListBoxItem">
              <Setter Property="Margin" Value="0"/>
              <Setter Property="Padding" Value="0"/>
              <Setter Property="Height" Value="28"/>
              <Setter Property="CornerRadius" Value="4"/>
            </Style>
          </ListBox.Styles>

          <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
              <VirtualizingStackPanel Orientation="Vertical"/>
            </ItemsPanelTemplate>
          </ListBox.ItemsPanel>

          <ListBox.ItemTemplate>
            <DataTemplate DataType="m:Commit">
              <Grid>
                <Grid.ColumnDefinitions>
                  <ColumnDefinition Width="24"/>
                  <ColumnDefinition Width="*"/>
                  <ColumnDefinition Width="Auto" SharedSizeGroup="SearchCommitTimeColumn"/>
                </Grid.ColumnDefinitions>
                <v:Avatar Grid.Column="0" Width="16" Height="16" HorizontalAlignment="Center" VerticalAlignment="Center" IsHitTestVisible="False" User="{Binding Author}"/>
                <Grid Grid.Column="1" ColumnDefinitions="Auto,*">
                  <Path Grid.Column="0" Width="14" Height="14" Margin="0,2,2,0" Data="{StaticResource Icons.Check}" Fill="Green" IsVisible="{Binding IsMerged, Mode=OneWay}"/>
                  <TextBlock Grid.Column="1" Text="{Binding Subject}" Margin="2,0,0,0" VerticalAlignment="Center" TextTrimming="CharacterEllipsis"/>
                </Grid>
                <TextBlock Grid.Column="2"
                           Margin="4,0"
                           Foreground="{DynamicResource Brush.FG2}"
                           Text="{Binding CommitterTimeShortStr}"
                           HorizontalAlignment="Right" VerticalAlignment="Center"
                           TextTrimming="CharacterEllipsis"/>
              </Grid>
            </DataTemplate>
          </ListBox.ItemTemplate>
        </ListBox>

        <Path Grid.Row="2"
              HorizontalAlignment="Center" VerticalAlignment="Center"
              Width="48" Height="48"
              Data="{StaticResource Icons.Empty}"
              Fill="{DynamicResource Brush.FG2}">
          <Path.IsVisible>
            <MultiBinding Converter="{x:Static BoolConverters.And}">
              <Binding Path="SearchCommitContext.Results" Converter="{x:Static c:ListConverters.IsNullOrEmpty}"/>
              <Binding Path="SearchCommitContext.IsQuerying" Converter="{x:Static BoolConverters.Not}"/>
            </MultiBinding>
          </Path.IsVisible>
        </Path>

        <v:LoadingIcon Grid.Row="2"
                       Width="48" Height="48"
                       HorizontalAlignment="Center" VerticalAlignment="Center"
                       IsVisible="{Binding SearchCommitContext.IsQuerying}"/>
      </Grid>
    </Grid>

    <!-- Splitter -->
    <GridSplitter Grid.Column="1"
                  MinWidth="1"
                  HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                  Background="Transparent"
                  BorderThickness="0,0,1,0"
                  BorderBrush="{DynamicResource Brush.Border0}"/>

    <!-- Right -->
    <Grid Grid.Column="2" RowDefinitions="Auto,Auto,Auto,*">
      <Grid Grid.Row="0" Height="28" ColumnDefinitions="*,Auto" Background="{DynamicResource Brush.Conflict}" IsVisible="{Binding InProgressContext, Converter={x:Static ObjectConverters.IsNotNull}}">
        <ContentControl Grid.Column="0" Margin="8,0" Content="{Binding InProgressContext}">
          <ContentControl.DataTemplates>
            <DataTemplate DataType="m:Commit">
              <StackPanel MinWidth="400" Orientation="Vertical">
                <Grid ColumnDefinitions="Auto,*,Auto">
                  <v:Avatar Grid.Column="0" Width="16" Height="16" VerticalAlignment="Center" IsHitTestVisible="False" User="{Binding Author}"/>
                  <TextBlock Grid.Column="1" Text="{Binding Author.Name}" Margin="8,0,0,0"/>
                  <TextBlock Grid.Column="2" Text="{Binding CommitterTimeStr}" Foreground="{DynamicResource Brush.FG2}" Margin="8,0,0,0"/>
                </Grid>

                <TextBlock Margin="0,8,0,0" Text="{Binding Subject}" TextWrapping="Wrap"/>
              </StackPanel>
            </DataTemplate>

            <DataTemplate DataType="vm:CherryPickInProgress">
              <Grid ColumnDefinitions="*,Auto">
                <StackPanel Grid.Column="0" Orientation="Horizontal">
                  <TextBlock FontWeight="Bold" Foreground="{DynamicResource Brush.ConflictForeground}" Text="{DynamicResource Text.InProgress.CherryPick}"/>
                  <TextBlock FontWeight="Bold" Margin="4,0,0,0" Foreground="{DynamicResource Brush.ConflictForeground}" Text="{DynamicResource Text.InProgress.CherryPick.Head}"/>
                  <TextBlock FontWeight="Bold" Margin="4,0,0,0" Foreground="DarkOrange" Text="{Binding Head.SHA, Converter={x:Static c:StringConverters.ToShortSHA}}" ToolTip.Tip="{Binding Head}"/>
                </StackPanel>

                <Button Grid.Column="1" Classes="flat" FontWeight="Regular" BorderThickness="0" Content="{DynamicResource Text.Repository.Skip}" Padding="8,2" Click="OnSkipInProgress"/>
              </Grid>
            </DataTemplate>

            <DataTemplate DataType="vm:RebaseInProgress">
              <Grid ColumnDefinitions="*,Auto">
                <StackPanel Grid.Column="0" Orientation="Horizontal">
                  <TextBlock FontWeight="Bold" Foreground="{DynamicResource Brush.ConflictForeground}" Text="{DynamicResource Text.InProgress.Rebase}"/>
                  <TextBlock FontWeight="Bold" Margin="4,0,0,0" Foreground="{DynamicResource Brush.ConflictForeground}"
                             Text="{DynamicResource Text.InProgress.Rebase.StoppedAt}"
                             IsVisible="{Binding StoppedAt, Converter={x:Static ObjectConverters.IsNotNull}}" />
                  <TextBlock FontWeight="Bold" Margin="4,0,0,0" Foreground="DarkOrange"
                             Text="{Binding StoppedAt.SHA, Converter={x:Static c:StringConverters.ToShortSHA}}"
                             IsVisible="{Binding StoppedAt, Converter={x:Static ObjectConverters.IsNotNull}}"
                             ToolTip.Tip="{Binding StoppedAt}" />
                </StackPanel>

                <Button Grid.Column="1" Classes="flat" FontWeight="Regular" BorderThickness="0"
                        Content="{DynamicResource Text.Repository.Skip}" Padding="8,2" Click="OnSkipInProgress"
                        IsVisible="{Binding StoppedAt, Converter={x:Static ObjectConverters.IsNotNull}}" />
              </Grid>
            </DataTemplate>

            <DataTemplate DataType="vm:RevertInProgress">
              <Grid ColumnDefinitions="*,Auto">
                <StackPanel Grid.Column="0" Orientation="Horizontal">
                  <TextBlock FontWeight="Bold" Foreground="{DynamicResource Brush.ConflictForeground}" Text="{DynamicResource Text.InProgress.Revert}"/>
                  <TextBlock FontWeight="Bold" Margin="4,0,0,0" Foreground="{DynamicResource Brush.ConflictForeground}" Text="{DynamicResource Text.InProgress.Revert.Head}"/>
                  <TextBlock FontWeight="Bold" Margin="4,0,0,0" Foreground="DarkOrange" Text="{Binding Head.SHA, Converter={x:Static c:StringConverters.ToShortSHA}}" ToolTip.Tip="{Binding Head}"/>
                </StackPanel>

                <Button Grid.Column="1" Classes="flat" FontWeight="Regular" BorderThickness="0" Content="{DynamicResource Text.Repository.Skip}" Padding="8,2" Click="OnSkipInProgress"/>
              </Grid>
            </DataTemplate>

            <DataTemplate DataType="vm:MergeInProgress">
              <StackPanel Orientation="Horizontal">
                <TextBlock FontWeight="Bold" Foreground="{DynamicResource Brush.ConflictForeground}" Text="{DynamicResource Text.InProgress.Merge}"/>
                <TextBlock FontWeight="Bold" Margin="4,0,0,0" Foreground="{DynamicResource Brush.ConflictForeground}" Text="{DynamicResource Text.InProgress.Merge.Operating}"/>
                <TextBlock FontWeight="Bold" Margin="4,0,0,0" Foreground="DarkOrange" Text="{Binding SourceName}" ToolTip.Tip="{Binding Source}"/>
                <TextBlock FontWeight="Bold" Margin="4,0,0,0" Foreground="{DynamicResource Brush.ConflictForeground}" Text="→"/>
                <TextBlock FontWeight="Bold" Margin="4,0,0,0" Foreground="DarkOrange" Text="{Binding Current}"/>
              </StackPanel>
            </DataTemplate>
          </ContentControl.DataTemplates>
        </ContentControl>

        <Button Grid.Column="1"
                Classes="flat"
                FontWeight="Regular"
                BorderThickness="0"
                Content="{DynamicResource Text.Repository.Abort}"
                Padding="8,2" Margin="0,0,8,0"
                Click="OnAbortInProgress"/>
      </Grid>

      <Grid Grid.Row="1"
            Height="28"
            ColumnDefinitions="*,Auto,Auto,Auto,Auto,Auto"
            Background="{DynamicResource Brush.Conflict}"
            IsVisible="{Binding BisectState, Converter={x:Static ObjectConverters.NotEqual}, ConverterParameter={x:Static m:BisectState.None}}">
        <TextBlock Grid.Column="0"
                   FontWeight="Bold"
                   Margin="8,0"
                   Foreground="{DynamicResource Brush.ConflictForeground}"
                   Text="{DynamicResource Text.Bisect.WaitingForRange}"
                   IsVisible="{Binding BisectState, Converter={x:Static ObjectConverters.Equal}, ConverterParameter={x:Static m:BisectState.WaitingForRange}}"/>

        <TextBlock Grid.Column="0"
                   FontWeight="Bold"
                   Margin="8,0"
                   Foreground="{DynamicResource Brush.ConflictForeground}"
                   Text="{DynamicResource Text.Bisect.Detecting}"
                   IsVisible="{Binding BisectState, Converter={x:Static ObjectConverters.Equal}, ConverterParameter={x:Static m:BisectState.Detecting}}"/>

        <v:LoadingIcon Grid.Column="1"
                       Width="14" Height="14"
                       Margin="4,0"
                       IsVisible="{Binding IsBisectCommandRunning}"/>

        <Button Grid.Column="2"
                Classes="flat"
                Margin="4,0"
                FontWeight="Regular"
                BorderThickness="0"
                Content="{DynamicResource Text.Bisect.Good}"
                IsEnabled="{Binding !IsBisectCommandRunning}"
                Padding="8,2"
                Click="OnBisectCommand"
                Tag="good"/>
        <Button Grid.Column="3"
                Classes="flat"
                Margin="4,0"
                FontWeight="Regular"
                BorderThickness="0"
                Content="{DynamicResource Text.Bisect.Bad}"
                IsEnabled="{Binding !IsBisectCommandRunning}"
                Padding="8,2"
                Click="OnBisectCommand"
                Tag="bad"/>
        <Button Grid.Column="4"
                Classes="flat"
                Margin="4,0"
                FontWeight="Regular"
                BorderThickness="0"
                Content="{DynamicResource Text.Bisect.Skip}"
                IsEnabled="{Binding !IsBisectCommandRunning}"
                Padding="8,2"
                Click="OnBisectCommand"
                Tag="skip"/>
        <Button Grid.Column="5"
                Classes="flat"
                Margin="4,0"
                FontWeight="Regular"
                BorderThickness="0"
                Content="{DynamicResource Text.Bisect.Abort}"
                IsEnabled="{Binding !IsBisectCommandRunning}"
                Padding="8,2"
                Click="OnBisectCommand"
                Tag="reset"/>
      </Grid>

      <Border Grid.Row="2" Background="{DynamicResource Brush.ToolBar}" BorderThickness="0,0,0,1" BorderBrush="{DynamicResource Brush.Border0}">
        <Border.IsVisible>
          <MultiBinding Converter="{x:Static BoolConverters.And}">
            <Binding Path="SelectedViewIndex" Converter="{x:Static c:IntConverters.IsZero}"/>
            <Binding Path="HistoryFilterCollection.Filters.Count" Converter="{x:Static c:IntConverters.IsGreaterThanZero}"/>
          </MultiBinding>
        </Border.IsVisible>

        <Grid ColumnDefinitions="Auto,*,Auto">
          <Path Grid.Column="0"
                Margin="8,0,0,0"
                Width="12" Height="12"
                Data="{StaticResource Icons.Filter}"
                Fill="{DynamicResource Brush.FG2}"
                IsVisible="{Binding HistoryFilterMode, Converter={x:Static ObjectConverters.Equal}, ConverterParameter={x:Static m:FilterMode.Included}}"/>
          <Path Grid.Column="0"
                Margin="8,0,0,0"
                Width="12" Height="12"
                Data="{StaticResource Icons.EyeClose}"
                Fill="{DynamicResource Brush.FG2}"
                IsVisible="{Binding HistoryFilterMode, Converter={x:Static ObjectConverters.Equal}, ConverterParameter={x:Static m:FilterMode.Excluded}}"/>

          <ItemsControl Grid.Column="1" Margin="8,4" ItemsSource="{Binding HistoryFilterCollection.Filters}">
            <ItemsControl.ItemsPanel>
              <ItemsPanelTemplate>
                <WrapPanel Orientation="Horizontal" ItemHeight="22"/>
              </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>

            <ItemsControl.ItemTemplate>
              <DataTemplate DataType="m:HistoryFilter">
                <Border Height="20"
                        Margin="0,0,6,0"
                        CornerRadius="12"
                        BorderThickness="1"
                        BorderBrush="{Binding Mode, Converter={x:Static c:FilterModeConverters.ToBorderBrush}}"
                        VerticalAlignment="Center">
                  <StackPanel Orientation="Horizontal" Margin="8,0">
                    <Path Width="10" Height="10" Data="{StaticResource Icons.Branch}" IsVisible="{Binding IsBranch}"/>
                    <Path Width="10" Height="10" Data="{StaticResource Icons.Tag}" IsVisible="{Binding !IsBranch}"/>
                    <TextBlock Text="{Binding Pattern, Converter={x:Static c:StringConverters.TrimRefsPrefix}}" Margin="4,0,8,0"/>

                    <Button Classes="icon_button" VerticalAlignment="Center" Margin="0" Padding="0" Click="OnRemoveSelectedHistoryFilter">
                      <Path Width="8" Height="8" Data="{StaticResource Icons.Close}"/>
                    </Button>
                  </StackPanel>
                </Border>
              </DataTemplate>
            </ItemsControl.ItemTemplate>
          </ItemsControl>

          <Button Grid.Column="2" Classes="icon_button" Command="{Binding ClearHistoryFilters}" ToolTip.Tip="{DynamicResource Text.Repository.ClearAllCommitsFilter}">
            <Path Width="14" Height="14" Margin="16,0,8,0" Data="{StaticResource Icons.RemoveAll}"/>
          </Button>
        </Grid>
      </Border>

      <ContentControl Grid.Row="3" Content="{Binding SelectedView}">
        <ContentControl.DataTemplates>
          <DataTemplate DataType="vm:Histories">
            <v:Histories CurrentBranch="{Binding $parent[v:Repository].((vm:Repository)DataContext).CurrentBranch}"
                         Bisect="{Binding Bisect}"
                         IssueTrackers="{Binding $parent[v:Repository].((vm:Repository)DataContext).IssueTrackers}"
                         OnlyHighlightCurrentBranch="{Binding $parent[v:Repository].((vm:Repository)DataContext).OnlyHighlightCurrentBranchInHistories}"
                         NavigationId="{Binding NavigationId}"/>
          </DataTemplate>

          <DataTemplate DataType="vm:WorkingCopy">
            <v:WorkingCopy/>
          </DataTemplate>

          <DataTemplate DataType="vm:StashesPage">
            <v:StashesPage/>
          </DataTemplate>
        </ContentControl.DataTemplates>
      </ContentControl>
    </Grid>

    <!-- Right (Auto-Fetch) -->
    <Border Grid.Column="2" Margin="12" HorizontalAlignment="Center" VerticalAlignment="Center" Effect="drop-shadow(0 0 12 #A0000000)" IsVisible="{Binding IsAutoFetching}">
      <Border Background="{DynamicResource Brush.Popup}" CornerRadius="6">
        <StackPanel Orientation="Vertical" Margin="16,8">
          <TextBlock Text="{DynamicResource Text.Repository.AutoFetching}"/>
          <ProgressBar Margin="0,8,0,0"
                       HorizontalAlignment="Stretch"
                       IsIndeterminate="{Binding IsAutoFetching}"
                       Background="{DynamicResource Brush.FG2}" Foreground="{DynamicResource Brush.Accent}"
                       Minimum="0" Maximum="100"/>
        </StackPanel>
      </Border>
    </Border>
  </Grid>
</UserControl>
