Skip to content

Commit c5a7d6e

Browse files
author
philmoz
committed
Fix and cleanup long press key handling.
1 parent 8087283 commit c5a7d6e

30 files changed

+174
-222
lines changed

radio/src/gui/colorlcd/LvglWrapper.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,10 @@ static void copy_kb_data_backup(lv_indev_data_t* data)
6666
memcpy(data, &kb_data_backup, sizeof(lv_indev_data_t));
6767
}
6868

69-
constexpr event_t _KEY_PRESSED = _MSK_KEY_FLAGS & ~_MSK_KEY_BREAK;
70-
7169
static bool evt_to_indev_data(event_t evt, lv_indev_data_t *data)
7270
{
7371
event_t key = EVT_KEY_MASK(evt);
72+
7473
switch(key) {
7574

7675
case KEY_ENTER:
@@ -90,7 +89,8 @@ static bool evt_to_indev_data(event_t evt, lv_indev_data_t *data)
9089
return false;
9190
}
9291

93-
if (evt & _KEY_PRESSED) {
92+
event_t flgs = evt & _MSK_KEY_FLAGS;
93+
if (flgs != _MSK_KEY_BREAK && flgs != _MSK_KEY_LONG_BRK) {
9494
data->state = LV_INDEV_STATE_PRESSED;
9595
} else {
9696
data->state = LV_INDEV_STATE_RELEASED;
@@ -122,6 +122,12 @@ static void keyboardDriverRead(lv_indev_drv_t *drv, lv_indev_data_t *data)
122122
if (isEvent()) { // event waiting
123123
event_t evt = getEvent();
124124

125+
if ((evt & _MSK_KEY_FLAGS) == _MSK_KEY_LONG_BRK) {
126+
data->state = LV_INDEV_STATE_RELEASED;
127+
backup_kb_data(data);
128+
return;
129+
}
130+
125131
if(evt == EVT_KEY_FIRST(KEY_PAGEUP) ||
126132
evt == EVT_KEY_FIRST(KEY_PAGEDN) ||
127133
evt == EVT_KEY_FIRST(KEY_ENTER) ||

radio/src/gui/colorlcd/curve_param.cpp

+31-8
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,34 @@ void CurveParam::value_changed(lv_event_t* e)
4646
param->update();
4747
}
4848

49+
class CurveChoice : public Choice
50+
{
51+
public:
52+
CurveChoice(Window* parent, CurveRef* ref, std::function<void(int32_t)> setRefValue, std::function<void(void)> refreshView) :
53+
Choice(parent, rect_t{}, -MAX_CURVES, MAX_CURVES, GET_DEFAULT(ref->value), setRefValue),
54+
ref(ref), refreshView(refreshView)
55+
{
56+
setTextHandler([](int value) { return getCurveString(value); });
57+
}
58+
59+
bool onLongPress() override
60+
{
61+
if (modelCurvesEnabled()) {
62+
if (ref->value) {
63+
ModelCurvesPage::pushEditCurve(abs(ref->value) - 1, refreshView);
64+
}
65+
}
66+
return true;
67+
}
68+
69+
protected:
70+
CurveRef* ref;
71+
std::function<void(void)> refreshView;
72+
};
73+
4974
CurveParam::CurveParam(Window* parent, const rect_t& rect, CurveRef* ref,
50-
std::function<void(int32_t)> setRefValue) :
51-
Window(parent, rect), ref(ref), setRefValue(setRefValue)
75+
std::function<void(int32_t)> setRefValue, std::function<void(void)> refreshView) :
76+
Window(parent, rect), ref(ref)
5277
{
5378
padAll(PAD_TINY);
5479
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,
6792

6893
// CURVE_REF_DIFF
6994
// CURVE_REF_EXPO
70-
value_edit = new GVarNumberEdit(this, -100, 100, GET_DEFAULT(ref->value), setRefValue);
71-
value_edit->setSuffix("%");
95+
auto gv = new GVarNumberEdit(this, -100, 100, GET_DEFAULT(ref->value), setRefValue);
96+
gv->setSuffix("%");
97+
value_edit = gv;
7298

7399
// CURVE_REF_FUNC
74100
func_choice = new Choice(this, rect_t{}, STR_VCURVEFUNC, 0, CURVE_BASE - 1,
75101
GET_DEFAULT(ref->value), setRefValue);
76102

77103
// CURVE_REF_CUSTOM
78-
cust_choice = new Choice(this, rect_t{}, -MAX_CURVES, MAX_CURVES,
79-
GET_DEFAULT(ref->value), setRefValue);
80-
cust_choice->setTextHandler([](int value) { return getCurveString(value); });
81-
cust_choice->set_lv_LongPressHandler(LongPressHandler, &(ref->value));
104+
cust_choice = new CurveChoice(this, ref, setRefValue, refreshView);
82105

83106
update();
84107
}

radio/src/gui/colorlcd/curve_param.h

+5-9
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,15 @@
2525

2626
struct CurveRef;
2727

28-
class GVarNumberEdit;
29-
class Choice;
30-
3128
class CurveParam : public Window
3229
{
3330
// Curve
3431
CurveRef* ref;
35-
std::function<void(int32_t)> setRefValue;
3632

3733
// Controls
38-
GVarNumberEdit* value_edit;
39-
Choice* func_choice;
40-
Choice* cust_choice;
41-
34+
Window* value_edit;
35+
Window* func_choice;
36+
Window* cust_choice;
4237
Window* act_field = nullptr;
4338

4439
void update();
@@ -47,5 +42,6 @@ class CurveParam : public Window
4742

4843
public:
4944
CurveParam(Window* parent, const rect_t& rect, CurveRef* ref,
50-
std::function<void(int32_t)> setRefValue);
45+
std::function<void(int32_t)> setRefValue,
46+
std::function<void(void)> refreshView = nullptr);
5147
};

radio/src/gui/colorlcd/curveedit.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,8 @@ void CurveEdit::checkEvents()
240240
Window::checkEvents();
241241
}
242242

243-
CurveEditWindow::CurveEditWindow(uint8_t index) :
244-
Page(ICON_MODEL_CURVES, PAD_ZERO), index(index)
243+
CurveEditWindow::CurveEditWindow(uint8_t index, std::function<void(void)> refreshView) :
244+
Page(ICON_MODEL_CURVES, PAD_ZERO), index(index), refreshView(refreshView)
245245
{
246246
buildBody(body);
247247
buildHeader(header);
@@ -392,3 +392,9 @@ void CurveEditWindow::buildBody(Window* window)
392392

393393
curveDataEdit->setCurveEdit(curveEdit);
394394
}
395+
396+
void CurveEditWindow::onCancel()
397+
{
398+
if (refreshView) refreshView();
399+
Page::onCancel();
400+
}

radio/src/gui/colorlcd/curveedit.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,16 @@ class CurveDataEdit : public Window
8282
class CurveEditWindow : public Page
8383
{
8484
public:
85-
CurveEditWindow(uint8_t index);
85+
CurveEditWindow(uint8_t index, std::function<void(void)> refreshView = nullptr);
8686

8787
protected:
8888
uint8_t index;
8989
CurveEdit * curveEdit = nullptr;
9090
CurveDataEdit * curveDataEdit = nullptr;
91+
std::function<void(void)> refreshView = nullptr;
9192

9293
void buildHeader(Window * window);
9394
void buildBody(Window * window);
95+
96+
void onCancel() override;
9497
};

radio/src/gui/colorlcd/fullscreen_dialog.cpp

+4-10
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ FullScreenDialog::FullScreenDialog(
7474

7575
build();
7676

77-
lv_obj_add_event_cb(lvobj, FullScreenDialog::long_pressed,
78-
LV_EVENT_LONG_PRESSED, nullptr);
7977
lv_obj_add_event_cb(lvobj, FullScreenDialog::on_draw,
8078
LV_EVENT_DRAW_MAIN_BEGIN, nullptr);
8179
}
@@ -164,15 +162,11 @@ void FullScreenDialog::closeDialog()
164162
deleteLater();
165163
}
166164

