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

feat: ResponsiveView #912

Merged
merged 23 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
f949a22
feat: Add ResponsiveView
eriklimakc Nov 8, 2023
27df5ae
chore: Improving ResponsiveView
eriklimakc Nov 9, 2023
b7fd11c
chore: Use DataTemplate in ResponsiveView
eriklimakc Nov 9, 2023
ba25244
chore: Improving ResponsiveView
eriklimakc Nov 10, 2023
cef6cd6
chore: Improving StateChanged
eriklimakc Nov 10, 2023
df56a93
chore: Adding ResponsiveView style
eriklimakc Nov 10, 2023
56ebab9
chore: Adding ResponsiveViewSamplePage
eriklimakc Nov 13, 2023
0f77cf8
chore: Setting up responsive content
eriklimakc Nov 13, 2023
bbb483f
chore: ResponsiveView style
eriklimakc Nov 13, 2023
d76b35e
chore: Improving ResponsiveView
eriklimakc Nov 13, 2023
1ac9fd1
chore: ResponsiveView sample
eriklimakc Nov 14, 2023
221c37a
chore: Adjusting to have ResponsiveLayout
eriklimakc Nov 22, 2023
91e2ee2
chore: Moving ResponsiveLayout to helpers
eriklimakc Nov 22, 2023
a799d4d
chore: Use ContentTemplate and windows support
eriklimakc Nov 23, 2023
10f18b7
chore: Add ResponsiveContentChanged
eriklimakc Nov 23, 2023
4aaf97c
chore: Add ResponsiveView RuntimeTests
eriklimakc Nov 27, 2023
3fc28a7
chore: Fix RuntimeTests
eriklimakc Nov 27, 2023
82da950
chore: Improving ResponsiveView
eriklimakc Nov 28, 2023
5d20069
docs: Add ResponsiveView docs
eriklimakc Nov 28, 2023
ed9299c
chore: Simplify setting content to ResponsiveView
eriklimakc Nov 28, 2023
1c2d35e
chore: Rename ResponsiveView properties
eriklimakc Nov 29, 2023
33be355
chore: Restructuring ResponsiveView doc
eriklimakc Nov 30, 2023
a6ed6d4
chore: Adding remarks to ResponsiveView doc
eriklimakc Nov 30, 2023
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
133 changes: 133 additions & 0 deletions doc/controls/ResponsiveView.md
eriklimakc marked this conversation as resolved.
Show resolved Hide resolved
eriklimakc marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
---
uid: Toolkit.Controls.ResponsiveView
---
# ResponsiveView

## Summary

The `ResponsiveView` provides the ability to display different content based on screen size, making it easy to adapt to various devices.

## Remarks

The ResponsiveView Control adapts to different screen sizes by dynamically choosing the right template. It looks at the current screen width and the defined templates. Since not all templates need a value, the control ensures a smooth user experience by picking the smallest defined template that satisfies the width requirements. If no match is found, it defaults to the largest defined template.

**Initialization**: The `ResponsiveHelper` needs to be hooked up to the window's `SizeChanged` event in order for it to receive updates when the window size changes.
This is typically done in the `OnLaunched` method in the `App` class, where you can get the current window and call the `HookupEvent` method on the `ResponsiveHelper`.

Here is an example of how this might be achieved:

```cs
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
#if NET6_0_OR_GREATER && WINDOWS && !HAS_UNO
MainWindow = new Window();
#else
MainWindow = Microsoft.UI.Xaml.Window.Current;
#endif
// ...
var helper = Uno.Toolkit.UI.ResponsiveHelper.GetForCurrentView();
helper.HookupEvent(MainWindow);
}
```

## Inheritance
Object → DependencyObject → UIElement → FrameworkElement → Control → ContentControl

## Properties
| Property | Type | Description |
| ----------------- | ---------------- | ------------------------------------------------------- |
| NarrowestTemplate | DataTemplate | Template to be displayed on the narrowest screen size. |
| NarrowTemplate | DataTemplate | Template to be displayed on a narrow screen size. |
| NormalTemplate | DataTemplate | Template to be displayed on a normal screen size. |
| WideTemplate | DataTemplate | Template to be displayed on a wide screen size. |
| WidestTemplate | DataTemplate | Template to be displayed on the widest screen size. |
| ResponsiveLayout | ResponsiveLayout | Overrides the screen size threshold/breakpoints. |
eriklimakc marked this conversation as resolved.
Show resolved Hide resolved

### ResponsiveLayout
Provides the ability to override the MaxWidth for each screen size: `Narrowest`, `Narrow`, `Normal`, `Wide`, and `Widest`.

### Properties
| Property | Type | Description |
| ---------- | ------ | ---------------------- |
| Narrowest | double | Default value is 150. |
| Narrow | double | Default value is 300. |
| Normal | double | Default value is 600. |
| Wide | double | Default value is 800. |
| Widest | double | Default value is 1080. |

## Usage

