Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/AIconLabel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ class AIconLabel : public ALabel {
bool label_contains_icon{false};

bool iconEnabled() const;
bool setTooltipMarkup (const Glib::ustring& markup) override;

private:
std::string iconLabel_;
std::string cleanLabel_;
};

} // namespace waybar
15 changes: 8 additions & 7 deletions include/ALabel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@ class ALabel : public AModule {
bool alt_ = false;
std::string default_format_;

bool setLabelMarkup(const Glib::ustring& markup);
bool setTooltipMarkup(const Glib::ustring& markup);
// Raw UTF-8 bytes, not Glib::ustring: ustring::operator== collates with
// g_utf8_collate(), which gives private-use codepoints (nerd-font icons)
// no collation weight, so two different icons compare equal.
std::optional<std::string> last_label_markup_;
std::optional<std::string> last_tooltip_markup_;

virtual bool setLabelMarkup(const Glib::ustring& markup);
virtual bool setTooltipMarkup(const Glib::ustring& markup);

// resolveTooltipFormat() / resolveFormat() are inherited from AModule.

Expand Down Expand Up @@ -88,11 +94,6 @@ class ALabel : public AModule {
static void handleGtkMenuEvent(GtkMenuItem* menuitem, gpointer data);

private:
// Raw UTF-8 bytes, not Glib::ustring: ustring::operator== collates with
// g_utf8_collate(), which gives private-use codepoints (nerd-font icons)
// no collation weight, so two different icons compare equal.
std::optional<std::string> last_label_markup_;
std::optional<std::string> last_tooltip_markup_;
Glib::RefPtr<Gtk::Tooltip> active_tooltip_;
};

Expand Down
4 changes: 2 additions & 2 deletions include/modules/cpu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
#include <utility>
#include <vector>

#include "ALabel.hpp"
#include "AIconLabel.hpp"
#include "util/sleeper_thread.hpp"

namespace waybar::modules {

class Cpu : public ALabel {
class Cpu : public AIconLabel {
public:
Cpu(const std::string&, const Json::Value&);
virtual ~Cpu() = default;
Expand Down
4 changes: 2 additions & 2 deletions include/modules/idle_inhibitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

#include <fmt/format.h>

#include "ALabel.hpp"
#include "AIconLabel.hpp"
#include "bar.hpp"
#include "client.hpp"

struct ext_idle_notification_v1;

namespace waybar::modules {

class IdleInhibitor : public ALabel {
class IdleInhibitor : public AIconLabel {
sigc::connection timeout_;
ext_idle_notification_v1* idle_notification_;
uint32_t idle_timeout_ms_;
Expand Down
4 changes: 2 additions & 2 deletions include/modules/inhibitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

#include <memory>

#include "ALabel.hpp"
#include "AIconLabel.hpp"
#include "bar.hpp"

namespace waybar::modules {

class Inhibitor : public ALabel {
class Inhibitor : public AIconLabel {
public:
Inhibitor(const std::string&, const waybar::Bar&, const Json::Value&);
virtual ~Inhibitor();
Expand Down
4 changes: 2 additions & 2 deletions include/modules/memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
#include <fstream>
#include <unordered_map>

#include "ALabel.hpp"
#include "AIconLabel.hpp"
#include "util/sleeper_thread.hpp"

namespace waybar::modules {

class Memory : public ALabel {
class Memory : public AIconLabel {
public:
Memory(const std::string&, const Json::Value&);
virtual ~Memory() = default;
Expand Down
4 changes: 2 additions & 2 deletions include/modules/network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <optional>
#include <vector>

#include "ALabel.hpp"
#include "AIconLabel.hpp"
#include "util/sleeper_thread.hpp"
#ifdef WANT_RFKILL
#include "util/rfkill.hpp"
Expand All @@ -23,7 +23,7 @@ enum ip_addr_pref : uint8_t { IPV4, IPV6, IPV4_6 };

namespace waybar::modules {

class Network : public ALabel {
class Network : public AIconLabel {
public:
Network(const std::string&, const Json::Value&);
virtual ~Network();
Expand Down
4 changes: 2 additions & 2 deletions include/modules/temperature.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

#include <fstream>

#include "ALabel.hpp"
#include "AIconLabel.hpp"
#include "util/sleeper_thread.hpp"

namespace waybar::modules {

class Temperature : public ALabel {
class Temperature : public AIconLabel {
public:
Temperature(const std::string&, const Json::Value&);
virtual ~Temperature() = default;
Expand Down
4 changes: 4 additions & 0 deletions man/waybar.5.scd.in
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ When positioning Waybar on the left or right side of the screen, sometimes it's

Valid options for the "rotate" property are: 0, 90, 180, and 270.

## Encode icon name in label string
You can use Rofi’s extended dmenu protocol to use system icon as module's icon. String in "format" and/or "format-icons" property may contain Rofi's extended dmenu protocol which is substring that prefixed with \0icon\1f followed by named icon or path to an image and ended with \n. Because of config file written in JSON then it should written \\0icon\\1f<icon-name>\\n in "format" and/or "format-icon" property.


## Swapping icon and label

If a module displays both a label and an icon, it might be desirable to swap them (for instance, for panels on the left or right of the screen, or for user adopting a right-to-left script). This can be achieved with the "swap-icon-label" property, taking a boolean. Example:
Expand Down
34 changes: 24 additions & 10 deletions src/AIconLabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <gdkmm/pixbuf.h>
#include <spdlog/spdlog.h>

#include <regex>
#include <string>

Expand Down Expand Up @@ -74,36 +75,38 @@ std::tuple<std::string, std::string> AIconLabel::extractIcon(const std::string&
label_result = std::regex_replace(input, clean_label_pattern, "");
}
} catch (const std::exception& e) {
spdlog::warn("Error while parsing icon from label. {}", e.what());
spdlog::warn("Error while parsing icon from label. {}", e.what());
}

return std::make_tuple(icon_result, label_result);
}

auto AIconLabel::update() -> void {
label_contains_icon = false;

auto [iconLabel, cleanLabel] = extractIcon(label_.get_label().c_str());
label_contains_icon = iconLabel.length() > 0;
if (label_.get_label().length() > 0 && label_.get_label().c_str() != cleanLabel_) {
auto [iconLabel, cleanLabel] = extractIcon(label_.get_label().c_str());
label_contains_icon = iconLabel.length() > 0;
iconLabel_ = iconLabel;
cleanLabel_ = cleanLabel;
}

if (label_contains_icon) {
label_.set_markup(cleanLabel);
label_.set_markup(cleanLabel_);

if (iconLabel.front() == '/') {
if (iconLabel_.front() == '/') {
try {
int scaled_icon_size = app_icon_size_ * image_.get_scale_factor();
auto pixbuf = Gdk::Pixbuf::create_from_file(iconLabel, scaled_icon_size, scaled_icon_size);
auto pixbuf = Gdk::Pixbuf::create_from_file(iconLabel_, scaled_icon_size, scaled_icon_size);

auto surface = Gdk::Cairo::create_surface_from_pixbuf(pixbuf, image_.get_scale_factor(),
image_.get_window());
image_.set(surface);
image_.set_visible(true);
} catch (const Glib::Exception& e) {
spdlog::warn("Failed to load embedded icon {}: {}", iconLabel, std::string(e.what()));
spdlog::warn("Failed to load embedded icon {}: {}", iconLabel_, std::string(e.what()));
image_.set_visible(false);
}
} else {
image_.set_from_icon_name(iconLabel, Gtk::ICON_SIZE_INVALID);
image_.set_from_icon_name(iconLabel_, Gtk::ICON_SIZE_INVALID);
image_.set_visible(true);
}
}
Expand All @@ -112,6 +115,17 @@ auto AIconLabel::update() -> void {
ALabel::update();
}

bool AIconLabel::setTooltipMarkup(const Glib::ustring& markup) {
if (last_tooltip_markup_ == markup) {
return false;
}

box_.set_tooltip_markup(markup);

last_tooltip_markup_ = markup;
return true;
}

bool AIconLabel::iconEnabled() const {
return label_contains_icon || (config_["icon"].isBool() ? config_["icon"].asBool() : false);
}
Expand Down
4 changes: 2 additions & 2 deletions src/modules/cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#endif

waybar::modules::Cpu::Cpu(const std::string& id, const Json::Value& config)
: ALabel(config, "cpu", id, "{usage}%", 10) {
: AIconLabel(config, "cpu", id, "{usage}%", 10) {
thread_ = [this] {
dp.emit();
thread_.sleep_for(interval_);
Expand Down Expand Up @@ -66,5 +66,5 @@ auto waybar::modules::Cpu::update() -> void {
}

// Call parent update
ALabel::update();
AIconLabel::update();
}
4 changes: 2 additions & 2 deletions src/modules/custom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,13 @@ void waybar::modules::Custom::handleEvent() {
}

bool waybar::modules::Custom::handleScroll(GdkEventScroll* e) {
auto ret = ALabel::handleScroll(e);
auto ret = AIconLabel::handleScroll(e);
handleEvent();
return ret;
}

bool waybar::modules::Custom::handleToggle(GdkEventButton* const& e) {
auto ret = ALabel::handleToggle(e);
auto ret = AIconLabel::handleToggle(e);
handleEvent();
return ret;
}
Expand Down
10 changes: 5 additions & 5 deletions src/modules/idle_inhibitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ long waybar::modules::IdleInhibitor::deactivationTime = time(nullptr);

waybar::modules::IdleInhibitor::IdleInhibitor(const std::string& id, const Bar& bar,
const Json::Value& config)
: ALabel(config, "idle_inhibitor", id, "{status}", 0, false, true),
: AIconLabel(config, "idle_inhibitor", id, "{status}", 0, false, true),
bar_(bar),
idle_inhibitor_(nullptr),
idle_notification_(nullptr),
Expand Down Expand Up @@ -99,7 +99,7 @@ auto waybar::modules::IdleInhibitor::update() -> void {
fmt::arg("icon", getIcon(0, status_text)));
label_.get_style_context()->add_class(status_text);
// Call parent update
ALabel::update();
AIconLabel::update();
}

auto waybar::modules::IdleInhibitor::refresh(int sig) -> void {
Expand Down Expand Up @@ -197,7 +197,7 @@ bool waybar::modules::IdleInhibitor::handleToggle(GdkEventButton* const& e) {
toggleStatus(0);
timeout = config_["timeout"].asDouble();
}
ALabel::handleToggle(e);
AIconLabel::handleToggle(e);
return true;
}

Expand All @@ -206,7 +206,7 @@ bool waybar::modules::IdleInhibitor::handleScroll(GdkEventScroll* e) {
// "dynamic-timeout" (singular) key spellings.
if (!(config_["dynamic-timeouts"].asBool() || config_["dynamic-timeout"].asBool())) {
// Delegate to the base handler so any configured on-scroll-* command still runs.
return ALabel::handleScroll(e);
return AIconLabel::handleScroll(e);
}
auto dir = AModule::getScrollDir(e);
if (dir == SCROLL_DIR::NONE) {
Expand All @@ -221,7 +221,7 @@ bool waybar::modules::IdleInhibitor::handleScroll(GdkEventScroll* e) {
}
deactivationTime = time(nullptr) + timeout * 60;

ALabel::handleScroll(e);
AIconLabel::handleScroll(e);
return true;
}

Expand Down
6 changes: 3 additions & 3 deletions src/modules/inhibitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ auto getInhibitors(const Json::Value& config) -> std::string {
namespace waybar::modules {

Inhibitor::Inhibitor(const std::string& id, const Bar& bar, const Json::Value& config)
: ALabel(config, "inhibitor", id, "{status}", true),
: AIconLabel(config, "inhibitor", id, "{status}", true),
dbus_(::dbus()),
inhibitors_(::getInhibitors(config)) {
event_box_.add_events(Gdk::BUTTON_PRESS_MASK);
Expand All @@ -126,7 +126,7 @@ auto Inhibitor::update() -> void {
label_.set_tooltip_markup(status_text);
}

return ALabel::update();
return AIconLabel::update();
}

auto Inhibitor::handleToggle(GdkEventButton* const& e) -> bool {
Expand All @@ -142,7 +142,7 @@ auto Inhibitor::handleToggle(GdkEventButton* const& e) -> bool {
}
}

return ALabel::handleToggle(e);
return AIconLabel::handleToggle(e);
}

} // namespace waybar::modules
4 changes: 2 additions & 2 deletions src/modules/memory/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const std::unordered_map<std::string, float> kUnits = {
}

waybar::modules::Memory::Memory(const std::string& id, const Json::Value& config)
: ALabel(config, "memory", id, "{}%", 30) {
: AIconLabel(config, "memory", id, "{}%", 30) {
thread_ = [this] {
dp.emit();
thread_.sleep_for(interval_);
Expand Down Expand Up @@ -91,5 +91,5 @@ auto waybar::modules::Memory::update() -> void {
event_box_.hide();
}
// Call parent update
ALabel::update();
AIconLabel::update();
}
4 changes: 2 additions & 2 deletions src/modules/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ uint32_t waybar::modules::Network::readLinkSpeed() const {
}

waybar::modules::Network::Network(const std::string& id, const Json::Value& config)
: ALabel(config, "network", id, DEFAULT_FORMAT, 60) {
: AIconLabel(config, "network", id, DEFAULT_FORMAT, 60) {
// Start with some "text" in the module's label_. update() will then
// update it. Since the text should be different, update() will be able
// to show or hide the event_box_. This is to work around the case where
Expand Down Expand Up @@ -496,7 +496,7 @@ auto waybar::modules::Network::update() -> void {
}

// Call parent update
ALabel::update();
AIconLabel::update();
}

// https://gist.github.com/rressi/92af77630faf055934c723ce93ae2495
Expand Down
4 changes: 2 additions & 2 deletions src/modules/temperature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#endif

waybar::modules::Temperature::Temperature(const std::string& id, const Json::Value& config)
: ALabel(config, "temperature", id, "{temperatureC}°C", 10) {
: AIconLabel(config, "temperature", id, "{temperatureC}°C", 10) {
#if defined(__FreeBSD__)
// FreeBSD uses sysctlbyname instead of read from a file
#else
Expand Down Expand Up @@ -160,7 +160,7 @@ auto waybar::modules::Temperature::update() -> void {
fmt::arg("temperatureK", temperature_k),
fmt::arg("icon", getIcon(temperature_c, "", max_temp)));
// Call parent update
ALabel::update();
AIconLabel::update();
}

float waybar::modules::Temperature::getTemperature() {
Expand Down
Loading