More window cleaning

This commit is contained in:
James Grogan 2022-11-11 14:22:31 +00:00
parent 6adc441e6f
commit 53c98a227d
29 changed files with 422 additions and 261 deletions

View file

@ -23,6 +23,7 @@ void MediaTool::initializeViews()
auto mainWindow = mDesktopManager->GetWindowManager()->GetMainWindow(); auto mainWindow = mDesktopManager->GetWindowManager()->GetMainWindow();
mainWindow->SetSize(800, 600); mainWindow->SetSize(800, 600);
/*
auto tabbedPanel = TabbedPanelWidget::Create(); auto tabbedPanel = TabbedPanelWidget::Create();
auto textEditor = TextEditorView::Create(); auto textEditor = TextEditorView::Create();
@ -50,5 +51,19 @@ void MediaTool::initializeViews()
horizontalSpace->AddWidgetWithScale(std::move(tabbedPanel), 20); horizontalSpace->AddWidgetWithScale(std::move(tabbedPanel), 20);
horizontalSpace->AddWidgetWithScale(std::move(statusBar), 1); horizontalSpace->AddWidgetWithScale(std::move(statusBar), 1);
mainWindow->AddWidget(std::move(horizontalSpace)); */
auto button = Button::Create();
button->setLabel("Click!");
button->setBounds(100, 200);
button->setBackgroundColor(Color::Create(0, 0, 255, 0));
auto background_widget = Widget::Create();
background_widget->setBackgroundColor(Color::Create(0, 255, 255, 0));
auto horizontal_spacer = HorizontalSpacer::Create();
horizontal_spacer->addWidgetWithScale(std::move(button), 1);
horizontal_spacer->addWidgetWithScale(std::move(background_widget), 1);
mainWindow->AddWidget(std::move(horizontal_spacer));
} }

View file

@ -6,10 +6,10 @@
AudioEditorView::AudioEditorView() AudioEditorView::AudioEditorView()
{ {
auto label = Label::Create(); auto label = Label::Create();
label->SetLabel("Audio Editor"); label->setLabel("Audio Editor");
label->SetBackgroundColor(Color::Create(200, 189, 160)); label->setBackgroundColor(Color::Create(200, 189, 160));
label->SetMargin(1); label->setMargin(1);
AddWidget(std::move(label)); addWidget(std::move(label));
} }
std::unique_ptr<AudioEditorView> AudioEditorView::Create() std::unique_ptr<AudioEditorView> AudioEditorView::Create()

View file

@ -11,8 +11,9 @@ int main(int argc, char *argv[])
args->recordLaunchPath(); args->recordLaunchPath();
// Start the gui app // Start the gui app
auto gui_app = GuiApplication(std::move(args)); auto app = MediaTool(std::move(args));
gui_app.run(); app.setUiInterfaceBackend(UiInterfaceFactory::Backend::X11_RASTER);
app.run();
return 0; return 0;
} }

View file

@ -6,10 +6,10 @@
ImageEditorView::ImageEditorView() ImageEditorView::ImageEditorView()
{ {
auto label = Label::Create(); auto label = Label::Create();
label->SetLabel("Image Editor"); label->setLabel("Image Editor");
label->SetBackgroundColor(Color::Create(200, 189, 160)); label->setBackgroundColor(Color::Create(200, 189, 160));
label->SetMargin(1); label->setMargin(1);
AddWidget(std::move(label)); addWidget(std::move(label));
} }
std::unique_ptr<ImageEditorView> ImageEditorView::Create() std::unique_ptr<ImageEditorView> ImageEditorView::Create()

View file

@ -18,17 +18,17 @@ TextEditorController* TextEditorView::GetController()
void TextEditorView::Initialize() void TextEditorView::Initialize()
{ {
auto label = Label::Create(); auto label = Label::Create();
label->SetLabel("Text Editor"); label->setLabel("Text Editor");
label->SetBackgroundColor(Color::Create(181, 189, 200)); label->setBackgroundColor(Color::Create(181, 189, 200));
label->SetMargin(1); label->setMargin(1);
auto textBox = TextBox::Create(); auto textBox = TextBox::Create();
mTextBox = textBox.get(); mTextBox = textBox.get();
auto saveButton = Button::Create(); auto saveButton = Button::Create();
saveButton->SetLabel("Save"); saveButton->setLabel("Save");
saveButton->SetBackgroundColor(Color::Create(200, 200, 200)); saveButton->setBackgroundColor(Color::Create(200, 200, 200));
saveButton->SetMargin(2); saveButton->setMargin(2);
auto onSave = [this](Widget* self){ auto onSave = [this](Widget* self){
if(this && mController && mTextBox) if(this && mController && mTextBox)
{ {
@ -36,12 +36,12 @@ void TextEditorView::Initialize()
mController->OnSave(); mController->OnSave();
}; };
}; };
saveButton->SetOnClickFunction(onSave); saveButton->setOnClickFunction(onSave);
auto clearButton = Button::Create(); auto clearButton = Button::Create();
clearButton->SetLabel("Clear"); clearButton->setLabel("Clear");
clearButton->SetBackgroundColor(Color::Create(200, 200, 200)); clearButton->setBackgroundColor(Color::Create(200, 200, 200));
clearButton->SetMargin(2); clearButton->setMargin(2);
auto onClear = [this](Widget* self){ auto onClear = [this](Widget* self){
if(this && mController && mTextBox) if(this && mController && mTextBox)
{ {
@ -49,12 +49,12 @@ void TextEditorView::Initialize()
mTextBox->SetContent(""); mTextBox->SetContent("");
}; };
}; };
clearButton->SetOnClickFunction(onClear); clearButton->setOnClickFunction(onClear);
auto loadButton = Button::Create(); auto loadButton = Button::Create();
loadButton->SetLabel("Load"); loadButton->setLabel("Load");
loadButton->SetBackgroundColor(Color::Create(200, 200, 200)); loadButton->setBackgroundColor(Color::Create(200, 200, 200));
loadButton->SetMargin(2); loadButton->setMargin(2);
auto onLoad = [this](Widget* self){ auto onLoad = [this](Widget* self){
if(this && mController && mTextBox) if(this && mController && mTextBox)
{ {
@ -62,7 +62,7 @@ void TextEditorView::Initialize()
mTextBox->SetContent(mController->GetContent()); mTextBox->SetContent(mController->GetContent());
}; };
}; };
loadButton->SetOnClickFunction(onLoad); loadButton->setOnClickFunction(onLoad);
auto buttonSpacer = VerticalSpacer::Create(); auto buttonSpacer = VerticalSpacer::Create();
buttonSpacer->AddWidgetWithScale(std::move(saveButton), 1); buttonSpacer->AddWidgetWithScale(std::move(saveButton), 1);
@ -70,11 +70,11 @@ void TextEditorView::Initialize()
buttonSpacer->AddWidgetWithScale(std::move(loadButton), 1); buttonSpacer->AddWidgetWithScale(std::move(loadButton), 1);
auto hSpacer = HorizontalSpacer::Create(); auto hSpacer = HorizontalSpacer::Create();
hSpacer->AddWidgetWithScale(std::move(label), 1); hSpacer->addWidgetWithScale(std::move(label), 1);
hSpacer->AddWidgetWithScale(std::move(textBox), 14); hSpacer->addWidgetWithScale(std::move(textBox), 14);
hSpacer->AddWidgetWithScale(std::move(buttonSpacer), 1); hSpacer->addWidgetWithScale(std::move(buttonSpacer), 1);
AddWidget(std::move(hSpacer)); addWidget(std::move(hSpacer));
} }
std::unique_ptr<TextEditorView> TextEditorView::Create() std::unique_ptr<TextEditorView> TextEditorView::Create()

