stuff-from-scratch/apps/notes_tk/text_editor/TextEditorView.cpp

88 lines
2.4 KiB
C++
Raw Normal View History

2020-06-27 09:47:30 +00:00
#include "TextEditorView.h"
2022-11-17 17:33:48 +00:00
#include "HorizontalSpacer.h"
2020-06-27 09:47:30 +00:00
#include "VerticalSpacer.h"
2022-11-17 17:33:48 +00:00
#include "Theme.h"
2022-11-16 15:06:08 +00:00
#include "TextNode.h"
2020-06-27 09:47:30 +00:00
#include <iostream>
TextEditorView::TextEditorView()
: mTextBox(),
mController(TextEditorController::Create())
{
}
2022-11-18 15:11:54 +00:00
TextEditorController* TextEditorView::getController()
2020-06-27 09:47:30 +00:00
{
return mController.get();
}
2022-11-18 15:11:54 +00:00
void TextEditorView::initialize()
2020-06-27 09:47:30 +00:00
{
auto label = Label::Create();
2022-11-11 14:22:31 +00:00
label->setLabel("Text Editor");
label->setMargin(1);
2020-06-27 09:47:30 +00:00
auto textBox = TextBox::Create();
textBox->setName("Text Box");
2020-06-27 09:47:30 +00:00
mTextBox = textBox.get();
auto saveButton = Button::Create();
2022-11-11 14:22:31 +00:00
saveButton->setLabel("Save");
2023-01-20 08:07:09 +00:00
saveButton->setBackground(Theme::Sys::Color::Primary);
2022-11-11 14:22:31 +00:00
saveButton->setMargin(2);
2020-06-27 09:47:30 +00:00
auto onSave = [this](Widget* self){
if(this && mController && mTextBox)
{
2022-11-13 17:02:09 +00:00
mController->SetContent(mTextBox->getContent());
2020-06-27 09:47:30 +00:00
mController->OnSave();
};
};
2022-11-11 14:22:31 +00:00
saveButton->setOnClickFunction(onSave);
2020-06-27 09:47:30 +00:00
auto clearButton = Button::Create();
2022-11-11 14:22:31 +00:00
clearButton->setLabel("Clear");
clearButton->setMargin(2);
2020-06-27 09:47:30 +00:00
auto onClear = [this](Widget* self){
if(this && mController && mTextBox)
{
mController->OnClear();
2022-11-13 17:02:09 +00:00
mTextBox->setContent("");
2020-06-27 09:47:30 +00:00
};
};
2022-11-11 14:22:31 +00:00
clearButton->setOnClickFunction(onClear);
2020-06-27 09:47:30 +00:00
auto loadButton = Button::Create();
2022-11-11 14:22:31 +00:00
loadButton->setLabel("Load");
loadButton->setMargin(2);
2020-06-27 09:47:30 +00:00
auto onLoad = [this](Widget* self){
if(this && mController && mTextBox)
{
mController->OnLoad();
2022-11-13 17:02:09 +00:00
mTextBox->setContent(mController->GetContent());
2020-06-27 09:47:30 +00:00
};
};
2022-11-11 14:22:31 +00:00
loadButton->setOnClickFunction(onLoad);
2020-06-27 09:47:30 +00:00
auto buttonSpacer = VerticalSpacer::Create();
2022-12-05 10:59:45 +00:00
buttonSpacer->addWidgetWithScale(std::move(saveButton), 1);
buttonSpacer->addWidgetWithScale(std::move(clearButton), 1);
buttonSpacer->addWidgetWithScale(std::move(loadButton), 1);
2020-06-27 09:47:30 +00:00
auto hSpacer = HorizontalSpacer::Create();
2022-11-11 14:22:31 +00:00
hSpacer->addWidgetWithScale(std::move(label), 1);
hSpacer->addWidgetWithScale(std::move(textBox), 14);
hSpacer->addWidgetWithScale(std::move(buttonSpacer), 1);
2020-06-27 09:47:30 +00:00
2022-11-11 14:22:31 +00:00
addWidget(std::move(hSpacer));
2020-06-27 09:47:30 +00:00
}
std::unique_ptr<TextEditorView> TextEditorView::Create()
{
return std::make_unique<TextEditorView>();
}