mirror of
https://github.com/rad4day/Waybar.git
synced 2023-12-21 10:22:59 +01:00
Since idle_inhibitor's have a surface, we should have one for each inhibitor module. Therefore, the status is stored on the Client, and all modules create or destroy their inhibitors depending on Client's idle_inhibitor_status. Also, when modules are destroyed they remove themselves from Client's idle_inhibitor_modules.
This commit is contained in:
parent
aa4fc3dd29
commit
4889e655eb
@ -23,8 +23,8 @@ class Client {
|
||||
struct zxdg_output_manager_v1 * xdg_output_manager = nullptr;
|
||||
|
||||
struct zwp_idle_inhibit_manager_v1 *idle_inhibit_manager = nullptr;
|
||||
struct zwp_idle_inhibitor_v1* idle_inhibitor;
|
||||
std::vector<waybar::AModule*> idle_inhibitor_modules;
|
||||
std::list<waybar::AModule*> idle_inhibitor_modules;
|
||||
std::string idle_inhibitor_status = "deactivated";
|
||||
|
||||
std::vector<std::unique_ptr<Bar>> bars;
|
||||
|
||||
|
@ -12,12 +12,12 @@ class IdleInhibitor : public ALabel {
|
||||
IdleInhibitor(const std::string&, const waybar::Bar&, const Json::Value&);
|
||||
~IdleInhibitor();
|
||||
auto update() -> void;
|
||||
struct zwp_idle_inhibitor_v1* idle_inhibitor_ = nullptr;
|
||||
|
||||
private:
|
||||
bool handleToggle(GdkEventButton* const& e);
|
||||
|
||||
const Bar& bar_;
|
||||
std::string status_;
|
||||
int pid_;
|
||||
};
|
||||
|
||||
|
@ -5,20 +5,26 @@ waybar::modules::IdleInhibitor::IdleInhibitor(const std::string& id, const Bar&
|
||||
const Json::Value& config)
|
||||
: ALabel(config, "idle_inhibitor", id, "{status}"),
|
||||
bar_(bar),
|
||||
status_("deactivated"),
|
||||
pid_(-1) {
|
||||
event_box_.add_events(Gdk::BUTTON_PRESS_MASK);
|
||||
event_box_.signal_button_press_event().connect(
|
||||
sigc::mem_fun(*this, &IdleInhibitor::handleToggle));
|
||||
|
||||
// Add this to the Client's idle_inhibitor_modules
|
||||
waybar::Client::inst()->idle_inhibitor_modules.push_back(this);
|
||||
|
||||
dp.emit();
|
||||
}
|
||||
|
||||
waybar::modules::IdleInhibitor::~IdleInhibitor() {
|
||||
if (waybar::Client::inst()->idle_inhibitor != nullptr) {
|
||||
zwp_idle_inhibitor_v1_destroy(waybar::Client::inst()->idle_inhibitor);
|
||||
waybar::Client::inst()->idle_inhibitor = nullptr;
|
||||
if (idle_inhibitor_ != nullptr) {
|
||||
zwp_idle_inhibitor_v1_destroy(idle_inhibitor_);
|
||||
idle_inhibitor_ = nullptr;
|
||||
}
|
||||
|
||||
// Remove this from the Client's idle_inhibitor_modules
|
||||
waybar::Client::inst()->idle_inhibitor_modules.remove(this);
|
||||
|
||||
if (pid_ != -1) {
|
||||
kill(-pid_, 9);
|
||||
pid_ = -1;
|
||||
@ -27,17 +33,24 @@ waybar::modules::IdleInhibitor::~IdleInhibitor() {
|
||||
|
||||
auto waybar::modules::IdleInhibitor::update() -> void {
|
||||
// Check status
|
||||
if (waybar::Client::inst()->idle_inhibitor != nullptr) {
|
||||
status_ = "activated";
|
||||
std::string status = waybar::Client::inst()->idle_inhibitor_status;
|
||||
if (status == "activated") {
|
||||
if (idle_inhibitor_ == nullptr) {
|
||||
idle_inhibitor_ = zwp_idle_inhibit_manager_v1_create_inhibitor(
|
||||
waybar::Client::inst()->idle_inhibit_manager, bar_.surface);
|
||||
}
|
||||
} else {
|
||||
status_ = "deactivated";
|
||||
if (idle_inhibitor_ != nullptr) {
|
||||
zwp_idle_inhibitor_v1_destroy(idle_inhibitor_);
|
||||
idle_inhibitor_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
label_.set_markup(
|
||||
fmt::format(format_, fmt::arg("status", status_), fmt::arg("icon", getIcon(0, status_))));
|
||||
label_.get_style_context()->add_class(status_);
|
||||
fmt::format(format_, fmt::arg("status", status), fmt::arg("icon", getIcon(0, status))));
|
||||
label_.get_style_context()->add_class(status);
|
||||
if (tooltipEnabled()) {
|
||||
label_.set_tooltip_text(status_);
|
||||
label_.set_tooltip_text(status);
|
||||
}
|
||||
// Call parent update
|
||||
ALabel::update();
|
||||
@ -45,17 +58,15 @@ auto waybar::modules::IdleInhibitor::update() -> void {
|
||||
|
||||
bool waybar::modules::IdleInhibitor::handleToggle(GdkEventButton* const& e) {
|
||||
if (e->button == 1) {
|
||||
label_.get_style_context()->remove_class(status_);
|
||||
if (waybar::Client::inst()->idle_inhibitor != nullptr) {
|
||||
zwp_idle_inhibitor_v1_destroy(waybar::Client::inst()->idle_inhibitor);
|
||||
waybar::Client::inst()->idle_inhibitor = nullptr;
|
||||
status_ = "deactivated";
|
||||
std::string status = waybar::Client::inst()->idle_inhibitor_status;
|
||||
label_.get_style_context()->remove_class(status);
|
||||
if (status == "activated") {
|
||||
status = "deactivated";
|
||||
} else {
|
||||
waybar::Client::inst()->idle_inhibitor = zwp_idle_inhibit_manager_v1_create_inhibitor(
|
||||
waybar::Client::inst()->idle_inhibit_manager, bar_.surface);
|
||||
status_ = "activated";
|
||||
status = "activated";
|
||||
}
|
||||
click_param = status_;
|
||||
waybar::Client::inst()->idle_inhibitor_status = status;
|
||||
click_param = status;
|
||||
}
|
||||
|
||||
// Make all other idle inhibitor modules update
|
||||
|
Loading…
Reference in New Issue
Block a user