Added tooltips

This commit is contained in:
Erik Reider
2023-10-29 00:17:53 +02:00
parent 46e36c0e68
commit d32da917e4
6 changed files with 121 additions and 42 deletions

View File

@ -23,12 +23,12 @@
namespace waybar::modules::privacy {
PrivacyItem::PrivacyItem(const Json::Value& config_,
enum util::PipewireBackend::PrivacyNodeType privacy_type_,
const std::string& pos)
PrivacyItem::PrivacyItem(const Json::Value &config_, enum PrivacyNodeType privacy_type_,
std::list<PrivacyNodeInfo *> *nodes_, const std::string &pos)
: Gtk::Revealer(),
privacy_type(privacy_type_),
mutex_(),
nodes(nodes_),
tooltip_window(Gtk::ORIENTATION_VERTICAL, 0),
box_(Gtk::ORIENTATION_HORIZONTAL, 0),
icon_() {
switch (privacy_type) {
@ -74,6 +74,26 @@ PrivacyItem::PrivacyItem(const Json::Value& config_,
}
icon_.set_from_icon_name(iconName, Gtk::ICON_SIZE_INVALID);
// Tooltip Icon Size
if (config_["tooltip-icon-size"].isUInt()) {
tooltipIconSize = config_["tooltip-icon-size"].asUInt();
}
// Tooltip
if (config_["tooltip"].isString()) {
tooltip = config_["tooltip"].asBool();
}
set_has_tooltip(tooltip);
if (tooltip) {
// Sets the window to use when showing the tooltip
update_tooltip();
this->signal_query_tooltip().connect(sigc::track_obj(
[this](int x, int y, bool keyboard_tooltip, const Glib::RefPtr<Gtk::Tooltip> &tooltip) {
tooltip->set_custom(tooltip_window);
return true;
},
*this));
}
property_child_revealed().signal_changed().connect(
sigc::mem_fun(*this, &PrivacyItem::on_child_revealed_changed));
signal_map().connect(sigc::mem_fun(*this, &PrivacyItem::on_map_changed));
@ -83,6 +103,31 @@ PrivacyItem::PrivacyItem(const Json::Value& config_,
set_visible(false);
}
void PrivacyItem::update_tooltip() {
// Removes all old nodes
for (auto child : tooltip_window.get_children()) {
delete child;
}
for (auto *node : *nodes) {
Gtk::Box *box = new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL, 4);
// Set device icon
Gtk::Image *node_icon = new Gtk::Image();
node_icon->set_pixel_size(tooltipIconSize);
node_icon->set_from_icon_name(node->get_icon_name(), Gtk::ICON_SIZE_INVALID);
box->add(*node_icon);
// Set model
Gtk::Label *node_name = new Gtk::Label(node->get_name());
box->add(*node_name);
tooltip_window.add(*box);
}
tooltip_window.show_all();
}
bool PrivacyItem::is_enabled() { return enabled; }
void PrivacyItem::on_child_revealed_changed() {
@ -98,12 +143,12 @@ void PrivacyItem::on_map_changed() {
}
void PrivacyItem::set_in_use(bool in_use) {
mutex_.lock();
if (this->in_use == in_use && init) {
mutex_.unlock();
return;
if (in_use) {
update_tooltip();
}
if (this->in_use == in_use && init) return;
if (init) {
this->in_use = in_use;
if (this->in_use) {
@ -136,8 +181,6 @@ void PrivacyItem::set_in_use(bool in_use) {
get_style_context()->add_class(status);
}
lastStatus = status;
mutex_.unlock();
}
void PrivacyItem::set_icon_size(uint size) { icon_.set_pixel_size(size); }