fix(#3239): hide cursor type change behind config flag

also, statically configure the cursor type
This commit is contained in:
Lars-Ragnar A. Haugen 2024-05-15 20:07:28 +02:00 committed by Austin Horstman
parent 6f994c849d
commit 7e2d8ab2a3
No known key found for this signature in database
2 changed files with 59 additions and 10 deletions

View File

@ -39,6 +39,38 @@ You can apply special styling to any module for when the cursor hovers it.
} }
``` ```
## Setting cursor style
Most, if not all, module types support setting the `cursor` option. This is
configured in your `config.jsonc`. If set to `true`, when hovering the module a
"pointer"(as commonly known from web CSS styling `cursor: pointer`) style cursor
will be shown.
There are more cursor types to choose from by setting the `cursor` option to
a number, see Gdk3 official docs for all possible cursor types:
https://docs.gtk.org/gdk3/enum.CursorType.html.
However, note that not all cursor options listed may be available on
your system. If you attempt to use a cursor which is not available, the
application will crash.
Example of enabling pointer(`Gdk::Hand2`) cursor type on a custom module:
```
"custom/my-custom-module": {
...
"cursor": true,
}
```
Example of setting cursor type to `Gdk::Boat`(according to
https://docs.gtk.org/gdk3/enum.CursorType.html#boat):
```
"custom/my-custom-module": {
...
"cursor": 8,
}
```
# SEE ALSO # SEE ALSO
- *waybar(5)* - *waybar(5)*

View File

@ -1,9 +1,13 @@
#include "AModule.hpp" #include "AModule.hpp"
#include <fmt/format.h> #include <fmt/format.h>
#include <spdlog/spdlog.h>
#include <util/command.hpp> #include <util/command.hpp>
#include "gdk/gdk.h"
#include "gdkmm/cursor.h"
namespace waybar { namespace waybar {
AModule::AModule(const Json::Value& config, const std::string& name, const std::string& id, AModule::AModule(const Json::Value& config, const std::string& name, const std::string& id,
@ -64,6 +68,16 @@ AModule::AModule(const Json::Value& config, const std::string& name, const std::
event_box_.add_events(Gdk::SCROLL_MASK | Gdk::SMOOTH_SCROLL_MASK); event_box_.add_events(Gdk::SCROLL_MASK | Gdk::SMOOTH_SCROLL_MASK);
event_box_.signal_scroll_event().connect(sigc::mem_fun(*this, &AModule::handleScroll)); event_box_.signal_scroll_event().connect(sigc::mem_fun(*this, &AModule::handleScroll));
} }
if (config_.isMember("cursor")) {
if (config_["cursor"].isBool() && config_["cursor"].asBool()) {
setCursor(Gdk::HAND2);
} else if (config_["cursor"].isInt()) {
setCursor(Gdk::CursorType(config_["cursor"].asInt()));
} else {
spdlog::warn("unknown cursor option configured on module {}", name_);
}
}
} }
AModule::~AModule() { AModule::~AModule() {
@ -91,19 +105,26 @@ auto AModule::doAction(const std::string& name) -> void {
} }
void AModule::setCursor(Gdk::CursorType const& c) { void AModule::setCursor(Gdk::CursorType const& c) {
auto cursor = Gdk::Cursor::create(c);
auto gdk_window = event_box_.get_window(); auto gdk_window = event_box_.get_window();
gdk_window->set_cursor(cursor); if (gdk_window) {
auto cursor = Gdk::Cursor::create(c);
gdk_window->set_cursor(cursor);
} else {
// window may not be accessible yet, in this case,
// schedule another call for setting the cursor in 1 sec
Glib::signal_timeout().connect_seconds(
[this, c]() {
setCursor(c);
return false;
},
1);
}
} }
bool AModule::handleMouseEnter(GdkEventCrossing* const& e) { bool AModule::handleMouseEnter(GdkEventCrossing* const& e) {
if (auto* module = event_box_.get_child(); module != nullptr) { if (auto* module = event_box_.get_child(); module != nullptr) {
module->set_state_flags(Gtk::StateFlags::STATE_FLAG_PRELIGHT); module->set_state_flags(Gtk::StateFlags::STATE_FLAG_PRELIGHT);
} }
if (hasUserEvents_) {
setCursor(Gdk::HAND2);
}
return false; return false;
} }
@ -111,10 +132,6 @@ bool AModule::handleMouseLeave(GdkEventCrossing* const& e) {
if (auto* module = event_box_.get_child(); module != nullptr) { if (auto* module = event_box_.get_child(); module != nullptr) {
module->unset_state_flags(Gtk::StateFlags::STATE_FLAG_PRELIGHT); module->unset_state_flags(Gtk::StateFlags::STATE_FLAG_PRELIGHT);
} }
if (hasUserEvents_) {
setCursor(Gdk::ARROW);
}
return false; return false;
} }