Menus can now have a title and a Cancel button
This commit is contained in:
+21
-11
@@ -19,6 +19,7 @@
|
||||
|
||||
#include "menu.h"
|
||||
#include "font.h"
|
||||
#include "theme.h"
|
||||
|
||||
MenuWindow::MenuWindow(Menu * parent):
|
||||
Window(parent, {(LCD_W - MENUS_WIDTH) / 2, (LCD_H - MENUS_WIDTH) / 2, MENUS_WIDTH, 0}, OPAQUE),
|
||||
@@ -114,41 +115,50 @@ void MenuWindow::paint(BitmapBuffer * dc)
|
||||
}
|
||||
}
|
||||
|
||||
Menu::Menu():
|
||||
Window(&mainWindow, {0, 0, LCD_W, LCD_H}, TRANSPARENT),
|
||||
#if !defined(HARDWARE_TOUCH)
|
||||
previousFocus(focusWindow),
|
||||
#endif
|
||||
menuWindow(createMenuWindow(this))
|
||||
{
|
||||
}
|
||||
|
||||
void Menu::updatePosition()
|
||||
{
|
||||
if (!toolbar) {
|
||||
// there is no navigation bar at the left, we may center the window on screen
|
||||
auto headerHeight = menuWindow.title.empty() ? 0 : MENUS_HEADER_HEIGHT;
|
||||
auto bodyHeight = limit<coord_t>(MENUS_MIN_HEIGHT, menuWindow.body.lines.size() * MENUS_LINE_HEIGHT - 1, MENUS_MAX_HEIGHT);
|
||||
menuWindow.setTop((LCD_H - headerHeight - bodyHeight - MENUS_FOOTER_HEIGHT) / 2 + MENUS_OFFSET_TOP);
|
||||
menuWindow.setHeight(headerHeight + bodyHeight + MENUS_FOOTER_HEIGHT);
|
||||
menuWindow.body.setTop(headerHeight);
|
||||
menuWindow.body.setHeight(bodyHeight);
|
||||
auto headerHeight = menuWindow->title.empty() ? 0 : MENUS_HEADER_HEIGHT;
|
||||
auto bodyHeight = limit<coord_t>(MENUS_MIN_HEIGHT, menuWindow->body.lines.size() * MENUS_LINE_HEIGHT - 1, MENUS_MAX_HEIGHT);
|
||||
menuWindow->setTop((LCD_H - headerHeight - bodyHeight - MENUS_FOOTER_HEIGHT) / 2 + MENUS_OFFSET_TOP);
|
||||
menuWindow->setHeight(headerHeight + bodyHeight + MENUS_FOOTER_HEIGHT);
|
||||
menuWindow->body.setTop(headerHeight);
|
||||
menuWindow->body.setHeight(bodyHeight);
|
||||
}
|
||||
menuWindow.body.setInnerHeight(menuWindow.body.lines.size() * MENUS_LINE_HEIGHT - 1);
|
||||
menuWindow->body.setInnerHeight(menuWindow->body.lines.size() * MENUS_LINE_HEIGHT - 1);
|
||||
}
|
||||
|
||||
void Menu::setTitle(const std::string text)
|
||||
{
|
||||
menuWindow.setTitle(text);
|
||||
menuWindow->setTitle(text);
|
||||
updatePosition();
|
||||
}
|
||||
|
||||
void Menu::addLine(const std::string & text, std::function<void()> onPress)
|
||||
{
|
||||
menuWindow.body.addLine(text, std::move(onPress));
|
||||
menuWindow->body.addLine(text, std::move(onPress));
|
||||
updatePosition();
|
||||
}
|
||||
|
||||
void Menu::addCustomLine(std::function<void(BitmapBuffer * dc, coord_t x, coord_t y, LcdFlags flags)> drawLine, std::function<void()> onPress)
|
||||
{
|
||||
menuWindow.body.addCustomLine(drawLine, std::move(onPress));
|
||||
menuWindow->body.addCustomLine(drawLine, std::move(onPress));
|
||||
updatePosition();
|
||||
}
|
||||
|
||||
void Menu::removeLines()
|
||||
{
|
||||
menuWindow.body.removeLines();
|
||||
menuWindow->body.removeLines();
|
||||
updatePosition();
|
||||
}
|
||||
|
||||
|
||||
+9
-21
@@ -140,25 +140,13 @@ class MenuWindow: public Window {
|
||||
std::string title;
|
||||
};
|
||||
|
||||
class Menu : public Window {
|
||||
class Menu: public Window {
|
||||
public:
|
||||
Menu() :
|
||||
Window(&mainWindow, {0, 0, LCD_W, LCD_H}, TRANSPARENT),
|
||||
#if !defined(HARDWARE_TOUCH)
|
||||
previousFocus(focusWindow),
|
||||
#endif
|
||||
menuWindow(this)
|
||||
{
|
||||
}
|
||||
|
||||
~Menu() override
|
||||
{
|
||||
menuWindow.detach();
|
||||
}
|
||||
|
||||
Menu();
|
||||
|
||||
void setCancelHandler(std::function<void()> handler)
|
||||
{
|
||||
menuWindow.body.setCancelHandler(handler);
|
||||
menuWindow->body.setCancelHandler(handler);
|
||||
}
|
||||
|
||||
void deleteLater()
|
||||
@@ -181,9 +169,9 @@ class Menu : public Window {
|
||||
void setToolbar(Window * window)
|
||||
{
|
||||
toolbar = window;
|
||||
menuWindow.setLeft(toolbar->right());
|
||||
menuWindow.setTop(toolbar->top());
|
||||
menuWindow.setHeight(toolbar->height());
|
||||
menuWindow->setLeft(toolbar->right());
|
||||
menuWindow->setTop(toolbar->top());
|
||||
menuWindow->setHeight(toolbar->height());
|
||||
}
|
||||
|
||||
void setTitle(const std::string text);
|
||||
@@ -196,7 +184,7 @@ class Menu : public Window {
|
||||
|
||||
inline void select(int index)
|
||||
{
|
||||
menuWindow.body.select(index);
|
||||
menuWindow->body.select(index);
|
||||
}
|
||||
|
||||
#if defined(HARDWARE_KEYS)
|
||||
@@ -216,7 +204,7 @@ class Menu : public Window {
|
||||
|
||||
protected:
|
||||
Window * previousFocus = nullptr;
|
||||
MenuWindow menuWindow;
|
||||
MenuWindow * menuWindow;
|
||||
Window * toolbar = nullptr;
|
||||
void updatePosition();
|
||||
};
|
||||
|
||||
+4
-1
@@ -23,7 +23,8 @@
|
||||
#include "bitmapbuffer.h"
|
||||
#include "checkbox.h"
|
||||
|
||||
class PageHeader;
|
||||
class Menu;
|
||||
class MenuWindow;
|
||||
class ChoiceBase;
|
||||
|
||||
enum IconState {
|
||||
@@ -51,4 +52,6 @@ class Theme {
|
||||
|
||||
extern Theme * theme;
|
||||
|
||||
MenuWindow * createMenuWindow(Menu * menu);
|
||||
|
||||
#endif // _THEME_H_
|
||||
|
||||
Reference in New Issue
Block a user