From 1127792ccf45640e9509154c9419ca500af915fe Mon Sep 17 00:00:00 2001 From: patrick96 Date: Sat, 3 Sep 2022 21:33:22 +0200 Subject: [PATCH] tray: Update module using callback --- include/events/signal.hpp | 3 --- include/events/signal_fwd.hpp | 1 - include/modules/tray.hpp | 6 +---- include/x11/tray_manager.hpp | 11 +++++---- src/modules/tray.cpp | 15 +----------- src/x11/tray_manager.cpp | 45 ++++++++++++++++++----------------- 6 files changed, 31 insertions(+), 50 deletions(-) diff --git a/include/events/signal.hpp b/include/events/signal.hpp index a91d8505..f25f92d4 100644 --- a/include/events/signal.hpp +++ b/include/events/signal.hpp @@ -101,9 +101,6 @@ namespace signals { } // namespace ui namespace ui_tray { - struct tray_width_change : public detail::base_signal { - using base_type::base_type; - }; struct tray_pos_change : public detail::value_signal { using base_type::base_type; }; diff --git a/include/events/signal_fwd.hpp b/include/events/signal_fwd.hpp index 5696beeb..7d4e26e4 100644 --- a/include/events/signal_fwd.hpp +++ b/include/events/signal_fwd.hpp @@ -35,7 +35,6 @@ namespace signals { struct update_geometry; } // namespace ui namespace ui_tray { - struct tray_width_change; struct tray_pos_change; struct tray_visibility; } // namespace ui_tray diff --git a/include/modules/tray.hpp b/include/modules/tray.hpp index dbf621f4..19b42f65 100644 --- a/include/modules/tray.hpp +++ b/include/modules/tray.hpp @@ -7,8 +7,7 @@ POLYBAR_NS namespace modules { - class tray_module : public static_module, - public signal_receiver { + class tray_module : public static_module { public: explicit tray_module(const bar_settings& bar_settings, string name_); string get_format() const; @@ -17,9 +16,6 @@ namespace modules { bool build(builder* builder, const string& tag) const; void update() {} - void teardown(); - - bool on(const signals::ui_tray::tray_width_change& evt) override; static constexpr auto TYPE = "internal/tray"; diff --git a/include/x11/tray_manager.hpp b/include/x11/tray_manager.hpp index d79b9183..0bf4dee9 100644 --- a/include/x11/tray_manager.hpp +++ b/include/x11/tray_manager.hpp @@ -56,16 +56,15 @@ struct tray_settings { xcb_window_t selection_owner{XCB_NONE}; }; +using on_update = std::function; + class tray_manager : public xpp::event::sink, public signal_receiver { public: - using make_type = unique_ptr; - 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); + explicit tray_manager(connection& conn, signal_emitter& emitter, const logger& logger, const bar_settings& bar_opts, on_update on_update); ~tray_manager(); @@ -107,7 +106,7 @@ class tray_manager : public xpp::event::sink(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_sig.attach(this); } string tray_module::get_format() const { @@ -33,17 +32,5 @@ namespace modules { 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 POLYBAR_NS_END diff --git a/src/x11/tray_manager.cpp b/src/x11/tray_manager.cpp index 6638713b..c8770690 100644 --- a/src/x11/tray_manager.cpp +++ b/src/x11/tray_manager.cpp @@ -25,6 +25,16 @@ * Tray implementation according to the System Tray Protocol. * * 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 -/** - * Create instance - */ -tray_manager::make_type tray_manager::make(const bar_settings& bar_opts) { - return std::make_unique(connection::make(), signal_emitter::make(), logger::make(), bar_opts); -} - tray_manager::tray_manager( - connection& conn, signal_emitter& emitter, const logger& logger, const bar_settings& bar_opts) - : m_connection(conn), m_sig(emitter), m_log(logger), m_bar_opts(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_on_update(on_update) { m_connection.attach_sink(this, SINK_PRIORITY_TRAY); } @@ -174,8 +177,7 @@ void tray_manager::deactivate(bool clear_selection) { m_connection.flush(); - // TODO update through module - m_sig.emit(signals::eventqueue::notify_forcechange{}); + reconfigure_window(); } /** @@ -186,11 +188,7 @@ void tray_manager::reconfigure() { return; } - try { - reconfigure_window(); - } catch (const exception& err) { - m_log.err("Failed to reconfigure tray window (%s)", err.what()); - } + reconfigure_window(); try { reconfigure_clients(); @@ -199,23 +197,26 @@ void tray_manager::reconfigure() { } m_connection.flush(); - - // TODO update through module - m_sig.emit(signals::eventqueue::notify_forcechange{}); } /** * Reconfigure container window + * + * TODO should we call update_width directly? */ void tray_manager::reconfigure_window() { m_log.trace("tray: Reconfigure window (hidden=%i, clients=%i)", static_cast(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) { m_tray_width = new_width; - m_sig.emit(signals::ui_tray::tray_width_change{}); + m_on_update(); } }