Skip to content

Commit 678dc37

Browse files
author
Ludwig Chilla (aka TenEniunlsl)
committed
TabbedPage some bugs fixed and implementation finished
1 parent 7c95c4e commit 678dc37

File tree

4 files changed

+286
-66
lines changed

4 files changed

+286
-66
lines changed

src/Controls/samples/Controls.Sample/Pages/Compatibility/TabbedPageGalleryMainPage.xaml.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
using Microsoft.Extensions.DependencyInjection;
44
using Microsoft.Maui.Controls;
55
using Microsoft.Maui.Graphics;
6-
using AndroidSpecific = Microsoft.Maui.Controls.PlatformConfiguration.AndroidSpecific;
6+
#if GTK
7+
using GTKSpecific = Microsoft.Maui.Controls.PlatformConfiguration.GTKSpecific;
8+
#else
9+
using AndroidSpecific = Microsoft.Maui.Controls.PlatformConfiguration.AndroidSpecific;
10+
#endif
711

812
namespace Maui.Controls.Sample.Pages
913
{
@@ -49,7 +53,11 @@ void OnSetToBottomTabs(object sender, EventArgs e)
4953
};
5054

5155
SetNewMainPage(bottomTabs);
56+
#if GTK
57+
GTKSpecific.TabbedPage.SetTabPosition(bottomTabs, GTKSpecific.TabPosition.Bottom);
58+
#else
5259
AndroidSpecific.TabbedPage.SetToolbarPlacement(bottomTabs, AndroidSpecific.ToolbarPlacement.Bottom);
60+
#endif
5361
Application.Current!.MainPage = bottomTabs;
5462
}
5563

Lines changed: 142 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Collections.Specialized;
4-
using System.Text;
3+
using System.Linq;
54
using Gdk;
6-
using GLib;
75
using Gtk;
8-
using Microsoft.Maui.Controls.Handlers.Items;
9-
using Microsoft.Maui.Controls.Handlers.Items.Platform;
106
using Microsoft.Maui.Controls.Platform;
11-
using Microsoft.Maui.Handlers;
12-
using Microsoft.Maui.Platform;
7+
using Microsoft.Maui.Controls.PlatformConfiguration.GTKSpecific;
138

