1
1
using System ;
2
+ using System . Collections . Specialized ;
3
+ using ElmSharp . Wearable ;
4
+ using Xamarin . Forms . Platform . Tizen . Native . Watch ;
2
5
using EColor = ElmSharp . Color ;
3
6
4
7
namespace Xamarin . Forms . Platform . Tizen
@@ -12,6 +15,7 @@ public class PageRenderer : VisualElementRenderer<Page>
12
15
/// Native control which holds the contents.
13
16
/// </summary>
14
17
Native . Page _page ;
18
+ Lazy < MoreOption > _moreOption ;
15
19
16
20
/// <summary>
17
21
/// Default constructor.
@@ -32,6 +36,22 @@ protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
32
36
base . OnElementChanged ( e ) ;
33
37
}
34
38
39
+ protected override void OnElementReady ( )
40
+ {
41
+ if ( Device . Idiom == TargetIdiom . Watch )
42
+ {
43
+ _moreOption = new Lazy < MoreOption > ( CreateMoreOption ) ;
44
+ if ( Element . ToolbarItems is INotifyCollectionChanged items )
45
+ {
46
+ items . CollectionChanged += OnToolbarCollectionChanged ;
47
+ }
48
+ if ( Element . ToolbarItems . Count > 0 )
49
+ {
50
+ UpdateToolbarItems ( true ) ;
51
+ }
52
+ }
53
+ }
54
+
35
55
protected override void Dispose ( bool disposing )
36
56
{
37
57
if ( disposing )
@@ -40,6 +60,21 @@ protected override void Dispose(bool disposing)
40
60
{
41
61
_page . LayoutUpdated -= OnLayoutUpdated ;
42
62
}
63
+
64
+ if ( Device . Idiom == TargetIdiom . Watch )
65
+ {
66
+ if ( Element . ToolbarItems is INotifyCollectionChanged items )
67
+ {
68
+ items . CollectionChanged -= OnToolbarCollectionChanged ;
69
+ }
70
+
71
+ if ( _moreOption . IsValueCreated )
72
+ {
73
+ _moreOption . Value . Clicked -= OnMoreOptionItemClicked ;
74
+ _moreOption . Value . Items . Clear ( ) ;
75
+ _moreOption . Value . Unrealize ( ) ;
76
+ }
77
+ }
43
78
}
44
79
base . Dispose ( disposing ) ;
45
80
}
@@ -61,9 +96,26 @@ protected override void UpdateLayout()
61
96
// empty on purpose
62
97
}
63
98
64
- void UpdateBackgroundImage ( bool initiaize )
99
+ protected virtual FormsMoreOptionItem CreateMoreOptionItem ( ToolbarItem item )
65
100
{
66
- if ( initiaize && Element . BackgroundImageSource . IsNullOrEmpty ( ) )
101
+ var moreOptionItem = new FormsMoreOptionItem
102
+ {
103
+ MainText = item . Text ,
104
+ ToolbarItem = item
105
+ } ;
106
+ var icon = item . IconImageSource as FileImageSource ;
107
+ if ( icon != null )
108
+ {
109
+ var img = new ElmSharp . Image ( _moreOption . Value ) ;
110
+ img . Load ( ResourcePath . GetPath ( icon ) ) ;
111
+ moreOptionItem . Icon = img ;
112
+ }
113
+ return moreOptionItem ;
114
+ }
115
+
116
+ void UpdateBackgroundImage ( bool initialize )
117
+ {
118
+ if ( initialize && Element . BackgroundImageSource . IsNullOrEmpty ( ) )
67
119
return ;
68
120
69
121
// TODO: investigate if we can use the other image source types: stream, font, uri
@@ -78,6 +130,52 @@ void UpdateBackgroundImage(bool initiaize)
78
130
void OnLayoutUpdated ( object sender , Native . LayoutEventArgs e )
79
131
{
80
132
Element . Layout ( e . Geometry . ToDP ( ) ) ;
133
+
134
+ if ( _moreOption != null && _moreOption . IsValueCreated )
135
+ {
136
+ _moreOption . Value . Geometry = _page . Geometry ;
137
+ }
138
+ }
139
+
140
+ MoreOption CreateMoreOption ( )
141
+ {
142
+ var moreOption = new MoreOption ( _page ) ;
143
+ moreOption . Clicked += OnMoreOptionItemClicked ;
144
+ _page . Children . Add ( moreOption ) ;
145
+ moreOption . Show ( ) ;
146
+ return moreOption ;
147
+ }
148
+
149
+ void OnToolbarCollectionChanged ( object sender , EventArgs eventArgs )
150
+ {
151
+ if ( Element . ToolbarItems . Count > 0 || _moreOption . IsValueCreated )
152
+ {
153
+ UpdateToolbarItems ( false ) ;
154
+ }
155
+ }
156
+
157
+ void UpdateToolbarItems ( bool initialize )
158
+ {
159
+ //clear existing more option items and add toolbar item again on purpose.
160
+ if ( ! initialize && _moreOption . Value . Items . Count > 0 )
161
+ {
162
+ _moreOption . Value . Items . Clear ( ) ;
163
+ }
164
+
165
+ foreach ( var item in Element . ToolbarItems )
166
+ {
167
+ _moreOption . Value . Items . Add ( CreateMoreOptionItem ( item ) ) ;
168
+ }
169
+ }
170
+
171
+ void OnMoreOptionItemClicked ( object sender , MoreOptionItemEventArgs e )
172
+ {
173
+ var formsMoreOptionItem = e . Item as FormsMoreOptionItem ;
174
+ if ( formsMoreOptionItem != null )
175
+ {
176
+ ( ( IMenuItemController ) formsMoreOptionItem . ToolbarItem ) ? . Activate ( ) ;
177
+ }
178
+ _moreOption . Value . IsOpened = false ;
81
179
}
82
180
}
83
181
}
0 commit comments