Skip to content

Commit

Permalink
Set bottom margin back to zero when removing Bottom Tabs (#12076)
Browse files Browse the repository at this point in the history
* Set bottom margin back to zero

* - run for android only
  • Loading branch information
PureWeen authored and rmarinho committed Jan 27, 2023
1 parent 487914f commit bf0526c
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 19 deletions.
26 changes: 15 additions & 11 deletions src/Controls/src/Core/Platform/Android/TabbedPageManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using Android.App.Roles;
using Android.Content;
using Android.Content.Res;
using Android.Graphics;
Expand Down Expand Up @@ -183,6 +184,8 @@ void RemoveTabs()

if (fragmentManager.IsAlive() && !fragmentManager.IsDestroyed)
{
SetContentBottomMargin(0);

_ = _context
.GetNavigationRootManager()
.FragmentManager
Expand Down Expand Up @@ -232,12 +235,7 @@ void RootViewChanged(object sender, EventArgs e)
return;

_tabLayoutFragment = new ViewFragment(BottomNavigationView);

var layoutContent = rootManager.RootView.FindViewById(Resource.Id.navigationlayout_content);
if (layoutContent.LayoutParameters is ViewGroup.MarginLayoutParams cl)
{
cl.BottomMargin = _context.Context.Resources.GetDimensionPixelSize(Resource.Dimension.design_bottom_navigation_height);
}
SetContentBottomMargin(_context.Context.Resources.GetDimensionPixelSize(Resource.Dimension.design_bottom_navigation_height));
}
else
{
Expand All @@ -246,11 +244,7 @@ void RootViewChanged(object sender, EventArgs e)
return;

_tabLayoutFragment = new ViewFragment(TabLayout);
var layoutContent = rootManager.RootView.FindViewById(Resource.Id.navigationlayout_content);
if (layoutContent.LayoutParameters is ViewGroup.MarginLayoutParams cl)
{
cl.BottomMargin = 0;
}
SetContentBottomMargin(0);
}

_tabplacementId = id;
Expand All @@ -262,6 +256,16 @@ void RootViewChanged(object sender, EventArgs e)
.Commit();
}

void SetContentBottomMargin(int bottomMargin)
{
var rootManager = _context.GetNavigationRootManager();
var layoutContent = rootManager.RootView?.FindViewById(Resource.Id.navigationlayout_content);
if (layoutContent != null && layoutContent.LayoutParameters is ViewGroup.MarginLayoutParams cl)
{
cl.BottomMargin = bottomMargin;
}
}

void OnChildrenCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
e.Apply((o, i, c) => SetupPage((Page)o), (o, i) => TeardownPage((Page)o), Reset);
Expand Down
7 changes: 5 additions & 2 deletions src/Controls/tests/DeviceTests/ControlsHandlerTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,11 @@ protected async Task OnFrameSetToNotEmpty(VisualElement frameworkElement, TimeSp

// Wait for the layout to propagate to the platform
await AssertionExtensions.Wait(
() => !frameworkElement.GetBoundingBox().Size.Equals(Size.Zero)
);
() =>
{
var size = frameworkElement.GetBoundingBox().Size;
return size.Height > 0 && size.Width > 0;
});

void OnBatchCommitted(object sender, Controls.Internals.EventArg<VisualElement> e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ void SetupBuilder()
{
builder.ConfigureMauiHandlers(handlers =>
{
handlers.AddHandler(typeof(VerticalStackLayout), typeof(LayoutHandler));
handlers.AddHandler(typeof(Toolbar), typeof(ToolbarHandler));
handlers.AddHandler<Page, PageHandler>();
Expand Down Expand Up @@ -53,16 +54,70 @@ await CreateHandlerAndAddToWindow<WindowHandlerStub>(new Window(navPage), async
});
}

TabbedPage CreateBasicTabbedPage()
[Theory]
#if ANDROID
[InlineData(true)]
#endif
[InlineData(false)]
public async Task NavigatingAwayFromTabbedPageResizesContentPage(bool bottomTabs)
{
return new TabbedPage()
SetupBuilder();

var tabbedPage = CreateBasicTabbedPage(bottomTabs);
var navPage = new NavigationPage(tabbedPage);

var layout1 = new VerticalStackLayout()
{
Background = SolidColorBrush.Green
};

(tabbedPage.CurrentPage as ContentPage).Content = layout1;
await CreateHandlerAndAddToWindow<WindowHandlerStub>(new Window(navPage), async (handler) =>
{
Title = "Tabbed Page",
Children =
await OnFrameSetToNotEmpty(layout1);
var pageHeight = tabbedPage.CurrentPage.Height;
var newPage = new ContentPage()
{
new ContentPage() { Title = "Page 1" }
}
Background = SolidColorBrush.Purple,
Content = new VerticalStackLayout()
};
await navPage.PushAsync(newPage);
await OnFrameSetToNotEmpty(newPage);
Assert.True(newPage.Content.Height > pageHeight);
});
}

TabbedPage CreateBasicTabbedPage(bool bottomTabs = false, bool isSmoothScrollEnabled = true, IEnumerable<Page> pages = null)
{
pages = pages ?? new List<Page>()
{
new ContentPage() { Title = "Page 1" }
};

var tabs = new TabbedPage()
{
Title = "Tabbed Page"
};

foreach (var page in pages)
{
tabs.Children.Add(page);
}

if (bottomTabs)
{
Controls.PlatformConfiguration.AndroidSpecific.TabbedPage.SetToolbarPlacement(tabs, Controls.PlatformConfiguration.AndroidSpecific.ToolbarPlacement.Bottom);
}
else
{
Controls.PlatformConfiguration.AndroidSpecific.TabbedPage.SetToolbarPlacement(tabs,
Controls.PlatformConfiguration.AndroidSpecific.ToolbarPlacement.Top);
}

Controls.PlatformConfiguration.AndroidSpecific.TabbedPage.SetIsSmoothScrollEnabled(tabs, isSmoothScrollEnabled);
return tabs;
}
}
}

0 comments on commit bf0526c

Please sign in to comment.