Window::setFocus() refactoring

This commit is contained in:
Bertrand Songis
2021-02-24 18:55:41 +01:00
parent ca53750db1
commit 8d8e7c395b
9 changed files with 66 additions and 49 deletions
+8 -3
View File
@@ -19,14 +19,15 @@
#pragma once #pragma once
#include <utility>
#include "form.h" #include "form.h"
class CheckBox : public FormField { class CheckBox : public FormField {
public: public:
CheckBox(Window * parent, const rect_t & rect, std::function<uint8_t()> getValue, std::function<void(uint8_t)> setValue, WindowFlags flags = 0) : CheckBox(Window * parent, const rect_t & rect, std::function<uint8_t()> getValue, std::function<void(uint8_t)> setValue, WindowFlags flags = 0) :
FormField(parent, rect, flags), FormField(parent, rect, flags),
_getValue(getValue), _getValue(std::move(getValue)),
_setValue(setValue) _setValue(std::move(setValue))
{ {
} }
@@ -62,9 +63,13 @@ class CheckBox : public FormField {
return _getValue(); return _getValue();
} }
void setSetValueHandler(std::function<void(uint8_t)> handler)
{
_setValue = std::move(handler);
}
protected: protected:
std::string label; std::string label;
std::function<uint8_t()> _getValue; std::function<uint8_t()> _getValue;
std::function<void(uint8_t)> _setValue; std::function<void(uint8_t)> _setValue;
}; };
+5 -5
View File
@@ -28,12 +28,12 @@ void ExpansionPanel::updateHeight(bool move)
setHeight(newHeight); setHeight(newHeight);
} }
void ExpansionPanel::setFocus(uint8_t flag) void ExpansionPanel::setFocus(uint8_t flag, Window * from)
{ {
if (!enabled || isOpen) if (!enabled || isOpen)
FormGroup::setFocus(flag); FormGroup::setFocus(flag, from);
else else
header->setFocus(flag); header->setFocus(flag, from);
} }
ExpansionPanelHeader::ExpansionPanelHeader(ExpansionPanel * parent): ExpansionPanelHeader::ExpansionPanelHeader(ExpansionPanel * parent):
@@ -51,12 +51,12 @@ void ExpansionPanelHeader::onEvent(event_t event)
else if (event == EVT_ROTARY_RIGHT && !panel->isOpen) { else if (event == EVT_ROTARY_RIGHT && !panel->isOpen) {
auto next = panel->getNextField(); auto next = panel->getNextField();
if (next) if (next)
next->setFocus(SET_FOCUS_FORWARD); next->setFocus(SET_FOCUS_FORWARD, this);
} }
else if (event == EVT_ROTARY_LEFT) { else if (event == EVT_ROTARY_LEFT) {
auto previous = panel->getPreviousField(); auto previous = panel->getPreviousField();
if (previous) { if (previous) {
previous->setFocus(SET_FOCUS_BACKWARD); previous->setFocus(SET_FOCUS_BACKWARD, this);
} }
} }
else { else {
+1 -1
View File
@@ -89,7 +89,7 @@ class ExpansionPanel: public FormGroup
void updateHeight(bool move = true); void updateHeight(bool move = true);
void setFocus(uint8_t flag) override; void setFocus(uint8_t flag = SET_FOCUS_DEFAULT, Window * from = nullptr) override;
FormGroup * body = nullptr; FormGroup * body = nullptr;
+23 -25
View File
@@ -40,13 +40,13 @@ void FormField::onEvent(event_t event)
if (event == EVT_ROTARY_RIGHT/*EVT_KEY_BREAK(KEY_DOWN)*/) { if (event == EVT_ROTARY_RIGHT/*EVT_KEY_BREAK(KEY_DOWN)*/) {
if (next) { if (next) {
onKeyPress(); onKeyPress();
next->setFocus(SET_FOCUS_FORWARD); next->setFocus(SET_FOCUS_FORWARD, this);
} }
} }
else if (event == EVT_ROTARY_LEFT/*EVT_KEY_BREAK(KEY_UP)*/) { else if (event == EVT_ROTARY_LEFT/*EVT_KEY_BREAK(KEY_UP)*/) {
if (previous) { if (previous) {
onKeyPress(); onKeyPress();
previous->setFocus(SET_FOCUS_BACKWARD); previous->setFocus(SET_FOCUS_BACKWARD, this);
} }
} }
else if (event == EVT_KEY_BREAK(KEY_ENTER)) { else if (event == EVT_KEY_BREAK(KEY_ENTER)) {
@@ -65,22 +65,20 @@ void FormField::onEvent(event_t event)
} }
#endif #endif
void FormField::setFocus(uint8_t flag) void FormField::setFocus(uint8_t flag, Window * from)
{ {
if (enabled) { if (enabled) {
Window::setFocus(flag); Window::setFocus(flag, from);
} }
else { else {
clearFocus();
focusWindow = this;
if (flag == SET_FOCUS_BACKWARD) { if (flag == SET_FOCUS_BACKWARD) {
if (previous) { if (previous) {
previous->setFocus(flag); previous->setFocus(flag, this);
} }
} }
else { else {
if (next) { if (next) {
next->setFocus(flag); next->setFocus(flag, this);
} }
} }
} }
@@ -146,29 +144,27 @@ void FormGroup::addField(FormField * field, bool front)
} }
} }
void FormGroup::setFocus(uint8_t flag) void FormGroup::setFocus(uint8_t flag, Window * from)
{ {
TRACE_WINDOWS("%s setFocus(%d)", getWindowDebugString("FormGroup").c_str(), flag); TRACE_WINDOWS("%s setFocus(%d)", getWindowDebugString("FormGroup").c_str(), flag);
if (windowFlags & FORM_FORWARD_FOCUS) { if (windowFlags & FORM_FORWARD_FOCUS) {
switch (flag) { switch (flag) {
case SET_FOCUS_BACKWARD: case SET_FOCUS_BACKWARD:
if (focusWindow->isChild(first)) { if (from && from->isChild(first)) {
if (previous == this) { if (previous == this) {
last->setFocus(SET_FOCUS_BACKWARD); last->setFocus(SET_FOCUS_BACKWARD, this);
} }
else if (previous) { else if (previous) {
previous->setFocus(SET_FOCUS_BACKWARD); previous->setFocus(SET_FOCUS_BACKWARD, this);
} }
} }
else { else {
if (last) { if (last) {
last->setFocus(SET_FOCUS_BACKWARD); last->setFocus(SET_FOCUS_BACKWARD, this);
} }
else if (previous) { else if (previous) {
clearFocus(); previous->setFocus(SET_FOCUS_BACKWARD, this);
focusWindow = this;
previous->setFocus(SET_FOCUS_BACKWARD);
} }
} }
break; break;
@@ -178,31 +174,33 @@ void FormGroup::setFocus(uint8_t flag)
// no break; // no break;
case SET_FOCUS_FORWARD: case SET_FOCUS_FORWARD:
if (focusWindow && focusWindow->isChild(this)) { if (from && from->isChild(this)) {
if (next == this) { if (next == this) {
first->setFocus(SET_FOCUS_FORWARD); first->setFocus(SET_FOCUS_FORWARD, this);
} }
else if (next) { else if (next) {
next->setFocus(SET_FOCUS_FORWARD); next->setFocus(SET_FOCUS_FORWARD, this);
} }
} }
else { else {
if (first) { if (first) {
first->setFocus(SET_FOCUS_FORWARD); first->setFocus(SET_FOCUS_FORWARD, this);
} }
else if (next) { else if (next) {
clearFocus(); next->setFocus(SET_FOCUS_FORWARD, this);
focusWindow = this;
next->setFocus(SET_FOCUS_FORWARD);
} }
} }
break; break;
default: default:
if (focusWindow == previous) { if (from == previous) {
if (first) { if (first) {
first->setFocus(SET_FOCUS_DEFAULT); first->setFocus(SET_FOCUS_DEFAULT);
} }
else {
clearFocus();
focusWindow = this;
}
} }
else if (next) { else if (next) {
next->setFocus(SET_FOCUS_FORWARD); next->setFocus(SET_FOCUS_FORWARD);
@@ -215,7 +213,7 @@ void FormGroup::setFocus(uint8_t flag)
} }
} }
else if (!(windowFlags & NO_FOCUS)) { else if (!(windowFlags & NO_FOCUS)) {
FormField::setFocus(flag); FormField::setFocus(flag, from);
} }
} }
+4 -4
View File
@@ -79,7 +79,7 @@ class FormField: public Window
enable(false); enable(false);
} }
void setFocus(uint8_t flag) override; void setFocus(uint8_t flag = SET_FOCUS_DEFAULT, Window * from = nullptr) override;
void onFocusLost() override void onFocusLost() override
{ {
@@ -131,7 +131,7 @@ class FormGroup: public FormField
} }
} }
void setFocus(uint8_t flag) override; void setFocus(uint8_t flag = SET_FOCUS_DEFAULT, Window * from = nullptr) override;
virtual void addField(FormField * field, bool front = false); // NOLINT(google-default-arguments) virtual void addField(FormField * field, bool front = false); // NOLINT(google-default-arguments)
@@ -155,7 +155,7 @@ class FormGroup: public FormField
return last; return last;
} }
void setFocusOnFirstVisibleField(uint8_t flag) const void setFocusOnFirstVisibleField(uint8_t flag = SET_FOCUS_DEFAULT) const
{ {
auto field = getFirstField(); auto field = getFirstField();
while (field && !field->isInsideParentScrollingArea()) { while (field && !field->isInsideParentScrollingArea()) {
@@ -166,7 +166,7 @@ class FormGroup: public FormField
} }
} }
void setFocusOnLastVisibleField(uint8_t flag) const void setFocusOnLastVisibleField(uint8_t flag = SET_FOCUS_DEFAULT) const
{ {
auto field = getLastField(); auto field = getLastField();
while (field && !field->isInsideParentScrollingArea()) { while (field && !field->isInsideParentScrollingArea()) {
+4 -2
View File
@@ -117,8 +117,10 @@ void Table::Body::onEvent(event_t event)
else { else {
auto next = table->getNextField(); auto next = table->getNextField();
if (next) { if (next) {
select(-1, false); next->setFocus(SET_FOCUS_FORWARD, this);
next->setFocus(SET_FOCUS_FORWARD); if (!hasFocus()) {
select(-1, false);
}
} }
} }
} }
+18 -7
View File
@@ -239,9 +239,6 @@ class Table: public FormField
header(this, {0, 0, width(), 0}, columnsCount), header(this, {0, 0, width(), 0}, columnsCount),
body(this, {0, 0, width(), height()}, windowFlags) body(this, {0, 0, width(), height()}, windowFlags)
{ {
if (hasFocus()) {
setFocus(SET_FOCUS_DEFAULT);
}
} }
#if defined(DEBUG_WINDOWS) #if defined(DEBUG_WINDOWS)
@@ -292,11 +289,25 @@ class Table: public FormField
body.invalidate(); body.invalidate();
} }
void setFocus(uint8_t flag) override void setFocus(uint8_t flag = SET_FOCUS_DEFAULT, Window * from = nullptr) override
{ {
body.setFocus(flag); if (body.lines.empty()) {
if (body.selection < 0 && !body.lines.empty()) { if (flag == SET_FOCUS_BACKWARD) {
select(flag == SET_FOCUS_BACKWARD ? body.lines.size() - 1 : 0); if (previous) {
previous->setFocus(flag, this);
}
}
else {
if (next) {
next->setFocus(flag, this);
}
}
}
else {
body.setFocus(flag, from);
if (body.selection < 0) {
select(flag == SET_FOCUS_BACKWARD ? body.lines.size() - 1 : 0);
}
} }
} }
+2 -1
View File
@@ -122,7 +122,7 @@ void Window::clearFocus()
} }
} }
void Window::setFocus(uint8_t flag) void Window::setFocus(uint8_t flag, Window * from)
{ {
if (deleted()) if (deleted())
return; return;
@@ -139,6 +139,7 @@ void Window::setFocus(uint8_t flag)
parent->scrollTo(this); parent->scrollTo(this);
invalidate(); invalidate();
} }
clearFocus(); clearFocus();
focusWindow = this; focusWindow = this;
if (focusHandler) { if (focusHandler) {
+1 -1
View File
@@ -164,7 +164,7 @@ class Window
static void clearFocus(); static void clearFocus();
virtual void setFocus(uint8_t flag); virtual void setFocus(uint8_t flag = SET_FOCUS_DEFAULT, Window * from = nullptr);
void setRect(rect_t value) void setRect(rect_t value)
{ {