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