Skip to content

Commit cfd5ae0

Browse files
authored
Fix unclickable, unselectable links in Avalonia package details (#4950)
1 parent 824e33f commit cfd5ae0

1 file changed

Lines changed: 116 additions & 68 deletions

File tree

‎src/UniGetUI.Avalonia/Views/DialogPages/PackageDetailsWindow.axaml.cs‎

Lines changed: 116 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using UniGetUI.Avalonia.Infrastructure;
1313
using UniGetUI.Avalonia.ViewModels;
1414
using UniGetUI.Avalonia.Views.DialogPages;
15+
using UniGetUI.Core.Logging;
1516
using UniGetUI.Core.Tools;
1617
using UniGetUI.Interface.Telemetry;
1718
using UniGetUI.PackageEngine.Enums;
@@ -297,106 +298,146 @@ private void AddInlineRow(StackPanel host, string label, string value)
297298

298299
private void AddInlineRow(StackPanel host, string label, Uri? url)
299300
{
300-
var tb = new SelectableTextBlock { TextWrapping = TextWrapping.Wrap };
301-
var inlines = tb.Inlines ??= new InlineCollection();
302-
inlines.Add(new Run(label + ": ") { FontWeight = FontWeight.Bold });
303-
304301
if (url is null)
305302
{
303+
var tb = new SelectableTextBlock { TextWrapping = TextWrapping.Wrap };
304+
var inlines = tb.Inlines ??= new InlineCollection();
305+
inlines.Add(new Run(label + ": ") { FontWeight = FontWeight.Bold });
306306
inlines.Add(new Run(_vm.LabelNotAvailable)
307307
{
308308
Foreground = NotAvailableBrush,
309309
FontStyle = FontStyle.Italic,
310310
});
311+
host.Children.Add(tb);
312+
return;
311313
}
312-
else
313-
{
314-
inlines.Add(new Run(url.ToString())
315-
{
316-
Foreground = LinkBrush,
317-
TextDecorations = TextDecorations.Underline,
318-
});
319-
tb.Cursor = new Cursor(StandardCursorType.Hand);
320-
tb.PointerPressed += (_, e) =>
321-
{
322-
if (e.GetCurrentPoint(tb).Properties.IsLeftButtonPressed)
323-
OpenUrl(url.ToString());
324-
};
325-
}
326-
host.Children.Add(tb);
314+
315+
host.Children.Add(BuildLinkRow(BoldLabel(label + ": "), url.ToString()));
327316
}
328317

329318
private void AddLicenseRow(StackPanel host)
330319
{
331-
var tb = new SelectableTextBlock { TextWrapping = TextWrapping.Wrap };
332-
var inlines = tb.Inlines ??= new InlineCollection();
333-
inlines.Add(new Run(_vm.LabelLicense + ": ") { FontWeight = FontWeight.Bold });
334-
335320
bool hasName = !string.IsNullOrEmpty(_vm.LicenseName);
336321
bool hasUrl = _vm.LicenseUrl is not null;
337322

338-
if (hasName)
339-
inlines.Add(new Run(_vm.LicenseName!));
340-
341-
if (hasUrl)
323+
if (!hasUrl)
342324
{
343-
if (hasName) inlines.Add(new Run(" "));
344-
var url = _vm.LicenseUrl!.ToString();
345-
inlines.Add(new Run(url)
346-
{
347-
Foreground = LinkBrush,
348-
TextDecorations = TextDecorations.Underline,
349-
});
350-
tb.Cursor = new Cursor(StandardCursorType.Hand);
351-
tb.PointerPressed += (_, e) =>
352-
{
353-
if (e.GetCurrentPoint(tb).Properties.IsLeftButtonPressed)
354-
OpenUrl(url);
355-
};
325+
// No link: render as a plain selectable line with the usual cursor.
326+
var tb = new SelectableTextBlock { TextWrapping = TextWrapping.Wrap };
327+
var inlines = tb.Inlines ??= new InlineCollection();
328+
inlines.Add(new Run(_vm.LabelLicense + ": ") { FontWeight = FontWeight.Bold });
329+
if (hasName)
330+
inlines.Add(new Run(_vm.LicenseName!));
331+
else
332+
inlines.Add(new Run(_vm.LabelNotAvailable)
333+
{
334+
Foreground = NotAvailableBrush,
335+
FontStyle = FontStyle.Italic,
336+
});
337+
host.Children.Add(tb);
338+
return;
356339
}
357-
else if (!hasName)
340+
341+
var labelBlock = new SelectableTextBlock
358342
{
359-
inlines.Add(new Run(_vm.LabelNotAvailable)
360-
{
361-
Foreground = NotAvailableBrush,
362-
FontStyle = FontStyle.Italic,
363-
});
364-
}
343+
TextWrapping = TextWrapping.Wrap,
344+
VerticalAlignment = VerticalAlignment.Top,
345+
};
346+
var labelInlines = labelBlock.Inlines ??= new InlineCollection();
347+
labelInlines.Add(new Run(_vm.LabelLicense + ": ") { FontWeight = FontWeight.Bold });
348+
if (hasName)
349+
labelInlines.Add(new Run(_vm.LicenseName! + " "));
365350

366-
host.Children.Add(tb);
351+
host.Children.Add(BuildLinkRow(labelBlock, _vm.LicenseUrl!.ToString()));
367352
}
368353