View file

@ -6,10 +6,10 @@
WebClientView::WebClientView() WebClientView::WebClientView()
{ {
auto label = Label::Create(); auto label = Label::Create();
label->SetLabel("Web Client"); label->setLabel("Web Client");
label->SetBackgroundColor(Color::Create(200, 189, 160)); label->setBackgroundColor(Color::Create(200, 189, 160));
label->SetMargin(1); label->setMargin(1);
AddWidget(std::move(label)); addWidget(std::move(label));
} }
std::unique_ptr<WebClientView> WebClientView::Create() std::unique_ptr<WebClientView> WebClientView::Create()

View file

@ -4,7 +4,7 @@
StatusBar::StatusBar() StatusBar::StatusBar()
{ {
SetBackgroundColor(Color::Create(200, 200, 200)); setBackgroundColor(Color::Create(200, 200, 200));
} }
std::unique_ptr<StatusBar> StatusBar::Create() std::unique_ptr<StatusBar> StatusBar::Create()

View file

@ -13,12 +13,12 @@ TabbedPanelWidget::TabbedPanelWidget()
auto nav = HorizontalSpacer::Create(); auto nav = HorizontalSpacer::Create();
mNavPanel = nav.get(); mNavPanel = nav.get();
nav->SetSize({0, 0, 0, 0, 200, 0}); nav->setSize({0, 0, 0, 0, 200, 0});
auto vertSpacer = VerticalSpacer::Create(); auto vertSpacer = VerticalSpacer::Create();
vertSpacer->AddWidgetWithScale(std::move(nav), 1); vertSpacer->AddWidgetWithScale(std::move(nav), 1);
vertSpacer->AddWidgetWithScale(std::move(stack), 4); vertSpacer->AddWidgetWithScale(std::move(stack), 4);
AddWidget(std::move(vertSpacer)); addWidget(std::move(vertSpacer));
} }
std::unique_ptr<TabbedPanelWidget> TabbedPanelWidget::Create() std::unique_ptr<TabbedPanelWidget> TabbedPanelWidget::Create()
@ -34,9 +34,9 @@ StackWidget* TabbedPanelWidget::GetStack() const
void TabbedPanelWidget::AddPanel(WidgetUPtr panel, const std::string& label) void TabbedPanelWidget::AddPanel(WidgetUPtr panel, const std::string& label)
{ {
auto button = Button::Create(); auto button = Button::Create();
button->SetLabel(label); button->setLabel(label);
button->SetBackgroundColor(Color::Create(156, 156, 156)); button->setBackgroundColor(Color::Create(156, 156, 156));
button->SetMargin({1, 0, 0, 1}); button->setMargin({1, 0, 0, 1});
auto rawPanel = panel.get(); auto rawPanel = panel.get();
auto onClick = [this, rawPanel](Widget*){ auto onClick = [this, rawPanel](Widget*){
if(this && rawPanel) if(this && rawPanel)
@ -44,9 +44,9 @@ void TabbedPanelWidget::AddPanel(WidgetUPtr panel, const std::string& label)
this->GetStack()->ShowChild(rawPanel); this->GetStack()->ShowChild(rawPanel);
} }
}; };
button->SetOnClickFunction(onClick); button->setOnClickFunction(onClick);
mStack->AddWidget(std::move(panel)); mStack->addWidget(std::move(panel));
mNavPanel->AddWidget(std::move(button)); mNavPanel->addWidget(std::move(button));
} }

View file

@ -4,7 +4,7 @@
TopBar::TopBar() TopBar::TopBar()
{ {
SetBackgroundColor(Color::Create(50, 50, 50)); setBackgroundColor(Color::Create(50, 50, 50));
} }
std::unique_ptr<TopBar> TopBar::Create() std::unique_ptr<TopBar> TopBar::Create()

View file

@ -18,6 +18,16 @@ public:
unsigned GetX() const; unsigned GetX() const;
unsigned GetY() const; unsigned GetY() const;
bool operator==(const DiscretePoint& rhs) const
{
return (mX == rhs.mX)
&& (mY == rhs.mY);
}
bool operator!=(const DiscretePoint& rhs) const
{
return !operator==(rhs);
}
}; };
using Pixel = DiscretePoint; using Pixel = DiscretePoint;

