From 6e2df67df1841a6fe39b3df921db74c8a34f1d88 Mon Sep 17 00:00:00 2001 From: Lehonti Ramos <17771375+Lehonti@users.noreply.github.com> Date: Thu, 21 Nov 2024 04:43:36 +0100 Subject: [PATCH] Migrated open file dialogs in `LayerActions` and `AddinManagerDialog` to `FileDialog` (#1152) * Migrated handler `LayerActions` to new file dialog * Migrated install dialog in `AddinManagerDialog` to `FileDialog` --- Pinta.Core/Actions/LayerActions.cs | 54 +++++++++++++------------- Pinta.Gui.Addins/AddinManagerDialog.cs | 29 +++++++------- 2 files changed, 41 insertions(+), 42 deletions(-) diff --git a/Pinta.Core/Actions/LayerActions.cs b/Pinta.Core/Actions/LayerActions.cs index 0484db3a6b..801146a8ee 100644 --- a/Pinta.Core/Actions/LayerActions.cs +++ b/Pinta.Core/Actions/LayerActions.cs @@ -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. @@ -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); diff --git a/Pinta.Gui.Addins/AddinManagerDialog.cs b/Pinta.Gui.Addins/AddinManagerDialog.cs index 20a49dcdf1..bf5e3a0eb1 100644 --- a/Pinta.Gui.Addins/AddinManagerDialog.cs +++ b/Pinta.Gui.Addins/AddinManagerDialog.cs @@ -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 files = - dialog.GetFileList () + choices .Select (f => f.GetPath () ?? string.Empty) .Where (f => !string.IsNullOrEmpty (f)) .ToArray ();