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
#include <utility>
#include "form.h"
class CheckBox : public FormField {
public:
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),
_getValue(getValue),
_setValue(setValue)
_getValue(std::move(getValue)),
_setValue(std::move(setValue))
{
}
@@ -62,9 +63,13 @@ class CheckBox : public FormField {
return _getValue();
}
void setSetValueHandler(std::function<void(uint8_t)> handler)
{
_setValue = std::move(handler);
}
protected:
std::string label;
std::function<uint8_t()> _getValue;
std::function<void(uint8_t)> _setValue;
};
+5 -5
View File
@@ -28,12 +28,12 @@ void ExpansionPanel::updateHeight(bool move)
setHeight(newHeight);
}
void ExpansionPanel::setFocus(uint8_t flag)
void ExpansionPanel::setFocus(uint8_t flag, Window * from)
{
if (!enabled || isOpen)
FormGroup::setFocus(flag);
FormGroup::setFocus(flag, from);
else
header->setFocus(flag);
header->setFocus(flag, from);
}
ExpansionPanelHeader::ExpansionPanelHeader(ExpansionPanel * parent):
@@ -51,12 +51,12 @@ void ExpansionPanelHeader::onEvent(event_t event)
else if (event == EVT_ROTARY_RIGHT && !panel->isOpen) {
auto next = panel->getNextField();
if (next)
next->setFocus(SET_FOCUS_FORWARD);
next->setFocus(SET_FOCUS_FORWARD, this);
}
else if (event == EVT_ROTARY_LEFT) {
auto previous = panel->getPreviousField();
if (previous) {
previous->setFocus(SET_FOCUS_BACKWARD);
previous->setFocus(SET_FOCUS_BACKWARD, this);
}
}
else {
+1 -1
View File
@@ -89,7 +89,7 @@ class ExpansionPanel: public FormGroup
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;
+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 (next) {
onKeyPress();
next->setFocus(SET_FOCUS_FORWARD);
next->setFocus(SET_FOCUS_FORWARD, this);
}
}
else if (event == EVT_ROTARY_LEFT/*EVT_KEY_BREAK(KEY_UP)*/) {
if (previous) {
onKeyPress();
previous->setFocus(SET_FOCUS_BACKWARD);
previous->setFocus(SET_FOCUS_BACKWARD, this);
}
}
else if (event == EVT_KEY_BREAK(KEY_ENTER)) {
@@ -65,22 +65,20 @@ void FormField::onEvent(event_t event)
}
#endif
void FormField::setFocus(uint8_t flag)
void FormField::setFocus(uint8_t flag, Window * from)
{
if (enabled) {
Window::setFocus(flag);
Window::setFocus(flag, from);
}
else {
clearFocus();
focusWindow = this;
if (flag == SET_FOCUS_BACKWARD) {
if (previous) {
previous->setFocus(flag);
previous->setFocus(flag, this);
}
}
else {
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);
if (windowFlags & FORM_FORWARD_FOCUS) {
switch (flag) {
case SET_FOCUS_BACKWARD:
if (focusWindow->isChild(first)) {
if (from && from->isChild(first)) {
if (previous == this) {
last->setFocus(SET_FOCUS_BACKWARD);
last->setFocus(SET_FOCUS_BACKWARD, this);
}
else if (previous) {
previous->setFocus(SET_FOCUS_BACKWARD);
previous->setFocus(SET_FOCUS_BACKWARD, this);
}
}
else {
if (last) {
last->setFocus(SET_FOCUS_BACKWARD);
last->setFocus(SET_FOCUS_BACKWARD, this);
}
else if (previous) {
clearFocus();
focusWindow = this;
previous->setFocus(SET_FOCUS_BACKWARD);
previous->setFocus(SET_FOCUS_BACKWARD, this);
}
}
break;
@@ -178,31 +174,33 @@ void FormGroup::setFocus(uint8_t flag)
// no break;
case SET_FOCUS_FORWARD:
if (focusWindow && focusWindow->isChild(this)) {
if (from && from->isChild(this)) {
if (next == this) {
first->setFocus(SET_FOCUS_FORWARD);
first->setFocus(SET_FOCUS_FORWARD, this);
}
else if (next) {
next->setFocus(SET_FOCUS_FORWARD);
next->setFocus(SET_FOCUS_FORWARD, this);
}
}
else {
if (first) {
first->setFocus(SET_FOCUS_FORWARD);
first->setFocus(SET_FOCUS_FORWARD, this);
}
else if (next) {
clearFocus();
focusWindow = this;
next->setFocus(SET_FOCUS_FORWARD);
next->setFocus(SET_FOCUS_FORWARD, this);
}
}
break;
default:
if (focusWindow == previous) {
if (from == previous) {
if (first) {
first->setFocus(SET_FOCUS_DEFAULT);
}
else {
clearFocus();
focusWindow = this;
}
}
else if (next) {
next->setFocus(SET_FOCUS_FORWARD);
@@ -215,7 +213,7 @@ void FormGroup::setFocus(uint8_t flag)
}
}
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);
}
void setFocus(uint8_t flag) override;
void setFocus(uint8_t flag = SET_FOCUS_DEFAULT, Window * from = nullptr) 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)
@@ -155,7 +155,7 @@ class FormGroup: public FormField
return last;
}
void setFocusOnFirstVisibleField(uint8_t flag) const
void setFocusOnFirstVisibleField(uint8_t flag = SET_FOCUS_DEFAULT) const
{
auto field = getFirstField();
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();
while (field && !field->isInsideParentScrollingArea()) {
+4 -2
View File
@@ -117,8 +117,10 @@ void Table::Body::onEvent(event_t event)
else {
auto next = table->getNextField();
if (next) {
select(-1, false);
next->setFocus(SET_FOCUS_FORWARD);
next->setFocus(SET_FOCUS_FORWARD, this);
if (!hasFocus()) {
select(-1, false);
}
}
}
}
+18 -7
View File
@@ -239,9 +239,6 @@ class Table: public FormField
header(this, {0, 0, width(), 0}, columnsCount),
body(this, {0, 0, width(), height()}, windowFlags)
{
if (hasFocus()) {
setFocus(SET_FOCUS_DEFAULT);
}
}
#if defined(DEBUG_WINDOWS)
@@ -292,11 +289,25 @@ class Table: public FormField
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.selection < 0 && !body.lines.empty()) {
select(flag == SET_FOCUS_BACKWARD ? body.lines.size() - 1 : 0);
if (body.lines.empty()) {
if (flag == SET_FOCUS_BACKWARD) {
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())
return;
@@ -139,6 +139,7 @@ void Window::setFocus(uint8_t flag)
parent->scrollTo(this);
invalidate();
}
clearFocus();
focusWindow = this;
if (focusHandler) {
+1 -1
View File
@@ -164,7 +164,7 @@ class Window
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)
{