-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SelectableTextBlock implemented for linux
- Loading branch information
Showing
10 changed files
with
78 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/Consolonia.Themes/Templates/Controls/Helpers/SelectTextWithPointerUpExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
8 changes: 6 additions & 2 deletions
8
src/Consolonia.Themes/Templates/Controls/SelectableTextBlock.axaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / build
|
||
<Setter Property="helpers:SelectTextWithPointerUpExtension.SelectOnMouseRightUp" | ||
Value="True" /> | ||
</ControlTheme> | ||
</ResourceDictionary> |