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
63 changes: 63 additions & 0 deletions src/Consolonia.Core/Controls/ConsoloniaAccessText.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
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 sealed class ConsoloniaAccessText : AccessText
{
private Run _accessRun;

public ConsoloniaAccessText()
{
this.PropertyChanged += OnPropertyChanged;
}

private void OnPropertyChanged(object sender, Avalonia.AvaloniaPropertyChangedEventArgs e)
{
switch (e.Property.Name)
{
case nameof(Text):
{
if (!String.IsNullOrEmpty(Text))
{
_accessRun = null;
tomlm marked this conversation as resolved.
Show resolved Hide resolved
InlineCollection inlines = new InlineCollection();
var iPos = Text.IndexOf('_', StringComparison.Ordinal);
if (iPos >= 0 && iPos < Text.Length - 1)
tomlm marked this conversation as resolved.
Show resolved Hide resolved
{
inlines.Add(new Run(Text.Substring(0, iPos)));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a hint: Text[..iPos] or Text[iPos..]

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, fancy. I totally forgot about that new feature. It hasn't been integrated into my coding patterns.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Holy shit, I just tried it...so clean, so readable, so succinct. .Substring always felt so verbose. I love it! Thanks for the tip!

_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;
}
}
break;

case nameof(ShowAccessKey):
if (_accessRun != null)
{
if (this.ShowAccessKey)
_accessRun.TextDecorations.Add(new TextDecoration { Location = TextDecorationLocation.Underline });
else
_accessRun.TextDecorations.Clear();
}
break;
}
}
}
}
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