Adapt the renderer side to always use rgba

This commit is contained in:
patrick96 2019-10-27 22:27:58 +01:00 committed by Patrick Ziegler
parent 64fa6469ab
commit 3655106a14
9 changed files with 63 additions and 74 deletions

View File

@ -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<unsigned char>(c) / 255.0,
color_util::green_channel<unsigned char>(c) / 255.0,
color_util::blue_channel<unsigned char>(c) / 255.0,
color_util::alpha_channel<unsigned char>(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<unsigned char>(color) / 255.0,
color_util::green_channel<unsigned char>(color) / 255.0,
color_util::blue_channel<unsigned char>(color) / 255.0,
color_util::alpha_channel<unsigned char>(color) / 255.0);
cairo_pattern_add_color_stop_rgba(pattern, offset, color.r(), color.g(), color.b(), color.a());
// clang-format on
offset += step;
}

View File

@ -45,7 +45,7 @@ namespace cairo {
double y1;
double x2;
double y2;
vector<unsigned int> steps;
vector<rgba> 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;
};
}
} // namespace cairo
POLYBAR_NS_END

View File

@ -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);

View File

@ -1,6 +1,7 @@
#pragma once
#include <cairo/cairo.h>
#include <bitset>
#include <memory>
@ -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<action_block> m_actions;
bool m_fixedcenter;

View File

@ -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<change_background, unsigned int> {
struct change_background : public detail::value_signal<change_background, rgba> {
using base_type::base_type;
};
struct change_foreground : public detail::value_signal<change_foreground, unsigned int> {
struct change_foreground : public detail::value_signal<change_foreground, rgba> {
using base_type::base_type;
};
struct change_underline : public detail::value_signal<change_underline, unsigned int> {
struct change_underline : public detail::value_signal<change_underline, rgba> {
using base_type::base_type;
};
struct change_overline : public detail::value_signal<change_overline, unsigned int> {
struct change_overline : public detail::value_signal<change_overline, rgba> {
using base_type::base_type;
};
struct change_font : public detail::value_signal<change_font, int> {

View File

@ -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<evt::expose, evt::visibility_notify, evt::client_message, evt::configure_request,
evt::resize_request, evt::selection_clear, evt::property_notify, evt::reparent_notify, evt::destroy_notify,
evt::map_notify, evt::unmap_notify>,
public signal_receiver<SIGN_PRIORITY_TRAY, signals::ui::visibility_change, signals::ui::dim_window, signals::ui::update_background> {
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::reparent_notify, evt::destroy_notify, evt::map_notify, evt::unmap_notify>,
public signal_receiver<SIGN_PRIORITY_TRAY, signals::ui::visibility_change, signals::ui::dim_window,
signals::ui::update_background> {
public:
using make_type = unique_ptr<tray_manager>;
static make_type make();

View File

@ -1,6 +1,7 @@
#include "components/parser.hpp"
#include <cassert>
#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;
}

View File

@ -239,7 +239,7 @@ void renderer::begin(xcb_rectangle_t rect) {
static_cast<double>(m_rect.width),
static_cast<double>(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;
}

View File

@ -1,4 +1,7 @@
#include "x11/tray_manager.hpp"
#include <xcb/xcb_image.h>
#include <thread>
#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,7 +42,8 @@ POLYBAR_NS
* Create instance
*/
tray_manager::make_type tray_manager::make() {
return factory_util::unique<tray_manager>(connection::make(), signal_emitter::make(), logger::make(), background_manager::make());
return factory_util::unique<tray_manager>(
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)
@ -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<unsigned char>(m_opts.background) != 255) {
if (color_util::alpha_channel(m_opts.background) != 255) {
m_log.trace("tray: enable transparency");
m_opts.transparent = true;
}
@ -393,7 +390,6 @@ void tray_manager::reconfigure_bg(bool realloc) {
m_log.trace("tray: Reconfigure bg (realloc=%i)", realloc);
if (!m_context) {
return m_log.err("tray: no context for drawing the background");
}
@ -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);
@ -556,7 +553,8 @@ 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);
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");
}
@ -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