Blog edition accessibility
This commit is contained in:
parent
a131a6ddf8
commit
f1c23965b2
14 changed files with 656 additions and 16 deletions
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@
|
|||
<ApplicationVersion>1</ApplicationVersion>
|
||||
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
|
||||
<AndroidPackageFormat>apk</AndroidPackageFormat>
|
||||
<EmbedAssembliesIntoApk>true</EmbedAssembliesIntoApk>
|
||||
<AndroidEnableProfiledAot>false</AndroidEnableProfiledAot>
|
||||
<!-- LibVLC's native package targets inspect the plural RID property. -->
|
||||
<RuntimeIdentifiers Condition="'$(RuntimeIdentifiers)' == '' and '$(RuntimeIdentifier)' != ''">$(RuntimeIdentifier)</RuntimeIdentifiers>
|
||||
|
|
@ -32,6 +33,7 @@
|
|||
<ProjectReference Include="..\PostIt\PostIt.csproj" />
|
||||
</ItemGroup>
|
||||
<ItemGroup Condition="'$(Configuration)' == 'Debug'">
|
||||
<Compile Include="..\PostIt.Tests\AndroidMarkdownEditorTestActivity.cs" Link="Tests\AndroidMarkdownEditorTestActivity.cs" />
|
||||
<Compile Include="..\PostIt.Tests\AndroidChatServiceTestActivity.cs" Link="Tests\AndroidChatServiceTestActivity.cs" />
|
||||
<Compile Include="..\PostIt.Tests\AndroidPodcastCaptureTestActivity.cs" Link="Tests\AndroidPodcastCaptureTestActivity.cs" />
|
||||
<Compile Include="..\PostIt.Tests\AndroidCameraFrameRotationTestActivity.cs" Link="Tests\AndroidCameraFrameRotationTestActivity.cs" />
|
||||
|
|
|
|||
241
src/PostIt/PostIt.Tests/AndroidMarkdownEditorTestActivity.cs
Normal file
241
src/PostIt/PostIt.Tests/AndroidMarkdownEditorTestActivity.cs
Normal file
|
|
@ -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<NavigationPage>("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<MarkdownEditorControl>().Single()
|
||||
.FindControl<TextEditor>("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<NavigationPage>("NavRoot")!;
|
||||
var banner = root.FindControl<PostIt.Views.SessionStatusBanner>("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<TextBox>("DraftTitleTextBox")?.IsVisible != shouldShowTitle)
|
||||
throw new InvalidOperationException("Blog controls are unavailable.");
|
||||
if (page.FindControl<TextBox>("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<bool> condition, CancellationToken ct)
|
||||
{
|
||||
while (!await Dispatcher.UIThread.InvokeAsync(condition))
|
||||
await Task.Delay(50, ct);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
|
@ -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<MarkdownScrollViewer>("ArticleReader")!
|
||||
.GetVisualDescendants().OfType<ScrollViewer>().Single();
|
||||
Assert.InRange(articleScroll.Viewport.Height, 1, window.ClientSize.Height);
|
||||
|
|
|
|||
|
|
@ -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<TextEditor>("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<TextEditor>("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<TextEditor>("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)]
|
||||
|
|
|
|||
|
|
@ -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<string?> MarkdownTextProperty =
|
||||
AvaloniaProperty.Register<MarkdownEditorControl, string?>(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)
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ namespace PostIt.Services;
|
|||
/// </summary>
|
||||
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;
|
||||
|
|
|
|||
|
|
@ -155,7 +155,6 @@
|
|||
</CommandBar.SecondaryCommands>
|
||||
</CommandBar>
|
||||
</NavigationPage.TopCommandBar>
|
||||
<ScrollViewer>
|
||||
<Grid
|
||||
x:Name="MainGrid"
|
||||
Margin="5,5,10,5"
|
||||
|
|
@ -239,7 +238,6 @@
|
|||
IsVisible="{Binding IsEditing}"
|
||||
RowSpacing="10">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="*" />
|
||||
<RowDefinition Height="Auto" />
|
||||
|
|
@ -248,18 +246,15 @@
|
|||
<ColumnDefinition Width="*" />
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<TextBlock
|
||||
Grid.Row="0"
|
||||
FontWeight="SemiBold"
|
||||
Text="Post detail" />
|
||||
<TextBox
|
||||
x:Name="DraftTitleTextBox"
|
||||
Grid.Row="1"
|
||||
Grid.Row="0"
|
||||
PlaceholderText="Title"
|
||||
Text="{Binding DraftTitle, Mode=TwoWay}" />
|
||||
|
||||
<postitControls:MarkdownEditorControl
|
||||
Grid.Row="2"
|
||||
x:Name="ArticleEditor"
|
||||
Grid.Row="1"
|
||||
HorizontalAlignment="Stretch"
|
||||
VerticalAlignment="Stretch"
|
||||
Document="{Binding DraftArticleDocument, Mode=TwoWay}"
|
||||
|
|
@ -268,12 +263,13 @@
|
|||
|
||||
<Border
|
||||
x:Name="AttachmentsBorder"
|
||||
Grid.Row="3"
|
||||
Grid.Row="2"
|
||||
Padding="8"
|
||||
HorizontalAlignment="Stretch"
|
||||
VerticalAlignment="Stretch"
|
||||
BorderBrush="LightGray"
|
||||
BorderThickness="1">
|
||||
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">
|
||||
<StackPanel Spacing="4">
|
||||
<TextBlock FontWeight="SemiBold" Text="Pièces jointes" />
|
||||
<ItemsControl ItemsSource="{Binding DraftAttachments}">
|
||||
|
|
@ -284,6 +280,7 @@
|
|||
</ItemsControl.ItemTemplate>
|
||||
</ItemsControl>
|
||||
</StackPanel>
|
||||
</ScrollViewer>
|
||||
</Border>
|
||||
|
||||
</Grid>
|
||||
|
|
@ -335,5 +332,4 @@
|
|||
Grid.Row="5"
|
||||
DataContext="{Binding ActionStatus}" />
|
||||
</Grid>
|
||||
</ScrollViewer>
|
||||
</ContentPage>
|
||||
|
|
|
|||
|
|
@ -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<string?> 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<MainView>()?.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;
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in a new issue