View file

@ -14,7 +14,7 @@ Window::Window()
mHeight(600), mHeight(600),
mWidget(Widget::Create()) mWidget(Widget::Create())
{ {
mWidget->setBounds(mWidth, mHeight);
} }
Window::~Window() Window::~Window()
@ -29,23 +29,22 @@ std::unique_ptr<Window> Window::Create()
std::vector<VisualLayer*> Window::GetLayers() std::vector<VisualLayer*> Window::GetLayers()
{ {
return mWidget->GetLayers(); return mWidget->getLayers();
} }
void Window::OnMouseEvent(const MouseEvent* event) void Window::OnMouseEvent(const MouseEvent* event)
{ {
mWidget->OnMouseEvent(event); mWidget->onMouseEvent(event);
} }
void Window::OnKeyboardEvent(const KeyboardEvent* event) void Window::OnKeyboardEvent(const KeyboardEvent* event)
{ {
mWidget->OnKeyboardEvent(event); mWidget->onKeyboardEvent(event);
} }
void Window::OnPaint(const PaintEvent* event) void Window::OnPaint(const PaintEvent* event)
{ {
mWidget->SetBounds(mWidth, mHeight); mWidget->onPaintEvent(event);
mWidget->OnPaintEvent(event);
} }
void Window::AddWidget(WidgetUPtr widget) void Window::AddWidget(WidgetUPtr widget)
@ -55,6 +54,7 @@ void Window::AddWidget(WidgetUPtr widget)
mWidget.reset(); mWidget.reset();
} }
mWidget = std::move(widget); mWidget = std::move(widget);
mWidget->setBounds(mWidth, mHeight);
} }
Widget* Window::GetWidget() const Widget* Window::GetWidget() const
@ -76,6 +76,7 @@ void Window::SetSize(unsigned width, unsigned height)
{ {
mWidth = width; mWidth = width;
mHeight = height; mHeight = height;
mWidget->setBounds(mWidth, mHeight);
} }
IPlatformWindow* Window::GetPlatformWindow() const IPlatformWindow* Window::GetPlatformWindow() const

View file

@ -21,22 +21,26 @@ std::unique_ptr<Button> Button::Create()
return std::make_unique<Button>(); return std::make_unique<Button>();
} }
void Button::SetOnClickFunction(clickFunc func) void Button::setOnClickFunction(clickFunc func)
{ {
mClickFunc = func; mClickFunc = func;
} }
void Button::SetLabel(const std::string& text) void Button::setLabel(const std::string& text)
{ {
if (text != mLabel)
{
mLabel = text; mLabel = text;
mDirty = true;
}
} }
void Button::OnMyMouseEvent(const MouseEvent* event) void Button::onMyMouseEvent(const MouseEvent* event)
{ {
if(event->GetAction() == MouseEvent::Action::Pressed) if(event->GetAction() == MouseEvent::Action::Pressed)
{ {
mCachedColor = *mBackgroundColor; mCachedColor = *mBackgroundColor;
SetBackgroundColor(Color::Create(*mClickedColor)); setBackgroundColor(Color::Create(*mClickedColor));
if(mClickFunc) if(mClickFunc)
{ {
mClickFunc(this); mClickFunc(this);
@ -44,24 +48,39 @@ void Button::OnMyMouseEvent(const MouseEvent* event)
} }
else if(event->GetAction() == MouseEvent::Action::Released) else if(event->GetAction() == MouseEvent::Action::Released)
{ {
SetBackgroundColor(Color::Create(mCachedColor)); setBackgroundColor(Color::Create(mCachedColor));
} }
} }
void Button::OnPaintEvent(const PaintEvent* event) void Button::onPaintEvent(const PaintEvent* event)
{ {
if (!needsUpdate())
{
return;
}
mLayers.clear();
if (mDirty)
{
mMyLayers.clear(); mMyLayers.clear();
AddBackground(event); if(!mVisible)
{
return;
}
addBackground(event);
if(!mLabel.empty()) if(!mLabel.empty())
{ {
unsigned fontOffset = unsigned(mLabel.size()) * 4; unsigned fontOffset = unsigned(mLabel.size()) * 4;
auto middle = DiscretePoint(mLocation.GetX() + mSize.mWidth/2 - fontOffset, auto middle = DiscretePoint(mLocation.GetX() + mSize.mWidth/2 - fontOffset, mLocation.GetY() + mSize.mHeight/2 + 4);
mLocation.GetY() + mSize.mHeight/2 + 4);
auto textLayer = VisualLayer::Create(); auto textLayer = VisualLayer::Create();
auto textElement = TextElement::Create(mLabel, middle); auto textElement = TextElement::Create(mLabel, middle);
textElement->SetFillColor(Color::Create(*mBackgroundColor)); textElement->SetFillColor(Color::Create(*mBackgroundColor));
textLayer->SetText(std::move(textElement)); textLayer->SetText(std::move(textElement));
mMyLayers.push_back(std::move(textLayer)); mMyLayers.push_back(std::move(textLayer));
} }
CopyMyLayers(); mDirty = false;
addMyLayers();
}
} }

View file

@ -11,27 +11,27 @@ class MouseEvent;
class Button : public Widget class Button : public Widget
{ {
private:
using clickFunc = std::function<void(Widget* self)>;
std::string mLabel;
clickFunc mClickFunc;
Color mCachedColor;
ColorUPtr mClickedColor;
public: public:
using clickFunc = std::function<void(Widget* self)>;
Button(); Button();
static std::unique_ptr<Button> Create(); static std::unique_ptr<Button> Create();
void SetLabel(const std::string& text); void setLabel(const std::string& text);
void SetOnClickFunction(clickFunc func); void setOnClickFunction(clickFunc func);
void OnPaintEvent(const PaintEvent* event) override;
protected: protected:
void onMyMouseEvent(const MouseEvent* event) override;
void OnMyMouseEvent(const MouseEvent* event) override; void onPaintEvent(const PaintEvent* event) override;
private:
std::string mLabel;
clickFunc mClickFunc;
Color mCachedColor;
ColorUPtr mClickedColor;
}; };
using ButtonUPtr = std::unique_ptr<Button>; using ButtonUPtr = std::unique_ptr<Button>;

View file

@ -15,20 +15,19 @@ std::unique_ptr<HorizontalSpacer> HorizontalSpacer::Create()
return std::make_unique<HorizontalSpacer>(); return std::make_unique<HorizontalSpacer>();
} }
void HorizontalSpacer::AddWidget(WidgetUPtr widget) void HorizontalSpacer::addWidget(WidgetUPtr widget)
{ {
AddWidgetWithScale(std::move(widget), 1.0); addWidgetWithScale(std::move(widget), 1.0);
} }
void HorizontalSpacer::AddWidgetWithScale(WidgetUPtr widget, double scale) void HorizontalSpacer::addWidgetWithScale(WidgetUPtr widget, double scale)
{ {
Widget::AddWidget(std::move(widget)); Widget::addWidget(std::move(widget));
mScales.push_back(scale); mScales.push_back(scale);
} }
void HorizontalSpacer::AddChildLayers(const PaintEvent* event) void HorizontalSpacer::addChildLayers(const PaintEvent* event)
{ {
mLayers.clear();
double scaleSum = std::accumulate(mScales.begin(), mScales.end(), 0.0); double scaleSum = std::accumulate(mScales.begin(), mScales.end(), 0.0);
double offset = 0; double offset = 0;
double height = mSize.mHeight; double height = mSize.mHeight;
@ -41,21 +40,27 @@ void HorizontalSpacer::AddChildLayers(const PaintEvent* event)
auto& child = mChildren[idx]; auto& child = mChildren[idx];
double scale = mScales[idx]; double scale = mScales[idx];
double delta = height * (scale/scaleSum); double delta = height * (scale/scaleSum);
auto size = child->GetSize(); auto size = child->getSize();
if (size.mMaxHeight > 0 && delta > size.mMaxHeight) if (size.mMaxHeight > 0 && delta > size.mMaxHeight)
{ {
delta = size.mMaxHeight; delta = size.mMaxHeight;
} }
child->SetBounds(mSize.mWidth, unsigned(delta)); child->setBounds(mSize.mWidth, unsigned(delta));
child->SetLocation(DiscretePoint(mLocation.GetX(), mLocation.GetY() + unsigned(offset))); child->setLocation(DiscretePoint(mLocation.GetX(), mLocation.GetY() + unsigned(offset)));
child->OnPaintEvent(event); child->onPaintEvent(event);
auto layers = child->GetLayers(); auto layers = child->getLayers();
mLayers.insert(mLayers.end(), layers.begin(), layers.end()); mLayers.insert(mLayers.end(), layers.begin(), layers.end());
offset += delta; offset += delta;
} }
} }
void HorizontalSpacer::OnPaintEvent(const PaintEvent* event) void HorizontalSpacer::onPaintEvent(const PaintEvent* event)
{ {
AddChildLayers(event); if (!needsUpdate())
{
return;
}
mLayers.clear();
addChildLayers(event);
} }

