From 3655106a1453bb5834101957e40952a6eb65fbf8 Mon Sep 17 00:00:00 2001 From: patrick96 Date: Sun, 27 Oct 2019 22:27:58 +0100 Subject: [PATCH] Adapt the renderer side to always use rgba --- include/cairo/context.hpp | 19 ++------------ include/cairo/types.hpp | 10 +++---- include/components/parser.hpp | 3 ++- include/components/renderer.hpp | 9 ++++--- include/events/signal.hpp | 9 +++---- include/x11/tray_manager.hpp | 12 ++++----- src/components/parser.cpp | 13 +++++++--- src/components/renderer.cpp | 16 +++++------- src/x11/tray_manager.cpp | 46 ++++++++++++++++----------------- 9 files changed, 63 insertions(+), 74 deletions(-) diff --git a/include/cairo/context.hpp b/include/cairo/context.hpp index fe6706d2..47d54f6c 100644 --- a/include/cairo/context.hpp +++ b/include/cairo/context.hpp @@ -56,17 +56,6 @@ namespace cairo { return *this; } - context& operator<<(const unsigned int& c) { - // clang-format off - cairo_set_source_rgba(m_c, - color_util::red_channel(c) / 255.0, - color_util::green_channel(c) / 255.0, - color_util::blue_channel(c) / 255.0, - color_util::alpha_channel(c) / 255.0); - // clang-format on - return *this; - } - context& operator<<(const abspos& p) { if (p.clear) { cairo_new_path(m_c); @@ -81,7 +70,7 @@ namespace cairo { } context& operator<<(const rgba& f) { - cairo_set_source_rgba(m_c, f.r, f.g, f.b, f.a); + cairo_set_source_rgba(m_c, f.r(), f.g(), f.b(), f.a()); return *this; } @@ -116,11 +105,7 @@ namespace cairo { auto offset = 0.0; for (auto&& color : l.steps) { // clang-format off - cairo_pattern_add_color_stop_rgba(pattern, offset, - color_util::red_channel(color) / 255.0, - color_util::green_channel(color) / 255.0, - color_util::blue_channel(color) / 255.0, - color_util::alpha_channel(color) / 255.0); + cairo_pattern_add_color_stop_rgba(pattern, offset, color.r(), color.g(), color.b(), color.a()); // clang-format on offset += step; } diff --git a/include/cairo/types.hpp b/include/cairo/types.hpp index 42e9111a..d0d81970 100644 --- a/include/cairo/types.hpp +++ b/include/cairo/types.hpp @@ -45,7 +45,7 @@ namespace cairo { double y1; double x2; double y2; - vector steps; + vector steps; }; struct rounded_corners { double x; @@ -58,12 +58,12 @@ namespace cairo { alignment align; string contents; int font; - unsigned int bg; + rgba bg{}; cairo_operator_t bg_operator; rect bg_rect; - double *x_advance; - double *y_advance; + double* x_advance; + double* y_advance; }; -} +} // namespace cairo POLYBAR_NS_END diff --git a/include/components/parser.hpp b/include/components/parser.hpp index 25b69c99..c2b9ec90 100644 --- a/include/components/parser.hpp +++ b/include/components/parser.hpp @@ -2,6 +2,7 @@ #include "common.hpp" #include "errors.hpp" +#include "utils/color.hpp" POLYBAR_NS @@ -29,7 +30,7 @@ class parser { void codeblock(string&& data, const bar_settings& bar); size_t text(string&& data); - static unsigned int parse_color(const string& s, unsigned int fallback = 0); + static rgba parse_color(const string& s, rgba fallback = rgba{0}); static int parse_fontindex(const string& s); static attribute parse_attr(const char attr); mousebtn parse_action_btn(const string& data); diff --git a/include/components/renderer.hpp b/include/components/renderer.hpp index 4d507fec..4d7e56af 100644 --- a/include/components/renderer.hpp +++ b/include/components/renderer.hpp @@ -1,6 +1,7 @@ #pragma once #include + #include #include @@ -128,10 +129,10 @@ class renderer alignment m_align; std::bitset<3> m_attr; int m_font{0}; - unsigned int m_bg{0U}; - unsigned int m_fg{0U}; - unsigned int m_ol{0U}; - unsigned int m_ul{0U}; + rgba m_bg{0U}; + rgba m_fg{0U}; + rgba m_ol{0U}; + rgba m_ul{0U}; vector m_actions; bool m_fixedcenter; diff --git a/include/events/signal.hpp b/include/events/signal.hpp index d873925a..dde362b4 100644 --- a/include/events/signal.hpp +++ b/include/events/signal.hpp @@ -1,7 +1,6 @@ #pragma once #include "common.hpp" - #include "components/ipc.hpp" #include "components/parser.hpp" #include "components/types.hpp" @@ -131,16 +130,16 @@ namespace signals { } // namespace ui_tray namespace parser { - struct change_background : public detail::value_signal { + struct change_background : public detail::value_signal { using base_type::base_type; }; - struct change_foreground : public detail::value_signal { + struct change_foreground : public detail::value_signal { using base_type::base_type; }; - struct change_underline : public detail::value_signal { + struct change_underline : public detail::value_signal { using base_type::base_type; }; - struct change_overline : public detail::value_signal { + struct change_overline : public detail::value_signal { using base_type::base_type; }; struct change_font : public detail::value_signal { diff --git a/include/x11/tray_manager.hpp b/include/x11/tray_manager.hpp index 6da0d9bd..1228c3a9 100644 --- a/include/x11/tray_manager.hpp +++ b/include/x11/tray_manager.hpp @@ -59,16 +59,16 @@ struct tray_settings { unsigned int height_fill{0U}; unsigned int spacing{0U}; unsigned int sibling{0U}; - unsigned int background{0U}; + rgba background{}; bool transparent{false}; bool detached{false}; }; -class tray_manager - : public xpp::event::sink, - public signal_receiver { +class tray_manager : public xpp::event::sink, + public signal_receiver { public: using make_type = unique_ptr; static make_type make(); diff --git a/src/components/parser.cpp b/src/components/parser.cpp index 05693dde..51d75743 100644 --- a/src/components/parser.cpp +++ b/src/components/parser.cpp @@ -1,6 +1,7 @@ +#include "components/parser.hpp" + #include -#include "components/parser.hpp" #include "components/types.hpp" #include "events/signal.hpp" #include "events/signal_emitter.hpp" @@ -218,9 +219,15 @@ size_t parser::text(string&& data) { /** * Process color hex string and convert it to the correct value */ -unsigned int parser::parse_color(const string& s, unsigned int fallback) { +rgba parser::parse_color(const string& s, rgba fallback) { if (!s.empty() && s[0] != '-') { - return color_util::parse(s, fallback); + rgba ret = rgba{s}; + + if (!ret.has_color() || ret.m_type == rgba::ALPHA_ONLY) { + return fallback; + } + + return ret; } return fallback; } diff --git a/src/components/renderer.cpp b/src/components/renderer.cpp index b0aa392e..59f7d612 100644 --- a/src/components/renderer.cpp +++ b/src/components/renderer.cpp @@ -239,7 +239,7 @@ void renderer::begin(xcb_rectangle_t rect) { static_cast(m_rect.width), static_cast(m_rect.height), m_bar.radius}; // clang-format on - *m_context << rgba{1.0, 1.0, 1.0, 1.0}; + *m_context << rgba{0xffffffff}; m_context->fill(); m_context->pop(&m_cornermask); m_context->restore(); @@ -383,7 +383,7 @@ void renderer::flush(alignment a) { *m_context << cairo::abspos{0.0, 0.0}; *m_context << cairo::rect{x + visible_width - fsize, y, fsize, h}; m_context->clip(true); - *m_context << cairo::linear_gradient{x + visible_width - fsize, y, x + visible_width, y, {0x00000000, 0xFF000000}}; + *m_context << cairo::linear_gradient{x + visible_width - fsize, y, x + visible_width, y, {rgba{0x00000000}, rgba{0xFF000000}}}; m_context->paint(0.25); m_context->restore(); } @@ -742,7 +742,7 @@ bool renderer::on(const signals::ui::request_snapshot& evt) { } bool renderer::on(const signals::parser::change_background& evt) { - const unsigned int color{evt.cast()}; + const rgba color{evt.cast()}; if (color != m_bg) { m_log.trace_x("renderer: change_background(#%08x)", color); m_bg = color; @@ -751,7 +751,7 @@ bool renderer::on(const signals::parser::change_background& evt) { } bool renderer::on(const signals::parser::change_foreground& evt) { - const unsigned int color{evt.cast()}; + const rgba color{evt.cast()}; if (color != m_fg) { m_log.trace_x("renderer: change_foreground(#%08x)", color); m_fg = color; @@ -760,7 +760,7 @@ bool renderer::on(const signals::parser::change_foreground& evt) { } bool renderer::on(const signals::parser::change_underline& evt) { - const unsigned int color{evt.cast()}; + const rgba color{evt.cast()}; if (color != m_ul) { m_log.trace_x("renderer: change_underline(#%08x)", color); m_ul = color; @@ -769,7 +769,7 @@ bool renderer::on(const signals::parser::change_underline& evt) { } bool renderer::on(const signals::parser::change_overline& evt) { - const unsigned int color{evt.cast()}; + const rgba color{evt.cast()}; if (color != m_ol) { m_log.trace_x("renderer: change_overline(#%08x)", color); m_ol = color; @@ -809,9 +809,7 @@ bool renderer::on(const signals::parser::change_alignment& evt) { bool renderer::on(const signals::parser::reverse_colors&) { m_log.trace_x("renderer: reverse_colors"); - m_fg = m_fg + m_bg; - m_bg = m_fg - m_bg; - m_fg = m_fg - m_bg; + std::swap(m_fg, m_bg); return true; } diff --git a/src/x11/tray_manager.cpp b/src/x11/tray_manager.cpp index f9da35fe..13a09ab9 100644 --- a/src/x11/tray_manager.cpp +++ b/src/x11/tray_manager.cpp @@ -1,4 +1,7 @@ +#include "x11/tray_manager.hpp" + #include + #include #include "cairo/context.hpp" @@ -14,7 +17,6 @@ #include "x11/background_manager.hpp" #include "x11/ewmh.hpp" #include "x11/icccm.hpp" -#include "x11/tray_manager.hpp" #include "x11/window.hpp" #include "x11/winspec.hpp" #include "x11/xembed.hpp" @@ -40,11 +42,12 @@ POLYBAR_NS * Create instance */ tray_manager::make_type tray_manager::make() { - return factory_util::unique(connection::make(), signal_emitter::make(), logger::make(), background_manager::make()); + return factory_util::unique( + connection::make(), signal_emitter::make(), logger::make(), background_manager::make()); } tray_manager::tray_manager(connection& conn, signal_emitter& emitter, const logger& logger, background_manager& back) - : m_connection(conn), m_sig(emitter), m_log(logger), m_background_manager(back) { + : m_connection(conn), m_sig(emitter), m_log(logger), m_background_manager(back) { m_connection.attach_sink(this, SINK_PRIORITY_TRAY); } @@ -125,15 +128,9 @@ void tray_manager::setup(const bar_settings& bar_opts) { } // Set user-defined background color - auto bg = conf.get(bs, "tray-background", ""s); + m_opts.background = conf.get(bs, "tray-background", bar_opts.background); - if (!bg.empty()) { - m_opts.background = color_util::parse(bg); - } else { - m_opts.background = bar_opts.background; - } - - if (color_util::alpha_channel(m_opts.background) != 255) { + if (color_util::alpha_channel(m_opts.background) != 255) { m_log.trace("tray: enable transparency"); m_opts.transparent = true; } @@ -393,13 +390,12 @@ void tray_manager::reconfigure_bg(bool realloc) { m_log.trace("tray: Reconfigure bg (realloc=%i)", realloc); - - if(!m_context) { + if (!m_context) { return m_log.err("tray: no context for drawing the background"); } cairo::surface* surface = m_bg_slice->get_surface(); - if(!surface) { + if (!surface) { return m_log.err("tray: no root surface"); } @@ -431,7 +427,8 @@ void tray_manager::refresh_window() { m_connection.poly_fill_rectangle(m_pixmap, m_gc, 1, &rect); } - if(m_surface) m_surface->flush(); + if (m_surface) + m_surface->flush(); m_connection.clear_area(0, m_tray, 0, 0, width, height); @@ -522,10 +519,10 @@ void tray_manager::create_bg(bool realloc) { m_gc = 0; } - if(realloc && m_surface) { + if (realloc && m_surface) { m_surface.release(); } - if(realloc && m_context) { + if (realloc && m_context) { m_context.release(); } @@ -555,15 +552,16 @@ void tray_manager::create_bg(bool realloc) { } } - if(!m_surface) { - xcb_visualtype_t* visual = m_connection.visual_type_for_id(m_connection.screen(), m_connection.screen()->root_visual); - if(!visual) { + if (!m_surface) { + xcb_visualtype_t* visual = + m_connection.visual_type_for_id(m_connection.screen(), m_connection.screen()->root_visual); + if (!visual) { return m_log.err("Failed to get root visual for tray background"); } m_surface = make_unique(m_connection, m_pixmap, visual, w, h); } - if(!m_context) { + if (!m_context) { m_context = make_unique(*m_surface, m_log); m_context->clear(); *m_context << CAIRO_OPERATOR_SOURCE << m_opts.background; @@ -640,9 +638,9 @@ void tray_manager::set_wm_hints() { void tray_manager::set_tray_colors() { m_log.trace("tray: Set _NET_SYSTEM_TRAY_COLORS to %x", m_opts.background); - auto r = color_util::red_channel(m_opts.background); - auto g = color_util::green_channel(m_opts.background); - auto b = color_util::blue_channel(m_opts.background); + auto r = m_opts.background.r_int(); + auto g = m_opts.background.g_int(); + auto b = m_opts.background.b_int(); const unsigned int colors[12] = { r, g, b, // normal