167-
void FullScreenDialog::long_pressed(lv_event_t* e)
165+
bool FullScreenDialog::onLongPress()
168166
{
169-
auto obj = lv_event_get_target(e);
170-
auto fs = (FullScreenDialog*)lv_obj_get_user_data(obj);
171-
172-
if (fs) {
173-
fs->closeDialog();
174-
lv_indev_wait_release(lv_indev_get_act());
175-
}
167+
closeDialog();
168+
lv_indev_wait_release(lv_indev_get_act());
169+
return false;
176170
}
177171

178172
void FullScreenDialog::onEvent(event_t event)

radio/src/gui/colorlcd/fullscreen_dialog.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class FullScreenDialog : public Window
4343

4444
void onEvent(event_t event) override;
4545
void onCancel() override;
46+
bool onLongPress() override;
4647

4748
void deleteLater(bool detach = true, bool trash = true) override;
4849

@@ -71,6 +72,5 @@ class FullScreenDialog : public Window
7172
virtual void delayedInit() {}
7273
void build();
7374

74-
static void long_pressed(lv_event_t* e);
7575
static void on_draw(lv_event_t* e);
7676
};

radio/src/gui/colorlcd/input_edit.cpp

+9-5
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,15 @@ void InputEditWindow::buildBody(Window* form)
135135
line = form->newLine(grid);
136136
new StaticText(line, rect_t{}, STR_CURVE);
137137
auto param =
138-
new CurveParam(line, rect_t{}, &input->curve, [=](int32_t newValue) {
139-
input->curve.value = newValue;
140-
preview->update();
141-
SET_DIRTY();
142-
});
138+
new CurveParam(line, rect_t{}, &input->curve,
139+
[=](int32_t newValue) {
140+
input->curve.value = newValue;
141+
preview->update();
142+
SET_DIRTY();
143+
},
144+
[=]() {
145+
preview->update();
146+
});
143147
lv_obj_set_style_grid_cell_x_align(param->getLvObj(), LV_GRID_ALIGN_STRETCH,
144148
0);
145149

