Skip to content

Commit

Permalink
Stopped using Triggers for color updates
Browse files Browse the repository at this point in the history
  • Loading branch information
roubachof committed Feb 5, 2021
1 parent a391ce7 commit d8ee071
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 70 deletions.
66 changes: 26 additions & 40 deletions Tabs/Tabs/BottomTabItem.xaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?xml version="1.0" encoding="UTF-8" ?>

<tabs:TabTextItem x:Class="Sharpnado.Tabs.BottomTabItem"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:effects="clr-namespace:Sharpnado.Tabs.Effects"
xmlns:tabs="clr-namespace:Sharpnado.Tabs"
x:Name="RootLayout">
<tabs:TabTextItem
x:Class="Sharpnado.Tabs.BottomTabItem"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:effects="clr-namespace:Sharpnado.Tabs.Effects"
xmlns:tabs="clr-namespace:Sharpnado.Tabs"
x:Name="RootLayout">

<ContentView.Resources>
<ResourceDictionary>
Expand All @@ -23,47 +24,32 @@
</ContentView.Resources>

<ContentView.Content>
<Grid x:Name="Grid"
BackgroundColor="Transparent"
ColumnSpacing="0"
RowSpacing="0">
<Grid
x:Name="Grid"
BackgroundColor="Transparent"
ColumnSpacing="0"
RowSpacing="0">

<Grid.RowDefinitions>
<RowDefinition Height="8*" />
<RowDefinition x:Name="TextRowDefinition" Height="5*" />
</Grid.RowDefinitions>

<Image x:Name="Icon"
Grid.Row="0"
Style="{StaticResource TabIcon}"
WidthRequest="{Binding Source={x:Reference RootLayout}, Path=IconSize}"
HeightRequest="{Binding Source={x:Reference RootLayout}, Path=IconSize}"
effects:ImageEffect.TintColor="{Binding Source={x:Reference RootLayout}, Path=UnselectedIconColor}"
Source="{Binding Source={x:Reference RootLayout}, Path=IconImageSource}">
<Image.Triggers>
<DataTrigger Binding="{Binding Source={x:Reference RootLayout}, Path=IsSelected}"
TargetType="Image"
Value="true">
<Setter Property="effects:ImageEffect.TintColor" Value="{Binding Source={x:Reference RootLayout}, Path=SelectedTabColor}" />
</DataTrigger>
</Image.Triggers>
</Image>
<Image
x:Name="Icon"
Grid.Row="0"
HeightRequest="{Binding Source={x:Reference RootLayout}, Path=IconSize}"
Source="{Binding Source={x:Reference RootLayout}, Path=IconImageSource}"
Style="{StaticResource TabIcon}"
WidthRequest="{Binding Source={x:Reference RootLayout}, Path=IconSize}" />

<Label x:Name="IconText"
Grid.Row="1"
Style="{StaticResource TabIconLabel}"
FontFamily="{Binding Source={x:Reference RootLayout}, Path=FontFamily}"
FontSize="{Binding Source={x:Reference RootLayout}, Path=LabelSize}"
Text="{Binding Source={x:Reference RootLayout}, Path=Label}"
TextColor="{Binding Source={x:Reference RootLayout}, Path=UnselectedLabelColor}">
<Label.Triggers>
<DataTrigger Binding="{Binding Source={x:Reference RootLayout}, Path=IsSelected}"
TargetType="Label"
Value="true">
<Setter Property="TextColor" Value="{Binding Source={x:Reference RootLayout}, Path=SelectedTabColor}" />
</DataTrigger>
</Label.Triggers>
</Label>
<Label
x:Name="IconText"
Grid.Row="1"
FontFamily="{Binding Source={x:Reference RootLayout}, Path=FontFamily}"
FontSize="{Binding Source={x:Reference RootLayout}, Path=LabelSize}"
Style="{StaticResource TabIconLabel}"
Text="{Binding Source={x:Reference RootLayout}, Path=Label}" />
</Grid>
</ContentView.Content>
</tabs:TabTextItem>
16 changes: 16 additions & 0 deletions Tabs/Tabs/BottomTabItem.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Runtime.CompilerServices;

using Sharpnado.Tabs.Effects;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

Expand Down Expand Up @@ -41,6 +43,7 @@ public BottomTabItem()
LabelSize = 12;

UpdateTextVisibility();
UpdateColors();
}

