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;
|
2024-08-12 21:08:33 -07:00
|
|
|
|
using Avalonia.Utilities;
|
2024-08-05 02:34:49 -07:00
|
|
|
|
|
|
|
|
|
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();
|
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-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);
|
2024-08-12 21:08:33 -07:00
|
|
|
|
_matches = matches;
|
2024-08-05 02:34:49 -07:00
|
|
|
|
|
|
|
|
|
int pos = 0;
|
|
|
|
|
foreach (var match in matches)
|
|
|
|
|
{
|
|
|
|
|
if (match.Start > pos)
|
|
|
|
|
Inlines.Add(new Run(message.Substring(pos, match.Start - pos)));
|
|
|
|
|
|
2024-08-12 21:08:33 -07:00
|
|
|
|
match.Link = new Run(message.Substring(match.Start, match.Length));
|
|
|
|
|
match.Link.Classes.Add("issue_link");
|
|
|
|
|
Inlines.Add(match.Link);
|
2024-08-05 02:34:49 -07:00
|
|
|
|
|
|
|
|
|
pos = match.Start + match.Length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pos < message.Length)
|
|
|
|
|
Inlines.Add(new Run(message.Substring(pos)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
if (e.Pointer.Captured == null && _matches != null)
|
2024-08-05 02:34:49 -07:00
|
|
|
|
{
|
2024-08-12 21:08:33 -07:00
|
|
|
|
var padding = Padding;
|
|
|
|
|
var point = e.GetPosition(this) - new Point(padding.Left, padding.Top);
|
|
|
|
|
point = new Point(
|
|
|
|
|
MathUtilities.Clamp(point.X, 0, Math.Max(TextLayout.WidthIncludingTrailingWhitespace, 0)),
|
|
|
|
|
MathUtilities.Clamp(point.Y, 0, Math.Max(TextLayout.Height, 0)));
|
|
|
|
|
|
|
|
|
|
var pos = TextLayout.HitTestPoint(point).TextPosition;
|
|
|
|
|
foreach (var match in _matches)
|
|
|
|
|
{
|
|
|
|
|
if (!match.Intersect(pos, 1))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (match == _lastHover)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
_lastHover = match;
|
2024-08-13 20:06:37 -07:00
|
|
|
|
//_lastHover.Link.Classes.Add("issue_link_hovered");
|
2024-08-12 21:25:06 -07:00
|
|
|
|
|
2024-08-12 21:08:33 -07:00
|
|
|
|
SetCurrentValue(CursorProperty, Cursor.Parse("Hand"));
|
|
|
|
|
ToolTip.SetTip(this, match.URL);
|
|
|
|
|
ToolTip.SetIsOpen(this, true);
|
|
|
|
|
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-12 21:08:33 -07:00
|
|
|
|
Native.OS.OpenBrowser(_lastHover.URL);
|
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"));
|
2024-08-13 20:06:37 -07:00
|
|
|
|
//_lastHover.Link.Classes.Remove("issue_link_hovered");
|
2024-08-12 21:08:33 -07:00
|
|
|
|
_lastHover = null;
|
2024-08-05 02:34:49 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-12 21:08:33 -07:00
|
|
|
|
|
|
|
|
|
private List<Models.IssueTrackerMatch> _matches = null;
|
|
|
|
|
private Models.IssueTrackerMatch _lastHover = null;
|
2024-08-05 02:34:49 -07:00
|
|
|
|
}
|
|
|
|
|
}
|