Skip to content

Commit 521159b

Browse files
committed
enhance: update subject length status after its guide length has been changed from Preferences dialog
Signed-off-by: leo <longshuang@msn.cn>
1 parent ea6f856 commit 521159b

File tree

5 files changed

+71
-34
lines changed

5 files changed

+71
-34
lines changed

src/Converters/BoolConverters.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
using Avalonia.Data.Converters;
1+
using Avalonia;
2+
using Avalonia.Controls;
3+
using Avalonia.Data.Converters;
24
using Avalonia.Media;
35

46
namespace SourceGit.Converters
57
{
68
public static class BoolConverters
79
{
810
public static readonly FuncValueConverter<bool, FontWeight> IsBoldToFontWeight =
9-
new FuncValueConverter<bool, FontWeight>(x => x ? FontWeight.Bold : FontWeight.Regular);
11+
new(x => x ? FontWeight.Bold : FontWeight.Regular);
1012

1113
public static readonly FuncValueConverter<bool, double> IsMergedToOpacity =
12-
new FuncValueConverter<bool, double>(x => x ? 1 : 0.65);
14+
new(x => x ? 1 : 0.65);
15+
16+
public static readonly FuncValueConverter<bool, IBrush> IsWarningToBrush =
17+
new(x => x ? Brushes.DarkGoldenrod : Application.Current?.FindResource("Brush.FG1") as IBrush);
1318
}
1419
}

src/Converters/IntConverters.cs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,24 @@ namespace SourceGit.Converters
88
public static class IntConverters
99
{
1010
public static readonly FuncValueConverter<int, bool> IsGreaterThanZero =
11-
new FuncValueConverter<int, bool>(v => v > 0);
11+
new(v => v > 0);
1212

1313
public static readonly FuncValueConverter<int, bool> IsGreaterThanFour =
14-
new FuncValueConverter<int, bool>(v => v > 4);
14+
new(v => v > 4);
1515

1616
public static readonly FuncValueConverter<int, bool> IsZero =
17-
new FuncValueConverter<int, bool>(v => v == 0);
18-
19-
public static readonly FuncValueConverter<int, bool> IsOne =
20-
new FuncValueConverter<int, bool>(v => v == 1);
17+
new(v => v == 0);
2118

2219
public static readonly FuncValueConverter<int, bool> IsNotOne =
23-
new FuncValueConverter<int, bool>(v => v != 1);
24-
25-
public static readonly FuncValueConverter<int, bool> IsSubjectLengthBad =
26-
new FuncValueConverter<int, bool>(v => v > ViewModels.Preferences.Instance.SubjectGuideLength);
27-
28-
public static readonly FuncValueConverter<int, bool> IsSubjectLengthGood =
29-
new FuncValueConverter<int, bool>(v => v <= ViewModels.Preferences.Instance.SubjectGuideLength);
20+
new(v => v != 1);
3021

3122
public static readonly FuncValueConverter<int, Thickness> ToTreeMargin =
32-
new FuncValueConverter<int, Thickness>(v => new Thickness(v * 16, 0, 0, 0));
23+
new(v => new Thickness(v * 16, 0, 0, 0));
3324

3425
public static readonly FuncValueConverter<int, IBrush> ToBookmarkBrush =
35-
new FuncValueConverter<int, IBrush>(v => Models.Bookmarks.Get(v) ?? App.Current?.FindResource("Brush.FG1") as IBrush);
26+
new(v => Models.Bookmarks.Get(v) ?? Application.Current?.FindResource("Brush.FG1") as IBrush);
3627

3728
public static readonly FuncValueConverter<int, string> ToUnsolvedDesc =
38-
new FuncValueConverter<int, string>(v => v == 0 ? App.Text("MergeConflictEditor.AllResolved") : App.Text("MergeConflictEditor.ConflictsRemaining", v));
29+
new(v => v == 0 ? App.Text("MergeConflictEditor.AllResolved") : App.Text("MergeConflictEditor.ConflictsRemaining", v));
3930
}
4031
}

src/ViewModels/Repository.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1766,7 +1766,14 @@ private BranchTreeNode FindBranchNode(List<BranchTreeNode> nodes, string path)
17661766

17671767
private void AutoFetchByTimer(object sender)
17681768
{
1769-
Dispatcher.UIThread.Invoke(AutoFetchOnUIThread);
1769+
try
1770+
{
1771+
Dispatcher.UIThread.Invoke(AutoFetchOnUIThread);
1772+
}
1773+
catch
1774+
{
1775+
// Ignore exception.
1776+
}
17701777
}
17711778

