Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Make Editor focused background color behavior consistent with Entry o…
Browse files Browse the repository at this point in the history
…n Windows
  • Loading branch information
hartez committed Oct 17, 2016
1 parent aa969f3 commit 4d1fa5e
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 5 deletions.
@@ -0,0 +1,44 @@
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;

namespace Xamarin.Forms.Controls
{
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Bugzilla, 44584,
"UWP - Editor: changing the background color will only take effect after the entry gained focus")]
public class Bugzilla44584 : TestContentPage
{
protected override void Init()
{
var instructions = new Label
{
Text = @"
Tap the first button once to turn the Entry background color to Green. Tap the Entry to focus it; the background should remain green; if it does not, the test has failed.
Tap the second button once to turn the Editor background color to Green. Tap the Editor to focus it; the background should remain green; if it does not, the test has failed."
};

var entryButton = new Button { Text = "Toggle Entry Background (Green/Default)" };
var entry = new Entry();

entryButton.Clicked +=
(sender, args) => { entry.BackgroundColor = entry.BackgroundColor != Color.Green ? Color.Green : Color.Default; };

var editorButton = new Button { Text = "Toggle Editor Background (Green/Default)" };
var editor = new Editor()
{
HeightRequest = 80
};

editorButton.Clicked +=
(sender, args) => { editor.BackgroundColor = editor.BackgroundColor != Color.Green ? Color.Green : Color.Default; };

var layout = new StackLayout
{
VerticalOptions = LayoutOptions.Center,
Children = { instructions, entryButton, entry, editorButton, editor }
};

Content = layout;
}
}
}
Expand Up @@ -128,6 +128,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla43516.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla44166.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla44461.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla44584.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla42832.cs" />
<Compile Include="$(MSBuildThisFileDirectory)CarouselAsync.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla34561.cs" />
Expand Down
29 changes: 25 additions & 4 deletions Xamarin.Forms.Platform.WinRT/EditorRenderer.cs
@@ -1,6 +1,7 @@
using System.ComponentModel;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;

#if WINDOWS_UWP

Expand All @@ -10,17 +11,23 @@ namespace Xamarin.Forms.Platform.UWP
namespace Xamarin.Forms.Platform.WinRT
#endif
{
public class EditorRenderer : ViewRenderer<Editor, TextBox>
public class EditorRenderer : ViewRenderer<Editor, FormsTextBox>
{
bool _fontApplied;
Brush _backgroundColorFocusedDefaultBrush;

protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
{
if (e.NewElement != null)
{
if (Control == null)
{
var textBox = new TextBox { AcceptsReturn = true, TextWrapping = TextWrapping.Wrap };
var textBox = new FormsTextBox
{
AcceptsReturn = true,
TextWrapping = TextWrapping.Wrap,
Style = Windows.UI.Xaml.Application.Current.Resources["FormsTextBoxStyle"] as Windows.UI.Xaml.Style
};

SetNativeControl(textBox);

Expand Down Expand Up @@ -50,6 +57,8 @@ protected override void Dispose(bool disposing)

protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);

if (e.PropertyName == Editor.TextColorProperty.PropertyName)
{
UpdateTextColor();
Expand All @@ -70,15 +79,27 @@ protected override void OnElementPropertyChanged(object sender, PropertyChangedE
{
UpdateText();
}

base.OnElementPropertyChanged(sender, e);
}

void OnLostFocus(object sender, RoutedEventArgs e)
{
Element.SendCompleted();
}

protected override void UpdateBackgroundColor()
{
base.UpdateBackgroundColor();

if (Control == null)
{
return;
}

// By default some platforms have alternate default background colors when focused
BrushHelpers.UpdateColor(Element.BackgroundColor, ref _backgroundColorFocusedDefaultBrush,
() => Control.BackgroundFocusBrush, brush => Control.BackgroundFocusBrush = brush);
}

void OnNativeTextChanged(object sender, Windows.UI.Xaml.Controls.TextChangedEventArgs args)
{
Element.SetValueCore(Editor.TextProperty, Control.Text);
Expand Down
2 changes: 1 addition & 1 deletion Xamarin.Forms.Platform.WinRT/FormsTextBox.cs
Expand Up @@ -111,7 +111,7 @@ protected override void OnApplyTemplate()
if (Device.Idiom == TargetIdiom.Phone)
{
// If we're on the phone, we need to grab this from the template
// so we can manually handle it's background when focused
// so we can manually handle its background when focused
_borderElement = (Border)GetTemplateChild("BorderElement");
}
}
Expand Down

0 comments on commit 4d1fa5e

Please sign in to comment.