Skip to content

Commit

Permalink
Fixes #334 by adding SelectAllTextOnFocus to Spinner
Browse files Browse the repository at this point in the history
  • Loading branch information
batzen committed Aug 13, 2016
1 parent afb6806 commit 5dfcec2
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 18 deletions.
7 changes: 5 additions & 2 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
## 5.0.0 (preview)

- ### Breaking changes
- Office 2010 and Windows 8 themes got removed. Office 2013 theme was moved from Themes/Office2013/Generic.xaml to Themes/Generic.xaml. This was a [community voted decision](../../issues/282).
- Office 2010 and Windows 8 themes got removed.
Office 2013 theme was moved from "Themes/Office2013/Generic.xaml" to "Themes/Generic.xaml".
This was a [community voted decision](../../issues/282).
- [#301](../../issues/301) - Remove Office 2010 and Windows 8 themes
- [#302](../../issues/302) - Rename Office 2013 theme to Generic
- [#309](../../issues/309) - Remove grouping from ComboBox and make GalleryPanel inherit from StackPanel
Expand All @@ -24,7 +26,8 @@
- [#279](../../issues/279) - Localization of ColorGallery
- [#299](../../issues/299) - Quick access items should show item text as tool tip if no tooltip is set
- [#324](../../issues/324) - Add "IsSeparatorVisible" to RibbonGroupBox (thanks to @maurosampietro)
- [#326](../../issues/326) - Add interface for controls which provide LargeIcon
- [#326](../../issues/326) - Add interface for controls which provide LargeIcon
- [#334](../../issues/334) - Select all text in Spinner on focus

## 4.0.3

Expand Down
3 changes: 2 additions & 1 deletion Fluent.Ribbon.Showcase/TestContent.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2187,7 +2187,8 @@
InputWidth="75"
Format="0 px"
Header="Right:"
Icon="pack://application:,,,/Fluent;component/Themes/Images/Warning.png" />
Icon="pack://application:,,,/Fluent;component/Themes/Images/Warning.png"
SelectAllTextOnFocus="True" />
<Fluent:Spinner KeyTip="KC"
InputWidth="75"
Format="0 px"
Expand Down
66 changes: 51 additions & 15 deletions Fluent.Ribbon/Controls/Spinner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,19 @@ private static void OnValueChanged(DependencyObject d, DependencyPropertyChanged
var spinner = (Spinner)d;
spinner.ValueToTextBoxText();

if (spinner.ValueChanged != null)
{
spinner.ValueChanged(spinner, new RoutedPropertyChangedEventArgs<double>((double)e.OldValue, (double)e.NewValue));
}
spinner.ValueChanged?.Invoke(spinner, new RoutedPropertyChangedEventArgs<double>((double)e.OldValue, (double)e.NewValue));
}

private void ValueToTextBoxText()
{
if (this.IsTemplateValid())
if (this.textBox == null)
{
var newText = (string)this.TextToValueConverter.ConvertBack(this.Value, typeof(string), this.Format, CultureInfo.CurrentCulture);
this.textBox.Text = newText;
this.Text = newText;
return;
}

var newText = (string)this.TextToValueConverter.ConvertBack(this.Value, typeof(string), this.Format, CultureInfo.CurrentCulture);
this.textBox.Text = newText;
this.Text = newText;
}

#endregion
Expand Down Expand Up @@ -327,6 +326,21 @@ public IValueConverter TextToValueConverter

#endregion TextToValueConverter

/// <summary>
/// Defines wether all text should be select as soon as this control gets focus.
/// </summary>
public bool SelectAllTextOnFocus
{
get { return (bool)this.GetValue(SelectAllTextOnFocusProperty); }
set { this.SetValue(SelectAllTextOnFocusProperty, value); }
}

/// <summary>
/// <see cref="DependencyProperty"/> for <see cref="SelectAllTextOnFocus"/>
/// </summary>
public static readonly DependencyProperty SelectAllTextOnFocusProperty =
DependencyProperty.Register("SelectAllTextOnFocus", typeof(bool), typeof(Spinner), new PropertyMetadata(false));

#endregion

#region Constructors
Expand Down Expand Up @@ -355,12 +369,22 @@ static Spinner()
/// </summary>
public override void OnApplyTemplate()
{
if (this.IsTemplateValid())
if (this.buttonUp != null)
{
this.buttonUp.Click -= this.OnButtonUpClick;
BindingOperations.ClearAllBindings(this.buttonUp);
}

if (this.buttonDown != null)
{
this.buttonDown.Click -= this.OnButtonDownClick;
BindingOperations.ClearAllBindings(this.buttonDown);
BindingOperations.ClearAllBindings(this.buttonUp);
}

if (this.textBox != null)
{
this.textBox.LostKeyboardFocus -= this.OnTextBoxLostKeyboardFocus;
this.textBox.PreviewKeyDown -= this.OnTextBoxPreviewKeyDown;
}

// Get template childs
Expand All @@ -381,10 +405,10 @@ public override void OnApplyTemplate()
Bind(this, this.buttonUp, "Interval", RepeatButton.IntervalProperty, BindingMode.OneWay);
Bind(this, this.buttonDown, "Interval", RepeatButton.IntervalProperty, BindingMode.OneWay);


// Events subscribing
this.buttonUp.Click += this.OnButtonUpClick;
this.buttonDown.Click += this.OnButtonDownClick;
this.textBox.GotFocus += this.HandleTextBoxGotFocus;
this.textBox.LostKeyboardFocus += this.OnTextBoxLostKeyboardFocus;
this.textBox.PreviewKeyDown += this.OnTextBoxPreviewKeyDown;

Expand All @@ -407,24 +431,36 @@ private bool IsTemplateValid()
/// </summary>
public override void OnKeyTipPressed()
{
if (this.IsTemplateValid() == false)
if (this.textBox == null)
{
return;
}

// Use dispatcher to avoid focus moving to backup'ed element
// (focused element before keytips processing)
this.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle,
this.Dispatcher.BeginInvoke(DispatcherPriority.Background,
(ThreadStart)(() =>
{
this.textBox.SelectAll();
this.textBox.Focus();
}));
base.OnKeyTipPressed();
}

private void HandleTextBoxGotFocus(object sender, RoutedEventArgs e)
{
if (this.SelectAllTextOnFocus)
{
// Async because setting the carret happens after focus.
this.Dispatcher.BeginInvoke(DispatcherPriority.Background,
(ThreadStart)(() =>
{
this.textBox.SelectAll();
}));
}
}

/// <summary>
/// Invoked when an unhandled System.Windows.Input.Keyboard.KeyUpattached event reaches
/// Invoked when an unhandled System.Windows.Input.Keyboard.KeyUp attached event reaches
/// an element in its route that is derived from this class. Implement this method to add class handling for this event.
/// </summary>
/// <param name="e">The System.Windows.Input.KeyEventArgs that contains the event data.</param>
Expand Down

0 comments on commit 5dfcec2

Please sign in to comment.