radio/src/gui/colorlcd/listbox.cpp

+4-13
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,6 @@
2323

2424
#include "libopenui.h"
2525

26-
// TODO:
27-
// - split this into 2 handlers:
28-
// - key_cb: PRESSED / LONG_PRESSED (always on)
29-
// - focus_cb: for FOCUSED / DEFOCUSED (only w/ 'auto-edit' mode)
30-
// - add 'auto-edit' mode (add / remove 'focus_cb')
31-
// - when turned on, ESC gets out of the edit-mode
32-
// and ENTER gets into edit-mode
33-
//
3426
void ListBox::event_cb(lv_event_t* e)
3527
{
3628
static bool _nested = false;
@@ -51,10 +43,6 @@ void ListBox::event_cb(lv_event_t* e)
5143
_nested = true;
5244
lv_group_set_editing((lv_group_t*)lv_obj_get_group(obj), false);
5345
_nested = false;
54-
} else if (code == LV_EVENT_LONG_PRESSED) {
55-
lb->onLongPressed();
56-
lv_obj_clear_state(obj, LV_STATE_PRESSED);
57-
lv_indev_wait_release(lv_indev_get_act());
5846
}
5947
}
6048

@@ -180,12 +168,15 @@ void ListBox::onPress(uint16_t row, uint16_t col)
180168
}
181169
}
182170

183-
void ListBox::onLongPressed()
171+
bool ListBox::onLongPress()
184172
{
185173
TRACE("LONG_PRESS");
186174
if (longPressHandler) {
187175
longPressHandler();
176+
lv_indev_wait_release(lv_indev_get_act());
177+
return false;
188178
}
179+
return true;
189180
}
190181

191182
// TODO: !auto-edit

radio/src/gui/colorlcd/listbox.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class ListBox : public TableField
9595
bool smallSelectMarker = false;
9696

9797
void onPress(uint16_t row, uint16_t col) override;
98-
void onLongPressed();
98+
bool onLongPress() override;
9999

100100
void onClicked() override;
101101
void onCancel() override;

radio/src/gui/colorlcd/model_curves.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,15 @@ ModelCurvesPage::ModelCurvesPage() : PageTab(STR_MENUCURVES, ICON_MODEL_CURVES)
122122

