Compare commits
No commits in common. "9ee4acfba5b5a2dd51714500798f3ea77ac43ad0" and "94c0be051be354b81f018e2dcf3feb07046f0c5b" have entirely different histories.
9ee4acfba5
...
94c0be051b
20 changed files with 188 additions and 193 deletions
|
@ -7,7 +7,6 @@ xlib_dep = dependency('x11')
|
|||
executable(
|
||||
'ravenapp',
|
||||
'./src/Box.cpp',
|
||||
'./src/Styles.cpp',
|
||||
'./src/Painter.cpp',
|
||||
'./src/Window.cpp',
|
||||
'./src/Widget.cpp',
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
#include "Box.hpp"
|
||||
#include "Window.hpp"
|
||||
#include "Painter.hpp"
|
||||
#include "Styles.hpp"
|
||||
#include "pango/pango-layout.h"
|
||||
#include <pango/pangocairo.h>
|
||||
|
||||
|
@ -18,21 +17,40 @@ void Button::set_text(std::string text) {
|
|||
}
|
||||
|
||||
void Button::on_init() {
|
||||
set_style(&default_button_style);
|
||||
set_background_border_radius(styles()->button_border_radius());
|
||||
set_background_fill_color(styles()->button_normal_color());
|
||||
set_do_background_fill(true);
|
||||
fit_text(m_text);
|
||||
|
||||
set_did_init(true);
|
||||
}
|
||||
|
||||
void Button::update_color() {
|
||||
if (is_active()) {
|
||||
set_background_fill_color(styles()->button_active_color());
|
||||
} else if (is_focused()) {
|
||||
set_background_fill_color(styles()->button_focused_color());
|
||||
} else {
|
||||
set_background_fill_color(styles()->button_normal_color());
|
||||
}
|
||||
}
|
||||
|
||||
void Button::on_paint() {
|
||||
auto painter = window()->painter();
|
||||
auto text_color = styles()->button_text_color();
|
||||
|
||||
painter.source_rgb(style()->foreground());
|
||||
painter.set_pango_font_description(style()->font_description());
|
||||
painter.text(rect(), m_text, PaintTextAlign::Center, PANGO_ELLIPSIZE_END, true);
|
||||
painter.source_rgb(text_color);
|
||||
painter.set_pango_font_description(styles()->controls_font_description());
|
||||
painter.text(current_geometry(), m_text, PaintTextAlign::Center, PANGO_ELLIPSIZE_END, true);
|
||||
painter.fill();
|
||||
}
|
||||
|
||||
void Button::on_focus_update(FocusUpdateEvent &event) {
|
||||
update_color();
|
||||
}
|
||||
|
||||
void Button::on_activation_update(ActivationUpdateEvent &event) {
|
||||
update_color();
|
||||
if (event.activation_status() == false) {
|
||||
on_click();
|
||||
}
|
||||
|
|
|
@ -22,6 +22,7 @@ public:
|
|||
protected:
|
||||
void on_paint();
|
||||
void on_init();
|
||||
void on_focus_update(FocusUpdateEvent &event);
|
||||
void on_activation_update(ActivationUpdateEvent &event);
|
||||
private:
|
||||
void update_color();
|
||||
|
|
|
@ -17,21 +17,21 @@ void DocumentLayout::run() {
|
|||
if (child->absolute())
|
||||
continue;
|
||||
|
||||
if (child->rect().height() > largest_height_so_far) {
|
||||
largest_height_so_far = child->rect().height();
|
||||
if (child->current_geometry().height() > largest_height_so_far) {
|
||||
largest_height_so_far = child->current_geometry().height();
|
||||
}
|
||||
|
||||
bool new_row_because_of_control_widget = (child->control_type() == ControlWidgetType::NewRow && largest_height_so_far);
|
||||
bool new_row_because_of_justification = (current_position.x() + child->rect().width() + m_margin) >= m_target->rect().width();
|
||||
bool new_row_because_of_justification = (current_position.x() + child->current_geometry().width() + m_margin) >= m_target->current_geometry().width();
|
||||
bool should_do_new_row = new_row_because_of_control_widget || new_row_because_of_justification;
|
||||
if (should_do_new_row) {
|
||||
current_position.add(0, largest_height_so_far + m_margin);
|
||||
current_position.set_x(m_margin);
|
||||
}
|
||||
|
||||
child->rect().set_x(current_position.x());
|
||||
child->rect().set_y(current_position.y());
|
||||
current_position.add(child->rect().width() + m_margin, 0);
|
||||
child->current_geometry().set_x(current_position.x());
|
||||
child->current_geometry().set_y(current_position.y());
|
||||
current_position.add(child->current_geometry().width() + m_margin, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
namespace Raven {
|
||||
class GenericStyle;
|
||||
class Box;
|
||||
class Button;
|
||||
class Events;
|
||||
class Painter;
|
||||
class Point;
|
||||
class TopLevelStyles;
|
||||
class Layout;
|
||||
class Widget;
|
||||
class Window;
|
||||
|
|
|
@ -1,49 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include "pango/pango-font.h"
|
||||
#include "RGB.hpp"
|
||||
|
||||
#define __RAVEN_STYLE_PROP(name, type, ...) \
|
||||
private: \
|
||||
type m_##name {__VA_ARGS__}; \
|
||||
public: \
|
||||
void set_##name(type new_prop_value) { m_##name = new_prop_value; style_prop_updated(); } \
|
||||
type name() { return m_##name; } \
|
||||
private:
|
||||
|
||||
namespace Raven {
|
||||
|
||||
class GenericStyle {
|
||||
__RAVEN_STYLE_PROP(font_description, PangoFontDescription*, nullptr)
|
||||
__RAVEN_STYLE_PROP(foreground, RGB, 0.00000, 0.00000, 0.00000)
|
||||
__RAVEN_STYLE_PROP(background_norm, RGB, 1.00000, 1.00000, 1.00000)
|
||||
__RAVEN_STYLE_PROP(background_focused, RGB, 1.00000, 1.00000, 1.00000)
|
||||
__RAVEN_STYLE_PROP(background_active, RGB, 1.00000, 1.00000, 1.00000)
|
||||
__RAVEN_STYLE_PROP(border_radius, double, 0.0)
|
||||
__RAVEN_STYLE_PROP(fill_background, bool, true)
|
||||
__RAVEN_STYLE_PROP(update_background, bool, false)
|
||||
|
||||
private:
|
||||
void style_prop_updated() { }
|
||||
public:
|
||||
GenericStyle(
|
||||
PangoFontDescription* font_description,
|
||||
RGB foreground,
|
||||
RGB background_norm,
|
||||
RGB background_focused,
|
||||
RGB background_active,
|
||||
double border_radius,
|
||||
bool fill_background,
|
||||
bool update_background
|
||||
)
|
||||
: m_font_description(font_description)
|
||||
, m_foreground(foreground)
|
||||
, m_background_norm(background_norm)
|
||||
, m_background_focused(background_focused)
|
||||
, m_background_active(background_active)
|
||||
, m_border_radius(border_radius)
|
||||
, m_fill_background(fill_background)
|
||||
, m_update_background(update_background) {}
|
||||
};
|
||||
|
||||
}
|
|
@ -1,7 +1,6 @@
|
|||
#include "Label.hpp"
|
||||
#include "Window.hpp"
|
||||
#include "pango/pango-layout.h"
|
||||
#include "Styles.hpp"
|
||||
#include <iostream>
|
||||
|
||||
namespace Raven {
|
||||
|
@ -14,17 +13,19 @@ void Label::set_text(std::string text) {
|
|||
}
|
||||
}
|
||||
void Label::on_init() {
|
||||
set_style(&default_label_style);
|
||||
fit_text(m_text);
|
||||
|
||||
set_did_init(true);
|
||||
set_do_background_fill(false);
|
||||
}
|
||||
|
||||
void Label::on_paint() {
|
||||
auto painter = window()->painter();
|
||||
auto text_color = styles()->label_text_color();
|
||||
|
||||
painter.source_rgb(style()->foreground());
|
||||
painter.set_pango_font_description(style()->font_description());
|
||||
painter.text(rect(), m_text, PaintTextAlign::Left, PANGO_ELLIPSIZE_NONE, true);
|
||||
painter.source_rgb(text_color);
|
||||
painter.set_pango_font_description(styles()->controls_font_description());
|
||||
painter.text(current_geometry(), m_text, PaintTextAlign::Left, PANGO_ELLIPSIZE_NONE, true);
|
||||
painter.fill();
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "Widget.hpp"
|
||||
#include "PropMacros.hpp"
|
||||
|
||||
namespace Raven {
|
||||
|
||||
|
|
|
@ -83,7 +83,7 @@ Point Painter::text(Box &geometry, std::string &text, PaintTextAlign align, Pang
|
|||
return {pango_units_to_double(pango_width), pango_units_to_double(pango_height)};
|
||||
}
|
||||
|
||||
void Painter::source_rgb(RGB source_rgb) {
|
||||
void Painter::source_rgb(RGB &source_rgb) {
|
||||
m_cairo->set_source_rgb(source_rgb.r(), source_rgb.g(), source_rgb.b());
|
||||
}
|
||||
|
||||
|
|
|
@ -33,7 +33,7 @@ public:
|
|||
|
||||
bool can_paint() { if (m_cairo) return true; else return false; }
|
||||
|
||||
void source_rgb(RGB source_rgb);
|
||||
void source_rgb(RGB &source_rgb);
|
||||
void fill();
|
||||
|
||||
void begin_paint_group();
|
||||
|
|
9
src/PropMacros.hpp
Normal file
9
src/PropMacros.hpp
Normal file
|
@ -0,0 +1,9 @@
|
|||
#pragma once
|
||||
|
||||
#define DEF_WIDGET_STYLE_PROP(name, type, ...) \
|
||||
private: \
|
||||
type m_##name {__VA_ARGS__}; \
|
||||
public: \
|
||||
void set_##name(type new_prop_value) { m_##name = new_prop_value; repaint(); } \
|
||||
type name() { return m_##name; } \
|
||||
private:
|
|
@ -1,41 +0,0 @@
|
|||
#include "Styles.hpp"
|
||||
|
||||
#include "pango/pango-font.h"
|
||||
#include "RGB.hpp"
|
||||
|
||||
namespace Raven {
|
||||
|
||||
GenericStyle default_widget_style {
|
||||
pango_font_description_from_string("sans-serif"),
|
||||
RGB(0.00000, 0.00000, 0.00000),
|
||||
RGB(1.00000, 1.00000, 1.00000),
|
||||
RGB(1.00000, 1.00000, 1.00000),
|
||||
RGB(1.00000, 1.00000, 1.00000),
|
||||
0.0,
|
||||
true,
|
||||
false
|
||||
};
|
||||
|
||||
GenericStyle default_button_style {
|
||||
pango_font_description_from_string("sans-serif"),
|
||||
RGB(0.00000, 0.00000, 0.00000),
|
||||
RGB(0.73333, 0.40392, 0.89412),
|
||||
RGB(0.63922, 0.27843, 0.81961),
|
||||
RGB(0.54118, 0.18039, 0.72157),
|
||||
6.0,
|
||||
true,
|
||||
true
|
||||
};
|
||||
|
||||
GenericStyle default_label_style {
|
||||
pango_font_description_from_string("sans-serif"),
|
||||
RGB(0.00000, 0.00000, 0.00000),
|
||||
RGB(0.00000, 0.00000, 0.00000),
|
||||
RGB(0.00000, 0.00000, 0.00000),
|
||||
RGB(0.00000, 0.00000, 0.00000),
|
||||
0.0,
|
||||
false,
|
||||
false
|
||||
};
|
||||
|
||||
}
|
|
@ -1,11 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include "GenericStyle.hpp"
|
||||
|
||||
namespace Raven {
|
||||
|
||||
extern GenericStyle default_widget_style;
|
||||
extern GenericStyle default_button_style;
|
||||
extern GenericStyle default_label_style;
|
||||
|
||||
}
|
12
src/TopLevelStyles.cpp
Normal file
12
src/TopLevelStyles.cpp
Normal file
|
@ -0,0 +1,12 @@
|
|||
#include "TopLevelStyles.hpp"
|
||||
#include "Window.hpp"
|
||||
|
||||
namespace Raven {
|
||||
|
||||
void TopLevelStyles::wants_repaint() {
|
||||
// when one of the styles here changes, we will dispatch
|
||||
// a full repaint to the entire window :^)
|
||||
m_window->dispatch_full_repaint();
|
||||
}
|
||||
|
||||
}
|
57
src/TopLevelStyles.hpp
Normal file
57
src/TopLevelStyles.hpp
Normal file
|
@ -0,0 +1,57 @@
|
|||
#pragma once
|
||||
|
||||
#include "Forward.hpp"
|
||||
#include "pango/pango-font.h"
|
||||
#include "PropMacros.hpp"
|
||||
#include "RGB.hpp"
|
||||
|
||||
namespace Raven {
|
||||
|
||||
class TopLevelStyles {
|
||||
DEF_WIDGET_STYLE_PROP(controls_font_description, PangoFontDescription*, nullptr)
|
||||
|
||||
/* FIXME: */
|
||||
/* right now, these colors are gruvbox */
|
||||
/* however the `accent_color_darkest` is a darkened version of `accent_color_darker` */
|
||||
DEF_WIDGET_STYLE_PROP(text_color, RGB, 0.0, 0.0, 0.0)
|
||||
DEF_WIDGET_STYLE_PROP(accent_color, RGB, 0.6941, 0.3843, 0.5254)
|
||||
DEF_WIDGET_STYLE_PROP(accent_color_darker, RGB, 0.5607, 0.2470, 0.4431)
|
||||
DEF_WIDGET_STYLE_PROP(accent_color_darkest, RGB, 0.48069, 0.1669, 0.3631)
|
||||
DEF_WIDGET_STYLE_PROP(background_color, RGB, 0.9764, 0.9607, 0.8431)
|
||||
|
||||
DEF_WIDGET_STYLE_PROP(button_text_color, RGB, m_text_color)
|
||||
DEF_WIDGET_STYLE_PROP(button_normal_color, RGB, m_accent_color)
|
||||
DEF_WIDGET_STYLE_PROP(button_focused_color, RGB, m_accent_color_darker)
|
||||
DEF_WIDGET_STYLE_PROP(button_active_color, RGB, m_accent_color_darkest)
|
||||
DEF_WIDGET_STYLE_PROP(button_border_radius, double, 6.0)
|
||||
|
||||
DEF_WIDGET_STYLE_PROP(label_text_color, RGB, m_text_color)
|
||||
|
||||
private:
|
||||
Window *m_window;
|
||||
public:
|
||||
TopLevelStyles(Window *window)
|
||||
: m_window(window)
|
||||
{
|
||||
PangoFontDescription *font_description = pango_font_description_new();
|
||||
|
||||
pango_font_description_set_family(font_description, "sans-serif");
|
||||
pango_font_description_set_weight(font_description, PANGO_WEIGHT_NORMAL);
|
||||
pango_font_description_set_absolute_size(font_description, 16 * PANGO_SCALE);
|
||||
|
||||
m_controls_font_description = font_description;
|
||||
}
|
||||
|
||||
~TopLevelStyles() {
|
||||
if (m_controls_font_description) {
|
||||
pango_font_description_free(m_controls_font_description);
|
||||
}
|
||||
}
|
||||
|
||||
Window *window() { return m_window; }
|
||||
void set_window(Window *window) { m_window = window; }
|
||||
|
||||
void repaint();
|
||||
};
|
||||
|
||||
}
|
|
@ -12,7 +12,7 @@ namespace Raven {
|
|||
Point Widget::window_relative() {
|
||||
Point point = { 0, 0 };
|
||||
for (Widget* parent = m_parent; parent; parent = parent->parent()) {
|
||||
point.add(parent->rect().x(), parent->rect().y());
|
||||
point.add(parent->current_geometry().x(), parent->current_geometry().y());
|
||||
}
|
||||
return point;
|
||||
}
|
||||
|
@ -21,32 +21,32 @@ void Widget::fit_text(std::string &text) {
|
|||
if (!window())
|
||||
return;
|
||||
|
||||
window()->painter().set_pango_font_description(m_style->font_description());
|
||||
auto size = window()->painter().compute_text_size(rect(), text);
|
||||
window()->painter().set_pango_font_description(styles()->controls_font_description());
|
||||
auto size = window()->painter().compute_text_size(current_geometry(), text);
|
||||
if (!resize(size)) {
|
||||
reflow();
|
||||
}
|
||||
}
|
||||
|
||||
bool Widget::resize(double width, double height) {
|
||||
if (m_rect.width() == width && m_rect.height() == height)
|
||||
if (m_current_geometry.width() == width && m_current_geometry.height() == height)
|
||||
return false;
|
||||
|
||||
if (width < 0 || height < 0)
|
||||
return false;
|
||||
|
||||
m_rect.set_width(width);
|
||||
m_rect.set_height(height);
|
||||
m_current_geometry.set_width(width);
|
||||
m_current_geometry.set_height(height);
|
||||
reflow();
|
||||
return true;
|
||||
}
|
||||
|
||||
void Widget::move_to(double x, double y) {
|
||||
if (m_rect.x() == x && m_rect.y() == y)
|
||||
if (m_current_geometry.x() == x && m_current_geometry.y() == y)
|
||||
return;
|
||||
|
||||
m_rect.set_x(x);
|
||||
m_rect.set_y(y);
|
||||
m_current_geometry.set_x(x);
|
||||
m_current_geometry.set_y(y);
|
||||
reflow();
|
||||
}
|
||||
|
||||
|
@ -65,6 +65,7 @@ void Widget::set_window(Window *window) {
|
|||
throw std::logic_error{"null window value for set_window"};
|
||||
|
||||
m_window = window;
|
||||
m_styles = m_window->top_level_styles();
|
||||
|
||||
on_init();
|
||||
}
|
||||
|
@ -106,7 +107,7 @@ void Widget::handle_repaint_rect(RepaintRectEvent &event) {
|
|||
if (!m_did_init || !painter.can_paint())
|
||||
return;
|
||||
|
||||
if (!event.box().contains_box(m_rect))
|
||||
if (!event.box().contains_box(current_geometry()))
|
||||
return; // widgets contain their children, thus we don't need to recurse further
|
||||
|
||||
// using a "group" in cairo reduces flickering
|
||||
|
@ -120,37 +121,27 @@ void Widget::handle_repaint_rect(RepaintRectEvent &event) {
|
|||
|
||||
cr->save();
|
||||
|
||||
// clip this widget. this ensures that the background fill or children won't overflow the bounds of it
|
||||
painter.rounded_rectangle(m_rect, m_style->border_radius());
|
||||
// clip this widget. this ensuers that the background fill or children won't overflow the bounds of it
|
||||
painter.rounded_rectangle(m_current_geometry, m_background_border_radius);
|
||||
cr->clip();
|
||||
|
||||
// paint the background fill
|
||||
// note that we're using the bounds of the paint event as the rectangle, this ensures that we dont draw over other widgets which won't be repainted.
|
||||
if (m_style->fill_background()) {
|
||||
if (m_style->update_background()) {
|
||||
if (m_is_focused) {
|
||||
painter.source_rgb(m_style->background_focused());
|
||||
} else if (m_is_active) {
|
||||
painter.source_rgb(m_style->background_active());
|
||||
} else {
|
||||
painter.source_rgb(m_style->background_norm());
|
||||
}
|
||||
} else {
|
||||
painter.source_rgb(m_style->background_norm());
|
||||
}
|
||||
painter.rounded_rectangle(event.box(), m_style->border_radius());
|
||||
if (m_do_background_fill) {
|
||||
painter.source_rgb(m_background_fill_color);
|
||||
painter.rounded_rectangle(event.box(), m_background_border_radius);
|
||||
cr->fill();
|
||||
}
|
||||
|
||||
// translate to the widget's position
|
||||
// the origin of the painter is now the widget's origin
|
||||
// this is because the position of children is relative to the origin of their parent
|
||||
cr->translate(rect().x(), rect().y());
|
||||
cr->translate(current_geometry().x(), current_geometry().y());
|
||||
|
||||
on_paint();
|
||||
|
||||
// convert the paint event's origin to our coordinate space because all positions of children are relative to the origin of their parent
|
||||
auto local_box = event.box().offset(-m_rect.x(), -m_rect.y());
|
||||
auto local_box = event.box().offset(-m_current_geometry.x(), -m_current_geometry.y());
|
||||
auto local_event = RepaintRectEvent(false, local_box);
|
||||
for (auto child : m_children) {
|
||||
child->dispatch_event(local_event);
|
||||
|
@ -171,7 +162,7 @@ void Widget::handle_mouse_move_event(MouseMoveEvent &event) {
|
|||
event.accept(); // we will do our own propagation logic
|
||||
|
||||
bool update_focus_to = true;
|
||||
if (!m_rect.contains_point(event.point())) {
|
||||
if (!m_current_geometry.contains_point(event.point())) {
|
||||
// we just became unfocused
|
||||
update_focus_to = false;
|
||||
}
|
||||
|
@ -185,17 +176,13 @@ void Widget::handle_mouse_move_event(MouseMoveEvent &event) {
|
|||
|
||||
auto focus_update_event = FocusUpdateEvent(update_focus_to);
|
||||
on_focus_update(focus_update_event);
|
||||
|
||||
if (m_style->update_background()) {
|
||||
repaint();
|
||||
}
|
||||
}
|
||||
|
||||
if (!m_consumes_hits) {
|
||||
// translate the event's point to our coordinate space because the position of all children is relative to the origin of their parent
|
||||
auto local_event = MouseMoveEvent(Point(
|
||||
event.point().x() - m_rect.x(),
|
||||
event.point().y() - m_rect.y()
|
||||
event.point().x() - m_current_geometry.x(),
|
||||
event.point().y() - m_current_geometry.y()
|
||||
));
|
||||
for (auto child : m_children) {
|
||||
child->dispatch_event(local_event);
|
||||
|
@ -208,7 +195,7 @@ void Widget::handle_mouse_button_event(MouseButtonEvent &event) {
|
|||
|
||||
bool update_activation_to = event.was_left_button_pressed();
|
||||
|
||||
if (!m_rect.contains_point(event.point())) {
|
||||
if (!m_current_geometry.contains_point(event.point())) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -221,17 +208,13 @@ void Widget::handle_mouse_button_event(MouseButtonEvent &event) {
|
|||
|
||||
auto activation_update_event = ActivationUpdateEvent(update_activation_to);
|
||||
on_activation_update(activation_update_event);
|
||||
|
||||
if (m_style->update_background()) {
|
||||
repaint();
|
||||
}
|
||||
}
|
||||
|
||||
if (!m_consumes_hits) {
|
||||
// translate the event's point to our coordinate space because the position of all children is relative to the origin of their parent
|
||||
auto local_event = MouseButtonEvent(event.was_left_button_pressed(), event.was_right_button_pressed(), Point(
|
||||
event.point().x() - m_rect.x(),
|
||||
event.point().y() - m_rect.y()
|
||||
event.point().x() - m_current_geometry.x(),
|
||||
event.point().y() - m_current_geometry.y()
|
||||
));
|
||||
for (auto child : m_children) {
|
||||
child->dispatch_event(local_event);
|
||||
|
@ -277,4 +260,9 @@ void Widget::dispatch_event(Event &event) {
|
|||
}
|
||||
}
|
||||
|
||||
void Widget::on_init() {
|
||||
set_background_fill_color(styles()->background_color());
|
||||
set_did_init(true);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -9,8 +9,8 @@
|
|||
#include "Events.hpp"
|
||||
#include "Forward.hpp"
|
||||
#include "RGB.hpp"
|
||||
#include "GenericStyle.hpp"
|
||||
#include "Styles.hpp"
|
||||
#include "TopLevelStyles.hpp"
|
||||
#include "PropMacros.hpp"
|
||||
|
||||
namespace Raven {
|
||||
|
||||
|
@ -27,6 +27,10 @@ enum class ControlWidgetType {
|
|||
};
|
||||
|
||||
class Widget {
|
||||
DEF_WIDGET_STYLE_PROP(do_background_fill, bool, true)
|
||||
DEF_WIDGET_STYLE_PROP(background_fill_color, RGB, 0, 0, 0)
|
||||
DEF_WIDGET_STYLE_PROP(background_border_radius, double, 0.0)
|
||||
|
||||
public:
|
||||
Widget() {}
|
||||
|
||||
|
@ -54,9 +58,9 @@ public:
|
|||
bool add_child(std::shared_ptr<Widget> child);
|
||||
void remove_child(std::shared_ptr<Widget> child);
|
||||
|
||||
Box &rect() { return m_rect; }
|
||||
void set_rect(Box rect) { m_rect = rect; reflow(); }
|
||||
void set_rect(Box rect, bool is_pure) { m_rect = rect; if (!is_pure) { reflow(); } }
|
||||
Box ¤t_geometry() { return m_current_geometry; }
|
||||
void set_current_geometry(Box current_geometry) { m_current_geometry = current_geometry; reflow(); }
|
||||
void set_current_geometry(Box current_geometry, bool is_pure) { m_current_geometry = current_geometry; if (!is_pure) { reflow(); } }
|
||||
|
||||
Point window_relative();
|
||||
|
||||
|
@ -69,11 +73,14 @@ public:
|
|||
Window *window() { return m_window; }
|
||||
void set_window(Window *window);
|
||||
|
||||
GenericStyle *style() { return m_style; }
|
||||
void set_style(GenericStyle *style) { m_style = style; reflow(); }
|
||||
std::shared_ptr<TopLevelStyles> styles() { return m_styles; }
|
||||
void set_styles(std::shared_ptr<TopLevelStyles> styles) { m_styles = styles; wants_relayout(); }
|
||||
|
||||
void set_did_init(bool did_init) { m_did_init = did_init; }
|
||||
bool did_init() { return m_did_init; }
|
||||
|
||||
bool is_focused() { return m_is_focused; }
|
||||
|
||||
bool is_active() { return m_is_active; }
|
||||
|
||||
bool consumes_hits() { return m_consumes_hits; }
|
||||
|
@ -89,6 +96,8 @@ public:
|
|||
std::shared_ptr<Layout> layout() { return m_layout; }
|
||||
|
||||
void dispatch_event(Event &event);
|
||||
void wants_repaint();
|
||||
void wants_relayout();
|
||||
|
||||
template<typename T, class... Args>
|
||||
std::shared_ptr<T> set_layout(Args&&... args) {
|
||||
|
@ -106,15 +115,13 @@ public:
|
|||
protected:
|
||||
WidgetType m_type { WidgetType::Widget };
|
||||
|
||||
virtual void on_init() { set_did_init(true); }
|
||||
virtual void on_init();
|
||||
virtual void on_mouse_button(MouseButtonEvent &event) {}
|
||||
virtual void on_mouse_move(MouseMoveEvent &event) {}
|
||||
virtual void on_focus_update(FocusUpdateEvent &event) {}
|
||||
virtual void on_activation_update(ActivationUpdateEvent &event) {}
|
||||
virtual void on_paint() {}
|
||||
|
||||
void set_did_init(bool did_init) { m_did_init = did_init; }
|
||||
|
||||
void repaint();
|
||||
void reflow();
|
||||
private:
|
||||
|
@ -124,11 +131,11 @@ private:
|
|||
void handle_mouse_move_event(MouseMoveEvent &event);
|
||||
void handle_mouse_button_event(MouseButtonEvent &event);
|
||||
|
||||
Box m_rect { 0, 0, 0, 0 };
|
||||
Box m_current_geometry { 0, 0, 0, 0 };
|
||||
std::vector<std::shared_ptr<Widget>> m_children;
|
||||
Widget *m_parent { nullptr };
|
||||
Window *m_window { nullptr };
|
||||
GenericStyle *m_style { &default_widget_style };
|
||||
std::shared_ptr<TopLevelStyles> m_styles { nullptr };
|
||||
std::shared_ptr<Layout> m_layout { nullptr };
|
||||
bool m_did_init { false };
|
||||
bool m_is_focused { false };
|
||||
|
|
|
@ -16,7 +16,7 @@ namespace Raven {
|
|||
void Window::set_main_widget(std::shared_ptr<Widget> main_widget) {
|
||||
m_main_widget = main_widget;
|
||||
m_main_widget->set_window(this);
|
||||
m_main_widget->set_rect(m_rect);
|
||||
m_main_widget->set_current_geometry(m_current_geometry);
|
||||
}
|
||||
|
||||
bool Window::spawn_window() {
|
||||
|
@ -33,8 +33,8 @@ bool Window::spawn_window() {
|
|||
DefaultRootWindow(dsp),
|
||||
0,
|
||||
0,
|
||||
m_rect.width(),
|
||||
m_rect.height(),
|
||||
m_current_geometry.width(),
|
||||
m_current_geometry.height(),
|
||||
0,
|
||||
0,
|
||||
CopyFromParent,
|
||||
|
@ -50,8 +50,8 @@ bool Window::spawn_window() {
|
|||
dsp,
|
||||
da,
|
||||
DefaultVisual(dsp, screen),
|
||||
rect().width(),
|
||||
rect().height()
|
||||
current_geometry().width(),
|
||||
current_geometry().height()
|
||||
);
|
||||
auto cairo_context = Cairo::Context::create(m_xlib_surface);
|
||||
|
||||
|
@ -82,11 +82,12 @@ void Window::repaint(Box geometry) {
|
|||
}
|
||||
|
||||
void Window::repaint(Widget *target) {
|
||||
repaint(target->rect().offset(target->window_relative().x(), target->window_relative().y()));
|
||||
std::cout << target->window_relative().x() << ", " << target->window_relative().y() << std::endl;
|
||||
repaint(target->current_geometry().offset(target->window_relative().x(), target->window_relative().y()));
|
||||
}
|
||||
|
||||
void Window::repaint() {
|
||||
repaint(m_rect);
|
||||
repaint(m_current_geometry);
|
||||
}
|
||||
|
||||
void Window::relayout(Widget *target) {
|
||||
|
@ -148,14 +149,14 @@ void Window::run(bool block) {
|
|||
break;
|
||||
}
|
||||
case ConfigureNotify: {
|
||||
if (e.xconfigure.width != m_rect.width() || e.xconfigure.height != m_rect.height()) {
|
||||
if (e.xconfigure.width != m_current_geometry.width() || e.xconfigure.height != m_current_geometry.height()) {
|
||||
m_xlib_surface->set_size(e.xconfigure.width, e.xconfigure.height);
|
||||
m_rect.set_width(e.xconfigure.width);
|
||||
m_rect.set_height(e.xconfigure.height);
|
||||
m_current_geometry.set_width(e.xconfigure.width);
|
||||
m_current_geometry.set_height(e.xconfigure.height);
|
||||
// if we have a main widget, we are going to have to resize it as well
|
||||
if (m_main_widget) {
|
||||
m_main_widget->rect().set_width(m_rect.width());
|
||||
m_main_widget->rect().set_height(m_rect.height());
|
||||
m_main_widget->current_geometry().set_width(m_current_geometry.width());
|
||||
m_main_widget->current_geometry().set_height(m_current_geometry.height());
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
#include <stack>
|
||||
#include "Widget.hpp"
|
||||
#include "Painter.hpp"
|
||||
#include "Events.hpp"
|
||||
#include "src/Events.hpp"
|
||||
#include <X11/Xlib.h>
|
||||
#include <cairomm/xlib_surface.h>
|
||||
#include <functional>
|
||||
|
@ -34,6 +34,8 @@ public:
|
|||
std::shared_ptr<Widget> main_widget() { return m_main_widget; }
|
||||
void set_main_widget(std::shared_ptr<Widget> main_widget);
|
||||
|
||||
std::shared_ptr<TopLevelStyles> top_level_styles() { return m_top_level_styles; }
|
||||
|
||||
void repaint();
|
||||
void repaint(Box geometry);
|
||||
void repaint(Widget *target);
|
||||
|
@ -41,7 +43,7 @@ public:
|
|||
void relayout();
|
||||
void reflow();
|
||||
|
||||
Box &rect() { return m_rect; }
|
||||
Box ¤t_geometry() { return m_current_geometry; }
|
||||
|
||||
template<typename T, class... Args>
|
||||
std::shared_ptr<T> set_main_widget(Args&&... args) {
|
||||
|
@ -53,8 +55,9 @@ private:
|
|||
Widget *m_focused_widget { nullptr };
|
||||
Widget *m_active_widget { nullptr };
|
||||
std::shared_ptr<Widget> m_main_widget { nullptr };
|
||||
Box m_rect { 0, 0, 800, 600 };
|
||||
Box m_current_geometry { 0, 0, 800, 600 };
|
||||
Painter m_painter {};
|
||||
std::shared_ptr<TopLevelStyles> m_top_level_styles = std::make_shared<TopLevelStyles>(this);
|
||||
Cairo::RefPtr<Cairo::XlibSurface> m_xlib_surface { nullptr };
|
||||
bool m_is_batching { true };
|
||||
bool m_did_relayout_during_batch { true };
|
||||
|
|
|
@ -6,8 +6,8 @@
|
|||
#include "Label.hpp"
|
||||
#include "Layout.hpp"
|
||||
#include "RGB.hpp"
|
||||
#include "DocumentLayout.hpp"
|
||||
#include "Events.hpp"
|
||||
#include "src/DocumentLayout.hpp"
|
||||
#include "src/Events.hpp"
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
@ -17,10 +17,9 @@ int main() {
|
|||
window.spawn_window();
|
||||
|
||||
auto main_widget = window.set_main_widget<Raven::Widget>();
|
||||
main_widget->set_layout<Raven::DocumentLayout>(10.0);
|
||||
|
||||
auto second_widget = main_widget->add<Raven::Widget>();
|
||||
second_widget->set_layout<Raven::DocumentLayout>(20.0);
|
||||
second_widget->set_layout<Raven::DocumentLayout>(16.0);
|
||||
second_widget->resize(800, 800);
|
||||
|
||||
auto inner_widget = second_widget->add<Raven::Widget>();
|
||||
|
|
Loading…
Reference in a new issue