369354
private void AddDownloadRow(StackPanel host)
370355
{
371-
var tb = new SelectableTextBlock { TextWrapping = TextWrapping.Wrap };
372-
var inlines = tb.Inlines ??= new InlineCollection();
373-
374356
if (_vm.CanDownloadInstaller && !_vm.IsLoading)
375357
{
376-
inlines.Add(new Run(_vm.LabelDownloadInstaller)
377-
{
378-
Foreground = LinkBrush,
379-
TextDecorations = TextDecorations.Underline,
380-
FontWeight = FontWeight.SemiBold,
381-
});
358+
var panel = new StackPanel { Orientation = Orientation.Horizontal };
359+
var link = CreateLinkBlock(_vm.LabelDownloadInstaller, DownloadInstaller);
360+
link.FontWeight = FontWeight.SemiBold;
361+
panel.Children.Add(link);
382362
if (!string.IsNullOrEmpty(_vm.InstallerSize))
383-
inlines.Add(new Run($" ({_vm.InstallerSize})"));
384-
tb.Cursor = new Cursor(StandardCursorType.Hand);
385-
tb.PointerPressed += (_, e) =>
386-
{
387-
if (e.GetCurrentPoint(tb).Properties.IsLeftButtonPressed)
388-
DownloadInstaller();
389-
};
363+
panel.Children.Add(new SelectableTextBlock { Text = $" ({_vm.InstallerSize})" });
364+
host.Children.Add(panel);
390365
}
391366
else
392367
{
393-
inlines.Add(new Run(_vm.LabelInstallerNotAvailable)
368+
host.Children.Add(new SelectableTextBlock
394369
{
370+
Text = _vm.LabelInstallerNotAvailable,
395371
Foreground = NotAvailableBrush,
396372
FontStyle = FontStyle.Italic,
397373
});
398374
}
399-
host.Children.Add(tb);
375+
}
376+
377+
private static SelectableTextBlock BoldLabel(string text) => new()
378+
{
379+
Text = text,
380+
FontWeight = FontWeight.Bold,
381+
VerticalAlignment = VerticalAlignment.Top,
382+
};
383+
384+
/// <summary>"Label: url" laid out so a long URL wraps under itself; only the URL is the link.</summary>
385+
private Grid BuildLinkRow(Control label, string url)
386+
{
387+
var grid = new Grid
388+
{
389+
ColumnDefinitions =
390+
[
391+
new ColumnDefinition(GridLength.Auto),
392+
new ColumnDefinition(new GridLength(1, GridUnitType.Star)),
393+
],
394+
};
395+
Grid.SetColumn(label, 0);
396+
var link = CreateLinkBlock(url, () => OpenUrl(url));
397+
link.TextWrapping = TextWrapping.Wrap;
398+
Grid.SetColumn(link, 1);
399+
grid.Children.Add(label);
400+
grid.Children.Add(link);
401+
return grid;
402+
}
403+
404+
/// <summary>A selectable, copyable hyperlink. The hand cursor and activation cover only this text.</summary>
405+
private SelectableTextBlock CreateLinkBlock(string text, Action activate)
406+
{
407+
var link = new SelectableTextBlock
408+
{
409+
Text = text,
410+
Foreground = LinkBrush,
411+
TextDecorations = TextDecorations.Underline,
412+
Cursor = new Cursor(StandardCursorType.Hand),
413+
};
414+
AttachLinkActivation(link, activate);
415+
return link;
416+
}
417+
418+
/// <summary>Activate on a clean click, but let a press-and-drag run the text selection instead.</summary>
419+
private static void AttachLinkActivation(Control link, Action activate)
420+
{
421+
Point press = default;
422+
bool tracking = false;
423+
424+
link.AddHandler(InputElement.PointerPressedEvent, (_, e) =>
425+
{
426+
if (e.GetCurrentPoint(link).Properties.IsLeftButtonPressed)
427+
{
428+
tracking = true;
429+
press = e.GetPosition(link);
430+
}
431+
}, RoutingStrategies.Bubble, handledEventsToo: true);
432+
433+
link.AddHandler(InputElement.PointerReleasedEvent, (_, e) =>
434+
{
435+
if (!tracking) return;
436+
tracking = false;
437+
var moved = e.GetPosition(link) - press;
438+
if (Math.Abs(moved.X) <= 4 && Math.Abs(moved.Y) <= 4)
439+
activate();
440+
}, RoutingStrategies.Bubble, handledEventsToo: true);
400441
}
401442

402443
private IBrush LinkBrush =>
@@ -408,8 +449,15 @@ private static void OpenUrl(string url)
408449
{
409450
if (string.IsNullOrEmpty(url) || !url.StartsWith("http", StringComparison.OrdinalIgnoreCase))
410451
return;
411-
try { Process.Start(new ProcessStartInfo(url) { UseShellExecute = true }); }
412-
catch { }
452+
try
453+
{
454+
Process.Start(new ProcessStartInfo(url) { UseShellExecute = true });
455+
}
456+
catch (Exception ex)
457+
{
458+
Logger.Error($"Failed to open URL: {url}");
459+
Logger.Error(ex);
460+
}
413461
}
414462

415463
private void DownloadInstaller()

0 commit comments

Comments
 (0)