feature: show issue link on commit list of histories view (#315)

This commit is contained in:
leo 2024-08-06 11:02:52 +08:00
parent 90e3abed8d
commit 9bee51686a
No known key found for this signature in database
3 changed files with 102 additions and 5 deletions

View file

@ -1,7 +1,7 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using Avalonia.Collections;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Platform.Storage; using Avalonia.Platform.Storage;
using Avalonia.VisualTree; using Avalonia.VisualTree;
@ -55,6 +55,11 @@ namespace SourceGit.ViewModels
set => SetProperty(ref _detailContext, value); set => SetProperty(ref _detailContext, value);
} }
public AvaloniaList<Models.IssueTrackerRule> IssueTrackerRules
{
get => _repo.Settings.IssueTrackerRules;
}
public Histories(Repository repo) public Histories(Repository repo)
{ {
_repo = repo; _repo = repo;

View file

@ -82,10 +82,11 @@
FontSize="10" FontSize="10"
VerticalAlignment="Center"/> VerticalAlignment="Center"/>
<TextBlock Classes="primary" <v:CommitSubjectPresenter Classes="primary"
Text="{Binding Subject}" Subject="{Binding Subject}"
Opacity="{Binding Opacity}" IssueTrackerRules="{Binding $parent[v:Histories].((vm:Histories)DataContext).IssueTrackerRules}"
FontWeight="{Binding FontWeight}"/> Opacity="{Binding Opacity}"
FontWeight="{Binding FontWeight}"/>
</StackPanel> </StackPanel>
</Border> </Border>
</DataTemplate> </DataTemplate>

View file

@ -1,7 +1,11 @@
using System; using System;
using System.Collections.Generic;
using System.Text; using System.Text;
using Avalonia; using Avalonia;
using Avalonia.Collections;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Controls.Documents;
using Avalonia.Controls.Primitives; using Avalonia.Controls.Primitives;
using Avalonia.Input; using Avalonia.Input;
using Avalonia.Interactivity; using Avalonia.Interactivity;
@ -70,6 +74,93 @@ namespace SourceGit.Views
} }
} }
public class CommitSubjectPresenter : TextBlock
{
public static readonly StyledProperty<string> SubjectProperty =
AvaloniaProperty.Register<CommitSubjectPresenter, string>(nameof(Subject));
public string Subject
{
get => GetValue(SubjectProperty);
set => SetValue(SubjectProperty, value);
}
public static readonly StyledProperty<AvaloniaList<Models.IssueTrackerRule>> IssueTrackerRulesProperty =
AvaloniaProperty.Register<CommitSubjectPresenter, AvaloniaList<Models.IssueTrackerRule>>(nameof(IssueTrackerRules));
public AvaloniaList<Models.IssueTrackerRule> IssueTrackerRules
{
get => GetValue(IssueTrackerRulesProperty);
set => SetValue(IssueTrackerRulesProperty, value);
}
protected override Type StyleKeyOverride => typeof(TextBlock);
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (change.Property == SubjectProperty || change.Property == IssueTrackerRulesProperty)
{
Inlines.Clear();
var subject = Subject;
if (string.IsNullOrEmpty(subject))
return;
var rules = IssueTrackerRules;
if (rules == null || rules.Count == 0)
{
Inlines.Add(new Run(subject));
return;
}
var matches = new List<Models.IssueTrackerMatch>();
foreach (var rule in rules)
rule.Matches(matches, subject);
if (matches.Count == 0)
{
Inlines.Add(new Run(subject));
return;
}
matches.Sort((l, r) => l.Start - r.Start);
int pos = 0;
foreach (var match in matches)
{
if (match.Start > pos)
Inlines.Add(new Run(subject.Substring(pos, match.Start - pos)));
var link = new TextBlock();
link.SetValue(TextProperty, subject.Substring(match.Start, match.Length));
link.SetValue(ToolTip.TipProperty, match.URL);
link.Classes.Add("issue_link");
link.PointerPressed += OnLinkPointerPressed;
Inlines.Add(link);
pos = match.Start + match.Length;
}
if (pos < subject.Length)
Inlines.Add(new Run(subject.Substring(pos)));
}
}
private void OnLinkPointerPressed(object sender, PointerPressedEventArgs e)
{
if (sender is TextBlock text)
{
var tooltip = text.GetValue(ToolTip.TipProperty) as string;
if (!string.IsNullOrEmpty(tooltip))
Native.OS.OpenBrowser(tooltip);
e.Handled = true;
}
}
}
public class CommitTimeTextBlock : TextBlock public class CommitTimeTextBlock : TextBlock
{ {
public static readonly StyledProperty<bool> ShowAsDateTimeProperty = public static readonly StyledProperty<bool> ShowAsDateTimeProperty =