feature: supports to search stashes (#213)

This commit is contained in:
leo 2024-06-28 18:57:56 +08:00
parent 3e54ab0227
commit aee6df4132
No known key found for this signature in database
3 changed files with 100 additions and 40 deletions

View file

@ -169,7 +169,7 @@ namespace SourceGit.ViewModels
[JsonIgnore] [JsonIgnore]
public int StashesCount public int StashesCount
{ {
get => _stashesPage == null ? 0 : _stashesPage.Count; get => _stashesPage == null ? 0 : _stashesPage.Stashes.Count;
} }
[JsonIgnore] [JsonIgnore]

View file

@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Avalonia.Controls; using Avalonia.Controls;
@ -10,20 +11,33 @@ namespace SourceGit.ViewModels
{ {
public class StashesPage : ObservableObject public class StashesPage : ObservableObject
{ {
public int Count
{
get => _stashes == null ? 0 : _stashes.Count;
}
public List<Models.Stash> Stashes public List<Models.Stash> Stashes
{ {
get => _stashes; get => _stashes;
set set
{ {
if (SetProperty(ref _stashes, value)) if (SetProperty(ref _stashes, value))
{ RefreshVisible();
}
}
public List<Models.Stash> VisibleStashes
{
get => _visibleStashes;
private set
{
if (SetProperty(ref _visibleStashes, value))
SelectedStash = null; SelectedStash = null;
} }
}
public string SearchFilter
{
get => _searchFilter;
set
{
if (SetProperty(ref _searchFilter, value))
RefreshVisible();
} }
} }
@ -43,10 +57,7 @@ namespace SourceGit.ViewModels
Task.Run(() => Task.Run(() =>
{ {
var changes = new Commands.QueryStashChanges(_repo.FullPath, value.SHA).Result(); var changes = new Commands.QueryStashChanges(_repo.FullPath, value.SHA).Result();
Dispatcher.UIThread.Invoke(() => Dispatcher.UIThread.Invoke(() => Changes = changes);
{
Changes = changes;
});
}); });
} }
} }
@ -59,9 +70,7 @@ namespace SourceGit.ViewModels
private set private set
{ {
if (SetProperty(ref _changes, value)) if (SetProperty(ref _changes, value))
{
SelectedChange = null; SelectedChange = null;
}
} }
} }
@ -73,13 +82,9 @@ namespace SourceGit.ViewModels
if (SetProperty(ref _selectedChange, value)) if (SetProperty(ref _selectedChange, value))
{ {
if (value == null) if (value == null)
{
DiffContext = null; DiffContext = null;
}
else else
{
DiffContext = new DiffContext(_repo.FullPath, new Models.DiffOption($"{_selectedStash.SHA}^", _selectedStash.SHA, value), _diffContext); DiffContext = new DiffContext(_repo.FullPath, new Models.DiffOption($"{_selectedStash.SHA}^", _selectedStash.SHA, value), _diffContext);
}
} }
} }
} }
@ -148,13 +153,37 @@ namespace SourceGit.ViewModels
public void Clear() public void Clear()
{ {
if (PopupHost.CanCreatePopup()) if (PopupHost.CanCreatePopup())
{
PopupHost.ShowPopup(new ClearStashes(_repo)); PopupHost.ShowPopup(new ClearStashes(_repo));
}
public void ClearSearchFilter()
{
SearchFilter = string.Empty;
}
private void RefreshVisible()
{
if (string.IsNullOrEmpty(_searchFilter))
{
VisibleStashes = _stashes;
}
else
{
var visible = new List<Models.Stash>();
foreach (var s in _stashes)
{
if (s.Message.Contains(_searchFilter, StringComparison.OrdinalIgnoreCase))
visible.Add(s);
}
VisibleStashes = visible;
} }
} }
private Repository _repo = null; private Repository _repo = null;
private List<Models.Stash> _stashes = null; private List<Models.Stash> _stashes = new List<Models.Stash>();
private List<Models.Stash> _visibleStashes = new List<Models.Stash>();
private string _searchFilter = string.Empty;
private Models.Stash _selectedStash = null; private Models.Stash _selectedStash = null;
private List<Models.Change> _changes = null; private List<Models.Change> _changes = null;
private Models.Change _selectedChange = null; private Models.Change _selectedChange = null;

View file

