diff --git a/Tabs/Tabs/TabButton.cs b/Tabs/Tabs/TabButton.cs index 5d5eaa0..5fc8e43 100644 --- a/Tabs/Tabs/TabButton.cs +++ b/Tabs/Tabs/TabButton.cs @@ -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), @@ -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() @@ -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); @@ -107,6 +123,17 @@ public double ButtonCircleSize set => SetValue(ButtonCircleSizeProperty, value); } + /// + /// 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. + /// + public bool ExpandToTabSize + { + get => (bool)GetValue(ExpandToTabSizeProperty); + set => SetValue(ExpandToTabSizeProperty, value); + } + + protected override void OnPropertyChanged([CallerMemberName] string propertyName = null) { base.OnPropertyChanged(propertyName); @@ -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): @@ -201,6 +232,7 @@ private void UpdateButtonCircleSize() } } + private void Initialize() { _imageButton = new ImageButton @@ -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; diff --git a/Tabs/Tabs/TabHostView.cs b/Tabs/Tabs/TabHostView.cs index 1f2c461..1d106ee 100644 --- a/Tabs/Tabs/TabHostView.cs +++ b/Tabs/Tabs/TabHostView.cs @@ -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 @@ -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) @@ -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) @@ -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; } }