Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ConsoloniaAccessText underline #205

Merged
merged 14 commits into from
Dec 14, 2024
66 changes: 66 additions & 0 deletions src/Consolonia.Core/Controls/ConsoloniaAccessText.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System;
using Avalonia.Media;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Documents;

namespace Consolonia.Core.Controls
{

/// <summary>
/// A text block that displays a character prefixed with an underscore as an access key.
/// </summary>
public class ConsoloniaAccessText : AccessText
{
private Run _accessRun;

public ConsoloniaAccessText()
{
this.PropertyChanged += (sender, e) =>

Check notice on line 18 in src/Consolonia.Core/Controls/ConsoloniaAccessText.cs

View workflow job for this annotation

GitHub Actions / build

"[ArrangeThisQualifier] Qualifier 'this.' is redundant" on /home/runner/work/Consolonia/Consolonia/src/Consolonia.Core/Controls/ConsoloniaAccessText.cs(18,13)

Check warning on line 18 in src/Consolonia.Core/Controls/ConsoloniaAccessText.cs

View workflow job for this annotation

GitHub Actions / build

"[UnusedParameter.Local] Parameter 'sender' is never used" on /home/runner/work/Consolonia/Consolonia/src/Consolonia.Core/Controls/ConsoloniaAccessText.cs(18,38)
{
switch (e.Property.Name)
{
case nameof(Text):
{
if (!String.IsNullOrEmpty(Text))

Check notice on line 24 in src/Consolonia.Core/Controls/ConsoloniaAccessText.cs

View workflow job for this annotation

GitHub Actions / build

"[BuiltInTypeReferenceStyleForMemberAccess] Built-in type reference is inconsistent with code style settings" on /home/runner/work/Consolonia/Consolonia/src/Consolonia.Core/Controls/ConsoloniaAccessText.cs(24,34)
{
_accessRun = null;
InlineCollection inlines = new InlineCollection();
var iPos = Text.IndexOf('_', StringComparison.Ordinal);
if (iPos >= 0)
{
inlines.Add(new Run(Text.Substring(0, iPos)));
_accessRun = new Run(Text.Substring(++iPos, 1))
{
TextDecorations = new TextDecorationCollection()
};
inlines.Add(_accessRun);
inlines.Add(new Run(Text.Substring(iPos + 1)));
}
else
{
inlines.Add(new Run(Text));
}
this.Inlines = inlines;

Check notice on line 43 in src/Consolonia.Core/Controls/ConsoloniaAccessText.cs

View workflow job for this annotation

GitHub Actions / build

"[ArrangeThisQualifier] Qualifier 'this.' is redundant" on /home/runner/work/Consolonia/Consolonia/src/Consolonia.Core/Controls/ConsoloniaAccessText.cs(43,33)
}
tomlm marked this conversation as resolved.
Show resolved Hide resolved
}
break;
case nameof(ShowAccessKey):
if (_accessRun != null)
{
if (this.ShowAccessKey)

Check notice on line 50 in src/Consolonia.Core/Controls/ConsoloniaAccessText.cs

View workflow job for this annotation

GitHub Actions / build

"[ArrangeThisQualifier] Qualifier 'this.' is redundant" on /home/runner/work/Consolonia/Consolonia/src/Consolonia.Core/Controls/ConsoloniaAccessText.cs(50,33)
_accessRun.TextDecorations.Add(new TextDecoration { Location = TextDecorationLocation.Underline });

Check warning on line 51 in src/Consolonia.Core/Controls/ConsoloniaAccessText.cs

View workflow job for this annotation

GitHub Actions / build

"[PossibleNullReferenceException] Possible 'System.NullReferenceException'" on /home/runner/work/Consolonia/Consolonia/src/Consolonia.Core/Controls/ConsoloniaAccessText.cs(51,33)
else
_accessRun.TextDecorations.Clear();

Check warning on line 53 in src/Consolonia.Core/Controls/ConsoloniaAccessText.cs

View workflow job for this annotation

GitHub Actions / build

"[PossibleNullReferenceException] Possible 'System.NullReferenceException'" on /home/runner/work/Consolonia/Consolonia/src/Consolonia.Core/Controls/ConsoloniaAccessText.cs(53,33)
}
break;
default:

Check warning on line 56 in src/Consolonia.Core/Controls/ConsoloniaAccessText.cs

View workflow job for this annotation

GitHub Actions / build

"[RedundantEmptySwitchSection] Redundant empty switch section" on /home/runner/work/Consolonia/Consolonia/src/Consolonia.Core/Controls/ConsoloniaAccessText.cs(56,21)
break;
}

};
tomlm marked this conversation as resolved.
Show resolved Hide resolved

/// <inheritdoc/>

Check warning on line 62 in src/Consolonia.Core/Controls/ConsoloniaAccessText.cs

View workflow job for this annotation

GitHub Actions / build

"[CSharpWarnings::CS1587] XML comment is not placed on a valid language element" on /home/runner/work/Consolonia/Consolonia/src/Consolonia.Core/Controls/ConsoloniaAccessText.cs(62,13)
}

}
}
3 changes: 2 additions & 1 deletion src/Consolonia.Themes/Templates/Controls/Menu.axaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<ResourceDictionary xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:core="clr-namespace:Consolonia.Core.Controls;assembly=Consolonia.Core"
xmlns:helpers="clr-namespace:Consolonia.Themes.Templates.Controls.Helpers;assembly=Consolonia.Themes"
xmlns:system="clr-namespace:System;assembly=System.Runtime">

Expand All @@ -24,7 +25,7 @@
<ContentPresenter.DataTemplates>
<DataTemplate DataType="system:String">
<!-- ReSharper disable once Xaml.BindingWithContextNotResolved Just workaround - Its not binding, it's TemplateBinding-->
<AccessText Text="{Binding}"
<core:ConsoloniaAccessText Text="{Binding}"
Foreground="{TemplateBinding (TemplatedControl.Foreground)}" />
</DataTemplate>
</ContentPresenter.DataTemplates>
Expand Down
3 changes: 2 additions & 1 deletion src/Consolonia.Themes/Templates/Controls/MenuItem.axaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<ResourceDictionary xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=netstandard"
xmlns:core="clr-namespace:Consolonia.Core.Controls;assembly=Consolonia.Core"
tomlm marked this conversation as resolved.
Show resolved Hide resolved
xmlns:helpers="clr-namespace:Consolonia.Themes.Templates.Controls.Helpers;assembly=Consolonia.Themes"
xmlns:converters="clr-namespace:Avalonia.Controls.Converters;assembly=Avalonia.Controls">

Expand Down Expand Up @@ -51,7 +52,7 @@
Grid.Column="2">
<ContentPresenter.DataTemplates>
<DataTemplate DataType="sys:String">
<AccessText Text="{Binding}" />
<core:ConsoloniaAccessText Text="{Binding}" />
</DataTemplate>
</ContentPresenter.DataTemplates>
</ContentPresenter>
Expand Down
Loading