149
namespace Microsoft.Maui.Controls
1510
{
@@ -20,14 +15,37 @@ public partial class TabbedPage
2015
MauiTabbedPage CreatePlatformView(TabbedPage tabbedPage)
2116
{
2217
var mauiTabbedPage = new MauiTabbedPage();
18+
19+
tabbedPage.PropertyChanged += (s, e) =>
20+
{
21+
if (e.PropertyName == PlatformConfiguration.GTKSpecific.TabbedPage.TabPositionProperty.PropertyName)
22+
{
23+
UpdateTabPosition(tabbedPage, mauiTabbedPage);
24+
}
25+
};
2326

24-
CreateChildPages(tabbedPage, mauiTabbedPage);
27+
UpdateChildPages(tabbedPage, mauiTabbedPage);
2528

2629
mauiTabbedPage.SwitchPage += MauiTabbedPage_SwitchPage;
2730

2831
return mauiTabbedPage;
2932
}
3033

34+
private void UpdateTabPosition(TabbedPage tabbedPage, MauiTabbedPage mauiTabbedPage)
35+
{
36+
var pos = PlatformConfiguration.GTKSpecific.TabbedPage.GetTabPosition(tabbedPage);
37+
38+
mauiTabbedPage.TabPos = pos switch
39+
{
40+
TabPosition.Top => Gtk.PositionType.Top,
41+
TabPosition.Bottom => Gtk.PositionType.Bottom,
42+
_ => Gtk.PositionType.Top
43+
};
44+
45+
mauiTabbedPage.QueueResize();
46+
mauiTabbedPage.QueueDraw();
47+
}
48+
3149
private void MauiTabbedPage_SwitchPage(object sender, SwitchPageArgs args)
3250
{
3351
var newPage = this.Children[(int)args.PageNum];
@@ -44,31 +62,100 @@ private void MauiTabbedPage_SwitchPage(object sender, SwitchPageArgs args)
4462
return null;
4563
}
4664

47-
private void CreateChildPages(TabbedPage tabbedPage, MauiTabbedPage mauiTabbedPage)
65+
private static void UpdateChildPages(TabbedPage tabbedPage, MauiTabbedPage mauiTabbedPage)
4866
{
49-
if (tabbedPage?.Handler?.MauiContext == null)
67+
var mauiContext = tabbedPage.Handler?.MauiContext;
68+
if (mauiContext == null)
5069
return;
5170

52-
foreach (var page in tabbedPage.Children)
53-
{
54-
var platformChild = page.ToPlatform(tabbedPage.Handler.MauiContext);
71+
var navigationItems = mauiTabbedPage.NavigationItems;
5572

56-
Gtk.Image? image = null;
73+
var toRemove = new List<int>();
5774

58-
if (page.IconImageSource != null)
75+
for (int i = 0; i < navigationItems.Count; i++)
76+
{
77+
var navItem = navigationItems[i];
78+
var hasItem = tabbedPage.Children.Any(x => x.Id == navItem.Id);
79+
80+
if (!hasItem)
5981
{
60-
var task = page.IconImageSource.GetPlatformImageAsync(tabbedPage.Handler.MauiContext);
61-
task.Wait();
82+
toRemove.Add(i);
83+
}
84+
}
85+
86+
foreach (var idx in toRemove)
87+
{
88+
navigationItems.RemoveAt(idx);
89+
mauiTabbedPage.RemovePage(idx);
90+
}
6291

63-
var pixBuf = task.Result?.Value;
64-
if (pixBuf != null)
92+
for (int i = 0; i < tabbedPage.Children.Count; i++)
93+
{
94+
var page = tabbedPage.Children[i];
95+
var existingNavItem = navigationItems.FirstOrDefault(x => x.Id == page.Id);
96+
97+
if (existingNavItem != default)
98+
{
99+
existingNavItem.Order = i;
100+
existingNavItem.TabPageLabel.TextLabel.Text = page.Title;
101+
if (page.IconImageSource != null)
65102
{
66-
image = new Gtk.Image(pixBuf.ScaleSimple(16, 16, InterpType.Bilinear));
103+
var newIconSourceHash = page.IconImageSource.GetHashCode();
104+
if (existingNavItem.TabPageLabel.IconSourceHash != page.IconImageSource.GetHashCode())
105+
{
106+
existingNavItem.TabPageLabel.Icon = GetImage(page.IconImageSource, mauiContext);
107+
existingNavItem.TabPageLabel.IconSourceHash = newIconSourceHash;
108+
}
109+
}
110+
else
111+
{
112+
existingNavItem.TabPageLabel.Icon = null;
113+
existingNavItem.TabPageLabel.IconSourceHash = 0;
67114
}
68115
}
116+
else
117+
{
118+
var platformChild = page.ToPlatform(mauiContext);
119+
120+
var image = GetImage(page.IconImageSource, mauiContext);
121+
var iconSourceHash = page.IconImageSource?.GetHashCode() ?? 0;
69122

70-
mauiTabbedPage.AppendPage(platformChild, new MauiTabbedPage.MauiTabbedPageLabel(page.Title, image));
123+
var navItem = new MauiTabbedPage.MauiTabbedPageNaviagtionItem(platformChild, new MauiTabbedPage.MauiTabPageLabel(page.Title, image, iconSourceHash), page.Id)
124+
{
125+
Order = i
126+
};
127+
128+
navigationItems.Add(navItem);
129+
navItem.TabPageLabel.Show();
130+
}
71131
}
132+
133+
mauiTabbedPage.UpdateTabPages();
134+
135+
mauiTabbedPage.BarBackgroundColor = tabbedPage.BarBackgroundColor;
136+
mauiTabbedPage.SelectedItemTextColor = tabbedPage.SelectedTabColor;
137+
mauiTabbedPage.UnselectedItemTextColor = tabbedPage.UnselectedTabColor;
138+
139+
int index = tabbedPage.Children.IndexOf(tabbedPage.CurrentPage);
140+
mauiTabbedPage.CurrentPage = index;
141+
}
142+
143+
private static Gtk.Image? GetImage(ImageSource imageSource, IMauiContext mauiContext)
144+
{
145+
Gtk.Image? image = null;
146+
147+
if (imageSource != null)
148+
{
149+
var task = imageSource.GetPlatformImageAsync(mauiContext);
150+
task.Wait();
151+
152+
var pixBuf = task.Result?.Value;
153+
if (pixBuf != null)
154+
{
155+
image = new Gtk.Image(pixBuf.ScaleSimple(16, 16, InterpType.Bilinear));
156+
}
157+
}
158+
return image;
72159
}
73160

74161
internal static void MapBarBackground(ITabbedViewHandler handler, TabbedPage view)
@@ -84,38 +171,59 @@ internal static void MapBarBackgroundColor(ITabbedViewHandler handler, TabbedPag
84171

85172
internal static void MapBarTextColor(ITabbedViewHandler handler, TabbedPage view)
86173
{
87-
view.PlatformView.SetLabelTextColor(view.BarTextColor);
174+
view.PlatformView.BarTextColor = view.BarTextColor;
88175
}
89176

90177
internal static void MapUnselectedTabColor(ITabbedViewHandler handler, TabbedPage view)
91178
{
92-
view.PlatformView.UnselectedColor = view.UnselectedTabColor;
179+
view.PlatformView.UnselectedItemTextColor = view.UnselectedTabColor;
93180
}
94181

95182
internal static void MapSelectedTabColor(ITabbedViewHandler handler, TabbedPage view)
96183
{
97-
view.PlatformView.SelectedColor = view.SelectedTabColor;
184+
view.PlatformView.SelectedItemTextColor = view.SelectedTabColor;
98185
}
99-
100-
internal static void MapItemsSource(ITabbedViewHandler handler, TabbedPage view)
186+
187+
internal static void MapItemsSource(ITabbedViewHandler handler, TabbedPage tabbedPage)
101188
{
102-
//not needed
189+
if (tabbedPage?.Handler?.MauiContext == null)
190+
return;
191+
192+
var mauiTabbedPage = tabbedPage.PlatformView;
193+
194+
UpdateChildPages(tabbedPage, mauiTabbedPage);
195+
196+
handler.UpdateValue(nameof(TabbedPage.CurrentPage));
103197
}
104198

105-
internal static void MapItemTemplate(ITabbedViewHandler handler, TabbedPage view)
199+
internal static void MapItemTemplate(ITabbedViewHandler handler, TabbedPage tabbedPage)
106200
{
107-
//not needed
201+
if (tabbedPage?.Handler?.MauiContext == null)
202+
return;
203+
204+
var mauiTabbedPage = tabbedPage.PlatformView;
205+
206+
UpdateChildPages(tabbedPage, mauiTabbedPage);
207+
208+
handler.UpdateValue(nameof(TabbedPage.CurrentPage));
108209
}
109210

110-
internal static void MapSelectedItem(ITabbedViewHandler handler, TabbedPage view)
211+
internal static void MapSelectedItem(ITabbedViewHandler handler, TabbedPage tabbedPage)
111212
{
112-
//not needed
213+
if (tabbedPage?.Handler?.MauiContext == null)
214+
return;
215+
216+
var mauiTabbedPage = tabbedPage.PlatformView;
217+
218+
UpdateChildPages(tabbedPage, mauiTabbedPage);
219+
220+
handler.UpdateValue(nameof(TabbedPage.CurrentPage));
113221
}
114222

115-
internal static void MapCurrentPage(ITabbedViewHandler handler, TabbedPage view)
223+
internal static void MapCurrentPage(ITabbedViewHandler handler, TabbedPage tabbedPage)
116224
{
117-
int index = view.Children.IndexOf(view.CurrentPage);
118-
view.PlatformView.CurrentPage = index;
225+
int index = tabbedPage.Children.IndexOf(tabbedPage.CurrentPage);
226+
tabbedPage.PlatformView.CurrentPage = index;
119227
}
120228
}
121229
}

src/Core/src/Handlers/Window/WindowHandler.Gtk.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ public static void MapContent(IWindowHandler handler, IWindow window)
2929
}
3030
else
3131
{
32+
if (handler.PlatformView.Child != null)
33+
{
34+
handler.PlatformView.Remove(handler.PlatformView.Child);
35+
}
3236
handler.PlatformView.Child = platformContent;
3337
}
3438
}

0 commit comments

Comments
 (0)