Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hover actions #2868

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `custom/text`: The `content` setting and all its properties are deprecated in favor of `format` with the same functionality. ([`#2676`](https://github.com/polybar/polybar/pull/2676))

### Added
- Added support for TAG_LABEL (`<label>`) in ipc module ([`#2841`](https://github.com/polybar/polybar/pull/2841)) by [@madhavpcm](https://github.com/madhavpcm).
- Added support for format-i for each hook-i defined in ipc module ([`#2775`](https://github.com/polybar/polybar/issues/2775), [`#2810`](https://github.com/polybar/polybar/pull/2810)) by [@madhavpcm](https://github.com/madhavpcm).
- Support for actions on hover via action numbers `9` and `10` ([`#1064`](https://github.com/polybar/polybar/issues/1064), [`#2868`](https://github.com/polybar/polybar/pull/2868)).
- `custom/script`: `hover-start` and `hover-end` actions ([`#1064`](https://github.com/polybar/polybar/issues/1064), [`#2868`](https://github.com/polybar/polybar/pull/2868)).
- Added support for TAG_LABEL (`<label>`) in ipc module ([`#2841`](https://github.com/polybar/polybar/pull/2841)) by [@madhavpcm](https://github.com/madhavpcm).
- Added support for format-i for each hook-i defined in ipc module ([`#2775`](https://github.com/polybar/polybar/issues/2775), [`#2810`](https://github.com/polybar/polybar/pull/2810)) by [@madhavpcm](https://github.com/madhavpcm).
- `internal/temperature`: `%temperature-k%` token displays the temperature in kelvin ([`#2774`](https://github.com/polybar/polybar/discussions/2774), [`#2784`](https://github.com/polybar/polybar/pull/2784))
- `internal/pulseaudio`: `reverse-scroll` option ([`#2664`](https://github.com/polybar/polybar/pull/2664))
- `custom/script`: Repeat interval for script failure (`interval-fail`) and `exec-if` (`interval-if`) ([`#943`](https://github.com/polybar/polybar/issues/943), [`#2606`](https://github.com/polybar/polybar/issues/2606), [`#2630`](https://github.com/polybar/polybar/pull/2630))
Expand Down
6 changes: 6 additions & 0 deletions include/components/bar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ class bar : public xpp::event::sink<evt::button_press, evt::expose, evt::propert
*/
string m_cursor{};

/**
* Action string of last hover action
*/
string m_last_start_hover_action{};
string m_last_end_hover_action{};

string m_lastinput{};
std::set<mousebtn> m_dblclicks;

Expand Down
4 changes: 4 additions & 0 deletions include/components/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ enum class mousebtn {
DOUBLE_LEFT,
DOUBLE_MIDDLE,
DOUBLE_RIGHT,
HOVER_START,
HOVER_END,
// Terminator value, do not use
BTN_COUNT,
};
Expand Down Expand Up @@ -224,6 +226,8 @@ struct bar_settings {

vector<action> actions{};

bool enable_hover_actions{false};

bool dimmed{false};
double dimvalue{1.0};

Expand Down
53 changes: 48 additions & 5 deletions src/components/bar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ bar::bar(connection& conn, signal_emitter& emitter, const config& config, const

m_opts.struts = m_conf.get(bs, "enable-struts", m_opts.struts);

m_opts.enable_hover_actions = m_conf.get(bs, "enable-hover", m_opts.enable_hover_actions);

if (only_initialize_values) {
return;
}
Expand Down Expand Up @@ -424,7 +426,8 @@ void bar::parse(string&& data, bool force) {

m_dblclicks.clear();
for (auto&& action : m_opts.actions) {
if (static_cast<int>(action.button) >= static_cast<int>(mousebtn::DOUBLE_LEFT)) {
if (action.button == mousebtn::DOUBLE_LEFT || action.button == mousebtn::DOUBLE_MIDDLE ||
action.button == mousebtn::DOUBLE_RIGHT) {
m_dblclicks.insert(action.button);
}
}
Expand Down Expand Up @@ -732,17 +735,57 @@ void bar::handle(const evt::leave_notify&) {
});
}
}

if (!m_last_end_hover_action.empty() && m_opts.enable_hover_actions) {
m_sig.emit(button_press{m_last_end_hover_action});
}

m_last_end_hover_action = ""s;
m_last_start_hover_action = ""s;
}

/**
* Event handler for XCB_MOTION_NOTIFY events
*
* Used to change the cursor depending on the module
* Used to change the cursor depending on the module and handle hover actions
*/
void bar::handle(const evt::motion_notify& evt) {
m_log.trace("bar: Detected motion: %i at pos(%i, %i)", evt->detail, evt->event_x, evt->event_y);
#if WITH_XCURSOR

int motion_pos = evt->event_x;

const auto get_hover_str = [&](const mousebtn& button) -> string {
if (!m_opts.enable_hover_actions) {
return ""s;
}

tags::action_t action = m_action_ctxt->has_action(button, motion_pos);
if (action != tags::NO_ACTION) {
m_log.trace("Found matching input area");
return m_action_ctxt->get_action(action);
}

return ""s;
};

string hover_start_action = get_hover_str(mousebtn::HOVER_START);
string hover_end_action = get_hover_str(mousebtn::HOVER_END);

if (hover_start_action != m_last_start_hover_action || hover_end_action != m_last_end_hover_action) {
m_log.trace("bar: Hover changed");
if (!m_last_end_hover_action.empty()) {
m_sig.emit(button_press{m_last_end_hover_action});
Astrono2 marked this conversation as resolved.
Show resolved Hide resolved
}

if (!hover_start_action.empty()) {
m_sig.emit(button_press{hover_start_action});
}

m_last_start_hover_action = hover_start_action;
m_last_end_hover_action = hover_end_action;
}

#if WITH_XCURSOR
// scroll cursor is less important than click cursor, so we shouldn't return until we are sure there is no click
// action
bool found_scroll = false;
Expand Down Expand Up @@ -898,10 +941,10 @@ void bar::start(const string& tray_module_name) {

// Subscribe to window enter and leave events
// if we should dim the window
if (m_opts.dimvalue != 1.0) {
if (m_opts.dimvalue != 1.0 || m_opts.enable_hover_actions) {
m_connection.ensure_event_mask(m_opts.window, XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW);
}
if (!m_opts.cursor_click.empty() || !m_opts.cursor_scroll.empty()) {
if (!m_opts.cursor_click.empty() || !m_opts.cursor_scroll.empty() || m_opts.enable_hover_actions) {
m_connection.ensure_event_mask(m_opts.window, XCB_EVENT_MASK_POINTER_MOTION);
}
m_connection.ensure_event_mask(m_opts.window, XCB_EVENT_MASK_STRUCTURE_NOTIFY);
Expand Down
12 changes: 12 additions & 0 deletions src/modules/script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ namespace modules {
m_actions[mousebtn::SCROLL_UP] = m_conf.get(name(), "scroll-up", ""s);
m_actions[mousebtn::SCROLL_DOWN] = m_conf.get(name(), "scroll-down", ""s);

if (bar.enable_hover_actions) {
m_actions[mousebtn::HOVER_START] = m_conf.get(name(), "hover-start", ""s);
m_actions[mousebtn::HOVER_END] = m_conf.get(name(), "hover-end", ""s);
} else {
if (m_conf.has(name(), "hover-start")) {
m_log.warn("Hover checking is disabled, ignoring hover-start");
}
if (m_conf.has(name(), "hover-end")) {
m_log.warn("Hover checking is disabled, ignoring hover-end");
}
}

// Setup formatting
m_formatter->add(DEFAULT_FORMAT, TAG_LABEL, {TAG_LABEL});
if (m_formatter->has(TAG_LABEL)) {
Expand Down
23 changes: 13 additions & 10 deletions src/tags/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,20 +402,23 @@ namespace tags {
* May return mousebtn::NONE if no button was given.
*/
mousebtn parser::parse_action_btn() {
if (has_next()) {
if (isdigit(peek())) {
char c = next();
int num = c - '0';
int num = 0;
string s;
while (has_next() && isdigit(peek())) {
s += next();
}

if (num < static_cast<int>(mousebtn::NONE) || num >= static_cast<int>(mousebtn::BTN_COUNT)) {
throw btn_error(string{c});
}
if(s.empty()) {
return mousebtn::NONE;
}

num = std::stoi(s, nullptr, 10);

return static_cast<mousebtn>(num);
}
if (num < static_cast<int>(mousebtn::NONE) || num >= static_cast<int>(mousebtn::BTN_COUNT)) {
throw btn_error(std::to_string(num));
}

return mousebtn::NONE;
return static_cast<mousebtn>(num);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion tests/unit_tests/tags/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ vector<single_action> parse_single_action_list = {
{"%{A6:cmd:}", mousebtn::DOUBLE_LEFT, "cmd"},
{"%{A7:cmd:}", mousebtn::DOUBLE_MIDDLE, "cmd"},
{"%{A8:cmd:}", mousebtn::DOUBLE_RIGHT, "cmd"},
{"%{A9:cmd:}", mousebtn::HOVER_START, "cmd"},
{"%{A10:cmd:}", mousebtn::HOVER_END, "cmd"},
{"%{A}", mousebtn::NONE, ""},
{"%{A1}", mousebtn::LEFT, ""},
{"%{A2}", mousebtn::MIDDLE, ""},
Expand All @@ -234,6 +236,8 @@ vector<single_action> parse_single_action_list = {
{"%{A6}", mousebtn::DOUBLE_LEFT, ""},
{"%{A7}", mousebtn::DOUBLE_MIDDLE, ""},
{"%{A8}", mousebtn::DOUBLE_RIGHT, ""},
{"%{A9}", mousebtn::HOVER_START, ""},
{"%{A10}", mousebtn::HOVER_END, ""},
{"%{A1:a\\:b:}", mousebtn::LEFT, "a:b"},
{"%{A1:\\:\\:\\::}", mousebtn::LEFT, ":::"},
{"%{A1:#apps.open.0:}", mousebtn::LEFT, "#apps.open.0"},
Expand Down Expand Up @@ -444,7 +448,7 @@ vector<exception_test> parse_error_test = {
{"%{O0ptx}", exc::OFFSET},
{"%{O0a}", exc::OFFSET},
{"%{A2:cmd:cmd:}", exc::TAG_END},
{"%{A9}", exc::BTN},
{"%{A11}", exc::BTN},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe actually use mousebtn::BTN_COUNT in this string directly so that we don't have to manually update it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this be a good way of doing it?

std::string invalid_mouse_btn = std::to_string(static_cast<int>(mousebtn::BTN_COUNT));

...

{"%{A" + invalid_mouse_btn + "}", exc::BTN},

{"%{rQ}", exc::TAG_END},
};

Expand Down