Compare commits

..

3 commits

Author SHA1 Message Date
hippoz
b6c6f1e78c
fix batch system 2022-07-26 04:47:17 +03:00
hippoz
3c5c5b6b51
add batch in add_child 2022-07-26 04:47:03 +03:00
hippoz
360952c25d
add label text align option 2022-07-26 02:44:09 +03:00
5 changed files with 42 additions and 22 deletions

View file

@ -28,7 +28,7 @@ void Label::on_paint() {
auto geometry = rect().max_geometry(); auto geometry = rect().max_geometry();
painter.source_rgb(style()->foreground()); painter.source_rgb(style()->foreground());
painter.text(geometry, m_text, PaintTextAlign::Left, PANGO_ELLIPSIZE_END, style()->font_description()); painter.text(geometry, m_text, m_align, PANGO_ELLIPSIZE_END, style()->font_description());
painter.fill(); painter.fill();
} }

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "Widget.hpp" #include "Widget.hpp"
#include "Painter.hpp"
namespace Raven { namespace Raven {
@ -10,15 +11,24 @@ public:
: Widget(WidgetType::Label) : Widget(WidgetType::Label)
, m_text(text) {} , m_text(text) {}
Label(std::string text, PaintTextAlign align)
: Widget(WidgetType::Label)
, m_text(text)
, m_align(align) {}
~Label() {} ~Label() {}
void set_text(std::string text); void set_text(std::string text);
std::string &text() { return m_text; } std::string &text() { return m_text; }
PaintTextAlign &align() { return m_align; }
void set_align(PaintTextAlign align) { m_align = align; }
protected: protected:
void on_paint(); void on_paint();
void on_init(); void on_init();
private: private:
std::string m_text; std::string m_text;
PaintTextAlign m_align { PaintTextAlign::Left };
}; };
} }

View file

@ -90,6 +90,10 @@ bool Widget::add_child(std::shared_ptr<Widget> child) {
if (child->parent()) { if (child->parent()) {
return false; return false;
} }
if (window())
window()->start_batch();
m_children.push_back(child); m_children.push_back(child);
child->set_parent(this); child->set_parent(this);
@ -98,6 +102,9 @@ bool Widget::add_child(std::shared_ptr<Widget> child) {
// after we call set_window() on it. // after we call set_window() on it.
child->set_window(m_window); child->set_window(m_window);
if (window())
window()->end_batch();
return true; return true;
} }

View file

@ -73,10 +73,8 @@ bool Window::dispatch_to_main_widget(Event &event) {
void Window::repaint(Box geometry) { void Window::repaint(Box geometry) {
m_did_repaint_during_batch = true; m_did_repaint_during_batch = true;
if (m_batches)
if (m_is_batching) {
return; return;
}
auto event = RepaintRectEvent(true, geometry); auto event = RepaintRectEvent(true, geometry);
dispatch_to_main_widget(event); dispatch_to_main_widget(event);
@ -92,10 +90,8 @@ void Window::repaint() {
void Window::relayout(Widget *target) { void Window::relayout(Widget *target) {
m_did_relayout_during_batch = true; m_did_relayout_during_batch = true;
if (m_batches)
if (m_is_batching) {
return; return;
}
auto event = RelayoutSubtreeEvent(); auto event = RelayoutSubtreeEvent();
target->dispatch_event(event); target->dispatch_event(event);
@ -103,10 +99,8 @@ void Window::relayout(Widget *target) {
void Window::relayout() { void Window::relayout() {
m_did_relayout_during_batch = true; m_did_relayout_during_batch = true;
if (m_batches)
if (m_is_batching) {
return; return;
}
auto event = RelayoutSubtreeEvent(); auto event = RelayoutSubtreeEvent();
dispatch_to_main_widget(event); dispatch_to_main_widget(event);
@ -118,20 +112,29 @@ void Window::reflow() {
} }
void Window::start_batch() { void Window::start_batch() {
m_is_batching = true; m_batches++;
m_did_relayout_during_batch = false;
m_did_repaint_during_batch = false;
} }
void Window::end_batch() { void Window::end_batch() {
if (m_is_batching) { if (m_batches < 1)
m_is_batching = false; return;
// we are only interested in performing batch operations for the last batch
if (m_batches > 1) {
m_batches--;
return;
}
m_batches = 0;
if (m_did_relayout_during_batch) { if (m_did_relayout_during_batch) {
reflow(); reflow();
} else if (m_did_repaint_during_batch) { } else if (m_did_repaint_during_batch) {
repaint(); repaint();
} }
}
m_did_relayout_during_batch = false;
m_did_repaint_during_batch = false;
} }
void Window::run(bool block) { void Window::run(bool block) {

View file

@ -56,9 +56,9 @@ private:
Box m_rect { 0, 0, 800, 600 }; Box m_rect { 0, 0, 800, 600 };
Painter m_painter {}; Painter m_painter {};
Cairo::RefPtr<Cairo::XlibSurface> m_xlib_surface { nullptr }; Cairo::RefPtr<Cairo::XlibSurface> m_xlib_surface { nullptr };
bool m_is_batching { true }; int m_batches { 1 };
bool m_did_relayout_during_batch { true };
bool m_did_repaint_during_batch { false }; bool m_did_repaint_during_batch { false };
bool m_did_relayout_during_batch { false };
Display *m_x_display { nullptr }; Display *m_x_display { nullptr };