fix: can not select the entire content of commit message

This commit is contained in:
leo 2024-08-13 12:08:33 +08:00
parent d1b236b090
commit 202aa379f8
No known key found for this signature in database
4 changed files with 141 additions and 30 deletions

View file

@ -1,6 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Avalonia.Controls.Documents;
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.ComponentModel;
namespace SourceGit.Models namespace SourceGit.Models
@ -10,6 +10,7 @@ namespace SourceGit.Models
public int Start { get; set; } = 0; public int Start { get; set; } = 0;
public int Length { get; set; } = 0; public int Length { get; set; } = 0;
public string URL { get; set; } = ""; public string URL { get; set; } = "";
public Run Link { get; set; } = null;
public bool Intersect(int start, int length) public bool Intersect(int start, int length)
{ {

View file

@ -280,11 +280,11 @@
<Setter Property="FontWeight" Value="Bold"/> <Setter Property="FontWeight" Value="Bold"/>
<Setter Property="HorizontalAlignment" Value="Right"/> <Setter Property="HorizontalAlignment" Value="Right"/>
</Style> </Style>
<Style Selector="TextBlock.issue_link">
<Style Selector="Run.issue_link">
<Setter Property="Foreground" Value="{DynamicResource Brush.Link}"/> <Setter Property="Foreground" Value="{DynamicResource Brush.Link}"/>
<Setter Property="Cursor" Value="Hand"/>
</Style> </Style>
<Style Selector="TextBlock.issue_link:pointerover"> <Style Selector="Run.issue_link.issue_link_hovered">
<Setter Property="TextDecorations" Value="Underline"/> <Setter Property="TextDecorations" Value="Underline"/>
</Style> </Style>

View file

@ -6,6 +6,7 @@ using Avalonia.Collections;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Controls.Documents; using Avalonia.Controls.Documents;
using Avalonia.Input; using Avalonia.Input;
using Avalonia.Utilities;
namespace SourceGit.Views namespace SourceGit.Views
{ {
@ -38,6 +39,8 @@ namespace SourceGit.Views
if (change.Property == MessageProperty || change.Property == IssueTrackerRulesProperty) if (change.Property == MessageProperty || change.Property == IssueTrackerRulesProperty)
{ {
Inlines.Clear(); Inlines.Clear();
_matches = null;
ClearHoveredIssueLink();
var message = Message; var message = Message;
if (string.IsNullOrEmpty(message)) if (string.IsNullOrEmpty(message))
@ -61,6 +64,7 @@ namespace SourceGit.Views
} }
matches.Sort((l, r) => l.Start - r.Start); matches.Sort((l, r) => l.Start - r.Start);
_matches = matches;
int pos = 0; int pos = 0;
foreach (var match in matches) foreach (var match in matches)
@ -68,12 +72,9 @@ namespace SourceGit.Views
if (match.Start > pos) if (match.Start > pos)
Inlines.Add(new Run(message.Substring(pos, match.Start - pos))); Inlines.Add(new Run(message.Substring(pos, match.Start - pos)));
var link = new TextBlock(); match.Link = new Run(message.Substring(match.Start, match.Length));
link.SetValue(TextProperty, message.Substring(match.Start, match.Length)); match.Link.Classes.Add("issue_link");
link.SetValue(ToolTip.TipProperty, match.URL); Inlines.Add(match.Link);
link.Classes.Add("issue_link");
link.PointerPressed += OnLinkPointerPressed;
Inlines.Add(link);
pos = match.Start + match.Length; pos = match.Start + match.Length;
} }
@ -83,16 +84,70 @@ namespace SourceGit.Views
} }
} }
private void OnLinkPointerPressed(object sender, PointerPressedEventArgs e) protected override void OnPointerMoved(PointerEventArgs e)
{ {
if (sender is TextBlock text) base.OnPointerMoved(e);
{
var tooltip = text.GetValue(ToolTip.TipProperty) as string;
if (!string.IsNullOrEmpty(tooltip))
Native.OS.OpenBrowser(tooltip);
if (e.Pointer.Captured == null && _matches != null)
{
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;
SetCurrentValue(CursorProperty, Cursor.Parse("Hand"));
ToolTip.SetTip(this, match.URL);
ToolTip.SetIsOpen(this, true);
return;
}
ClearHoveredIssueLink();
}
}
protected override void OnPointerPressed(PointerPressedEventArgs e)
{
if (_lastHover != null)
{
SetCurrentValue(SelectionEndProperty, SelectionStart);
Native.OS.OpenBrowser(_lastHover.URL);
ClearHoveredIssueLink();
e.Handled = true; e.Handled = true;
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.Link.Classes.Remove("issue_link_hovered");
_lastHover = null;
} }
} }
private List<Models.IssueTrackerMatch> _matches = null;
private Models.IssueTrackerMatch _lastHover = null;
} }
} }

