Skip to content

Commit

Permalink
Migrated open file dialogs in LayerActions and AddinManagerDialog
Browse files Browse the repository at this point in the history
… to `FileDialog` (#1152)

* Migrated handler `LayerActions` to new file dialog

* Migrated install dialog in `AddinManagerDialog` to `FileDialog`
  • Loading branch information
Lehonti authored Nov 21, 2024
1 parent de8f4f9 commit 6e2df67
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 42 deletions.
54 changes: 28 additions & 26 deletions Pinta.Core/Actions/LayerActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,18 +160,13 @@ private void EnableOrDisableLayerActions (object? sender, EventArgs e)
MoveLayerUp.Sensitive = false;
}

private async void HandlePintaCoreActionsLayersImportFromFileActivated (object sender, EventArgs e)
private Gtk.FileFilter CreateImagesFileFilter ()
{
Document doc = workspace.ActiveDocument;

tools.Commit ();

// Add image files filter
using Gtk.FileFilter ff = Gtk.FileFilter.New ();
Gtk.FileFilter imagesFilter = Gtk.FileFilter.New ();
foreach (var format in image_formats.Formats) {
if (format.IsWriteOnly ()) continue;
foreach (string ext in format.Extensions)
ff.AddPattern ($"*.{ext}");
imagesFilter.AddPattern ($"*.{ext}");
}

// On Unix-like systems, file extensions are often considered optional.
Expand All @@ -181,37 +176,44 @@ private async void HandlePintaCoreActionsLayersImportFromFileActivated (object s
if (SystemManager.GetOperatingSystem () != OS.Windows)
foreach (var format in image_formats.Formats)
foreach (var mime in format.Mimes)
ff.AddMimeType (mime);
imagesFilter.AddMimeType (mime);

ff.Name = Translations.GetString ("Image files");
imagesFilter.Name = Translations.GetString ("Image files");

using Gtk.FileChooserNative fcd = Gtk.FileChooserNative.New (
Translations.GetString ("Open Image File"),
chrome.MainWindow,
Gtk.FileChooserAction.Open,
Translations.GetString ("Open"),
Translations.GetString ("Cancel"));
return imagesFilter;
}

if (recent_files.GetDialogDirectory () is Gio.File dir && dir.QueryExists (null))
fcd.SetCurrentFolder (dir);
private async void HandlePintaCoreActionsLayersImportFromFileActivated (object sender, EventArgs e)
{
Document doc = workspace.ActiveDocument;

tools.Commit ();

fcd.AddFilter (ff);
// Add image files filter
using Gtk.FileFilter imagesFilter = CreateImagesFileFilter ();

using Gio.ListStore fileFilters = Gio.ListStore.New (Gtk.FileFilter.GetGType ());
fileFilters.Append (imagesFilter);

using Gtk.FileDialog fileDialog = Gtk.FileDialog.New ();
fileDialog.SetTitle (Translations.GetString ("Open Image File"));
fileDialog.SetFilters (fileFilters);
if (recent_files.GetDialogDirectory () is Gio.File dir && dir.QueryExists (null))
fileDialog.SetInitialFolder (dir);

Gtk.ResponseType response = await fcd.ShowAsync ();
Gio.File? choice = await fileDialog.OpenFileAsync (chrome.MainWindow);

if (response != Gtk.ResponseType.Accept)
return;
if (choice is null) return;

Gio.File file = fcd.GetFile ()!;
Gio.File? directory = choice.GetParent ();

Gio.File? directory = file.GetParent ();
if (directory is not null)
recent_files.LastDialogDirectory = directory;

// Open the image and add it to the layers
UserLayer layer = doc.Layers.AddNewLayer (file.GetDisplayName ());
UserLayer layer = doc.Layers.AddNewLayer (choice.GetDisplayName ());

using (Gio.FileInputStream fs = file.Read (null)) {
using (Gio.FileInputStream fs = choice.Read (null)) {
try {
using GdkPixbuf.Pixbuf bg = GdkPixbuf.Pixbuf.NewFromStream (fs, cancellable: null)!; // NRT: only nullable when an error is thrown
using Cairo.Context context = new (layer.Surface);
Expand Down
29 changes: 13 additions & 16 deletions Pinta.Gui.Addins/AddinManagerDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,24 +246,21 @@ private async void HandleInstallFromFileClicked ()
using Gtk.FileFilter mpackFilter = CreateMpackFilter ();
using Gtk.FileFilter catchAllFilter = CreateCatchAllFilter ();

using Gtk.FileChooserNative dialog = Gtk.FileChooserNative.New (
Translations.GetString ("Install Extension Package"),
this,
Gtk.FileChooserAction.Open,
Translations.GetString ("Open"),
Translations.GetString ("Cancel"));
dialog.Modal = true;
dialog.SelectMultiple = true;
dialog.AddFilter (mpackFilter);
dialog.AddFilter (catchAllFilter);

Gtk.ResponseType response = await dialog.ShowAsync ();

if (response != Gtk.ResponseType.Accept)
return;
using Gio.ListStore fileFilters = Gio.ListStore.New (Gtk.FileFilter.GetGType ());
fileFilters.Append (mpackFilter);
fileFilters.Append (catchAllFilter);

using Gtk.FileDialog fileDialog = Gtk.FileDialog.New ();
fileDialog.SetTitle (Translations.GetString ("Install Extension Package"));
fileDialog.SetFilters (fileFilters);
fileDialog.Modal = true;

var choices = await fileDialog.OpenFilesAsync (this);

if (choices is null) return;

IReadOnlyList<string> files =
dialog.GetFileList ()
choices
.Select (f => f.GetPath () ?? string.Empty)
.Where (f => !string.IsNullOrEmpty (f))
.ToArray ();
Expand Down

0 comments on commit 6e2df67

Please sign in to comment.