Skip to content

Commit

Permalink
SelectableTextBlock implemented for linux
Browse files Browse the repository at this point in the history
  • Loading branch information
jinek committed Dec 19, 2024
1 parent f299a2a commit 2b60d09
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/Consolonia.Core/Dummy/DummyConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public bool CaretVisible

public bool SupportsComplexEmoji => true;
public bool SupportsAltSolo => false;
public bool SupportsMouse => false;
public bool SupportsMouseMove => false;

public void ClearOutput()
{
Expand Down
2 changes: 2 additions & 0 deletions src/Consolonia.Core/Infrastructure/DefaultNetConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public DefaultNetConsole()
}

public override bool SupportsAltSolo => false;
public override bool SupportsMouse => false;
public override bool SupportsMouseMove => false;

protected override void Dispose(bool disposing)
{
Expand Down
2 changes: 2 additions & 0 deletions src/Consolonia.Core/Infrastructure/IConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public interface IConsole : IDisposable
bool SupportsComplexEmoji { get; }

bool SupportsAltSolo { get; }
bool SupportsMouse { get; }
bool SupportsMouseMove { get; }

void SetTitle(string title);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ protected InputLessDefaultNetConsole()

public bool SupportsComplexEmoji => _supportEmoji ?? false;
public abstract bool SupportsAltSolo { get; }
public abstract bool SupportsMouse { get; }
public abstract bool SupportsMouseMove { get; }

public void SetTitle(string title)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Consolonia.Core.InternalHelpers
{
internal static class CommonInternalHelper
public static class CommonInternalHelper
{
public static bool IsNearlyEqual(this double value, double compareTo)
{
Expand Down
2 changes: 2 additions & 0 deletions src/Consolonia.NUnit/UnitTestConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public void Dispose()

public bool SupportsComplexEmoji => true;
public bool SupportsAltSolo => true;
public bool SupportsMouse => false;
public bool SupportsMouseMove => false;

public void SetTitle(string title)
{
Expand Down
2 changes: 2 additions & 0 deletions src/Consolonia.PlatformSupport/CursesConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ public CursesConsole()
}

public override bool SupportsAltSolo => false;
public override bool SupportsMouse => true;
public override bool SupportsMouseMove => false;

private void StartEventLoop()
{
Expand Down
2 changes: 2 additions & 0 deletions src/Consolonia.PlatformSupport/WindowsConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public Win32Console()
}

public override bool SupportsAltSolo => true;
public override bool SupportsMouse => true;
public override bool SupportsMouseMove => true;

public override void PauseIO(Task task)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Media;
using Avalonia.Utilities;
using Consolonia.Core.Helpers;
using Consolonia.Core.Infrastructure;
using Consolonia.Core.InternalHelpers;

namespace Consolonia.Themes.Templates.Controls.Helpers
{
internal static class SelectTextWithPointerUpExtension
{
public static readonly AttachedProperty<bool> SelectOnMouseRightUpProperty =
AvaloniaProperty.RegisterAttached<SelectableTextBlock, bool>(CommonInternalHelper.GetStyledPropertyName(),
typeof(SelectTextWithPointerUpExtension));

static SelectTextWithPointerUpExtension()
{
var console = AvaloniaLocator.Current.GetService<IConsole>();
bool supportsMouse = console.SupportsMouse;
bool supportsMoveMove = console.SupportsMouseMove;
if (!supportsMouse || supportsMoveMove)
return;

SelectOnMouseRightUpProperty.Changed.SubscribeAction(OnPropertyChanged);
}

private static void OnPropertyChanged(AvaloniaPropertyChangedEventArgs<bool> args)
{
if (args.Sender is not SelectableTextBlock selectableTextBlock) return;

selectableTextBlock.PointerReleased -= OnPointerReleased;

if (args.GetNewValue<bool>()) selectableTextBlock.PointerReleased += OnPointerReleased;
}

private static void OnPointerReleased(object sender, PointerReleasedEventArgs e)
{
// simplified copy of SelectableTextBlock.PointerMove
var tb = (SelectableTextBlock)sender;

Thickness padding = tb.Padding;

Point point = e.GetPosition(tb) - new Point(padding.Left, padding.Top);

point = new Point(
MathUtilities.Clamp(point.X, 0, Math.Max(tb.TextLayout.WidthIncludingTrailingWhitespace, 0)),
MathUtilities.Clamp(point.Y, 0, Math.Max(tb.TextLayout.Height, 0)));

TextHitTestResult hit = tb.TextLayout.HitTestPoint(point);
int textPosition = hit.TextPosition;
tb.SetCurrentValue(SelectableTextBlock.SelectionEndProperty, textPosition);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<ResourceDictionary xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:helpers="clr-namespace:Consolonia.Themes.Templates.Controls.Helpers">
<ControlTheme x:Key="{x:Type SelectableTextBlock}"
TargetType="SelectableTextBlock">
<Setter Property="SelectionBrush" Value="{StaticResource ThemeActionBackgroundBrush}"/>
<Setter Property="SelectionBrush"
Value="{StaticResource ThemeActionBackgroundBrush}" />

Check warning on line 7 in src/Consolonia.Themes/Templates/Controls/SelectableTextBlock.axaml

View workflow job for this annotation

GitHub Actions / build

"[Xaml.StaticResourceNotResolved] Resource 'ThemeActionBackgroundBrush' is not found" on /home/runner/work/Consolonia/Consolonia/src/Consolonia.Themes/Templates/Controls/SelectableTextBlock.axaml(7,40)
<Setter Property="helpers:SelectTextWithPointerUpExtension.SelectOnMouseRightUp"
Value="True" />
</ControlTheme>
</ResourceDictionary>

0 comments on commit 2b60d09

Please sign in to comment.