Implemented Editor Placeholder and PlaceholderColor in GTK (#3135)

This commit is contained in:
Javier Suárez Ruiz 2018-06-25 11:02:28 +02:00 коммит произвёл Stephane Delcroix
Родитель 295bcdfe5e
Коммит 673cffb1ab
2 изменённых файлов: 271 добавлений и 158 удалений

Просмотреть файл

@ -2,45 +2,134 @@
namespace Xamarin.Forms.Platform.GTK.Controls namespace Xamarin.Forms.Platform.GTK.Controls
{ {
public class ScrolledTextView : ScrolledWindow public class ScrolledTextView : EventBox
{ {
private TextView _textView; private Table _table;
private ScrolledWindow _scrolledWindow;
private Gtk.Label _placeholder;
private EventBox _placeholderContainer;
private int _maxLength; private int _maxLength;
public ScrolledTextView() public ScrolledTextView()
{ {
ShadowType = ShadowType.In; _table = new Table(1, 1, true);
HscrollbarPolicy = PolicyType.Never;
VscrollbarPolicy = PolicyType.Always;
_textView = new TextView TextView = new TextView
{ {
AcceptsTab = false, AcceptsTab = false,
WrapMode = WrapMode.WordChar WrapMode = WrapMode.WordChar
}; };
_textView.Buffer.InsertText += InsertText; TextView.Buffer.InsertText += InsertText;
Add(_textView); TextView.FocusOutEvent += FocusedOut;
_scrolledWindow = new ScrolledWindow
{
ShadowType = ShadowType.In,
HscrollbarPolicy = PolicyType.Never,
VscrollbarPolicy = PolicyType.Automatic
};
_scrolledWindow.Add(TextView);
_placeholder = new Gtk.Label();
_placeholder.SetAlignment(0, 0);
_placeholderContainer = new EventBox
{
BorderWidth = 2
};
_placeholderContainer.Add(_placeholder);
_placeholderContainer.ButtonPressEvent += PlaceHolderContainerPressed;
SetBackgroundColor(TextView.Style.BaseColors[(int)StateType.Normal]);
Add(_table);
_table.Attach(_placeholderContainer, 0, 1, 0, 1, AttachOptions.Fill, AttachOptions.Fill, 0, 0);
_table.Attach(_scrolledWindow, 0, 1, 0, 1);
} }
public TextView TextView => _textView; public TextView TextView { get; }
public string PlaceholderText
{
get
{
return _placeholder.Text;
}
set
{
_placeholder.Text = GLib.Markup.EscapeText(value ?? string.Empty);
}
}
public void SetBackgroundColor(Gdk.Color color)
{
ModifyBg(StateType.Normal, color);
TextView.ModifyBase(StateType.Normal, color);
_placeholderContainer.ModifyBg(StateType.Normal, color);
}
public void SetPlaceholderTextColor(Gdk.Color color)
{
_placeholder.ModifyFg(StateType.Normal, color);
}
public void SetMaxLength(int maxLength) public void SetMaxLength(int maxLength)
{ {
_maxLength = maxLength; _maxLength = maxLength;
if (_textView.Buffer.CharCount > maxLength) if (TextView.Buffer.CharCount > maxLength)
_textView.Buffer.Text = _textView.Buffer.Text.Substring(0, maxLength); TextView.Buffer.Text = TextView.Buffer.Text.Substring(0, maxLength);
} }
protected override void OnFocusGrabbed() protected override void OnFocusGrabbed()
{ {
_textView?.GrabFocus(); TextView?.GrabFocus();
} }
void InsertText(object o, InsertTextArgs args) protected override void OnSizeAllocated(Gdk.Rectangle allocation)
{
base.OnSizeAllocated(allocation);
TextView.SetSizeRequest(allocation.Width, allocation.Height);
ShowPlaceholderIfNeeded();
}
private void InsertText(object o, InsertTextArgs args)
{ {
args.RetVal = args.Length <= _maxLength; args.RetVal = args.Length <= _maxLength;
} }
private void FocusedOut(object o, FocusOutEventArgs args)
{
ShowPlaceholderIfNeeded();
}
private void ShowPlaceholderIfNeeded()
{
if (string.IsNullOrEmpty(TextView.Buffer.Text) && !string.IsNullOrEmpty(_placeholder.Text))
{
_placeholderContainer.VisibleWindow = true;
}
else
{
_placeholderContainer.VisibleWindow = false;
}
}
private void PlaceHolderContainerPressed(object o, ButtonPressEventArgs args)
{
if (Sensitive)
{
TextView.Sensitive = true;
TextView.HasFocus = true;
TextView.GdkWindow?.Raise();
}
}
} }
} }

Просмотреть файл

@ -20,10 +20,9 @@ namespace Xamarin.Forms.Platform.GTK.Renderers
{ {
if (!Element.BackgroundColor.IsDefaultOrTransparent()) if (!Element.BackgroundColor.IsDefaultOrTransparent())
{ {
var color = Element.BackgroundColor.ToGtkColor(); var backgroundColor = Element.BackgroundColor.ToGtkColor();
Control.TextView.ModifyBg(StateType.Normal, color); Control.SetBackgroundColor(backgroundColor);
Control.TextView.ModifyBase(StateType.Normal, color);
} }
} }
@ -34,6 +33,7 @@ namespace Xamarin.Forms.Platform.GTK.Renderers
var scrolled = new ScrolledTextView(); var scrolled = new ScrolledTextView();
scrolled.TextView.Buffer.TagTable.Add(new TextTag(TextColorTagName)); scrolled.TextView.Buffer.TagTable.Add(new TextTag(TextColorTagName));
scrolled.TextView.Buffer.Changed += TextViewBufferChanged; scrolled.TextView.Buffer.Changed += TextViewBufferChanged;
scrolled.TextView.Focused += TextViewFocused; scrolled.TextView.Focused += TextViewFocused;
scrolled.TextView.FocusOutEvent += TextViewFocusedOut; scrolled.TextView.FocusOutEvent += TextViewFocusedOut;
@ -47,6 +47,8 @@ namespace Xamarin.Forms.Platform.GTK.Renderers
UpdateText(); UpdateText();
UpdateFont(); UpdateFont();
UpdateTextColor(); UpdateTextColor();
UpdatePlaceholder();
UpdatePlaceholderColor();
UpdateMaxLength(); UpdateMaxLength();
} }
@ -61,6 +63,10 @@ namespace Xamarin.Forms.Platform.GTK.Renderers
UpdateText(); UpdateText();
else if (e.PropertyName == Editor.TextColorProperty.PropertyName) else if (e.PropertyName == Editor.TextColorProperty.PropertyName)
UpdateTextColor(); UpdateTextColor();
else if (e.PropertyName == Editor.PlaceholderProperty.PropertyName)
UpdatePlaceholder();
else if (e.PropertyName == Editor.PlaceholderColorProperty.PropertyName)
UpdatePlaceholderColor();
else if (e.PropertyName == Editor.FontAttributesProperty.PropertyName) else if (e.PropertyName == Editor.FontAttributesProperty.PropertyName)
UpdateFont(); UpdateFont();
else if (e.PropertyName == Editor.FontFamilyProperty.PropertyName) else if (e.PropertyName == Editor.FontFamilyProperty.PropertyName)
@ -161,5 +167,23 @@ namespace Xamarin.Forms.Platform.GTK.Renderers
{ {
Control.SetMaxLength(Element.MaxLength); Control.SetMaxLength(Element.MaxLength);
} }
private void UpdatePlaceholder()
{
if (Element.Placeholder != Control.PlaceholderText)
{
Control.PlaceholderText = Element.Placeholder;
}
}
private void UpdatePlaceholderColor()
{
if (!Element.PlaceholderColor.IsDefaultOrTransparent())
{
var placeholderColor = Element.PlaceholderColor.ToGtkColor();
Control.SetPlaceholderTextColor(placeholderColor);
}
}
} }
} }