screen setup: flex layout

This commit is contained in:
Raphael Coeffic
2022-06-11 15:38:08 +02:00
committed by Raphael Coeffic
parent 7c63b86cfe
commit 80a5c9976f
4 changed files with 217 additions and 176 deletions
-3
View File
@@ -25,12 +25,9 @@
#include "tabsgroup.h" #include "tabsgroup.h"
class ScreenMenu: public TabsGroup { class ScreenMenu: public TabsGroup {
friend class ScreenSetupPage;
public: public:
ScreenMenu(); ScreenMenu();
protected:
void updateTabs(); void updateTabs();
}; };
+194 -157
View File
@@ -32,81 +32,92 @@
#define BUTTON_HEIGHT 30 #define BUTTON_HEIGHT 30
#define BUTTON_WIDTH 75 #define BUTTON_WIDTH 75
class LayoutChoice: public FormField class LayoutChoice : public Button
{ {
public: public:
typedef std::function<const LayoutFactory*()> LayoutFactoryGetter;
typedef std::function<void(const LayoutFactory*)> LayoutFactorySetter;
typedef std::function<const LayoutFactory *()> LayoutFactoryGetter; LayoutChoice(Window* parent, LayoutFactoryGetter getValue, LayoutFactorySetter setValue) :
typedef std::function<void(const LayoutFactory *)> LayoutFactorySetter; Button(parent, rect_t{}, nullptr, 0, 0, lv_btn_create),
LayoutChoice(Window * parent, const rect_t & rect, LayoutFactoryGetter getValue, LayoutFactorySetter setValue):
FormField(parent, {rect.x, rect.y, 59, 33}),
getValue(std::move(getValue)), getValue(std::move(getValue)),
setValue(std::move(setValue)) setValue(std::move(setValue))
{ {
} canvas = lv_canvas_create(lvobj);
lv_obj_center(canvas);
void paint(BitmapBuffer * dc) override update();
{ }
FormField::paint(dc);
auto layout = getValue();
if (layout) {
layout->drawThumb(dc, 4, 4, editMode ? COLOR_THEME_PRIMARY2 : COLOR_THEME_PRIMARY3);
}
}
#if defined(HARDWARE_KEYS) #if defined(HARDWARE_KEYS)
void onEvent(event_t event) override void onEvent(event_t event) override
{ {
TRACE_WINDOWS("%s received event 0x%X", getWindowDebugString().c_str(), event); TRACE_WINDOWS("%s received event 0x%X", getWindowDebugString().c_str(),
event);
if (event == EVT_KEY_BREAK(KEY_ENTER)) { if (event == EVT_KEY_BREAK(KEY_ENTER)) {
editMode = true; editMode = true;
invalidate(); invalidate();
openMenu(); openMenu();
} } else {
else { FormField::onEvent(event);
FormField::onEvent(event);
}
} }
}
#endif #endif
#if defined(HARDWARE_TOUCH) #if defined(HARDWARE_TOUCH)
bool onTouchEnd(coord_t, coord_t) override bool onTouchEnd(coord_t, coord_t) override
{ {
if (enabled) { if (enabled) {
if (!hasFocus()) { if (!hasFocus()) {
setFocus(SET_FOCUS_DEFAULT); setFocus(SET_FOCUS_DEFAULT);
}
onKeyPress();
openMenu();
} }
return true; onKeyPress();
openMenu();
} }
return true;
}
#endif #endif
void openMenu() void openMenu()
{ {
auto menu = new Menu(parent); auto menu = new Menu(parent);
for (auto layout : getRegisteredLayouts()) { for (auto layout : getRegisteredLayouts()) {
menu->addLine(layout->getBitmap(), layout->getName(), menu->addLine(layout->getBitmap(), layout->getName(),
[=]() { setValue(layout); }); [=]() { setValue(layout); });
}
auto it = std::find(getRegisteredLayouts().begin(),
getRegisteredLayouts().end(), getValue());
menu->select(std::distance(getRegisteredLayouts().begin(), it));
menu->setCloseHandler([=]() {
editMode = false;
setFocus(SET_FOCUS_DEFAULT);
});
} }
protected: auto it = std::find(getRegisteredLayouts().begin(),
std::function<const LayoutFactory *()> getValue; getRegisteredLayouts().end(), getValue());
std::function<void(const LayoutFactory *)> setValue; menu->select(std::distance(getRegisteredLayouts().begin(), it));
// menu->setCloseHandler([=]() {
// editMode = false;
// update();
// // setFocus();
// });
}
protected:
lv_obj_t* canvas = nullptr;
std::function<const LayoutFactory*()> getValue;
std::function<void(const LayoutFactory*)> setValue;
void update()
{
if (!getValue || !canvas) return;
auto layout = getValue();
if (!layout) return;
const uint8_t* bitmap = layout->getBitmap();
if (!bitmap) return;
lv_coord_t w = *((uint16_t*)bitmap);
lv_coord_t h = *(((uint16_t*)bitmap) + 1);
void* buf = (void*)(bitmap + 4);
lv_canvas_set_buffer(canvas, buf, w, h, LV_IMG_CF_ALPHA_8BIT);
}
}; };
ScreenAddPage::ScreenAddPage(ScreenMenu * menu, uint8_t pageIndex): ScreenAddPage::ScreenAddPage(ScreenMenu * menu, uint8_t pageIndex):
@@ -175,143 +186,169 @@ void ScreenAddPage::build(FormWindow * window)
}); });
} }
ScreenSetupPage::ScreenSetupPage(ScreenMenu * menu, unsigned pageIndex, unsigned customScreenIndex): static const lv_coord_t line_col_dsc[] = {LV_GRID_FR(1), LV_GRID_FR(1),
PageTab(), LV_GRID_TEMPLATE_LAST};
menu(menu), static const lv_coord_t line_row_dsc[] = {LV_GRID_CONTENT,
pageIndex(pageIndex), LV_GRID_TEMPLATE_LAST};
customScreenIndex(customScreenIndex)
static std::function<uint8_t()> startWidgetsSetup(ScreenMenu* menu,
uint8_t screen_idx)
{ {
return [=]() -> uint8_t {
new SetupWidgetsPage(menu, screen_idx);
return 0;
};
} }
static void updateLayoutOptions(FormGroup* optionsWindow, unsigned customScreenIndex) static std::function<uint8_t()> removeScreen(ScreenMenu* menu,
uint8_t screen_idx)
{ {
FormGridLayout grid; return [=]() -> uint8_t {
optionsWindow->clear(); // Set the current tab to "User interface" to trigger body clearing
menu->setCurrentTab(0);
// Layout options... // Remove this screen from the model
int index = 0; disposeCustomScreen(screen_idx);
auto layout = dynamic_cast<Layout*>(customScreens[customScreenIndex]);
if (!layout) {
return;
}
auto factory = layout->getFactory();
if (!factory) {
return;
}
for (auto * option = factory->getOptions(); option->name; option++, index++) { // Delete all custom screens
deleteCustomScreens();
auto layoutData = &g_model.screenData[customScreenIndex].layoutData; // ... and reload
ZoneOptionValue * value = &layoutData->options[index].value; loadCustomScreens();
menu->updateTabs();
// Option label // Let's try to stay on the same page
new StaticText(optionsWindow, grid.getLabelSlot(false), option->name, 0, COLOR_THEME_PRIMARY1); // (first tab is "User interface")
auto pageIdx = screen_idx + 1;
// Option value // Subtract one more as the last one is "New main screen"
switch (option->type) { if (pageIdx > menu->getTabs() - 2) {
case ZoneOption::Bool: pageIdx = menu->getTabs() - 2;
new CheckBox(optionsWindow, grid.getFieldSlot(), GET_SET_DEFAULT(value->boolValue));
break;
case ZoneOption::Color:
new ColorEdit(optionsWindow, grid.getFieldSlot(), GET_SET_DEFAULT(value->unsignedValue));
break;
default:
break;
} }
grid.nextLine(); menu->setCurrentTab(pageIdx);
} return 0;
};
optionsWindow->adjustHeight();
} }
void ScreenSetupPage::build(FormWindow * window) ScreenSetupPage::ScreenSetupPage(ScreenMenu* menu, unsigned pageIndex,
unsigned customScreenIndex) :
PageTab(),
menu(menu),
pageIndex(pageIndex),
customScreenIndex(customScreenIndex)
{ {
FormGridLayout grid; }
grid.spacer(PAGE_PADDING);
void ScreenSetupPage::build(FormWindow * form)
{
FlexGridLayout grid(line_col_dsc, line_row_dsc);
form->setFlexLayout();
// Layout choice... // Layout choice...
new StaticText(window, grid.getLabelSlot(false), STR_LAYOUT, 0, COLOR_THEME_PRIMARY1); auto line = form->newLine(&grid);
auto label = new StaticText(line, rect_t{}, STR_LAYOUT, 0, COLOR_THEME_PRIMARY1);
auto layoutSlot = grid.getFieldSlot(); auto obj = label->getLvObj();
layoutSlot.h = 2 * PAGE_LINE_HEIGHT - 1; lv_obj_set_style_grid_cell_y_align(obj, LV_GRID_ALIGN_START, 0);
// Dynamic options window... // Dynamic options window...
auto idx = customScreenIndex; auto idx = customScreenIndex;
rect_t optRect = {0, grid.getWindowHeight(), window->width(), 0};
auto optionsWindow = new FormGroup(window, optRect, FORWARD_SCROLL | FORM_FORWARD_FOCUS);
LayoutChoice::LayoutFactoryGetter getFactory = [idx] () -> const LayoutFactory * { LayoutChoice::LayoutFactoryGetter getFactory = [idx] () -> const LayoutFactory * {
auto layout = dynamic_cast<Layout*>(customScreens[idx]); auto layout = dynamic_cast<Layout*>(customScreens[idx]);
if (!layout) return nullptr; if (!layout) return nullptr;
return layout->getFactory(); return layout->getFactory();
}; };
LayoutChoice::LayoutFactorySetter setLayout = LayoutChoice::LayoutFactorySetter setLayout =
[=](const LayoutFactory* factory) { [=](const LayoutFactory* factory) {
// delete any options potentially accessing // delete any options potentially accessing
// the old custom screen // the old custom screen before re-creating it
window->clear(); clearLayoutOptions();
createCustomScreen(factory, idx); createCustomScreen(factory, idx);
build(window); buildLayoutOptions();
}; };
auto layoutChoice = new LayoutChoice(window, layoutSlot, getFactory, setLayout); auto layoutForm = new FormGroup(line, rect_t{}, FORWARD_SCROLL | FORM_FORWARD_FOCUS);
grid.nextLine(layoutChoice->height()); layoutForm->setFlexLayout(LV_FLEX_FLOW_ROW_WRAP, 8);
// Setup widgets button... obj = layoutForm->getLvObj();
auto setupWidgetsButton = new TextButton(window, grid.getFieldSlot(), STR_SETUP_WIDGETS); lv_obj_set_style_grid_cell_x_align(obj, LV_GRID_ALIGN_STRETCH, 0);
auto menu = this->menu; Window* btn = new LayoutChoice(layoutForm, getFactory, setLayout);
setupWidgetsButton->setPressHandler([idx, menu]() -> uint8_t { obj = btn->getLvObj();
new SetupWidgetsPage(menu, idx); lv_obj_set_style_min_width(obj, LV_DPI_DEF / 2, LV_PART_MAIN);
return 0; lv_obj_set_style_min_height(obj, LV_DPI_DEF / 3, LV_PART_MAIN);
}); lv_obj_set_style_pad_all(obj, 8, LV_PART_MAIN);
grid.nextLine(); lv_obj_set_style_radius(obj, 8, LV_PART_MAIN);
// place the options window at the right spot btn = new TextButton(layoutForm, rect_t{}, STR_SETUP_WIDGETS,
optionsWindow->setTop(grid.getWindowHeight()); startWidgetsSetup(menu, idx));
updateLayoutOptions(optionsWindow, idx); obj = btn->getLvObj();
grid.addWindow(optionsWindow); lv_obj_set_style_min_width(obj, LV_DPI_DEF / 2, LV_PART_MAIN);
lv_obj_set_style_min_height(obj, LV_DPI_DEF / 3, LV_PART_MAIN);
lv_obj_set_style_pad_all(obj, 8, LV_PART_MAIN);
lv_obj_set_style_radius(obj, 8, LV_PART_MAIN);
line = form->newLine();
layoutOptions = new FormGroup(line, rect_t{}, FORWARD_SCROLL | FORM_FORWARD_FOCUS);
buildLayoutOptions();
// fix focus order due to early insertion
FormField::link(setupWidgetsButton, optionsWindow);
window->setLastField(optionsWindow);
// Prevent removing the last page // Prevent removing the last page
if (customScreens[1] != nullptr) { if (customScreens[1] != nullptr) {
auto button = new TextButton(window, grid.getFieldSlot(), STR_REMOVE_SCREEN); new TextButton(form, rect_t{}, STR_REMOVE_SCREEN, removeScreen(menu, idx));
auto menu = this->menu;
button->setPressHandler([menu, idx]() -> uint8_t {
// Set the current tab to "User interface" to trigger body clearing
menu->setCurrentTab(0);
// Remove this screen from the model
disposeCustomScreen(idx);
// Delete all custom screens
deleteCustomScreens();
// ... and reload
loadCustomScreens();
menu->updateTabs();
// Let's try to stay on the same page
// (first tab is "User interface")
auto pageIdx = idx + 1;
// Subtract one more as the last one is "New main screen"
if (pageIdx > menu->getTabs() - 2) {
pageIdx = menu->getTabs() - 2;
}
menu->setCurrentTab(pageIdx);
return 0;
});
// fix focus order due to early insertion
FormField::link(button, layoutChoice);
} }
window->adjustInnerHeight(); form->updateSize();
}
void ScreenSetupPage::clearLayoutOptions()
{
if (!layoutOptions) return;
}
void ScreenSetupPage::buildLayoutOptions()
{
if (!layoutOptions) return;
layoutOptions->clear();
FlexGridLayout grid(line_col_dsc, line_row_dsc);
layoutOptions->setFlexLayout();
// Layout options...
auto layout = dynamic_cast<Layout*>(customScreens[customScreenIndex]);
if (!layout) return;
auto factory = layout->getFactory();
if (!factory) return;
int index = 0;
for (auto* option = factory->getOptions(); option->name; option++, index++) {
auto layoutData = &g_model.screenData[customScreenIndex].layoutData;
ZoneOptionValue* value = &layoutData->options[index].value;
// Option label
auto line = layoutOptions->newLine(&grid);
new StaticText(line, rect_t{}, option->name, 0, COLOR_THEME_PRIMARY1);
// Option value
switch (option->type) {
case ZoneOption::Bool:
new CheckBox(line, rect_t{}, GET_SET_DEFAULT(value->boolValue));
break;
case ZoneOption::Color:
new ColorEdit(line, rect_t{}, GET_SET_DEFAULT(value->unsignedValue));
break;
default:
break;
}
}
layoutOptions->updateSize();
auto parent = layoutOptions->getParent();
if (parent) parent->updateSize();
} }
+22 -15
View File
@@ -25,25 +25,32 @@
#include "layout.h" #include "layout.h"
#include "datastructs.h" #include "datastructs.h"
class ScreenAddPage: public PageTab { class ScreenAddPage : public PageTab
public: {
ScreenAddPage(ScreenMenu * menu, uint8_t pageIndex); public:
ScreenAddPage(ScreenMenu* menu, uint8_t pageIndex);
void build(FormWindow * window) override; void build(FormWindow* window) override;
protected: protected:
ScreenMenu * menu; ScreenMenu* menu;
uint8_t pageIndex; uint8_t pageIndex;
}; };
class ScreenSetupPage: public PageTab { class ScreenSetupPage : public PageTab
public: {
ScreenSetupPage(ScreenMenu * menu, unsigned pageIndex, unsigned customScreenIndex); public:
ScreenSetupPage(ScreenMenu* menu, unsigned pageIndex,
unsigned customScreenIndex);
void build(FormWindow * window) override; void build(FormWindow* form) override;
protected: protected:
ScreenMenu * menu; ScreenMenu* menu;
unsigned pageIndex; unsigned pageIndex;
unsigned customScreenIndex; unsigned customScreenIndex;
FormGroup* layoutOptions = nullptr;
void clearLayoutOptions();
void buildLayoutOptions();
}; };