View file

@ -12,13 +12,13 @@ public:
static std::unique_ptr<HorizontalSpacer> Create(); static std::unique_ptr<HorizontalSpacer> Create();
void AddWidget(WidgetUPtr widget) override; void addWidget(WidgetUPtr widget) override;
void AddWidgetWithScale(WidgetUPtr widget, double scale); void addWidgetWithScale(WidgetUPtr widget, double scale);
void AddChildLayers(const PaintEvent* event) override; void addChildLayers(const PaintEvent* event) override;
void OnPaintEvent(const PaintEvent* event) override; void onPaintEvent(const PaintEvent* event) override;
}; };
using HorizontalSpacerUPtr = std::unique_ptr<HorizontalSpacer>; using HorizontalSpacerUPtr = std::unique_ptr<HorizontalSpacer>;

View file

@ -16,15 +16,30 @@ std::unique_ptr<Label> Label::Create()
return std::make_unique<Label>(); return std::make_unique<Label>();
} }
void Label::SetLabel(const std::string& text) void Label::setLabel(const std::string& text)
{ {
if (text != mLabel)
{
mLabel = text; mLabel = text;
mDirty = true;
}
} }
void Label::OnPaintEvent(const PaintEvent* event) void Label::onPaintEvent(const PaintEvent* event)
{ {
if (!needsUpdate())
{
return;
}
mLayers.clear();
if (mDirty)
{
mMyLayers.clear(); mMyLayers.clear();
AddBackground(event); if(!mVisible)
{
return;
}
if(!mLabel.empty()) if(!mLabel.empty())
{ {
@ -37,5 +52,6 @@ void Label::OnPaintEvent(const PaintEvent* event)
textLayer->SetText(std::move(textElement)); textLayer->SetText(std::move(textElement));
mMyLayers.push_back(std::move(textLayer)); mMyLayers.push_back(std::move(textLayer));
} }
CopyMyLayers(); addMyLayers();
}
} }

View file

@ -16,9 +16,9 @@ public:
static std::unique_ptr<Label> Create(); static std::unique_ptr<Label> Create();
void SetLabel(const std::string& text); void setLabel(const std::string& text);
void OnPaintEvent(const PaintEvent* event) override; void onPaintEvent(const PaintEvent* event) override;
}; };
using LabelUPtr = std::unique_ptr<Label>; using LabelUPtr = std::unique_ptr<Label>;

View file

@ -16,11 +16,11 @@ void StackWidget::ShowChild(Widget* target)
{ {
if(child.get() == target) if(child.get() == target)
{ {
child->SetVisible(true); child->setVisible(true);
} }
else else
{ {
child->SetVisible(false); child->setVisible(false);
} }
} }
} }

View file