123123
// can be called from any other screen to edit a curve.
124124
// currently called from model_mixes.cpp on longpress.
125-
void ModelCurvesPage::pushEditCurve(int index)
125+
void ModelCurvesPage::pushEditCurve(int index, std::function<void(void)> refreshView)
126126
{
127127
if (!isCurveUsed(index)) {
128128
CurveHeader &curve = g_model.curves[index];
129129
int8_t *points = curveAddress(index);
130130
initPoints(curve, points);
131131
}
132132

133-
new CurveEditWindow(index);
133+
new CurveEditWindow(index, refreshView);
134134
}
135135

136136
void ModelCurvesPage::rebuild(Window *window)

radio/src/gui/colorlcd/model_curves.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class ModelCurvesPage : public PageTab
2828
{
2929
public:
3030
ModelCurvesPage();
31-
static void pushEditCurve(int index);
31+
static void pushEditCurve(int index, std::function<void(void)> refreshView = nullptr);
3232

3333
bool isVisible() const override { return modelCurvesEnabled(); }
3434

radio/src/gui/colorlcd/sourcechoice.cpp

+6-10
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,14 @@ class SourceChoiceMenuToolbar : public MenuToolbar
149149
#endif
150150
};
151151

152-
void SourceChoice::LongPressHandler(void* data)
152+
bool SourceChoice::onLongPress()
153153
{
154-
SourceChoice* src = (SourceChoice*)data;
155-
if (src && src->canInvert) {
156-
int16_t val = src->_getValue();
157-
if (src->isValueAvailable && src->isValueAvailable(-val)) {
158-
src->setValue(-val);
159-
src->invalidate();
160-
}
154+
if (canInvert) {
155+
int16_t val = _getValue();
156+
if (isValueAvailable && isValueAvailable(-val))
157+
setValue(-val);
161158
}
159+
return true;
162160
}
163161

164162
void SourceChoice::setValue(int value)
@@ -234,7 +232,5 @@ SourceChoice::SourceChoice(Window *parent, const rect_t &rect, int16_t vmin,
234232
return std::string(getSourceString(value));
235233
});
236234

237-
set_lv_LongPressHandler(LongPressHandler, this);
238-
239235
setAvailableHandler([](int v) { return isSourceAvailable(v); });
240236
}

radio/src/gui/colorlcd/sourcechoice.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ class SourceChoice : public Choice
3535
std::function<int16_t()> getValue,
3636
std::function<void(int16_t)> setValue, bool allowInvert = false);
3737

38-
static void LongPressHandler(void* data);
39-
4038
protected:
4139
friend SourceChoiceMenuToolbar;
4240

@@ -45,6 +43,7 @@ class SourceChoice : public Choice
4543

4644
void setValue(int value) override;
4745
int getIntValue() const override;
46+
bool onLongPress() override;
4847

4948
void invertChoice();
5049

radio/src/gui/colorlcd/switchchoice.cpp

+5-9
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,12 @@ class SwitchChoiceMenuToolbar : public MenuToolbar
103103
#endif
104104
};
105105

106-
void SwitchChoice::LongPressHandler(void* data)
106+
bool SwitchChoice::onLongPress()
107107
{
108-
SwitchChoice* swch = (SwitchChoice*)data;
109-
if (!swch) return;
110-
int16_t val = swch->_getValue();
111-
if (swch->isValueAvailable && swch->isValueAvailable(-val)) {
112-
swch->setValue(-val);
113-
}
108+
int16_t val = _getValue();
109+
if (isValueAvailable && isValueAvailable(-val))
110+
setValue(-val);
111+
return true;
114112
}
115113

116114
void SwitchChoice::setValue(int value)
@@ -182,7 +180,5 @@ SwitchChoice::SwitchChoice(Window* parent, const rect_t& rect, int vmin,
182180
return std::string(getSwitchPositionName(value));
183181
});
184182

185-
set_lv_LongPressHandler(LongPressHandler, this);
186-
187183
setAvailableHandler(isSwitchAvailableInMixes);
188184
}

0 commit comments

Comments
 (0)