Skip to content

Commit

Permalink
Add the ability to triple click text boxes to select a whole line (#765)
Browse files Browse the repository at this point in the history
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
ScrubN authored Jul 31, 2023
1 parent 87b28c7 commit 3ea62f0
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 0 deletions.
66 changes: 66 additions & 0 deletions TwitchDownloaderWPF/Behaviors/TextBoxTripleClickBehavior.cs
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);
}
}
}
7 changes: 7 additions & 0 deletions TwitchDownloaderWPF/PageChatDownload.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@
lex:ResxLocalizationProvider.DefaultAssembly="TwitchDownloaderWPF"
lex:ResxLocalizationProvider.DefaultDictionary="Strings"
xmlns:local="clr-namespace:TwitchDownloaderWPF"
xmlns:behave="clr-namespace:TwitchDownloaderWPF.Behaviors"
mc:Ignorable="d"
d:DesignHeight="400" d:DesignWidth="800"
Title="PageChatDownload" Initialized="Page_Initialized" Loaded="Page_Loaded">
<Page.Resources>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="behave:TextBoxTripleClickBehavior.TripleClickSelectLine" Value="True" />
<Setter Property="Template" Value="{StaticResource TextBoxExtendLeftTemplate}" />
</Style>
</Page.Resources>

<Grid Background="{DynamicResource AppBackground}">
<Grid.ColumnDefinitions>
Expand Down
7 changes: 7 additions & 0 deletions TwitchDownloaderWPF/PageChatRender.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
xmlns:hc="https://handyorg.github.io/handycontrol"
xmlns:fa="http://schemas.fontawesome.com/icons/"
xmlns:local="clr-namespace:TwitchDownloaderWPF"
xmlns:behave="clr-namespace:TwitchDownloaderWPF.Behaviors"
xmlns:lex="http://wpflocalizeextension.codeplex.com"
lex:LocalizeDictionary.DesignCulture=""
lex:ResxLocalizationProvider.DefaultAssembly="TwitchDownloaderWPF"
Expand All @@ -16,6 +17,12 @@
d:DesignHeight="400" d:DesignWidth="800"
Title="PageChatRender" Initialized="Page_Initialized" Unloaded="Page_Unloaded" Loaded="Page_Loaded"
>
<Page.Resources>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="behave:TextBoxTripleClickBehavior.TripleClickSelectLine" Value="True" />
<Setter Property="Template" Value="{StaticResource TextBoxExtendLeftTemplate}" />
</Style>
</Page.Resources>

<Grid Background="{DynamicResource AppBackground}">
<Grid.ColumnDefinitions>
Expand Down
7 changes: 7 additions & 0 deletions TwitchDownloaderWPF/PageChatUpdate.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,20 @@
xmlns:hc="https://handyorg.github.io/handycontrol"
xmlns:fa="http://schemas.fontawesome.com/icons/"
xmlns:local="clr-namespace:TwitchDownloaderWPF"
xmlns:behave="clr-namespace:TwitchDownloaderWPF.Behaviors"
xmlns:lex="http://wpflocalizeextension.codeplex.com"
lex:LocalizeDictionary.DesignCulture=""
lex:ResxLocalizationProvider.DefaultAssembly="TwitchDownloaderWPF"
lex:ResxLocalizationProvider.DefaultDictionary="Strings"
mc:Ignorable="d"
d:DesignHeight="400" d:DesignWidth="800"
Title="PageChatUpdate" Initialized="Page_Initialized" Loaded="Page_Loaded">
<Page.Resources>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="behave:TextBoxTripleClickBehavior.TripleClickSelectLine" Value="True" />
<Setter Property="Template" Value="{StaticResource TextBoxExtendLeftTemplate}" />
</Style>
</Page.Resources>

