feat: Fade if unfocused and handle double clicks

This commit is contained in:
Michael Carlberg 2016-12-16 06:44:55 +01:00
parent d94d8ccfd3
commit a7005be82c
5 changed files with 63 additions and 3 deletions

View File

@ -22,7 +22,7 @@ class screen;
class tray_manager;
class logger;
class bar : public xpp::event::sink<evt::button_press, evt::expose, evt::property_notify> {
class bar : public xpp::event::sink<evt::button_press, evt::expose, evt::property_notify, evt::enter_notify, evt::leave_notify> {
public:
using make_type = unique_ptr<bar>;
static make_type make();
@ -43,6 +43,8 @@ class bar : public xpp::event::sink<evt::button_press, evt::expose, evt::propert
void reconfigure_wm_hints();
void broadcast_visibility();
void handle(const evt::enter_notify& evt);
void handle(const evt::leave_notify& evt);
void handle(const evt::button_press& evt);
void handle(const evt::expose& evt);
void handle(const evt::property_notify& evt);
@ -63,6 +65,7 @@ class bar : public xpp::event::sink<evt::button_press, evt::expose, evt::propert
std::mutex m_mutex{};
event_timer m_buttonpress{0L, 5L};
event_timer m_doubleclick{0L, 250L};
};
POLYBAR_NS_END

View File

@ -26,7 +26,7 @@ enum class syntaxtag : uint8_t {
u, // underline color
};
enum class mousebtn : uint8_t { NONE = 0U, LEFT, MIDDLE, RIGHT, SCROLL_UP, SCROLL_DOWN };
enum class mousebtn : uint8_t { NONE = 0U, LEFT, MIDDLE, RIGHT, SCROLL_UP, SCROLL_DOWN, DOUBLE_CLICK };
enum class strut : uint16_t {
LEFT = 0U,
@ -127,6 +127,9 @@ struct bar_settings {
vector<action> actions{};
bool dimmed{false};
double dimvalue{1.0};
const xcb_rectangle_t inner_area(bool abspos = false) const {
xcb_rectangle_t rect{0, 0, size.w, size.h};

View File

@ -13,6 +13,7 @@ namespace wm_util {
void set_wm_state(xcb_connection_t* conn, xcb_window_t win, vector<xcb_atom_t> states);
void set_wm_pid(xcb_connection_t* conn, xcb_window_t win, pid_t pid);
void set_wm_desktop(xcb_connection_t* conn, xcb_window_t win, uint32_t desktop = -1u);
void set_wm_window_opacity(xcb_connection_t* conn, xcb_window_t win, uint64_t value);
}
POLYBAR_NS_END

View File

@ -93,6 +93,9 @@ bar::bar(connection& conn, signal_emitter& emitter, const config& config, const
m_opts.override_redirect = m_conf.get<bool>(bs, "override-redirect", m_opts.override_redirect);
}
m_opts.dimvalue = m_conf.get<double>(bs, "dim-value", 1.0);
m_opts.dimvalue = math_util::cap(m_opts.dimvalue, 0.0, 1.0);
// Build WM_NAME
m_opts.wmname = m_conf.get<string>(bs, "wm-name", "polybar-" + bs.substr(4) + "_" + m_opts.monitor->name);
m_opts.wmname = string_util::replace(m_opts.wmname, " ", "-");
@ -120,6 +123,7 @@ bar::bar(connection& conn, signal_emitter& emitter, const config& config, const
actions.emplace_back(action{mousebtn::RIGHT, m_conf.get<string>(bs, "click-right", "")});
actions.emplace_back(action{mousebtn::SCROLL_UP, m_conf.get<string>(bs, "scroll-up", "")});
actions.emplace_back(action{mousebtn::SCROLL_DOWN, m_conf.get<string>(bs, "scroll-down", "")});
actions.emplace_back(action{mousebtn::DOUBLE_CLICK, m_conf.get<string>(bs, "click-double", "")});
for (auto&& act : actions) {
if (!act.command.empty()) {
@ -217,6 +221,12 @@ bar::bar(connection& conn, signal_emitter& emitter, const config& config, const
m_log.info("Bar geometry: %ix%i+%i+%i", m_opts.size.w, m_opts.size.h, m_opts.pos.x, m_opts.pos.y);
m_opts.window = m_renderer->window();
// Subscribe to window enter and leave events
// if we should dim the window
if (m_opts.dimvalue != 1.0) {
m_connection.ensure_event_mask(m_opts.window, XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW);
}
m_log.info("Bar window: %s", m_connection.id(m_opts.window));
restack_window();
@ -405,6 +415,34 @@ void bar::broadcast_visibility() {
}
}
/**
* Event handler for XCB_ENTER_NOTIFY events
*
* Used to brighten the window by setting the
* _NET_WM_WINDOW_OPACITY atom value
*/
void bar::handle(const evt::enter_notify&) {
if (m_opts.dimmed) {
window win{m_connection, m_opts.window};
wm_util::set_wm_window_opacity(m_connection, m_opts.window, 1.0 * 0xFFFFFFFF);
m_opts.dimmed = false;
}
}
/**
* Event handler for XCB_LEAVE_NOTIFY events
*
* Used to dim the window by setting the
* _NET_WM_WINDOW_OPACITY atom value
*/
void bar::handle(const evt::leave_notify&) {
if (!m_opts.dimmed) {
window win{m_connection, m_opts.window};
wm_util::set_wm_window_opacity(m_connection, m_opts.window, m_opts.dimvalue * 0xFFFFFFFF);
m_opts.dimmed = true;
}
}
/**
* Event handler for XCB_BUTTON_PRESS events
*
@ -422,8 +460,13 @@ void bar::handle(const evt::button_press& evt) {
}
m_log.trace("bar: Received button press: %i at pos(%i, %i)", evt->detail, evt->event_x, evt->event_y);
mousebtn button{static_cast<mousebtn>(evt->detail)};
const mousebtn button{static_cast<mousebtn>(evt->detail)};
// Using the event_timer so if the event gets denied
// we are within the double click time window
if (button == mousebtn::LEFT && m_doubleclick.deny(evt->time)) {
button = mousebtn::DOUBLE_CLICK;
}
for (auto&& action : m_renderer->get_actions()) {
if (action.active) {

View File

@ -1,6 +1,7 @@
#include <unistd.h>
#include <xcb/xcb_icccm.h>
#include "utils/math.hpp"
#include "x11/atoms.hpp"
#include "x11/wm.hpp"
@ -35,6 +36,15 @@ namespace wm_util {
const uint32_t value_list[1]{desktop};
xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win, _NET_WM_DESKTOP, XCB_ATOM_CARDINAL, 32, 1, value_list);
}
void set_wm_window_opacity(xcb_connection_t* conn, xcb_window_t win, uint64_t values) {
xcb_intern_atom_reply_t* reply{xcb_intern_atom_reply(conn, xcb_intern_atom(conn, false, 22, "_NET_WM_WINDOW_OPACITY"), nullptr)};
if (reply) {
xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win, reply->atom, XCB_ATOM_CARDINAL, 32, 1, &values);
xcb_flush(conn);
free(reply);
}
}
}
POLYBAR_NS_END