-
-
Notifications
You must be signed in to change notification settings - Fork 12
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
203-selectabletextblock-does-not-work #219
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f299a2a
1) fixed right click on linux
jinek 2b60d09
SelectableTextBlock implemented for linux
jinek 74c92d4
PR build failed
jinek 8b11a94
more PR fixes
jinek 4143b0d
Automated JetBrains cleanup
github-actions[bot] 2b3bf20
Merge branch 'main' into 203-selectabletextblock-does-not-work
jinek 068ba88
renaming
jinek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
next level magic.
I haven't ever created a mixin extension like this. Xaml is so freaking powerful as a model.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True. Its supposed to be simpler with "behaviours", they manage lifetime etc, but Avalonia does not have them builtin, i dont want to include that assembly to core level packages of consolonia.