@ -36,7 +36,7 @@ void TextBox::AppendContent(const std::string& text)
mContent += text; mContent += text;
} }
bool TextBox::OnMyKeyboardEvent(const KeyboardEvent* event) bool TextBox::onMyKeyboardEvent(const KeyboardEvent* event)
{ {
if(!event) return false; if(!event) return false;
@ -72,10 +72,10 @@ bool TextBox::OnMyKeyboardEvent(const KeyboardEvent* event)
return true; return true;
} }
void TextBox::OnPaintEvent(const PaintEvent* event) void TextBox::onPaintEvent(const PaintEvent* event)
{ {
mMyLayers.clear(); mMyLayers.clear();
AddBackground(event); addBackground(event);
double offset = 0; double offset = 0;
if(!mContent.empty()) if(!mContent.empty())
@ -99,5 +99,5 @@ void TextBox::OnPaintEvent(const PaintEvent* event)
offset += 20; offset += 20;
} }
} }
CopyMyLayers(); addMyLayers();
} }

View file

@ -23,9 +23,9 @@ public:
void AppendContent(const std::string& text); void AppendContent(const std::string& text);
void OnPaintEvent(const PaintEvent* event) override; void onPaintEvent(const PaintEvent* event) override;
bool OnMyKeyboardEvent(const KeyboardEvent* event) override; bool onMyKeyboardEvent(const KeyboardEvent* event) override;
}; };
using TextBoxUPtr = std::unique_ptr<TextBox>; using TextBoxUPtr = std::unique_ptr<TextBox>;

View file

@ -15,18 +15,18 @@ std::unique_ptr<VerticalSpacer> VerticalSpacer::Create()
return std::make_unique<VerticalSpacer>(); return std::make_unique<VerticalSpacer>();
} }
void VerticalSpacer::AddWidget(WidgetUPtr widget) void VerticalSpacer::addWidget(WidgetUPtr widget)
{ {
AddWidgetWithScale(std::move(widget), 1.0); AddWidgetWithScale(std::move(widget), 1.0);
} }
void VerticalSpacer::AddWidgetWithScale(WidgetUPtr widget, double scale) void VerticalSpacer::AddWidgetWithScale(WidgetUPtr widget, double scale)
{ {
Widget::AddWidget(std::move(widget)); Widget::addWidget(std::move(widget));
mScales.push_back(scale); mScales.push_back(scale);
} }
void VerticalSpacer::AddChildLayers(const PaintEvent* event) void VerticalSpacer::addChildLayers(const PaintEvent* event)
{ {
mLayers.clear(); mLayers.clear();
double scaleSum = std::accumulate(mScales.begin(), mScales.end(), 0.0); double scaleSum = std::accumulate(mScales.begin(), mScales.end(), 0.0);
@ -37,16 +37,16 @@ void VerticalSpacer::AddChildLayers(const PaintEvent* event)
auto& child = mChildren[idx]; auto& child = mChildren[idx];
double scale = mScales[idx]; double scale = mScales[idx];
double delta = mSize.mWidth * (scale/scaleSum); double delta = mSize.mWidth * (scale/scaleSum);
child->SetBounds(unsigned(delta), mSize.mHeight); child->setBounds(unsigned(delta), mSize.mHeight);
child->SetLocation(DiscretePoint(mLocation.GetX() + unsigned(offset), mLocation.GetY())); child->setLocation(DiscretePoint(mLocation.GetX() + unsigned(offset), mLocation.GetY()));
child->OnPaintEvent(event); child->onPaintEvent(event);
auto layers = child->GetLayers(); auto layers = child->getLayers();
mLayers.insert(mLayers.end(), layers.begin(), layers.end()); mLayers.insert(mLayers.end(), layers.begin(), layers.end());
offset += delta; offset += delta;
} }
} }
void VerticalSpacer::OnPaintEvent(const PaintEvent* event) void VerticalSpacer::onPaintEvent(const PaintEvent* event)
{ {
AddChildLayers(event); addChildLayers(event);
} }

View file

@ -11,13 +11,13 @@ public:
static std::unique_ptr<VerticalSpacer> Create(); static std::unique_ptr<VerticalSpacer> Create();
void AddWidget(WidgetUPtr widget) override; void addWidget(WidgetUPtr widget) override;
void AddWidgetWithScale(WidgetUPtr widget, double scale); void AddWidgetWithScale(WidgetUPtr widget, double scale);
void AddChildLayers(const PaintEvent* event) override; void addChildLayers(const PaintEvent* event) override;
void OnPaintEvent(const PaintEvent* event) override; void onPaintEvent(const PaintEvent* event) override;
}; };
using VerticalSpacerUPtr = std::unique_ptr<VerticalSpacer>; using VerticalSpacerUPtr = std::unique_ptr<VerticalSpacer>;

View file