View file

@ -11,6 +11,7 @@ using Avalonia.Input;
using Avalonia.Interactivity; using Avalonia.Interactivity;
using Avalonia.Media; using Avalonia.Media;
using Avalonia.Threading; using Avalonia.Threading;
using Avalonia.Utilities;
using Avalonia.VisualTree; using Avalonia.VisualTree;
namespace SourceGit.Views namespace SourceGit.Views
@ -185,6 +186,8 @@ namespace SourceGit.Views
if (change.Property == SubjectProperty || change.Property == IssueTrackerRulesProperty) if (change.Property == SubjectProperty || change.Property == IssueTrackerRulesProperty)
{ {
Inlines.Clear(); Inlines.Clear();
_matches = null;
ClearHoveredIssueLink();
var subject = Subject; var subject = Subject;
if (string.IsNullOrEmpty(subject)) if (string.IsNullOrEmpty(subject))
@ -208,6 +211,7 @@ namespace SourceGit.Views
} }
matches.Sort((l, r) => l.Start - r.Start); matches.Sort((l, r) => l.Start - r.Start);
_matches = matches;
int pos = 0; int pos = 0;
foreach (var match in matches) foreach (var match in matches)
@ -215,12 +219,9 @@ namespace SourceGit.Views
if (match.Start > pos) if (match.Start > pos)
Inlines.Add(new Run(subject.Substring(pos, match.Start - pos))); Inlines.Add(new Run(subject.Substring(pos, match.Start - pos)));
var link = new TextBlock(); match.Link = new Run(subject.Substring(match.Start, match.Length));
link.SetValue(TextProperty, subject.Substring(match.Start, match.Length)); match.Link.Classes.Add("issue_link");
link.SetValue(ToolTip.TipProperty, match.URL); Inlines.Add(match.Link);
link.Classes.Add("issue_link");
link.PointerPressed += OnLinkPointerPressed;
Inlines.Add(link);
pos = match.Start + match.Length; pos = match.Start + match.Length;
} }
@ -230,17 +231,71 @@ namespace SourceGit.Views
} }
} }
private void OnLinkPointerPressed(object sender, PointerPressedEventArgs e) protected override void OnPointerMoved(PointerEventArgs e)
{ {
if (sender is TextBlock text) base.OnPointerMoved(e);
{
var tooltip = text.GetValue(ToolTip.TipProperty) as string;
if (!string.IsNullOrEmpty(tooltip))
Native.OS.OpenBrowser(tooltip);
if (_matches != null)
{
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 textPosition = TextLayout.HitTestPoint(point).TextPosition;
foreach (var match in _matches)
{
if (!match.Intersect(textPosition, 1))
continue;
if (match == _lastHover)
return;
_lastHover = match;
SetCurrentValue(CursorProperty, Cursor.Parse("Hand"));
ToolTip.SetTip(this, match.URL);
ToolTip.SetIsOpen(this, true);
e.Handled = true; e.Handled = true;
return;
}
ClearHoveredIssueLink();
} }
} }
protected override void OnPointerPressed(PointerPressedEventArgs e)
{
if (_lastHover != null)
{
Native.OS.OpenBrowser(_lastHover.URL);
ClearHoveredIssueLink();
e.Handled = true;
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("Arrow"));
_lastHover.Link.Classes.Remove("issue_link_hovered");
_lastHover = null;
}
}
private List<Models.IssueTrackerMatch> _matches = null;
private Models.IssueTrackerMatch _lastHover = null;
} }
public class CommitTimeTextBlock : TextBlock public class CommitTimeTextBlock : TextBlock