-
Notifications
You must be signed in to change notification settings - Fork 311
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1356 from oxygen-dioxide/numberedit
When inputting invalid number, textbox won't show yellow error message
- Loading branch information
Showing
4 changed files
with
140 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using System; | ||
|
||
using Avalonia; | ||
using Avalonia.Controls; | ||
using Avalonia.Data; | ||
using Avalonia.Interactivity; | ||
using ReactiveUI; | ||
|
||
namespace OpenUtau.Controls{ | ||
public class FloatEditor : TextBox | ||
{ | ||
protected override Type StyleKeyOverride => typeof(TextBox); | ||
public static readonly DirectProperty<FloatEditor, float> ValueProperty = | ||
AvaloniaProperty.RegisterDirect<FloatEditor, float>( | ||
nameof(Value), | ||
o => o.Value, | ||
(o, v) => o.Value = v, | ||
defaultBindingMode: BindingMode.TwoWay); | ||
private float _value = 0; | ||
|
||
public FloatEditor() | ||
{ | ||
Text = "0"; | ||
this.WhenAnyValue(x => x.Text) | ||
.Subscribe((text => { | ||
OnTextChanged(text); | ||
})); | ||
} | ||
|
||
public float Value | ||
{ | ||
get => _value; | ||
set | ||
{ | ||
if (SetAndRaise(ValueProperty, ref _value, value)) | ||
{ | ||
Text = value.ToString(); | ||
} | ||
} | ||
} | ||
|
||
protected void OnTextChanged(string? newText) | ||
{ | ||
if (!IsKeyboardFocusWithin){ | ||
return; | ||
} | ||
|
||
if( newText != null && float.TryParse(newText, out float newValue)) | ||
{ | ||
Value = newValue; | ||
} | ||
} | ||
|
||
protected override void OnLostFocus(RoutedEventArgs e) { | ||
base.OnLostFocus(e); | ||
if (!float.TryParse(Text, out float newValue)) | ||
{ | ||
Text = Value.ToString(); | ||
} | ||
} | ||
} | ||
} |
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,62 @@ | ||
using System; | ||
|
||
using Avalonia; | ||
using Avalonia.Controls; | ||
using Avalonia.Data; | ||
using Avalonia.Interactivity; | ||
using ReactiveUI; | ||
|
||
namespace OpenUtau.Controls{ | ||
public class IntEditor : TextBox | ||
{ | ||
protected override Type StyleKeyOverride => typeof(TextBox); | ||
public static readonly DirectProperty<IntEditor, int> ValueProperty = | ||
AvaloniaProperty.RegisterDirect<IntEditor, int>( | ||
nameof(Value), | ||
o => o.Value, | ||
(o, v) => o.Value = v, | ||
defaultBindingMode: BindingMode.TwoWay); | ||
private int _value = 0; | ||
|
||
public IntEditor() | ||
{ | ||
Text = "0"; | ||
this.WhenAnyValue(x => x.Text) | ||
.Subscribe((text => { | ||
OnTextChanged(text); | ||
})); | ||
} | ||
|
||
public int Value | ||
{ | ||
get => _value; | ||
set | ||
{ | ||
if (SetAndRaise(ValueProperty, ref _value, value)) | ||
{ | ||
Text = value.ToString() ?? ""; | ||
} | ||
} | ||
} | ||
|
||
protected void OnTextChanged(string? newText) | ||
{ | ||
if (!IsKeyboardFocusWithin){ | ||
return; | ||
} | ||
|
||
if( newText != null && int.TryParse(newText, out int newValue)) | ||
{ | ||
Value = newValue; | ||
} | ||
} | ||
|
||
protected override void OnLostFocus(RoutedEventArgs e) { | ||
base.OnLostFocus(e); | ||
if (!int.TryParse(Text, out int newValue)) | ||
{ | ||
Text = Value.ToString(); | ||
} | ||
} | ||
} | ||
} |
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