17721779
private async Task AutoFetchOnUIThread()

src/Views/CommitMessageToolBox.axaml

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
CommitMessage="{Binding #ThisControl.CommitMessage, Mode=TwoWay}"
1919
Placeholder="{DynamicResource Text.CommitMessageTextBox.Placeholder}"
2020
SubjectLineBrush="{DynamicResource Brush.Border2}"
21+
SubjectGuideLength="{Binding Source={x:Static vm:Preferences.Instance}, Path=SubjectGuideLength, Mode=OneWay}"
2122
Foreground="{DynamicResource Brush.FG1}"
2223
FontFamily="{DynamicResource Fonts.Default}"
23-
FontSize="{Binding Source={x:Static vm:Preferences.Instance}, Path=EditorFontSize}"
24+
FontSize="{Binding Source={x:Static vm:Preferences.Instance}, Path=EditorFontSize, Mode=OneWay}"
2425
Tag="{Binding Source={x:Static v:StealHotKey.Enter}}"/>
2526

2627
<Rectangle Grid.Row="1"
@@ -69,12 +70,22 @@
6970
HorizontalAlignment="Right"
7071
VerticalAlignment="Center"
7172
Orientation="Horizontal">
72-
<TextBlock Classes="info_label" FontSize="13" HorizontalAlignment="Left" Text="{DynamicResource Text.CommitMessageTextBox.SubjectCount}"/>
73-
<TextBlock Margin="8,0,0,0" FontSize="11" Text="{Binding #Editor.SubjectLength}" IsVisible="{Binding #Editor.SubjectLength, Converter={x:Static c:IntConverters.IsSubjectLengthGood}}" VerticalAlignment="Center"/>
74-
<TextBlock Margin="8,0,0,0" FontSize="11" Foreground="DarkGoldenrod" Text="{Binding #Editor.SubjectLength}" IsVisible="{Binding #Editor.SubjectLength, Converter={x:Static c:IntConverters.IsSubjectLengthBad}}" VerticalAlignment="Center"/>
75-
<TextBlock FontSize="11" Text="/" VerticalAlignment="Center"/>
76-
<TextBlock FontSize="11" Text="{Binding Source={x:Static vm:Preferences.Instance}, Path=SubjectGuideLength}" VerticalAlignment="Center"/>
77-
<Path Width="10" Height="10" Margin="4,0,0,0" Data="{StaticResource Icons.Error}" Fill="DarkGoldenrod" IsVisible="{Binding #Editor.SubjectLength, Converter={x:Static c:IntConverters.IsSubjectLengthBad}}"/>
73+
<TextBlock Classes="info_label"
74+
FontSize="13"
75+
HorizontalAlignment="Left"
76+
Text="{DynamicResource Text.CommitMessageTextBox.SubjectCount}"/>
77+
<TextBlock Margin="8,0,0,0"
78+
FontSize="11"
79+
Text="{Binding #Editor.SubjectLength}"
80+
Foreground="{Binding #Editor.IsSubjectWarningIconVisible, Converter={x:Static c:BoolConverters.IsWarningToBrush}, Mode=OneWay}"
81+
VerticalAlignment="Center"/>
82+
<TextBlock FontSize="11"
83+
Text="/"
84+
VerticalAlignment="Center"/>
85+
<TextBlock FontSize="11"
86+
Text="{Binding #Editor.SubjectGuideLength, Mode=OneWay}"
87+
VerticalAlignment="Center"/>
88+
<Path Width="10" Height="10" Margin="4,0,0,0" Data="{StaticResource Icons.Error}" Fill="DarkGoldenrod" IsVisible="{Binding #Editor.IsSubjectWarningIconVisible, Mode=OneWay}"/>
7889
</StackPanel>
7990
</Grid>
8091
</Border>

src/Views/CommitMessageToolBox.axaml.cs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,32 @@ public string Placeholder
7878
}
7979

8080
public static readonly StyledProperty<int> SubjectLengthProperty =
81-
AvaloniaProperty.Register<CommitMessageTextEditor, int>(nameof(SubjectLength), 0);
81+
AvaloniaProperty.Register<CommitMessageTextEditor, int>(nameof(SubjectLength));
8282

