Interfaces adaptatives¶
Changer la mise en page selon la place disponible, avec les container queries.
Container queries¶
Un conteneur déclare sa taille ; des styles s'appliquent selon cette taille.
CarteCarteCarteCarte
<Grid Container.Name="Page" Container.Sizing="Width">
<Grid.Styles>
<!-- Écran étroit : colonnes empilées -->
<ContainerQuery Name="Page" Query="max-width:600">
<Style Selector="UniformGrid#Cartes">
<Setter Property="Columns" Value="1" />
</Style>
</ContainerQuery>
<!-- Écran moyen -->
<ContainerQuery Name="Page" Query="min-width:600 and max-width:1000">
<Style Selector="UniformGrid#Cartes">
<Setter Property="Columns" Value="2" />
</Style>
</ContainerQuery>
<!-- Écran large -->
<ContainerQuery Name="Page" Query="min-width:1000">
<Style Selector="UniformGrid#Cartes">
<Setter Property="Columns" Value="4" />
</Style>
</ContainerQuery>
</Grid.Styles>
<UniformGrid x:Name="Cartes">
<Border Classes="carte" /> <Border Classes="carte" />
<Border Classes="carte" /> <Border Classes="carte" />
</UniformGrid>
</Grid>
flowchart LR
L["largeur du conteneur"] --> Q1{"< 600 ?"}
Q1 -- oui --> C1["Columns = 1"]
Q1 -- non --> Q2{"< 1000 ?"}
Q2 -- oui --> C2["Columns = 2"]
Q2 -- non --> C4["Columns = 4"]
| Requête | Exemple |
|---|---|
| Largeur | min-width:600, max-width:900 |
| Hauteur | min-height:400 (avec Container.Sizing="Height" ou "WidthAndHeight") |
| ET | min-width:600 and max-width:900 |
Warning
Une container query ne cible que les descendants du conteneur, jamais le conteneur lui-même (sinon changer sa taille relancerait la requête en boucle).
Menu latéral qui se replie¶
> 700 : DisplayMode="Inline", ouvert
≤ 700 : CompactOverlay, replié
<Panel Container.Name="Racine" Container.Sizing="Width">
<Panel.Styles>
<ContainerQuery Name="Racine" Query="max-width:700">
<Style Selector="SplitView#Menu">
<Setter Property="DisplayMode" Value="CompactOverlay" />
<Setter Property="IsPaneOpen" Value="False" />
</Style>
</ContainerQuery>
</Panel.Styles>
<SplitView x:Name="Menu"
DisplayMode="Inline" IsPaneOpen="True"
OpenPaneLength="220" CompactPaneLength="48">
<SplitView.Pane>
<ListBox>
<ListBoxItem>Accueil</ListBoxItem>
<ListBoxItem>Rapports</ListBoxItem>
</ListBox>
</SplitView.Pane>
<ContentControl Margin="16" />
</SplitView>
</Panel>
Boutons côte à côte ou empilés¶
<Border Container.Name="Actions" Container.Sizing="Width">
<Border.Styles>
<ContainerQuery Name="Actions" Query="max-width:420">
<Style Selector="StackPanel.actions">
<Setter Property="Orientation" Value="Vertical" />
</Style>
<Style Selector="StackPanel.actions > Button">
<Setter Property="HorizontalAlignment" Value="Stretch" />
</Style>
</ContainerQuery>
</Border.Styles>
<StackPanel Classes="actions" Orientation="Horizontal" Spacing="8">
<Button Content="Annuler" />
<Button Content="Enregistrer le brouillon" />
<Button Content="Publier" Classes="accent" />
</StackPanel>
</Border>
Réagir à la taille en C¶
protected override void OnSizeChanged(SizeChangedEventArgs e)
{
base.OnSizeChanged(e);
Classes.Set("compact", e.NewSize.Width < 600);
}
<Style Selector="Window.compact TextBlock.titre">
<Setter Property="FontSize" Value="18" />
</Style>
Adapter selon la plateforme¶
OKDesktop : Padding 8,4 · « Clic droit »
OKMobile : Padding 16,12 · « Appui long »