2019-06-11 15:53:16 +00:00
|
|
|
#include "modules/sni/item.hpp"
|
2021-06-23 07:27:42 +00:00
|
|
|
|
2021-07-21 05:29:34 +00:00
|
|
|
#include <gdkmm/general.h>
|
2019-03-30 01:40:28 +00:00
|
|
|
#include <glibmm/main.h>
|
2021-06-24 06:47:35 +00:00
|
|
|
#include <gtkmm/tooltip.h>
|
2019-05-18 23:44:45 +00:00
|
|
|
#include <spdlog/spdlog.h>
|
2021-06-23 07:27:42 +00:00
|
|
|
|
2019-08-11 15:23:37 +00:00
|
|
|
#include <fstream>
|
2021-06-23 07:27:42 +00:00
|
|
|
#include <map>
|
2019-05-20 15:00:07 +00:00
|
|
|
|
|
|
|
template <>
|
|
|
|
struct fmt::formatter<Glib::ustring> : formatter<std::string> {
|
|
|
|
template <typename FormatContext>
|
|
|
|
auto format(const Glib::ustring& value, FormatContext& ctx) {
|
|
|
|
return formatter<std::string>::format(value, ctx);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct fmt::formatter<Glib::VariantBase> : formatter<std::string> {
|
|
|
|
bool is_printable(const Glib::VariantBase& value) {
|
|
|
|
auto type = value.get_type_string();
|
|
|
|
/* Print only primitive (single character excluding 'v') and short complex types */
|
|
|
|
return (type.length() == 1 && islower(type[0]) && type[0] != 'v') || value.get_size() <= 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename FormatContext>
|
|
|
|
auto format(const Glib::VariantBase& value, FormatContext& ctx) {
|
|
|
|
if (is_printable(value)) {
|
|
|
|
return formatter<std::string>::format(value.print(), ctx);
|
|
|
|
} else {
|
|
|
|
return formatter<std::string>::format(value.get_type_string(), ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2018-08-29 18:36:39 +00:00
|
|
|
|
2019-04-24 10:37:24 +00:00
|
|
|
namespace waybar::modules::SNI {
|
2019-03-28 02:56:19 +00:00
|
|
|
|
2019-04-24 10:37:24 +00:00
|
|
|
static const Glib::ustring SNI_INTERFACE_NAME = sn_item_interface_info()->name;
|
|
|
|
static const unsigned UPDATE_DEBOUNCE_TIME = 10;
|
2019-03-28 02:56:19 +00:00
|
|
|
|
2019-04-24 10:37:24 +00:00
|
|
|
Item::Item(const std::string& bn, const std::string& op, const Json::Value& config)
|
2019-04-17 12:19:04 +00:00
|
|
|
: bus_name(bn),
|
|
|
|
object_path(op),
|
|
|
|
icon_size(16),
|
|
|
|
effective_icon_size(0),
|
2021-06-23 07:27:42 +00:00
|
|
|
icon_theme(Gtk::IconTheme::create()) {
|
2018-11-22 14:47:23 +00:00
|
|
|
if (config["icon-size"].isUInt()) {
|
|
|
|
icon_size = config["icon-size"].asUInt();
|
2018-10-26 12:53:39 +00:00
|
|
|
}
|
2021-06-26 23:33:25 +00:00
|
|
|
if (config["smooth-scrolling-threshold"].isNumeric()) {
|
|
|
|
scroll_threshold_ = config["smooth-scrolling-threshold"].asDouble();
|
|
|
|
}
|
2021-07-03 02:44:10 +00:00
|
|
|
if (config["show-passive-items"].isBool()) {
|
|
|
|
show_passive_ = config["show-passive-items"].asBool();
|
|
|
|
}
|
2018-11-22 14:47:23 +00:00
|
|
|
event_box.add(image);
|
2021-06-26 23:33:25 +00:00
|
|
|
event_box.add_events(Gdk::BUTTON_PRESS_MASK | Gdk::SCROLL_MASK | Gdk::SMOOTH_SCROLL_MASK);
|
2019-04-17 12:19:04 +00:00
|
|
|
event_box.signal_button_press_event().connect(sigc::mem_fun(*this, &Item::handleClick));
|
2021-06-26 23:33:25 +00:00
|
|
|
event_box.signal_scroll_event().connect(sigc::mem_fun(*this, &Item::handleScroll));
|
2021-07-03 02:44:10 +00:00
|
|
|
// initial visibility
|
|
|
|
event_box.set_visible(show_passive_);
|
2018-08-29 18:36:39 +00:00
|
|
|
|
2019-03-28 02:56:19 +00:00
|
|
|
cancellable_ = Gio::Cancellable::create();
|
2018-10-04 16:47:06 +00:00
|
|
|
|
2019-03-28 02:56:19 +00:00
|
|
|
auto interface = Glib::wrap(sn_item_interface_info(), true);
|
2019-04-18 15:52:00 +00:00
|
|
|
Gio::DBus::Proxy::create_for_bus(Gio::DBus::BusType::BUS_TYPE_SESSION,
|
|
|
|
bus_name,
|
|
|
|
object_path,
|
|
|
|
SNI_INTERFACE_NAME,
|
|
|
|
sigc::mem_fun(*this, &Item::proxyReady),
|
|
|
|
cancellable_,
|
|
|
|
interface);
|
2018-10-04 16:03:01 +00:00
|
|
|
}
|
|
|
|
|
2019-04-24 10:37:24 +00:00
|
|
|
void Item::proxyReady(Glib::RefPtr<Gio::AsyncResult>& result) {
|
2019-03-28 02:56:19 +00:00
|
|
|
try {
|
|
|
|
this->proxy_ = Gio::DBus::Proxy::create_for_bus_finish(result);
|
|
|
|
/* Properties are already cached during object creation */
|
|
|
|
auto cached_properties = this->proxy_->get_cached_property_names();
|
2019-04-17 12:19:04 +00:00
|
|
|
for (const auto& name : cached_properties) {
|
2019-03-28 02:56:19 +00:00
|
|
|
Glib::VariantBase value;
|
|
|
|
this->proxy_->get_cached_property(value, name);
|
|
|
|
setProperty(name, value);
|
2018-08-29 18:36:39 +00:00
|
|
|
}
|
2019-03-28 02:56:19 +00:00
|
|
|
|
2019-03-30 01:40:28 +00:00
|
|
|
this->proxy_->signal_signal().connect(sigc::mem_fun(*this, &Item::onSignal));
|
|
|
|
|
2021-07-03 02:44:10 +00:00
|
|
|
if (this->id.empty() || this->category.empty()) {
|
2019-05-18 23:44:45 +00:00
|
|
|
spdlog::error("Invalid Status Notifier Item: {}, {}", bus_name, object_path);
|
2019-03-28 02:56:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this->updateImage();
|
|
|
|
|
|
|
|
} catch (const Glib::Error& err) {
|
2019-05-20 15:00:07 +00:00
|
|
|
spdlog::error("Failed to create DBus Proxy for {} {}: {}", bus_name, object_path, err.what());
|
2019-03-28 02:56:19 +00:00
|
|
|
} catch (const std::exception& err) {
|
2019-05-20 12:35:05 +00:00
|
|
|
spdlog::error("Failed to create DBus Proxy for {} {}: {}", bus_name, object_path, err.what());
|
2018-08-29 18:36:39 +00:00
|
|
|
}
|
2019-03-28 02:56:19 +00:00
|
|
|
}
|
|
|
|
|
2019-04-17 12:19:04 +00:00
|
|
|
template <typename T>
|
2021-06-24 06:47:35 +00:00
|
|
|
T get_variant(const Glib::VariantBase& value) {
|
2019-04-24 10:37:24 +00:00
|
|
|
return Glib::VariantBase::cast_dynamic<Glib::Variant<T>>(value).get();
|
2019-03-28 02:56:19 +00:00
|
|
|
}
|
|
|
|
|
2021-06-24 06:47:35 +00:00
|
|
|
template <>
|
|
|
|
ToolTip get_variant<ToolTip>(const Glib::VariantBase& value) {
|
|
|
|
ToolTip result;
|
|
|
|
// Unwrap (sa(iiay)ss)
|
|
|
|
auto container = value.cast_dynamic<Glib::VariantContainerBase>(value);
|
|
|
|
result.icon_name = get_variant<Glib::ustring>(container.get_child(0));
|
|
|
|
result.text = get_variant<Glib::ustring>(container.get_child(2));
|
|
|
|
auto description = get_variant<Glib::ustring>(container.get_child(3));
|
|
|
|
if (!description.empty()) {
|
|
|
|
result.text = fmt::format("<b>{}</b>\n{}", result.text, description);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-04-24 10:37:24 +00:00
|
|
|
void Item::setProperty(const Glib::ustring& name, Glib::VariantBase& value) {
|
2019-05-20 15:00:07 +00:00
|
|
|
try {
|
|
|
|
spdlog::trace("Set tray item property: {}.{} = {}", id.empty() ? bus_name : id, name, value);
|
|
|
|
|
|
|
|
if (name == "Category") {
|
|
|
|
category = get_variant<std::string>(value);
|
|
|
|
} else if (name == "Id") {
|
|
|
|
id = get_variant<std::string>(value);
|
|
|
|
} else if (name == "Title") {
|
|
|
|
title = get_variant<std::string>(value);
|
2021-07-01 05:17:27 +00:00
|
|
|
if (tooltip.text.empty()) {
|
|
|
|
event_box.set_tooltip_markup(title);
|
|
|
|
}
|
2019-05-20 15:00:07 +00:00
|
|
|
} else if (name == "Status") {
|
2021-07-03 02:44:10 +00:00
|
|
|
setStatus(get_variant<Glib::ustring>(value));
|
2019-05-20 15:00:07 +00:00
|
|
|
} else if (name == "IconName") {
|
|
|
|
icon_name = get_variant<std::string>(value);
|
|
|
|
} else if (name == "IconPixmap") {
|
|
|
|
icon_pixmap = this->extractPixBuf(value.gobj());
|
|
|
|
} else if (name == "OverlayIconName") {
|
|
|
|
overlay_icon_name = get_variant<std::string>(value);
|
|
|
|
} else if (name == "OverlayIconPixmap") {
|
|
|
|
// TODO: overlay_icon_pixmap
|
|
|
|
} else if (name == "AttentionIconName") {
|
|
|
|
attention_icon_name = get_variant<std::string>(value);
|
|
|
|
} else if (name == "AttentionIconPixmap") {
|
|
|
|
// TODO: attention_icon_pixmap
|
|
|
|
} else if (name == "AttentionMovieName") {
|
|
|
|
attention_movie_name = get_variant<std::string>(value);
|
|
|
|
} else if (name == "ToolTip") {
|
2021-06-24 06:47:35 +00:00
|
|
|
tooltip = get_variant<ToolTip>(value);
|
|
|
|
if (!tooltip.text.empty()) {
|
|
|
|
event_box.set_tooltip_markup(tooltip.text);
|
|
|
|
}
|
2019-05-20 15:00:07 +00:00
|
|
|
} else if (name == "IconThemePath") {
|
|
|
|
icon_theme_path = get_variant<std::string>(value);
|
|
|
|
if (!icon_theme_path.empty()) {
|
|
|
|
icon_theme->set_search_path({icon_theme_path});
|
|
|
|
}
|
|
|
|
} else if (name == "Menu") {
|
|
|
|
menu = get_variant<std::string>(value);
|
2019-08-24 15:20:32 +00:00
|
|
|
makeMenu();
|
2019-05-20 15:00:07 +00:00
|
|
|
} else if (name == "ItemIsMenu") {
|
|
|
|
item_is_menu = get_variant<bool>(value);
|
2019-04-11 13:20:39 +00:00
|
|
|
}
|
2019-05-20 15:00:07 +00:00
|
|
|
} catch (const Glib::Error& err) {
|
|
|
|
spdlog::warn("Failed to set tray item property: {}.{}, value = {}, err = {}",
|
|
|
|
id.empty() ? bus_name : id,
|
|
|
|
name,
|
|
|
|
value,
|
|
|
|
err.what());
|
|
|
|
} catch (const std::exception& err) {
|
|
|
|
spdlog::warn("Failed to set tray item property: {}.{}, value = {}, err = {}",
|
|
|
|
id.empty() ? bus_name : id,
|
|
|
|
name,
|
|
|
|
value,
|
|
|
|
err.what());
|
2018-08-29 18:36:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-03 02:44:10 +00:00
|
|
|
void Item::setStatus(const Glib::ustring& value) {
|
|
|
|
Glib::ustring lower = value.lowercase();
|
|
|
|
event_box.set_visible(show_passive_ || lower.compare("passive") != 0);
|
|
|
|
|
|
|
|
auto style = event_box.get_style_context();
|
|
|
|
for (const auto& class_name : style->list_classes()) {
|
|
|
|
style->remove_class(class_name);
|
|
|
|
}
|
|
|
|
if (lower.compare("needsattention") == 0) {
|
|
|
|
// convert status to dash-case for CSS
|
|
|
|
lower = "needs-attention";
|
|
|
|
}
|
|
|
|
style->add_class(lower);
|
|
|
|
}
|
|
|
|
|
2019-04-24 10:37:24 +00:00
|
|
|
void Item::getUpdatedProperties() {
|
|
|
|
auto params = Glib::VariantContainerBase::create_tuple(
|
|
|
|
{Glib::Variant<Glib::ustring>::create(SNI_INTERFACE_NAME)});
|
2019-03-30 01:40:28 +00:00
|
|
|
proxy_->call("org.freedesktop.DBus.Properties.GetAll",
|
2019-04-18 15:52:00 +00:00
|
|
|
sigc::mem_fun(*this, &Item::processUpdatedProperties),
|
|
|
|
params);
|
2019-03-30 01:40:28 +00:00
|
|
|
};
|
|
|
|
|
2019-04-24 10:37:24 +00:00
|
|
|
void Item::processUpdatedProperties(Glib::RefPtr<Gio::AsyncResult>& _result) {
|
2019-03-30 01:40:28 +00:00
|
|
|
try {
|
|
|
|
auto result = proxy_->call_finish(_result);
|
|
|
|
// extract "a{sv}" from VariantContainerBase
|
2019-04-24 10:37:24 +00:00
|
|
|
Glib::Variant<std::map<Glib::ustring, Glib::VariantBase>> properties_variant;
|
2019-03-30 01:40:28 +00:00
|
|
|
result.get_child(properties_variant);
|
|
|
|
auto properties = properties_variant.get();
|
|
|
|
|
2019-04-17 12:19:04 +00:00
|
|
|
for (const auto& [name, value] : properties) {
|
2021-06-23 07:27:42 +00:00
|
|
|
if (update_pending_.count(name.raw())) {
|
2019-04-24 10:37:24 +00:00
|
|
|
setProperty(name, const_cast<Glib::VariantBase&>(value));
|
2019-03-30 01:40:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this->updateImage();
|
|
|
|
} catch (const Glib::Error& err) {
|
2019-05-20 15:00:07 +00:00
|
|
|
spdlog::warn("Failed to update properties: {}", err.what());
|
2019-03-30 01:40:28 +00:00
|
|
|
} catch (const std::exception& err) {
|
2019-05-20 12:35:05 +00:00
|
|
|
spdlog::warn("Failed to update properties: {}", err.what());
|
2019-03-30 01:40:28 +00:00
|
|
|
}
|
2021-06-23 07:27:42 +00:00
|
|
|
update_pending_.clear();
|
2019-03-30 01:40:28 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 07:27:42 +00:00
|
|
|
/**
|
|
|
|
* Mapping from a signal name to a set of possibly changed properties.
|
|
|
|
* Commented signals are not handled by the tray module at the moment.
|
|
|
|
*/
|
|
|
|
static const std::map<std::string_view, std::set<std::string_view>> signal2props = {
|
|
|
|
{"NewTitle", {"Title"}},
|
|
|
|
{"NewIcon", {"IconName", "IconPixmap"}},
|
|
|
|
// {"NewAttentionIcon", {"AttentionIconName", "AttentionIconPixmap", "AttentionMovieName"}},
|
|
|
|
// {"NewOverlayIcon", {"OverlayIconName", "OverlayIconPixmap"}},
|
|
|
|
{"NewIconThemePath", {"IconThemePath"}},
|
|
|
|
{"NewToolTip", {"ToolTip"}},
|
|
|
|
{"NewStatus", {"Status"}},
|
|
|
|
// {"XAyatanaNewLabel", {"XAyatanaLabel"}},
|
|
|
|
};
|
|
|
|
|
2019-04-24 10:37:24 +00:00
|
|
|
void Item::onSignal(const Glib::ustring& sender_name, const Glib::ustring& signal_name,
|
|
|
|
const Glib::VariantContainerBase& arguments) {
|
2019-05-20 15:00:07 +00:00
|
|
|
spdlog::trace("Tray item '{}' got signal {}", id, signal_name);
|
2021-06-23 07:27:42 +00:00
|
|
|
auto changed = signal2props.find(signal_name.raw());
|
|
|
|
if (changed != signal2props.end()) {
|
|
|
|
if (update_pending_.empty()) {
|
|
|
|
/* Debounce signals and schedule update of all properties.
|
|
|
|
* Based on behavior of Plasma dataengine for StatusNotifierItem.
|
|
|
|
*/
|
|
|
|
Glib::signal_timeout().connect_once(sigc::mem_fun(*this, &Item::getUpdatedProperties),
|
|
|
|
UPDATE_DEBOUNCE_TIME);
|
|
|
|
}
|
|
|
|
update_pending_.insert(changed->second.begin(), changed->second.end());
|
2019-03-30 01:40:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-17 12:19:04 +00:00
|
|
|
static void pixbuf_data_deleter(const guint8* data) { g_free((void*)data); }
|
2019-03-30 01:40:28 +00:00
|
|
|
|
2019-04-24 10:37:24 +00:00
|
|
|
Glib::RefPtr<Gdk::Pixbuf> Item::extractPixBuf(GVariant* variant) {
|
2019-04-17 12:19:04 +00:00
|
|
|
GVariantIter* it;
|
2018-09-09 18:46:26 +00:00
|
|
|
g_variant_get(variant, "a(iiay)", &it);
|
|
|
|
if (it == nullptr) {
|
|
|
|
return Glib::RefPtr<Gdk::Pixbuf>{};
|
|
|
|
}
|
2019-04-17 12:19:04 +00:00
|
|
|
GVariant* val;
|
2019-04-18 15:52:00 +00:00
|
|
|
gint lwidth = 0;
|
|
|
|
gint lheight = 0;
|
|
|
|
gint width;
|
|
|
|
gint height;
|
|
|
|
guchar* array = nullptr;
|
2018-09-09 18:46:26 +00:00
|
|
|
while (g_variant_iter_loop(it, "(ii@ay)", &width, &height, &val)) {
|
2019-04-17 12:19:04 +00:00
|
|
|
if (width > 0 && height > 0 && val != nullptr && width * height > lwidth * lheight) {
|
2018-10-04 16:47:06 +00:00
|
|
|
auto size = g_variant_get_size(val);
|
|
|
|
/* Sanity check */
|
|
|
|
if (size == 4U * width * height) {
|
|
|
|
/* Find the largest image */
|
|
|
|
gconstpointer data = g_variant_get_data(val);
|
|
|
|
if (data != nullptr) {
|
|
|
|
if (array != nullptr) {
|
|
|
|
g_free(array);
|
2018-09-09 18:46:26 +00:00
|
|
|
}
|
2019-04-17 12:19:04 +00:00
|
|
|
array = static_cast<guchar*>(g_memdup(data, size));
|
2018-10-04 16:47:06 +00:00
|
|
|
lwidth = width;
|
|
|
|
lheight = height;
|
2018-09-09 18:46:26 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-04 16:47:06 +00:00
|
|
|
}
|
2018-09-09 18:46:26 +00:00
|
|
|
}
|
|
|
|
g_variant_iter_free(it);
|
|
|
|
if (array != nullptr) {
|
|
|
|
/* argb to rgba */
|
|
|
|
for (uint32_t i = 0; i < 4U * lwidth * lheight; i += 4) {
|
|
|
|
guchar alpha = array[i];
|
|
|
|
array[i] = array[i + 1];
|
|
|
|
array[i + 1] = array[i + 2];
|
|
|
|
array[i + 2] = array[i + 3];
|
|
|
|
array[i + 3] = alpha;
|
|
|
|
}
|
2019-04-18 15:52:00 +00:00
|
|
|
return Gdk::Pixbuf::create_from_data(array,
|
|
|
|
Gdk::Colorspace::COLORSPACE_RGB,
|
|
|
|
true,
|
|
|
|
8,
|
|
|
|
lwidth,
|
|
|
|
lheight,
|
|
|
|
4 * lwidth,
|
|
|
|
&pixbuf_data_deleter);
|
2018-09-09 18:46:26 +00:00
|
|
|
}
|
|
|
|
return Glib::RefPtr<Gdk::Pixbuf>{};
|
|
|
|
}
|
|
|
|
|
2019-04-24 10:37:24 +00:00
|
|
|
void Item::updateImage() {
|
2021-07-31 23:14:02 +00:00
|
|
|
auto pixbuf = getIconPixbuf();
|
|
|
|
auto scaled_icon_size = getScaledIconSize();
|
|
|
|
|
|
|
|
if (!pixbuf) {
|
|
|
|
pixbuf = getIconByName("image-missing", getScaledIconSize());
|
|
|
|
}
|
2021-07-21 05:29:34 +00:00
|
|
|
|
2021-07-31 23:14:02 +00:00
|
|
|
// If the loaded icon is not square, assume that the icon height should match the
|
|
|
|
// requested icon size, but the width is allowed to be different. As such, if the
|
|
|
|
// height of the image does not match the requested icon size, resize the icon such that
|
|
|
|
// the aspect ratio is maintained, but the height matches the requested icon size.
|
|
|
|
if (pixbuf->get_height() != scaled_icon_size) {
|
|
|
|
int width = scaled_icon_size * pixbuf->get_width() / pixbuf->get_height();
|
|
|
|
pixbuf = pixbuf->scale_simple(width, scaled_icon_size, Gdk::InterpType::INTERP_BILINEAR);
|
|
|
|
}
|
2021-07-21 05:29:34 +00:00
|
|
|
|
2021-07-31 23:14:02 +00:00
|
|
|
auto surface = Gdk::Cairo::create_surface_from_pixbuf(pixbuf, 0, image.get_window());
|
|
|
|
image.set(surface);
|
|
|
|
}
|
2020-01-09 06:29:45 +00:00
|
|
|
|
2021-07-31 23:14:02 +00:00
|
|
|
Glib::RefPtr<Gdk::Pixbuf> Item::getIconPixbuf() {
|
|
|
|
try {
|
|
|
|
if (!icon_name.empty()) {
|
|
|
|
std::ifstream temp(icon_name);
|
|
|
|
if (temp.is_open()) {
|
|
|
|
return Gdk::Pixbuf::create_from_file(icon_name);
|
2018-08-29 18:36:39 +00:00
|
|
|
}
|
2021-07-31 23:14:02 +00:00
|
|
|
return getIconByName(icon_name, getScaledIconSize());
|
|
|
|
} else if (icon_pixmap) {
|
|
|
|
return icon_pixmap;
|
2018-08-29 18:36:39 +00:00
|
|
|
}
|
2021-07-31 23:14:02 +00:00
|
|
|
} catch (Glib::Error& e) {
|
|
|
|
spdlog::error("Item '{}': {}", id, static_cast<std::string>(e.what()));
|
|
|
|
}
|
|
|
|
return getIconByName("image-missing", getScaledIconSize());
|
2018-08-29 18:36:39 +00:00
|
|
|
}
|
|
|
|
|
2019-04-24 10:37:24 +00:00
|
|
|
Glib::RefPtr<Gdk::Pixbuf> Item::getIconByName(const std::string& name, int request_size) {
|
2018-10-26 12:53:39 +00:00
|
|
|
int tmp_size = 0;
|
2018-08-29 18:36:39 +00:00
|
|
|
icon_theme->rescan_if_needed();
|
|
|
|
auto sizes = icon_theme->get_icon_sizes(name.c_str());
|
2018-10-04 16:47:06 +00:00
|
|
|
|
2019-04-17 12:19:04 +00:00
|
|
|
for (auto const& size : sizes) {
|
2018-08-29 18:36:39 +00:00
|
|
|
// -1 == scalable
|
|
|
|
if (size == request_size || size == -1) {
|
2018-10-26 12:53:39 +00:00
|
|
|
tmp_size = request_size;
|
2018-08-29 18:36:39 +00:00
|
|
|
break;
|
2019-04-15 10:10:37 +00:00
|
|
|
} else if (size < request_size) {
|
2018-10-26 12:53:39 +00:00
|
|
|
tmp_size = size;
|
2019-04-15 10:10:37 +00:00
|
|
|
} else if (size > tmp_size && tmp_size > 0) {
|
|
|
|
tmp_size = request_size;
|
|
|
|
break;
|
2018-08-29 18:36:39 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-26 12:53:39 +00:00
|
|
|
if (tmp_size == 0) {
|
|
|
|
tmp_size = request_size;
|
2018-08-29 18:36:39 +00:00
|
|
|
}
|
2019-04-19 15:30:40 +00:00
|
|
|
if (!icon_theme_path.empty() &&
|
|
|
|
icon_theme->lookup_icon(
|
|
|
|
name.c_str(), tmp_size, Gtk::IconLookupFlags::ICON_LOOKUP_FORCE_SIZE)) {
|
|
|
|
return icon_theme->load_icon(
|
2019-04-18 15:52:00 +00:00
|
|
|
name.c_str(), tmp_size, Gtk::IconLookupFlags::ICON_LOOKUP_FORCE_SIZE);
|
2019-04-17 20:15:18 +00:00
|
|
|
}
|
2019-04-19 15:30:40 +00:00
|
|
|
Glib::RefPtr<Gtk::IconTheme> default_theme = Gtk::IconTheme::get_default();
|
|
|
|
default_theme->rescan_if_needed();
|
|
|
|
return default_theme->load_icon(
|
|
|
|
name.c_str(), tmp_size, Gtk::IconLookupFlags::ICON_LOOKUP_FORCE_SIZE);
|
2018-10-04 16:03:01 +00:00
|
|
|
}
|
2018-09-17 21:32:05 +00:00
|
|
|
|
2021-07-31 23:14:02 +00:00
|
|
|
double Item::getScaledIconSize() {
|
|
|
|
// apply the scale factor from the Gtk window to the requested icon size
|
|
|
|
return icon_size * image.get_scale_factor();
|
|
|
|
}
|
|
|
|
|
2019-05-17 12:23:52 +00:00
|
|
|
void Item::onMenuDestroyed(Item* self, GObject* old_menu_pointer) {
|
|
|
|
if (old_menu_pointer == reinterpret_cast<GObject*>(self->dbus_menu)) {
|
|
|
|
self->gtk_menu = nullptr;
|
|
|
|
self->dbus_menu = nullptr;
|
|
|
|
}
|
2018-09-17 21:32:05 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 15:20:32 +00:00
|
|
|
void Item::makeMenu() {
|
2019-05-17 12:23:52 +00:00
|
|
|
if (gtk_menu == nullptr && !menu.empty()) {
|
|
|
|
dbus_menu = dbusmenu_gtkmenu_new(bus_name.data(), menu.data());
|
|
|
|
if (dbus_menu != nullptr) {
|
|
|
|
g_object_ref_sink(G_OBJECT(dbus_menu));
|
|
|
|
g_object_weak_ref(G_OBJECT(dbus_menu), (GWeakNotify)onMenuDestroyed, this);
|
|
|
|
gtk_menu = Glib::wrap(GTK_MENU(dbus_menu));
|
|
|
|
gtk_menu->attach_to_widget(event_box);
|
2018-11-22 14:47:23 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-17 21:32:05 +00:00
|
|
|
}
|
|
|
|
|
2019-04-24 10:37:24 +00:00
|
|
|
bool Item::handleClick(GdkEventButton* const& ev) {
|
|
|
|
auto parameters = Glib::VariantContainerBase::create_tuple(
|
|
|
|
{Glib::Variant<int>::create(ev->x), Glib::Variant<int>::create(ev->y)});
|
2019-06-14 13:44:11 +00:00
|
|
|
if ((ev->button == 1 && item_is_menu) || ev->button == 3) {
|
2019-08-24 15:20:32 +00:00
|
|
|
makeMenu();
|
2019-05-17 12:23:52 +00:00
|
|
|
if (gtk_menu != nullptr) {
|
|
|
|
#if GTK_CHECK_VERSION(3, 22, 0)
|
|
|
|
gtk_menu->popup_at_pointer(reinterpret_cast<GdkEvent*>(ev));
|
|
|
|
#else
|
|
|
|
gtk_menu->popup(ev->button, ev->time);
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
} else {
|
2019-03-28 02:56:19 +00:00
|
|
|
proxy_->call("ContextMenu", parameters);
|
|
|
|
return true;
|
2018-10-04 16:47:06 +00:00
|
|
|
}
|
2018-11-22 14:47:23 +00:00
|
|
|
} else if (ev->button == 1) {
|
2019-03-28 02:56:19 +00:00
|
|
|
proxy_->call("Activate", parameters);
|
|
|
|
return true;
|
2018-11-22 14:47:23 +00:00
|
|
|
} else if (ev->button == 2) {
|
2019-03-28 02:56:19 +00:00
|
|
|
proxy_->call("SecondaryActivate", parameters);
|
|
|
|
return true;
|
2018-09-17 21:32:05 +00:00
|
|
|
}
|
2018-11-22 14:47:23 +00:00
|
|
|
return false;
|
2018-10-04 16:47:06 +00:00
|
|
|
}
|
2019-04-24 10:37:24 +00:00
|
|
|
|
2021-06-26 23:33:25 +00:00
|
|
|
bool Item::handleScroll(GdkEventScroll* const& ev) {
|
|
|
|
int dx = 0, dy = 0;
|
|
|
|
switch (ev->direction) {
|
|
|
|
case GDK_SCROLL_UP:
|
|
|
|
dy = -1;
|
|
|
|
break;
|
|
|
|
case GDK_SCROLL_DOWN:
|
|
|
|
dy = 1;
|
|
|
|
break;
|
|
|
|
case GDK_SCROLL_LEFT:
|
|
|
|
dx = -1;
|
|
|
|
break;
|
|
|
|
case GDK_SCROLL_RIGHT:
|
|
|
|
dx = 1;
|
|
|
|
break;
|
|
|
|
case GDK_SCROLL_SMOOTH:
|
|
|
|
distance_scrolled_x_ += ev->delta_x;
|
|
|
|
distance_scrolled_y_ += ev->delta_y;
|
|
|
|
// check against the configured threshold and ensure that the absolute value >= 1
|
|
|
|
if (distance_scrolled_x_ > scroll_threshold_) {
|
|
|
|
dx = (int)lround(std::max(distance_scrolled_x_, 1.0));
|
|
|
|
distance_scrolled_x_ = 0;
|
|
|
|
} else if (distance_scrolled_x_ < -scroll_threshold_) {
|
|
|
|
dx = (int)lround(std::min(distance_scrolled_x_, -1.0));
|
|
|
|
distance_scrolled_x_ = 0;
|
|
|
|
}
|
|
|
|
if (distance_scrolled_y_ > scroll_threshold_) {
|
|
|
|
dy = (int)lround(std::max(distance_scrolled_y_, 1.0));
|
|
|
|
distance_scrolled_y_ = 0;
|
|
|
|
} else if (distance_scrolled_y_ < -scroll_threshold_) {
|
|
|
|
dy = (int)lround(std::min(distance_scrolled_y_, -1.0));
|
|
|
|
distance_scrolled_y_ = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (dx != 0) {
|
|
|
|
auto parameters = Glib::VariantContainerBase::create_tuple(
|
|
|
|
{Glib::Variant<int>::create(dx), Glib::Variant<Glib::ustring>::create("horizontal")});
|
|
|
|
proxy_->call("Scroll", parameters);
|
|
|
|
}
|
|
|
|
if (dy != 0) {
|
|
|
|
auto parameters = Glib::VariantContainerBase::create_tuple(
|
|
|
|
{Glib::Variant<int>::create(dy), Glib::Variant<Glib::ustring>::create("vertical")});
|
|
|
|
proxy_->call("Scroll", parameters);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-18 23:44:45 +00:00
|
|
|
} // namespace waybar::modules::SNI
|