Skip to content

Commit 487fe23

Browse files
committed
refactor: removed web_page dependency from draw_buffer class
1 parent 4bedf4b commit 487fe23

File tree

4 files changed

+74
-62
lines changed

4 files changed

+74
-62
lines changed

support/gtkmm4/html_widget.cpp

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ void html_widget::scroll_to(int x, int y)
203203

204204
void html_widget::on_redraw()
205205
{
206-
m_draw_buffer.redraw(current_page());
206+
m_draw_buffer.redraw(get_draw_function(current_page()));
207207
queue_draw();
208208
}
209209

@@ -416,16 +416,16 @@ void html_widget::size_allocate_vfunc(int width, int height, int /* baseline */)
416416
{
417417
m_rendered_width = width;
418418
m_rendered_height = height;
419-
m_draw_buffer.on_size_allocate(page, width, height);
419+
m_draw_buffer.on_size_allocate(get_draw_function(page), width, height);
420420
page->media_changed();
421421
page->render(m_rendered_width);
422422
update_view_port(page);
423-
m_draw_buffer.redraw(page);
423+
m_draw_buffer.redraw(get_draw_function(page));
424424
queue_draw();
425425
}
426426
} else
427427
{
428-
m_draw_buffer.on_size_allocate(page, width, height);
428+
m_draw_buffer.on_size_allocate(get_draw_function(page), width, height);
429429
}
430430

431431
}
@@ -459,9 +459,11 @@ void html_widget::allocate_scrollbars(int width, int height)
459459

