1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-22 07:42:37 +00:00
transmission/gtk/SystemTrayIcon.cc
Mike Gelfand c75c6bf5c8
Add support for GTK 4 (#3916)
* Make compact mode switch work for both GTK 3 and 4

* Implement GTK 4-specific view gesture handling

* Fix torrents view context menu on GTK 4

* Explicitly show/hide menubar on startup/teardown

* Switch from `Gtk::Pixbuf` to `Gio::Icon` for views

* Support GTK 4 exceptions based on `std::exception`

* Fix options menu setup with GTK 4

* Use `delete-event` (GTK 3) and `close-request` (GTK 4) signals to handle window clousure

* Add custom file chooser button implementation

GTK 4 drops FileChooserButton widget and suggests implementing it using
Button.

* Add helpers to set X11 hints with GTK 4

* Remove `HigWorkarea` class that's no longer used

* Make main menu shortcuts work with GTK 4

* Make drops work in main window and make dialog with GTK 4

* Remove unused `gtr_action_get_widget()` helper

* Fix text direction mark setup with GTK 4 (due to switch to enum class)

* Fix file tree font size calculation with GTK 4

* Fix crash during shutdown with GTK 4

* Switch from `RadioButton` to `CheckButton` for compatibility with GTK 4

* Fix opening files with GTK 4

* Rework torrent cell renderer to support both GTK 3 and 4

* Disable system tray icon support with GTK 4

* Fix windows positioning with GTK 4

* Fix focus event handling with GTK 4

* Adapt to tree model row/iterator changes in GTK 4

* Adapt to toplevel/root window changes in GTK 4

* Adapt to clipboard changes in GTK 4

* Adapt to icon/theme changes in GTK 4

* Adapt to file/path changes in GTK 4

* Random leftover fixes for GTK 4 compatibility

* Clean up unused code

* Move GTK 3 *.ui files into a subdirectory

* Add GTK 4 *.ui files

* Search for both GTK 3 and 4 during configuration
2022-10-09 01:50:03 +03:00

208 lines
5.3 KiB
C++

// This file Copyright © 2007-2022 Mnemosyne LLC.
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
// or any future license endorsed by Mnemosyne LLC.
// License text can be found in the licenses/ folder.
// _AppIndicatorClass::{fallback,unfallback} use deprecated GtkStatusIcon
#undef GTK_DISABLE_DEPRECATED
// We're using deprecated Gtk::StatusItem ourselves as well
#undef GTKMM_DISABLE_DEPRECATED
#include <string>
#include <glibmm.h>
#include <glibmm/i18n.h>
#ifdef HAVE_LIBAPPINDICATOR
#include <libappindicator/app-indicator.h>
#endif
#include <libtransmission/transmission.h>
#include <libtransmission/utils.h>
#include "Actions.h"
#include "Session.h"
#include "SystemTrayIcon.h"
#include "Utils.h"
#define TR_SYS_TRAY_IMPL_NONE 0
#define TR_SYS_TRAY_IMPL_APPINDICATOR 1
#define TR_SYS_TRAY_IMPL_STATUS_ICON 2
#ifdef HAVE_LIBAPPINDICATOR
#define TR_SYS_TRAY_IMPL TR_SYS_TRAY_IMPL_APPINDICATOR
#elif !GTKMM_CHECK_VERSION(4, 0, 0)
#define TR_SYS_TRAY_IMPL TR_SYS_TRAY_IMPL_STATUS_ICON
#else
#define TR_SYS_TRAY_IMPL TR_SYS_TRAY_IMPL_NONE
#endif
using namespace std::literals;
namespace
{
#if TR_SYS_TRAY_IMPL != TR_SYS_TRAY_IMPL_NONE
auto const TrayIconName = Glib::ustring("transmission-tray-icon"s);
auto const AppIconName = Glib::ustring("transmission"s);
#endif
#if TR_SYS_TRAY_IMPL == TR_SYS_TRAY_IMPL_APPINDICATOR
auto const AppName = Glib::ustring("transmission-gtk"s);
#endif
} // namespace
class SystemTrayIcon::Impl
{
public:
Impl(Gtk::Window& main_window, Glib::RefPtr<Session> const& core);
~Impl();
TR_DISABLE_COPY_MOVE(Impl)
void refresh();
private:
void activated();
void popup(guint button, guint when);
[[nodiscard]] std::string make_tooltip_text() const;
private:
Glib::RefPtr<Session> const core_;
#if TR_SYS_TRAY_IMPL != TR_SYS_TRAY_IMPL_NONE
Gtk::Menu* menu_;
#endif
#if TR_SYS_TRAY_IMPL == TR_SYS_TRAY_IMPL_APPINDICATOR
AppIndicator* indicator_;
#elif TR_SYS_TRAY_IMPL == TR_SYS_TRAY_IMPL_STATUS_ICON
Glib::RefPtr<Gtk::StatusIcon> icon_;
#endif
};
#if TR_SYS_TRAY_IMPL == TR_SYS_TRAY_IMPL_APPINDICATOR
SystemTrayIcon::Impl::~Impl()
{
g_object_unref(indicator_);
}
void SystemTrayIcon::Impl::refresh()
{
}
#elif TR_SYS_TRAY_IMPL == TR_SYS_TRAY_IMPL_STATUS_ICON
SystemTrayIcon::Impl::~Impl() = default;
void SystemTrayIcon::Impl::activated()
{
gtr_action_activate("toggle-main-window");
}
void SystemTrayIcon::Impl::popup(guint /*button*/, guint /*when*/)
{
menu_->popup_at_pointer(nullptr);
}
void SystemTrayIcon::Impl::refresh()
{
icon_->set_tooltip_text(make_tooltip_text());
}
#else
SystemTrayIcon::Impl::~Impl() = default;
#endif
namespace
{
#if TR_SYS_TRAY_IMPL != TR_SYS_TRAY_IMPL_NONE
Glib::ustring getIconName()
{
Glib::ustring icon_name;
// if the tray's icon is a 48x48 file, use it.
// otherwise, use the fallback builtin icon.
if (auto theme = Gtk::IconTheme::get_default(); !theme->has_icon(TrayIconName))
{
icon_name = AppIconName;
}
else
{
auto const icon_info = theme->lookup_icon(TrayIconName, 48, Gtk::ICON_LOOKUP_USE_BUILTIN);
bool const icon_is_builtin = icon_info.get_filename().empty();
icon_name = icon_is_builtin ? AppIconName : TrayIconName;
}
return icon_name;
}
#endif
} // namespace
SystemTrayIcon::SystemTrayIcon(Gtk::Window& main_window, Glib::RefPtr<Session> const& core)
: impl_(std::make_unique<Impl>(main_window, core))
{
}
SystemTrayIcon::~SystemTrayIcon() = default;
void SystemTrayIcon::refresh()
{
#if TR_SYS_TRAY_IMPL != TR_SYS_TRAY_IMPL_NONE
impl_->refresh();
#endif
}
bool SystemTrayIcon::is_available()
{
#if TR_SYS_TRAY_IMPL != TR_SYS_TRAY_IMPL_NONE
return true;
#else
return false;
#endif
}
std::unique_ptr<SystemTrayIcon> SystemTrayIcon::create(Gtk::Window& main_window, Glib::RefPtr<Session> const& core)
{
return is_available() ? std::make_unique<SystemTrayIcon>(main_window, core) : nullptr;
}
SystemTrayIcon::Impl::Impl([[maybe_unused]] Gtk::Window& main_window, Glib::RefPtr<Session> const& core)
: core_(core)
{
#if TR_SYS_TRAY_IMPL != TR_SYS_TRAY_IMPL_NONE
auto const icon_name = getIconName();
menu_ = Gtk::make_managed<Gtk::Menu>(gtr_action_get_object<Gio::Menu>("icon-popup"));
menu_->attach_to_widget(main_window);
#endif
#if TR_SYS_TRAY_IMPL == TR_SYS_TRAY_IMPL_APPINDICATOR
indicator_ = app_indicator_new(AppName.c_str(), icon_name.c_str(), APP_INDICATOR_CATEGORY_SYSTEM_SERVICES);
app_indicator_set_status(indicator_, APP_INDICATOR_STATUS_ACTIVE);
app_indicator_set_menu(indicator_, Glib::unwrap(menu_));
app_indicator_set_title(indicator_, Glib::get_application_name().c_str());
#elif TR_SYS_TRAY_IMPL == TR_SYS_TRAY_IMPL_STATUS_ICON
icon_ = Gtk::StatusIcon::create(icon_name);
icon_->signal_activate().connect(sigc::mem_fun(*this, &Impl::activated));
icon_->signal_popup_menu().connect(sigc::mem_fun(*this, &Impl::popup));
#endif
}
std::string SystemTrayIcon::Impl::make_tooltip_text() const
{
auto const* const session = core_->get_session();
return fmt::format(
_("{upload_speed} ▲ {download_speed} ▼"),
fmt::arg("upload_speed", tr_formatter_speed_KBps(tr_sessionGetRawSpeed_KBps(session, TR_UP))),
fmt::arg("download_speed", tr_formatter_speed_KBps(tr_sessionGetRawSpeed_KBps(session, TR_DOWN))));
}