Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit 8629dbe

Browse files
authored
[Tizen] Enable Page.ToolbarItem on Watch (#10145)
1 parent 5ba5a2d commit 8629dbe

File tree

2 files changed

+109
-2
lines changed

2 files changed

+109
-2
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using ElmSharp.Wearable;
2+
3+
namespace Xamarin.Forms.Platform.Tizen.Native.Watch
4+
{
5+
public class FormsMoreOptionItem : MoreOptionItem
6+
{
7+
public ToolbarItem ToolbarItem { get; set; }
8+
}
9+
}

Xamarin.Forms.Platform.Tizen/Renderers/PageRenderer.cs

Lines changed: 100 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
using System;
2+
using System.Collections.Specialized;
3+
using ElmSharp.Wearable;
4+
using Xamarin.Forms.Platform.Tizen.Native.Watch;
25
using EColor = ElmSharp.Color;
36

47
namespace Xamarin.Forms.Platform.Tizen
@@ -12,6 +15,7 @@ public class PageRenderer : VisualElementRenderer<Page>
1215
/// Native control which holds the contents.
1316
/// </summary>
1417
Native.Page _page;
18+
Lazy<MoreOption> _moreOption;
1519

1620
/// <summary>
1721
/// Default constructor.
@@ -32,6 +36,22 @@ protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
3236
base.OnElementChanged(e);
3337
}
3438

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+
3555
protected override void Dispose(bool disposing)
3656
{
3757
if (disposing)
@@ -40,6 +60,21 @@ protected override void Dispose(bool disposing)
4060
{
4161
_page.LayoutUpdated -= OnLayoutUpdated;
4262
}
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+
}
4378
}
4479
base.Dispose(disposing);
4580
}
@@ -61,9 +96,26 @@ protected override void UpdateLayout()
6196
// empty on purpose
6297
}
6398

64-
void UpdateBackgroundImage(bool initiaize)
99+
protected virtual FormsMoreOptionItem CreateMoreOptionItem(ToolbarItem item)
65100
{
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())
67119
return;
68120

69121
// TODO: investigate if we can use the other image source types: stream, font, uri
@@ -78,6 +130,52 @@ void UpdateBackgroundImage(bool initiaize)
78130
void OnLayoutUpdated(object sender, Native.LayoutEventArgs e)
79131
{
80132
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;
81179
}
82180
}
83181
}

0 commit comments

Comments
 (0)