@ -10,6 +10,7 @@
#include <algorithm> #include <algorithm>
#include <iterator> #include <iterator>
#include <iostream>
Widget::Widget() Widget::Widget()
: mLocation(DiscretePoint(0, 0)), : mLocation(DiscretePoint(0, 0)),
@ -37,52 +38,102 @@ std::unique_ptr<Widget> Widget::Create()
return std::make_unique<Widget>(); return std::make_unique<Widget>();
} }
void Widget::AddWidget(WidgetUPtr widget) void Widget::addWidget(WidgetUPtr widget)
{ {
mChildren.push_back(std::move(widget)); mChildren.push_back(std::move(widget));
} }
Widget::BoundedSize Widget::GetSize() const Widget::BoundedSize Widget::getSize() const
{ {
return mSize; return mSize;
} }
DiscretePoint Widget::GetLocation() const DiscretePoint Widget::getLocation() const
{ {
return mLocation; return mLocation;
} }
std::vector<VisualLayer*> Widget::GetLayers() const std::vector<VisualLayer*> Widget::getLayers() const
{ {
return mLayers; return mLayers;
} }
void Widget::SetSize(const BoundedSize& size) void Widget::setSize(const BoundedSize& size)
{ {
if (size != mSize)
{
mSize = size; mSize = size;
mDirty = true;
}
} }
void Widget::SetMargin(unsigned margin) void Widget::setMargin(unsigned margin)
{ {
mMargin = {margin, margin, margin, margin}; setMargin({margin, margin, margin, margin});
} }
void Widget::SetMargin(const BoundaryOffset& margin) void Widget::setMargin(const BoundaryOffset& margin)
{ {
if (margin != mMargin)
{
mMargin = margin; mMargin = margin;
mDirty = true;
}
} }
void Widget::SetPadding(unsigned padding) void Widget::setPadding(unsigned padding)
{ {
mPadding = {padding, padding, padding, padding}; setPadding({padding, padding, padding, padding});
} }
void Widget::SetPadding(const BoundaryOffset& padding) void Widget::setPadding(const BoundaryOffset& padding)
{ {
if (padding != mPadding)
{
mPadding = padding; mPadding = padding;
mDirty = true;
}
} }
void Widget::CopyMyLayers() void Widget::setBounds(unsigned width, unsigned height)
{
if (width != mSize.mWidth || height != mSize.mHeight)
{
mDirty = true;
}
mSize.mWidth = width;
mSize.mHeight = height;
}
void Widget::setBackgroundColor(ColorUPtr color)
{
if (mBackgroundColor != color)
{
mBackgroundColor = std::move(color);
mDirty = true;
}
}
void Widget::setLocation(const DiscretePoint& loc)
{
if (mLocation != loc)
{
mLocation = loc;
mDirty = true;
}
}
void Widget::setVisible(bool visible)
{
if (mVisible != visible)
{
mDirty = true;
}
mVisible = visible;
}
void Widget::addMyLayers()
{ {
mLayers.clear(); mLayers.clear();
mLayers.reserve(mMyLayers.size()); mLayers.reserve(mMyLayers.size());
@ -90,73 +141,107 @@ void Widget::CopyMyLayers()
std::transform(mMyLayers.begin(), mMyLayers.end(), std::back_inserter(mLayers), getRaw); std::transform(mMyLayers.begin(), mMyLayers.end(), std::back_inserter(mLayers), getRaw);
} }
void Widget::SetBounds(unsigned width, unsigned height) void Widget::addChildLayers(const PaintEvent* event)
{
mSize.mWidth = width;
mSize.mHeight = height;
}
void Widget::AddChildLayers(const PaintEvent* event)
{ {
for(auto& child: mChildren) for(auto& child: mChildren)
{ {
child->SetBounds(mSize.mWidth, mSize.mHeight); child->setBounds(mSize.mWidth, mSize.mHeight);
child->SetLocation(mLocation); child->setLocation(mLocation);
child->OnPaintEvent(event); child->onPaintEvent(event);
const auto layers = child->GetLayers(); const auto layers = child->getLayers();
mLayers.insert(mLayers.end(), layers.begin(), layers.end()); mLayers.insert(mLayers.end(), layers.begin(), layers.end());
} }
} }
void Widget::OnPaintEvent(const PaintEvent* event) bool Widget::needsUpdate() const
{ {
mMyLayers.clear(); if (mDirty)
mLayers.clear(); {
return true;
if(!mVisible)return; }
AddBackground(event); for(auto& child: mChildren)
CopyMyLayers(); {
AddChildLayers(event); if (child->needsUpdate())
{
return true;
}
}
return false;
} }
bool Widget::Contains(const DiscretePoint& loc) const void Widget::onPaintEvent(const PaintEvent* event)
{ {
if(loc.GetX() < mLocation.GetX()) return false; if (!needsUpdate())
if(loc.GetX() > mLocation.GetX() + mSize.mWidth) return false; {
if(loc.GetY() > mLocation.GetY() + mSize.mHeight) return false; return;
if(loc.GetY() < mLocation.GetY()) return false; }
mLayers.clear();
if (mDirty)
{
mMyLayers.clear();
if(!mVisible)
{
return;
}
addBackground(event);
mDirty = false;
}
addMyLayers();
addChildLayers(event);
}
bool Widget::contains(const DiscretePoint& loc) const
{
if(loc.GetX() < mLocation.GetX())
{
return false;
}
if(loc.GetX() > mLocation.GetX() + mSize.mWidth)
{
return false;
}
if(loc.GetY() > mLocation.GetY() + mSize.mHeight)
{
return false;
}
if(loc.GetY() < mLocation.GetY())
{
return false;
}
return true; return true;
} }
bool Widget::OnKeyboardEvent(const KeyboardEvent* event) bool Widget::onKeyboardEvent(const KeyboardEvent* event)
{ {
bool inChild = false; bool inChild = false;
for(const auto& child : mChildren) for(const auto& child : mChildren)
{ {
if(child->OnKeyboardEvent(event)) if(child->onKeyboardEvent(event))
{ {
inChild = true; inChild = true;
break; break;
} }
} }
if(!inChild) if(!inChild)
{ {
return OnMyKeyboardEvent(event); return onMyKeyboardEvent(event);
} }
return true; return true;
} }
bool Widget::OnMyKeyboardEvent(const KeyboardEvent* event) bool Widget::onMyKeyboardEvent(const KeyboardEvent* event)
{ {
return false; return false;
} }
bool Widget::OnMouseEvent(const MouseEvent* event) bool Widget::onMouseEvent(const MouseEvent* event)
{ {
bool inChild = false; bool inChild = false;
for(const auto& child : mChildren) for(const auto& child : mChildren)
{ {
if(child->OnMouseEvent(event)) if(child->onMouseEvent(event))
{ {
inChild = true; inChild = true;
break; break;
@ -164,9 +249,9 @@ bool Widget::OnMouseEvent(const MouseEvent* event)
} }
if(!inChild) if(!inChild)
{ {
if(Contains(event->GetClientLocation())) if(contains(event->GetClientLocation()))
{ {
OnMyMouseEvent(event); onMyMouseEvent(event);
return true; return true;
} }
else else
@ -177,36 +262,27 @@ bool Widget::OnMouseEvent(const MouseEvent* event)
return true; return true;
} }
void Widget::OnMyMouseEvent(const MouseEvent* event) void Widget::onMyMouseEvent(const MouseEvent* event)
{ {
} }
void Widget::AddBackground(const PaintEvent* event) void Widget::addBackground(const PaintEvent* event)
{ {
unsigned locX = mLocation.GetX() + mMargin.mLeft; unsigned locX = mLocation.GetX() + mMargin.mLeft;
unsigned locY = mLocation.GetY() + mMargin.mTop; unsigned locY = mLocation.GetY() + mMargin.mTop;
unsigned deltaX = mSize.mWidth - mMargin.mLeft - mMargin.mRight; unsigned deltaX = mSize.mWidth - mMargin.mLeft - mMargin.mRight;
unsigned deltaY = mSize.mHeight - mMargin.mTop - mMargin.mBottom; unsigned deltaY = mSize.mHeight - mMargin.mTop - mMargin.mBottom;
auto shape = RectangleElement::Create(DiscretePoint(locX, locY), auto shape = RectangleElement::Create(DiscretePoint(locX, locY), deltaX, deltaY);
deltaX, deltaY);
shape->SetFillColor(Color::Create(*mBackgroundColor)); std::cout << "Adding shape at : " << locX << " | " << locY << std::endl;
std::cout << "Has dimensions : " << deltaX << " | " << deltaY << std::endl;
auto color = Color::Create(*mBackgroundColor);
std::cout << "Has color : " << color->GetR() << " | " << color->GetG() << " | " << color->GetB() << " | " << color->GetAlpha() << std::endl;
shape->SetFillColor(std::move(color));
auto shapeLayer = VisualLayer::Create(); auto shapeLayer = VisualLayer::Create();
shapeLayer->SetShape(std::move(shape)); shapeLayer->SetShape(std::move(shape));
mMyLayers.push_back(std::move(shapeLayer)); mMyLayers.push_back(std::move(shapeLayer));
} }
void Widget::SetBackgroundColor(ColorUPtr color)
{
mBackgroundColor = std::move(color);
}
void Widget::SetLocation(const DiscretePoint& loc)
{
mLocation = loc;
}
void Widget::SetVisible(bool visible)
{
mVisible = visible;
}

View file

@ -15,14 +15,42 @@ class Color;
class Widget class Widget
{ {
public: public:
struct BoundaryOffset{ struct BoundaryOffset
{
bool operator==(const BoundaryOffset& rhs) const
{
return (mLeft == rhs.mLeft)
&& (mRight == rhs.mRight)
&& (mTop == rhs.mTop)
&& (mBottom == rhs.mBottom);
}
bool operator!=(const BoundaryOffset& rhs) const
{
return !operator==(rhs);
}
unsigned mLeft = 0; unsigned mLeft = 0;
unsigned mRight = 0; unsigned mRight = 0;
unsigned mTop = 0; unsigned mTop = 0;
unsigned mBottom = 0; unsigned mBottom = 0;
}; };
struct BoundedSize{ struct BoundedSize
{
bool operator==(const BoundedSize& rhs) const
{
return (mWidth == rhs.mWidth)
&& (mMaxWidth == rhs.mMaxWidth)
&& (mMinWidth == rhs.mMinWidth)
&& (mHeight == rhs.mHeight)
&& (mMaxHeight == rhs.mMaxHeight)
&& (mMinHeight == rhs.mMinHeight);
}
bool operator!=(const BoundedSize& rhs) const
{
return !operator==(rhs);
}
unsigned mWidth = 0; unsigned mWidth = 0;
unsigned mMaxWidth = 0; unsigned mMaxWidth = 0;
unsigned mMinWidth = 0; unsigned mMinWidth = 0;
@ -31,19 +59,6 @@ public:
unsigned mMinHeight = 0; unsigned mMinHeight = 0;
}; };
protected:
DiscretePoint mLocation;
BoundedSize mSize;
BoundaryOffset mPadding;
BoundaryOffset mMargin;
std::vector<std::unique_ptr<VisualLayer> > mMyLayers;
std::vector<VisualLayer*> mLayers;
std::vector<std::unique_ptr<Widget> > mChildren;
unsigned mBorderThickness;
std::unique_ptr<Color> mBackgroundColor;
std::unique_ptr<Color> mBorderColor;
bool mVisible;
public: public:
Widget(); Widget();
@ -52,51 +67,66 @@ public:
static std::unique_ptr<Widget> Create(); static std::unique_ptr<Widget> Create();
virtual void AddWidget(std::unique_ptr<Widget> widget); virtual void addWidget(std::unique_ptr<Widget> widget);
BoundedSize GetSize() const; BoundedSize getSize() const;
std::vector<VisualLayer*> GetLayers() const; std::vector<VisualLayer*> getLayers() const;
DiscretePoint GetLocation() const; DiscretePoint getLocation() const;
virtual void OnPaintEvent(const PaintEvent* event); virtual void onPaintEvent(const PaintEvent* event);
virtual bool OnMouseEvent(const MouseEvent* event); virtual bool onMouseEvent(const MouseEvent* event);
virtual bool OnKeyboardEvent(const KeyboardEvent* event); virtual bool onKeyboardEvent(const KeyboardEvent* event);
bool Contains(const DiscretePoint& loc) const; bool contains(const DiscretePoint& loc) const;
void SetBackgroundColor(std::unique_ptr<Color> color); void setBackgroundColor(std::unique_ptr<Color> color);
void SetBounds(unsigned width, unsigned height); void setBounds(unsigned width, unsigned height);
void SetSize(const BoundedSize& size); void setSize(const BoundedSize& size);
void SetMargin(unsigned margin); void setMargin(unsigned margin);
void SetMargin(const BoundaryOffset& margin); void setMargin(const BoundaryOffset& margin);
void SetPadding(unsigned padding); void setPadding(unsigned padding);
void SetPadding(const BoundaryOffset& padding); void setPadding(const BoundaryOffset& padding);
void SetLocation(const DiscretePoint& loc); void setLocation(const DiscretePoint& loc);
void SetVisible(bool visible); void setVisible(bool visible);
protected: protected:
virtual bool onMyKeyboardEvent(const KeyboardEvent* event);
virtual bool OnMyKeyboardEvent(const KeyboardEvent* event); virtual void onMyMouseEvent(const MouseEvent* event);
virtual void OnMyMouseEvent(const MouseEvent* event); virtual void addChildLayers(const PaintEvent* event);
virtual void AddChildLayers(const PaintEvent* event); virtual void addBackground(const PaintEvent* event);
virtual void AddBackground(const PaintEvent* event); void addMyLayers();
void CopyMyLayers(); bool needsUpdate() const;
protected:
DiscretePoint mLocation;
BoundedSize mSize;
BoundaryOffset mPadding;
BoundaryOffset mMargin;
std::vector<std::unique_ptr<VisualLayer> > mMyLayers;
std::vector<VisualLayer*> mLayers;
std::vector<std::unique_ptr<Widget> > mChildren;
unsigned mBorderThickness{0};
std::unique_ptr<Color> mBackgroundColor;
std::unique_ptr<Color> mBorderColor;
bool mVisible{false};
bool mDirty{true};
}; };
using WidgetUPtr = std::unique_ptr<Widget>; using WidgetUPtr = std::unique_ptr<Widget>;

View file

@ -32,7 +32,6 @@ void DesktopManager::ClearEvents()
void DesktopManager::OnKeyboardEvent(const KeyboardEvent* event) void DesktopManager::OnKeyboardEvent(const KeyboardEvent* event)
{ {
MLOG_INFO("Got key event: " << event->GetKeyString());
GetWindowManager()->OnKeyboardEvent(event); GetWindowManager()->OnKeyboardEvent(event);
} }

View file

@ -10,9 +10,7 @@
class WindowManager class WindowManager
{ {
public: public:
WindowManager(); WindowManager();
~WindowManager(); ~WindowManager();

View file

@ -180,8 +180,7 @@ void XcbInterface::onPaint()
auto mainWindow = mDesktopManager->GetWindowManager()->GetMainWindow(); auto mainWindow = mDesktopManager->GetWindowManager()->GetMainWindow();
auto defaultScreen = mDesktopManager->GetDefaultScreen(); auto defaultScreen = mDesktopManager->GetDefaultScreen();
auto xcb_screen = dynamic_cast<XcbScreen*>(defaultScreen->GetPlatformScreen()); auto xcb_screen = dynamic_cast<XcbScreen*>(defaultScreen->GetPlatformScreen());
mXcbWindowInterface->Paint(mainWindow, mConnection, mXcbWindowInterface->Paint(mainWindow, mConnection, xcb_screen->GetNativeScreen(), xcb_screen->GetGraphicsContext());
xcb_screen->GetNativeScreen(), xcb_screen->GetGraphicsContext());
} }
void XcbInterface::onExposeEvent(xcb_expose_event_t* event) void XcbInterface::onExposeEvent(xcb_expose_event_t* event)

View file

@ -8,8 +8,7 @@
uint32_t XcbLayerInterface::GetColor(const Color* color) uint32_t XcbLayerInterface::GetColor(const Color* color)
{ {
return XcbLayerInterface::GetColor(color->GetR(), return XcbLayerInterface::GetColor(color->GetR(), color->GetG(), color->GetB());
color->GetG(), color->GetB());
} }
uint32_t XcbLayerInterface::GetColor(int r, int g, int b) uint32_t XcbLayerInterface::GetColor(int r, int g, int b)
@ -17,8 +16,7 @@ uint32_t XcbLayerInterface::GetColor(int r, int g, int b)
return b + (g<<8) + (r<<16); return b + (g<<8) + (r<<16);
} }
void XcbLayerInterface::ModifyGcColor(xcb_connection_t* connection, xcb_gcontext_t gc, void XcbLayerInterface::ModifyGcColor(xcb_connection_t* connection, xcb_gcontext_t gc, const Color* color)
const Color* color)
{ {
uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES; uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES;
uint32_t values[2] = {XcbLayerInterface::GetColor(color), 0}; uint32_t values[2] = {XcbLayerInterface::GetColor(color), 0};
@ -26,9 +24,7 @@ void XcbLayerInterface::ModifyGcColor(xcb_connection_t* connection, xcb_gcontext
xcb_change_gc(connection, gc, mask, values); xcb_change_gc(connection, gc, mask, values);
} }
void XcbLayerInterface::AddLayer(xcb_connection_t* connection, void XcbLayerInterface::AddLayer(xcb_connection_t* connection, xcb_screen_t* screen, xcb_window_t window, xcb_gcontext_t gc, VisualLayer* layer)
xcb_screen_t* screen, xcb_window_t window, xcb_gcontext_t gc,
VisualLayer* layer)
{ {
if(layer->HasText()) if(layer->HasText())
{ {

View file

@ -8,16 +8,12 @@ class VisualLayer;
class XcbLayerInterface class XcbLayerInterface
{ {
public: public:
static uint32_t GetColor(const Color* color); static uint32_t GetColor(const Color* color);
static uint32_t GetColor(int r, int g, int b); static uint32_t GetColor(int r, int g, int b);
static void ModifyGcColor(xcb_connection_t* connection, xcb_gcontext_t gc, static void ModifyGcColor(xcb_connection_t* connection, xcb_gcontext_t gc, const Color* color);
const Color* color);
static void AddLayer(xcb_connection_t* connection, static void AddLayer(xcb_connection_t* connection, xcb_screen_t* screen, xcb_window_t window, xcb_gcontext_t gc, VisualLayer* layer);
xcb_screen_t* screen, xcb_window_t window, xcb_gcontext_t gc,
VisualLayer* layer);
}; };