Use native file chooser dialogs (GTK client) (#6545)

This commit is contained in:
Mike Gelfand 2024-01-22 00:24:28 +00:00 committed by GitHub
parent 32ef92e7a7
commit 9f77ef9c7a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 28 additions and 30 deletions

View File

@ -1516,7 +1516,7 @@ void Application::Impl::actions_handler(Glib::ustring const& action_name)
else if (action_name == "open-torrent")
{
auto w = std::shared_ptr<TorrentFileChooserDialog>(TorrentFileChooserDialog::create(*wind_, core_));
gtr_window_on_close(*w, [w]() mutable { w.reset(); });
w->signal_response().connect([w](int /*response*/) mutable { w.reset(); });
w->show();
}
else if (action_name == "show-stats")

View File

@ -24,7 +24,7 @@
#include <glibmm/variant.h>
#include <gtkmm/cellrenderertext.h>
#include <gtkmm/combobox.h>
#include <gtkmm/filechooserdialog.h>
#include <gtkmm/filechoosernative.h>
#include <gtkmm/liststore.h>
#include <gtkmm/messagedialog.h>
#include <gtkmm/treemodel.h>
@ -71,8 +71,8 @@ private:
bool onRefresh();
void onSaveRequest();
void onSaveDialogResponse(std::shared_ptr<Gtk::FileChooserDialog>& d, int response);
void doSave(Gtk::Window& parent, Glib::ustring const& filename);
void onSaveDialogResponse(Glib::RefPtr<Gtk::FileChooserNative>& d, int response);
void doSave(std::string const& filename);
void onClearRequest();
void onPauseToggled(Gio::SimpleAction& action);
@ -200,13 +200,13 @@ Glib::ustring gtr_asctime(time_t t)
} // namespace
void MessageLogWindow::Impl::doSave(Gtk::Window& parent, Glib::ustring const& filename)
void MessageLogWindow::Impl::doSave(std::string const& filename)
{
try
{
auto stream = std::ofstream();
stream.exceptions(std::ios_base::failbit | std::ios_base::badbit);
stream.open(Glib::locale_from_utf8(filename), std::ios_base::trunc);
stream.open(filename, std::ios_base::trunc);
for (auto const& row : store_->children())
{
@ -222,10 +222,10 @@ void MessageLogWindow::Impl::doSave(Gtk::Window& parent, Glib::ustring const& fi
catch (std::ios_base::failure const& e)
{
auto w = std::make_shared<Gtk::MessageDialog>(
parent,
window_,
fmt::format(
_("Couldn't save '{path}': {error} ({error_code})"),
fmt::arg("path", filename),
fmt::arg("path", Glib::filename_to_utf8(filename)),
fmt::arg("error", e.code().message()),
fmt::arg("error_code", e.code().value())),
false,
@ -237,22 +237,21 @@ void MessageLogWindow::Impl::doSave(Gtk::Window& parent, Glib::ustring const& fi
}
}
void MessageLogWindow::Impl::onSaveDialogResponse(std::shared_ptr<Gtk::FileChooserDialog>& d, int response)
void MessageLogWindow::Impl::onSaveDialogResponse(Glib::RefPtr<Gtk::FileChooserNative>& d, int response)
{
if (response == TR_GTK_RESPONSE_TYPE(ACCEPT))
{
doSave(*d, d->get_file()->get_path());
}
auto const filename = response == TR_GTK_RESPONSE_TYPE(ACCEPT) ? d->get_file()->get_path() : std::string();
d.reset();
if (!filename.empty())
{
doSave(filename);
}
}
void MessageLogWindow::Impl::onSaveRequest()
{
auto d = std::make_shared<Gtk::FileChooserDialog>(window_, _("Save Log"), TR_GTK_FILE_CHOOSER_ACTION(SAVE));
d->add_button(_("_Cancel"), TR_GTK_RESPONSE_TYPE(CANCEL));
d->add_button(_("_Save"), TR_GTK_RESPONSE_TYPE(ACCEPT));
auto d = Gtk::FileChooserNative::create(_("Save Log"), window_, TR_GTK_FILE_CHOOSER_ACTION(SAVE), _("_Save"), _("_Cancel"));
d->signal_response().connect([this, d](int response) mutable { onSaveDialogResponse(d, response); });
d->show();
}

View File

@ -353,8 +353,6 @@ void TorrentFileChooserDialog::onOpenDialogResponse(int response, Glib::RefPtr<S
core->add_files(files, do_start, do_prompt, do_notify);
}
close();
}
std::unique_ptr<TorrentFileChooserDialog> TorrentFileChooserDialog::create(
@ -365,13 +363,10 @@ std::unique_ptr<TorrentFileChooserDialog> TorrentFileChooserDialog::create(
}
TorrentFileChooserDialog::TorrentFileChooserDialog(Gtk::Window& parent, Glib::RefPtr<Session> const& core)
: Gtk::FileChooserDialog(parent, _("Open a Torrent"), TR_GTK_FILE_CHOOSER_ACTION(OPEN))
: Gtk::FileChooserNative(_("Open a Torrent"), parent, TR_GTK_FILE_CHOOSER_ACTION(OPEN), _("_Open"), _("_Cancel"))
{
set_modal(true);
add_button(_("_Cancel"), TR_GTK_RESPONSE_TYPE(CANCEL));
add_button(_("_Open"), TR_GTK_RESPONSE_TYPE(ACCEPT));
set_select_multiple(true);
addTorrentFilters(this);
signal_response().connect([this, core](int response) { onOpenDialogResponse(response, core); });

View File

@ -11,7 +11,7 @@
#include <gtkmm/builder.h>
#include <gtkmm/dialog.h>
#include <gtkmm/entry.h>
#include <gtkmm/filechooserdialog.h>
#include <gtkmm/filechoosernative.h>
#include <gtkmm/window.h>
#include <memory>
@ -38,7 +38,7 @@ private:
void onOpenURLResponse(int response, Gtk::Entry const& entry, Glib::RefPtr<Session> const& core);
};
class TorrentFileChooserDialog : public Gtk::FileChooserDialog
class TorrentFileChooserDialog : public Gtk::FileChooserNative
{
public:
~TorrentFileChooserDialog() override = default;

View File

@ -5,6 +5,7 @@
#include "PathButton.h"
#include "GtkCompat.h"
#include "Utils.h"
#include <giomm/file.h>
@ -13,7 +14,8 @@
#if GTKMM_CHECK_VERSION(4, 0, 0)
#include <glibmm/error.h>
#include <glibmm/property.h>
#include <gtkmm/filechooserdialog.h>
#include <gtkmm/dialog.h>
#include <gtkmm/filechoosernative.h>
#include <gtkmm/image.h>
#include <gtkmm/label.h>
#include <gtkmm/separator.h>
@ -144,10 +146,12 @@ void PathButton::Impl::show_dialog()
{
auto const title = title_.get_value();
auto dialog = std::make_shared<Gtk::FileChooserDialog>(!title.empty() ? title : _("Select a File"), action_.get_value());
auto dialog = Gtk::FileChooserNative::create(
!title.empty() ? title : _("Select a File"),
action_.get_value(),
_("_Open"),
_("_Cancel"));
dialog->set_transient_for(gtr_widget_get_window(widget_));
dialog->add_button(_("_Cancel"), Gtk::ResponseType::CANCEL);
dialog->add_button(_("_Open"), Gtk::ResponseType::ACCEPT);
dialog->set_modal(true);
if (!current_file_.empty())
@ -169,7 +173,7 @@ void PathButton::Impl::show_dialog()
dialog->signal_response().connect(
[this, dialog](int response) mutable
{
if (response == Gtk::ResponseType::ACCEPT)
if (response == TR_GTK_RESPONSE_TYPE(ACCEPT))
{
set_filename(dialog->get_file()->get_path());
selection_changed_.emit();