From 960ea2260cebf30d7f79d4d6e85cb96afdd2271c Mon Sep 17 00:00:00 2001 From: affederaffe <68356204+affederaffe@users.noreply.github.com> Date: Sat, 28 May 2022 16:45:55 +0200 Subject: [PATCH 01/11] Add xdg-desktop-portal file chooser --- src/Avalonia.FreeDesktop/DBusFileChooser.cs | 32 ++++++++ src/Avalonia.FreeDesktop/DBusRequest.cs | 16 ++++ src/Avalonia.FreeDesktop/DBusSystemDialog.cs | 80 ++++++++++++++++++++ 3 files changed, 128 insertions(+) create mode 100644 src/Avalonia.FreeDesktop/DBusFileChooser.cs create mode 100644 src/Avalonia.FreeDesktop/DBusRequest.cs create mode 100644 src/Avalonia.FreeDesktop/DBusSystemDialog.cs diff --git a/src/Avalonia.FreeDesktop/DBusFileChooser.cs b/src/Avalonia.FreeDesktop/DBusFileChooser.cs new file mode 100644 index 0000000000..996f5dfe86 --- /dev/null +++ b/src/Avalonia.FreeDesktop/DBusFileChooser.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Runtime.CompilerServices; +using System.Threading.Tasks; +using Tmds.DBus; + +[assembly: InternalsVisibleTo(Connection.DynamicAssemblyName)] +namespace Avalonia.FreeDesktop +{ + [DBusInterface("org.freedesktop.portal.FileChooser")] + public interface IFileChooser : IDBusObject + { + Task OpenFileAsync(string ParentWindow, string Title, IDictionary Options); + Task SaveFileAsync(string ParentWindow, string Title, IDictionary Options); + Task SaveFilesAsync(string ParentWindow, string Title, IDictionary Options); + Task GetAsync(string prop); + Task GetAllAsync(); + Task SetAsync(string prop, object val); + Task WatchPropertiesAsync(Action handler); + } + + [Dictionary] + public class FileChooserProperties + { + public uint Version { get; set; } + } + + public static class FileChooserExtensions + { + public static Task GetVersionAsync(this IFileChooser o) => o.GetAsync("version"); + } +} diff --git a/src/Avalonia.FreeDesktop/DBusRequest.cs b/src/Avalonia.FreeDesktop/DBusRequest.cs new file mode 100644 index 0000000000..940a476916 --- /dev/null +++ b/src/Avalonia.FreeDesktop/DBusRequest.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Runtime.CompilerServices; +using System.Threading.Tasks; +using Tmds.DBus; + +[assembly: InternalsVisibleTo(Connection.DynamicAssemblyName)] +namespace Avalonia.FreeDesktop +{ + [DBusInterface("org.freedesktop.portal.Request")] + internal interface IRequest : IDBusObject + { + Task CloseAsync(); + Task WatchResponseAsync(Action<(uint response, IDictionary results)> handler, Action onError = null); + } +} diff --git a/src/Avalonia.FreeDesktop/DBusSystemDialog.cs b/src/Avalonia.FreeDesktop/DBusSystemDialog.cs new file mode 100644 index 0000000000..d76e22b113 --- /dev/null +++ b/src/Avalonia.FreeDesktop/DBusSystemDialog.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Avalonia.Controls; +using Avalonia.Controls.Platform; +using Tmds.DBus; + +namespace Avalonia.FreeDesktop +{ + public class DBusSystemDialog : ISystemDialogImpl + { + private readonly IFileChooser _fileChooser; + + public DBusSystemDialog() + { + _fileChooser = DBusHelper.Connection.CreateProxy("org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop"); + } + + public async Task ShowFileDialogAsync(FileDialog dialog, Window parent) + { + var parentWindow = $"x11:{parent.PlatformImpl!.Handle.Handle.ToString("X")}"; + ObjectPath objectPath; + var options = new Dictionary(); + if (dialog.Filters is not null) + options.Add("filters", ParseFilters(dialog)); + + switch (dialog) + { + case OpenFileDialog openFileDialog: + options.Add("multiple", openFileDialog.AllowMultiple); + objectPath = await _fileChooser.OpenFileAsync(parentWindow, openFileDialog.Title ?? string.Empty, options); + break; + case SaveFileDialog saveFileDialog: + if (saveFileDialog.InitialFileName is not null) + options.Add("current_name", saveFileDialog.InitialFileName); + if (saveFileDialog.Directory is not null) + options.Add("current_folder", Encoding.UTF8.GetBytes(saveFileDialog.Directory)); + objectPath = await _fileChooser.SaveFileAsync(parentWindow, saveFileDialog.Title ?? string.Empty, options); + break; + } + + var request = DBusHelper.Connection.CreateProxy("org.freedesktop.portal.Request", objectPath); + var tsc = new TaskCompletionSource(); + using var disposable = await request.WatchResponseAsync(x => tsc.TrySetResult(x.results["uris"] as string[])); + var uris = await tsc.Task; + for (var i = 0; i < uris.Length; i++) + uris[i] = new Uri(uris[i]).AbsolutePath; + return uris; + } + + public async Task ShowFolderDialogAsync(OpenFolderDialog dialog, Window parent) + { + var parentWindow = $"x11:{parent.PlatformImpl!.Handle.Handle.ToString("X")}"; + var options = new Dictionary + { + { "directory", true } + }; + var objectPath = await _fileChooser.OpenFileAsync(parentWindow, dialog.Title ?? string.Empty, options); + var request = DBusHelper.Connection.CreateProxy("org.freedesktop.portal.Request", objectPath); + var tsc = new TaskCompletionSource(); + using var disposable = await request.WatchResponseAsync(x => tsc.TrySetResult(x.results["uris"] as string[])); + var uris = await tsc.Task; + return uris.Length != 1 ? string.Empty : new Uri(uris[0]).AbsolutePath; + } + + private static (string name, (uint style, string extension)[])[] ParseFilters(FileDialog dialog) + { + var filters = new (string name, (uint style, string extension)[])[dialog.Filters!.Count]; + for (var i = 0; i < filters.Length; i++) + { + var extensions = dialog.Filters[i].Extensions.Select(static x => (0u, x)).ToArray(); + filters[i] = (dialog.Filters[i].Name, extensions); + } + + return filters; + } + } +} From 4c0fda3495d14b5357a161218aca67592b3663aa Mon Sep 17 00:00:00 2001 From: affederaffe <68356204+affederaffe@users.noreply.github.com> Date: Sat, 28 May 2022 22:54:14 +0200 Subject: [PATCH 02/11] Make classes internal --- src/Avalonia.FreeDesktop/DBusFileChooser.cs | 6 +++--- src/Avalonia.FreeDesktop/DBusSystemDialog.cs | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Avalonia.FreeDesktop/DBusFileChooser.cs b/src/Avalonia.FreeDesktop/DBusFileChooser.cs index 996f5dfe86..24db614a02 100644 --- a/src/Avalonia.FreeDesktop/DBusFileChooser.cs +++ b/src/Avalonia.FreeDesktop/DBusFileChooser.cs @@ -8,7 +8,7 @@ using Tmds.DBus; namespace Avalonia.FreeDesktop { [DBusInterface("org.freedesktop.portal.FileChooser")] - public interface IFileChooser : IDBusObject + internal interface IFileChooser : IDBusObject { Task OpenFileAsync(string ParentWindow, string Title, IDictionary Options); Task SaveFileAsync(string ParentWindow, string Title, IDictionary Options); @@ -20,12 +20,12 @@ namespace Avalonia.FreeDesktop } [Dictionary] - public class FileChooserProperties + internal class FileChooserProperties { public uint Version { get; set; } } - public static class FileChooserExtensions + internal static class FileChooserExtensions { public static Task GetVersionAsync(this IFileChooser o) => o.GetAsync("version"); } diff --git a/src/Avalonia.FreeDesktop/DBusSystemDialog.cs b/src/Avalonia.FreeDesktop/DBusSystemDialog.cs index d76e22b113..88f3e528e5 100644 --- a/src/Avalonia.FreeDesktop/DBusSystemDialog.cs +++ b/src/Avalonia.FreeDesktop/DBusSystemDialog.cs @@ -9,11 +9,11 @@ using Tmds.DBus; namespace Avalonia.FreeDesktop { - public class DBusSystemDialog : ISystemDialogImpl + internal class DBusSystemDialog : ISystemDialogImpl { private readonly IFileChooser _fileChooser; - public DBusSystemDialog() + internal DBusSystemDialog() { _fileChooser = DBusHelper.Connection.CreateProxy("org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop"); } From 8026fb1047e9ce309056787d7277aa36ff631c70 Mon Sep 17 00:00:00 2001 From: affederaffe <68356204+affederaffe@users.noreply.github.com> Date: Sun, 29 May 2022 12:15:00 +0200 Subject: [PATCH 03/11] Fall back to managed dialogs when xdg-desktop-portal is unavailable --- .../ManagedFileDialogExtensions.cs | 8 ++- .../Avalonia.FreeDesktop.csproj | 2 + src/Avalonia.FreeDesktop/DBusHelper.cs | 24 +++---- src/Avalonia.FreeDesktop/DBusSystemDialog.cs | 68 +++++++++++++++---- src/Avalonia.X11/X11Platform.cs | 9 ++- 5 files changed, 78 insertions(+), 33 deletions(-) diff --git a/src/Avalonia.Dialogs/ManagedFileDialogExtensions.cs b/src/Avalonia.Dialogs/ManagedFileDialogExtensions.cs index 1970c5557d..effdc847a0 100644 --- a/src/Avalonia.Dialogs/ManagedFileDialogExtensions.cs +++ b/src/Avalonia.Dialogs/ManagedFileDialogExtensions.cs @@ -139,9 +139,15 @@ namespace Avalonia.Dialogs return builder; } + public static Task ShowManagedAsync(this FileDialog dialog, Window parent) + => new ManagedSystemDialogImpl().ShowFileDialogAsync(dialog, parent); + + public static Task ShowManagedAsync(this OpenFolderDialog dialog, Window parent) + => new ManagedSystemDialogImpl().ShowFolderDialogAsync(dialog, parent); + public static Task ShowManagedAsync(this OpenFileDialog dialog, Window parent, ManagedFileDialogOptions options = null) => ShowManagedAsync(dialog, parent, options); - + public static Task ShowManagedAsync(this OpenFileDialog dialog, Window parent, ManagedFileDialogOptions options = null) where TWindow : Window, new() { diff --git a/src/Avalonia.FreeDesktop/Avalonia.FreeDesktop.csproj b/src/Avalonia.FreeDesktop/Avalonia.FreeDesktop.csproj index e9d6394aa5..a5cb207223 100644 --- a/src/Avalonia.FreeDesktop/Avalonia.FreeDesktop.csproj +++ b/src/Avalonia.FreeDesktop/Avalonia.FreeDesktop.csproj @@ -2,10 +2,12 @@ net6.0;netstandard2.0 + enable + diff --git a/src/Avalonia.FreeDesktop/DBusHelper.cs b/src/Avalonia.FreeDesktop/DBusHelper.cs index c14539d7bf..7204e51dbd 100644 --- a/src/Avalonia.FreeDesktop/DBusHelper.cs +++ b/src/Avalonia.FreeDesktop/DBusHelper.cs @@ -6,7 +6,7 @@ using Tmds.DBus; namespace Avalonia.FreeDesktop { - public class DBusHelper + public static class DBusHelper { /// /// This class uses synchronous execution at DBus connection establishment stage @@ -14,14 +14,14 @@ namespace Avalonia.FreeDesktop /// private class DBusSyncContext : SynchronizationContext { - private SynchronizationContext _ctx; - private object _lock = new object(); + private readonly object _lock = new(); + private SynchronizationContext? _ctx; public override void Post(SendOrPostCallback d, object state) { lock (_lock) { - if (_ctx != null) + if (_ctx is not null) _ctx?.Post(d, state); else lock (_lock) @@ -33,10 +33,9 @@ namespace Avalonia.FreeDesktop { lock (_lock) { - if (_ctx != null) + if (_ctx is not null) _ctx?.Send(d, state); else - d(state); } } @@ -47,15 +46,14 @@ namespace Avalonia.FreeDesktop _ctx = new AvaloniaSynchronizationContext(); } } - public static Connection Connection { get; private set; } - public static Connection TryInitialize(string dbusAddress = null) + public static Connection? Connection { get; private set; } + + public static Connection? TryInitialize(string? dbusAddress = null) + => Connection ?? TryCreateNewConnection(dbusAddress); + + public static Connection? TryCreateNewConnection(string? dbusAddress = null) { - return Connection ?? TryCreateNewConnection(dbusAddress); - } - - public static Connection TryCreateNewConnection(string dbusAddress = null) - { var oldContext = SynchronizationContext.Current; try { diff --git a/src/Avalonia.FreeDesktop/DBusSystemDialog.cs b/src/Avalonia.FreeDesktop/DBusSystemDialog.cs index 88f3e528e5..7bc287ea28 100644 --- a/src/Avalonia.FreeDesktop/DBusSystemDialog.cs +++ b/src/Avalonia.FreeDesktop/DBusSystemDialog.cs @@ -5,20 +5,56 @@ using System.Text; using System.Threading.Tasks; using Avalonia.Controls; using Avalonia.Controls.Platform; +using Avalonia.Dialogs; +using Avalonia.Logging; using Tmds.DBus; namespace Avalonia.FreeDesktop { internal class DBusSystemDialog : ISystemDialogImpl { - private readonly IFileChooser _fileChooser; + private readonly IFileChooser? _fileChooser; + private bool _isDbusAvailable; internal DBusSystemDialog() { - _fileChooser = DBusHelper.Connection.CreateProxy("org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop"); + _fileChooser = DBusHelper.Connection?.CreateProxy("org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop"); + _isDbusAvailable = _fileChooser is not null; } - public async Task ShowFileDialogAsync(FileDialog dialog, Window parent) + public async Task ShowFileDialogAsync(FileDialog dialog, Window parent) + { + if (!_isDbusAvailable) + return await dialog.ShowManagedAsync(parent); + try + { + return await ShowNativeFileDialogAsync(dialog, parent); + } + catch (Exception e) + { + Logger.TryGet(LogEventLevel.Error, LogArea.X11Platform)?.Log(this, e.Message); + _isDbusAvailable = false; + return await dialog.ShowManagedAsync(parent); + } + } + + public async Task ShowFolderDialogAsync(OpenFolderDialog dialog, Window parent) + { + if (!_isDbusAvailable) + return await dialog.ShowManagedAsync(parent); + try + { + return await ShowNativeFolderDialogAsync(dialog, parent); + } + catch (Exception e) + { + Logger.TryGet(LogEventLevel.Error, LogArea.X11Platform)?.Log(this, e.Message); + _isDbusAvailable = false; + return await dialog.ShowManagedAsync(parent); + } + } + + private async Task ShowNativeFileDialogAsync(FileDialog dialog, Window parent) { var parentWindow = $"x11:{parent.PlatformImpl!.Handle.Handle.ToString("X")}"; ObjectPath objectPath; @@ -30,38 +66,42 @@ namespace Avalonia.FreeDesktop { case OpenFileDialog openFileDialog: options.Add("multiple", openFileDialog.AllowMultiple); - objectPath = await _fileChooser.OpenFileAsync(parentWindow, openFileDialog.Title ?? string.Empty, options); + objectPath = await _fileChooser!.OpenFileAsync(parentWindow, openFileDialog.Title ?? string.Empty, options); break; case SaveFileDialog saveFileDialog: if (saveFileDialog.InitialFileName is not null) options.Add("current_name", saveFileDialog.InitialFileName); if (saveFileDialog.Directory is not null) options.Add("current_folder", Encoding.UTF8.GetBytes(saveFileDialog.Directory)); - objectPath = await _fileChooser.SaveFileAsync(parentWindow, saveFileDialog.Title ?? string.Empty, options); + objectPath = await _fileChooser!.SaveFileAsync(parentWindow, saveFileDialog.Title ?? string.Empty, options); break; } - var request = DBusHelper.Connection.CreateProxy("org.freedesktop.portal.Request", objectPath); - var tsc = new TaskCompletionSource(); - using var disposable = await request.WatchResponseAsync(x => tsc.TrySetResult(x.results["uris"] as string[])); + var request = DBusHelper.Connection!.CreateProxy("org.freedesktop.portal.Request", objectPath); + var tsc = new TaskCompletionSource(); + using var disposable = await request.WatchResponseAsync(x => tsc.SetResult(x.results["uris"] as string[]), tsc.SetException); var uris = await tsc.Task; + if (uris is null) + return null; for (var i = 0; i < uris.Length; i++) uris[i] = new Uri(uris[i]).AbsolutePath; return uris; } - public async Task ShowFolderDialogAsync(OpenFolderDialog dialog, Window parent) + private async Task ShowNativeFolderDialogAsync(OpenFolderDialog dialog, Window parent) { var parentWindow = $"x11:{parent.PlatformImpl!.Handle.Handle.ToString("X")}"; var options = new Dictionary { { "directory", true } }; - var objectPath = await _fileChooser.OpenFileAsync(parentWindow, dialog.Title ?? string.Empty, options); - var request = DBusHelper.Connection.CreateProxy("org.freedesktop.portal.Request", objectPath); - var tsc = new TaskCompletionSource(); - using var disposable = await request.WatchResponseAsync(x => tsc.TrySetResult(x.results["uris"] as string[])); + var objectPath = await _fileChooser!.OpenFileAsync(parentWindow, dialog.Title ?? string.Empty, options); + var request = DBusHelper.Connection!.CreateProxy("org.freedesktop.portal.Request", objectPath); + var tsc = new TaskCompletionSource(); + using var disposable = await request.WatchResponseAsync(x => tsc.SetResult(x.results["uris"] as string[]), tsc.SetException); var uris = await tsc.Task; + if (uris is null) + return null; return uris.Length != 1 ? string.Empty : new Uri(uris[0]).AbsolutePath; } @@ -71,7 +111,7 @@ namespace Avalonia.FreeDesktop for (var i = 0; i < filters.Length; i++) { var extensions = dialog.Filters[i].Extensions.Select(static x => (0u, x)).ToArray(); - filters[i] = (dialog.Filters[i].Name, extensions); + filters[i] = (dialog.Filters[i].Name ?? string.Empty, extensions); } return filters; diff --git a/src/Avalonia.X11/X11Platform.cs b/src/Avalonia.X11/X11Platform.cs index ec3f29c806..8765299d1d 100644 --- a/src/Avalonia.X11/X11Platform.cs +++ b/src/Avalonia.X11/X11Platform.cs @@ -15,7 +15,6 @@ using Avalonia.Platform; using Avalonia.Rendering; using Avalonia.X11; using Avalonia.X11.Glx; -using Avalonia.X11.NativeDialogs; using static Avalonia.X11.XLib; namespace Avalonia.X11 @@ -80,7 +79,7 @@ namespace Avalonia.X11 .Bind().ToConstant(new X11Clipboard(this)) .Bind().ToConstant(new PlatformSettingsStub()) .Bind().ToConstant(new X11IconLoader(Info)) - .Bind().ToConstant(new GtkSystemDialog()) + .Bind().ToConstant(new DBusSystemDialog()) .Bind().ToConstant(new LinuxMountedVolumeInfoProvider()) .Bind().ToConstant(new X11PlatformLifetimeEvents(this)); @@ -209,10 +208,10 @@ namespace Avalonia public bool OverlayPopups { get; set; } /// - /// Enables global menu support on Linux desktop environments where it's supported (e. g. XFCE and MATE with plugin, KDE, etc). - /// The default value is false. + /// Enables native file dialogs as well as global menu support on Linux desktop environments where it's supported (e. g. XFCE and MATE with plugin, KDE, etc). + /// The default value is true. /// - public bool UseDBusMenu { get; set; } + public bool UseDBusMenu { get; set; } = true; /// /// Deferred renderer would be used when set to true. Immediate renderer when set to false. The default value is true. From 3c3fc4521ea31cf9c6562065a1552b4e2932e1c1 Mon Sep 17 00:00:00 2001 From: affederaffe <68356204+affederaffe@users.noreply.github.com> Date: Mon, 30 May 2022 20:20:27 +0200 Subject: [PATCH 04/11] Clean up DBusSystemDialog.cs + add factory method --- src/Avalonia.Dialogs/Avalonia.Dialogs.csproj | 4 ++ .../ManagedFileDialogExtensions.cs | 8 +-- src/Avalonia.FreeDesktop/DBusSystemDialog.cs | 50 ++++++------------- src/Avalonia.X11/X11Platform.cs | 3 +- 4 files changed, 23 insertions(+), 42 deletions(-) diff --git a/src/Avalonia.Dialogs/Avalonia.Dialogs.csproj b/src/Avalonia.Dialogs/Avalonia.Dialogs.csproj index a311efdfb0..80159c82d7 100644 --- a/src/Avalonia.Dialogs/Avalonia.Dialogs.csproj +++ b/src/Avalonia.Dialogs/Avalonia.Dialogs.csproj @@ -14,6 +14,10 @@ + + + + diff --git a/src/Avalonia.Dialogs/ManagedFileDialogExtensions.cs b/src/Avalonia.Dialogs/ManagedFileDialogExtensions.cs index effdc847a0..e4025453c4 100644 --- a/src/Avalonia.Dialogs/ManagedFileDialogExtensions.cs +++ b/src/Avalonia.Dialogs/ManagedFileDialogExtensions.cs @@ -8,7 +8,7 @@ namespace Avalonia.Dialogs { public static class ManagedFileDialogExtensions { - private class ManagedSystemDialogImpl : ISystemDialogImpl where T : Window, new() + internal class ManagedSystemDialogImpl : ISystemDialogImpl where T : Window, new() { async Task Show(SystemDialog d, Window parent, ManagedFileDialogOptions options = null) { @@ -139,12 +139,6 @@ namespace Avalonia.Dialogs return builder; } - public static Task ShowManagedAsync(this FileDialog dialog, Window parent) - => new ManagedSystemDialogImpl().ShowFileDialogAsync(dialog, parent); - - public static Task ShowManagedAsync(this OpenFolderDialog dialog, Window parent) - => new ManagedSystemDialogImpl().ShowFolderDialogAsync(dialog, parent); - public static Task ShowManagedAsync(this OpenFileDialog dialog, Window parent, ManagedFileDialogOptions options = null) => ShowManagedAsync(dialog, parent, options); diff --git a/src/Avalonia.FreeDesktop/DBusSystemDialog.cs b/src/Avalonia.FreeDesktop/DBusSystemDialog.cs index 7bc287ea28..d1905a4569 100644 --- a/src/Avalonia.FreeDesktop/DBusSystemDialog.cs +++ b/src/Avalonia.FreeDesktop/DBusSystemDialog.cs @@ -5,7 +5,6 @@ using System.Text; using System.Threading.Tasks; using Avalonia.Controls; using Avalonia.Controls.Platform; -using Avalonia.Dialogs; using Avalonia.Logging; using Tmds.DBus; @@ -13,48 +12,31 @@ namespace Avalonia.FreeDesktop { internal class DBusSystemDialog : ISystemDialogImpl { - private readonly IFileChooser? _fileChooser; - private bool _isDbusAvailable; + private readonly IFileChooser _fileChooser; - internal DBusSystemDialog() + internal static DBusSystemDialog? TryCreate() { - _fileChooser = DBusHelper.Connection?.CreateProxy("org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop"); - _isDbusAvailable = _fileChooser is not null; - } - - public async Task ShowFileDialogAsync(FileDialog dialog, Window parent) - { - if (!_isDbusAvailable) - return await dialog.ShowManagedAsync(parent); + var fileChooser = DBusHelper.Connection?.CreateProxy("org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop"); + if (fileChooser is null) + return null; try { - return await ShowNativeFileDialogAsync(dialog, parent); + fileChooser.GetVersionAsync().GetAwaiter().GetResult(); + return new DBusSystemDialog(fileChooser); } catch (Exception e) { - Logger.TryGet(LogEventLevel.Error, LogArea.X11Platform)?.Log(this, e.Message); - _isDbusAvailable = false; - return await dialog.ShowManagedAsync(parent); + Logger.TryGet(LogEventLevel.Error, LogArea.X11Platform)?.Log(null, $"Unable to connect to org.freedesktop.portal.Desktop: {e.Message}"); + return null; } } - public async Task ShowFolderDialogAsync(OpenFolderDialog dialog, Window parent) + private DBusSystemDialog(IFileChooser fileChooser) { - if (!_isDbusAvailable) - return await dialog.ShowManagedAsync(parent); - try - { - return await ShowNativeFolderDialogAsync(dialog, parent); - } - catch (Exception e) - { - Logger.TryGet(LogEventLevel.Error, LogArea.X11Platform)?.Log(this, e.Message); - _isDbusAvailable = false; - return await dialog.ShowManagedAsync(parent); - } + _fileChooser = fileChooser; } - private async Task ShowNativeFileDialogAsync(FileDialog dialog, Window parent) + public async Task ShowFileDialogAsync(FileDialog dialog, Window parent) { var parentWindow = $"x11:{parent.PlatformImpl!.Handle.Handle.ToString("X")}"; ObjectPath objectPath; @@ -66,14 +48,14 @@ namespace Avalonia.FreeDesktop { case OpenFileDialog openFileDialog: options.Add("multiple", openFileDialog.AllowMultiple); - objectPath = await _fileChooser!.OpenFileAsync(parentWindow, openFileDialog.Title ?? string.Empty, options); + objectPath = await _fileChooser.OpenFileAsync(parentWindow, openFileDialog.Title ?? string.Empty, options); break; case SaveFileDialog saveFileDialog: if (saveFileDialog.InitialFileName is not null) options.Add("current_name", saveFileDialog.InitialFileName); if (saveFileDialog.Directory is not null) options.Add("current_folder", Encoding.UTF8.GetBytes(saveFileDialog.Directory)); - objectPath = await _fileChooser!.SaveFileAsync(parentWindow, saveFileDialog.Title ?? string.Empty, options); + objectPath = await _fileChooser.SaveFileAsync(parentWindow, saveFileDialog.Title ?? string.Empty, options); break; } @@ -88,14 +70,14 @@ namespace Avalonia.FreeDesktop return uris; } - private async Task ShowNativeFolderDialogAsync(OpenFolderDialog dialog, Window parent) + public async Task ShowFolderDialogAsync(OpenFolderDialog dialog, Window parent) { var parentWindow = $"x11:{parent.PlatformImpl!.Handle.Handle.ToString("X")}"; var options = new Dictionary { { "directory", true } }; - var objectPath = await _fileChooser!.OpenFileAsync(parentWindow, dialog.Title ?? string.Empty, options); + var objectPath = await _fileChooser.OpenFileAsync(parentWindow, dialog.Title ?? string.Empty, options); var request = DBusHelper.Connection!.CreateProxy("org.freedesktop.portal.Request", objectPath); var tsc = new TaskCompletionSource(); using var disposable = await request.WatchResponseAsync(x => tsc.SetResult(x.results["uris"] as string[]), tsc.SetException); diff --git a/src/Avalonia.X11/X11Platform.cs b/src/Avalonia.X11/X11Platform.cs index 8765299d1d..fa7ae69759 100644 --- a/src/Avalonia.X11/X11Platform.cs +++ b/src/Avalonia.X11/X11Platform.cs @@ -5,6 +5,7 @@ using System.Reflection; using System.Runtime.InteropServices; using Avalonia.Controls; using Avalonia.Controls.Platform; +using Avalonia.Dialogs; using Avalonia.FreeDesktop; using Avalonia.FreeDesktop.DBusIme; using Avalonia.Input; @@ -79,7 +80,7 @@ namespace Avalonia.X11 .Bind().ToConstant(new X11Clipboard(this)) .Bind().ToConstant(new PlatformSettingsStub()) .Bind().ToConstant(new X11IconLoader(Info)) - .Bind().ToConstant(new DBusSystemDialog()) + .Bind().ToConstant(DBusSystemDialog.TryCreate() as ISystemDialogImpl ?? new ManagedFileDialogExtensions.ManagedSystemDialogImpl()) .Bind().ToConstant(new LinuxMountedVolumeInfoProvider()) .Bind().ToConstant(new X11PlatformLifetimeEvents(this)); From 9e03c05713e1817243f580943c4ed178ec7b42e3 Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Thu, 2 Jun 2022 23:03:31 +0100 Subject: [PATCH 05/11] osx: fix crash when modal dialog is opened over fullscreen parent. --- native/Avalonia.Native/src/OSX/WindowImpl.mm | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/native/Avalonia.Native/src/OSX/WindowImpl.mm b/native/Avalonia.Native/src/OSX/WindowImpl.mm index e6e704e149..cae1c09513 100644 --- a/native/Avalonia.Native/src/OSX/WindowImpl.mm +++ b/native/Avalonia.Native/src/OSX/WindowImpl.mm @@ -564,6 +564,11 @@ bool WindowImpl::IsDialog() { NSWindowStyleMask WindowImpl::GetStyle() { unsigned long s = NSWindowStyleMaskBorderless; + + if(_actualWindowState == FullScreen) + { + s |= NSWindowStyleMaskFullScreen; + } switch (_decorations) { case SystemDecorationsNone: From a6d1e74b4fa1d14e7c9e2047865ec253558b0eca Mon Sep 17 00:00:00 2001 From: Oxc3 Date: Thu, 2 Jun 2022 16:22:44 -0700 Subject: [PATCH 06/11] RazorViewTopLevel will now test keys first on Code, then Key as a backup This is an android specific hack. the chrome browser event sends an empty string on Code property so this will test both Code and Key properties to match to an Avalonia key. If the user is on a sane system then Code will be used to Key match, else it will failover to try and match with Key. --- src/Web/Avalonia.Web.Blazor/AvaloniaView.razor.cs | 4 ++-- src/Web/Avalonia.Web.Blazor/RazorViewTopLevelImpl.cs | 11 +++++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Web/Avalonia.Web.Blazor/AvaloniaView.razor.cs b/src/Web/Avalonia.Web.Blazor/AvaloniaView.razor.cs index 1f411d0cee..7531dbf681 100644 --- a/src/Web/Avalonia.Web.Blazor/AvaloniaView.razor.cs +++ b/src/Web/Avalonia.Web.Blazor/AvaloniaView.razor.cs @@ -233,12 +233,12 @@ namespace Avalonia.Web.Blazor private void OnKeyDown(KeyboardEventArgs e) { - _topLevelImpl.RawKeyboardEvent(RawKeyEventType.KeyDown, e.Code, GetModifiers(e)); + _topLevelImpl.RawKeyboardEvent(RawKeyEventType.KeyDown, e.Code, e.Key, GetModifiers(e)); } private void OnKeyUp(KeyboardEventArgs e) { - _topLevelImpl.RawKeyboardEvent(RawKeyEventType.KeyUp, e.Code, GetModifiers(e)); + _topLevelImpl.RawKeyboardEvent(RawKeyEventType.KeyUp, e.Code, e.Key, GetModifiers(e)); } private void OnInput(ChangeEventArgs e) diff --git a/src/Web/Avalonia.Web.Blazor/RazorViewTopLevelImpl.cs b/src/Web/Avalonia.Web.Blazor/RazorViewTopLevelImpl.cs index 50070e6e2c..a8a1a970dc 100644 --- a/src/Web/Avalonia.Web.Blazor/RazorViewTopLevelImpl.cs +++ b/src/Web/Avalonia.Web.Blazor/RazorViewTopLevelImpl.cs @@ -91,9 +91,16 @@ namespace Avalonia.Web.Blazor } } - public void RawKeyboardEvent(RawKeyEventType type, string key, RawInputModifiers modifiers) + public void RawKeyboardEvent(RawKeyEventType type, string code, string key, RawInputModifiers modifiers) { - if (Keycodes.KeyCodes.TryGetValue(key, out var avkey)) + if (Keycodes.KeyCodes.TryGetValue(code, out var avkey)) + { + if (_inputRoot is { }) + { + Input?.Invoke(new RawKeyEventArgs(KeyboardDevice, Timestamp, _inputRoot, type, avkey, modifiers)); + } + } + else if (Keycodes.KeyCodes.TryGetValue(key, out avkey)) { if (_inputRoot is { }) { From bd0d81d6afea1554a8f79d1a432d86de9838efe5 Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Fri, 3 Jun 2022 11:08:32 +0100 Subject: [PATCH 07/11] fix logic for deciding if chrome buttons should be shown or not in extended mode. --- native/Avalonia.Native/src/OSX/WindowImpl.mm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/native/Avalonia.Native/src/OSX/WindowImpl.mm b/native/Avalonia.Native/src/OSX/WindowImpl.mm index cae1c09513..ef2a91ef14 100644 --- a/native/Avalonia.Native/src/OSX/WindowImpl.mm +++ b/native/Avalonia.Native/src/OSX/WindowImpl.mm @@ -32,7 +32,7 @@ void WindowImpl::HideOrShowTrafficLights() { } bool wantsChrome = (_extendClientHints & AvnSystemChrome) || (_extendClientHints & AvnPreferSystemChrome); - bool hasTrafficLights = _isClientAreaExtended ? !wantsChrome : _decorations != SystemDecorationsFull; + bool hasTrafficLights = _isClientAreaExtended ? wantsChrome : _decorations != SystemDecorationsFull; [[Window standardWindowButton:NSWindowCloseButton] setHidden:hasTrafficLights]; [[Window standardWindowButton:NSWindowMiniaturizeButton] setHidden:hasTrafficLights]; From 6a848a389fb2ca600ed4173d5f121b14b97e24cb Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Fri, 3 Jun 2022 11:34:20 +0100 Subject: [PATCH 08/11] dont re-initialise nswindow on show. --- native/Avalonia.Native/src/OSX/WindowBaseImpl.mm | 2 -- 1 file changed, 2 deletions(-) diff --git a/native/Avalonia.Native/src/OSX/WindowBaseImpl.mm b/native/Avalonia.Native/src/OSX/WindowBaseImpl.mm index c420736b46..bf221047f9 100644 --- a/native/Avalonia.Native/src/OSX/WindowBaseImpl.mm +++ b/native/Avalonia.Native/src/OSX/WindowBaseImpl.mm @@ -90,8 +90,6 @@ HRESULT WindowBaseImpl::Show(bool activate, bool isDialog) { START_COM_CALL; @autoreleasepool { - InitialiseNSWindow(); - if(hasPosition) { SetPosition(lastPositionSet); From 809f459233f018111cacb39e2564ff242b612b4a Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Fri, 3 Jun 2022 11:39:03 +0100 Subject: [PATCH 09/11] fix osx thick titlebar setting. --- samples/ControlCatalog/MainWindow.xaml.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/samples/ControlCatalog/MainWindow.xaml.cs b/samples/ControlCatalog/MainWindow.xaml.cs index 20591103b7..8ee8b2ebd4 100644 --- a/samples/ControlCatalog/MainWindow.xaml.cs +++ b/samples/ControlCatalog/MainWindow.xaml.cs @@ -29,8 +29,6 @@ namespace ControlCatalog DataContext = new MainWindowViewModel(_notificationArea); _recentMenu = ((NativeMenu.GetMenu(this).Items[0] as NativeMenuItem).Menu.Items[2] as NativeMenuItem).Menu; - - ExtendClientAreaChromeHints = Avalonia.Platform.ExtendClientAreaChromeHints.OSXThickTitleBar; } public static string MenuQuitHeader => RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? "Quit Avalonia" : "E_xit"; From 11595184d5e87520073dbba37b0afd8cce1d3a2e Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Fri, 3 Jun 2022 11:39:14 +0100 Subject: [PATCH 10/11] fix traffic light setting. --- native/Avalonia.Native/src/OSX/WindowImpl.mm | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/native/Avalonia.Native/src/OSX/WindowImpl.mm b/native/Avalonia.Native/src/OSX/WindowImpl.mm index ef2a91ef14..f49b676ce6 100644 --- a/native/Avalonia.Native/src/OSX/WindowImpl.mm +++ b/native/Avalonia.Native/src/OSX/WindowImpl.mm @@ -32,11 +32,11 @@ void WindowImpl::HideOrShowTrafficLights() { } bool wantsChrome = (_extendClientHints & AvnSystemChrome) || (_extendClientHints & AvnPreferSystemChrome); - bool hasTrafficLights = _isClientAreaExtended ? wantsChrome : _decorations != SystemDecorationsFull; + bool hasTrafficLights = _isClientAreaExtended ? wantsChrome : _decorations == SystemDecorationsFull; - [[Window standardWindowButton:NSWindowCloseButton] setHidden:hasTrafficLights]; - [[Window standardWindowButton:NSWindowMiniaturizeButton] setHidden:hasTrafficLights]; - [[Window standardWindowButton:NSWindowZoomButton] setHidden:hasTrafficLights]; + [[Window standardWindowButton:NSWindowCloseButton] setHidden:!hasTrafficLights]; + [[Window standardWindowButton:NSWindowMiniaturizeButton] setHidden:!hasTrafficLights]; + [[Window standardWindowButton:NSWindowZoomButton] setHidden:!hasTrafficLights]; } void WindowImpl::OnInitialiseNSWindow(){ From 6e916c21e5d5effba473013086b111123c38cf26 Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Fri, 3 Jun 2022 11:46:30 +0100 Subject: [PATCH 11/11] fix transition to fullscreen mode. --- native/Avalonia.Native/src/OSX/AvnWindow.mm | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/native/Avalonia.Native/src/OSX/AvnWindow.mm b/native/Avalonia.Native/src/OSX/AvnWindow.mm index 9fc7ec4fec..f6dcd0cabf 100644 --- a/native/Avalonia.Native/src/OSX/AvnWindow.mm +++ b/native/Avalonia.Native/src/OSX/AvnWindow.mm @@ -33,6 +33,7 @@ bool _isEnabled; bool _canBecomeKeyWindow; bool _isExtended; + bool _isTransitioningToFullScreen; AvnMenu* _menu; } @@ -175,6 +176,7 @@ [self setBackgroundColor: [NSColor clearColor]]; _isExtended = false; + _isTransitioningToFullScreen = false; if(self.isDialog) { @@ -349,6 +351,7 @@ - (void)windowWillEnterFullScreen:(NSNotification *_Nonnull)notification { + _isTransitioningToFullScreen = true; auto parent = dynamic_cast(_parent.operator->()); if(parent != nullptr) @@ -359,6 +362,7 @@ - (void)windowDidEnterFullScreen:(NSNotification *_Nonnull)notification { + _isTransitioningToFullScreen = false; auto parent = dynamic_cast(_parent.operator->()); if(parent != nullptr) @@ -441,7 +445,10 @@ _parent->BaseEvents->RawMouseEvent(NonClientLeftButtonDown, static_cast([event timestamp] * 1000), AvnInputModifiersNone, point, delta); } - _parent->BringToFront(); + if(!_isTransitioningToFullScreen) + { + _parent->BringToFront(); + } } break;