Skip to content

Commit

Permalink
feat(commandbar): Add support for native CommandBar.HorizontalContent…
Browse files Browse the repository at this point in the history
…Alignment on Android.

BREAKING CHANGE: Alignment of native CommandBar.Content used to be `Stretch`/`Stretch` and is now aligned with `Top`/`Left` on UWP.

Use  `CommandBar.HorizontalContentAlignment` and first element of content's VerticalAlignment to control.
  • Loading branch information
jeromelaban committed Mar 19, 2020
1 parent a81896d commit 5e6ca82
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 15 deletions.
30 changes: 16 additions & 14 deletions doc/articles/controls/CommandBar.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,22 @@ On **iOS**, tapping the back button automatically triggers a back navigation on

## Properties

| Property | Windows | iOS | Android | Comments |
|---------------------------------------|:-------:|:---:|:-------:|------------------------------------------------------ |
| Background | x | x | x | |
| Content | x | x | x | |
| Foreground | x | x | x | |
| Height | x | - | - | **iOS** and **Android**: Fixed and can't be changed. |
| HorizontalAlignment | x | - | x | **iOS**: Always use `HorizontalAlignment.Stretch`. |
| Opacity | x | x | x | |
| Padding | x | x | x | **iOS** and **Android**: Please refer to the `Padding` section. |
| PrimaryCommands | x | x | x | |
| SecondaryCommands | x | - | x | **iOS**: Not supported. |
| VerticalAlignment | x | - | x | **iOS**: Always use `VerticalAlignment.Top`. |
| Visibility | x | x | x | |
| Width | x | - | x | **iOS**: Always use `double.NaN`. |
| Property | Windows | iOS | Android | Comments |
|---------------------------------------|:-------:|:---:|:-------:|------------------------------------------------------ |
| Background | x | x | x | |
| Content | x | x | x | |
| Foreground | x | x | x | |
| Height | x | - | - | **iOS** and **Android**: Fixed and can't be changed. |
| HorizontalAlignment | x | - | x | **iOS**: Always use `HorizontalAlignment.Stretch`. |
| Opacity | x | x | x | |
| Padding | x | x | x | **iOS** and **Android**: Please refer to the `Padding` section. |
| PrimaryCommands | x | x | x | |
| SecondaryCommands | x | - | x | **iOS**: Not supported. |
| VerticalAlignment | x | - | x | **iOS**: Always use `VerticalAlignment.Top`. |
| Visibility | x | x | x | |
| Width | x | - | x | **iOS**: Always use `double.NaN`. |
| HorizontalContentAlignment | x | - | x | **Android**: Stretch and Left are supported. **Windows**: Set `IsDynamicOverflowEnabled="False"` for proper behavior. |
| VerticalContentAlignment | x | - | - | Only supported on Windows. **Android**: Alignment needs to be done through the content itself. |

*If it's not listed, assume it's not supported.*

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,40 @@ public void NativeCommandBar_Automated()

_app.WaitForText(result, "Clicked!");
}

[Test]
[AutoRetry]
[ActivePlatforms(Platform.Android)]
public void NativeCommandBar_Content_Alignment_Automated()
{
Run("UITests.Windows_UI_Xaml_Controls.CommandBar.CommandBar_Native_With_TextBox");

var verticalValue = _app.Marked("verticalValue");
var horizontalValue = _app.Marked("horizontalValue");
var innerTextBox = _app.Marked("InnerTextBox");
var innerTextBlock = _app.Marked("InnerTextBlock");
var myCommandBar = _app.Marked("MyCommandBar");

var myCommandBarResult = _app.Query(myCommandBar).First();

TakeScreenshot("Default");

var innerTextBoxResult = _app.Query(innerTextBox).First();
Assert.IsTrue(innerTextBoxResult.Rect.Width <= myCommandBarResult.Rect.Width / 2, "TextBox Width is too large");

horizontalValue.SetDependencyPropertyValue("SelectedItem", "Stretch");

TakeScreenshot("Stretch");

innerTextBoxResult = _app.Query(innerTextBox).First();
Assert.IsTrue(innerTextBoxResult.Rect.Width > myCommandBarResult.Rect.Width * .75, "TextBox Width is not large enough");

horizontalValue.SetDependencyPropertyValue("SelectedItem", "Left");

innerTextBoxResult = _app.Query(innerTextBox).First();
Assert.IsTrue(innerTextBoxResult.Rect.Width <= myCommandBarResult.Rect.Width / 2, "TextBox Width is too large");

TakeScreenshot("Left");
}
}
}
9 changes: 8 additions & 1 deletion src/SamplesApp/UITests.Shared/UITests.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\CommandBar\CommandBar_Native_With_TextBox.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\CommandBar\CommandBar_Xaml_Automated.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down Expand Up @@ -3401,6 +3405,9 @@
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\CommandBar\CommandBar_Native_With_Content.xaml.cs">
<DependentUpon>CommandBar_Native_With_Content.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\CommandBar\CommandBar_Native_With_TextBox.xaml.cs">
<DependentUpon>CommandBar_Native_With_TextBox.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\CommandBar\CommandBar_Xaml_Automated.xaml.cs">
<DependentUpon>CommandBar_Xaml_Automated.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -5529,4 +5536,4 @@
<DependentUpon>EventsSequences.xaml</DependentUpon>
</Compile>
</ItemGroup>
</Project>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<Page x:Class="UITests.Windows_UI_Xaml_Controls.CommandBar.CommandBar_Native_With_TextBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:android="http://uno.ui/android"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UITests.Windows_UI_Xaml_Controls.CommandBar"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d android"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Page.Resources>
<android:Style x:Key="MyNativeDefaultCommandBar"
TargetType="CommandBar">
<Setter Property="Background"
Value="{x:Null}" />
<Setter Property="Foreground"
Value="{x:Null}" />
<Setter Property="HorizontalAlignment"
Value="Stretch" />
<Setter Property="VerticalAlignment"
Value="Top" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="CommandBar">
<NativeCommandBarPresenter Height="44" />
</ControlTemplate>
</Setter.Value>
</Setter>
</android:Style>

