diff options
author | Jason Smith <jason.smith@xamarin.com> | 2016-03-22 13:02:25 -0700 |
---|---|---|
committer | Jason Smith <jason.smith@xamarin.com> | 2016-03-22 16:13:41 -0700 |
commit | 17fdde66d94155fc62a034fa6658995bef6fd6e5 (patch) | |
tree | b5e5073a2a7b15cdbe826faa5c763e270a505729 /Xamarin.Forms.Platform.WinRT/EditorRenderer.cs | |
download | xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.tar.gz xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.tar.bz2 xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.zip |
Initial import
Diffstat (limited to 'Xamarin.Forms.Platform.WinRT/EditorRenderer.cs')
-rw-r--r-- | Xamarin.Forms.Platform.WinRT/EditorRenderer.cs | 155 |
1 files changed, 155 insertions, 0 deletions
diff --git a/Xamarin.Forms.Platform.WinRT/EditorRenderer.cs b/Xamarin.Forms.Platform.WinRT/EditorRenderer.cs new file mode 100644 index 00000000..d6236871 --- /dev/null +++ b/Xamarin.Forms.Platform.WinRT/EditorRenderer.cs @@ -0,0 +1,155 @@ +using System.ComponentModel; +using Windows.UI.Xaml; +using Windows.UI.Xaml.Controls; + +#if WINDOWS_UWP + +namespace Xamarin.Forms.Platform.UWP +#else + +namespace Xamarin.Forms.Platform.WinRT +#endif +{ + public class EditorRenderer : ViewRenderer<Editor, TextBox> + { + bool _fontApplied; + + protected override void OnElementChanged(ElementChangedEventArgs<Editor> e) + { + if (e.NewElement != null) + { + if (Control == null) + { + var textBox = new TextBox { AcceptsReturn = true, TextWrapping = TextWrapping.Wrap }; + + textBox.TextChanged += OnNativeTextChanged; + textBox.LostFocus += OnLostFocus; + + SetNativeControl(textBox); + } + + UpdateText(); + UpdateInputScope(); + UpdateTextColor(); + UpdateFont(); + } + + base.OnElementChanged(e); + } + + protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) + { + if (e.PropertyName == Editor.TextColorProperty.PropertyName) + { + UpdateTextColor(); + } + else if (e.PropertyName == Editor.FontAttributesProperty.PropertyName) + { + UpdateFont(); + } + else if (e.PropertyName == Editor.FontFamilyProperty.PropertyName) + { + UpdateFont(); + } + else if (e.PropertyName == Editor.FontSizeProperty.PropertyName) + { + UpdateFont(); + } + else if (e.PropertyName == Editor.TextProperty.PropertyName) + { + UpdateText(); + } + + base.OnElementPropertyChanged(sender, e); + } + + void OnLostFocus(object sender, RoutedEventArgs e) + { + Element?.SendCompleted(); + } + + void OnNativeTextChanged(object sender, Windows.UI.Xaml.Controls.TextChangedEventArgs args) + { + Element?.SetValueCore(Editor.TextProperty, Control.Text); + } + + void UpdateFont() + { + if (Control == null) + return; + + Editor editor = Element; + + if (editor == null) + return; + + bool editorIsDefault = editor.FontFamily == null && editor.FontSize == Device.GetNamedSize(NamedSize.Default, typeof(Editor), true) && editor.FontAttributes == FontAttributes.None; + + if (editorIsDefault && !_fontApplied) + return; + + if (editorIsDefault) + { + // ReSharper disable AccessToStaticMemberViaDerivedType + // Resharper wants to simplify 'TextBox' to 'Control', but then it'll conflict with the property 'Control' + Control.ClearValue(TextBox.FontStyleProperty); + Control.ClearValue(TextBox.FontSizeProperty); + Control.ClearValue(TextBox.FontFamilyProperty); + Control.ClearValue(TextBox.FontWeightProperty); + Control.ClearValue(TextBox.FontStretchProperty); + // ReSharper restore AccessToStaticMemberViaDerivedType + } + else + { + Control.ApplyFont(editor); + } + + _fontApplied = true; + } + + void UpdateInputScope() + { + var custom = Element.Keyboard as CustomKeyboard; + if (custom != null) + { + Control.IsTextPredictionEnabled = (custom.Flags & KeyboardFlags.Suggestions) != 0; + Control.IsSpellCheckEnabled = (custom.Flags & KeyboardFlags.Spellcheck) != 0; + } + else + { + Control.ClearValue(TextBox.IsTextPredictionEnabledProperty); + Control.ClearValue(TextBox.IsSpellCheckEnabledProperty); + } + + Control.InputScope = Element.Keyboard.ToInputScope(); + } + + void UpdateText() + { + string newText = Element.Text ?? ""; + + if (Control.Text == newText) + { + return; + } + + Control.Text = newText; + Control.SelectionStart = Control.Text.Length; + } + + void UpdateTextColor() + { + Color textColor = Element.TextColor; + + if (textColor.IsDefault || !Element.IsEnabled) + { + // ReSharper disable once AccessToStaticMemberViaDerivedType + Control.ClearValue(TextBox.ForegroundProperty); + } + else + { + Control.Foreground = textColor.ToBrush(); + } + } + } +}
\ No newline at end of file |