8383
public int SubjectLength
8484
{
8585
get => GetValue(SubjectLengthProperty);
8686
set => SetValue(SubjectLengthProperty, value);
8787
}
8888

89+
public static readonly StyledProperty<int> SubjectGuideLengthProperty =
90+
AvaloniaProperty.Register<CommitMessageTextEditor, int>(nameof(SubjectGuideLength));
91+
92+
public int SubjectGuideLength
93+
{
94+
get => GetValue(SubjectGuideLengthProperty);
95+
set => SetValue(SubjectGuideLengthProperty, value);
96+
}
97+
98+
public static readonly StyledProperty<bool> IsSubjectWarningIconVisibleProperty =
99+
AvaloniaProperty.Register<CommitMessageTextEditor, bool>(nameof(IsSubjectWarningIconVisible));
100+
101+
public bool IsSubjectWarningIconVisible
102+
{
103+
get => GetValue(IsSubjectWarningIconVisibleProperty);
104+
set => SetValue(IsSubjectWarningIconVisibleProperty, value);
105+
}
106+
89107
public static readonly StyledProperty<IBrush> SubjectLineBrushProperty =
90108
AvaloniaProperty.Register<CommitMessageTextEditor, IBrush>(nameof(SubjectLineBrush), Brushes.Gray);
91109

@@ -230,6 +248,11 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
230248
if (string.IsNullOrWhiteSpace(CommitMessage))
231249
InvalidateVisual();
232250
}
251+
else if (change.Property == SubjectLengthProperty ||
252+
change.Property == SubjectGuideLengthProperty)
253+
{
254+
SetCurrentValue(IsSubjectWarningIconVisibleProperty, SubjectLength > SubjectGuideLength);
255+
}
233256
}
234257

235258
protected override void OnTextChanged(EventArgs e)
@@ -274,7 +297,7 @@ protected override void OnTextChanged(EventArgs e)
274297
if (_completionWnd == null)
275298
{
276299
_completionWnd = new CompletionWindow(TextArea);
277-
_completionWnd.Closed += (_, ev) => _completionWnd = null;
300+
_completionWnd.Closed += (_, __) => _completionWnd = null;
278301
_completionWnd.Show();
279302
}
280303

@@ -298,7 +321,7 @@ private void OnTextViewContextRequested(object sender, ContextRequestedEventArgs
298321
copy.Header = App.Text("Copy");
299322
copy.Icon = App.CreateMenuIcon("Icons.Copy");
300323
copy.IsEnabled = hasSelected;
301-
copy.Click += (o, ev) =>
324+
copy.Click += (_, ev) =>
302325
{
303326
Copy();
304327
ev.Handled = true;
@@ -308,7 +331,7 @@ private void OnTextViewContextRequested(object sender, ContextRequestedEventArgs
308331
cut.Header = App.Text("Cut");
309332
cut.Icon = App.CreateMenuIcon("Icons.Cut");
310333
cut.IsEnabled = hasSelected;
311-
cut.Click += (o, ev) =>
334+
cut.Click += (_, ev) =>
312335
{
313336
Cut();
314337
ev.Handled = true;
@@ -317,7 +340,7 @@ private void OnTextViewContextRequested(object sender, ContextRequestedEventArgs
317340
var paste = new MenuItem();
318341
paste.Header = App.Text("Paste");
319342
paste.Icon = App.CreateMenuIcon("Icons.Paste");
320-
paste.Click += (o, ev) =>
343+
paste.Click += (_, ev) =>
321344
{
322345
Paste();
323346
ev.Handled = true;
@@ -495,7 +518,7 @@ private async void OnOpenCommitMessagePicker(object sender, RoutedEventArgs e)
495518

496519
button.IsEnabled = false;
497520
menu.Placement = PlacementMode.TopEdgeAlignedLeft;
498-
menu.Closed += (o, ev) => button.IsEnabled = true;
521+
menu.Closed += (_, _) => button.IsEnabled = true;
499522
menu.Open(button);
500523
}
501524

@@ -544,7 +567,7 @@ private async void OnOpenOpenAIHelper(object sender, RoutedEventArgs e)
544567

545568
button.IsEnabled = false;
546569
menu.Placement = PlacementMode.TopEdgeAlignedLeft;
547-
menu.Closed += (o, ev) => button.IsEnabled = true;
570+
menu.Closed += (_, _) => button.IsEnabled = true;
548571
menu.Open(button);
549572
}
550573

0 commit comments

Comments
 (0)