Skip to content

Commit ae558c5

Browse files
committed
isActiveWidget optimization
1 parent 35468ec commit ae558c5

17 files changed

+139
-244
lines changed

src/bb3/psu/gui/page.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ void ToastMessagePage::onEncoderClicked() {
146146
void ToastMessagePage::updateInternalPage(const WidgetCursor &widgetCursor) {
147147
WidgetCursor toastPageWidgetCursor(widgetCursor.assets, appContext, &actionWidget, actionWidget.x, actionWidget.y, widgetCursor.currentState, widgetCursor.hasPreviousState);
148148

149-
auto temp = isActiveWidget(toastPageWidgetCursor);
149+
auto temp = toastPageWidgetCursor == g_activeWidget;
150150
if (widgetCursor.hasPreviousState && actionWidgetIsActive == temp && !dirty) {
151151
return;
152152
}
@@ -320,7 +320,7 @@ bool ToastMessagePage::canClickPassThrough() {
320320
}
321321

322322
bool ToastMessagePage::closeIfTouchedOutside() {
323-
return false;
323+
return hasAction();
324324
}
325325

326326
void ToastMessagePage::executeAction() {
@@ -702,7 +702,7 @@ void MenuWithButtonsPage::updateInternalPage(const WidgetCursor &widgetCursor2)
702702
widgetCursor.y = y + m_buttonTextWidgets[i].y;
703703
widgetCursor.cursor = i;
704704
TextWidgetState textWidgetState;
705-
textWidgetState.flags.active = isActiveWidget(widgetCursor);
705+
textWidgetState.flags.active = widgetCursor == g_activeWidget;
706706
textWidgetState.flags.blinking = 0;
707707
textWidgetState.flags.focused = 0;
708708
textWidgetState.render(widgetCursor);

src/bb3/psu/gui/psu.cpp

+17-35
Original file line numberDiff line numberDiff line change
@@ -396,18 +396,6 @@ void PsuAppContext::stateManagment() {
396396
}
397397
}
398398

399-
bool PsuAppContext::isActiveWidget(const WidgetCursor &widgetCursor) {
400-
if (getActivePageId() == PAGE_ID_TOUCH_CALIBRATION) {
401-
if (touch::getEventType() != EVENT_TYPE_TOUCH_NONE) {
402-
if (widgetCursor.widget->type == WIDGET_TYPE_TEXT && isTouchPointActivated()) {
403-
return true;
404-
}
405-
}
406-
}
407-
408-
return false;
409-
}
410-
411399
int PsuAppContext::getMainPageId() {
412400
return PAGE_ID_MAIN;
413401
}
@@ -831,17 +819,17 @@ void PsuAppContext::showUncaughtScriptExceptionMessage() {
831819
}
832820

833821
void TextInputParams::onSet(char *value) {
834-
popPage();
835-
836822
g_psuAppContext.m_textInputParams.m_input = value;
837823
g_psuAppContext.m_inputReady = true;
838-
}
839824

840-
void TextInputParams::onCancel() {
841825
popPage();
826+
}
842827

828+
void TextInputParams::onCancel() {
843829
g_psuAppContext.m_textInputParams.m_input = nullptr;
844830
g_psuAppContext.m_inputReady = true;
831+
832+
popPage();
845833
}
846834

847835
const char *PsuAppContext::textInput(const char *label, size_t minChars, size_t maxChars, const char *value) {
@@ -866,17 +854,17 @@ void PsuAppContext::doShowTextInput() {
866854
}
867855

868856
void NumberInputParams::onSet(float value) {
869-
popPage();
870-
871857
g_psuAppContext.m_numberInputParams.m_input = value;
872858
g_psuAppContext.m_inputReady = true;
873-
}
874859

875-
void NumberInputParams::onCancel() {
876860
popPage();
861+
}
877862

863+
void NumberInputParams::onCancel() {
878864
g_psuAppContext.m_numberInputParams.m_input = NAN;
879865
g_psuAppContext.m_inputReady = true;
866+
867+
popPage();
880868
}
881869

882870
float PsuAppContext::numberInput(const char *label, Unit unit, float min, float max, float value) {
@@ -909,18 +897,18 @@ void PsuAppContext::doShowNumberInput() {
909897
}
910898

911899
void IntegerInputParams::onSet(float value) {
912-
popPage();
913-
914900
g_psuAppContext.m_integerInputParams.m_input = (int32_t)value;
915901
g_psuAppContext.m_integerInputParams.canceled = false;
916902
g_psuAppContext.m_inputReady = true;
917-
}
918903

919-
void IntegerInputParams::onCancel() {
920904
popPage();
905+
}
921906

907+
void IntegerInputParams::onCancel() {
922908
g_psuAppContext.m_integerInputParams.canceled = true;
923909
g_psuAppContext.m_inputReady = true;
910+
911+
popPage();
924912
}
925913

926914
bool PsuAppContext::integerInput(const char *label, int32_t min, int32_t max, int32_t &value) {
@@ -1063,10 +1051,10 @@ int PsuAppContext::getExtraLongTouchActionHook(const WidgetCursor &widgetCursor)
10631051
}
10641052

10651053
void MenuInputParams::onSet(int value) {
1066-
popPage();
1067-
10681054
g_psuAppContext.m_menuInputParams.m_input = value;
10691055
g_psuAppContext.m_inputReady = true;
1056+
1057+
popPage();
10701058
}
10711059

10721060
int PsuAppContext::menuInput(const char *label, MenuType menuType, const char **menuItems) {
@@ -1123,12 +1111,11 @@ void SelectParams::enumDefinition(DataOperationEnum operation, const WidgetCurso
11231111
}
11241112

11251113
void SelectParams::onSelect(uint16_t value) {
1126-
popPage();
1127-
11281114
g_psuAppContext.m_selectParams.m_input = value;
11291115
g_psuAppContext.m_inputReady = true;
1130-
}
11311116

1117+
popPage();
1118+
}
11321119

11331120
void PsuAppContext::doShowSelect() {
11341121
pushSelectFromEnumPage(SelectParams::enumDefinition, m_selectParams.m_defaultSelection, nullptr, SelectParams::onSelect, false, true);
@@ -1150,22 +1137,17 @@ bool PsuAppContext::canExecuteActionWhenTouchedOutsideOfActivePage(int pageId, i
11501137
return false;
11511138
}
11521139

1153-
void PsuAppContext::updatePage(int i, WidgetCursor &widgetCursor) {
1154-
AppContext::updatePage(i, widgetCursor);
1155-
1140+
void PsuAppContext::pageRenderCustom(int i, WidgetCursor &widgetCursor) {
11561141
if (getActivePageId() == PAGE_ID_TOUCH_CALIBRATION_YES_NO || getActivePageId() == PAGE_ID_TOUCH_CALIBRATION_YES_NO_CANCEL) {
11571142
auto eventType = touch::getEventType();
11581143
if (eventType == EVENT_TYPE_TOUCH_DOWN || eventType == EVENT_TYPE_TOUCH_MOVE) {
1159-
display::selectBuffer(m_pageNavigationStack[m_pageNavigationStackPointer].displayBufferIndex);
11601144
int x = MIN(MAX(touch::getX(), 1), eez::display::getDisplayWidth() - 2);
11611145
int y = MIN(MAX(touch::getY(), 1), eez::display::getDisplayHeight() - 2);
11621146
eez::display::setColor(255, 255, 255);
11631147
eez::display::fillRect(x - 1, y - 1, x + 1, y + 1);
11641148
}
11651149
} else if (getActivePageId() == PAGE_ID_TOUCH_TEST) {
11661150
if (g_findCallback == nullptr) {
1167-
display::selectBuffer(m_pageNavigationStack[i].displayBufferIndex);
1168-
11691151
if (get(widgetCursor, DATA_ID_TOUCH_CALIBRATED_PRESSED).getInt()) {
11701152
int x = MIN(MAX(get(widgetCursor, DATA_ID_TOUCH_CALIBRATED_X).getInt(), 1), eez::display::getDisplayWidth() - 2);
11711153
int y = MIN(MAX(get(widgetCursor, DATA_ID_TOUCH_CALIBRATED_Y).getInt(), 1), eez::display::getDisplayHeight() - 2);

src/bb3/psu/gui/psu.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,6 @@ class PsuAppContext : public AppContext {
197197

198198
void stateManagment() override;
199199

200-
bool isActiveWidget(const WidgetCursor &widgetCursor) override;
201-
202200
bool isFocusWidget(const WidgetCursor &widgetCursor) override;
203201

204202
bool isBlinking(const WidgetCursor &widgetCursor, int16_t id) override;
@@ -285,7 +283,7 @@ class PsuAppContext : public AppContext {
285283
void onPageTouch(const WidgetCursor &foundWidget, Event &touchEvent) override;
286284
bool testExecuteActionOnTouchDown(int action) override;
287285
bool canExecuteActionWhenTouchedOutsideOfActivePage(int pageId, int action) override;
288-
void updatePage(int i, WidgetCursor &widgetCursor) override;
286+
void pageRenderCustom(int i, WidgetCursor &widgetCursor) override;
289287

290288
private:
291289
void doShowProgressPage();

src/bb3/psu/gui/touch_calibration.cpp

+22-5
Original file line numberDiff line numberDiff line change
@@ -126,20 +126,37 @@ void selectTouchCalibrationPoint() {
126126
}
127127
}
128128

129+
bool isTouchPointActivated() {
130+
return millis() - g_pointStartTime > TOUCH_POINT_ACTIVATION_THRESHOLD;
131+
}
132+
133+
void findActiveWidget(const WidgetCursor &widgetCursor) {
134+
if (g_activeWidget) {
135+
return;
136+
}
137+
138+
if (widgetCursor.appContext->getActivePageId() == PAGE_ID_TOUCH_CALIBRATION) {
139+
if (widgetCursor.widget->type == WIDGET_TYPE_TEXT) {
140+
g_activeWidget = widgetCursor;
141+
}
142+
}
143+
}
144+
129145
void onTouchCalibrationPageTouch(const WidgetCursor &foundWidget, Event &touchEvent) {
130146
if (touchEvent.type == EVENT_TYPE_TOUCH_DOWN) {
131147
g_pointStartTime = millis();
148+
} else if (touchEvent.type == EVENT_TYPE_TOUCH_MOVE) {
149+
if (!g_activeWidget && isTouchPointActivated()) {
150+
forEachWidget(findActiveWidget);
151+
}
132152
} else if (touchEvent.type == EVENT_TYPE_TOUCH_UP) {
133-
if (millis() - g_pointStartTime > TOUCH_POINT_ACTIVATION_THRESHOLD) {
153+
if (isTouchPointActivated()) {
154+
g_activeWidget = 0;
134155
selectTouchCalibrationPoint();
135156
}
136157
}
137158
}
138159

139-
bool isTouchPointActivated() {
140-
return millis() - g_pointStartTime > TOUCH_POINT_ACTIVATION_THRESHOLD;
141-
}
142-
143160
} // namespace gui
144161
} // namespace psu
145162
} // namespace eez

src/bb3/psu/gui/touch_calibration.h

-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ namespace gui {
2727
bool isTouchCalibrated();
2828
void enterTouchCalibration();
2929
void onTouchCalibrationPageTouch(const WidgetCursor &foundWidget, Event &touchEvent);
30-
bool isTouchPointActivated();
3130

3231
} // namespace gui
3332
} // namespace psu

src/eez/gui/app_context.cpp

+9-13
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ namespace gui {
4646
////////////////////////////////////////////////////////////////////////////////
4747

4848
AppContext::AppContext() {
49+
m_updatePageIndex = -1;
4950
}
5051

5152
void AppContext::stateManagment() {
@@ -263,10 +264,6 @@ bool AppContext::isBlinking(const WidgetCursor &widgetCursor, int16_t id) {
263264
return false;
264265
}
265266

266-
bool AppContext::isActiveWidget(const WidgetCursor &widgetCursor) {
267-
return false;
268-
}
269-
270267
bool AppContext::canExecuteActionWhenTouchedOutsideOfActivePage(int pageId, int action) {
271268
return false;
272269
}
@@ -299,15 +296,10 @@ void AppContext::onPageTouch(const WidgetCursor &foundWidget, Event &touchEvent)
299296

300297
void AppContext::updatePage(int i, WidgetCursor &widgetCursor) {
301298
if (g_findCallback == nullptr) {
302-
if (!widgetCursor.hasPreviousState) {
303-
m_pageNavigationStack[i].displayBufferIndex = display::allocBuffer();
304-
}
305-
306-
display::selectBuffer(m_pageNavigationStack[i].displayBufferIndex);
299+
m_pageNavigationStack[i].displayBufferIndex = display::beginBufferRendering();
307300
}
308301

309-
auto savedPageNavigationStackPointer = m_pageNavigationStackPointer;
310-
m_pageNavigationStackPointer = i;
302+
m_updatePageIndex = i;
311303

312304
int x;
313305
int y;
@@ -345,10 +337,14 @@ void AppContext::updatePage(int i, WidgetCursor &widgetCursor) {
345337
}
346338

347339
if (g_findCallback == nullptr) {
348-
display::setBufferBounds(m_pageNavigationStack[i].displayBufferIndex, x, y, width, height, withShadow, 255, 0, 0, withShadow && activePageHasBackdropHook() ? &rect : nullptr);
340+
pageRenderCustom(i, widgetCursor);
341+
display::endBufferRendering(m_pageNavigationStack[i].displayBufferIndex, x, y, width, height, withShadow, 255, 0, 0, withShadow && activePageHasBackdropHook() ? &rect : nullptr);
349342
}
350343

351-
m_pageNavigationStackPointer = savedPageNavigationStackPointer;
344+
m_updatePageIndex = -1;
345+
}
346+
347+
void AppContext::pageRenderCustom(int i, WidgetCursor &widgetCursor) {
352348
}
353349

354350
bool isRect1FullyCoveredByRect2(int xRect1, int yRect1, int wRect1, int hRect1, int xRect2, int yRect2, int wRect2, int hRect2) {

src/eez/gui/app_context.h

+12-6
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,23 @@ class AppContext {
5252
void popPage();
5353
void removePageFromStack(int pageId);
5454

55+
int getActivePageStackPointer() {
56+
return m_updatePageIndex != -1 ? m_updatePageIndex : m_pageNavigationStackPointer;
57+
}
58+
5559
int getActivePageId() {
56-
return m_pageNavigationStack[m_pageNavigationStackPointer].pageId;
60+
return m_pageNavigationStack[getActivePageStackPointer()].pageId;
5761
}
5862

5963
Page *getActivePage() {
60-
return m_pageNavigationStack[m_pageNavigationStackPointer].page;
64+
return m_pageNavigationStack[getActivePageStackPointer()].page;
6165
}
6266

6367
bool isActivePageInternal();
6468

6569
int getPreviousPageId() {
66-
return m_pageNavigationStackPointer == 0 ? PAGE_ID_NONE : m_pageNavigationStack[m_pageNavigationStackPointer - 1].pageId;
70+
int index = getActivePageStackPointer();
71+
return index == 0 ? PAGE_ID_NONE : m_pageNavigationStack[index - 1].pageId;
6772
}
6873

6974
void replacePage(int pageId, Page *page = nullptr);
@@ -79,7 +84,6 @@ class AppContext {
7984

8085
virtual bool isBlinking(const WidgetCursor &widgetCursor, int16_t id);
8186

82-
virtual bool isActiveWidget(const WidgetCursor &widgetCursor);
8387
virtual void onPageTouch(const WidgetCursor &foundWidget, Event &touchEvent);
8488

8589
virtual bool testExecuteActionOnTouchDown(int action);
@@ -97,6 +101,7 @@ class AppContext {
97101
protected:
98102
PageOnStack m_pageNavigationStack[CONF_GUI_PAGE_NAVIGATION_STACK_SIZE];
99103
int m_pageNavigationStackPointer = 0;
104+
int m_updatePageIndex;
100105

101106
uint32_t m_showPageTime;
102107

@@ -106,8 +111,9 @@ class AppContext {
106111
void doShowPage(int index, Page *page, int previousPageId);
107112
void setPage(int pageId);
108113

109-
virtual void updatePage(int i, WidgetCursor &widgetCursor);
110-
114+
void updatePage(int i, WidgetCursor &widgetCursor);
115+
virtual void pageRenderCustom(int i, WidgetCursor &widgetCursor);
116+
111117
bool isPageFullyCovered(int pageNavigationStackIndex);
112118

113119
virtual bool canExecuteActionWhenTouchedOutsideOfActivePage(int pageId, int action);

src/eez/gui/display-private.h

+1-5
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,10 @@ void drawStrInit();
3636
void drawGlyph(const uint8_t *src, uint32_t srcLineOffset, int x, int y, int width, int height);
3737

3838
static const int NUM_BUFFERS = 6;
39-
struct BufferFlags {
40-
unsigned allocated : 1;
41-
unsigned used : 1;
42-
};
4339

4440
struct Buffer {
4541
void *bufferPointer;
46-
BufferFlags flags;
42+
void *previousBuffer;
4743
int x;
4844
int y;
4945
int width;

0 commit comments

Comments
 (0)