Skip to content

Commit 4cb9b6a

Browse files
committed
enhance: avoid crashing due to invalid bookmark value in preferences.json
Signed-off-by: leo <[email protected]>
1 parent 8af75be commit 4cb9b6a

File tree

6 files changed

+26
-34
lines changed

6 files changed

+26
-34
lines changed

src/Converters/IntConverters.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@ public static class IntConverters
3232
new FuncValueConverter<int, Thickness>(v => new Thickness(v * 16, 0, 0, 0));
3333

3434
public static readonly FuncValueConverter<int, IBrush> ToBookmarkBrush =
35-
new FuncValueConverter<int, IBrush>(bookmark =>
36-
{
37-
if (bookmark == 0)
38-
return Application.Current?.FindResource("Brush.FG1") as IBrush;
39-
else
40-
return Models.Bookmarks.Brushes[bookmark];
41-
});
35+
new FuncValueConverter<int, IBrush>(v => Models.Bookmarks.Get(v) ?? App.Current?.FindResource("Brush.FG1") as IBrush);
4236
}
4337
}

src/Models/Bookmarks.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
using System.Collections.Generic;
2-
3-
namespace SourceGit.Models
1+
namespace SourceGit.Models
42
{
53
public static class Bookmarks
64
{
75
public static readonly Avalonia.Media.IBrush[] Brushes = [
8-
Avalonia.Media.Brushes.Transparent,
6+
null,
97
Avalonia.Media.Brushes.Red,
108
Avalonia.Media.Brushes.Orange,
119
Avalonia.Media.Brushes.Gold,
@@ -15,12 +13,9 @@ public static class Bookmarks
1513
Avalonia.Media.Brushes.Purple,
1614
];
1715

18-
public static readonly List<int> Supported = new List<int>();
19-
20-
static Bookmarks()
16+
public static Avalonia.Media.IBrush Get(int i)
2117
{
22-
for (int i = 0; i < Brushes.Length; i++)
23-
Supported.Add(i);
18+
return (i >= 0 && i < Brushes.Length) ? Brushes[i] : null;
2419
}
2520
}
2621
}

src/Models/Branch.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,9 @@ public class Branch
2424
public bool IsUpstreamGone { get; set; }
2525
public string WorktreePath { get; set; }
2626

27-
public bool IsTrackStatusVisible
28-
{
29-
get
30-
{
31-
return Ahead.Count + Behind.Count > 0;
32-
}
33-
}
27+
public bool HasWorktree => !IsCurrent && !string.IsNullOrEmpty(WorktreePath);
28+
public string FriendlyName => IsLocal ? Name : $"{Remote}/{Name}";
29+
public bool IsTrackStatusVisible => Ahead.Count + Behind.Count > 0;
3430

3531
public string TrackStatusDescription
3632
{
@@ -44,8 +40,5 @@ public string TrackStatusDescription
4440
return behind > 0 ? $"{behind}↓" : string.Empty;
4541
}
4642
}
47-
48-
public bool HasWorktree => !IsCurrent && !string.IsNullOrEmpty(WorktreePath);
49-
public string FriendlyName => IsLocal ? Name : $"{Remote}/{Name}";
5043
}
5144
}

src/ViewModels/EditRepositoryNode.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.ComponentModel.DataAnnotations;
1+
using System.Collections.Generic;
2+
using System.ComponentModel.DataAnnotations;
23
using System.Threading.Tasks;
34

45
namespace SourceGit.ViewModels
@@ -18,6 +19,11 @@ public string Name
1819
set => SetProperty(ref _name, value, true);
1920
}
2021

22+
public List<int> Bookmarks
23+
{
24+
get;
25+
}
26+
2127
public int Bookmark
2228
{
2329
get => _bookmark;
@@ -37,6 +43,10 @@ public EditRepositoryNode(RepositoryNode node)
3743
_name = node.Name;
3844
_isRepository = node.IsRepository;
3945
_bookmark = node.Bookmark;
46+
47+
Bookmarks = new List<int>();
48+
for (var i = 0; i < Models.Bookmarks.Brushes.Length; i++)
49+
Bookmarks.Add(i);
4050
}
4151

4252
public override Task<bool> Sure()

src/Views/EditRepositoryNode.axaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@
3434
<ComboBox Grid.Column="1"
3535
BorderThickness="0"
3636
Height="28"
37-
ItemsSource="{x:Static m:Bookmarks.Supported}"
37+
ItemsSource="{Binding Bookmarks}"
3838
SelectedIndex="{Binding Bookmark, Mode=TwoWay}">
3939
<ComboBox.ItemTemplate>
4040
<DataTemplate>
41-
<Border Height="20" VerticalAlignment="Center">
41+
<Grid Height="20">
4242
<Path Width="12" Height="12"
4343
Fill="{Binding Converter={x:Static c:IntConverters.ToBookmarkBrush}}"
4444
HorizontalAlignment="Center" VerticalAlignment="Center"
4545
Data="{StaticResource Icons.Bookmark}"/>
46-
</Border>
46+
</Grid>
4747
</DataTemplate>
4848
</ComboBox.ItemTemplate>
4949
</ComboBox>

src/Views/LauncherTabBar.axaml.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,12 @@ private void OnTabContextRequested(object sender, ContextRequestedEventArgs e)
269269
bookmark.Header = App.Text("PageTabBar.Tab.Bookmark");
270270
bookmark.Icon = App.CreateMenuIcon("Icons.Bookmark");
271271

272-
for (int i = 0; i < Models.Bookmarks.Supported.Count; i++)
272+
for (int i = 0; i < Models.Bookmarks.Brushes.Length; i++)
273273
{
274+
var brush = Models.Bookmarks.Brushes[i];
274275
var icon = App.CreateMenuIcon("Icons.Bookmark");
275-
276-
if (i != 0)
277-
icon.Fill = Models.Bookmarks.Brushes[i];
276+
if (brush != null)
277+
icon.Fill = brush;
278278

279279
var dupIdx = i;
280280
var setter = new MenuItem();

0 commit comments

Comments
 (0)