Skip to content

Commit 26741db

Browse files
sharpninjaCopilot
andcommitted
Add adaptive layout for foldable devices
Replace static Phone/Tablet view selection with AdaptiveMainView that listens to OnSizeChanged and dynamically swaps between PhoneMainView (<600px) and TabletMainView (>=600px). This handles foldable device fold/unfold and rotation at runtime. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 27032d7 commit 26741db

3 files changed

Lines changed: 76 additions & 3 deletions

File tree

src/RequestTracker.Android/App.axaml.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ public override void OnFrameworkInitializationCompleted()
3636
{
3737
var clipboardService = new AndroidClipboardService();
3838
var vm = new MainWindowViewModel(clipboardService, mcpBaseUrl);
39-
singleView.MainView = DeviceFormFactor.IsTablet()
40-
? new TabletMainView { DataContext = vm }
41-
: (Control)new PhoneMainView { DataContext = vm };
39+
singleView.MainView = new AdaptiveMainView { DataContext = vm };
4240
vm.InitializeAfterWindowShown();
4341
}
4442
catch (Exception ex)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<UserControl xmlns="https://github.com/avaloniaui"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
xmlns:vm="using:RequestTracker.Core.ViewModels"
4+
xmlns:views="using:RequestTracker.Android.Views"
5+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
7+
mc:Ignorable="d"
8+
x:Class="RequestTracker.Android.Views.AdaptiveMainView"
9+
x:DataType="vm:MainWindowViewModel">
10+
11+
<Panel x:Name="HostPanel">
12+
<!-- Content is set dynamically in code-behind based on available width -->
13+
</Panel>
14+
</UserControl>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
using Avalonia;
3+
using Avalonia.Controls;
4+
using Avalonia.Markup.Xaml;
5+
6+
namespace RequestTracker.Android.Views;
7+
8+
/// <summary>
9+
/// Switches between PhoneMainView and TabletMainView based on available width.
10+
/// Reacts to fold/unfold and rotation on foldable devices.
11+
/// </summary>
12+
public partial class AdaptiveMainView : UserControl
13+
{
14+
private const double TabletWidthThreshold = 600;
15+
private bool _isTabletLayout;
16+
private Control? _currentView;
17+
private Panel? _hostPanel;
18+
19+
public AdaptiveMainView()
20+
{
21+
InitializeComponent();
22+
_hostPanel = this.FindControl<Panel>("HostPanel");
23+
}
24+
25+
protected override void OnSizeChanged(SizeChangedEventArgs e)
26+
{
27+
base.OnSizeChanged(e);
28+
UpdateLayout(e.NewSize.Width);
29+
}
30+
31+
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
32+
{
33+
base.OnAttachedToVisualTree(e);
34+
UpdateLayout(Bounds.Width);
35+
}
36+
37+
private void UpdateLayout(double availableWidth)
38+
{
39+
bool shouldBeTablet = availableWidth >= TabletWidthThreshold;
40+
41+
if (_currentView != null && shouldBeTablet == _isTabletLayout)
42+
return;
43+
44+
_isTabletLayout = shouldBeTablet;
45+
var dc = DataContext;
46+
47+
Control newView = shouldBeTablet
48+
? new TabletMainView()
49+
: new PhoneMainView();
50+
51+
newView.DataContext = dc;
52+
53+
if (_hostPanel != null)
54+
{
55+
_hostPanel.Children.Clear();
56+
_hostPanel.Children.Add(newView);
57+
}
58+
59+
_currentView = newView;
60+
}
61+
}

0 commit comments

Comments
 (0)