From ecb10dca8a32a79f00d1c357fc4993feb0b68d1a Mon Sep 17 00:00:00 2001 From: Ilya Fedin Date: Wed, 31 Mar 2021 12:34:17 +0400 Subject: [PATCH] Add option to choose file dialog --- Telegram/Resources/langs/lang.strings | 3 +++ Telegram/Resources/langs/rewrites/en.json | 2 ++ Telegram/Resources/langs/rewrites/ru.json | 2 ++ Telegram/SourceFiles/kotato/json_settings.cpp | 9 +++++++ Telegram/SourceFiles/kotato/settings.cpp | 1 + Telegram/SourceFiles/kotato/settings.h | 3 +++ Telegram/SourceFiles/kotato/settings_menu.cpp | 26 +++++++++++++++++++ .../platform/linux/file_utilities_linux.cpp | 13 ++++++++++ .../platform/linux/file_utilities_linux.h | 12 +++++++++ .../platform/linux/linux_gtk_file_dialog.cpp | 5 ++-- .../platform/linux/linux_xdp_file_dialog.cpp | 9 ++++--- .../platform/mac/file_utilities_mac.h | 9 +++++++ .../platform/platform_file_utilities.h | 4 +++ .../platform/win/file_utilities_win.h | 9 +++++++ 14 files changed, 101 insertions(+), 6 deletions(-) diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings index f860ef842..2354b75ac 100644 --- a/Telegram/Resources/langs/lang.strings +++ b/Telegram/Resources/langs/lang.strings @@ -2754,6 +2754,9 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "ktg_settings_system" = "System"; "ktg_settings_gtk_integration" = "GTK integration"; +"ktg_settings_file_dialog_type" = "File dialog"; +"ktg_file_dialog_type_default" = "Default"; + "ktg_settings_other" = "Other"; "ktg_profile_copy_id" = "Copy ID"; "ktg_profile_bot_id" = "Bot ID"; diff --git a/Telegram/Resources/langs/rewrites/en.json b/Telegram/Resources/langs/rewrites/en.json index ffe2cace7..31949541b 100644 --- a/Telegram/Resources/langs/rewrites/en.json +++ b/Telegram/Resources/langs/rewrites/en.json @@ -68,6 +68,8 @@ "ktg_net_speed_boost_big": "Big", "ktg_settings_system": "System", "ktg_settings_gtk_integration": "GTK integration", + "ktg_settings_file_dialog_type": "File dialog", + "ktg_file_dialog_type_default": "Default", "ktg_settings_other": "Other", "ktg_profile_copy_id": "Copy ID", "ktg_profile_bot_id": "Bot ID", diff --git a/Telegram/Resources/langs/rewrites/ru.json b/Telegram/Resources/langs/rewrites/ru.json index 1316a57a3..84b41ef97 100644 --- a/Telegram/Resources/langs/rewrites/ru.json +++ b/Telegram/Resources/langs/rewrites/ru.json @@ -67,6 +67,8 @@ "ktg_net_speed_boost_big": "Высокое", "ktg_settings_system": "Система", "ktg_settings_gtk_integration": "GTK-интеграция", + "ktg_settings_file_dialog_type": "Файловый диалог", + "ktg_file_dialog_type_default": "По умолчанию", "ktg_settings_other": "Прочие", "ktg_profile_copy_id": "Копировать ID", "ktg_profile_bot_id": "ID бота", diff --git a/Telegram/SourceFiles/kotato/json_settings.cpp b/Telegram/SourceFiles/kotato/json_settings.cpp index 82e784f58..403b46be9 100644 --- a/Telegram/SourceFiles/kotato/json_settings.cpp +++ b/Telegram/SourceFiles/kotato/json_settings.cpp @@ -370,6 +370,7 @@ QByteArray GenerateSettingsJson(bool areDefault = false) { settings.insert(qsl("userpic_corner_type"), cUserpicCornersType()); settings.insert(qsl("always_show_top_userpic"), cShowTopBarUserpic()); settings.insert(qsl("gtk_integration"), cGtkIntegration()); + settings.insert(qsl("file_dialog_type"), int(cFileDialogType())); settings.insert(qsl("disable_tray_counter"), cDisableTrayCounter()); settings.insert(qsl("use_telegram_panel_icon"), cUseTelegramPanelIcon()); settings.insert(qsl("custom_app_icon"), cCustomAppIcon()); @@ -616,6 +617,14 @@ bool Manager::readCustomFile() { cSetGtkIntegration(v); }); + ReadIntOption(settings, "file_dialog_type", [&](auto v) { + const auto typedValue = Platform::FileDialog::ImplementationType(v); + if (typedValue >= Platform::FileDialog::ImplementationType::Default + && typedValue < Platform::FileDialog::ImplementationType::Count) { + cSetFileDialogType(typedValue); + } + }); + ReadBoolOption(settings, "disable_tray_counter", [&](auto v) { cSetDisableTrayCounter(v); }); diff --git a/Telegram/SourceFiles/kotato/settings.cpp b/Telegram/SourceFiles/kotato/settings.cpp index e624add1c..a730f71cd 100644 --- a/Telegram/SourceFiles/kotato/settings.cpp +++ b/Telegram/SourceFiles/kotato/settings.cpp @@ -148,6 +148,7 @@ rpl::producer RecentStickersLimitChanges() { int gUserpicCornersType = 3; bool gShowTopBarUserpic = false; bool gGtkIntegration = false; +Platform::FileDialog::ImplementationType gFileDialogType = Platform::FileDialog::ImplementationType::Default; bool gDisableTrayCounter = Platform::IsLinux(); bool gUseTelegramPanelIcon = false; int gCustomAppIcon = 0; diff --git a/Telegram/SourceFiles/kotato/settings.h b/Telegram/SourceFiles/kotato/settings.h index 3fe662ab7..fe4e9a3da 100644 --- a/Telegram/SourceFiles/kotato/settings.h +++ b/Telegram/SourceFiles/kotato/settings.h @@ -7,6 +7,8 @@ https://github.com/kotatogram/kotatogram-desktop/blob/dev/LEGAL */ #pragma once +#include "platform/platform_file_utilities.h" + #define DeclareReadSetting(Type, Name) extern Type g##Name; \ inline const Type &c##Name() { \ return g##Name; \ @@ -102,6 +104,7 @@ void SetRecentStickersLimit(int limit); DeclareSetting(int, UserpicCornersType); DeclareSetting(bool, ShowTopBarUserpic); DeclareSetting(bool, GtkIntegration); +DeclareSetting(Platform::FileDialog::ImplementationType, FileDialogType); DeclareSetting(bool, DisableTrayCounter); DeclareSetting(bool, UseTelegramPanelIcon); DeclareSetting(int, CustomAppIcon); diff --git a/Telegram/SourceFiles/kotato/settings_menu.cpp b/Telegram/SourceFiles/kotato/settings_menu.cpp index 3a97172b4..ed09bf8a8 100644 --- a/Telegram/SourceFiles/kotato/settings_menu.cpp +++ b/Telegram/SourceFiles/kotato/settings_menu.cpp @@ -41,6 +41,15 @@ namespace Settings { namespace { +QString FileDialogTypeLabel(int value) { + const auto typedValue = Platform::FileDialog::ImplementationType(value); + switch (typedValue) { + case Platform::FileDialog::ImplementationType::Default: + return tr::ktg_file_dialog_type_default(tr::now); + } + return Platform::FileDialog::ImplementationTypeLabel(typedValue); +} + QString NetBoostLabel(int boost) { switch (boost) { case 0: @@ -457,6 +466,23 @@ void SetupKotatoSystem( } #endif // !DESKTOP_APP_DISABLE_GTK_INTEGRATION + AddButtonWithLabel( + container, + tr::ktg_settings_file_dialog_type(), + rpl::single(FileDialogTypeLabel(int(cFileDialogType()))), + st::settingsButton + )->addClickHandler([=] { + Ui::show(Box<::Kotato::RadioBox>( + tr::ktg_settings_file_dialog_type(tr::now), + int(cFileDialogType()), + int(Platform::FileDialog::ImplementationType::Count), + FileDialogTypeLabel, + [=](int value) { + cSetFileDialogType(Platform::FileDialog::ImplementationType(value)); + ::Kotato::JsonSettings::Write(); + }, false)); + }); + if (Platform::IsMac()) { const auto useNativeDecorationsToggled = Ui::CreateChild>( container.get()); diff --git a/Telegram/SourceFiles/platform/linux/file_utilities_linux.cpp b/Telegram/SourceFiles/platform/linux/file_utilities_linux.cpp index 92dc89179..c5c4fc7db 100644 --- a/Telegram/SourceFiles/platform/linux/file_utilities_linux.cpp +++ b/Telegram/SourceFiles/platform/linux/file_utilities_linux.cpp @@ -159,6 +159,19 @@ bool GetQt( } +QString ImplementationTypeLabel(ImplementationType value) { + switch (value) { +#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION + case ImplementationType::XDP: return qsl("XDG Desktop Portal"); +#endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION +#ifndef DESKTOP_APP_DISABLE_GTK_INTEGRATION + case ImplementationType::GTK: return qsl("GTK"); +#endif // !DESKTOP_APP_DISABLE_GTK_INTEGRATION + case ImplementationType::Qt: return qsl("Qt"); + } + Unexpected("Value in Platform::FileDialog::ImplementationTypeLabel."); +} + bool Get( QPointer parent, QStringList &files, diff --git a/Telegram/SourceFiles/platform/linux/file_utilities_linux.h b/Telegram/SourceFiles/platform/linux/file_utilities_linux.h index 00008a8ca..1cef5305c 100644 --- a/Telegram/SourceFiles/platform/linux/file_utilities_linux.h +++ b/Telegram/SourceFiles/platform/linux/file_utilities_linux.h @@ -27,6 +27,18 @@ inline void PostprocessDownloaded(const QString &filepath) { namespace FileDialog { +enum class ImplementationType { + Default, +#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION + XDP, +#endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION +#ifndef DESKTOP_APP_DISABLE_GTK_INTEGRATION + GTK, +#endif // !DESKTOP_APP_DISABLE_GTK_INTEGRATION + Qt, + Count, +}; + inline void InitLastPath() { ::FileDialog::internal::InitLastPathDefault(); } diff --git a/Telegram/SourceFiles/platform/linux/linux_gtk_file_dialog.cpp b/Telegram/SourceFiles/platform/linux/linux_gtk_file_dialog.cpp index 9befbf897..5bdfd2f87 100644 --- a/Telegram/SourceFiles/platform/linux/linux_gtk_file_dialog.cpp +++ b/Telegram/SourceFiles/platform/linux/linux_gtk_file_dialog.cpp @@ -642,11 +642,12 @@ void GtkFileDialog::setNameFilters(const QStringList &filters) { } // namespace bool Use(Type type) { - if (!Supported()) { + if (!Supported() + || (cFileDialogType() > ImplementationType::GTK)) { return false; } - return qEnvironmentVariableIsSet("TDESKTOP_USE_GTK_FILE_DIALOG") + return (cFileDialogType() == ImplementationType::GTK) || DesktopEnvironment::IsGtkBased(); } diff --git a/Telegram/SourceFiles/platform/linux/linux_xdp_file_dialog.cpp b/Telegram/SourceFiles/platform/linux/linux_xdp_file_dialog.cpp index ecbe52ed5..0495f17f5 100644 --- a/Telegram/SourceFiles/platform/linux/linux_xdp_file_dialog.cpp +++ b/Telegram/SourceFiles/platform/linux/linux_xdp_file_dialog.cpp @@ -642,17 +642,18 @@ rpl::producer<> XDPFileDialog::rejected() { } // namespace bool Use(Type type) { - static const auto ShouldUse = [&] { - const auto envVar = qEnvironmentVariableIsSet("TDESKTOP_USE_PORTAL"); + const auto shouldUse = [&] { + const auto setting = cFileDialogType() <= ImplementationType::XDP; + const auto forceSetting = cFileDialogType() == ImplementationType::XDP; const auto confined = InFlatpak() || InSnap(); const auto notGtkBased = !DesktopEnvironment::IsGtkBased(); - return confined || notGtkBased || envVar; + return setting && (confined || notGtkBased || forceSetting); }(); static const auto Version = FileChooserPortalVersion(); - return ShouldUse + return shouldUse && Version.has_value() && (type != Type::ReadFolder || *Version >= 3); } diff --git a/Telegram/SourceFiles/platform/mac/file_utilities_mac.h b/Telegram/SourceFiles/platform/mac/file_utilities_mac.h index aacb80c4c..8a6b93b44 100644 --- a/Telegram/SourceFiles/platform/mac/file_utilities_mac.h +++ b/Telegram/SourceFiles/platform/mac/file_utilities_mac.h @@ -27,6 +27,15 @@ inline void PostprocessDownloaded(const QString &filepath) { namespace FileDialog { +enum class ImplementationType { + Default, + Count, +}; + +inline QString ImplementationTypeLabel(ImplementationType value) { + Unexpected("Value in Platform::FileDialog::TypeLabel."); +} + inline void InitLastPath() { ::FileDialog::internal::InitLastPathDefault(); } diff --git a/Telegram/SourceFiles/platform/platform_file_utilities.h b/Telegram/SourceFiles/platform/platform_file_utilities.h index ad229d244..fc6730ee1 100644 --- a/Telegram/SourceFiles/platform/platform_file_utilities.h +++ b/Telegram/SourceFiles/platform/platform_file_utilities.h @@ -27,6 +27,10 @@ void PostprocessDownloaded(const QString &filepath); namespace FileDialog { +enum class ImplementationType; + +QString ImplementationTypeLabel(ImplementationType value); + void InitLastPath(); bool Get( diff --git a/Telegram/SourceFiles/platform/win/file_utilities_win.h b/Telegram/SourceFiles/platform/win/file_utilities_win.h index 4a5b07f66..43e23a955 100644 --- a/Telegram/SourceFiles/platform/win/file_utilities_win.h +++ b/Telegram/SourceFiles/platform/win/file_utilities_win.h @@ -12,6 +12,15 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL namespace Platform { namespace File { +enum class ImplementationType { + Default, + Count, +}; + +inline QString ImplementationTypeLabel(ImplementationType value) { + Unexpected("Value in Platform::FileDialog::TypeLabel."); +} + inline QString UrlToLocal(const QUrl &url) { return ::File::internal::UrlToLocalDefault(url); }