Skip to content

Commit

Permalink
fix(textbox): Update on Foreground brush Color changed
Browse files Browse the repository at this point in the history
Subscribe to changes to SolidColorBrush.Color when set as TextBox.Foreground.
  • Loading branch information
davidjohnoliver committed Apr 12, 2021
1 parent 59a1d8a commit 96b7371
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 18 deletions.
13 changes: 11 additions & 2 deletions src/Uno.UI/UI/Xaml/Controls/TextBox/MultilineTextBoxView.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Windows.UI.Xaml.Media;
using Uno.UI.Controls;
using Windows.UI;
using Uno.Disposables;

namespace Windows.UI.Xaml.Controls
{
Expand All @@ -21,6 +22,7 @@ public partial class MultilineTextBoxView : UITextView, ITextBoxView, Dependency
private MultilineTextBoxDelegate _delegate;
private readonly WeakReference<TextBox> _textBox;
private WeakReference<Uno.UI.Controls.Window> _window;
private readonly SerialDisposable _foregroundChanged = new SerialDisposable();

CGPoint IUIScrollView.UpperScrollLimit { get { return (CGPoint)(ContentSize - Frame.Size); } }

Expand Down Expand Up @@ -179,6 +181,7 @@ public Brush Foreground

public void OnForegroundChanged(Brush oldValue, Brush newValue)
{
_foregroundChanged.Disposable = null;
var textBox = _textBox.GetTarget();

if (textBox != null)
Expand All @@ -187,8 +190,14 @@ public void OnForegroundChanged(Brush oldValue, Brush newValue)

if (scb != null)
{
this.TextColor = scb.Color;
this.TintColor = scb.Color;
_foregroundChanged.Disposable = Brush.AssignAndObserveBrush(scb, _ => ApplyColor());
ApplyColor();

void ApplyColor()
{
this.TextColor = scb.Color;
this.TintColor = scb.Color;
}
}
}
}
Expand Down
13 changes: 11 additions & 2 deletions src/Uno.UI/UI/Xaml/Controls/TextBox/SinglelineTextBoxView.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
using Windows.UI.Xaml.Media;
using Uno.UI.Controls;
using Windows.UI;
using Uno.Disposables;

