Browse Source

current_folder added to open file / folder picker (#13596)

Co-authored-by: Herman Kirshin <herman.kirshin@jetbrains.com>
pull/13630/head
Herman K 3 years ago
committed by GitHub
parent
commit
0567a6dbe7
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      src/Avalonia.FreeDesktop/Avalonia.FreeDesktop.csproj
  2. 15
      src/Avalonia.FreeDesktop/DBusSystemDialog.cs

2
src/Avalonia.FreeDesktop/Avalonia.FreeDesktop.csproj

@ -13,7 +13,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Tmds.DBus.Protocol" Version="0.15.0" /> <PackageReference Include="Tmds.DBus.Protocol" Version="0.15.0" />
<PackageReference Include="Tmds.DBus.SourceGenerator" Version="0.0.10" PrivateAssets="All" /> <PackageReference Include="Tmds.DBus.SourceGenerator" Version="0.0.11" PrivateAssets="All" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

15
src/Avalonia.FreeDesktop/DBusSystemDialog.cs

@ -20,27 +20,30 @@ namespace Avalonia.FreeDesktop
return null; return null;
var dbusFileChooser = new OrgFreedesktopPortalFileChooser(DBusHelper.Connection, "org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop"); var dbusFileChooser = new OrgFreedesktopPortalFileChooser(DBusHelper.Connection, "org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop");
uint version = 0;
try try
{ {
await dbusFileChooser.GetVersionPropertyAsync(); version = await dbusFileChooser.GetVersionPropertyAsync();
} }
catch catch
{ {
return null; return null;
} }
return new DBusSystemDialog(DBusHelper.Connection, handle, dbusFileChooser); return new DBusSystemDialog(DBusHelper.Connection, handle, dbusFileChooser, version);
} }
private readonly Connection _connection; private readonly Connection _connection;
private readonly OrgFreedesktopPortalFileChooser _fileChooser; private readonly OrgFreedesktopPortalFileChooser _fileChooser;
private readonly IPlatformHandle _handle; private readonly IPlatformHandle _handle;
private readonly uint _version;
private DBusSystemDialog(Connection connection, IPlatformHandle handle, OrgFreedesktopPortalFileChooser fileChooser) private DBusSystemDialog(Connection connection, IPlatformHandle handle, OrgFreedesktopPortalFileChooser fileChooser, uint version)
{ {
_connection = connection; _connection = connection;
_fileChooser = fileChooser; _fileChooser = fileChooser;
_handle = handle; _handle = handle;
_version = version;
} }
public override bool CanOpen => true; public override bool CanOpen => true;
@ -58,6 +61,8 @@ namespace Avalonia.FreeDesktop
if (filters is not null) if (filters is not null)
chooserOptions.Add("filters", filters); chooserOptions.Add("filters", filters);
if (options.SuggestedStartLocation?.TryGetLocalPath() is { } folderPath && _version >= 4)
chooserOptions.Add("current_folder", new DBusVariantItem("ay", new DBusByteArrayItem(Encoding.UTF8.GetBytes(folderPath + "\0"))));
chooserOptions.Add("multiple", new DBusVariantItem("b", new DBusBoolItem(options.AllowMultiple))); chooserOptions.Add("multiple", new DBusVariantItem("b", new DBusBoolItem(options.AllowMultiple)));
objectPath = await _fileChooser.OpenFileAsync(parentWindow, options.Title ?? string.Empty, chooserOptions); objectPath = await _fileChooser.OpenFileAsync(parentWindow, options.Title ?? string.Empty, chooserOptions);
@ -120,7 +125,9 @@ namespace Avalonia.FreeDesktop
{ "directory", new DBusVariantItem("b", new DBusBoolItem(true)) }, { "directory", new DBusVariantItem("b", new DBusBoolItem(true)) },
{ "multiple", new DBusVariantItem("b", new DBusBoolItem(options.AllowMultiple)) } { "multiple", new DBusVariantItem("b", new DBusBoolItem(options.AllowMultiple)) }
}; };
if (options.SuggestedStartLocation?.TryGetLocalPath() is { } folderPath && _version >= 4)
chooserOptions.Add("current_folder", new DBusVariantItem("ay", new DBusByteArrayItem(Encoding.UTF8.GetBytes(folderPath + "\0"))));
var objectPath = await _fileChooser.OpenFileAsync(parentWindow, options.Title ?? string.Empty, chooserOptions); var objectPath = await _fileChooser.OpenFileAsync(parentWindow, options.Title ?? string.Empty, chooserOptions);
var request = new OrgFreedesktopPortalRequest(_connection, "org.freedesktop.portal.Desktop", objectPath); var request = new OrgFreedesktopPortalRequest(_connection, "org.freedesktop.portal.Desktop", objectPath);
var tsc = new TaskCompletionSource<string[]?>(); var tsc = new TaskCompletionSource<string[]?>();

Loading…
Cancel
Save