Skip to content

Commit

Permalink
Merge pull request xamarin#36 from jsuarezruiz/gtk-missing-cell-handlers
Browse files Browse the repository at this point in the history
Adding some missing cell handlers
  • Loading branch information
jsuarezruiz authored Jul 7, 2017
2 parents b0a200e + 8e98dca commit 56ae9cd
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 16 deletions.
8 changes: 6 additions & 2 deletions Xamarin.Forms.Platform.GTK/Cells/CellRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@

namespace Xamarin.Forms.Platform.GTK.Cells
{
public class CellRenderer : IRegisterable
public abstract class CellRenderer : IRegisterable
{
static readonly BindableProperty RealCellProperty =
BindableProperty.CreateAttached("RealCell", typeof(Gtk.Container),
typeof(Cell), null);

EventHandler _onForceUpdateSizeRequested;
protected Cell Cell { get; private set; }

private EventHandler _onForceUpdateSizeRequested;

public virtual CellBase GetCell(Cell item, Gtk.Container reusableView, Controls.ListView listView)
{
Cell = item;

var cell = reusableView as Gtk.Container ?? GetCellWidgetInstance(item);

var cellBase = cell as CellBase;
Expand Down
36 changes: 26 additions & 10 deletions Xamarin.Forms.Platform.GTK/Cells/EntryCell.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Gtk;
using System;
using Xamarin.Forms.Platform.GTK.Controls;

namespace Xamarin.Forms.Platform.GTK.Cells
Expand All @@ -11,7 +12,7 @@ public class EntryCell : CellBase
private string _placeholder;
private VBox _root;
private Gtk.Label _textLabel;
private EntryWrapper _entry;
private EntryWrapper _entryWrapper;

public EntryCell(
string label,
Expand All @@ -29,12 +30,14 @@ public EntryCell(

_root.PackStart(_textLabel, false, false, 0);

_entry = new EntryWrapper();
_entry.Sensitive = true;
_entry.Entry.Text = text;
_entry.PlaceholderText = placeholder;
_entryWrapper = new EntryWrapper();
_entryWrapper.Sensitive = true;
_entryWrapper.Entry.Text = text;
_entryWrapper.PlaceholderText = placeholder;
_entryWrapper.Entry.Changed += OnEntryChanged;
_entryWrapper.Entry.EditingDone += OnEditingDone;

_root.PackStart(_entry, false, false, 0);
_root.PackStart(_entryWrapper, false, false, 0);
}

public string Label
Expand All @@ -61,6 +64,9 @@ public string Placeholder
set { _placeholder = value; UpdatePlaceholder(_placeholder); }
}

public event EventHandler<string> TextChanged;
public event EventHandler EditingDone;

private void UpdateLabel(string label)
{
if (_textLabel != null)
Expand All @@ -79,18 +85,28 @@ private void UpdateLabelColor(Gdk.Color textColor)

private void UpdateText(string text)
{
if (_entry != null)
if (_entryWrapper != null)
{
_entry.Entry.Text = text;
_entryWrapper.Entry.Text = text;
}
}

private void UpdatePlaceholder(string placeholder)
{
if (_entry != null)
if (_entryWrapper != null)
{
_entry.PlaceholderText = placeholder;
_entryWrapper.PlaceholderText = placeholder;
}
}

private void OnEntryChanged(object sender, EventArgs e)
{
TextChanged?.Invoke(this, _entryWrapper.Entry.Text);
}

private void OnEditingDone(object sender, EventArgs e)
{
EditingDone?.Invoke(this, EventArgs.Empty);
}
}
}
26 changes: 25 additions & 1 deletion Xamarin.Forms.Platform.GTK/Cells/EntryCellRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
using System.ComponentModel;
using System;
using System.ComponentModel;
using Xamarin.Forms.Platform.GTK.Extensions;

namespace Xamarin.Forms.Platform.GTK.Cells
{
public class EntryCellRenderer : CellRenderer
{
public override CellBase GetCell(Cell item, Gtk.Container reusableView, Controls.ListView listView)
{
var entryCell = base.GetCell(item, reusableView, listView) as EntryCell;

entryCell.TextChanged -= OnTextChanged;
entryCell.TextChanged += OnTextChanged;
entryCell.EditingDone -= OnEditingDone;
entryCell.EditingDone += OnEditingDone;

return entryCell;
}

protected override Gtk.Container GetCellWidgetInstance(Cell item)
{
var entryCell = (Xamarin.Forms.EntryCell)item;
Expand Down Expand Up @@ -45,5 +58,16 @@ protected override void CellPropertyChanged(object sender, PropertyChangedEventA
gtkEntryCell.Placeholder = entryCell.Placeholder;
}
}

private void OnTextChanged(object sender, string text)
{
((Xamarin.Forms.EntryCell)Cell).Text = text;
}

private void OnEditingDone(object sender, EventArgs e)
{
var entryCell = (IEntryCellController)Cell;
entryCell.SendCompleted();
}
}
}
9 changes: 9 additions & 0 deletions Xamarin.Forms.Platform.GTK/Cells/SwitchCell.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Gtk;
using System;

namespace Xamarin.Forms.Platform.GTK.Cells
{
Expand Down Expand Up @@ -30,6 +31,7 @@ public SwitchCell(
_checkButton = new CheckButton();
_checkButton.SetAlignment(0, 0);
_checkButton.Active = on;
_checkButton.Toggled += OnCheckButtonToggled;

_root.PackStart(_checkButton, false, false, 0);
}
Expand All @@ -46,6 +48,8 @@ public bool On
set { _on = value; UpdateOn(_on); }
}

public EventHandler<bool> Toggled;

private void UpdateText(string text)
{
if (_textLabel != null)
Expand All @@ -61,5 +65,10 @@ private void UpdateOn(bool on)
_checkButton.Active = on;
}
}

private void OnCheckButtonToggled(object sender, EventArgs e)
{
Toggled?.Invoke(this, _checkButton.Active);
}
}
}
19 changes: 16 additions & 3 deletions Xamarin.Forms.Platform.GTK/Cells/SwitchCellRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,24 @@ namespace Xamarin.Forms.Platform.GTK.Cells
{
public class SwitchCellRenderer : CellRenderer
{
public override CellBase GetCell(Cell item, Gtk.Container reusableView, Controls.ListView listView)
{
var switchCell = base.GetCell(item, reusableView, listView) as SwitchCell;

switchCell.Toggled -= OnToggled;
switchCell.Toggled += OnToggled;

return switchCell;
}

protected override Gtk.Container GetCellWidgetInstance(Cell item)
{
var switchCell = (Xamarin.Forms.SwitchCell)item;

var text = switchCell.Text ?? string.Empty;
var on = switchCell.On;

return new SwitchCell(
text,
on);
return new SwitchCell(text, on);
}

protected override void CellPropertyChanged(object sender, PropertyChangedEventArgs args)
Expand All @@ -32,5 +40,10 @@ protected override void CellPropertyChanged(object sender, PropertyChangedEventA
gtkSwitchCell.On = switchCell.On;
}
}

private void OnToggled(object sender, bool active)
{
((Xamarin.Forms.SwitchCell)Cell).On = active;
}
}
}

0 comments on commit 56ae9cd

Please sign in to comment.