-
Notifications
You must be signed in to change notification settings - Fork 260
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the ability to triple click text boxes to select a whole line (#765)
Note: The behavior must be defined everywhere that it is used; defining it just once within app.xaml breaks the default template.
- Loading branch information
Showing
11 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
TwitchDownloaderWPF/Behaviors/TextBoxTripleClickBehavior.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,66 @@ | ||
using System.Windows; | ||
using System.Windows.Controls; | ||
using System.Windows.Input; | ||
|
||
namespace TwitchDownloaderWPF.Behaviors | ||
{ | ||
public class TextBoxTripleClickBehavior : DependencyObject | ||
{ | ||
public static readonly DependencyProperty TripleClickSelectLineProperty = DependencyProperty.RegisterAttached( | ||
nameof(TripleClickSelectLine), typeof(bool), typeof(TextBoxTripleClickBehavior), new PropertyMetadata(false, OnPropertyChanged)); | ||
|
||
private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | ||
{ | ||
if (d is not TextBox textBox) | ||
{ | ||
return; | ||
} | ||
|
||
var enable = (bool)e.NewValue; | ||
if (enable) | ||
{ | ||
textBox.PreviewMouseLeftButtonDown += OnTextBoxMouseDown; | ||
} | ||
else | ||
{ | ||
textBox.PreviewMouseLeftButtonDown -= OnTextBoxMouseDown; | ||
} | ||
} | ||
|
||
private static void OnTextBoxMouseDown(object sender, MouseButtonEventArgs e) | ||
{ | ||
if (e.ClickCount == 3 && sender is TextBox textBox) | ||
{ | ||
var (start, length) = GetCurrentLine(textBox); | ||
textBox.Select(start, length); | ||
} | ||
} | ||
|
||
private static (int start, int length) GetCurrentLine(TextBox textBox) | ||
{ | ||
var caretPos = textBox.CaretIndex; | ||
var text = textBox.Text; | ||
|
||
var start = text.LastIndexOf('\n', caretPos, caretPos); | ||
var end = text.IndexOf('\n', caretPos); | ||
|
||
if (start == -1) | ||
{ | ||
start = 0; | ||
} | ||
|
||
if (end == -1) | ||
{ | ||
end = text.Length; | ||
} | ||
|
||
return (start, end - start); | ||
} | ||
|
||
public bool TripleClickSelectLine | ||
{ | ||
get => (bool)GetValue(TripleClickSelectLineProperty); | ||
set => SetValue(TripleClickSelectLineProperty, value); | ||
} | ||
} | ||
} |
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