460460
void html_widget::on_vadjustment_changed()
461461
{
462-
m_draw_buffer.on_scroll( current_page(),
463-
(int) m_hadjustment->get_value(),
464-
(int) m_vadjustment->get_value());
462+
auto page = current_page();
463+
m_draw_buffer.on_scroll(get_draw_function(page),
464+
(int) m_hadjustment->get_value(),
465+
(int) m_vadjustment->get_value(),
466+
page ? page->get_fixed_boxes() : litehtml::position::vector{});
465467

466468
if(m_do_force_redraw_on_adjustment)
467469
{
@@ -474,9 +476,12 @@ void html_widget::on_vadjustment_changed()
474476

475477
void html_widget::on_hadjustment_changed()
476478
{
477-
m_draw_buffer.on_scroll( current_page(),
478-
(int) m_hadjustment->get_value(),
479-
(int) m_vadjustment->get_value());
479+
auto page = current_page();
480+
m_draw_buffer.on_scroll(get_draw_function(page),
481+
(int) m_hadjustment->get_value(),
482+
(int) m_vadjustment->get_value(),
483+
page ? page->get_fixed_boxes() : litehtml::position::vector{});
484+
480485
if(m_do_force_redraw_on_adjustment)
481486
{
482487
force_redraw();
@@ -512,21 +517,17 @@ void html_widget::on_realize()
512517
{
513518
Gtk::Widget::on_realize();
514519

515-
auto native = get_native();
516-
if(native)
520+
if(auto native = get_native())
517521
{
518-
auto surface = native->get_surface();
519-
if(surface)
522+
if(auto surface = native->get_surface())
520523
{
521524
surface->property_scale().signal_changed().connect([this]()
522525
{
523-
auto native = get_native();
524-
if(native)
526+
if(auto native = get_native())
525527
{
526-
auto surface = native->get_surface();
527-
if(surface)
528+
if(auto surface = native->get_surface())
528529
{
529-
m_draw_buffer.set_scale_factor(current_page(), surface->get_scale());
530+
m_draw_buffer.set_scale_factor(get_draw_function(current_page()), surface->get_scale());
530531
queue_draw();
531532
}
532533
}
@@ -592,7 +593,7 @@ void html_widget::redraw_boxes(const litehtml::position::vector& boxes)
592593

593594
if(!rect.has_zero_area())
594595
{
595-
m_draw_buffer.redraw_area(current_page(), rect.get_x(), rect.get_y(), rect.get_width(), rect.get_height());
596+
m_draw_buffer.redraw_area(get_draw_function(current_page()), rect.get_x(), rect.get_y(), rect.get_width(), rect.get_height());
596597
queue_draw();
597598
}
598599
}
@@ -686,7 +687,7 @@ void html_widget::render()
686687
{
687688
page->render(m_draw_buffer.get_width());
688689
update_view_port(page);
689-
m_draw_buffer.redraw(page);
690+
m_draw_buffer.redraw(get_draw_function(page));
690691
queue_draw();
691692
}
692693
}

support/gtkmm4/html_widget.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,16 @@ class html_widget : public Gtk::Widget,
287287
queue_draw();
288288
while (g_main_context_iteration(nullptr, false)) {}
289289
}
290+
litebrowser::draw_buffer::draw_page_function_t get_draw_function(const std::shared_ptr<litebrowser::web_page>& page)
291+
{
292+
return [this, page](cairo_t* cr, int x, int y, const litehtml::position* clip)
293+
{
294+
if (page)
295+
{
296+
page->draw((litehtml::uint_ptr) cr, x, y, clip);
297+
}
298+
};
299+
}
290300

291301
public:
292302
// Signals types

support/webpage/draw_buffer.cpp

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
/// Note, the actual position of the draw buffer can be rounded according to the scale factor.
77
/// Use get_left() and get_top() to know the actual position.
88
///
9-
/// @param page webpage to be redraw if the position was changed
9+
/// @param cb_draw the callback for drawing the page
1010
/// @param left new horizontal position
1111
/// @param top new vertical position
12-
void litebrowser::draw_buffer::on_scroll(std::shared_ptr<litebrowser::web_page> page, int left, int top)
12+
/// @param fixed_boxes fixed boxes to be redrawn
13+
void litebrowser::draw_buffer::on_scroll(const draw_page_function_t& cb_draw, int left, int top, const litehtml::position::vector& fixed_boxes)
1314
{
1415
if(m_width <= 0 || m_height <= 0 || !m_draw_buffer)
1516
return;
@@ -26,7 +27,7 @@ void litebrowser::draw_buffer::on_scroll(std::shared_ptr<litebrowser::web_page>
2627
{
2728
m_left = left;
2829
m_top = top;
29-
redraw(page);
30+
redraw(cb_draw);
3031
} else
3132
{
3233
int shift_x = m_left - left;
@@ -58,43 +59,42 @@ void litebrowser::draw_buffer::on_scroll(std::shared_ptr<litebrowser::web_page>
5859

5960
if(rec_clean.x > m_left)
6061
{
61-
redraw_area(page, m_left, rec_clean.y, rec_clean.x - m_left, rec_clean.height);
62+
redraw_area(cb_draw, m_left, rec_clean.y, rec_clean.x - m_left, rec_clean.height);
6263
}
6364
if(clean_right < right)
6465
{
65-
redraw_area(page, clean_right, rec_clean.y, right - clean_right, rec_clean.height);
66+
redraw_area(cb_draw, clean_right, rec_clean.y, right - clean_right, rec_clean.height);
6667
}
6768

6869
if(rec_clean.y > m_top)
6970
{
70-
redraw_area(page, m_left, m_top, m_width, rec_clean.y - m_top);
71+
redraw_area(cb_draw, m_left, m_top, m_width, rec_clean.y - m_top);
7172
}
7273
if(clean_bottom < bottom)
7374
{
74-
redraw_area(page, m_left, clean_bottom, m_width, bottom - clean_bottom);
75+
redraw_area(cb_draw, m_left, clean_bottom, m_width, bottom - clean_bottom);
7576
}
76-
litehtml::position::vector fixed_boxes = page->get_fixed_boxes();
77+
7778
for(const auto& box : fixed_boxes)
7879
{
79-
redraw_area(page, m_left + box.left(), m_top + box.top(), box.width, box.height);
80-
redraw_area(page, m_left + box.left() + shift_x, m_top + box.top() + shift_y, box.width, box.height);
80+
redraw_area(cb_draw, m_left + box.left(), m_top + box.top(), box.width, box.height);
81+
redraw_area(cb_draw, m_left + box.left() + shift_x, m_top + box.top() + shift_y, box.width, box.height);
8182
}
8283
}
8384
}
8485
}
8586

86-
/// @brief Reraw the defined area of the buffer
87+
/// @brief Redraw the defined area of the buffer
8788
///
88-
/// All coordinated are not scaled. Actual rectangle could be different according to the scale factor,
89+
/// All coordinated are not scaled. The actual rectangle could be different, according to the scale factor,
8990
/// but it must always cover the requested.
9091
///
91-
/// @param page webpage to be redraw
92+
/// @param cb_draw the callback for drawing the page
9293
/// @param x left position of the area
9394
/// @param y top position of the area
9495
/// @param width width of the area
9596
/// @param height height of the area
96-
void litebrowser::draw_buffer::redraw_area(std::shared_ptr<litebrowser::web_page> page, int x, int y, int width,
97-
int height)
97+
void litebrowser::draw_buffer::redraw_area(const draw_page_function_t& cb_draw, int x, int y, int width, int height)
9898
{
9999
if(m_draw_buffer)
100100
{
@@ -134,10 +134,7 @@ void litebrowser::draw_buffer::redraw_area(std::shared_ptr<litebrowser::web_page
134134
cairo_scale(cr, m_scale_factor, m_scale_factor);
135135

136136
// Draw page
137-
if(page)
138-
{
139-
page->draw((litehtml::uint_ptr) cr, -m_left, -m_top, &pos);
140-
}
137+
cb_draw(cr, -m_left, -m_top, &pos);
141138

142139
cairo_destroy(cr);
143140
}

support/webpage/draw_buffer.h

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22
#define LITEBROWSER_DRAW_BUFFER_H
33

44
#include <cairo.h>
5+
#include <functional>
6+
#include <cmath>
57
#include <memory>
6-
#include "web_page.h"
8+
#include "litehtml/types.h"
79

810
namespace litebrowser
911
{
1012
/// @brief Draw Buffer Class
1113
///
1214
/// This class performs the draw operations into the cairo surface.
13-
/// The application draws everything to the buffer, then buffer are
14-
/// drawn on widged or window.
15+
/// The application draws everything to the buffer, then buffer is
16+
/// drawn on widget or window.
1517
class draw_buffer
1618
{
1719
cairo_surface_t* m_draw_buffer = nullptr;
@@ -23,6 +25,8 @@ namespace litebrowser
2325
int m_min_int_position = 1;
2426
public:
2527

28+
using draw_page_function_t = std::function<void(cairo_t* cr, int x, int y, const litehtml::position* clip)>;
29+
2630
~draw_buffer()
2731
{
2832
if(m_draw_buffer)
@@ -50,9 +54,9 @@ namespace litebrowser
5054
double get_scale_factor() const { return m_scale_factor; }
5155

5256
/// @brief Set scale factor for draw buffer
53-
/// @param page the webpage to be redraw if required
57+
/// @param cb_draw the callback for drawing the page
5458
/// @param scale the scale factor to be applied
55-
void set_scale_factor(std::shared_ptr<litebrowser::web_page> page, double scale)
59+
void set_scale_factor(const draw_page_function_t& cb_draw, double scale)
5660
{
5761
if(m_scale_factor != scale)
5862
{
@@ -68,16 +72,16 @@ namespace litebrowser
6872
}
6973
m_draw_buffer = nullptr;
7074
create_draw_buffer(m_width, m_height);
71-
redraw(page);
75+
redraw(cb_draw);
7276
}
7377
}
7478

7579
/// @brief Create cairo surface for draw buffer
7680
/// @param width surface width (not scaled)
7781
/// @param height surface height (not scaled)
7882
/// @param scale_factor scale factor
79-
/// @return poiter to the cairo surface
80-
cairo_surface_t* make_surface(int width, int height, double scale_factor)
83+
/// @return pointer to the cairo surface
84+
static cairo_surface_t* make_surface(int width, int height, double scale_factor)
8185
{
8286
return cairo_image_surface_create(CAIRO_FORMAT_RGB24,
8387
std::ceil((double) width * scale_factor),
@@ -109,14 +113,14 @@ namespace litebrowser
109113
}
110114

111115
/// @brief Call this function when widget size changed
112-
/// @param page webpage to be redraw if buffer size changed
116+
/// @param cb_draw the callback for drawing the page
113117
/// @param width new draw buffer width
114118
/// @param height new draw buffer height
115-
void on_size_allocate(std::shared_ptr<litebrowser::web_page> page, int width, int height)
119+
void on_size_allocate(const draw_page_function_t& cb_draw, int width, int height)
116120
{
117121
if(create_draw_buffer(width, height))
118122
{
119-
redraw(page);
123+
redraw(cb_draw);
120124
}
121125
}
122126

@@ -125,32 +129,33 @@ namespace litebrowser
125129
/// Note, the actual position of the draw buffer can be rounded according to the scale factor.
126130
/// Use get_left() and get_top() to know the actual position.
127131
///
128-
/// @param page webpage to be redraw if the position was changed
132+
/// @param cb_draw the callback for drawing the page
129133
/// @param left new horizontal position
130134
/// @param top new vertical position
131-
void on_scroll(std::shared_ptr<litebrowser::web_page> page, int left, int top);
135+
/// @param fixed_boxes fixed boxes to be redrawn
136+
void on_scroll(const draw_page_function_t& cb_draw, int left, int top, const litehtml::position::vector& fixed_boxes);
132137

133-
/// @brief Reraw the defined area of the buffer
138+
/// @brief Redraw the defined area of the buffer
134139
///
135-
/// All coordinated are not scaled. Actual rectangle could be different according to the scale factor,
140+
/// All coordinated are not scaled. The actual rectangle could be different, according to the scale factor,
136141
/// but it must always cover the requested.
137142
///
138-
/// @param page webpage to be redraw
143+
/// @param cb_draw the callback for drawing the page
139144
/// @param x left position of the area
140145
/// @param y top position of the area
141146
/// @param width width of the area
142147
/// @param height height of the area
143-
void redraw_area(std::shared_ptr<litebrowser::web_page> page, int x, int y, int width, int height);
148+
void redraw_area(const draw_page_function_t& cb_draw, int x, int y, int width, int height);
144149

145150
/// @brief Redraw entire buffer
146-
/// @param page webpage to be redraw
147-
void redraw(std::shared_ptr<litebrowser::web_page> page)
151+
/// @param cb_draw the callback for drawing the page
152+
void redraw(const draw_page_function_t& cb_draw)
148153
{
149-
redraw_area(page, m_left, m_top, m_width, m_height);
154+
redraw_area(cb_draw, m_left, m_top, m_width, m_height);
150155
}
151156

152157
private:
153-
inline int fix_position(int pos)
158+
[[nodiscard]] int fix_position(int pos) const
154159
{
155160
return (pos / m_min_int_position) * m_min_int_position;
156161
}
@@ -172,7 +177,6 @@ namespace litebrowser
172177
int denominator = 100;
173178

174179
int common_divisor = get_common_divisor(numerator, denominator);
175-
numerator /= common_divisor;
176180
return denominator / common_divisor;
177181
}
178182
};

0 commit comments

Comments
 (0)