namespace Windows.UI.Xaml.Controls
{
public partial class SinglelineTextBoxView : UITextField, ITextBoxView, DependencyObject, IFontScalable
{
private SinglelineTextBoxDelegate _delegate;
private readonly WeakReference<TextBox> _textBox;
private readonly SerialDisposable _foregroundChanged = new SerialDisposable();

public SinglelineTextBoxView(TextBox textBox)
{
Expand Down Expand Up @@ -161,6 +163,7 @@ public Brush Foreground

public void OnForegroundChanged(Brush oldValue, Brush newValue)
{
_foregroundChanged.Disposable = null;
var textBox = _textBox.GetTarget();

if (textBox != null)
Expand All @@ -169,8 +172,14 @@ public void OnForegroundChanged(Brush oldValue, Brush newValue)

if (scb != null)
{
this.TextColor = scb.Color;
this.TintColor = scb.Color;
_foregroundChanged.Disposable = Brush.AssignAndObserveBrush(scb, _ => ApplyColor());
ApplyColor();

void ApplyColor()
{
TextColor = scb.Color;
TintColor = scb.Color;
}
}
}
}
Expand Down
14 changes: 12 additions & 2 deletions src/Uno.UI/UI/Xaml/Controls/TextBox/TextBoxView.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using Uno.UI.DataBinding;
using AndroidX.Core.Content;
using AndroidX.Core.Graphics;
using Uno.Disposables;

namespace Windows.UI.Xaml.Controls
{
Expand All @@ -33,6 +34,8 @@ internal partial class TextBoxView : EditText, DependencyObject
private readonly ManagedWeakReference? _ownerRef;
internal TextBox? Owner => _ownerRef?.Target as TextBox;

private readonly SerialDisposable _foregroundChanged = new SerialDisposable();

public TextBoxView(TextBox owner)
: base(ContextHelper.Current)
{
Expand Down Expand Up @@ -267,12 +270,19 @@ Brush Foreground

private void OnForegroundChanged(Brush oldValue, Brush newValue)
{
_foregroundChanged.Disposable = null;
var scb = newValue as SolidColorBrush;

if (scb != null)
{
this.SetTextColor(scb.Color);
this.SetCursorColor(scb.Color);
_foregroundChanged.Disposable = Brush.AssignAndObserveBrush(scb, _ => ApplyColor());
ApplyColor();

void ApplyColor()
{
SetTextColor(scb.Color);
SetCursorColor(scb.Color);
}
}
}
}
Expand Down
27 changes: 19 additions & 8 deletions src/Uno.UI/UI/Xaml/Controls/TextBox/TextBoxView.macOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using AppKit;
using _TextField = AppKit.NSTextField;
using Windows.UI;
using Uno.Disposables;

namespace Windows.UI.Xaml.Controls
{
Expand All @@ -18,6 +19,8 @@ internal partial class TextBoxView : _TextField, ITextBoxView, DependencyObject,
private TextBoxViewDelegate _delegate;
private readonly WeakReference<TextBox> _textBox;

private readonly SerialDisposable _foregroundChanged = new SerialDisposable();

public TextBoxView(TextBox textBox)
{
_textBox = new WeakReference<TextBox>(textBox);
Expand Down Expand Up @@ -162,16 +165,24 @@ public Brush Foreground

public void OnForegroundChanged(Brush oldValue, Brush newValue)
{
var textBox = _textBox.GetTarget();
_foregroundChanged.Disposable = null;

if (textBox != null && Brush.TryGetColorWithOpacity(newValue, out var color))
{
this.TextColor = color;
UpdateCaretColor(color);
}
else
_foregroundChanged.Disposable = Brush.AssignAndObserveBrush(newValue, _ => ApplyColor());
ApplyColor();

void ApplyColor()
{
UpdateCaretColor();
var textBox = _textBox.GetTarget();

if (textBox != null && Brush.TryGetColorWithOpacity(newValue, out var color))
{
this.TextColor = color;
UpdateCaretColor(color);
}
else
{
UpdateCaretColor();
}
}
}

Expand Down
18 changes: 14 additions & 4 deletions src/Uno.UI/UI/Xaml/Controls/TextBox/TextBoxView.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@
using Windows.Foundation;
using System.Globalization;
using Uno.UI.UI.Xaml.Documents;
using Uno.Disposables;

namespace Windows.UI.Xaml.Controls
{
internal partial class TextBoxView : FrameworkElement
{
private readonly TextBox _textBox;
private readonly SerialDisposable _foregroundChanged = new SerialDisposable();

public Brush Foreground
{
get => (Brush)GetValue(ForegroundProperty);
set => SetValue(ForegroundProperty, value);
}

internal static DependencyProperty ForegroundProperty { get ; } =
internal static DependencyProperty ForegroundProperty { get; } =
DependencyProperty.Register(
name: "Foreground",
propertyType: typeof(Brush),
Expand All @@ -32,7 +34,15 @@ public Brush Foreground
propertyChangedCallback: (s, e) => (s as TextBoxView)?.OnForegroundChanged(e)));

private void OnForegroundChanged(DependencyPropertyChangedEventArgs e)
=> this.SetForeground(e.NewValue);
{
_foregroundChanged.Disposable = null;
if (e.NewValue is SolidColorBrush scb)
{
_foregroundChanged.Disposable = Brush.AssignAndObserveBrush(scb, _ => this.SetForeground(e.NewValue));
}

this.SetForeground(e.NewValue);
}

public TextBoxView(TextBox textBox, bool isMultiline)
: base(isMultiline ? "textarea" : "input")
Expand Down Expand Up @@ -62,7 +72,7 @@ private event EventHandler HtmlInput
private protected override void OnLoaded()
{
base.OnLoaded();

HtmlInput += OnInput;

SetTextNative(_textBox.Text);
Expand All @@ -71,7 +81,7 @@ private protected override void OnLoaded()
private protected override void OnUnloaded()
{
base.OnUnloaded();

HtmlInput -= OnInput;
}

Expand Down

0 comments on commit 96b7371

Please sign in to comment.