```xml
xmlns:utu="using:Uno.Toolkit.UI"
...
<utu:ResponsiveView>
<utu:ResponsiveView.NarrowestTemplate>
<DataTemplate>
<!-- Narrowest content -->
</DataTemplate>
</utu:ResponsiveView.NarrowestTemplate>
<utu:ResponsiveView.NarrowTemplate>
<DataTemplate>
<!-- Narrow content -->
</DataTemplate>
</utu:ResponsiveView.NarrowTemplate>
<utu:ResponsiveView.NormalTemplate>
<DataTemplate>
<!-- Normal content -->
</DataTemplate>
</utu:ResponsiveView.NormalTemplate>
<utu:ResponsiveView.WideTemplate>
<DataTemplate>
<!-- Wide content -->
</DataTemplate>
</utu:ResponsiveView.WideTemplate>
<utu:ResponsiveView.WidestTemplate>
<DataTemplate>
<!-- Widest content -->
</DataTemplate>
</utu:ResponsiveView.WidestTemplate>
</utu:ResponsiveView>
```

Using `ResponsiveLayout` to customize the screen size breakpoints.

```xml
xmlns:utu="using:Uno.Toolkit.UI"
...
<utu:ResponsiveView>
<utu:ResponsiveView.ResponsiveLayout>
<utu:ResponsiveLayout>
<utu:ResponsiveLayout.Narrowest>200</utu:ResponsiveLayout.Narrowest>
<utu:ResponsiveLayout.Narrow>350</utu:ResponsiveLayout.Narrow>
<utu:ResponsiveLayout.Normal>800</utu:ResponsiveLayout.Normal>
<utu:ResponsiveLayout.Wide>1200</utu:ResponsiveLayout.Wide>
<utu:ResponsiveLayout.Widest>1500</utu:ResponsiveLayout.Widest>
</utu:ResponsiveLayout>
</utu:ResponsiveView.ResponsiveLayout>
<utu:ResponsiveView.NarrowestTemplate>
<DataTemplate>
<!-- Narrowest content -->
</DataTemplate>
</utu:ResponsiveView.NarrowestTemplate>
<utu:ResponsiveView.NarrowTemplate>
<DataTemplate>
<!-- Narrow content -->
</DataTemplate>
</utu:ResponsiveView.NarrowTemplate>
<utu:ResponsiveView.NormalTemplate>
<DataTemplate>
<!-- Normal content -->
</DataTemplate>
</utu:ResponsiveView.NormalTemplate>
<utu:ResponsiveView.WideTemplate>
<DataTemplate>
<!-- Wide content -->
</DataTemplate>
</utu:ResponsiveView.WideTemplate>
<utu:ResponsiveView.WidestTemplate>
<DataTemplate>
<!-- Widest content -->
</DataTemplate>
</utu:ResponsiveView.WidestTemplate>
</utu:ResponsiveView>
```
2 changes: 2 additions & 0 deletions doc/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
href: controls/LoadingView.md
- name: NavigationBar
href: controls/NavigationBar.md
- name: ResponsiveView
href: controls/ResponsiveView.md
- name: SafeArea
href: controls/SafeArea.md
- name: ShadowContainer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ protected override async void OnLaunched(XamlLaunchActivatedEventArgs e)
#else
_window = XamlWindow.Current;
#endif
var helper = ResponsiveHelper.GetForCurrentView();
helper.HookupEvent(_window);
eriklimakc marked this conversation as resolved.
Show resolved Hide resolved

if (_window.Content is null)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<Page x:Class="Uno.Toolkit.Samples.Content.Controls.ResponsiveViewSamplePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Uno.Toolkit.Samples.Content.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:utu="using:Uno.Toolkit.UI"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sample="using:Uno.Toolkit.Samples"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<sample:SamplePageLayout x:Name="SamplePageLayout" IsDesignAgnostic="True">
<sample:SamplePageLayout.DesignAgnosticTemplate>
<DataTemplate>
<StackPanel>
<StackPanel.Resources>
<Color x:Key="UnoGreen">#67E5AD</Color>
<Color x:Key="UnoPurple">#7A67F8</Color>
<Color x:Key="UnoRed">#F85977</Color>
<Color x:Key="UnoBlue">#159BFF</Color>
</StackPanel.Resources>
<utu:ResponsiveView>
<utu:ResponsiveView.ResponsiveLayout>
<utu:ResponsiveLayout>
<utu:ResponsiveLayout.Narrowest>200</utu:ResponsiveLayout.Narrowest>
<utu:ResponsiveLayout.Narrow>350</utu:ResponsiveLayout.Narrow>
<utu:ResponsiveLayout.Normal>800</utu:ResponsiveLayout.Normal>
<utu:ResponsiveLayout.Wide>1200</utu:ResponsiveLayout.Wide>
<utu:ResponsiveLayout.Widest>1500</utu:ResponsiveLayout.Widest>
</utu:ResponsiveLayout>
</utu:ResponsiveView.ResponsiveLayout>
<utu:ResponsiveView.NarrowestTemplate>
<DataTemplate>
<StackPanel Spacing="16">
<TextBlock Text="Extra Narrow"
HorizontalAlignment="Center"
Style="{StaticResource DisplaySmall}" />
<StackPanel Orientation="Vertical"
HorizontalAlignment="Center"
Spacing="20">
<Rectangle Height="100"
Width="100"
Fill="{StaticResource UnoGreen}" />
<Ellipse Height="100"
Width="100"
Fill="{StaticResource UnoPurple}" />
<Rectangle Height="100"
Width="100"
Fill="{StaticResource UnoRed}" />
<Ellipse Height="100"
Width="100"
Fill="{StaticResource UnoBlue}" />

