fix(color): long press key handling, curve preview not updating (#5107)

This commit is contained in:
philmoz
2024-06-07 15:43:58 +10:00
committed by GitHub
parent f8d542c5e2
commit 76dde34f19
33 changed files with 201 additions and 244 deletions
+9 -3
View File
@@ -66,11 +66,10 @@ static void copy_kb_data_backup(lv_indev_data_t* data)
memcpy(data, &kb_data_backup, sizeof(lv_indev_data_t)); memcpy(data, &kb_data_backup, sizeof(lv_indev_data_t));
} }
constexpr event_t _KEY_PRESSED = _MSK_KEY_FLAGS & ~_MSK_KEY_BREAK;
static bool evt_to_indev_data(event_t evt, lv_indev_data_t *data) static bool evt_to_indev_data(event_t evt, lv_indev_data_t *data)
{ {
event_t key = EVT_KEY_MASK(evt); event_t key = EVT_KEY_MASK(evt);
switch(key) { switch(key) {
case KEY_ENTER: case KEY_ENTER:
@@ -90,7 +89,8 @@ static bool evt_to_indev_data(event_t evt, lv_indev_data_t *data)
return false; return false;
} }
if (evt & _KEY_PRESSED) { event_t flgs = evt & _MSK_KEY_FLAGS;
if (flgs != _MSK_KEY_BREAK && flgs != _MSK_KEY_LONG_BRK) {
data->state = LV_INDEV_STATE_PRESSED; data->state = LV_INDEV_STATE_PRESSED;
} else { } else {
data->state = LV_INDEV_STATE_RELEASED; data->state = LV_INDEV_STATE_RELEASED;
@@ -122,6 +122,12 @@ static void keyboardDriverRead(lv_indev_drv_t *drv, lv_indev_data_t *data)
if (isEvent()) { // event waiting if (isEvent()) { // event waiting
event_t evt = getEvent(); event_t evt = getEvent();
if ((evt & _MSK_KEY_FLAGS) == _MSK_KEY_LONG_BRK) {
data->state = LV_INDEV_STATE_RELEASED;
backup_kb_data(data);
return;
}
if(evt == EVT_KEY_FIRST(KEY_PAGEUP) || if(evt == EVT_KEY_FIRST(KEY_PAGEUP) ||
evt == EVT_KEY_FIRST(KEY_PAGEDN) || evt == EVT_KEY_FIRST(KEY_PAGEDN) ||
evt == EVT_KEY_FIRST(KEY_ENTER) || evt == EVT_KEY_FIRST(KEY_ENTER) ||
+31 -8
View File
@@ -46,9 +46,34 @@ void CurveParam::value_changed(lv_event_t* e)
param->update(); param->update();
} }
class CurveChoice : public Choice
{
public:
CurveChoice(Window* parent, CurveRef* ref, std::function<void(int32_t)> setRefValue, std::function<void(void)> refreshView) :
Choice(parent, rect_t{}, -MAX_CURVES, MAX_CURVES, GET_DEFAULT(ref->value), setRefValue),
ref(ref), refreshView(refreshView)
{
setTextHandler([](int value) { return getCurveString(value); });
}
bool onLongPress() override
{
if (modelCurvesEnabled()) {
if (ref->value) {
ModelCurvesPage::pushEditCurve(abs(ref->value) - 1, refreshView);
}
}
return true;
}
protected:
CurveRef* ref;
std::function<void(void)> refreshView;
};
CurveParam::CurveParam(Window* parent, const rect_t& rect, CurveRef* ref, CurveParam::CurveParam(Window* parent, const rect_t& rect, CurveRef* ref,
std::function<void(int32_t)> setRefValue) : std::function<void(int32_t)> setRefValue, std::function<void(void)> refreshView) :
Window(parent, rect), ref(ref), setRefValue(setRefValue) Window(parent, rect), ref(ref)
{ {
padAll(PAD_TINY); padAll(PAD_TINY);
lv_obj_set_flex_flow(lvobj, LV_FLEX_FLOW_ROW_WRAP); lv_obj_set_flex_flow(lvobj, LV_FLEX_FLOW_ROW_WRAP);
@@ -67,18 +92,16 @@ CurveParam::CurveParam(Window* parent, const rect_t& rect, CurveRef* ref,
// CURVE_REF_DIFF // CURVE_REF_DIFF
// CURVE_REF_EXPO // CURVE_REF_EXPO
value_edit = new GVarNumberEdit(this, -100, 100, GET_DEFAULT(ref->value), setRefValue); auto gv = new GVarNumberEdit(this, -100, 100, GET_DEFAULT(ref->value), setRefValue);
value_edit->setSuffix("%"); gv->setSuffix("%");
value_edit = gv;
// CURVE_REF_FUNC // CURVE_REF_FUNC
func_choice = new Choice(this, rect_t{}, STR_VCURVEFUNC, 0, CURVE_BASE - 1, func_choice = new Choice(this, rect_t{}, STR_VCURVEFUNC, 0, CURVE_BASE - 1,
GET_DEFAULT(ref->value), setRefValue); GET_DEFAULT(ref->value), setRefValue);
// CURVE_REF_CUSTOM // CURVE_REF_CUSTOM
cust_choice = new Choice(this, rect_t{}, -MAX_CURVES, MAX_CURVES, cust_choice = new CurveChoice(this, ref, setRefValue, refreshView);
GET_DEFAULT(ref->value), setRefValue);
cust_choice->setTextHandler([](int value) { return getCurveString(value); });
cust_choice->set_lv_LongPressHandler(LongPressHandler, &(ref->value));
update(); update();
} }
+5 -9
View File
@@ -25,20 +25,15 @@
struct CurveRef; struct CurveRef;
class GVarNumberEdit;
class Choice;
class CurveParam : public Window class CurveParam : public Window
{ {
// Curve // Curve
CurveRef* ref; CurveRef* ref;
std::function<void(int32_t)> setRefValue;
// Controls // Controls
GVarNumberEdit* value_edit; Window* value_edit;
Choice* func_choice; Window* func_choice;
Choice* cust_choice; Window* cust_choice;
Window* act_field = nullptr; Window* act_field = nullptr;
void update(); void update();
@@ -47,5 +42,6 @@ class CurveParam : public Window
public: public:
CurveParam(Window* parent, const rect_t& rect, CurveRef* ref, CurveParam(Window* parent, const rect_t& rect, CurveRef* ref,
std::function<void(int32_t)> setRefValue); std::function<void(int32_t)> setRefValue,
std::function<void(void)> refreshView = nullptr);
}; };
+8 -2
View File
@@ -240,8 +240,8 @@ void CurveEdit::checkEvents()
Window::checkEvents(); Window::checkEvents();
} }
CurveEditWindow::CurveEditWindow(uint8_t index) : CurveEditWindow::CurveEditWindow(uint8_t index, std::function<void(void)> refreshView) :
Page(ICON_MODEL_CURVES, PAD_ZERO), index(index) Page(ICON_MODEL_CURVES, PAD_ZERO), index(index), refreshView(refreshView)
{ {
buildBody(body); buildBody(body);
buildHeader(header); buildHeader(header);
@@ -392,3 +392,9 @@ void CurveEditWindow::buildBody(Window* window)
curveDataEdit->setCurveEdit(curveEdit); curveDataEdit->setCurveEdit(curveEdit);
} }
void CurveEditWindow::onCancel()
{
if (refreshView) refreshView();
Page::onCancel();
}
+4 -1
View File
@@ -82,13 +82,16 @@ class CurveDataEdit : public Window
class CurveEditWindow : public Page class CurveEditWindow : public Page
{ {
public: public:
CurveEditWindow(uint8_t index); CurveEditWindow(uint8_t index, std::function<void(void)> refreshView = nullptr);
protected: protected:
uint8_t index; uint8_t index;
CurveEdit * curveEdit = nullptr; CurveEdit * curveEdit = nullptr;
CurveDataEdit * curveDataEdit = nullptr; CurveDataEdit * curveDataEdit = nullptr;
std::function<void(void)> refreshView = nullptr;
void buildHeader(Window * window); void buildHeader(Window * window);
void buildBody(Window * window); void buildBody(Window * window);
void onCancel() override;
}; };
+4 -10
View File
@@ -74,8 +74,6 @@ FullScreenDialog::FullScreenDialog(
build(); build();
lv_obj_add_event_cb(lvobj, FullScreenDialog::long_pressed,
LV_EVENT_LONG_PRESSED, nullptr);
lv_obj_add_event_cb(lvobj, FullScreenDialog::on_draw, lv_obj_add_event_cb(lvobj, FullScreenDialog::on_draw,
LV_EVENT_DRAW_MAIN_BEGIN, nullptr); LV_EVENT_DRAW_MAIN_BEGIN, nullptr);
} }
@@ -164,15 +162,11 @@ void FullScreenDialog::closeDialog()
deleteLater(); deleteLater();
} }
void FullScreenDialog::long_pressed(lv_event_t* e) bool FullScreenDialog::onLongPress()
{ {
auto obj = lv_event_get_target(e); closeDialog();
auto fs = (FullScreenDialog*)lv_obj_get_user_data(obj); lv_indev_wait_release(lv_indev_get_act());
return false;
if (fs) {
fs->closeDialog();
lv_indev_wait_release(lv_indev_get_act());
}
} }
void FullScreenDialog::onEvent(event_t event) void FullScreenDialog::onEvent(event_t event)
+1 -1
View File
@@ -43,6 +43,7 @@ class FullScreenDialog : public Window
void onEvent(event_t event) override; void onEvent(event_t event) override;
void onCancel() override; void onCancel() override;
bool onLongPress() override;
void deleteLater(bool detach = true, bool trash = true) override; void deleteLater(bool detach = true, bool trash = true) override;
@@ -71,6 +72,5 @@ class FullScreenDialog : public Window
virtual void delayedInit() {} virtual void delayedInit() {}
void build(); void build();
static void long_pressed(lv_event_t* e);
static void on_draw(lv_event_t* e); static void on_draw(lv_event_t* e);
}; };
+9 -5
View File
@@ -135,11 +135,15 @@ void InputEditWindow::buildBody(Window* form)
line = form->newLine(grid); line = form->newLine(grid);
new StaticText(line, rect_t{}, STR_CURVE); new StaticText(line, rect_t{}, STR_CURVE);
auto param = auto param =
new CurveParam(line, rect_t{}, &input->curve, [=](int32_t newValue) { new CurveParam(line, rect_t{}, &input->curve,
input->curve.value = newValue; [=](int32_t newValue) {
preview->update(); input->curve.value = newValue;
SET_DIRTY(); preview->update();
}); SET_DIRTY();
},
[=]() {
preview->update();
});
lv_obj_set_style_grid_cell_x_align(param->getLvObj(), LV_GRID_ALIGN_STRETCH, lv_obj_set_style_grid_cell_x_align(param->getLvObj(), LV_GRID_ALIGN_STRETCH,
0); 0);
+4 -13
View File
@@ -23,14 +23,6 @@
#include "libopenui.h" #include "libopenui.h"
// TODO:
// - split this into 2 handlers:
// - key_cb: PRESSED / LONG_PRESSED (always on)
// - focus_cb: for FOCUSED / DEFOCUSED (only w/ 'auto-edit' mode)
// - add 'auto-edit' mode (add / remove 'focus_cb')
// - when turned on, ESC gets out of the edit-mode
// and ENTER gets into edit-mode
//
void ListBox::event_cb(lv_event_t* e) void ListBox::event_cb(lv_event_t* e)
{ {
static bool _nested = false; static bool _nested = false;
@@ -51,10 +43,6 @@ void ListBox::event_cb(lv_event_t* e)
_nested = true; _nested = true;
lv_group_set_editing((lv_group_t*)lv_obj_get_group(obj), false); lv_group_set_editing((lv_group_t*)lv_obj_get_group(obj), false);
_nested = false; _nested = false;
} else if (code == LV_EVENT_LONG_PRESSED) {
lb->onLongPressed();
lv_obj_clear_state(obj, LV_STATE_PRESSED);
lv_indev_wait_release(lv_indev_get_act());
} }
} }
@@ -180,12 +168,15 @@ void ListBox::onPress(uint16_t row, uint16_t col)
} }
} }
void ListBox::onLongPressed() bool ListBox::onLongPress()
{ {
TRACE("LONG_PRESS"); TRACE("LONG_PRESS");
if (longPressHandler) { if (longPressHandler) {
longPressHandler(); longPressHandler();
lv_indev_wait_release(lv_indev_get_act());
return false;
} }
return true;
} }
// TODO: !auto-edit // TODO: !auto-edit
+1 -1
View File
@@ -95,7 +95,7 @@ class ListBox : public TableField
bool smallSelectMarker = false; bool smallSelectMarker = false;
void onPress(uint16_t row, uint16_t col) override; void onPress(uint16_t row, uint16_t col) override;
void onLongPressed(); bool onLongPress() override;
void onClicked() override; void onClicked() override;
void onCancel() override; void onCancel() override;
+2 -2
View File
@@ -122,7 +122,7 @@ ModelCurvesPage::ModelCurvesPage() : PageTab(STR_MENUCURVES, ICON_MODEL_CURVES)
// can be called from any other screen to edit a curve. // can be called from any other screen to edit a curve.
// currently called from model_mixes.cpp on longpress. // currently called from model_mixes.cpp on longpress.
void ModelCurvesPage::pushEditCurve(int index) void ModelCurvesPage::pushEditCurve(int index, std::function<void(void)> refreshView)
{ {
if (!isCurveUsed(index)) { if (!isCurveUsed(index)) {
CurveHeader &curve = g_model.curves[index]; CurveHeader &curve = g_model.curves[index];
@@ -130,7 +130,7 @@ void ModelCurvesPage::pushEditCurve(int index)
initPoints(curve, points); initPoints(curve, points);
} }
new CurveEditWindow(index); new CurveEditWindow(index, refreshView);
} }
void ModelCurvesPage::rebuild(Window *window) void ModelCurvesPage::rebuild(Window *window)
+1 -1
View File
@@ -28,7 +28,7 @@ class ModelCurvesPage : public PageTab
{ {
public: public:
ModelCurvesPage(); ModelCurvesPage();
static void pushEditCurve(int index); static void pushEditCurve(int index, std::function<void(void)> refreshView = nullptr);
bool isVisible() const override { return modelCurvesEnabled(); } bool isVisible() const override { return modelCurvesEnabled(); }
+6 -10
View File
@@ -149,16 +149,14 @@ class SourceChoiceMenuToolbar : public MenuToolbar
#endif #endif
}; };
void SourceChoice::LongPressHandler(void* data) bool SourceChoice::onLongPress()
{ {
SourceChoice* src = (SourceChoice*)data; if (canInvert) {
if (src && src->canInvert) { int16_t val = _getValue();
int16_t val = src->_getValue(); if (isValueAvailable && isValueAvailable(-val))
if (src->isValueAvailable && src->isValueAvailable(-val)) { setValue(-val);
src->setValue(-val);
src->invalidate();
}
} }
return true;
} }
void SourceChoice::setValue(int value) void SourceChoice::setValue(int value)
@@ -234,7 +232,5 @@ SourceChoice::SourceChoice(Window *parent, const rect_t &rect, int16_t vmin,
return std::string(getSourceString(value)); return std::string(getSourceString(value));
}); });
set_lv_LongPressHandler(LongPressHandler, this);
setAvailableHandler([](int v) { return isSourceAvailable(v); }); setAvailableHandler([](int v) { return isSourceAvailable(v); });
} }
+1 -2
View File
@@ -35,8 +35,6 @@ class SourceChoice : public Choice
std::function<int16_t()> getValue, std::function<int16_t()> getValue,
std::function<void(int16_t)> setValue, bool allowInvert = false); std::function<void(int16_t)> setValue, bool allowInvert = false);
static void LongPressHandler(void* data);
protected: protected:
friend SourceChoiceMenuToolbar; friend SourceChoiceMenuToolbar;
@@ -45,6 +43,7 @@ class SourceChoice : public Choice
void setValue(int value) override; void setValue(int value) override;
int getIntValue() const override; int getIntValue() const override;
bool onLongPress() override;
void invertChoice(); void invertChoice();
+5 -9
View File
@@ -103,14 +103,12 @@ class SwitchChoiceMenuToolbar : public MenuToolbar
#endif #endif
}; };
void SwitchChoice::LongPressHandler(void* data) bool SwitchChoice::onLongPress()
{ {
SwitchChoice* swch = (SwitchChoice*)data; int16_t val = _getValue();
if (!swch) return; if (isValueAvailable && isValueAvailable(-val))
int16_t val = swch->_getValue(); setValue(-val);
if (swch->isValueAvailable && swch->isValueAvailable(-val)) { return true;
swch->setValue(-val);
}
} }
void SwitchChoice::setValue(int value) void SwitchChoice::setValue(int value)
@@ -182,7 +180,5 @@ SwitchChoice::SwitchChoice(Window* parent, const rect_t& rect, int vmin,
return std::string(getSwitchPositionName(value)); return std::string(getSwitchPositionName(value));
}); });
set_lv_LongPressHandler(LongPressHandler, this);
setAvailableHandler(isSwitchAvailableInMixes); setAvailableHandler(isSwitchAvailableInMixes);
} }
+1 -2
View File
@@ -33,8 +33,6 @@ class SwitchChoice : public Choice
std::function<int16_t()> getValue, std::function<int16_t()> getValue,
std::function<void(int16_t)> setValue); std::function<void(int16_t)> setValue);
static void LongPressHandler(void* data);
protected: protected:
friend SwitchChoiceMenuToolbar; friend SwitchChoiceMenuToolbar;
@@ -42,6 +40,7 @@ class SwitchChoice : public Choice
void setValue(int value) override; void setValue(int value) override;
int getIntValue() const override; int getIntValue() const override;
bool onLongPress() override;
void invertChoice(); void invertChoice();
+2 -13
View File
@@ -85,8 +85,6 @@ ViewMain::ViewMain() :
lv_obj_add_event_cb(tile_view, tile_view_scroll, LV_EVENT_SCROLL, nullptr); lv_obj_add_event_cb(tile_view, tile_view_scroll, LV_EVENT_SCROLL, nullptr);
lv_obj_add_event_cb(tile_view, tile_view_scroll, LV_EVENT_SCROLL_END, lv_obj_add_event_cb(tile_view, tile_view_scroll, LV_EVENT_SCROLL_END,
nullptr); nullptr);
lv_obj_add_event_cb(lvobj, ViewMain::long_pressed, LV_EVENT_LONG_PRESSED,
nullptr);
// create last to be on top // create last to be on top
topbar = TopbarFactory::create(this); topbar = TopbarFactory::create(this);
@@ -342,7 +340,7 @@ void ViewMain::ws_timer(lv_timer_t* t)
view->enableWidgetSelect(false); view->enableWidgetSelect(false);
} }
void ViewMain::longPress() bool ViewMain::onLongPress()
{ {
if (isAppMode()) { if (isAppMode()) {
int view = getCurrentMainView(); int view = getCurrentMainView();
@@ -350,17 +348,8 @@ void ViewMain::longPress()
} else { } else {
enableWidgetSelect(true); enableWidgetSelect(true);
} }
}
void ViewMain::long_pressed(lv_event_t* e)
{
auto obj = lv_event_get_target(e);
// kill subsequent CLICKED event
lv_obj_clear_state(obj, LV_STATE_PRESSED);
lv_indev_wait_release(lv_indev_get_act()); lv_indev_wait_release(lv_indev_get_act());
return false;
auto view = (ViewMain*)lv_obj_get_user_data(obj);
if (view) view->longPress();
} }
void ViewMain::show(bool visible) void ViewMain::show(bool visible)
+1 -2
View File
@@ -72,7 +72,7 @@ class ViewMain : public NavWindow
void onClicked() override; void onClicked() override;
void onCancel() override; void onCancel() override;
void openMenu(); void openMenu();
void longPress(); bool onLongPress() override;
void show(bool visible = true) override; void show(bool visible = true) override;
void showTopBarEdgeTxButton(); void showTopBarEdgeTxButton();
@@ -103,7 +103,6 @@ class ViewMain : public NavWindow
// Set topbar visibility [0.0 -> 1.0] // Set topbar visibility [0.0 -> 1.0]
void setTopbarVisible(float visible); void setTopbarVisible(float visible);
static void long_pressed(lv_event_t* e);
static void ws_timer(lv_timer_t* t); static void ws_timer(lv_timer_t* t);
#if defined(HARDWARE_KEYS) #if defined(HARDWARE_KEYS)
+2 -1
View File
@@ -144,9 +144,10 @@ void Widget::setFullscreen(bool enable)
update(); update();
} }
void Widget::onLongPress() bool Widget::onLongPress()
{ {
if (!fullscreen) ButtonBase::onLongPress(); if (!fullscreen) ButtonBase::onLongPress();
return true;
} }
const ZoneOption* Widget::getOptions() const const ZoneOption* Widget::getOptions() const
+1 -1
View File
@@ -97,7 +97,7 @@ class Widget : public ButtonBase
lv_point_t borderPts[5]; lv_point_t borderPts[5];
void onCancel() override; void onCancel() override;
void onLongPress() override; bool onLongPress() override;
virtual void onFullscreen(bool enable) {} virtual void onFullscreen(bool enable) {}
void openMenu(); void openMenu();
+6
View File
@@ -97,9 +97,15 @@ event_t Key::input(bool val)
if ((m_state || m_flags) && m_vals == 0) { if ((m_state || m_flags) && m_vals == 0) {
// key is released // key is released
#if defined(COLORLCD)
if ((m_flags & (KFLAG_KILLED)) == 0) {
evt = (m_flags & KFLAG_LONG_PRESS) ? _MSK_KEY_LONG_BRK : _MSK_KEY_BREAK;
}
#else
if ((m_flags & (KFLAG_KILLED|KFLAG_LONG_PRESS)) == 0) { if ((m_flags & (KFLAG_KILLED|KFLAG_LONG_PRESS)) == 0) {
evt = _MSK_KEY_BREAK; evt = _MSK_KEY_BREAK;
} }
#endif
m_state = KSTATE_OFF; m_state = KSTATE_OFF;
m_cnt = 0; m_cnt = 0;
m_flags = 0; m_flags = 0;
+1
View File
@@ -38,6 +38,7 @@ constexpr event_t _MSK_KEY_BREAK = 0x0200;
constexpr event_t _MSK_KEY_REPT = 0x0400; constexpr event_t _MSK_KEY_REPT = 0x0400;
constexpr event_t _MSK_KEY_FIRST = 0x0600; constexpr event_t _MSK_KEY_FIRST = 0x0600;
constexpr event_t _MSK_KEY_LONG = 0x0800; constexpr event_t _MSK_KEY_LONG = 0x0800;
constexpr event_t _MSK_KEY_LONG_BRK = 0x0A00;
constexpr event_t _MSK_KEY_FLAGS = 0x0E00; constexpr event_t _MSK_KEY_FLAGS = 0x0E00;
#else #else
constexpr event_t _MSK_KEY_BREAK = 0x0020; constexpr event_t _MSK_KEY_BREAK = 0x0020;
+3 -10
View File
@@ -58,8 +58,6 @@ ButtonBase::ButtonBase(Window* parent, const rect_t& rect,
objConstruct ? objConstruct : lv_btn_create), objConstruct ? objConstruct : lv_btn_create),
pressHandler(std::move(pressHandler)) pressHandler(std::move(pressHandler))
{ {
lv_obj_add_event_cb(lvobj, ButtonBase::long_pressed, LV_EVENT_LONG_PRESSED,
nullptr);
} }
void ButtonBase::check(bool checked) void ButtonBase::check(bool checked)
@@ -81,13 +79,15 @@ bool ButtonBase::checked() const
void ButtonBase::onPress() { check(pressHandler && pressHandler()); } void ButtonBase::onPress() { check(pressHandler && pressHandler()); }
void ButtonBase::onLongPress() bool ButtonBase::onLongPress()
{ {
if (longPressHandler) { if (longPressHandler) {
check(longPressHandler()); check(longPressHandler());
lv_obj_clear_state(lvobj, LV_STATE_PRESSED); lv_obj_clear_state(lvobj, LV_STATE_PRESSED);
lv_indev_wait_release(lv_indev_get_act()); lv_indev_wait_release(lv_indev_get_act());
return false;
} }
return true;
} }
void ButtonBase::onClicked() { onPress(); } void ButtonBase::onClicked() { onPress(); }
@@ -98,13 +98,6 @@ void ButtonBase::checkEvents()
if (checkHandler) checkHandler(); if (checkHandler) checkHandler();
} }
void ButtonBase::long_pressed(lv_event_t* e)
{
auto obj = lv_event_get_target(e);
auto btn = (ButtonBase*)lv_obj_get_user_data(obj);
if (obj) btn->onLongPress();
}
TextButton::TextButton(Window* parent, const rect_t& rect, std::string text, TextButton::TextButton(Window* parent, const rect_t& rect, std::string text,
std::function<uint8_t(void)> pressHandler) : std::function<uint8_t(void)> pressHandler) :
ButtonBase(parent, rect, pressHandler, button_create), ButtonBase(parent, rect, pressHandler, button_create),
+1 -3
View File
@@ -36,7 +36,7 @@ class ButtonBase : public FormField
#endif #endif
virtual void onPress(); virtual void onPress();
virtual void onLongPress(); bool onLongPress() override;
void onClicked() override; void onClicked() override;
void checkEvents() override; void checkEvents() override;
@@ -63,8 +63,6 @@ class ButtonBase : public FormField
std::function<uint8_t(void)> pressHandler; std::function<uint8_t(void)> pressHandler;
std::function<uint8_t(void)> longPressHandler; std::function<uint8_t(void)> longPressHandler;
std::function<void(void)> checkHandler; std::function<void(void)> checkHandler;
static void long_pressed(lv_event_t* e);
}; };
class Button : public ButtonBase class Button : public ButtonBase
+4 -27
View File
@@ -110,8 +110,7 @@ Choice::Choice(Window* parent, const rect_t& rect, int vmin, int vmax,
vmax(vmax), vmax(vmax),
menuTitle(title), menuTitle(title),
_getValue(std::move(_getValue)), _getValue(std::move(_getValue)),
_setValue(std::move(_setValue)), _setValue(std::move(_setValue))
longPressData({})
{ {
lv_event_send(lvobj, LV_EVENT_VALUE_CHANGED, nullptr); lv_event_send(lvobj, LV_EVENT_VALUE_CHANGED, nullptr);
} }
@@ -124,8 +123,7 @@ Choice::Choice(Window* parent, const rect_t& rect, const char* const values[],
vmax(vmax), vmax(vmax),
menuTitle(title), menuTitle(title),
_getValue(std::move(_getValue)), _getValue(std::move(_getValue)),
_setValue(std::move(_setValue)), _setValue(std::move(_setValue))
longPressData({})
{ {
setValues(values); setValues(values);
lv_event_send(lvobj, LV_EVENT_VALUE_CHANGED, nullptr); lv_event_send(lvobj, LV_EVENT_VALUE_CHANGED, nullptr);
@@ -141,8 +139,7 @@ Choice::Choice(Window* parent, const rect_t& rect,
vmax(vmax), vmax(vmax),
menuTitle(title), menuTitle(title),
_getValue(std::move(_getValue)), _getValue(std::move(_getValue)),
_setValue(std::move(_setValue)), _setValue(std::move(_setValue))
longPressData({})
{ {
lv_event_send(lvobj, LV_EVENT_VALUE_CHANGED, nullptr); lv_event_send(lvobj, LV_EVENT_VALUE_CHANGED, nullptr);
} }
@@ -179,7 +176,7 @@ void Choice::setValue(int val)
void Choice::onClicked() void Choice::onClicked()
{ {
if (!longPressData.isLongPressed) openMenu(); openMenu();
} }
void Choice::fillMenu(Menu* menu, const FilterFct& filter) void Choice::fillMenu(Menu* menu, const FilterFct& filter)
@@ -235,23 +232,3 @@ void Choice::openMenu()
menu->setCloseHandler([=]() { setEditMode(false); }); menu->setCloseHandler([=]() { setEditMode(false); });
} }
static void localLongPressHandler(lv_event_t* e)
{
lv_eventData_t* ld = (lv_eventData_t*)lv_event_get_user_data(e);
ld->isLongPressed = true;
ld->lv_LongPressHandler(ld->userData);
}
void Choice::set_lv_LongPressHandler(lvHandler_t longPressHandler, void* data)
{
TRACE("longPressHandler=%p", longPressHandler);
if (longPressHandler) {
longPressData.userData = data;
longPressData.lv_LongPressHandler = longPressHandler;
lv_obj_add_event_cb(lvobj, localLongPressHandler, LV_EVENT_LONG_PRESSED,
&longPressData);
lv_obj_add_event_cb(lvobj, ClickHandler, LV_EVENT_CLICKED, this);
}
}
-19
View File
@@ -22,14 +22,6 @@
#include "form.h" #include "form.h"
typedef void (*lvHandler_t)(void *);
typedef struct {
void *userData;
bool isLongPressed;
lvHandler_t lv_LongPressHandler;
} lv_eventData_t;
class Menu; class Menu;
enum ChoiceType { enum ChoiceType {
@@ -153,8 +145,6 @@ class Choice : public ChoiceBase
std::string getString(int val) { return values[val]; } std::string getString(int val) { return values[val]; }
void set_lv_LongPressHandler(lvHandler_t longPressHandler, void *data);
int selectedIx0 = 0; int selectedIx0 = 0;
protected: protected:
@@ -177,13 +167,4 @@ class Choice : public ChoiceBase
void fillMenu(Menu *menu, const FilterFct &filter = nullptr); void fillMenu(Menu *menu, const FilterFct &filter = nullptr);
virtual void openMenu(); virtual void openMenu();
lv_eventData_t longPressData;
std::function<void(event_t)> longPressHandler = nullptr;
static void ClickHandler(lv_event_t *e)
{
auto ch = (Choice *)lv_event_get_user_data(e);
if (ch) ch->longPressData.isLongPressed = false;
}
}; };
-21
View File
@@ -91,9 +91,6 @@ class MenuBody : public TableField
}); });
lv_group_set_editing(g, true); lv_group_set_editing(g, true);
} }
lv_obj_add_event_cb(lvobj, MenuBody::localLongPressHandler,
LV_EVENT_LONG_PRESSED, this);
} }
#if defined(DEBUG_WINDOWS) #if defined(DEBUG_WINDOWS)
@@ -219,13 +216,6 @@ class MenuBody : public TableField
isLongPressed = false; isLongPressed = false;
} }
void longPress()
{
isLongPressed = true;
Menu* menu = getParentMenu();
if (menu) menu->longPress();
}
void onDrawBegin(uint16_t row, uint16_t col, void onDrawBegin(uint16_t row, uint16_t col,
lv_obj_draw_part_dsc_t* dsc) override lv_obj_draw_part_dsc_t* dsc) override
{ {
@@ -284,12 +274,6 @@ class MenuBody : public TableField
int selectedIndex = 0; int selectedIndex = 0;
Menu* getParentMenu() { return static_cast<Menu*>(getParent()->getParent()); } Menu* getParentMenu() { return static_cast<Menu*>(getParent()->getParent()); }
static void localLongPressHandler(lv_event_t* e)
{
MenuBody* menuBody = (MenuBody*)lv_event_get_user_data(e);
if (menuBody) menuBody->longPress();
}
}; };
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@@ -451,11 +435,6 @@ void Menu::onCancel()
deleteLater(); deleteLater();
} }
void Menu::longPress()
{
if (toolbar) toolbar->longPress();
}
void Menu::setCancelHandler(std::function<void()> handler) void Menu::setCancelHandler(std::function<void()> handler)
{ {
cancelHandler = std::move(handler); cancelHandler = std::move(handler);
-2
View File
@@ -83,8 +83,6 @@ class Menu : public ModalWindow
} }
} }
void longPress();
protected: protected:
bool multiple; bool multiple;
MenuWindowContent *content; MenuWindowContent *content;
+2 -5
View File
@@ -22,9 +22,6 @@
#include "themes/etx_lv_theme.h" #include "themes/etx_lv_theme.h"
#include "translations.h" #include "translations.h"
constexpr uint32_t MENUS_TOOLBAR_BUTTON_WIDTH = 36;
constexpr uint32_t MENUS_TOOLBAR_BUTTON_HEIGHT = 32;
static const lv_obj_class_t menu_button_class = { static const lv_obj_class_t menu_button_class = {
.base_class = &button_class, .base_class = &button_class,
.constructor_cb = nullptr, .constructor_cb = nullptr,
@@ -116,13 +113,13 @@ rect_t MenuToolbar::getButtonRect(bool wideButton)
coord_t x = coord_t x =
(nxtBtnPos % filterColumns) * (MENUS_TOOLBAR_BUTTON_WIDTH + PAD_SMALL); (nxtBtnPos % filterColumns) * (MENUS_TOOLBAR_BUTTON_WIDTH + PAD_SMALL);
coord_t y = coord_t y =
(nxtBtnPos / filterColumns) * (MENUS_TOOLBAR_BUTTON_HEIGHT + PAD_SMALL); (nxtBtnPos / filterColumns) * (EdgeTxStyles::UI_ELEMENT_HEIGHT + PAD_SMALL);
coord_t w = wideButton ? (MENUS_TOOLBAR_BUTTON_WIDTH + PAD_SMALL) * coord_t w = wideButton ? (MENUS_TOOLBAR_BUTTON_WIDTH + PAD_SMALL) *
(filterColumns - 1) + (filterColumns - 1) +
MENUS_TOOLBAR_BUTTON_WIDTH MENUS_TOOLBAR_BUTTON_WIDTH
: MENUS_TOOLBAR_BUTTON_WIDTH; : MENUS_TOOLBAR_BUTTON_WIDTH;
nxtBtnPos += wideButton ? filterColumns : 1; nxtBtnPos += wideButton ? filterColumns : 1;
return {x, y, w, MENUS_TOOLBAR_BUTTON_HEIGHT}; return {x, y, w, EdgeTxStyles::UI_ELEMENT_HEIGHT};
} }
bool MenuToolbar::filterMenu(MenuToolbarButton* btn, int16_t filtermin, bool MenuToolbar::filterMenu(MenuToolbarButton* btn, int16_t filtermin,
+2 -7
View File
@@ -45,13 +45,8 @@ class MenuToolbar : public Window
virtual void longPress() {} virtual void longPress() {}
static LAYOUT_VAL(MENUS_TOOLBAR_BUTTON_WIDTH, 32, 32) static LAYOUT_VAL(MENUS_TOOLBAR_BUTTON_WIDTH, 36, 36)
static LAYOUT_VAL(MENUS_MAX_HEIGHT, ListBox::MENUS_LINE_HEIGHT * 7 + 8, ListBox::MENUS_LINE_HEIGHT * 10)
#if PORTRAIT_LCD
static constexpr coord_t MENUS_MAX_HEIGHT = (ListBox::MENUS_LINE_HEIGHT * 10);
#else
static constexpr coord_t MENUS_MAX_HEIGHT = (ListBox::MENUS_LINE_HEIGHT * 7) + 8;
#endif
protected: protected:
Choice* choice; Choice* choice;
+12 -3
View File
@@ -45,6 +45,14 @@ class NumberArea : public FormField
lv_obj_add_event_cb(lvobj, NumberArea::numberedit_cb, LV_EVENT_KEY, this); lv_obj_add_event_cb(lvobj, NumberArea::numberedit_cb, LV_EVENT_KEY, this);
setFocusHandler([=](bool focus) {
if (!focus && editMode) {
setEditMode(false);
hide();
lv_obj_clear_state(parent->getLvObj(), LV_STATE_FOCUSED);
}
});
update(); update();
} }
@@ -150,6 +158,7 @@ class NumberArea : public FormField
setEditMode(true); setEditMode(true);
} else { } else {
FormField::onClicked(); FormField::onClicked();
if (!editMode) changeEnd();
} }
} }
@@ -316,12 +325,12 @@ void NumberEdit::openEdit()
this->vmin, this->vmax, _getValue, _setValue, textFlags); this->vmin, this->vmax, _getValue, _setValue, textFlags);
edit->setChangeHandler([=]() { edit->setChangeHandler([=]() {
update(); update();
lv_group_focus_obj(lvobj); if (edit->hasFocus())
lv_group_focus_obj(lvobj);
edit->hide(); edit->hide();
}); });
edit->setCancelHandler([=]() { edit->setCancelHandler([=]() {
lv_group_focus_obj(lvobj); edit->onClicked();
edit->hide();
}); });
} }
edit->setTextFlag(textFlags); edit->setTextFlag(textFlags);
+69 -51
View File
@@ -24,6 +24,7 @@
#include "themes/etx_lv_theme.h" #include "themes/etx_lv_theme.h"
std::list<Window *> Window::trash; std::list<Window *> Window::trash;
bool Window::_longPressed = false;
const lv_obj_class_t window_base_class = { const lv_obj_class_t window_base_class = {
.base_class = &lv_obj_class, .base_class = &lv_obj_class,
@@ -43,49 +44,60 @@ lv_obj_t *window_create(lv_obj_t *parent)
} }
void Window::window_event_cb(lv_event_t *e) void Window::window_event_cb(lv_event_t *e)
{
Window *window = (Window *)lv_obj_get_user_data(lv_event_get_target(e));
if (window)
window->eventHandler(e);
}
void Window::eventHandler(lv_event_t *e)
{ {
lv_obj_t *target = lv_event_get_target(e); lv_obj_t *target = lv_event_get_target(e);
lv_event_code_t code = lv_event_get_code(e); lv_event_code_t code = lv_event_get_code(e);
Window *window = (Window *)lv_obj_get_user_data(target); if (code == LV_EVENT_DELETE || deleted()) return;
if (window) {
if (code == LV_EVENT_DELETE || window->deleted()) return;
switch (code) { switch (code) {
case LV_EVENT_SCROLL: { case LV_EVENT_SCROLL: {
// exclude pointer based scrolling (only focus scrolling) // exclude pointer based scrolling (only focus scrolling)
if (!lv_obj_is_scrolling(target)) { if (!lv_obj_is_scrolling(target)) {
lv_point_t *p = (lv_point_t *)lv_event_get_param(e); lv_point_t *p = (lv_point_t *)lv_event_get_param(e);
lv_coord_t scroll_bottom = lv_obj_get_scroll_bottom(target); lv_coord_t scroll_bottom = lv_obj_get_scroll_bottom(target);
lv_coord_t scroll_y = lv_obj_get_scroll_y(target); lv_coord_t scroll_y = lv_obj_get_scroll_y(target);
TRACE("SCROLL[x=%d;y=%d;top=%d;bottom=%d]", p->x, p->y, scroll_y, TRACE("SCROLL[x=%d;y=%d;top=%d;bottom=%d]", p->x, p->y, scroll_y,
scroll_bottom); scroll_bottom);
if (scroll_y <= 45 && p->y > 0) { if (scroll_y <= 45 && p->y > 0) {
lv_obj_scroll_by(target, 0, scroll_y, LV_ANIM_OFF); lv_obj_scroll_by(target, 0, scroll_y, LV_ANIM_OFF);
} else if (scroll_bottom <= 16 && p->y < 0) { } else if (scroll_bottom <= 16 && p->y < 0) {
lv_obj_scroll_by(target, 0, -scroll_bottom, LV_ANIM_OFF); lv_obj_scroll_by(target, 0, -scroll_bottom, LV_ANIM_OFF);
}
} }
} break; }
case LV_EVENT_CLICKED: } break;
TRACE("CLICKED[%p]", window); case LV_EVENT_CLICKED:
window->onClicked(); if (!_longPressed) {
break; TRACE("CLICKED[%p]", this);
case LV_EVENT_CANCEL: onClicked();
TRACE("CANCEL[%p]", window); }
window->onCancel(); _longPressed = false;
break; break;
case LV_EVENT_FOCUSED: case LV_EVENT_CANCEL:
if (window->focusHandler) window->focusHandler(true); TRACE("CANCEL[%p]", this);
break; onCancel();
case LV_EVENT_DEFOCUSED: break;
if (window->focusHandler) window->focusHandler(false); case LV_EVENT_FOCUSED:
break; if (focusHandler) focusHandler(true);
default: break;
break; case LV_EVENT_DEFOCUSED:
} if (focusHandler) focusHandler(false);
break;
case LV_EVENT_LONG_PRESSED:
TRACE("LONG PRESS[%p]", this);
_longPressed = onLongPress();
break;
default:
break;
} }
} }
@@ -295,23 +307,25 @@ void Window::onEvent(event_t event)
{ {
TRACE_WINDOWS("%s received event 0x%X", TRACE_WINDOWS("%s received event 0x%X",
Window::getWindowDebugString("Window").c_str(), event); Window::getWindowDebugString("Window").c_str(), event);
if (parent) { if (parent)
parent->onEvent(event); parent->onEvent(event);
}
} }
void Window::onClicked() void Window::onClicked()
{ {
if (parent && !(windowFlags & OPAQUE)) { if (parent && !(windowFlags & OPAQUE))
parent->onClicked(); parent->onClicked();
}
} }
void Window::onCancel() void Window::onCancel()
{ {
if (parent) { if (parent)
parent->onCancel(); parent->onCancel();
} }
bool Window::onLongPress()
{
return true;
} }
void Window::addChild(Window *window) void Window::addChild(Window *window)
@@ -355,21 +369,25 @@ FormLine *Window::newLine(FlexGridLayout &layout)
void Window::show(bool visible) void Window::show(bool visible)
{ {
if (lv_obj_has_flag(lvobj, LV_OBJ_FLAG_HIDDEN) == visible) { if (!_deleted && lvobj) {
if (visible) if (lv_obj_has_flag(lvobj, LV_OBJ_FLAG_HIDDEN) == visible) {
lv_obj_clear_flag(lvobj, LV_OBJ_FLAG_HIDDEN); if (visible)
else lv_obj_clear_flag(lvobj, LV_OBJ_FLAG_HIDDEN);
lv_obj_add_flag(lvobj, LV_OBJ_FLAG_HIDDEN); else
lv_obj_add_flag(lvobj, LV_OBJ_FLAG_HIDDEN);
}
} }
} }
void Window::enable(bool enabled) void Window::enable(bool enabled)
{ {
if (lv_obj_has_state(lvobj, LV_STATE_DISABLED) == enabled) { if (!_deleted && lvobj) {
if (enabled) if (lv_obj_has_state(lvobj, LV_STATE_DISABLED) == enabled) {
lv_obj_clear_state(lvobj, LV_STATE_DISABLED); if (enabled)
else lv_obj_clear_state(lvobj, LV_STATE_DISABLED);
lv_obj_add_state(lvobj, LV_STATE_DISABLED); else
lv_obj_add_state(lvobj, LV_STATE_DISABLED);
}
} }
} }
+3
View File
@@ -144,6 +144,7 @@ class Window
virtual void onEvent(event_t event); virtual void onEvent(event_t event);
virtual void onClicked(); virtual void onClicked();
virtual void onCancel(); virtual void onCancel();
virtual bool onLongPress();
void invalidate(); void invalidate();
@@ -192,6 +193,7 @@ class Window
LcdFlags textFlags = 0; LcdFlags textFlags = 0;
bool _deleted = false; bool _deleted = false;
static bool _longPressed;
std::function<void()> closeHandler; std::function<void()> closeHandler;
std::function<void(bool)> focusHandler; std::function<void(bool)> focusHandler;
@@ -201,6 +203,7 @@ class Window
virtual void addChild(Window *window); virtual void addChild(Window *window);
void removeChild(Window *window); void removeChild(Window *window);
void eventHandler(lv_event_t *e);
static void window_event_cb(lv_event_t *e); static void window_event_cb(lv_event_t *e);
}; };