2024-08-05 02:34:49 -07:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2024-08-20 21:46:36 -07:00
|
|
|
|
using System.Text.RegularExpressions;
|
2024-08-05 02:34:49 -07:00
|
|
|
|
|
|
|
|
|
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;
|
2024-08-20 21:46:36 -07:00
|
|
|
|
using Avalonia.VisualTree;
|
2024-08-05 02:34:49 -07:00
|
|
|
|
|
|
|
|
|
namespace SourceGit.Views
|
|
|
|
|
{
|
2024-08-20 21:46:36 -07:00
|
|
|
|
public partial class CommitMessagePresenter : SelectableTextBlock
|
2024-08-05 02:34:49 -07:00
|
|
|
|
{
|
2024-08-20 21:46:36 -07:00
|
|
|
|
[GeneratedRegex(@"\b([0-9a-fA-F]{8,40})\b")]
|
|
|
|
|
private static partial Regex REG_SHA_FORMAT();
|
|
|
|
|
|
2024-08-05 02:34:49 -07:00
|
|
|
|
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
|
|
|
|
{
|
2024-08-24 20:33:38 -07:00
|
|
|
|
Inlines!.Clear();
|
2024-08-12 21:08:33 -07:00
|
|
|
|
_matches = null;
|
|
|
|
|
ClearHoveredIssueLink();
|
2024-08-05 02:34:49 -07:00
|
|
|
|
|
|
|
|
|
var message = Message;
|
|
|
|
|
if (string.IsNullOrEmpty(message))
|
|
|
|
|
return;
|
|
|
|
|
|
2024-08-20 21:46:36 -07:00
|
|
|
|
var matches = new List<Models.Hyperlink>();
|
|
|
|
|
if (IssueTrackerRules is { Count: > 0 } rules)
|
2024-08-05 02:34:49 -07:00
|
|
|
|
{
|
2024-08-20 21:46:36 -07:00
|
|
|
|
foreach (var rule in rules)
|
|
|
|
|
rule.Matches(matches, message);
|
2024-08-05 02:34:49 -07:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-20 21:46:36 -07:00
|
|
|
|
var shas = REG_SHA_FORMAT().Matches(message);
|
|
|
|
|
for (int i = 0; i < shas.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
var sha = shas[i];
|
|
|
|
|
if (!sha.Success)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var start = sha.Index;
|
|
|
|
|
var len = sha.Length;
|
|
|
|
|
var intersect = false;
|
|
|
|
|
foreach (var match in matches)
|
|
|
|
|
{
|
|
|
|
|
if (match.Intersect(start, len))
|
|
|
|
|
{
|
|
|
|
|
intersect = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!intersect)
|
|
|
|
|
matches.Add(new Models.Hyperlink(start, len, sha.Groups[1].Value, true));
|
|
|
|
|
}
|
2024-08-05 02:34:49 -07:00
|
|
|
|
|
|
|
|
|
if (matches.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
Inlines.Add(new Run(message));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
matches.Sort((l, r) => l.Start - r.Start);
|
2024-08-12 21:08:33 -07:00
|
|
|
|
_matches = matches;
|
2024-08-05 02:34:49 -07:00
|
|
|
|
|
2024-08-26 21:20:36 -07:00
|
|
|
|
var inlines = new List<Inline>();
|
2024-08-26 06:41:48 -07:00
|
|
|
|
var pos = 0;
|
2024-08-05 02:34:49 -07:00
|
|
|
|
foreach (var match in matches)
|
|
|
|
|
{
|
|
|
|
|
if (match.Start > pos)
|
2024-08-26 06:41:48 -07:00
|
|
|
|
inlines.Add(new Run(message.Substring(pos, match.Start - pos)));
|
2024-08-05 02:34:49 -07:00
|
|
|
|
|
2024-08-20 21:46:36 -07:00
|
|
|
|
var link = new Run(message.Substring(match.Start, match.Length));
|
|
|
|
|
link.Classes.Add(match.IsCommitSHA ? "commit_link" : "issue_link");
|
2024-08-26 06:41:48 -07:00
|
|
|
|
inlines.Add(link);
|
2024-08-05 02:34:49 -07:00
|
|
|
|
|
|
|
|
|
pos = match.Start + match.Length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pos < message.Length)
|
2024-08-26 06:41:48 -07:00
|
|
|
|
inlines.Add(new Run(message.Substring(pos)));
|
|
|
|
|
|
|
|
|
|
Inlines.AddRange(inlines);
|
2024-08-05 02:34:49 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-12 21:08:33 -07:00
|
|
|
|
protected override void OnPointerMoved(PointerEventArgs e)
|
2024-08-05 02:34:49 -07:00
|
|
|
|
{
|
2024-08-12 21:08:33 -07:00
|
|
|
|
base.OnPointerMoved(e);
|
|
|
|
|
|
2024-08-26 23:47:20 -07:00
|
|
|
|
if (e.Pointer.Captured == this)
|
|
|
|
|
{
|
|
|
|
|
var relativeSelfY = e.GetPosition(this).Y;
|
|
|
|
|
if (relativeSelfY <= 0 || relativeSelfY > Bounds.Height)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var scrollViewer = this.FindAncestorOfType<ScrollViewer>();
|
|
|
|
|
if (scrollViewer != null)
|
|
|
|
|
{
|
|
|
|
|
var relativeY = e.GetPosition(scrollViewer).Y;
|
|
|
|
|
if (relativeY <= 8)
|
|
|
|
|
scrollViewer.LineUp();
|
|
|
|
|
else if (relativeY >= scrollViewer.Bounds.Height - 8)
|
|
|
|
|
scrollViewer.LineDown();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (_matches != null)
|
2024-08-05 02:34:49 -07:00
|
|
|
|
{
|
2024-08-20 21:46:36 -07:00
|
|
|
|
var point = e.GetPosition(this) - new Point(Padding.Left, Padding.Top);
|
|
|
|
|
var x = Math.Min(Math.Max(point.X, 0), Math.Max(TextLayout.WidthIncludingTrailingWhitespace, 0));
|
|
|
|
|
var y = Math.Min(Math.Max(point.Y, 0), Math.Max(TextLayout.Height, 0));
|
|
|
|
|
point = new Point(x, y);
|
2024-08-12 21:08:33 -07:00
|
|
|
|
|
|
|
|
|
var pos = TextLayout.HitTestPoint(point).TextPosition;
|
|
|
|
|
foreach (var match in _matches)
|
|
|
|
|
{
|
|
|
|
|
if (!match.Intersect(pos, 1))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (match == _lastHover)
|
|
|
|
|
return;
|
|
|
|
|
|
2024-08-20 21:46:36 -07:00
|
|
|
|
SetCurrentValue(CursorProperty, Cursor.Parse("Hand"));
|
|
|
|
|
|
2024-08-12 21:08:33 -07:00
|
|
|
|
_lastHover = match;
|
2024-08-20 21:46:36 -07:00
|
|
|
|
if (!_lastHover.IsCommitSHA)
|
|
|
|
|
{
|
|
|
|
|
ToolTip.SetTip(this, match.Link);
|
|
|
|
|
ToolTip.SetIsOpen(this, true);
|
|
|
|
|
}
|
2024-08-12 21:25:06 -07:00
|
|
|
|
|
2024-08-12 21:08:33 -07:00
|
|
|
|
return;
|
|
|
|
|
}
|
2024-08-05 02:34:49 -07:00
|
|
|
|
|
2024-08-12 21:08:33 -07:00
|
|
|
|
ClearHoveredIssueLink();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnPointerPressed(PointerPressedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (_lastHover != null)
|
|
|
|
|
{
|
2024-08-12 21:18:26 -07:00
|
|
|
|
e.Pointer.Capture(null);
|
2024-08-20 21:46:36 -07:00
|
|
|
|
|
|
|
|
|
if (_lastHover.IsCommitSHA)
|
|
|
|
|
{
|
|
|
|
|
var parentView = this.FindAncestorOfType<CommitBaseInfo>();
|
|
|
|
|
if (parentView is { DataContext: ViewModels.CommitDetail detail })
|
|
|
|
|
detail.NavigateTo(_lastHover.Link);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Native.OS.OpenBrowser(_lastHover.Link);
|
|
|
|
|
}
|
2024-08-20 21:49:46 -07:00
|
|
|
|
|
2024-08-05 02:34:49 -07:00
|
|
|
|
e.Handled = true;
|
2024-08-12 21:08:33 -07:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
base.OnPointerPressed(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnPointerExited(PointerEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
base.OnPointerExited(e);
|
|
|
|
|
ClearHoveredIssueLink();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ClearHoveredIssueLink()
|
|
|
|
|
{
|
|
|
|
|
if (_lastHover != null)
|
|
|
|
|
{
|
|
|
|
|
ToolTip.SetTip(this, null);
|
|
|
|
|
SetCurrentValue(CursorProperty, Cursor.Parse("IBeam"));
|
|
|
|
|
_lastHover = null;
|
2024-08-05 02:34:49 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-12 21:08:33 -07:00
|
|
|
|
|
2024-08-20 21:46:36 -07:00
|
|
|
|
private List<Models.Hyperlink> _matches = null;
|
|
|
|
|
private Models.Hyperlink _lastHover = null;
|
2024-08-05 02:34:49 -07:00
|
|
|
|
}
|
|
|
|
|
}
|