<android:Style x:Key="CommandBarTypo"
TargetType="TextBlock">
<Setter Property="FontWeight"
Value="Bold" />
<Setter Property="Foreground"
Value="Black" />
<Setter Property="TextTrimming"
Value="CharacterEllipsis" />
<Setter Property="VerticalAlignment"
Value="Center" />
</android:Style>
</Page.Resources>

<Grid>
<StackPanel Margin="0,150,0,0">
<ComboBox x:Name="verticalValue"
Header="Vertical Alignment"
SelectedItem="Top">
<x:String>Top</x:String>
<x:String>Bottom</x:String>
<x:String>Center</x:String>
<x:String>Stretch</x:String>
</ComboBox>
<ComboBox x:Name="horizontalValue"
Header="Horizontal Alignment"
SelectedItem="Left">
<x:String>Left</x:String>
<x:String>Right</x:String>
<x:String>Center</x:String>
<x:String>Stretch</x:String>
</ComboBox>
<TextBlock>
<Run Text="v:" />
<Run Text="{x:Bind MyCommandBar.VerticalContentAlignment, Mode=OneWay}" />
<Run Text="h:" />
<Run Text="{x:Bind MyCommandBar.HorizontalContentAlignment, Mode=OneWay}" />
</TextBlock>
</StackPanel>
<StackPanel>
<CommandBar x:Name="MyCommandBar"
IsDynamicOverflowEnabled="False"
android:Style="{StaticResource MyNativeDefaultCommandBar}"
VerticalContentAlignment="{Binding SelectedItem, ElementName=verticalValue, Mode=TwoWay}"
HorizontalContentAlignment="{Binding SelectedItem, ElementName=horizontalValue, Mode=TwoWay}"
Background="Gray">
<CommandBar.Content>
<TextBox Text="Hello Title 2 !"
Foreground="Black"
x:Name="InnerTextBox" />
</CommandBar.Content>
<CommandBar.PrimaryCommands>
<AppBarButton Content="Hello">
</AppBarButton>
</CommandBar.PrimaryCommands>
</CommandBar>
<CommandBar x:Name="TextBlockCommandBar"
IsDynamicOverflowEnabled="False"
android:Style="{StaticResource MyNativeDefaultCommandBar}"
VerticalContentAlignment="{Binding SelectedItem, ElementName=verticalValue, Mode=TwoWay}"
HorizontalContentAlignment="{Binding SelectedItem, ElementName=horizontalValue, Mode=TwoWay}"
Background="Gray">
<CommandBar.Content>
<TextBlock Text="Hello Title 2 !"
Foreground="Black"
VerticalAlignment="Center"
x:Name="InnerTextBlock" />
</CommandBar.Content>
<CommandBar.PrimaryCommands>
<AppBarButton Content="Hello">
</AppBarButton>
</CommandBar.PrimaryCommands>
</CommandBar>
</StackPanel>
</Grid>
</Page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Uno.UI.Samples.Controls;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238

namespace UITests.Windows_UI_Xaml_Controls.CommandBar
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
[SampleControlInfo("CommandBar")]
public sealed partial class CommandBar_Native_With_TextBox : Page
{
public CommandBar_Native_With_TextBox()
{
this.InitializeComponent();
}
}
}
5 changes: 5 additions & 0 deletions src/Uno.UI/Controls/CommandBarRenderer.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ protected override IEnumerable<IDisposable> Initialize()
new[] { CommandBar.VisibilityProperty },
new[] { CommandBar.PaddingProperty },
new[] { CommandBar.OpacityProperty },
new[] { CommandBar.HorizontalContentAlignmentProperty },
new[] { CommandBar.VerticalContentAlignmentProperty },
new[] { CommandBar.OpacityProperty },
new[] { SubtitleProperty },
new[] { NavigationCommandProperty },
new[] { BackButtonVisibilityProperty },
Expand All @@ -149,6 +152,8 @@ protected override void Render()
// Content
Native.Title = Element.Content as string;
_contentContainer.Child = Element.Content as View;
_contentContainer.VerticalAlignment = Element.VerticalContentAlignment;
_contentContainer.HorizontalAlignment = Element.HorizontalContentAlignment;
_contentContainer.Visibility = Element.Content is View
? Visibility.Visible
: Visibility.Collapsed;
Expand Down

0 comments on commit 5e6ca82

Please sign in to comment.