2024-08-05 02:34:49 -07:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
using Avalonia;
|
2024-08-05 03:18:57 -07:00
|
|
|
|
using Avalonia.Collections;
|
2024-08-05 02:34:49 -07:00
|
|
|
|
using Avalonia.Controls;
|
|
|
|
|
using Avalonia.Controls.Documents;
|
|
|
|
|
using Avalonia.Input;
|
|
|
|
|
|
|
|
|
|
namespace SourceGit.Views
|
|
|
|
|
{
|
|
|
|
|
public class CommitMessagePresenter : SelectableTextBlock
|
|
|
|
|
{
|
|
|
|
|
public static readonly StyledProperty<string> MessageProperty =
|
|
|
|
|
AvaloniaProperty.Register<CommitMessagePresenter, string>(nameof(Message));
|
|
|
|
|
|
|
|
|
|
public string Message
|
|
|
|
|
{
|
|
|
|
|
get => GetValue(MessageProperty);
|
|
|
|
|
set => SetValue(MessageProperty, value);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-05 03:18:57 -07:00
|
|
|
|
public static readonly StyledProperty<AvaloniaList<Models.IssueTrackerRule>> IssueTrackerRulesProperty =
|
|
|
|
|
AvaloniaProperty.Register<CommitMessagePresenter, AvaloniaList<Models.IssueTrackerRule>>(nameof(IssueTrackerRules));
|
2024-08-05 02:34:49 -07:00
|
|
|
|
|
2024-08-05 03:18:57 -07:00
|
|
|
|
public AvaloniaList<Models.IssueTrackerRule> IssueTrackerRules
|
2024-08-05 02:34:49 -07:00
|
|
|
|
{
|
2024-08-05 03:18:57 -07:00
|
|
|
|
get => GetValue(IssueTrackerRulesProperty);
|
|
|
|
|
set => SetValue(IssueTrackerRulesProperty, value);
|
2024-08-05 02:34:49 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override Type StyleKeyOverride => typeof(SelectableTextBlock);
|
|
|
|
|
|
|
|
|
|
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
|
|
|
|
|
{
|
|
|
|
|
base.OnPropertyChanged(change);
|
|
|
|
|
|
2024-08-05 03:18:57 -07:00
|
|
|
|
if (change.Property == MessageProperty || change.Property == IssueTrackerRulesProperty)
|
2024-08-05 02:34:49 -07:00
|
|
|
|
{
|
|
|
|
|
Inlines.Clear();
|
|
|
|
|
|
|
|
|
|
var message = Message;
|
|
|
|
|
if (string.IsNullOrEmpty(message))
|
|
|
|
|
return;
|
|
|
|
|
|
2024-08-05 03:18:57 -07:00
|
|
|
|
var rules = IssueTrackerRules;
|
2024-08-05 02:34:49 -07:00
|
|
|
|
if (rules == null || rules.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
Inlines.Add(new Run(message));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-05 03:18:57 -07:00
|
|
|
|
var matches = new List<Models.IssueTrackerMatch>();
|
2024-08-05 02:34:49 -07:00
|
|
|
|
foreach (var rule in rules)
|
|
|
|
|
rule.Matches(matches, message);
|
|
|
|
|
|
|
|
|
|
if (matches.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
Inlines.Add(new Run(message));
|
|
|
|
|
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(message.Substring(pos, match.Start - pos)));
|
|
|
|
|
|
|
|
|
|
var link = new TextBlock();
|
|
|
|
|
link.SetValue(TextProperty, message.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 < message.Length)
|
|
|
|
|
Inlines.Add(new Run(message.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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|