Skip to content

Commit

Permalink
GTK4: Migration. Return label_ back.
Browse files Browse the repository at this point in the history
Signed-off-by: Viktar Lukashonak <myxabeer@gmail.com>
  • Loading branch information
LukashonakV committed Mar 25, 2024
1 parent d3e32d1 commit 702171b
Show file tree
Hide file tree
Showing 20 changed files with 109 additions and 105 deletions.
4 changes: 3 additions & 1 deletion include/ALabel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@

namespace waybar {

class ALabel : public AModule, public Gtk::Label {
class ALabel : public AModule {
public:
virtual ~ALabel() = default;
auto update() -> void override;
virtual std::string getIcon(uint16_t, const std::string &alt = "", uint16_t max = 0);
virtual std::string getIcon(uint16_t, const std::vector<std::string> &alts, uint16_t max = 0);
operator Gtk::Widget &() override;

protected:
ALabel(const Json::Value &, const std::string &, const std::string &, const std::string &format,
uint16_t interval = 0, bool ellipsize = false, bool enable_click = false,
bool enable_scroll = false);

std::string format_;
Gtk::Label label_;
const std::chrono::seconds interval_;
bool alt_ = false;
std::string default_format_;
Expand Down
2 changes: 1 addition & 1 deletion include/bar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ class Bar {

Glib::RefPtr<Gdk::Surface> gdk_surface_;
struct wl_surface *surface;
Json::Value config;
int x_global;
int y_global;
Json::Value config;

/* Copy initial set of modes to allow customization */
bar_mode_map configured_modes = PRESET_MODES;
Expand Down
30 changes: 16 additions & 14 deletions src/ALabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ ALabel::ALabel(const Json::Value& config, const std::string& name, const std::st
: std::chrono::seconds(
config_["interval"].isUInt() ? config_["interval"].asUInt() : interval)),
default_format_(format_) {
Gtk::Label::set_name(name);
label_.set_name(name);
if (!id.empty()) {
Gtk::Label::get_style_context()->add_class(id);
label_.get_style_context()->add_class(id);
}
Gtk::Label::get_style_context()->add_class(MODULE_CLASS);
label_.get_style_context()->add_class(MODULE_CLASS);
if (config_["max-length"].isUInt()) {
Gtk::Label::set_max_width_chars(config_["max-length"].asInt());
Gtk::Label::set_ellipsize(Pango::EllipsizeMode::END);
Gtk::Label::set_single_line_mode(true);
} else if (ellipsize && Gtk::Label::get_max_width_chars() == -1) {
Gtk::Label::set_ellipsize(Pango::EllipsizeMode::END);
Gtk::Label::set_single_line_mode(true);
label_.set_max_width_chars(config_["max-length"].asInt());
label_.set_ellipsize(Pango::EllipsizeMode::END);
label_.set_single_line_mode(true);
} else if (ellipsize && label_.get_max_width_chars() == -1) {
label_.set_ellipsize(Pango::EllipsizeMode::END);
label_.set_single_line_mode(true);
}

if (config_["min-length"].isUInt()) {
Gtk::Label::set_width_chars(config_["min-length"].asUInt());
label_.set_width_chars(config_["min-length"].asUInt());
}

uint rotate = 0;
Expand All @@ -41,9 +41,9 @@ ALabel::ALabel(const Json::Value& config, const std::string& name, const std::st
if (config_["align"].isDouble()) {
auto align = config_["align"].asFloat();
if (rotate == 90 || rotate == 270) {
Gtk::Label::set_yalign(align);
label_.set_yalign(align);
} else {
Gtk::Label::set_xalign(align);
label_.set_xalign(align);
}
}

Expand Down Expand Up @@ -133,13 +133,15 @@ std::string ALabel::getState(uint8_t value, bool lesser) {
std::string valid_state;
for (auto const& state : states) {
if ((lesser ? value <= state.second : value >= state.second) && valid_state.empty()) {
Gtk::Label::get_style_context()->add_class(state.first);
label_.get_style_context()->add_class(state.first);
valid_state = state.first;
} else {
Gtk::Label::get_style_context()->remove_class(state.first);
label_.get_style_context()->remove_class(state.first);
}
}
return valid_state;
}

ALabel::operator Gtk::Widget&() { return label_; };

} // namespace waybar
2 changes: 1 addition & 1 deletion src/AModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ bool AModule::handleScroll(double dx, double dy) {

bool AModule::tooltipEnabled() { return isTooltip; }

AModule::operator Gtk::Widget&() { return dynamic_cast<Gtk::Widget&>(*this); }
AModule::operator Gtk::Widget&() { return this->operator Gtk::Widget&(); };

void AModule::bindEvents(Gtk::Widget& wg) {
wg.add_controller(controllClick_);
Expand Down
2 changes: 1 addition & 1 deletion src/bar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ void from_json(const Json::Value& j, std::map<Key, Value>& m) {

waybar::Bar::Bar(struct waybar_output* w_output, const Json::Value& w_config)
: output(w_output),
config(w_config),
window{Gtk::Window()},
config(w_config),
x_global(0),
y_global(0),
margins_{.top = 0, .right = 0, .bottom = 0, .left = 0},
Expand Down
12 changes: 6 additions & 6 deletions src/modules/backlight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,34 @@ auto waybar::modules::Backlight::update() -> void {
}

if (best->get_powered()) {
Gtk::Label::show();
label_.show();
const uint8_t percent =
best->get_max() == 0 ? 100 : round(best->get_actual() * 100.0f / best->get_max());
std::string desc = fmt::format(fmt::runtime(format_), fmt::arg("percent", percent),
fmt::arg("icon", getIcon(percent)));
Gtk::Label::set_markup(desc);
label_.set_markup(desc);
getState(percent);
if (tooltipEnabled()) {
std::string tooltip_format;
if (config_["tooltip-format"].isString()) {
tooltip_format = config_["tooltip-format"].asString();
}
if (!tooltip_format.empty()) {
Gtk::Label::set_tooltip_text(fmt::format(fmt::runtime(tooltip_format),
label_.set_tooltip_text(fmt::format(fmt::runtime(tooltip_format),
fmt::arg("percent", percent),
fmt::arg("icon", getIcon(percent))));
} else {
Gtk::Label::set_tooltip_text(desc);
label_.set_tooltip_text(desc);
}
}
} else {
Gtk::Label::hide();
label_.hide();
}
} else {
if (previous_best_device == nullptr) {
return;
}
Gtk::Label::set_markup("");
label_.set_markup("");
}
backend.set_previous_best_device(best);
previous_format_ = format_;
Expand Down
24 changes: 12 additions & 12 deletions src/modules/battery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ const std::string waybar::modules::Battery::formatTimeRemaining(float hoursRemai
auto waybar::modules::Battery::update() -> void {
#if defined(__linux__)
if (batteries_.empty()) {
Gtk::Label::hide();
label_.hide();
return;
}
#endif
Expand Down Expand Up @@ -663,15 +663,15 @@ auto waybar::modules::Battery::update() -> void {
} else if (config_["tooltip-format"].isString()) {
tooltip_format = config_["tooltip-format"].asString();
}
Gtk::Label::set_tooltip_text(fmt::format(fmt::runtime(tooltip_format),
label_.set_tooltip_text(fmt::format(fmt::runtime(tooltip_format),
fmt::arg("timeTo", tooltip_text_default),
fmt::arg("power", power), fmt::arg("capacity", capacity),
fmt::arg("time", time_remaining_formatted)));
}
if (!old_status_.empty()) {
Gtk::Label::get_style_context()->remove_class(old_status_);
label_.get_style_context()->remove_class(old_status_);
}
Gtk::Label::get_style_context()->add_class(status);
label_.get_style_context()->add_class(status);
old_status_ = status;
if (!state.empty() && config_["format-" + status + "-" + state].isString()) {
format = config_["format-" + status + "-" + state].asString();
Expand All @@ -681,11 +681,11 @@ auto waybar::modules::Battery::update() -> void {
format = config_["format-" + state].asString();
}
if (format.empty()) {
Gtk::Label::hide();
label_.hide();
} else {
Gtk::Label::show();
label_.show();
auto icons = std::vector<std::string>{status + "-" + state, status, state};
Gtk::Label::set_markup(fmt::format(
label_.set_markup(fmt::format(
fmt::runtime(format), fmt::arg("capacity", capacity), fmt::arg("power", power),
fmt::arg("icon", getIcon(capacity, icons)), fmt::arg("time", time_remaining_formatted)));
}
Expand All @@ -695,7 +695,7 @@ auto waybar::modules::Battery::update() -> void {

void waybar::modules::Battery::setBarClass(std::string& state) {
// Drop style
auto classes = Gtk::Widget::get_css_classes();
auto classes = label_.get_css_classes();
const Glib::ustring prefix{"battery-"};

auto old_class_it = std::find_if(classes.cbegin(), classes.cend(), [&prefix](auto classname) {
Expand All @@ -707,7 +707,7 @@ void waybar::modules::Battery::setBarClass(std::string& state) {
// If the bar doesn't have any `battery-` class
if (old_class_it == classes.end()) {
if (!state.empty()) {
Gtk::Label::get_style_context()->add_class(new_class);
label_.get_style_context()->add_class(new_class);
}
return;
}
Expand All @@ -717,14 +717,14 @@ void waybar::modules::Battery::setBarClass(std::string& state) {
// If the bar has a `battery-` class,
// but `state` is empty
if (state.empty()) {
Gtk::Label::get_style_context()->remove_class(old_class);
label_.get_style_context()->remove_class(old_class);
return;
}

// If the bar has a `battery-` class,
// and `state` is NOT empty
if (old_class != new_class) {
Gtk::Label::get_style_context()->remove_class(old_class);
Gtk::Label::get_style_context()->add_class(new_class);
label_.get_style_context()->remove_class(old_class);
label_.get_style_context()->add_class(new_class);
}
}
16 changes: 8 additions & 8 deletions src/modules/bluetooth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,10 @@ auto waybar::modules::Bluetooth::update() -> void {
}

auto update_style_context = [this](const std::string& style_class, bool in_next_state) {
if (in_next_state && !Gtk::Label::get_style_context()->has_class(style_class)) {
Gtk::Label::get_style_context()->add_class(style_class);
} else if (!in_next_state && Gtk::Label::get_style_context()->has_class(style_class)) {
Gtk::Label::get_style_context()->remove_class(style_class);
if (in_next_state && !label_.get_style_context()->has_class(style_class)) {
label_.get_style_context()->add_class(style_class);
} else if (!in_next_state && label_.get_style_context()->has_class(style_class)) {
label_.get_style_context()->remove_class(style_class);
}
};
update_style_context("discoverable", cur_controller_ ? cur_controller_->discoverable : false);
Expand All @@ -211,10 +211,10 @@ auto waybar::modules::Bluetooth::update() -> void {
state_ = state;

if (format_.empty()) {
Gtk::Label::hide();
label_.hide();
} else {
Gtk::Label::show();
Gtk::Label::set_markup(fmt::format(
label_.show();
label_.set_markup(fmt::format(
fmt::runtime(format_), fmt::arg("status", state_),
fmt::arg("num_connections", connected_devices_.size()),
fmt::arg("controller_address", cur_controller_ ? cur_controller_->address : "null"),
Expand Down Expand Up @@ -259,7 +259,7 @@ auto waybar::modules::Bluetooth::update() -> void {
device_enumerate_.erase(0, 1);
}
}
Gtk::Label::set_tooltip_text(fmt::format(
label_.set_tooltip_text(fmt::format(
fmt::runtime(tooltip_format), fmt::arg("status", state_),
fmt::arg("num_connections", connected_devices_.size()),
fmt::arg("controller_address", cur_controller_ ? cur_controller_->address : "null"),
Expand Down
6 changes: 3 additions & 3 deletions src/modules/cava.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,13 @@ auto waybar::modules::Cava::update() -> void {
if (prm_.bar_delim != 0) text_.push_back(prm_.bar_delim);
}

Gtk::Label::set_markup(text_);
Gtk::Label::show();
label_.set_markup(text_);
label_.show();
ALabel::update();
}
} else {
upThreadDelay(frame_time_milsec_, suspend_silence_delay_);
if (hide_on_silence_) Gtk::Label::hide();
if (hide_on_silence_) label_.hide();
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/modules/clock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config)
if (config_[kCldPlaceholder]["on-scroll"].isInt()) {
controllMot_->set_propagation_phase(Gtk::PropagationPhase::TARGET);
controllMot_->signal_leave().connect([this](){ cldCurrShift_ = months{0}; });
this->add_controller(controllMot_);
label_.add_controller(controllMot_);
}
}

Expand All @@ -128,7 +128,7 @@ auto waybar::modules::Clock::update() -> void {
auto tz{tzList_[tzCurrIdx_]};
const zoned_time now{tz, floor<seconds>(system_clock::now())};

Gtk::Label::set_markup(fmt_lib::vformat(locale_, format_, fmt_lib::make_format_args(now)));
label_.set_markup(fmt_lib::vformat(locale_, format_, fmt_lib::make_format_args(now)));

if (tooltipEnabled()) {
const year_month_day today{floor<days>(now.get_local_time())};
Expand All @@ -147,7 +147,7 @@ auto waybar::modules::Clock::update() -> void {

tlpText_ = fmt_lib::vformat(locale_, tlpText_, fmt_lib::make_format_args(shiftedNow));

Gtk::Label::set_tooltip_markup(tlpText_);
label_.set_tooltip_markup(tlpText_);
}

ALabel::update();
Expand Down
12 changes: 6 additions & 6 deletions src/modules/cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ auto waybar::modules::Cpu::update() -> void {
auto [cpu_usage, tooltip] = CpuUsage::getCpuUsage(prev_times_);
auto [max_frequency, min_frequency, avg_frequency] = CpuFrequency::getCpuFrequency();
if (tooltipEnabled()) {
Gtk::Label::set_tooltip_text(tooltip);
label_.set_tooltip_text(tooltip);
}
auto format = format_;
auto total_usage = cpu_usage.empty() ? 0 : cpu_usage[0];
Expand All @@ -37,15 +37,15 @@ auto waybar::modules::Cpu::update() -> void {
}

if (!prev_state_.empty()) {
Gtk::Label::get_style_context()->remove_class(prev_state_);
label_.get_style_context()->remove_class(prev_state_);
}
Gtk::Label::get_style_context()->add_class(state);
label_.get_style_context()->add_class(state);
prev_state_ = state;

if (format.empty()) {
Gtk::Label::hide();
label_.hide();
} else {
Gtk::Label::show();
label_.show();
auto icons = std::vector<std::string>{state};
fmt::dynamic_format_arg_store<fmt::format_context> store;
store.push_back(fmt::arg("load", load1));
Expand All @@ -61,7 +61,7 @@ auto waybar::modules::Cpu::update() -> void {
auto icon_format = fmt::format("icon{}", core_i);
store.push_back(fmt::arg(icon_format.c_str(), getIcon(cpu_usage[i], icons)));
}
Gtk::Label::set_markup(fmt::vformat(format, store));
label_.set_markup(fmt::vformat(format, store));
}

// Call parent update
Expand Down
8 changes: 4 additions & 4 deletions src/modules/cpu_frequency/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ auto waybar::modules::CpuFrequency::update() -> void {
auto tooltip =
fmt::format("Minimum frequency: {}\nAverage frequency: {}\nMaximum frequency: {}\n",
min_frequency, avg_frequency, max_frequency);
Gtk::Label::set_tooltip_text(tooltip);
label_.set_tooltip_text(tooltip);
}
auto format = format_;
auto state = getState(avg_frequency);
Expand All @@ -36,16 +36,16 @@ auto waybar::modules::CpuFrequency::update() -> void {
}

if (format.empty()) {
Gtk::Label::hide();
label_.hide();
} else {
Gtk::Label::show();
label_.show();
auto icons = std::vector<std::string>{state};
fmt::dynamic_format_arg_store<fmt::format_context> store;
store.push_back(fmt::arg("icon", getIcon(avg_frequency, icons)));
store.push_back(fmt::arg("max_frequency", max_frequency));
store.push_back(fmt::arg("min_frequency", min_frequency));
store.push_back(fmt::arg("avg_frequency", avg_frequency));
Gtk::Label::set_markup(fmt::vformat(format, store));
label_.set_markup(fmt::vformat(format, store));
}

// Call parent update
Expand Down
Loading

0 comments on commit 702171b

Please sign in to comment.