-
-
Notifications
You must be signed in to change notification settings - Fork 588
Expand file tree
/
Copy pathDocsHelpButton.cs
More file actions
67 lines (56 loc) · 2.02 KB
/
Copy pathDocsHelpButton.cs
File metadata and controls
67 lines (56 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using Avalonia;
using Avalonia.Controls;
using Microsoft.Extensions.DependencyInjection;
using NLog;
using StabilityMatrix.Avalonia.Services;
namespace StabilityMatrix.Avalonia.Controls;
/// <summary>
/// Contextual help button that opens the in-app documentation viewer at <see cref="DocsPath"/>.
/// Resolves navigation itself so that adding help to a surface needs no view model plumbing.
/// </summary>
/// <remarks>
/// Prefer a <see cref="Core.Models.Documentation.DocumentationPages"/> constant over a literal
/// path so a moved page is fixed in one place.
/// </remarks>
public class DocsHelpButton : Button
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
public static readonly StyledProperty<string?> DocsPathProperty = AvaloniaProperty.Register<
DocsHelpButton,
string?
>(nameof(DocsPath));
/// <summary>
/// Page path relative to the docs root, e.g. <c>advanced/environment-variables.md</c>.
/// </summary>
public string? DocsPath
{
get => GetValue(DocsPathProperty);
set => SetValue(DocsPathProperty, value);
}
public static readonly StyledProperty<string?> AnchorProperty = AvaloniaProperty.Register<
DocsHelpButton,
string?
>(nameof(Anchor));
/// <summary>
/// Optional heading slug within the page to scroll to, e.g. <c>setting-a-variable</c>.
/// </summary>
public string? Anchor
{
get => GetValue(AnchorProperty);
set => SetValue(AnchorProperty, value);
}
protected override Type StyleKeyOverride => typeof(DocsHelpButton);
protected override void OnClick()
{
base.OnClick();
if (Design.IsDesignMode)
return;
if (string.IsNullOrWhiteSpace(DocsPath))
{
Logger.Warn("{Control} clicked with no {Property} set", nameof(DocsHelpButton), nameof(DocsPath));
return;
}
App.Services?.GetService<IDocumentationNavigationService>()?.OpenDocumentation(DocsPath, Anchor);
}
}