From f1c23965b2f5d6995e54b0979ad5a25d704feba8 Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Sun, 8 Nov 2026 02:04:59 +0000 Subject: [PATCH] Blog edition accessibility --- .vscode/launch.json | 3 +- CHANGELOG.md | 4 + doc/architecture/postit.md | 45 +++- src/PostIt/PostIt.Android/MainActivity.cs | 22 ++ .../PostIt.Android/PlatformBootstrap.cs | 1 + .../PostIt.Android/PostIt.Android.csproj | 2 + .../AndroidMarkdownEditorTestActivity.cs | 241 ++++++++++++++++++ src/PostIt/PostIt.Tests/BlogReaderTests.cs | 3 +- .../PostIt.Tests/MarkdownEditorScrollTests.cs | 200 +++++++++++++++ .../Controls/MarkdownEditorControl.axaml.cs | 45 ++++ src/PostIt/PostIt/Services/Platform.cs | 1 + src/PostIt/PostIt/Views/Blogs/BlogsPage.axaml | 16 +- .../PostIt/Views/Blogs/BlogsPage.axaml.cs | 27 +- .../PostIt/Views/Layout/MainView.axaml.cs | 62 +++++ 14 files changed, 656 insertions(+), 16 deletions(-) create mode 100644 src/PostIt/PostIt.Tests/AndroidMarkdownEditorTestActivity.cs diff --git a/.vscode/launch.json b/.vscode/launch.json index 87cf40dbb..d8a9f1c8c 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -17,8 +17,7 @@ "program": "${workspaceFolder}/src/PostIt/PostIt.Desktop/bin/Debug/net10.0/PostIt.Desktop.dll", "env": { "POSTIT_SETTINGS_JSON": "/home/paul/Workspace/yavsc/src/PostIt/PostIt/postit-settings.json" - }, - "preLaunchTask": "dotnet: build-postit-desktop" + } }, { "name": "Android Debug", diff --git a/CHANGELOG.md b/CHANGELOG.md index 337e74288..799dd624b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ ### Added +* [PostIt.Android] Embed managed assemblies in APKs by default, including Debug builds launched outside the Makefile, to prevent the native startup abort "No assemblies found" when fast-deployment files are absent. +* [PostIt] Remove the redundant "Post detail" heading and its grid row from the blog editor, including the compact layout. +* [PostIt/Android] Combine system-bar/cutout safe-area insets with actual keyboard overlap at the root, instead of replacing Avalonia's automatic safe-area padding with keyboard-only padding. Preserve the toolbar, session banner, attachments and status controls; place article details beside the editor during landscape keyboard input. Only on Android, temporarily hide the title input while editing in compact mode with the keyboard open, restoring it on keyboard closure or focus change without modifying the draft. Verify visible controls outside native system bars, at least three editor lines and the editor's bottom above IME insets. +* [PostIt/Android] Remove the outer blog-page scroll container so the editor, reader and list retain bounded viewports. Keep the editing caret visible after the keyboard resizes the editor; request the native keyboard on a touch tap, not a scroll gesture, without cycling focus or replacing the selection. Add headless regressions and a debug-only device check using a local synthetic article. * [PostIt.Android] Fix the crash when starting a podcast broadcast with camera preview active: detach preview callbacks using the retained surface holder, before deferred native-view destruction, without accessing an already disposed SurfaceView. Add a device regression covering preview teardown followed by local capture. * [PostIt.Android] Keep the screen awake during foreground video playback and release it on pause, stop, completion, error or video teardown. Add a persisted, default-off podcast option to keep the screen awake during camera preview/live capture; release on capture end and allow normal sleep in the background without changing the broadcast service. Include native LibVLC libraries when building with a single runtime identifier (including physical ARM64 phones). * [PostIt/Podcasts] Preview the selected camera before starting a live broadcast, without creating an emission, encoding files or opening the microphone. Release preview on stop, navigation, logout and Android backgrounding, and before starting the actual broadcast. Correct Android portrait orientation for rear and front cameras: subtract the sensor rotation already carried by Camera2's SurfaceTexture matrix, reversing the remaining rotation when that matrix mirrors the front camera (it was turned 90° or upside down); track display changes for both preview and encoded frames, preserving aspect ratio and fixed live stream dimensions. diff --git a/doc/architecture/postit.md b/doc/architecture/postit.md index 9a0ae000e..4e63f1c06 100644 --- a/doc/architecture/postit.md +++ b/doc/architecture/postit.md @@ -12,6 +12,13 @@ ## Surface : un code-base, trois front-ends +Le projet Android active `EmbedAssembliesIntoApk` par défaut, y compris en +Debug : un APK installé directement doit contenir les assemblies .NET, +sans dépendre des fichiers de Fast Deployment sous `files/.__override__`. +Sans elles, le runtime Android abandonne avant de charger l'interface +avec « No assemblies found ». Le Makefile conserve également ce réglage +explicite pour ses commandes de compilation. + ``` ┌────────────────────────┐ │ PostIt (lib) │ @@ -376,7 +383,8 @@ de modifier cette répartition. La liste et l'article défilent séparément. Lorsque la largeur de la fenêtre atteint ou dépasse sa hauteur, le lecteur ou l'éditeur passe à gauche (deux tiers de la largeur par défaut), et -l'en-tête, la recherche, les commandes et la liste passent à droite. +l'en-tête, la recherche et la liste passent à droite ; les commandes +restent dans la barre de navigation. Le séparateur devient vertical. En portrait, la disposition verticale revient sans recréer les contrôles ni perdre le brouillon. Le seuil utilise les dimensions clientes de la fenêtre, indépendamment @@ -395,6 +403,41 @@ dépasser le viewport pour que les barres automatiques s'activent. Ne pas placer cette grille dans un `StackPanel` vertical ou un `ScrollViewer` de page, qui lui donnerait une hauteur de mesure illimitée. +Après un redimensionnement de la zone de texte (notamment à l'ouverture du +clavier Android avec `AdjustResize`), l'éditeur remet le curseur dans son +viewport après la mise en page, uniquement s'il garde le focus. Un toucher +dans la zone éditable demande aussi le clavier via le pont natif Android, +sans retirer puis redonner le focus ni modifier la sélection. Les autres +plateformes conservent leur gestion habituelle du clavier. + +`MainView` compose les marges de `TopLevel.InsetsManager` (barres système +et découpes) avec le recouvrement réel indiqué par `TopLevel.InputPane`, +car le clavier ne redimensionne pas toujours la fenêtre Android en mode +edge-to-edge. Si `AdjustResize` a déjà réduit la fenêtre, cette réserve +ne soustrait pas une seconde fois la hauteur du clavier. La racine gère +explicitement ces marges et désactive `TopLevel.AutoSafeAreaPadding` pour +éviter deux propriétaires du même padding. Ne pas remplacer les marges +système par un padding contenant uniquement la hauteur du clavier. +En disposition large, tant que le clavier est ouvert et que l'article +garde le focus, son titre et ses pièces jointes passent dans +une colonne à côté du texte. La barre de commandes, la bannière de session +et le statut restent visibles. La disposition habituelle revient à la +fermeture du clavier ou au changement de focus, sans recréer le brouillon. +Sous Android uniquement, le champ du titre est masqué pendant la saisie +compacte avec le clavier ouvert, en portrait comme en paysage ; il réapparaît à la fermeture du clavier +ou quand le focus quitte l'article. Sa valeur n'est jamais effacée. + +En Debug Android, `fr.pschneider.postit.MarkdownEditorTestActivity` ouvre +un billet synthétique local, vérifie au moins trois lignes visibles et +le bas de l'éditeur au-dessus des insets natifs du clavier et les contrôles +en dehors des barres système, +ferme puis rouvre le clavier par toucher sans perte de focus. Le résultat +est écrit dans `files/markdown-editor-test-result.txt`. Ce test nécessite +Android 11 ou plus pour vérifier les insets du clavier ; il ne publie +aucun billet et retire sa page à la fin. +L'extra booléen `landscape=true` force le paysage pendant le test et restaure +l'orientation demandée précédemment à la fin. + ## Navigation Le host de navigation est un `NavigationPage x:Name="NavRoot"` diff --git a/src/PostIt/PostIt.Android/MainActivity.cs b/src/PostIt/PostIt.Android/MainActivity.cs index 0b56b3776..f962761c2 100644 --- a/src/PostIt/PostIt.Android/MainActivity.cs +++ b/src/PostIt/PostIt.Android/MainActivity.cs @@ -44,6 +44,28 @@ public class MainActivity : AvaloniaMainActivity private void OnScreenAwakeChanged() => RunOnUiThread(UpdateScreenAwake); + internal static void ShowSoftKeyboard() + { + if (Current is not { } activity || !IsForeground) + { + global::Android.Util.Log.Warn("PostIt", "Cannot show the keyboard without a foreground activity."); + return; + } + + activity.RunOnUiThread(() => + { + if (activity.CurrentFocus is not { } view + || activity.GetSystemService(InputMethodService) is not global::Android.Views.InputMethods.InputMethodManager inputMethod) + { + global::Android.Util.Log.Warn("PostIt", "Cannot show the keyboard without a focused native view and input method."); + return; + } + + if (!inputMethod.ShowSoftInput(view, global::Android.Views.InputMethods.ShowFlags.Implicit)) + global::Android.Util.Log.Warn("PostIt", "The input method did not accept the keyboard activation request."); + }); + } + private void UpdateScreenAwake() { if (Window is not { } window) return; diff --git a/src/PostIt/PostIt.Android/PlatformBootstrap.cs b/src/PostIt/PostIt.Android/PlatformBootstrap.cs index 9117aea69..30d76aba5 100644 --- a/src/PostIt/PostIt.Android/PlatformBootstrap.cs +++ b/src/PostIt/PostIt.Android/PlatformBootstrap.cs @@ -22,6 +22,7 @@ internal static class PlatformBootstrap Platform.CreateServiceProvider = AndroidServiceProvider.GetOrCreate; Platform.BackgroundChatService = AndroidBackgroundChatService.Instance; + Platform.ShowSoftKeyboard = MainActivity.ShowSoftKeyboard; Platform.TryGetCurrentLocationAsync = AndroidCurrentLocationProvider.TryGetCurrentLocationAsync; Platform.CreateVideoSurface = (player, replay) => new AndroidVideoSurface(player, replay); if (System.OperatingSystem.IsAndroidVersionAtLeast(26)) diff --git a/src/PostIt/PostIt.Android/PostIt.Android.csproj b/src/PostIt/PostIt.Android/PostIt.Android.csproj index a2e97677b..36736de19 100644 --- a/src/PostIt/PostIt.Android/PostIt.Android.csproj +++ b/src/PostIt/PostIt.Android/PostIt.Android.csproj @@ -8,6 +8,7 @@ 1 1.0 apk + true false $(RuntimeIdentifier) @@ -32,6 +33,7 @@ + diff --git a/src/PostIt/PostIt.Tests/AndroidMarkdownEditorTestActivity.cs b/src/PostIt/PostIt.Tests/AndroidMarkdownEditorTestActivity.cs new file mode 100644 index 000000000..02580a7cc --- /dev/null +++ b/src/PostIt/PostIt.Tests/AndroidMarkdownEditorTestActivity.cs @@ -0,0 +1,241 @@ +#if ANDROID +using System; +using System.IO; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using Android.App; +using Android.Content; +using Android.Content.PM; +using Android.OS; +using Android.Views; +using Android.Views.InputMethods; +using Avalonia; +using Avalonia.Controls; +using Avalonia.LogicalTree; +using Avalonia.Threading; +using AvaloniaEdit; +using PostIt.Android; +using PostIt.Controls; +using PostIt.ViewModels; +using PostIt.Views.Blogs; + +namespace PostIt.Tests; + +[Activity(Name = "fr.pschneider.postit.MarkdownEditorTestActivity", Exported = true)] +public sealed class AndroidMarkdownEditorTestActivity : Activity +{ + private string _stage = "startup"; + private ScreenOrientation? _previousOrientation; + protected override async void OnCreate(Bundle? state) + { + base.OnCreate(state); + var result = Path.Combine(FilesDir!.AbsolutePath, "markdown-editor-test-result.txt"); + File.WriteAllText(result, "RUNNING"); + try + { + using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(60)); + await RunAsync(timeout.Token); + File.WriteAllText(result, "PASS\nbounded viewport above IME,at least three visible lines,visible caret,keyboard reopen,retained focus"); + } + catch (Exception ex) + { + File.WriteAllText(result, $"FAIL at {_stage}\n" + ex); + } + finally + { + RunOnUiThread(Finish); + } + } + + private async Task RunAsync(CancellationToken ct) + { + if (!OperatingSystem.IsAndroidVersionAtLeast(30)) + throw new PlatformNotSupportedException("Android 11 required for keyboard visibility assertions."); + StartActivity(new Intent(this, typeof(MainActivity)).AddFlags(ActivityFlags.NewTask)); + _stage = "waiting for main activity"; + await WaitAsync(() => MainActivity.IsForeground && (Avalonia.Application.Current as App)?.View is not null, ct); + var root = ((App)Avalonia.Application.Current!).View!; + if (Intent?.GetBooleanExtra("landscape", false) == true) + { + _previousOrientation = MainActivity.Current!.RequestedOrientation; + MainActivity.Current.RequestedOrientation = ScreenOrientation.Landscape; + await WaitAsync(() => TopLevel.GetTopLevel(root)!.ClientSize.Width > TopLevel.GetTopLevel(root)!.ClientSize.Height, ct); + } + var navigation = root.FindControl("NavRoot")!; + BlogsPage? page = null; + TextEditor? source = null; + var focusLosses = 0; + try + { + await Dispatcher.UIThread.InvokeAsync(async () => + { + page = new BlogsPage + { + DataContext = new BlogsViewModel + { + IsEditing = true, + DraftTitle = "Local keyboard test", + DraftArticle = string.Join("\n", Enumerable.Range(1, 150).Select(i => $"Line {i}: local keyboard test")) + } + }; + await navigation.PushAsync(page, null); + source = page.GetLogicalDescendants().OfType().Single() + .FindControl("SourceEditor")!; + }); + _stage = "waiting for editor layout"; + await WaitAsync(() => source!.TextArea.Bounds.Height > 0, ct); + await Dispatcher.UIThread.InvokeAsync(() => CheckSystemBars(root)); + await Dispatcher.UIThread.InvokeAsync(() => + { + if (!source!.TextArea.Focus()) + throw new InvalidOperationException("Unable to focus the mounted editor."); + source.CaretOffset = source.Document.TextLength; + source.TextArea.Caret.BringCaretToView(); + source.TextArea.LostFocus += (_, _) => focusLosses++; + }); + _stage = "waiting for native input connection"; + await WaitAsync(() => + { + var activity = MainActivity.Current!; + var manager = (InputMethodManager)activity.GetSystemService(InputMethodService)!; + return activity.CurrentFocus is { } view && view.HasWindowFocus + && manager.InvokeIsActive(view) && manager.IsAcceptingText; + }, ct); + _stage = "opening keyboard"; + await TapAsync(source!, root); + await WaitAsync(KeyboardVisible, ct); + await Task.Delay(500, ct); + await Dispatcher.UIThread.InvokeAsync(() => CheckCaret(source!, root)); + var activity = MainActivity.Current!; + await Dispatcher.UIThread.InvokeAsync(() => + { + var manager = (InputMethodManager)activity.GetSystemService(InputMethodService)!; + if (!manager.HideSoftInputFromWindow(activity.CurrentFocus!.WindowToken, HideSoftInputFlags.None)) + throw new InvalidOperationException("Keyboard hide request rejected."); + }); + await WaitAsync(() => !KeyboardVisible(), ct); + await Task.Delay(500, ct); + await Dispatcher.UIThread.InvokeAsync(() => CheckSystemBars(root)); + await TapAsync(source!, root); + _stage = "reopening keyboard"; + await WaitAsync(KeyboardVisible, ct); + await Task.Delay(500, ct); + await Dispatcher.UIThread.InvokeAsync(() => + { + CheckCaret(source!, root); + if (focusLosses != 0) throw new InvalidOperationException($"Editor lost focus {focusLosses} times."); + }); + } + finally + { + if (page is not null) + await Dispatcher.UIThread.InvokeAsync(async () => + { + if (ReferenceEquals(navigation.NavigationStack.LastOrDefault(), page)) + await navigation.PopAsync(); + }); + if (_previousOrientation is { } orientation && MainActivity.Current is { } main) + main.RunOnUiThread(() => main.RequestedOrientation = orientation); + } + } + + private static bool KeyboardVisible() + => OperatingSystem.IsAndroidVersionAtLeast(30) + && MainActivity.Current?.Window?.DecorView?.RootWindowInsets?.IsVisible(WindowInsets.Type.Ime()) == true; + + private static void CheckCaret(TextEditor source, Control root) + { + var area = source.TextArea; + if (!area.IsFocused) throw new InvalidOperationException("Editor is not focused."); + var rect = area.Caret.CalculateCaretRectangle(); + var view = area.TextView; + var bottom = rect.Bottom - view.ScrollOffset.Y; + if (rect.Top - view.ScrollOffset.Y < -1 || bottom > view.Bounds.Height + 1) + throw new InvalidOperationException($"Caret outside viewport: bottom={bottom}, height={view.Bounds.Height}."); + var editorBottom = source.TranslatePoint(new Point(0, source.Bounds.Height), root)!.Value.Y; + if (editorBottom > root.Bounds.Height + 1) + throw new InvalidOperationException($"Editor outside root: bottom={editorBottom}, height={root.Bounds.Height}."); + if (view.Bounds.Height < view.DefaultLineHeight * 3) + throw new InvalidOperationException($"Less than three visible lines: viewport={view.Bounds.Height}, line={view.DefaultLineHeight}."); + if (OperatingSystem.IsAndroidVersionAtLeast(30) && KeyboardVisible()) + { + var activity = MainActivity.Current!; + var decor = activity.Window!.DecorView; + var keyboardTop = decor.Height - decor.RootWindowInsets!.GetInsets(WindowInsets.Type.Ime()).Bottom; + var location = new int[2]; + activity.CurrentFocus!.GetLocationInWindow(location); + var topLevel = TopLevel.GetTopLevel(root)!; + var bottomInWindow = source.TranslatePoint(new Point(0, source.Bounds.Height), topLevel)!.Value.Y + * topLevel.RenderScaling + location[1]; + if (bottomInWindow > keyboardTop + 2) + throw new InvalidOperationException($"Editor hidden by keyboard: bottom={bottomInWindow}, keyboardTop={keyboardTop}."); + } + CheckSystemBars(root); + } + + private static void CheckSystemBars(Control root) + { + if (!OperatingSystem.IsAndroidVersionAtLeast(30)) + throw new PlatformNotSupportedException("Android 11 required for native inset assertions."); + var activity = MainActivity.Current!; + var decor = activity.Window!.DecorView; + var insets = decor.RootWindowInsets!; + var bars = insets.GetInsets(WindowInsets.Type.SystemBars() | WindowInsets.Type.DisplayCutout()); + var location = new int[2]; + activity.CurrentFocus!.GetLocationInWindow(location); + var topLevel = TopLevel.GetTopLevel(root)!; + var navigation = root.FindControl("NavRoot")!; + var banner = root.FindControl("SessionBanner")!; + var top = navigation.TranslatePoint(new Point(), topLevel)!.Value.Y * topLevel.RenderScaling + location[1]; + var bottom = banner.TranslatePoint(new Point(0, banner.Bounds.Height), topLevel)!.Value.Y + * topLevel.RenderScaling + location[1]; + var visibleBottom = decor.Height - bars.Bottom; + if (KeyboardVisible()) + visibleBottom = Math.Min(visibleBottom, decor.Height - insets.GetInsets(WindowInsets.Type.Ime()).Bottom); + if (top < bars.Top - 2 || bottom > visibleBottom + 2 || !banner.IsVisible) + throw new InvalidOperationException($"Controls overlap system bars: top={top}, safeTop={bars.Top}, bottom={bottom}, safeBottom={visibleBottom}, banner={banner.IsVisible}."); + if (navigation.NavigationStack.LastOrDefault() is BlogsPage page) + { + var bar = NavigationPage.GetTopCommandBar(page)!; + var shouldShowTitle = !KeyboardVisible(); + if (!bar.IsVisible || bar.Bounds.Height <= 0 + || page.FindControl("DraftTitleTextBox")?.IsVisible != shouldShowTitle) + throw new InvalidOperationException("Blog controls are unavailable."); + if (page.FindControl("DraftTitleTextBox")?.Text != "Local keyboard test") + throw new InvalidOperationException("The title changed during keyboard layout transitions."); + } + } + + private static async Task TapAsync(TextEditor source, Control root) + { + await Dispatcher.UIThread.InvokeAsync(() => + { + var activity = MainActivity.Current!; + var point = source.TextArea.TranslatePoint(new Point(80, source.TextArea.Bounds.Height / 2), root)!.Value; + var location = new int[2]; + activity.CurrentFocus!.GetLocationInWindow(location); + var scale = TopLevel.GetTopLevel(root)!.RenderScaling; + var x = (float)(point.X * scale + location[0]); + var y = (float)(point.Y * scale + location[1]); + var now = SystemClock.UptimeMillis(); + using var properties = new MotionEvent.PointerProperties { Id = 0, ToolType = MotionEventToolType.Finger }; + using var coordinates = new MotionEvent.PointerCoords { X = x, Y = y, Pressure = 1, Size = 1 }; + using var down = MotionEvent.Obtain(now, now, MotionEventActions.Down, 1, + new[] { properties }, new[] { coordinates }, 0, 0, 1, 1, 0, 0, + InputSourceType.Touchscreen, 0)!; + using var up = MotionEvent.Obtain(now, now + 50, MotionEventActions.Up, 1, + new[] { properties }, new[] { coordinates }, 0, 0, 1, 1, 0, 0, + InputSourceType.Touchscreen, 0)!; + activity.DispatchTouchEvent(down); + activity.DispatchTouchEvent(up); + }); + } + + private static async Task WaitAsync(Func condition, CancellationToken ct) + { + while (!await Dispatcher.UIThread.InvokeAsync(condition)) + await Task.Delay(50, ct); + } +} +#endif diff --git a/src/PostIt/PostIt.Tests/BlogReaderTests.cs b/src/PostIt/PostIt.Tests/BlogReaderTests.cs index d07cf7a30..0b26cf33e 100644 --- a/src/PostIt/PostIt.Tests/BlogReaderTests.cs +++ b/src/PostIt/PostIt.Tests/BlogReaderTests.cs @@ -228,7 +228,6 @@ public class BlogReaderTests { Assert.True(reader.Bounds.Right <= list.Bounds.Left); Assert.Equal(2, Grid.GetColumn(page.BlogsHeaderPanel)); - Assert.Equal(2, Grid.GetColumn(page.BlogsCommandBar)); Assert.Equal(reader.Margin.Top, reader.Bounds.Top); Assert.Equal(page.StatusBar.Bounds.Top - reader.Margin.Bottom, reader.Bounds.Bottom); Assert.Equal(36, page.TranslatePoint(new Point(), nav)!.Value.Y); @@ -237,9 +236,9 @@ public class BlogReaderTests { Assert.True(list.Bounds.Bottom <= reader.Bounds.Top); Assert.Equal(0, Grid.GetColumn(page.BlogsHeaderPanel)); - Assert.Equal(0, Grid.GetColumn(page.BlogsCommandBar)); } + Assert.Same(page.BlogsCommandBar, NavigationPage.GetTopCommandBar(page)); var articleScroll = page.FindControl("ArticleReader")! .GetVisualDescendants().OfType().Single(); Assert.InRange(articleScroll.Viewport.Height, 1, window.ClientSize.Height); diff --git a/src/PostIt/PostIt.Tests/MarkdownEditorScrollTests.cs b/src/PostIt/PostIt.Tests/MarkdownEditorScrollTests.cs index 6cdaa5b5b..f89352b70 100644 --- a/src/PostIt/PostIt.Tests/MarkdownEditorScrollTests.cs +++ b/src/PostIt/PostIt.Tests/MarkdownEditorScrollTests.cs @@ -1,12 +1,17 @@ using Avalonia; using Avalonia.Controls; using Avalonia.Controls.Primitives; +using Avalonia.Headless; using Avalonia.Headless.XUnit; +using Avalonia.Input; using Avalonia.LogicalTree; using Avalonia.Threading; using Avalonia.VisualTree; using AvaloniaEdit; using PostIt.Controls; +using PostIt.Services; +using PostIt.ViewModels; +using PostIt.Views; using PostIt.Views.Blogs; namespace PostIt.Tests; @@ -16,6 +21,201 @@ public class MarkdownEditorScrollTests private static readonly string LongArticle = string.Join("\n\n", Enumerable.Range(1, 100).Select(i => $"Paragraph {i}: article content.")); + [AvaloniaFact] + public async Task Landscape_keyboard_preserves_controls_and_restores_editor_layout() + { + var root = new MainView(); + var page = new BlogsPage { DataContext = new BlogsViewModel { IsEditing = true, DraftArticle = LongArticle } }; + var window = new Window { Width = 800, Height = 400, Content = root }; + try + { + window.Show(); + await root.NavRoot.PushAsync(page, null); + Dispatcher.UIThread.RunJobs(); + window.UpdateLayout(); + var editor = page.ArticleEditor; + var source = editor.FindControl("SourceEditor")!; + source.TextArea.Focus(); + source.CaretOffset = source.Document.TextLength; + root.ApplyKeyboardOcclusion(new Rect(0, 200, 800, 200)); + window.UpdateLayout(); + Dispatcher.UIThread.RunJobs(); + window.UpdateLayout(); + Assert.True(page.DraftTitleTextBox.IsVisible); + Assert.True(page.AttachmentsBorder.IsVisible); + Assert.True(root.SessionBanner.IsVisible); + Assert.Equal(1, Grid.GetColumn(page.DraftTitleTextBox)); + Assert.Equal(1, Grid.GetColumn(page.AttachmentsBorder)); + Assert.True(page.StatusBar.IsVisible); + Assert.Equal(2, Grid.GetColumn(page.StatusBar)); + Assert.Same(page.BlogsCommandBar, NavigationPage.GetTopCommandBar(page)); + Assert.True(source.TextArea.Bounds.Height >= source.TextArea.TextView.DefaultLineHeight * 3); + var bottom = source.TranslatePoint(new Point(0, source.Bounds.Height), window)!.Value.Y; + Assert.InRange(bottom, 1, 200); + var document = source.Document; + root.ApplyKeyboardOcclusion(null); + window.UpdateLayout(); + Dispatcher.UIThread.RunJobs(); + window.UpdateLayout(); + Assert.True(page.DraftTitleTextBox.IsVisible); + Assert.True(page.AttachmentsBorder.IsVisible); + Assert.True(root.SessionBanner.IsVisible); + Assert.Equal(0, Grid.GetColumn(page.DraftTitleTextBox)); + Assert.Same(document, source.Document); + Assert.True(source.TextArea.IsFocused); + + window.Height = 200; + Dispatcher.UIThread.RunJobs(); + window.UpdateLayout(); + root.ApplyKeyboardOcclusion(new Rect(0, 200, 800, 200)); + Assert.Equal(0, root.Padding.Bottom); + } + finally + { + CloseWindow(window, page.ArticleEditor); + } + } + + [AvaloniaFact] + public void Keyboard_padding_preserves_system_bar_insets_and_restores_them() + { + var root = new MainView(); + var window = new Window { Width = 600, Height = 800, Content = root }; + try + { + window.Show(); + Dispatcher.UIThread.RunJobs(); + window.UpdateLayout(); + var safeArea = new Thickness(10, 24, 12, 30); + root.ApplySafeArea(safeArea); + Assert.Equal(safeArea, root.Padding); + root.ApplyKeyboardOcclusion(new Rect(0, 500, 600, 300)); + Assert.Equal(new Thickness(10, 24, 12, 300), root.Padding); + root.ApplySafeArea(new Thickness(20, 26, 22, 32)); + Assert.Equal(new Thickness(20, 26, 22, 300), root.Padding); + root.ApplyKeyboardOcclusion(null); + window.UpdateLayout(); + Assert.Equal(new Thickness(20, 26, 22, 32), root.Padding); + var navigationTop = root.NavRoot.TranslatePoint(new Point(), window)!.Value.Y; + var bannerBottom = root.SessionBanner.TranslatePoint(new Point(0, root.SessionBanner.Bounds.Height), window)!.Value.Y; + Assert.True(navigationTop >= 26); + Assert.True(bannerBottom <= 768); + Assert.True(root.SessionBanner.IsVisible); + } + finally + { + window.Close(); + } + } + + [AvaloniaTheory] + [InlineData(true)] + [InlineData(false)] + public void Resizing_keeps_the_focused_caret_visible_without_changing_selection(bool focused) + { + var editor = new MarkdownEditorControl { MarkdownText = LongArticle }; + var other = new TextBox(); + var panel = new DockPanel(); + DockPanel.SetDock(other, Dock.Top); + panel.Children.Add(other); + panel.Children.Add(editor); + var window = new Window { Width = 600, Height = 500, Content = panel }; + try + { + window.Show(); + Dispatcher.UIThread.RunJobs(); + window.UpdateLayout(); + var source = editor.FindControl("SourceEditor")!; + source.TextArea.Focus(); + source.Select(LongArticle.Length - 20, 10); + source.TextArea.Caret.BringCaretToView(); + Dispatcher.UIThread.RunJobs(); + window.UpdateLayout(); + var selectedText = source.SelectedText; + var caretOffset = source.CaretOffset; + var scroller = FindArticleScroller(editor, false); + if (!focused) + { + other.Focus(); + scroller.ScrollToHome(); + } + + window.Height = 250; + window.UpdateLayout(); + Dispatcher.UIThread.RunJobs(); + window.UpdateLayout(); + + Assert.Equal(selectedText, source.SelectedText); + Assert.Equal(caretOffset, source.CaretOffset); + if (focused) + { + Assert.True(source.TextArea.IsFocused); + var caret = source.TextArea.Caret.CalculateCaretRectangle(); + var view = source.TextArea.TextView; + Assert.InRange(caret.Top - view.ScrollOffset.Y, 0, view.Bounds.Height); + Assert.InRange(caret.Bottom - view.ScrollOffset.Y, 0, view.Bounds.Height); + } + else + { + Assert.True(other.IsFocused); + Assert.Equal(0, scroller.Offset.Y); + } + } + finally + { + CloseWindow(window, editor); + } + } + + [AvaloniaFact] + public void Repeated_touch_requests_keyboard_without_losing_focus_and_read_only_does_not() + { + var editor = new MarkdownEditorControl { MarkdownText = LongArticle }; + var window = new Window { Width = 600, Height = 400, Content = editor }; + var previous = Platform.ShowSoftKeyboard; + var requests = 0; + Platform.ShowSoftKeyboard = () => requests++; + try + { + window.Show(); + Dispatcher.UIThread.RunJobs(); + window.UpdateLayout(); + var source = editor.FindControl("SourceEditor")!; + source.TextArea.Focus(); + var focusLosses = 0; + source.TextArea.LostFocus += (_, _) => focusLosses++; + var point = source.TextArea.TranslatePoint(new Point(60, 20), window)!.Value; + for (var i = 0; i < 2; i++) + { + using var touch = window.TouchBegin(point); + window.TouchEnd(touch, point); + Assert.True(source.TextArea.IsFocused); + Assert.Equal(i + 1, requests); + } + + window.MouseDown(point, MouseButton.Left); + window.MouseUp(point, MouseButton.Left); + Assert.Equal(2, requests); + using (var scrollTouch = window.TouchBegin(point)) + { + var end = point + new Vector(0, -80); + window.TouchMove(scrollTouch, end); + window.TouchEnd(scrollTouch, end); + } + Assert.Equal(2, requests); + source.IsReadOnly = true; + using var readOnlyTouch = window.TouchBegin(point); + window.TouchEnd(readOnlyTouch, point); + Assert.Equal(2, requests); + Assert.Equal(0, focusLosses); + } + finally + { + Platform.ShowSoftKeyboard = previous; + CloseWindow(window, editor); + } + } + [AvaloniaTheory] [InlineData(false)] [InlineData(true)] diff --git a/src/PostIt/PostIt/Controls/MarkdownEditorControl.axaml.cs b/src/PostIt/PostIt/Controls/MarkdownEditorControl.axaml.cs index 2538f7168..89c7126f3 100644 --- a/src/PostIt/PostIt/Controls/MarkdownEditorControl.axaml.cs +++ b/src/PostIt/PostIt/Controls/MarkdownEditorControl.axaml.cs @@ -1,12 +1,19 @@ using System; using Avalonia; using Avalonia.Controls; +using Avalonia.Input; +using Avalonia.Interactivity; +using Avalonia.Threading; +using Avalonia.VisualTree; using AvaloniaEdit.Document; +using PostIt.Services; namespace PostIt.Controls; public partial class MarkdownEditorControl : UserControl { + private IPointer? _touchPointer; + private Point _touchStart; public static readonly StyledProperty MarkdownTextProperty = AvaloniaProperty.Register(nameof(MarkdownText)); @@ -38,6 +45,44 @@ public partial class MarkdownEditorControl : UserControl { InitializeComponent(); PropertyChanged += OnPropertyChanged; + SourceEditor.TextArea.SizeChanged += (_, _) => + Dispatcher.UIThread.Post(KeepCaretVisible, DispatcherPriority.Loaded); + SourceEditor.TextArea.AddHandler(InputElement.PointerPressedEvent, OnEditorPointerPressed, + RoutingStrategies.Tunnel, handledEventsToo: true); + SourceEditor.TextArea.AddHandler(InputElement.PointerReleasedEvent, OnEditorPointerReleased, + RoutingStrategies.Bubble, handledEventsToo: true); + } + + private void KeepCaretVisible() + { + if (SourceEditor.TextArea.IsFocused && SourceEditor.IsEffectivelyVisible + && SourceEditor.TextArea.Bounds.Height > 0) + SourceEditor.TextArea.Caret.BringCaretToView(); + } + + private void OnEditorPointerPressed(object? sender, PointerPressedEventArgs e) + { + if (e.Pointer.Type != PointerType.Touch) + return; + _touchPointer = e.Pointer; + _touchStart = e.GetPosition(SourceEditor.TextArea); + } + + private void OnEditorPointerReleased(object? sender, PointerReleasedEventArgs e) + { + if (!ReferenceEquals(_touchPointer, e.Pointer)) + return; + _touchPointer = null; + var point = e.GetPosition(SourceEditor.TextArea); + var settings = this.GetPlatformSettings() + ?? throw new InvalidOperationException("The editor has no platform input settings."); + var tapSize = settings.GetTapSize(PointerType.Touch); + // AvaloniaEdit changes pointer capture during selection, so its taps may not raise Tapped. + if (new Rect(_touchStart, default(Size)).Inflate(new Thickness(tapSize.Width, tapSize.Height)).Contains(point) + && SourceEditor.TextArea.IsFocused + && !SourceEditor.IsReadOnly + && new Rect(SourceEditor.TextArea.Bounds.Size).Contains(point)) + Platform.ShowSoftKeyboard?.Invoke(); } private void OnPropertyChanged(object? sender, AvaloniaPropertyChangedEventArgs e) diff --git a/src/PostIt/PostIt/Services/Platform.cs b/src/PostIt/PostIt/Services/Platform.cs index c7b717818..9038a82f7 100644 --- a/src/PostIt/PostIt/Services/Platform.cs +++ b/src/PostIt/PostIt/Services/Platform.cs @@ -12,6 +12,7 @@ namespace PostIt.Services; /// public static class Platform { + public static Action? ShowSoftKeyboard { get; set; } public static ScreenAwakeRequests ScreenAwake { get; } = new(); private static bool _isApplicationActive = !OperatingSystem.IsAndroid(); public static event Action? ApplicationActivityChanged; diff --git a/src/PostIt/PostIt/Views/Blogs/BlogsPage.axaml b/src/PostIt/PostIt/Views/Blogs/BlogsPage.axaml index a2116ff31..be9118ffe 100644 --- a/src/PostIt/PostIt/Views/Blogs/BlogsPage.axaml +++ b/src/PostIt/PostIt/Views/Blogs/BlogsPage.axaml @@ -155,7 +155,6 @@ - - @@ -248,18 +246,15 @@ - + @@ -284,6 +280,7 @@ + @@ -335,5 +332,4 @@ Grid.Row="5" DataContext="{Binding ActionStatus}" /> - diff --git a/src/PostIt/PostIt/Views/Blogs/BlogsPage.axaml.cs b/src/PostIt/PostIt/Views/Blogs/BlogsPage.axaml.cs index 9123ca1a9..26eb7177c 100644 --- a/src/PostIt/PostIt/Views/Blogs/BlogsPage.axaml.cs +++ b/src/PostIt/PostIt/Views/Blogs/BlogsPage.axaml.cs @@ -7,6 +7,9 @@ using Avalonia.Layout; using Avalonia.Platform.Storage; using Avalonia.Controls.Primitives; using Avalonia.Media; +using Avalonia.Input; +using Avalonia.Interactivity; +using Avalonia.VisualTree; using CommunityToolkit.Mvvm.Input; using Markdown.Avalonia; @@ -17,11 +20,14 @@ public partial class BlogsPage : ContentPage private static readonly RelayCommand DisabledLinkCommand = new(static _ => { }, static _ => false); private bool? _isWideLayout; + private bool? _isCompactEditor; private ViewModels.BlogsViewModel? _observedViewModel; public BlogsPage() { InitializeComponent(); + AddHandler(InputElement.GotFocusEvent, (_, _) => QueueLayoutUpdate(), RoutingStrategies.Bubble); + AddHandler(InputElement.LostFocusEvent, (_, _) => QueueLayoutUpdate(), RoutingStrategies.Bubble); SizeChanged += (_, _) => Avalonia.Threading.Dispatcher.UIThread.Post(() => { @@ -38,6 +44,9 @@ public partial class BlogsPage : ContentPage UpdateHyperlinkCommand(); } + private void QueueLayoutUpdate() + => Avalonia.Threading.Dispatcher.UIThread.Post(UpdateResponsiveLayout); + private void UpdateResponsiveLayout() { var size = TopLevel.GetTopLevel(this)?.ClientSize ?? Bounds.Size; @@ -46,6 +55,23 @@ public partial class BlogsPage : ContentPage // Use the window size so compacting the navigation bar cannot change the orientation. var isWide = size.Width >= size.Height; + var compact = this.FindAncestorOfType()?.IsSoftKeyboardOpen == true + && ArticleEditor.IsKeyboardFocusWithin && !ArticleEditor.IsPreviewVisible; + var sideBySide = compact && isWide; + if (_isCompactEditor != compact || _isWideLayout != isWide) + { + _isCompactEditor = compact; + DraftTitleTextBox.IsVisible = !compact || !System.OperatingSystem.IsAndroid(); + PostEditor.ColumnDefinitions = new ColumnDefinitions(sideBySide ? "3*,2*" : "*"); + SetArea(ArticleEditor, sideBySide ? 0 : 1, 0, rowSpan: sideBySide ? 3 : 1); + SetArea(DraftTitleTextBox, 0, sideBySide ? 1 : 0); + SetArea(AttachmentsBorder, sideBySide ? 1 : 2, sideBySide ? 1 : 0); + ArticleEditor.SourceEditor.Padding = compact ? new Thickness(2) : new Thickness(5, 5, 5, 15); + PostEditor.Margin = new Thickness(compact ? 4 : 12); + PostEditor.RowSpacing = compact ? 2 : 10; + } + SetArea(StatusBar, sideBySide ? 1 : isWide ? 3 : 5, sideBySide ? 2 : 0, + columnSpan: isWide && !sideBySide ? 3 : 1); if (_isWideLayout == isWide) return; _isWideLayout = isWide; @@ -58,7 +84,6 @@ public partial class BlogsPage : ContentPage SetArea(PostsSplitter, isWide ? 0 : 3, isWide ? 1 : 0, isWide ? 3 : 1); SetArea(PostReader, isWide ? 0 : 4, 0, isWide ? 3 : 1); SetArea(PostEditor, isWide ? 0 : 4, 0, isWide ? 3 : 1); - SetArea(StatusBar, isWide ? 3 : 5, 0, columnSpan: isWide ? 3 : 1); PostsSplitter.ResizeDirection = isWide ? GridResizeDirection.Columns : GridResizeDirection.Rows; PostsSplitter.HorizontalAlignment = isWide ? HorizontalAlignment.Center : HorizontalAlignment.Stretch; PostsSplitter.VerticalAlignment = isWide ? VerticalAlignment.Stretch : VerticalAlignment.Center; diff --git a/src/PostIt/PostIt/Views/Layout/MainView.axaml.cs b/src/PostIt/PostIt/Views/Layout/MainView.axaml.cs index 22bf537c6..9c7a40a80 100644 --- a/src/PostIt/PostIt/Views/Layout/MainView.axaml.cs +++ b/src/PostIt/PostIt/Views/Layout/MainView.axaml.cs @@ -1,13 +1,75 @@ using System; +using Avalonia; using Avalonia.Controls; +using Avalonia.Controls.Platform; namespace PostIt.Views; public partial class MainView : UserControl { + private IInputPane? _inputPane; + private IInsetsManager? _insetsManager; + private Thickness _safeArea; + private Rect? _keyboardRect; + internal bool IsSoftKeyboardOpen { get; private set; } + public MainView() { InitializeComponent(); + TopLevel.SetAutoSafeAreaPadding(this, false); + AttachedToVisualTree += (_, _) => + { + var topLevel = TopLevel.GetTopLevel(this); + _inputPane = topLevel?.InputPane; + _insetsManager = topLevel?.InsetsManager; + if (_insetsManager is not null) + { + _insetsManager.SafeAreaChanged += SafeAreaChanged; + _safeArea = _insetsManager.SafeAreaPadding; + } + if (_inputPane is not null) + { + _inputPane.StateChanged += InputPaneStateChanged; + ApplyKeyboardOcclusion(_inputPane.State == InputPaneState.Open ? _inputPane.OccludedRect : null); + } + ApplyKeyboardOcclusion(_keyboardRect); + }; + DetachedFromVisualTree += (_, _) => + { + if (_inputPane is not null) + _inputPane.StateChanged -= InputPaneStateChanged; + _inputPane = null; + if (_insetsManager is not null) + _insetsManager.SafeAreaChanged -= SafeAreaChanged; + _insetsManager = null; + }; + SizeChanged += (_, _) => ApplyKeyboardOcclusion(_keyboardRect); + } + + private void InputPaneStateChanged(object? sender, InputPaneStateEventArgs e) + => ApplyKeyboardOcclusion(e.NewState == InputPaneState.Open ? e.EndRect : null); + + private void SafeAreaChanged(object? sender, SafeAreaChangedArgs e) + => ApplySafeArea(e.SafeAreaPadding); + + internal void ApplySafeArea(Thickness safeArea) + { + _safeArea = safeArea; + ApplyKeyboardOcclusion(_keyboardRect); + } + + internal void ApplyKeyboardOcclusion(Rect? keyboardRect) + { + _keyboardRect = keyboardRect; + IsSoftKeyboardOpen = keyboardRect is { Height: > 0 }; + var topLevel = TopLevel.GetTopLevel(this); + var bottom = topLevel is null ? null : this.TranslatePoint(new Point(0, Bounds.Height), topLevel); + // Subtract only actual overlap: AdjustResize may already have removed the keyboard area. + var overlap = keyboardRect is { Height: > 0 } rect && bottom is { } point + ? Math.Clamp(point.Y - rect.Top, 0, Bounds.Height) + : 0; + Padding = new Thickness(_safeArea.Left, _safeArea.Top, _safeArea.Right, + Math.Max(_safeArea.Bottom, overlap)); } protected override void OnDataContextChanged(EventArgs e)