<Grid Background="{DynamicResource AppBackground}">
<Grid.ColumnDefinitions>
Expand Down
7 changes: 7 additions & 0 deletions TwitchDownloaderWPF/PageClipDownload.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,16 @@
xmlns:hc="https://handyorg.github.io/handycontrol"
xmlns:fa="http://schemas.fontawesome.com/icons/"
xmlns:local="clr-namespace:TwitchDownloaderWPF"
xmlns:behave="clr-namespace:TwitchDownloaderWPF.Behaviors"
mc:Ignorable="d"
d:DesignHeight="400" d:DesignWidth="800"
Title="PageClipDownload" Initialized="Page_Initialized" Loaded="Page_Loaded">
<Page.Resources>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="behave:TextBoxTripleClickBehavior.TripleClickSelectLine" Value="True" />
<Setter Property="Template" Value="{StaticResource TextBoxExtendLeftTemplate}" />
</Style>
</Page.Resources>

<Grid Background="{DynamicResource AppBackground}">
<Grid.ColumnDefinitions>
Expand Down
7 changes: 7 additions & 0 deletions TwitchDownloaderWPF/PageVodDownload.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
xmlns:local="clr-namespace:TwitchDownloaderWPF"
xmlns:behave="clr-namespace:TwitchDownloaderWPF.Behaviors"
xmlns:lex="http://wpflocalizeextension.codeplex.com"
lex:LocalizeDictionary.DesignCulture=""
lex:ResxLocalizationProvider.DefaultAssembly="TwitchDownloaderWPF"
Expand All @@ -17,6 +18,12 @@
mc:Ignorable="d"
d:DesignHeight="400" d:DesignWidth="800"
Title="PageVodDownload" Initialized="Page_Initialized" Loaded="Page_Loaded">
<Page.Resources>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="behave:TextBoxTripleClickBehavior.TripleClickSelectLine" Value="True" />
<Setter Property="Template" Value="{StaticResource TextBoxExtendLeftTemplate}" />
</Style>
</Page.Resources>

<Grid Background="{DynamicResource AppBackground}">
<Grid.ColumnDefinitions>
Expand Down
8 changes: 8 additions & 0 deletions TwitchDownloaderWPF/WindowMassDownload.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TwitchDownloaderWPF"
xmlns:behave="clr-namespace:TwitchDownloaderWPF.Behaviors"
xmlns:lex="http://wpflocalizeextension.codeplex.com"
lex:LocalizeDictionary.DesignCulture=""
lex:ResxLocalizationProvider.DefaultAssembly="TwitchDownloaderWPF"
lex:ResxLocalizationProvider.DefaultDictionary="Strings"
xmlns:emoji="clr-namespace:Emoji.Wpf;assembly=Emoji.Wpf"
mc:Ignorable="d"
Title="Mass Downloader" MinHeight="250" Height="700" MinWidth="775" Width="1100" Loaded="Window_Loaded">
<Window.Resources>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="behave:TextBoxTripleClickBehavior.TripleClickSelectLine" Value="True" />
<Setter Property="Template" Value="{StaticResource TextBoxExtendLeftTemplate}" />
</Style>
</Window.Resources>

<Grid Background="{DynamicResource AppBackground}">
<StackPanel Orientation="Horizontal">
<Label x:Name="labelSort" Content="{lex:Loc Sort}" HorizontalAlignment="Left" Margin="10,6,0,0" VerticalAlignment="Top" Background="{DynamicResource AppElementBackground}" BorderBrush="{DynamicResource AppElementBorder}" Foreground="{DynamicResource AppText}"/>
Expand Down
8 changes: 8 additions & 0 deletions TwitchDownloaderWPF/WindowQueueOptions.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TwitchDownloaderWPF"
xmlns:behave="clr-namespace:TwitchDownloaderWPF.Behaviors"
xmlns:lex="http://wpflocalizeextension.codeplex.com"
lex:LocalizeDictionary.DesignCulture=""
lex:ResxLocalizationProvider.DefaultAssembly="TwitchDownloaderWPF"
lex:ResxLocalizationProvider.DefaultDictionary="Strings"
mc:Ignorable="d"
Title="Queue Options" MinHeight="240" Height="240" MinWidth="500" Width="500" Loaded="Window_loaded">
<Window.Resources>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="behave:TextBoxTripleClickBehavior.TripleClickSelectLine" Value="True" />
<Setter Property="Template" Value="{StaticResource TextBoxExtendLeftTemplate}" />
</Style>
</Window.Resources>

