-
-
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.
203-selectabletextblock-does-not-work (#219)
- Loading branch information
Showing
12 changed files
with
97 additions
and
7 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
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
60 changes: 60 additions & 0 deletions
60
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,60 @@ | ||
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> SelectOnMouseLeftUpProperty = | ||
AvaloniaProperty.RegisterAttached<SelectableTextBlock, bool>(CommonInternalHelper.GetStyledPropertyName(), | ||
typeof(SelectTextWithPointerUpExtension)); | ||
|
||
static SelectTextWithPointerUpExtension() | ||
{ | ||
var console = AvaloniaLocator.Current.GetService<IConsole>(); | ||
bool supportsMouse = console.SupportsMouse; | ||
bool supportsMouseMove = console.SupportsMouseMove; | ||
if (!supportsMouse || supportsMouseMove) | ||
return; | ||
|
||
SelectOnMouseLeftUpProperty.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; | ||
|
||
if (e.InitialPressMouseButton != MouseButton.Left) | ||
return; | ||
|
||
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); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<ResourceDictionary xmlns="https://github.com/avaloniaui" | ||
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="{DynamicResource ThemeActionBackgroundBrush}" /> | ||
<Setter Property="helpers:SelectTextWithPointerUpExtension.SelectOnMouseLeftUp" | ||
Value="True" /> | ||
</ControlTheme> | ||
</ResourceDictionary> |