Skip to content

Commit 90aecc6

Browse files
committed
feature: branch filtering in Statistics window (#2532)
Signed-off-by: leo <longshuang@msn.cn>
1 parent 64d4116 commit 90aecc6

4 files changed

Lines changed: 150 additions & 81 deletions

File tree

‎src/Commands/Statistics.cs‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
11
using System.IO;
2+
using System.Text;
23
using System.Threading.Tasks;
34

45
namespace SourceGit.Commands
56
{
67
public class Statistics : Command
78
{
8-
public Statistics(string repo, int max)
9+
public Statistics(string repo, int max, Models.Branch specBranch)
910
{
1011
WorkingDirectory = repo;
1112
Context = repo;
12-
Args = $"log --date-order --branches --remotes -{max} --format=%ct$%aN±%aE";
13+
14+
var builder = new StringBuilder();
15+
builder
16+
.Append("log --date-order -")
17+
.Append(max)
18+
.Append(" --format=%ct$%aN±%aE ");
19+
20+
if (specBranch == null)
21+
builder.Append("--branches --remotes");
22+
else
23+
builder.Append(specBranch.FullName);
24+
25+
Args = builder.ToString();
1326
}
1427

1528
public async Task<Models.Statistics> ReadAsync()

‎src/ViewModels/Statistics.cs‎

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Threading.Tasks;
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
23
using Avalonia.Threading;
34
using CommunityToolkit.Mvvm.ComponentModel;
45

@@ -12,6 +13,22 @@ public bool IsLoading
1213
private set => SetProperty(ref _isLoading, value);
1314
}
1415

16+
public List<Models.Branch> Branches
17+
{
18+
get => _branches;
19+
private set => SetProperty(ref _branches, value);
20+
}
21+
22+
public Models.Branch SelectedBranch
23+
{
24+
get => _selectedBranch;
25+
set
26+
{
27+
if (SetProperty(ref _selectedBranch, value))
28+
LoadStatistics();
29+
}
30+
}
31+
1532
public Models.StatisticsMode ViewMode
1633
{
1734
get => _viewMode;
@@ -36,9 +53,39 @@ public Models.StatisticsSamples Samples
3653

3754
public Statistics(string repo)
3855
{
56+
_repo = repo;
57+
LoadBranches();
58+
LoadStatistics();
59+
}
60+
61+
public void ChangeAuthor(Models.StatisticsAuthor author)
62+
{
63+
if (SelectedReport == null)
64+
return;
65+
66+
Samples = SelectedReport.GetSamples(author);
67+
}
68+
69+
private void LoadBranches()
70+
{
71+
Task.Run(async () =>
72+
{
73+
var branches = await new Commands.QueryBranches(_repo)
74+
.GetResultAsync()
75+
.ConfigureAwait(false);
76+
77+
branches.Insert(0, _selectedBranch);
78+
Dispatcher.UIThread.Post(() => Branches = branches);
79+
});
80+
}
81+
82+
private void LoadStatistics()
83+
{
84+
IsLoading = true;
85+
3986
Task.Run(async () =>
4087
{
41-
var result = await new Commands.Statistics(repo, Preferences.Instance.MaxHistoryCommits)
88+
var result = await new Commands.Statistics(_repo, Preferences.Instance.MaxHistoryCommits, _selectedBranch)
4289
.ReadAsync()
4390
.ConfigureAwait(false);
4491

@@ -51,14 +98,6 @@ public Statistics(string repo)
5198
});
5299
}
53100

54-
public void ChangeAuthor(Models.StatisticsAuthor author)
55-
{
56-
if (SelectedReport == null)
57-
return;
58-
59-
Samples = SelectedReport.GetSamples(author);
60-
}
61-
62101
private void RefreshReport()
63102
{
64103
if (_data == null)
@@ -74,7 +113,10 @@ private void RefreshReport()
74113
Samples = SelectedReport.GetSamples(null);
75114
}
76115

116+
private string _repo = null;
77117
private bool _isLoading = true;
118+
private List<Models.Branch> _branches = new List<Models.Branch>();
119+
private Models.Branch _selectedBranch = new Models.Branch() { Name = "--- (All)", IsLocal = true, FullName = "", Head = "---" }; // Fake branch to represent all branches
78120
private Models.Statistics _data = null;
79121
private Models.StatisticsMode _viewMode = Models.StatisticsMode.All;
80122
private Models.StatisticsReport _selectedReport = null;

‎src/Views/BranchSelector.axaml‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,17 @@
1414
<Border/>
1515
</FocusAdornerTemplate>
1616
</Setter>
17+
18+
<Setter Property="Background" Value="{DynamicResource Brush.Contents}"/>
19+
<Setter Property="BorderBrush" Value="{DynamicResource Brush.Border1}"/>
1720

1821
<Setter Property="Template">
1922
<ControlTemplate>
2023
<Grid Background="Transparent">
2124
<Border x:Name="PART_Background"
22-
Background="{DynamicResource Brush.Contents}"
25+
Background="{TemplateBinding Background}"
2326
BorderThickness="1"
24-
BorderBrush="{DynamicResource Brush.Border1}"
27+
BorderBrush="{TemplateBinding BorderBrush}"
2528
CornerRadius="3"/>
2629

2730
<Grid x:Name="PART_Selected"

‎src/Views/Statistics.axaml‎

Lines changed: 78 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -117,74 +117,85 @@
117117
<!-- Contents -->
118118
<Grid Grid.Row="2" Margin="8" ColumnDefinitions="256,*">
119119
<!-- Left -->
120-
<ContentControl Grid.Column="0" Content="{Binding SelectedReport, Mode=OneWay}">
121-
<ContentControl.DataTemplates>
122-
<DataTemplate DataType="m:StatisticsReport">
123-
<Grid RowDefinitions="*,16">
124-
<!-- Table of author -->
125-
<ListBox Grid.Column="0"
126-
ItemsSource="{Binding Authors}"
127-
SelectionMode="Single"
128-
BorderThickness="1"
129-
BorderBrush="{DynamicResource Brush.Border2}"
130-
Background="{DynamicResource Brush.Contents}"
131-
SelectionChanged="OnAuthorSelectionChanged"
132-
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
133-
ScrollViewer.VerticalScrollBarVisibility="Auto">
134-
<ListBox.Styles>
135-
<Style Selector="ListBoxItem">
136-
<Setter Property="Margin" Value="0"/>
137-
<Setter Property="Padding" Value="0"/>
138-
<Setter Property="Height" Value="26"/>
139-
</Style>
140-
</ListBox.Styles>
141-
142-
<ListBox.ItemsPanel>
143-
<ItemsPanelTemplate>
144-
<VirtualizingStackPanel Orientation="Vertical"/>
145-
</ItemsPanelTemplate>
146-
</ListBox.ItemsPanel>
147-
148-
<ListBox.ItemTemplate>
149-
<DataTemplate DataType="m:StatisticsAuthor">
150-
<Border BorderThickness="0,0,0,1" BorderBrush="{DynamicResource Brush.Border2}"
151-
Background="Transparent"
152-
ToolTip.Tip="{Binding User}">
153-
<Grid ColumnDefinitions="26,*,72">
154-
<v:Avatar Grid.Column="0"
155-
Width="16" Height="16"
156-
HorizontalAlignment="Center" VerticalAlignment="Center"
157-
User="{Binding User}"/>
158-
<Border Grid.Column="1" Padding="4,0,8,0" ClipToBounds="True">
159-
<TextBlock Text="{Binding User.Name}" HorizontalAlignment="Left"/>
160-
</Border>
161-
<Border Grid.Column="2" Padding="8,0" ClipToBounds="True">
162-
<TextBlock Text="{Binding Count}" HorizontalAlignment="Right"/>
163-
</Border>
164-
</Grid>
165-
</Border>
166-
</DataTemplate>
167-
</ListBox.ItemTemplate>
168-
</ListBox>
169-
170-
<!-- Summary -->
171-
<Grid Grid.Row="1" ColumnDefinitions="*,*">
172-
<!-- Total Authors -->
173-
<StackPanel Grid.Column="0" Orientation="Horizontal" VerticalAlignment="Bottom">
174-
<TextBlock Text="{DynamicResource Text.Statistics.TotalAuthors}" FontSize="11" Foreground="{DynamicResource Brush.FG2}"/>
175-
<TextBlock Text="{Binding Authors.Count}" FontSize="11" Margin="4,0,0,0"/>
176-
</StackPanel>
177-
178-
<!-- Total Commits -->
179-
<StackPanel Grid.Column="1" Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Bottom">
180-
<TextBlock Text="{DynamicResource Text.Statistics.TotalCommits}" FontSize="11" Foreground="{DynamicResource Brush.FG2}"/>
181-
<TextBlock Text="{Binding Total}" FontSize="11" Margin="4,0,0,0"/>
182-
</StackPanel>
120+
<Grid Grid.Column="0" RowDefinitions="36,*">
121+
<v:BranchSelector Grid.Row="0"
122+
Height="28"
123+
VerticalAlignment="Center" HorizontalAlignment="Stretch"
124+
BorderBrush="{DynamicResource Brush.Border2}"
125+
Branches="{Binding Branches, Mode=OneWay}"
126+
SelectedBranch="{Binding SelectedBranch, Mode=TwoWay}"
127+
UsePureName="False"/>
128+
129+
<ContentControl Grid.Row="1" Content="{Binding SelectedReport, Mode=OneWay}">
130+
<ContentControl.DataTemplates>
131+
<DataTemplate DataType="m:StatisticsReport">
132+
<Grid RowDefinitions="*,24">
133+
<!-- Table of author -->
134+
<ListBox Grid.Column="0"
135+
ItemsSource="{Binding Authors}"
136+
SelectionMode="Single"
137+
BorderThickness="1"
138+
BorderBrush="{DynamicResource Brush.Border2}"
139+
Background="{DynamicResource Brush.Contents}"
140+
CornerRadius="3"
141+
SelectionChanged="OnAuthorSelectionChanged"
142+
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
143+
ScrollViewer.VerticalScrollBarVisibility="Auto">
144+
<ListBox.Styles>
145+
<Style Selector="ListBoxItem">
146+
<Setter Property="Margin" Value="0"/>
147+
<Setter Property="Padding" Value="0"/>
148+
<Setter Property="Height" Value="26"/>
149+
</Style>
150+
</ListBox.Styles>
151+
152+
<ListBox.ItemsPanel>
153+
<ItemsPanelTemplate>
154+
<VirtualizingStackPanel Orientation="Vertical"/>
155+
</ItemsPanelTemplate>
156+
</ListBox.ItemsPanel>
157+
158+
<ListBox.ItemTemplate>
159+
<DataTemplate DataType="m:StatisticsAuthor">
160+
<Border BorderThickness="0,0,0,1" BorderBrush="{DynamicResource Brush.Border2}"
161+
Background="Transparent"
162+
ToolTip.Tip="{Binding User}">
163+
<Grid ColumnDefinitions="26,*,72">
164+
<v:Avatar Grid.Column="0"
165+
Width="16" Height="16"
166+
HorizontalAlignment="Center" VerticalAlignment="Center"
167+
User="{Binding User}"/>
168+
<Border Grid.Column="1" Padding="4,0,8,0" ClipToBounds="True">
169+
<TextBlock Text="{Binding User.Name}" HorizontalAlignment="Left"/>
170+
</Border>
171+
<Border Grid.Column="2" Padding="8,0" ClipToBounds="True">
172+
<TextBlock Text="{Binding Count}" HorizontalAlignment="Right"/>
173+
</Border>
174+
</Grid>
175+
</Border>
176+
</DataTemplate>
177+
</ListBox.ItemTemplate>
178+
</ListBox>
179+
180+
<!-- Summary -->
181+
<Grid Grid.Row="1" ColumnDefinitions="*,*" Margin="0,2,0,0">
182+
<!-- Total Authors -->
183+
<StackPanel Grid.Column="0" Orientation="Horizontal" VerticalAlignment="Bottom">
184+
<TextBlock Text="{DynamicResource Text.Statistics.TotalAuthors}" FontSize="11" Foreground="{DynamicResource Brush.FG2}"/>
185+
<TextBlock Text="{Binding Authors.Count}" FontSize="11" Margin="4,0,0,0"/>
186+
</StackPanel>
187+
188+
<!-- Total Commits -->
189+
<StackPanel Grid.Column="1" Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Bottom">
190+
<TextBlock Text="{DynamicResource Text.Statistics.TotalCommits}" FontSize="11" Foreground="{DynamicResource Brush.FG2}"/>
191+
<TextBlock Text="{Binding Total}" FontSize="11" Margin="4,0,0,0"/>
192+
</StackPanel>
193+
</Grid>
183194
</Grid>
184-
</Grid>
185-
</DataTemplate>
186-
</ContentControl.DataTemplates>
187-
</ContentControl>
195+
</DataTemplate>
196+
</ContentControl.DataTemplates>
197+
</ContentControl>
198+
</Grid>
188199

189200
<!-- Right -->
190201
<v:Chart Grid.Column="1"

0 commit comments

Comments
 (0)