Skip to content

Commit 4517ce5

Browse files
sharpninjaCopilot
andcommitted
Fix adaptive layout: respond to resize via Bounds observable and MeasureOverride
Added BoundsProperty observer and MeasureOverride to detect size changes from foldable device fold/unfold and rotation. The previous OnSizeChanged alone was not sufficient on Android. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent f976882 commit 4517ce5

2 files changed

Lines changed: 33 additions & 11 deletions

File tree

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
<UserControl xmlns="https://github.com/avaloniaui"
22
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
33
xmlns:vm="using:RequestTracker.Core.ViewModels"
4-
xmlns:views="using:RequestTracker.Android.Views"
54
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
65
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
76
mc:Ignorable="d"
87
x:Class="RequestTracker.Android.Views.AdaptiveMainView"
9-
x:DataType="vm:MainWindowViewModel">
8+
x:DataType="vm:MainWindowViewModel"
9+
HorizontalAlignment="Stretch"
10+
VerticalAlignment="Stretch">
1011

11-
<Panel x:Name="HostPanel">
12-
<!-- Content is set dynamically in code-behind based on available width -->
13-
</Panel>
12+
<Panel x:Name="HostPanel"
13+
HorizontalAlignment="Stretch"
14+
VerticalAlignment="Stretch"/>
1415
</UserControl>

src/RequestTracker.Android/Views/AdaptiveMainView.axaml.cs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using Avalonia;
33
using Avalonia.Controls;
4+
using Avalonia.Layout;
45
using Avalonia.Markup.Xaml;
56

67
namespace RequestTracker.Android.Views;
@@ -12,7 +13,7 @@ namespace RequestTracker.Android.Views;
1213
public partial class AdaptiveMainView : UserControl
1314
{
1415
private const double TabletWidthThreshold = 600;
15-
private bool _isTabletLayout;
16+
private bool? _isTabletLayout;
1617
private Control? _currentView;
1718
private Panel? _hostPanel;
1819

@@ -25,20 +26,40 @@ public AdaptiveMainView()
2526
protected override void OnSizeChanged(SizeChangedEventArgs e)
2627
{
2728
base.OnSizeChanged(e);
28-
UpdateLayout(e.NewSize.Width);
29+
EvaluateLayout();
2930
}
3031

3132
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
3233
{
3334
base.OnAttachedToVisualTree(e);
34-
UpdateLayout(Bounds.Width);
35+
// Also listen to Bounds property changes for Android config changes
36+
this.GetObservable(BoundsProperty).Subscribe(new BoundsObserver(this));
37+
EvaluateLayout();
3538
}
3639

37-
private void UpdateLayout(double availableWidth)
40+
private sealed class BoundsObserver : IObserver<Rect>
3841
{
39-
bool shouldBeTablet = availableWidth >= TabletWidthThreshold;
42+
private readonly AdaptiveMainView _owner;
43+
public BoundsObserver(AdaptiveMainView owner) => _owner = owner;
44+
public void OnNext(Rect value) => _owner.EvaluateLayout();
45+
public void OnError(Exception error) { }
46+
public void OnCompleted() { }
47+
}
48+
49+
protected override Size MeasureOverride(Size availableSize)
50+
{
51+
EvaluateLayout(availableSize.Width);
52+
return base.MeasureOverride(availableSize);
53+
}
54+
55+
private void EvaluateLayout(double? widthOverride = null)
56+
{
57+
double width = widthOverride ?? Bounds.Width;
58+
if (width <= 0) return;
59+
60+
bool shouldBeTablet = width >= TabletWidthThreshold;
4061

41-
if (_currentView != null && shouldBeTablet == _isTabletLayout)
62+
if (_isTabletLayout.HasValue && shouldBeTablet == _isTabletLayout.Value)
4263
return;
4364

4465
_isTabletLayout = shouldBeTablet;

0 commit comments

Comments
 (0)