</StackPanel>
</StackPanel>
</DataTemplate>
</utu:ResponsiveView.NarrowestTemplate>
<utu:ResponsiveView.NarrowTemplate>
<DataTemplate>
<StackPanel Spacing="16">
<TextBlock Text="Narrow"
HorizontalAlignment="Center"
Style="{StaticResource DisplaySmall}" />
<StackPanel Orientation="Vertical"
HorizontalAlignment="Center"
Spacing="20">
<Rectangle Height="150"
Width="150"
Fill="{StaticResource UnoGreen}" />
<Ellipse Height="150"
Width="150"
Fill="{StaticResource UnoPurple}" />
<Rectangle Height="150"
Width="150"
Fill="{StaticResource UnoRed}" />
<Ellipse Height="150"
Width="150"
Fill="{StaticResource UnoBlue}" />
</StackPanel>
</StackPanel>
</DataTemplate>
</utu:ResponsiveView.NarrowTemplate>
<utu:ResponsiveView.NormalTemplate>
<DataTemplate>
<StackPanel Spacing="16">
<TextBlock Text="Default"
HorizontalAlignment="Center"
Style="{StaticResource DisplayMedium}" />
<StackPanel Orientation="Vertical"
HorizontalAlignment="Center"
Spacing="20">
<Rectangle Height="200"
Width="200"
Fill="{StaticResource UnoGreen}" />
<Ellipse Height="200"
Width="200"
Fill="{StaticResource UnoPurple}" />
<Rectangle Height="200"
Width="200"
Fill="{StaticResource UnoRed}" />
<Ellipse Height="200"
Width="200"
Fill="{StaticResource UnoBlue}" />
</StackPanel>
</StackPanel>
</DataTemplate>
</utu:ResponsiveView.NormalTemplate>
<utu:ResponsiveView.WideTemplate>
<DataTemplate>
<StackPanel Spacing="16">
<TextBlock Text="Wide"
HorizontalAlignment="Center"
Style="{StaticResource DisplayLarge}" />
<StackPanel Orientation="Horizontal"
HorizontalAlignment="Center"
Spacing="20">
<Rectangle Height="250"
Width="250"
Fill="{StaticResource UnoGreen}" />
<Ellipse Height="250"
Width="250"
Fill="{StaticResource UnoPurple}" />
<Rectangle Height="250"
Width="250"
Fill="{StaticResource UnoRed}" />
<Ellipse Height="250"
Width="250"
Fill="{StaticResource UnoBlue}" />
</StackPanel>
</StackPanel>
</DataTemplate>
</utu:ResponsiveView.WideTemplate>
<utu:ResponsiveView.WidestTemplate>
<DataTemplate>
<StackPanel Spacing="16">
<TextBlock Text="Extra Wide"
HorizontalAlignment="Center"
Style="{StaticResource DisplayLarge}" />
<StackPanel Orientation="Horizontal"
HorizontalAlignment="Center"
Spacing="20">
<Rectangle Height="300"
Width="300"
Fill="{StaticResource UnoGreen}" />
<Ellipse Height="300"
Width="300"
Fill="{StaticResource UnoPurple}" />
<Rectangle Height="300"
Width="300"
Fill="{StaticResource UnoRed}" />
<Ellipse Height="300"
Width="300"
Fill="{StaticResource UnoBlue}" />
</StackPanel>
</StackPanel>
</DataTemplate>
</utu:ResponsiveView.WidestTemplate>
</utu:ResponsiveView>
</StackPanel>
</DataTemplate>
</sample:SamplePageLayout.DesignAgnosticTemplate>
</sample:SamplePageLayout>
</Grid>
</Page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Uno.Toolkit.Samples.Entities;
#if IS_WINUI
using Microsoft.UI.Xaml.Controls;
#else
using Windows.UI.Xaml.Controls;
#endif

namespace Uno.Toolkit.Samples.Content.Controls
{
[SamplePage(SampleCategory.Controls, "ResponsiveView")]
public sealed partial class ResponsiveViewSamplePage : Page
{
public ResponsiveViewSamplePage()
{
this.InitializeComponent();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
<Compile Include="$(MSBuildThisFileDirectory)Content\Controls\AutoLayoutPage.xaml.cs">
<DependentUpon>AutoLayoutPage.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Content\Controls\ResponsiveViewSamplePage.xaml.cs">
<DependentUpon>ResponsiveViewSamplePage.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Content\Controls\CardSamplePage.xaml.cs">
<DependentUpon>CardSamplePage.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -221,6 +224,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Content\Controls\ResponsiveViewSamplePage.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Content\Controls\CardSamplePage.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down
Loading
Loading