Skip to content

Commit

Permalink
fix(#3239): hide cursor type change behind config flag
Browse files Browse the repository at this point in the history
also, statically configure the cursor type
  • Loading branch information
haug1 authored and khaneliman committed Jul 2, 2024
1 parent ca3877d commit 5d6c5ec
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 10 deletions.
32 changes: 32 additions & 0 deletions man/waybar-styles.5.scd.in
Original file line number Diff line number Diff line change
Expand Up @@ -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

- *waybar(5)*
37 changes: 27 additions & 10 deletions src/AModule.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#include "AModule.hpp"

#include <fmt/format.h>
#include <spdlog/spdlog.h>

#include <util/command.hpp>

#include "gdk/gdk.h"
#include "gdkmm/cursor.h"

namespace waybar {

AModule::AModule(const Json::Value& config, const std::string& name, const std::string& id,
Expand Down Expand Up @@ -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_.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() {
Expand Down Expand Up @@ -91,30 +105,33 @@ auto AModule::doAction(const std::string& name) -> void {
}

void AModule::setCursor(Gdk::CursorType const& c) {
auto cursor = Gdk::Cursor::create(c);
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) {
if (auto* module = event_box_.get_child(); module != nullptr) {
module->set_state_flags(Gtk::StateFlags::STATE_FLAG_PRELIGHT);
}

if (hasUserEvents_) {
setCursor(Gdk::HAND2);
}
return false;
}

bool AModule::handleMouseLeave(GdkEventCrossing* const& e) {
if (auto* module = event_box_.get_child(); module != nullptr) {
module->unset_state_flags(Gtk::StateFlags::STATE_FLAG_PRELIGHT);
}

if (hasUserEvents_) {
setCursor(Gdk::ARROW);
}
return false;
}

Expand Down

0 comments on commit 5d6c5ec

Please sign in to comment.