Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A tab button can now be set to not take the full space within the tab bar, also exposed the button's Background property in TabButton. #140

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Tabs/Tabs/TabButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public class TabButton : TabItem
typeof(ICommand),
typeof(TabButton));

public static readonly BindableProperty ButtonBackgroundProperty = BindableProperty.Create(
nameof(ButtonBackground),
typeof(Brush),
typeof(TabButton));

public static readonly BindableProperty ButtonBackgroundColorProperty = BindableProperty.Create(
nameof(ButtonBackgroundColor),
typeof(Color),
Expand Down Expand Up @@ -46,6 +51,11 @@ public class TabButton : TabItem
typeof(double),
typeof(TabButton));

public static readonly BindableProperty ExpandToTabSizeProperty = BindableProperty.Create(
nameof(ExpandToTabSize),
typeof(bool),
typeof(TabButton));

private ImageButton _imageButton;

public TabButton()
Expand Down Expand Up @@ -77,6 +87,12 @@ public int CornerRadius
set => SetValue(CornerRadiusProperty, value);
}

public Brush ButtonBackground
{
get => (Brush)GetValue(ButtonBackgroundProperty);
set => SetValue(ButtonBackgroundProperty, value);
}

public Color ButtonBackgroundColor
{
get => (Color)GetValue(ButtonBackgroundColorProperty);
Expand Down Expand Up @@ -107,6 +123,17 @@ public double ButtonCircleSize
set => SetValue(ButtonCircleSizeProperty, value);
}

/// <summary>
/// When this property is disabled, the button occupies the calculated width (for horizontal tab bars) or height (for vertical tab bars), letting other tabs' content be properly centered within the free space.
/// When this property is enabled, the button occupies the same width (for horizontal tab bars) or height (for vertical tab bars) as the other tabs.
/// </summary>
public bool ExpandToTabSize
{
get => (bool)GetValue(ExpandToTabSizeProperty);
set => SetValue(ExpandToTabSizeProperty, value);
}


protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
base.OnPropertyChanged(propertyName);
Expand All @@ -115,6 +142,10 @@ protected override void OnPropertyChanged([CallerMemberName] string propertyName
{
case nameof(CornerRadius):
UpdateCornerRadius();
break;

case nameof(ButtonBackground):
_imageButton.Background = ButtonBackground;
break;

case nameof(ButtonBackgroundColor):
Expand Down Expand Up @@ -201,6 +232,7 @@ private void UpdateButtonCircleSize()
}
}


private void Initialize()
{
_imageButton = new ImageButton
Expand All @@ -215,8 +247,10 @@ private void Initialize()

Content = _imageButton;

ExpandToTabSize = true;
IsSelectable = false;

_imageButton.Background = ButtonBackground;
_imageButton.BackgroundColor = ButtonBackgroundColor;
_imageButton.Source = IconImageSource;
_imageButton.Command = TapCommand;
Expand Down
37 changes: 28 additions & 9 deletions Tabs/Tabs/TabHostView.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using System.Collections;
using Microsoft.Maui.Controls.Shapes;
using Sharpnado.Tabs.Effects;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Windows.Input;

using Microsoft.Maui.Controls.Shapes;

using Sharpnado.Tabs.Effects;

namespace Sharpnado.Tabs;

public enum TabType
Expand Down Expand Up @@ -420,7 +418,7 @@ private void OnChildAdded(TabItem tabItem, int index)
tabIndexInGrid,
new ColumnDefinition
{
Width = TabType == TabType.Fixed ? GridLength.Star : GridLength.Auto,
Width = ((TabType == TabType.Fixed) && ((tabItem is not TabButton tabButton) || tabButton.ExpandToTabSize)) ? GridLength.Star : GridLength.Auto,
});

if (TabType == TabType.Scrollable)
Expand Down Expand Up @@ -449,7 +447,7 @@ private void OnChildAdded(TabItem tabItem, int index)
tabIndexInGrid,
new RowDefinition
{
Height = TabType == TabType.Fixed ? GridLength.Star : GridLength.Auto,
Height = ((TabType == TabType.Fixed) && ((tabItem is not TabButton tabButton) || tabButton.ExpandToTabSize)) ? GridLength.Star : GridLength.Auto,
});

if (TabType == TabType.Scrollable)
Expand Down Expand Up @@ -742,24 +740,45 @@ private void ConsolidateSelectedIndex()
private void OnTabItemPropertyChanged(object sender, PropertyChangedEventArgs e)
{
var tabItem = (TabItem)sender;
if (e.PropertyName == "ExpandToTabSize" && tabItem is TabButton tabButton)
{
UpdateTabButtonSize(tabButton);
}
else
if (e.PropertyName == nameof(IsVisible))
{
UpdateTabVisibility(tabItem);
}
}

private void UpdateTabButtonSize(TabButton tabItem)
{
if (Orientation == OrientationType.Horizontal)
{
int columnIndex = Grid.GetColumn(tabItem);
ColumnDefinition columnDefinition = _grid.ColumnDefinitions[columnIndex];
columnDefinition.Width = tabItem.IsVisible ? (tabItem.ExpandToTabSize ? GridLength.Star : GridLength.Auto) : 0;
}
else
{
int rowIndex = Grid.GetRow(tabItem);
RowDefinition rowDefinition = _grid.RowDefinitions[rowIndex];
rowDefinition.Height = tabItem.IsVisible ? (tabItem.ExpandToTabSize ? GridLength.Star : GridLength.Auto) : 0;
}
}

private void UpdateTabVisibility(TabItem tabItem)
{
if (Orientation == OrientationType.Horizontal)
{
int columnIndex = Grid.GetColumn(tabItem);
var columnDefinition = _grid.ColumnDefinitions[columnIndex];
ColumnDefinition columnDefinition = _grid.ColumnDefinitions[columnIndex];
columnDefinition.Width = tabItem.IsVisible ? GridLength.Star : 0;
}
else
{
int rowIndex = Grid.GetRow(tabItem);
var rowDefinition = _grid.RowDefinitions[rowIndex];
RowDefinition rowDefinition = _grid.RowDefinitions[rowIndex];
rowDefinition.Height = tabItem.IsVisible ? GridLength.Star : 0;
}
}
Expand Down