<Grid Background="{DynamicResource AppBackground}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="6"/>
Expand Down
8 changes: 8 additions & 0 deletions TwitchDownloaderWPF/WindowRangeSelect.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TwitchDownloaderWPF"
xmlns:behave="clr-namespace:TwitchDownloaderWPF.Behaviors"
xmlns:lex="http://wpflocalizeextension.codeplex.com"
lex:LocalizeDictionary.DesignCulture=""
lex:ResxLocalizationProvider.DefaultAssembly="TwitchDownloaderWPF"
lex:ResxLocalizationProvider.DefaultDictionary="Strings"
mc:Ignorable="d"
Title="Select Render Range (Seconds)" Height="150" Width="400" Background="#FFEFEFEF" Initialized="Window_Initialized" Loaded="Window_Loaded">
<Window.Resources>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="behave:TextBoxTripleClickBehavior.TripleClickSelectLine" Value="True" />
<Setter Property="Template" Value="{StaticResource TextBoxExtendLeftTemplate}" />
</Style>
</Window.Resources>

<Grid Background="{DynamicResource AppBackground}">
<StackPanel Orientation="Vertical">
<hc:RangeSlider x:Name="rangeTime" IsSnapToTickEnabled="True" Height="20" Margin="20,10,20,0" ValueChanged="rangeTime_ValueChanged" />
Expand Down
8 changes: 8 additions & 0 deletions TwitchDownloaderWPF/WindowSettings.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TwitchDownloaderWPF"
xmlns:behave="clr-namespace:TwitchDownloaderWPF.Behaviors"
xmlns:lex="http://wpflocalizeextension.codeplex.com"
lex:LocalizeDictionary.DesignCulture=""
lex:ResxLocalizationProvider.DefaultAssembly="TwitchDownloaderWPF"
Expand All @@ -12,6 +13,13 @@
xmlns:fa="http://schemas.fontawesome.com/icons/"
mc:Ignorable="d"
Title="Global Settings" MinWidth="400" MinHeight="460" Width="500" Height="547" Initialized="Window_Initialized" Closing="Window_Closing">
<Window.Resources>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="behave:TextBoxTripleClickBehavior.TripleClickSelectLine" Value="True" />
<Setter Property="Template" Value="{StaticResource TextBoxExtendLeftTemplate}" />
</Style>
</Window.Resources>

<Grid Background="{DynamicResource AppBackground}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="8"/>
Expand Down
8 changes: 8 additions & 0 deletions TwitchDownloaderWPF/WindowUrlList.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TwitchDownloaderWPF"
xmlns:behave="clr-namespace:TwitchDownloaderWPF.Behaviors"
xmlns:lex="http://wpflocalizeextension.codeplex.com"
lex:LocalizeDictionary.DesignCulture=""
lex:ResxLocalizationProvider.DefaultAssembly="TwitchDownloaderWPF"
lex:ResxLocalizationProvider.DefaultDictionary="Strings"
mc:Ignorable="d"
Title="Mass Download URL List" MinHeight="590" Height="600" MinWidth="485" Width="500" Loaded="Window_Loaded">
<Window.Resources>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="behave:TextBoxTripleClickBehavior.TripleClickSelectLine" Value="True" />
<Setter Property="Template" Value="{StaticResource TextBoxExtendLeftTemplate}" />
</Style>
</Window.Resources>

<Grid Background="{DynamicResource AppBackground}">
<Button x:Name="btnQueue" Content="{lex:Loc AddToQueue}" HorizontalAlignment="Center" VerticalAlignment="Bottom" MinWidth="110" Height="45" Margin="0,0,0,10" Click="btnQueue_Click" Background="{DynamicResource ActionButtonBackground}" Foreground="{DynamicResource ActionButtonText}" BorderBrush="{DynamicResource ActionButtonBorder}"/>
<Label Content="{lex:Loc ListOfVodsClips}" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Background="{DynamicResource AppElementBackground}" BorderBrush="{DynamicResource AppElementBorder}" Foreground="{DynamicResource AppText}"/>
Expand Down

0 comments on commit 3ea62f0

Please sign in to comment.