-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[Enhancement] Entry: Read-only Entry and Editor #1972
Changes from all commits
f4efb90
8a0efac
c95b5b4
63428f3
3911b45
613704f
c36ea78
2d83e01
40423ee
ffaa298
c1eb7fd
d7360bc
2339be5
8e71c91
0b6a421
eddeab2
6734fdb
9855fb9
55d52eb
d83a47e
89a9125
1b3509e
571764e
4b21b84
e314ef7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
using Xamarin.Forms.CustomAttributes; | ||
using Xamarin.Forms.Internals; | ||
|
||
namespace Xamarin.Forms.Controls.Issues | ||
{ | ||
[Preserve(AllMembers = true)] | ||
[Issue(IssueTracker.Github, 1678, "[Enhancement] Entry: Read-only entry", PlatformAffected.All)] | ||
public class Issue1678 | ||
: TestContentPage | ||
{ | ||
protected override void Init() | ||
{ | ||
var entryText = "Entry Lorem Ipsum"; | ||
var editorText = "Editor Lorem Ipsum"; | ||
|
||
var entryDefaults = new Entry { Text = entryText }; | ||
var editorDefaults = new Editor { Text = editorText }; | ||
var entryReadOnly = new Entry { Text = entryText, IsReadOnly = true }; | ||
var editorReadOnly = new Editor { Text = editorText, IsReadOnly = true }; | ||
var entryToggleable = new Entry { Text = entryText }; | ||
var editorToggleable = new Editor { Text = editorText }; | ||
|
||
var toggle = new Switch { IsToggled = false }; | ||
|
||
var stackLayout = new StackLayout(); | ||
stackLayout.Children.Add(new Label { Text = "Defaults" }); | ||
stackLayout.Children.Add(entryDefaults); | ||
stackLayout.Children.Add(editorDefaults); | ||
stackLayout.Children.Add(new Label { Text = "Read Only" }); | ||
stackLayout.Children.Add(entryReadOnly); | ||
stackLayout.Children.Add(editorReadOnly); | ||
stackLayout.Children.Add(new Label { Text = "Toggleable is read only" }); | ||
stackLayout.Children.Add(entryToggleable); | ||
stackLayout.Children.Add(editorToggleable); | ||
stackLayout.Children.Add(toggle); | ||
|
||
toggle.Toggled += (_, b) => | ||
{ | ||
entryToggleable.IsReadOnly = b.Value; | ||
editorToggleable.IsReadOnly = b.Value; | ||
}; | ||
|
||
stackLayout.Padding = new Thickness(0, 20, 0, 0); | ||
Content = stackLayout; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,7 @@ protected override void OnElementChanged(ElementChangedEventArgs<Editor> e) | |
UpdateTextColor(); | ||
UpdateEditable(); | ||
UpdateMaxLength(); | ||
UpdateIsReadOnly(); | ||
} | ||
|
||
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) | ||
|
@@ -62,6 +63,8 @@ protected override void OnElementPropertyChanged(object sender, PropertyChangedE | |
UpdateFont(); | ||
else if (e.PropertyName == InputView.MaxLengthProperty.PropertyName) | ||
UpdateMaxLength(); | ||
else if (e.PropertyName == Xamarin.Forms.InputView.IsReadOnlyProperty.PropertyName) | ||
UpdateIsReadOnly(); | ||
} | ||
|
||
protected override void SetBackgroundColor(Color color) | ||
|
@@ -148,5 +151,12 @@ void UpdateMaxLength() | |
if (currentControlText.Length > Element?.MaxLength) | ||
Control.StringValue = currentControlText.Substring(0, Element.MaxLength); | ||
} | ||
|
||
void UpdateIsReadOnly() | ||
{ | ||
Control.Editable = !Element.IsReadOnly; | ||
if (Element.IsReadOnly && Control.Window?.FirstResponder == Control.CurrentEditor) | ||
Control.Window?.MakeFirstResponder(null); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -132,6 +132,8 @@ protected override void OnElementPropertyChanged(object sender, PropertyChangedE | |
UpdateAlignment(); | ||
else if (e.PropertyName == InputView.MaxLengthProperty.PropertyName) | ||
UpdateMaxLength(); | ||
else if (e.PropertyName == Xamarin.Forms.InputView.IsReadOnlyProperty.PropertyName) | ||
UpdateIsReadOnly(); | ||
|
||
base.OnElementPropertyChanged(sender, e); | ||
} | ||
|
@@ -201,7 +203,8 @@ void UpdateControl() | |
UpdateFont(); | ||
UpdateAlignment(); | ||
UpdateMaxLength(); | ||
} | ||
UpdateIsReadOnly(); | ||
} | ||
|
||
void TextFieldFocusChanged(object sender, BoolEventArgs e) | ||
{ | ||
|
@@ -289,5 +292,13 @@ void UpdateMaxLength() | |
if (currentControlText.Length > Element?.MaxLength) | ||
Control.StringValue = currentControlText.Substring(0, Element.MaxLength); | ||
} | ||
|
||
|
||
void UpdateIsReadOnly() | ||
{ | ||
Control.Editable = !Element.IsReadOnly; | ||
if (Element.IsReadOnly && Control.Window?.FirstResponder == Control.CurrentEditor) | ||
Control.Window?.MakeFirstResponder(null); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ public EntryRenderer() | |
RegisterPropertyHandler(Entry.IsTextPredictionEnabledProperty, UpdateIsSpellCheckEnabled); | ||
RegisterPropertyHandler(Specific.FontWeightProperty, UpdateFontWeight); | ||
RegisterPropertyHandler(Entry.SelectionLengthProperty, UpdateSelectionLength); | ||
RegisterPropertyHandler(InputView.IsReadOnlyProperty, UpdateIsReadOnly); | ||
} | ||
|
||
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e) | ||
|
@@ -198,5 +199,10 @@ int GetCursorPosition() | |
|
||
return Element.Text.IndexOf(selection, Math.Max(Control.CursorPosition - selection.Length, 0)); | ||
} | ||
|
||
void UpdateIsReadOnly() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can move this above There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is fine. |
||
{ | ||
Control.IsEditable = !Element.IsReadOnly; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MacOS/iOS/gtk looks to have a quirk where if the field is the first responder when it goes readonly the field remains editable until it's no longer the first responder
UWP nothing happens with these focuses
Android the keyboard shows up which is a little awkward
Another way to see this is to add focus calls here.
I'm wondering if ShouldBeginEditing would be a more thorough way to go?
https://stackoverflow.com/questions/7949071/iphone-ipad-how-to-make-uitextfield-readonly-but-not-disabled
Wire that up and return false if Element.IsReadOnly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for suggestion, I will try :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comments I made earlier on this look to still apply