Fix tray icon scaling on multi-display setups

This commit is contained in:
Michael Swiger 2021-08-16 23:33:29 -07:00
parent e5787a2617
commit 2d80d31527
5 changed files with 20 additions and 6 deletions

View File

@ -5,13 +5,15 @@
#include <glibmm/refptr.h> #include <glibmm/refptr.h>
#include <json/json.h> #include <json/json.h>
#include <tuple> #include <tuple>
#include "bar.hpp"
#include "modules/sni/item.hpp" #include "modules/sni/item.hpp"
namespace waybar::modules::SNI { namespace waybar::modules::SNI {
class Host { class Host {
public: public:
Host(const std::size_t id, const Json::Value&, const std::function<void(std::unique_ptr<Item>&)>&, Host(const std::size_t id, const Json::Value&, const Bar&,
const std::function<void(std::unique_ptr<Item>&)>&,
const std::function<void(std::unique_ptr<Item>&)>&); const std::function<void(std::unique_ptr<Item>&)>&);
~Host(); ~Host();
@ -36,6 +38,7 @@ class Host {
GCancellable* cancellable_ = nullptr; GCancellable* cancellable_ = nullptr;
SnWatcher* watcher_ = nullptr; SnWatcher* watcher_ = nullptr;
const Json::Value& config_; const Json::Value& config_;
const Bar& bar_;
const std::function<void(std::unique_ptr<Item>&)> on_add_; const std::function<void(std::unique_ptr<Item>&)> on_add_;
const std::function<void(std::unique_ptr<Item>&)> on_remove_; const std::function<void(std::unique_ptr<Item>&)> on_remove_;
}; };

View File

@ -14,6 +14,8 @@
#include <set> #include <set>
#include <string_view> #include <string_view>
#include "bar.hpp"
namespace waybar::modules::SNI { namespace waybar::modules::SNI {
struct ToolTip { struct ToolTip {
@ -23,7 +25,7 @@ struct ToolTip {
class Item : public sigc::trackable { class Item : public sigc::trackable {
public: public:
Item(const std::string&, const std::string&, const Json::Value&); Item(const std::string&, const std::string&, const Json::Value&, const Bar&);
~Item() = default; ~Item() = default;
std::string bus_name; std::string bus_name;
@ -56,6 +58,7 @@ class Item : public sigc::trackable {
bool item_is_menu = true; bool item_is_menu = true;
private: private:
void onConfigure(GdkEventConfigure* ev);
void proxyReady(Glib::RefPtr<Gio::AsyncResult>& result); void proxyReady(Glib::RefPtr<Gio::AsyncResult>& result);
void setProperty(const Glib::ustring& name, Glib::VariantBase& value); void setProperty(const Glib::ustring& name, Glib::VariantBase& value);
void setStatus(const Glib::ustring& value); void setStatus(const Glib::ustring& value);

View File

@ -4,7 +4,7 @@
namespace waybar::modules::SNI { namespace waybar::modules::SNI {
Host::Host(const std::size_t id, const Json::Value& config, Host::Host(const std::size_t id, const Json::Value& config, const Bar& bar,
const std::function<void(std::unique_ptr<Item>&)>& on_add, const std::function<void(std::unique_ptr<Item>&)>& on_add,
const std::function<void(std::unique_ptr<Item>&)>& on_remove) const std::function<void(std::unique_ptr<Item>&)>& on_remove)
: bus_name_("org.kde.StatusNotifierHost-" + std::to_string(getpid()) + "-" + : bus_name_("org.kde.StatusNotifierHost-" + std::to_string(getpid()) + "-" +
@ -13,6 +13,7 @@ Host::Host(const std::size_t id, const Json::Value& config,
bus_name_id_(Gio::DBus::own_name(Gio::DBus::BusType::BUS_TYPE_SESSION, bus_name_, bus_name_id_(Gio::DBus::own_name(Gio::DBus::BusType::BUS_TYPE_SESSION, bus_name_,
sigc::mem_fun(*this, &Host::busAcquired))), sigc::mem_fun(*this, &Host::busAcquired))),
config_(config), config_(config),
bar_(bar),
on_add_(on_add), on_add_(on_add),
on_remove_(on_remove) {} on_remove_(on_remove) {}
@ -136,7 +137,7 @@ void Host::addRegisteredItem(std::string service) {
return bus_name == item->bus_name && object_path == item->object_path; return bus_name == item->bus_name && object_path == item->object_path;
}); });
if (it == items_.end()) { if (it == items_.end()) {
items_.emplace_back(new Item(bus_name, object_path, config_)); items_.emplace_back(new Item(bus_name, object_path, config_, bar_));
on_add_(items_.back()); on_add_(items_.back());
} }
} }

View File

@ -39,7 +39,7 @@ namespace waybar::modules::SNI {
static const Glib::ustring SNI_INTERFACE_NAME = sn_item_interface_info()->name; static const Glib::ustring SNI_INTERFACE_NAME = sn_item_interface_info()->name;
static const unsigned UPDATE_DEBOUNCE_TIME = 10; static const unsigned UPDATE_DEBOUNCE_TIME = 10;
Item::Item(const std::string& bn, const std::string& op, const Json::Value& config) Item::Item(const std::string& bn, const std::string& op, const Json::Value& config, const Bar& bar)
: bus_name(bn), : bus_name(bn),
object_path(op), object_path(op),
icon_size(16), icon_size(16),
@ -54,6 +54,9 @@ Item::Item(const std::string& bn, const std::string& op, const Json::Value& conf
if (config["show-passive-items"].isBool()) { if (config["show-passive-items"].isBool()) {
show_passive_ = config["show-passive-items"].asBool(); show_passive_ = config["show-passive-items"].asBool();
} }
auto &window = const_cast<Bar &>(bar).window;
window.signal_configure_event().connect_notify(sigc::mem_fun(*this, &Item::onConfigure));
event_box.add(image); event_box.add(image);
event_box.add_events(Gdk::BUTTON_PRESS_MASK | Gdk::SCROLL_MASK | Gdk::SMOOTH_SCROLL_MASK); event_box.add_events(Gdk::BUTTON_PRESS_MASK | Gdk::SCROLL_MASK | Gdk::SMOOTH_SCROLL_MASK);
event_box.signal_button_press_event().connect(sigc::mem_fun(*this, &Item::handleClick)); event_box.signal_button_press_event().connect(sigc::mem_fun(*this, &Item::handleClick));
@ -73,6 +76,10 @@ Item::Item(const std::string& bn, const std::string& op, const Json::Value& conf
interface); interface);
} }
void Item::onConfigure(GdkEventConfigure* ev) {
this->updateImage();
}
void Item::proxyReady(Glib::RefPtr<Gio::AsyncResult>& result) { void Item::proxyReady(Glib::RefPtr<Gio::AsyncResult>& result) {
try { try {
this->proxy_ = Gio::DBus::Proxy::create_for_bus_finish(result); this->proxy_ = Gio::DBus::Proxy::create_for_bus_finish(result);

View File

@ -7,7 +7,7 @@ Tray::Tray(const std::string& id, const Bar& bar, const Json::Value& config)
: AModule(config, "tray", id), : AModule(config, "tray", id),
box_(bar.vertical ? Gtk::ORIENTATION_VERTICAL : Gtk::ORIENTATION_HORIZONTAL, 0), box_(bar.vertical ? Gtk::ORIENTATION_VERTICAL : Gtk::ORIENTATION_HORIZONTAL, 0),
watcher_(SNI::Watcher::getInstance()), watcher_(SNI::Watcher::getInstance()),
host_(nb_hosts_, config, std::bind(&Tray::onAdd, this, std::placeholders::_1), host_(nb_hosts_, config, bar, std::bind(&Tray::onAdd, this, std::placeholders::_1),
std::bind(&Tray::onRemove, this, std::placeholders::_1)) { std::bind(&Tray::onRemove, this, std::placeholders::_1)) {
spdlog::warn( spdlog::warn(
"For a functional tray you must have libappindicator-* installed and export " "For a functional tray you must have libappindicator-* installed and export "