@ -17,28 +17,59 @@
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<!-- Left --> <!-- Left -->
<Grid Grid.Column="0" RowDefinitions="28,*,28,*"> <Grid Grid.Column="0" RowDefinitions="28,36,*,28,*">
<!-- Stash Bar --> <!-- Stash Bar -->
<Border Grid.Row="0" BorderThickness="0,0,0,1" BorderBrush="{DynamicResource Brush.Border0}"> <Grid Grid.Row="0" ColumnDefinitions="Auto,Auto,Auto,*,Auto">
<Grid ColumnDefinitions="Auto,Auto,Auto,*,Auto"> <Path Grid.Column="0" Margin="8,0,0,0" Width="14" Height="14" Fill="{DynamicResource Brush.FG2}" Data="{StaticResource Icons.Stashes}"/>
<Path Grid.Column="0" Margin="8,0,0,0" Width="14" Height="14" Fill="{DynamicResource Brush.FG2}" Data="{StaticResource Icons.Stashes}"/> <TextBlock Grid.Column="1" Text="{DynamicResource Text.Stashes.Stashes}" Foreground="{DynamicResource Brush.FG2}" FontWeight="Bold" Margin="8,0,0,0"/>
<TextBlock Grid.Column="1" Text="{DynamicResource Text.Stashes.Stashes}" Foreground="{DynamicResource Brush.FG2}" FontWeight="Bold" Margin="8,0,0,0"/> <TextBlock Grid.Column="2" Text="{Binding Stashes, Converter={x:Static c:ListConverters.ToCount}}" Foreground="{DynamicResource Brush.FG2}" FontWeight="Bold"/>
<TextBlock Grid.Column="2" Text="{Binding Stashes, Converter={x:Static c:ListConverters.ToCount}}" Foreground="{DynamicResource Brush.FG2}" FontWeight="Bold"/> <Button Grid.Column="4"
<Button Grid.Column="4" Classes="icon_button"
Classes="icon_button" Width="26" Height="14"
Width="26" Height="14" Padding="0"
Padding="0" Command="{Binding Clear}"
Command="{Binding Clear}" IsEnabled="{Binding Stashes.Count, Converter={x:Static c:IntConverters.IsGreaterThanZero}}">
IsEnabled="{Binding Stashes.Count, Converter={x:Static c:IntConverters.IsGreaterThanZero}}"> <Path Width="14" Height="14" Data="{StaticResource Icons.Clean}"/>
<Path Width="14" Height="14" Data="{StaticResource Icons.Clean}"/> </Button>
</Button> </Grid>
</Grid>
<!-- Search Bar -->
<Border Grid.Row="1" BorderThickness="0,1" BorderBrush="{DynamicResource Brush.Border0}">
<TextBox Grid.Row="1"
Height="24"
Margin="4,0"
BorderThickness="1"
CornerRadius="12"
Text="{Binding SearchFilter, Mode=TwoWay}"
BorderBrush="{DynamicResource Brush.Border2}"
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 ClearSearchFilter}"
IsVisible="{Binding SearchFilter, 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>
</Border> </Border>
<!-- Stash List --> <!-- Stash List -->
<DataGrid Grid.Row="1" <DataGrid Grid.Row="2"
Background="{DynamicResource Brush.Contents}" Background="{DynamicResource Brush.Contents}"
ItemsSource="{Binding Stashes}" ItemsSource="{Binding VisibleStashes}"
SelectedItem="{Binding SelectedStash, Mode=TwoWay}" SelectedItem="{Binding SelectedStash, Mode=TwoWay}"
SelectionMode="Single" SelectionMode="Single"
CanUserReorderColumns="False" CanUserReorderColumns="False"
@ -71,7 +102,7 @@
</DataGrid> </DataGrid>
<!-- Changes Bar --> <!-- Changes Bar -->
<Border Grid.Row="2" BorderThickness="0,1" BorderBrush="{DynamicResource Brush.Border0}"> <Border Grid.Row="3" BorderThickness="0,1" BorderBrush="{DynamicResource Brush.Border0}">
<Grid ColumnDefinitions="Auto,Auto,*"> <Grid ColumnDefinitions="Auto,Auto,*">
<Path Grid.Column="0" Margin="8,0,0,0" Width="14" Height="14" Fill="{DynamicResource Brush.FG2}" Data="{StaticResource Icons.File}"/> <Path Grid.Column="0" Margin="8,0,0,0" Width="14" Height="14" Fill="{DynamicResource Brush.FG2}" Data="{StaticResource Icons.File}"/>
<TextBlock Grid.Column="1" Text="{DynamicResource Text.Stashes.Changes}" Foreground="{DynamicResource Brush.FG2}" FontWeight="Bold" Margin="8,0,0,0"/> <TextBlock Grid.Column="1" Text="{DynamicResource Text.Stashes.Changes}" Foreground="{DynamicResource Brush.FG2}" FontWeight="Bold" Margin="8,0,0,0"/>
@ -80,7 +111,7 @@
</Border> </Border>
<!-- View Changes --> <!-- View Changes -->
<DataGrid Grid.Row="3" <DataGrid Grid.Row="4"
Background="{DynamicResource Brush.Contents}" Background="{DynamicResource Brush.Contents}"
ItemsSource="{Binding Changes}" ItemsSource="{Binding Changes}"
SelectedItem="{Binding SelectedChange, Mode=TwoWay}" SelectedItem="{Binding SelectedChange, Mode=TwoWay}"