Move ChoiceEx to libopenui + fix keyboard crash (#19)
* moved choiceex.* to libopenui where it belongs * added choiceex to build * enhanced system * removed construtor parameter for getTicks. Turns out you can just access the timer directly in libopenui * fixed keyboard crash for widget settings * code cleanup Co-authored-by: kkoenig1@optonline.com <kkoenig1@optonline.com>
This commit is contained in:
committed by
GitHub
co-authored by
kkoenig1@optonline.com <kkoenig1@optonline.com>
parent
e9451cd042
commit
89978ff17c
@@ -26,6 +26,7 @@ set(LIBOPENUI_SRC
|
|||||||
menu.cpp
|
menu.cpp
|
||||||
menutoolbar.cpp
|
menutoolbar.cpp
|
||||||
choice.cpp
|
choice.cpp
|
||||||
|
choiceex.cpp
|
||||||
filechoice.cpp
|
filechoice.cpp
|
||||||
numberedit.cpp
|
numberedit.cpp
|
||||||
textedit.cpp
|
textedit.cpp
|
||||||
|
|||||||
@@ -162,6 +162,11 @@ void Choice::openMenu()
|
|||||||
menu->select(current);
|
menu->select(current);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (beforeDisplayMenuHandler) {
|
||||||
|
beforeDisplayMenuHandler(menu);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
menu->setCloseHandler([=]() {
|
menu->setCloseHandler([=]() {
|
||||||
setEditMode(false);
|
setEditMode(false);
|
||||||
});
|
});
|
||||||
|
|||||||
+8
@@ -22,6 +22,8 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include "form.h"
|
#include "form.h"
|
||||||
|
|
||||||
|
class Menu;
|
||||||
|
|
||||||
enum ChoiceType {
|
enum ChoiceType {
|
||||||
CHOICE_TYPE_DROPOWN,
|
CHOICE_TYPE_DROPOWN,
|
||||||
CHOICE_TYPE_FOLDER,
|
CHOICE_TYPE_FOLDER,
|
||||||
@@ -77,6 +79,11 @@ class Choice: public ChoiceBase {
|
|||||||
bool onTouchEnd(coord_t x, coord_t y) override;
|
bool onTouchEnd(coord_t x, coord_t y) override;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
void setBeforeDisplayMenuHandler(std::function<void(Menu*)> handler)
|
||||||
|
{
|
||||||
|
beforeDisplayMenuHandler = std::move(handler);
|
||||||
|
}
|
||||||
|
|
||||||
void setSetValueHandler(std::function<void(int)> handler)
|
void setSetValueHandler(std::function<void(int)> handler)
|
||||||
{
|
{
|
||||||
setValue = std::move(handler);
|
setValue = std::move(handler);
|
||||||
@@ -155,6 +162,7 @@ class Choice: public ChoiceBase {
|
|||||||
std::function<void(int)> setValue;
|
std::function<void(int)> setValue;
|
||||||
std::function<bool(int)> isValueAvailable;
|
std::function<bool(int)> isValueAvailable;
|
||||||
std::function<std::string(int)> textHandler;
|
std::function<std::string(int)> textHandler;
|
||||||
|
std::function <void(Menu *)> beforeDisplayMenuHandler;
|
||||||
|
|
||||||
virtual void openMenu();
|
virtual void openMenu();
|
||||||
};
|
};
|
||||||
|
|||||||
+111
@@ -0,0 +1,111 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) EdgeTX
|
||||||
|
*
|
||||||
|
* Based on code named
|
||||||
|
* opentx - https://github.com/opentx/opentx
|
||||||
|
* th9x - http://code.google.com/p/th9x
|
||||||
|
* er9x - http://code.google.com/p/er9x
|
||||||
|
* gruvin9x - http://code.google.com/p/gruvin9x
|
||||||
|
*
|
||||||
|
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License version 2 as
|
||||||
|
* published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*/
|
||||||
|
#include "choiceex.h"
|
||||||
|
|
||||||
|
constexpr int LONG_PRESS_10MS = 40;
|
||||||
|
|
||||||
|
extern inline tmr10ms_t getTicks()
|
||||||
|
{
|
||||||
|
return g_tmr10ms;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void ChoiceEx::setLongPressHandler(std::function<void(event_t)> handler)
|
||||||
|
{
|
||||||
|
longPressHandler = handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
ChoiceEx::ChoiceEx(FormGroup * parent, const rect_t & rect, int16_t vmin, int16_t vmax, std::function<int16_t()> getValue,
|
||||||
|
std::function<void(int16_t)> setValue, WindowFlags windowFlags) :
|
||||||
|
Choice(parent, rect, vmin, vmax, getValue, setValue, windowFlags)
|
||||||
|
{
|
||||||
|
#if defined(HARDWARE_TOUCH)
|
||||||
|
duration10ms = 0;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(HARDWARE_KEYS)
|
||||||
|
void ChoiceEx::onEvent(event_t event)
|
||||||
|
{
|
||||||
|
if (event == EVT_KEY_LONG(KEY_ENTER)) {
|
||||||
|
if (longPressHandler) {
|
||||||
|
killEvents(event);
|
||||||
|
longPressHandler(event);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Choice::onEvent(event);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(HARDWARE_TOUCH)
|
||||||
|
bool ChoiceEx::isLongPress()
|
||||||
|
{
|
||||||
|
unsigned int curTimer = getTicks();
|
||||||
|
return (!longPressed && duration10ms != 0 && curTimer - duration10ms > LONG_PRESS_10MS);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ChoiceEx::checkEvents(void)
|
||||||
|
{
|
||||||
|
event_t event = getEvent();
|
||||||
|
|
||||||
|
if (isLongPress()) {
|
||||||
|
if (!longPressed && longPressHandler) {
|
||||||
|
longPressHandler(event);
|
||||||
|
killEvents(event);
|
||||||
|
duration10ms = 0;
|
||||||
|
longPressed = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hasFocus())
|
||||||
|
onEvent(event);
|
||||||
|
else
|
||||||
|
pushEvent(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ChoiceEx::onTouchStart(coord_t x, coord_t y)
|
||||||
|
{
|
||||||
|
if (!longPressed && duration10ms == 0) {
|
||||||
|
duration10ms = getTicks();
|
||||||
|
}
|
||||||
|
|
||||||
|
return Choice::onTouchStart(x, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool ChoiceEx::onTouchEnd(coord_t x, coord_t y)
|
||||||
|
{
|
||||||
|
if (longPressed) {
|
||||||
|
longPressed = false;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
duration10ms = 0;
|
||||||
|
return Choice::onTouchEnd(x,y);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
+60
@@ -0,0 +1,60 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) EdgeTX
|
||||||
|
*
|
||||||
|
* Based on code named
|
||||||
|
* opentx - https://github.com/opentx/opentx
|
||||||
|
* th9x - http://code.google.com/p/th9x
|
||||||
|
* er9x - http://code.google.com/p/er9x
|
||||||
|
* gruvin9x - http://code.google.com/p/gruvin9x
|
||||||
|
*
|
||||||
|
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License version 2 as
|
||||||
|
* published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*/
|
||||||
|
#ifndef _CHOICEEX_H_
|
||||||
|
#define _CHOICEEX_H_
|
||||||
|
#include "choice.h"
|
||||||
|
|
||||||
|
class ChoiceEx : public Choice
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ChoiceEx(FormGroup * parent, const rect_t &rect, int16_t vmin, int16_t vmax, std::function<int16_t()> getValue,
|
||||||
|
std::function<void(int16_t)> setValue = nullptr, WindowFlags windowFlags = 0);
|
||||||
|
|
||||||
|
void setLongPressHandler(std::function<void(event_t)> handler);
|
||||||
|
|
||||||
|
#if defined(HARDWARE_KEYS)
|
||||||
|
void onEvent(event_t event) override;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(HARDWARE_TOUCH)
|
||||||
|
bool onTouchEnd(coord_t x, coord_t y) override;
|
||||||
|
bool onTouchStart(coord_t x, coord_t y) override;
|
||||||
|
void checkEvents(void) override;
|
||||||
|
bool isLongPress();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(DEBUG_WINDOWS)
|
||||||
|
std::string getName() const override
|
||||||
|
{
|
||||||
|
return "ChoceEx";
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
protected:
|
||||||
|
std::function<void(event_t)> longPressHandler = nullptr;
|
||||||
|
|
||||||
|
#if defined(HARDWARE_TOUCH)
|
||||||
|
uint32_t duration10ms;
|
||||||
|
bool longPressed = false;
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
+9
-3
@@ -35,14 +35,17 @@ coord_t calcScrollOffsetForField(FormField *newField, Window *topWindow)
|
|||||||
return offsetY - topWindow->height() / 2;
|
return offsetY - topWindow->height() / 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Keyboard::attachKeyboard()
|
bool Keyboard::attachKeyboard()
|
||||||
{
|
{
|
||||||
|
TRACE("ATTACH KEYBOARD");
|
||||||
if (activeKeyboard) {
|
if (activeKeyboard) {
|
||||||
if (activeKeyboard == this) return;
|
if (activeKeyboard == this) return false;
|
||||||
activeKeyboard->clearField();
|
activeKeyboard->clearField();
|
||||||
}
|
}
|
||||||
|
|
||||||
activeKeyboard = this;
|
activeKeyboard = this;
|
||||||
attach(MainWindow::instance());
|
attach(MainWindow::instance());
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -76,7 +79,10 @@ FormWindow *Keyboard::findFormWindow(Window *parent)
|
|||||||
|
|
||||||
void Keyboard::setField(FormField* newField)
|
void Keyboard::setField(FormField* newField)
|
||||||
{
|
{
|
||||||
attachKeyboard();
|
TRACE("SET FIELD");
|
||||||
|
|
||||||
|
if (!attachKeyboard())
|
||||||
|
return;
|
||||||
|
|
||||||
fieldContainer = getFieldContainer(newField);
|
fieldContainer = getFieldContainer(newField);
|
||||||
if (fieldContainer) {
|
if (fieldContainer) {
|
||||||
|
|||||||
+2
-1
@@ -31,6 +31,7 @@ class Keyboard: public FormWindow
|
|||||||
|
|
||||||
void clearField()
|
void clearField()
|
||||||
{
|
{
|
||||||
|
TRACE("CLEAR FIELD");
|
||||||
detach();
|
detach();
|
||||||
if (fields) {
|
if (fields) {
|
||||||
fields->setHeight(oldHeight);
|
fields->setHeight(oldHeight);
|
||||||
@@ -64,7 +65,7 @@ class Keyboard: public FormWindow
|
|||||||
|
|
||||||
void setField(FormField *newField);
|
void setField(FormField *newField);
|
||||||
Window *getFieldContainer(FormField * field);
|
Window *getFieldContainer(FormField * field);
|
||||||
void attachKeyboard();
|
bool attachKeyboard();
|
||||||
FormWindow *findFormWindow(Window *parent);
|
FormWindow *findFormWindow(Window *parent);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user