Merge pull request #3398 from khaneliman/cursor

AModule: Cursor config option
This commit is contained in:
Alexis Rouillard 2024-07-17 22:40:12 +02:00 committed by GitHub
commit 152053e069
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 68 additions and 4 deletions

View File

@ -39,6 +39,40 @@ 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 `false`, when hovering the module a
"pointer"(as commonly known from web CSS styling `cursor: pointer`) style cursor
will not be shown. Default behavior is to indicate an interaction event is
available.
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 disabling pointer(`Gdk::Hand2`) cursor type on a custom module:
```
"custom/my-custom-module": {
...
"cursor": false,
}
```
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,17 @@ 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));
} }
// Respect user configuration of cursor
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,9 +106,20 @@ 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) {
@ -101,9 +127,11 @@ bool AModule::handleMouseEnter(GdkEventCrossing* const& e) {
module->set_state_flags(Gtk::StateFlags::STATE_FLAG_PRELIGHT); module->set_state_flags(Gtk::StateFlags::STATE_FLAG_PRELIGHT);
} }
if (hasUserEvents_) { // Default behavior indicating event availability
if (hasUserEvents_ && !config_.isMember("cursor")) {
setCursor(Gdk::HAND2); setCursor(Gdk::HAND2);
} }
return false; return false;
} }
@ -112,9 +140,11 @@ bool AModule::handleMouseLeave(GdkEventCrossing* const& e) {
module->unset_state_flags(Gtk::StateFlags::STATE_FLAG_PRELIGHT); module->unset_state_flags(Gtk::StateFlags::STATE_FLAG_PRELIGHT);
} }
if (hasUserEvents_) { // Default behavior indicating event availability
if (hasUserEvents_ && !config_.isMember("cursor")) {
setCursor(Gdk::ARROW); setCursor(Gdk::ARROW);
} }
return false; return false;
} }