tray: Update module using callback

This commit is contained in:
patrick96 2022-09-03 21:33:22 +02:00
parent b72458a6b0
commit 1127792ccf
No known key found for this signature in database
GPG Key ID: 521E5E03AEBCA1A7
6 changed files with 31 additions and 50 deletions

View File

@ -101,9 +101,6 @@ namespace signals {
} // namespace ui } // namespace ui
namespace ui_tray { namespace ui_tray {
struct tray_width_change : public detail::base_signal<tray_width_change> {
using base_type::base_type;
};
struct tray_pos_change : public detail::value_signal<tray_pos_change, int> { struct tray_pos_change : public detail::value_signal<tray_pos_change, int> {
using base_type::base_type; using base_type::base_type;
}; };

View File

@ -35,7 +35,6 @@ namespace signals {
struct update_geometry; struct update_geometry;
} // namespace ui } // namespace ui
namespace ui_tray { namespace ui_tray {
struct tray_width_change;
struct tray_pos_change; struct tray_pos_change;
struct tray_visibility; struct tray_visibility;
} // namespace ui_tray } // namespace ui_tray

View File

@ -7,8 +7,7 @@
POLYBAR_NS POLYBAR_NS
namespace modules { namespace modules {
class tray_module : public static_module<tray_module>, class tray_module : public static_module<tray_module> {
public signal_receiver<SIGN_PRIORITY_TRAY, signals::ui_tray::tray_width_change> {
public: public:
explicit tray_module(const bar_settings& bar_settings, string name_); explicit tray_module(const bar_settings& bar_settings, string name_);
string get_format() const; string get_format() const;
@ -17,9 +16,6 @@ namespace modules {
bool build(builder* builder, const string& tag) const; bool build(builder* builder, const string& tag) const;
void update() {} void update() {}
void teardown();
bool on(const signals::ui_tray::tray_width_change& evt) override;
static constexpr auto TYPE = "internal/tray"; static constexpr auto TYPE = "internal/tray";

View File

@ -56,16 +56,15 @@ struct tray_settings {
xcb_window_t selection_owner{XCB_NONE}; xcb_window_t selection_owner{XCB_NONE};
}; };
using on_update = std::function<void(void)>;
class tray_manager : public xpp::event::sink<evt::expose, evt::visibility_notify, evt::client_message, class tray_manager : public xpp::event::sink<evt::expose, evt::visibility_notify, evt::client_message,
evt::configure_request, evt::resize_request, evt::selection_clear, evt::property_notify, evt::configure_request, evt::resize_request, evt::selection_clear, evt::property_notify,
evt::reparent_notify, evt::destroy_notify, evt::map_notify, evt::unmap_notify>, evt::reparent_notify, evt::destroy_notify, evt::map_notify, evt::unmap_notify>,
public signal_receiver<SIGN_PRIORITY_TRAY, signals::ui::update_background, public signal_receiver<SIGN_PRIORITY_TRAY, signals::ui::update_background,
signals::ui_tray::tray_pos_change, signals::ui_tray::tray_visibility> { signals::ui_tray::tray_pos_change, signals::ui_tray::tray_visibility> {
public: public:
using make_type = unique_ptr<tray_manager>; explicit tray_manager(connection& conn, signal_emitter& emitter, const logger& logger, const bar_settings& bar_opts, on_update on_update);
static make_type make(const bar_settings& bar_opts);
explicit tray_manager(connection& conn, signal_emitter& emitter, const logger& logger, const bar_settings& bar_opts);
~tray_manager(); ~tray_manager();
@ -107,7 +106,7 @@ class tray_manager : public xpp::event::sink<evt::expose, evt::visibility_notify
int calculate_client_y(); int calculate_client_y();
void update_width(unsigned new_width); void update_width();
bool is_embedded(const xcb_window_t& win) const; bool is_embedded(const xcb_window_t& win) const;
tray_client* find_client(const xcb_window_t& win); tray_client* find_client(const xcb_window_t& win);
@ -140,6 +139,8 @@ class tray_manager : public xpp::event::sink<evt::expose, evt::visibility_notify
tray_settings m_opts{}; tray_settings m_opts{};
const bar_settings& m_bar_opts; const bar_settings& m_bar_opts;
const on_update m_on_update;
/** /**
* Systray selection atom _NET_SYSTEM_TRAY_Sn * Systray selection atom _NET_SYSTEM_TRAY_Sn
*/ */

View File

@ -9,9 +9,8 @@ namespace modules {
tray_module::tray_module(const bar_settings& bar_settings, string name_) tray_module::tray_module(const bar_settings& bar_settings, string name_)
: static_module<tray_module>(bar_settings, move(name_)) : static_module<tray_module>(bar_settings, move(name_))
, m_tray(connection::make(), signal_emitter::make(), m_log, bar_settings) { , m_tray(connection::make(), signal_emitter::make(), m_log, bar_settings, [&] { this->broadcast(); }) {
m_formatter->add(DEFAULT_FORMAT, TAG_TRAY, {TAG_TRAY}); m_formatter->add(DEFAULT_FORMAT, TAG_TRAY, {TAG_TRAY});
m_sig.attach(this);
} }
string tray_module::get_format() const { string tray_module::get_format() const {
@ -33,17 +32,5 @@ namespace modules {
return false; return false;
} }
/**
* TODO Replace signal with callback passed to tray.
*/
bool tray_module::on(const signals::ui_tray::tray_width_change&) {
broadcast();
return true;
}
void tray_module::teardown() {
m_sig.detach(this);
}
} // namespace modules } // namespace modules
POLYBAR_NS_END POLYBAR_NS_END

View File

@ -25,6 +25,16 @@
* Tray implementation according to the System Tray Protocol. * Tray implementation according to the System Tray Protocol.
* *
* Ref: https://specifications.freedesktop.org/systemtray-spec/systemtray-spec-latest.html * Ref: https://specifications.freedesktop.org/systemtray-spec/systemtray-spec-latest.html
*
* This class manages embedded tray icons by placing them on the bar in the correct position; the position itself is
* requested by the renderer.
*
* The tray manager needs to trigger bar updates only when the size of the entire tray changes (e.g. when tray icons are
* added/removed). EVerything else can be handled without an update.
*
* TODO
* * Better handling of state:
* * Differentiate between, inactive, active, and waiting for selection
*/ */
// ==================================================================================================== // ====================================================================================================
@ -44,16 +54,9 @@
POLYBAR_NS POLYBAR_NS
/**
* Create instance
*/
tray_manager::make_type tray_manager::make(const bar_settings& bar_opts) {
return std::make_unique<tray_manager>(connection::make(), signal_emitter::make(), logger::make(), bar_opts);
}
tray_manager::tray_manager( tray_manager::tray_manager(
connection& conn, signal_emitter& emitter, const logger& logger, const bar_settings& bar_opts) connection& conn, signal_emitter& emitter, const logger& logger, const bar_settings& bar_opts, on_update on_update)
: m_connection(conn), m_sig(emitter), m_log(logger), m_bar_opts(bar_opts) { : m_connection(conn), m_sig(emitter), m_log(logger), m_bar_opts(bar_opts), m_on_update(on_update) {
m_connection.attach_sink(this, SINK_PRIORITY_TRAY); m_connection.attach_sink(this, SINK_PRIORITY_TRAY);
} }
@ -174,8 +177,7 @@ void tray_manager::deactivate(bool clear_selection) {
m_connection.flush(); m_connection.flush();
// TODO update through module reconfigure_window();
m_sig.emit(signals::eventqueue::notify_forcechange{});
} }
/** /**
@ -186,11 +188,7 @@ void tray_manager::reconfigure() {
return; return;
} }
try { reconfigure_window();
reconfigure_window();
} catch (const exception& err) {
m_log.err("Failed to reconfigure tray window (%s)", err.what());
}
try { try {
reconfigure_clients(); reconfigure_clients();
@ -199,23 +197,26 @@ void tray_manager::reconfigure() {
} }
m_connection.flush(); m_connection.flush();
// TODO update through module
m_sig.emit(signals::eventqueue::notify_forcechange{});
} }
/** /**
* Reconfigure container window * Reconfigure container window
*
* TODO should we call update_width directly?
*/ */
void tray_manager::reconfigure_window() { void tray_manager::reconfigure_window() {
m_log.trace("tray: Reconfigure window (hidden=%i, clients=%i)", static_cast<bool>(m_hidden), m_clients.size()); m_log.trace("tray: Reconfigure window (hidden=%i, clients=%i)", static_cast<bool>(m_hidden), m_clients.size());
update_width(calculate_w()); update_width();
} }
void tray_manager::update_width(unsigned new_width) { /**
* TODO make sure this is always called when m_clients changes
*/
void tray_manager::update_width() {
unsigned new_width = calculate_w();
if (m_tray_width != new_width) { if (m_tray_width != new_width) {
m_tray_width = new_width; m_tray_width = new_width;
m_sig.emit(signals::ui_tray::tray_width_change{}); m_on_update();
} }
} }