Skip to content

Commit 152053e

Browse files
authored
Merge pull request #3398 from khaneliman/cursor
AModule: Cursor config option
2 parents 496dd05 + f78f29e commit 152053e

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

man/waybar-styles.5.scd.in

+34
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,40 @@ You can apply special styling to any module for when the cursor hovers it.
3939
}
4040
```
4141

42+
## Setting cursor style
43+
44+
Most, if not all, module types support setting the `cursor` option. This is
45+
configured in your `config.jsonc`. If set to `false`, when hovering the module a
46+
"pointer"(as commonly known from web CSS styling `cursor: pointer`) style cursor
47+
will not be shown. Default behavior is to indicate an interaction event is
48+
available.
49+
50+
There are more cursor types to choose from by setting the `cursor` option to
51+
a number, see Gdk3 official docs for all possible cursor types:
52+
https://docs.gtk.org/gdk3/enum.CursorType.html.
53+
However, note that not all cursor options listed may be available on
54+
your system. If you attempt to use a cursor which is not available, the
55+
application will crash.
56+
57+
Example of disabling pointer(`Gdk::Hand2`) cursor type on a custom module:
58+
59+
```
60+
"custom/my-custom-module": {
61+
...
62+
"cursor": false,
63+
}
64+
```
65+
66+
Example of setting cursor type to `Gdk::Boat`(according to
67+
https://docs.gtk.org/gdk3/enum.CursorType.html#boat):
68+
69+
```
70+
"custom/my-custom-module": {
71+
...
72+
"cursor": 8,
73+
}
74+
```
75+
4276
# SEE ALSO
4377

4478
- *waybar(5)*

src/AModule.cpp

+34-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
#include "AModule.hpp"
22

33
#include <fmt/format.h>
4+
#include <spdlog/spdlog.h>
45

56
#include <util/command.hpp>
67

8+
#include "gdk/gdk.h"
9+
#include "gdkmm/cursor.h"
10+
711
namespace waybar {
812

913
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::
6468
event_box_.add_events(Gdk::SCROLL_MASK | Gdk::SMOOTH_SCROLL_MASK);
6569
event_box_.signal_scroll_event().connect(sigc::mem_fun(*this, &AModule::handleScroll));
6670
}
71+
72+
// Respect user configuration of cursor
73+
if (config_.isMember("cursor")) {
74+
if (config_["cursor"].isBool() && config_["cursor"].asBool()) {
75+
setCursor(Gdk::HAND2);
76+
} else if (config_["cursor"].isInt()) {
77+
setCursor(Gdk::CursorType(config_["cursor"].asInt()));
78+
} else {
79+
spdlog::warn("unknown cursor option configured on module {}", name_);
80+
}
81+
}
6782
}
6883

6984
AModule::~AModule() {
@@ -91,19 +106,32 @@ auto AModule::doAction(const std::string& name) -> void {
91106
}
92107

93108
void AModule::setCursor(Gdk::CursorType const& c) {
94-
auto cursor = Gdk::Cursor::create(c);
95109
auto gdk_window = event_box_.get_window();
96-
gdk_window->set_cursor(cursor);
110+
if (gdk_window) {
111+
auto cursor = Gdk::Cursor::create(c);
112+
gdk_window->set_cursor(cursor);
113+
} else {
114+
// window may not be accessible yet, in this case,
115+
// schedule another call for setting the cursor in 1 sec
116+
Glib::signal_timeout().connect_seconds(
117+
[this, c]() {
118+
setCursor(c);
119+
return false;
120+
},
121+
1);
122+
}
97123
}
98124

99125
bool AModule::handleMouseEnter(GdkEventCrossing* const& e) {
100126
if (auto* module = event_box_.get_child(); module != nullptr) {
101127
module->set_state_flags(Gtk::StateFlags::STATE_FLAG_PRELIGHT);
102128
}
103129

104-
if (hasUserEvents_) {
130+
// Default behavior indicating event availability
131+
if (hasUserEvents_ && !config_.isMember("cursor")) {
105132
setCursor(Gdk::HAND2);
106133
}
134+
107135
return false;
108136
}
109137

@@ -112,9 +140,11 @@ bool AModule::handleMouseLeave(GdkEventCrossing* const& e) {
112140
module->unset_state_flags(Gtk::StateFlags::STATE_FLAG_PRELIGHT);
113141
}
114142

115-
if (hasUserEvents_) {
143+
// Default behavior indicating event availability
144+
if (hasUserEvents_ && !config_.isMember("cursor")) {
116145
setCursor(Gdk::ARROW);
117146
}
147+
118148
return false;
119149
}
120150

0 commit comments

Comments
 (0)