-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
CellBase.cs
150 lines (121 loc) · 2.55 KB
/
CellBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using Gtk;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Xamarin.Forms.Platform.GTK.Extensions;
namespace Xamarin.Forms.Platform.GTK.Cells
{
public abstract class CellBase : EventBox
{
private Cell _cell;
private int _desiredHeight;
private IList<MenuItem> _contextActions;
public Action<object, PropertyChangedEventArgs> PropertyChanged;
protected CellBase()
{
ButtonReleaseEvent += OnClick;
}
public Cell Cell
{
get { return _cell; }
set
{
if (_cell == value)
return;
if (_cell != null)
Device.BeginInvokeOnMainThread(_cell.SendDisappearing);
_cell = value;
UpdateCell();
_contextActions = Cell.ContextActions;
if (_cell != null)
Device.BeginInvokeOnMainThread(_cell.SendAppearing);
}
}
public object Item => Cell?.BindingContext;
protected bool ParentHasUnevenRows
{
get
{
var table = Cell.RealParent as TableView;
if (table != null)
return table.HasUnevenRows;
var list = Cell.RealParent as ListView;
if (list != null)
return list.HasUnevenRows;
return false;
}
}
public int DesiredHeight
{
get
{
return _desiredHeight;
}
set
{
_desiredHeight = value;
}
}
public void SetDesiredHeight(int height)
{
DesiredHeight = height;
if (IsRealized)
{
HeightRequest = DesiredHeight;
}
}
public void HandlePropertyChanged(object sender, PropertyChangedEventArgs e)
{
PropertyChanged?.Invoke(this, e);
}
protected override void OnRealized()
{
base.OnRealized();
HeightRequest = DesiredHeight;
}
protected override void OnDestroyed()
{
base.OnDestroyed();
ButtonReleaseEvent -= OnClick;
}
protected virtual void UpdateCell()
{
}
private void OnClick(object o, ButtonReleaseEventArgs args)
{
if (args.Event.Button != 3) // Right button
{
return;
}
if (_contextActions.Any())
{
OpenContextMenu();
}
}
private void OpenContextMenu()
{
var menu = new Gtk.Menu();
SetupMenuItems(menu);
menu.ShowAll();
menu.Popup();
}
private void SetupMenuItems(Gtk.Menu menu)
{
foreach (MenuItem item in Cell.ContextActions)
{
var menuItem = new ImageMenuItem(item.Text);
_ = item.ApplyNativeImageAsync(MenuItem.IconImageSourceProperty, icon =>
{
if (icon != null)
menuItem.Image = new Gtk.Image(icon);
});
menuItem.ButtonPressEvent += (sender, args) =>
{
((IMenuItemController)item).Activate();
};
menu.Add(menuItem);
}
}
}
}