[TypeConverter(typeof(ImageSourceConverter))]
Expand Down Expand Up @@ -82,6 +85,13 @@ protected override void OnPropertyChanged([CallerMemberName] string propertyName
case nameof(IsTextVisible):
UpdateTextVisibility();
break;

case nameof(UnselectedLabelColor):
case nameof(UnselectedIconColor):
case nameof(SelectedTabColor):
case nameof(IsSelected):
UpdateColors();
break;
}
}

Expand Down Expand Up @@ -110,5 +120,11 @@ private void UpdateTextVisibility()
Icon.VerticalOptions = LayoutOptions.Center;
}
}

private void UpdateColors()
{
IconText.TextColor = IsSelected ? SelectedTabColor : UnselectedLabelColor;
ImageEffect.SetTintColor(Icon, IsSelected ? SelectedTabColor : UnselectedIconColor);
}
}
}
54 changes: 24 additions & 30 deletions Tabs/Tabs/UnderlinedTabItem.xaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?xml version="1.0" encoding="UTF-8" ?>

<tabs:TabTextItem x:Class="Sharpnado.Tabs.UnderlinedTabItem"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:tabs="clr-namespace:Sharpnado.Tabs"
x:Name="RootLayout">
<tabs:TabTextItem
x:Class="Sharpnado.Tabs.UnderlinedTabItem"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:tabs="clr-namespace:Sharpnado.Tabs"
x:Name="RootLayout">

<ContentView.Resources>
<ResourceDictionary>
Expand All @@ -26,36 +27,29 @@
<RowDefinition Height="*" />
</Grid.RowDefinitions>

<BoxView x:Name="Underline"
HeightRequest="0"
Margin="0,0"
HorizontalOptions="Center"
VerticalOptions="End"
BackgroundColor="Transparent">
<BoxView
x:Name="Underline"
Margin="0,0"
BackgroundColor="Transparent"
HeightRequest="0"
HorizontalOptions="Center"
VerticalOptions="End">
<BoxView.Triggers>
<DataTrigger Binding="{Binding Source={x:Reference RootLayout}, Path=IsSelected}"
TargetType="BoxView"
Value="true">
<Setter Property="BackgroundColor" Value="{Binding Source={x:Reference RootLayout}, Path=SelectedTabColor}" />
<DataTrigger
Binding="{Binding Source={x:Reference RootLayout}, Path=IsSelected}"
TargetType="BoxView"
Value="true">
<Setter Property="HeightRequest" Value="{Binding Source={x:Reference RootLayout}, Path=UnderlineHeight}" />
</DataTrigger>
</BoxView.Triggers>
</BoxView>
<Label x:Name="InnerLabel"
Style="{StaticResource TabTextHeader}"
HorizontalOptions="Center"
FontFamily="{Binding Source={x:Reference RootLayout}, Path=FontFamily}"
FontSize="{Binding Source={x:Reference RootLayout}, Path=LabelSize}"
Text="{Binding Source={x:Reference RootLayout}, Path=Label}"
TextColor="{Binding Source={x:Reference RootLayout}, Path=UnselectedLabelColor}">
<Label.Triggers>
<DataTrigger Binding="{Binding Source={x:Reference RootLayout}, Path=IsSelected}"
TargetType="Label"
Value="true">
<Setter Property="TextColor" Value="{Binding Source={x:Reference RootLayout}, Path=SelectedTabColor}" />
</DataTrigger>
</Label.Triggers>
</Label>
<Label
x:Name="InnerLabel"
FontFamily="{Binding Source={x:Reference RootLayout}, Path=FontFamily}"
FontSize="{Binding Source={x:Reference RootLayout}, Path=LabelSize}"
HorizontalOptions="Center"
Style="{StaticResource TabTextHeader}"
Text="{Binding Source={x:Reference RootLayout}, Path=Label}" />
</Grid>
</ContentView.Content>
</tabs:TabTextItem>
12 changes: 12 additions & 0 deletions Tabs/Tabs/UnderlinedTabItem.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ protected override void OnPropertyChanged([CallerMemberName] string propertyName
case nameof(UnderlineAllTab):
UpdateUnderlineAllTab();
break;

case nameof(UnselectedLabelColor):
case nameof(SelectedTabColor):
case nameof(IsSelected):
UpdateColors();
break;
}
}

Expand Down Expand Up @@ -100,5 +106,11 @@ private void UpdateMargin()
? new Thickness(Underline.Margin.Left - Margin.Left, 0, Underline.Margin.Right - Margin.Right, 0)
: new Thickness(0);
}

private void UpdateColors()
{
InnerLabel.TextColor = IsSelected ? SelectedTabColor : UnselectedLabelColor;
Underline.Color = SelectedTabColor;
}
}
}

0 comments on commit d8ee071

Please sign in to comment.