From 00274c57a936e46110cb6407b23a2d32896c39fe Mon Sep 17 00:00:00 2001
From: patrick96
Date: Fri, 22 Nov 2019 03:11:12 +0100
Subject: [PATCH] fix(renderer): Falloff gradient
Before it did not take into account borders or a tray on the left.
It also sometimes rendered the gradient way to large
---
include/cairo/context.hpp | 6 +++---
src/components/renderer.cpp | 37 ++++++++++++++++++++++++++-----------
2 files changed, 29 insertions(+), 14 deletions(-)
diff --git a/include/cairo/context.hpp b/include/cairo/context.hpp
index 8ed4f500..f6ab233e 100644
--- a/include/cairo/context.hpp
+++ b/include/cairo/context.hpp
@@ -108,10 +108,9 @@ namespace cairo {
}
context& operator<<(const linear_gradient& l) {
- if (l.steps.size() >= 2) {
+ auto stops = l.steps.size();
+ if (stops >= 2) {
auto pattern = cairo_pattern_create_linear(l.x1, l.y1, l.x2, l.y2);
- *this << pattern;
- auto stops = l.steps.size();
auto step = 1.0 / (stops - 1);
auto offset = 0.0;
for (auto&& color : l.steps) {
@@ -124,6 +123,7 @@ namespace cairo {
// clang-format on
offset += step;
}
+ *this << pattern;
cairo_pattern_destroy(pattern);
}
return *this;
diff --git a/src/components/renderer.cpp b/src/components/renderer.cpp
index 81d08b03..599f8589 100644
--- a/src/components/renderer.cpp
+++ b/src/components/renderer.cpp
@@ -341,7 +341,7 @@ void renderer::flush(alignment a) {
double w = static_cast(block_w(a) + 0.5);
double h = static_cast(block_h(a) + 0.5);
double xw = x + w;
- bool fits{xw <= m_rect.x + m_rect.width};
+ bool fits{xw <= m_rect.width};
m_log.trace("renderer: flush(%i geom=%gx%g+%g+%g, falloff=%i)", static_cast(a), w, h, x, y, !fits);
@@ -359,19 +359,34 @@ void renderer::flush(alignment a) {
*m_context << m_blocks[a].pattern;
m_context->paint();
- if (!fits) {
- // Paint falloff gradient at the end of the visible block
- // to indicate that the content expands past the canvas
- double fx = w - (xw - m_rect.width);
- double fsize = std::max(5.0, std::min(std::abs(fx), 30.0));
- m_log.trace("renderer: Drawing falloff (pos=%g, size=%g)", fx, fsize);
- *m_context << cairo::linear_gradient{fx - fsize, 0.0, fx, 0.0, {0x00000000, 0xFF000000}};
- m_context->paint(0.25);
- }
-
*m_context << cairo::abspos{0.0, 0.0};
m_context->destroy(&m_blocks[a].pattern);
m_context->restore();
+
+ if (!fits) {
+ // Paint falloff gradient at the end of the visible block
+ // to indicate that the content expands past the canvas
+
+ /*
+ * How many pixels are hidden
+ */
+ double overflow = xw - m_rect.width;
+ double visible_width = w - overflow;
+
+ /*
+ * Width of the falloff gradient. Depends on how much of the block is hidden
+ */
+ double fsize = std::max(5.0, std::min(std::abs(overflow), 30.0));
+ m_log.trace("renderer: Drawing falloff (pos=%g, size=%g, overflow=%g)", visible_width - fsize, fsize, overflow);
+ m_context->save();
+ *m_context << cairo::translate{(double) m_rect.x, (double) m_rect.y};
+ *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->paint(0.25);
+ m_context->restore();
+ }
}
/**