keyboard changes

make window honor no focus.
This commit is contained in:
kkoenig1@optonline.com
2022-06-11 15:13:11 +02:00
committed by Raphael Coeffic
parent 551d7ae766
commit 0284a520fa
10 changed files with 122 additions and 105 deletions
+44 -4
View File
@@ -20,6 +20,7 @@
#pragma once #pragma once
#include "form.h" #include "form.h"
#include "bitmapbuffer.h"
class BaseNumberEdit: public FormField class BaseNumberEdit: public FormField
{ {
@@ -76,11 +77,40 @@ class BaseNumberEdit: public FormField
void setValue(int value) void setValue(int value)
{ {
currentValue = limit(vmin, value, vmax); auto newValue = limit(vmin, value, vmax);
if (instantChange) { if (newValue != currentValue) {
_setValue(currentValue); currentValue = newValue;
if (instantChange) {
_setValue(currentValue);
}
} }
invalidate(); if (lvobj != nullptr) {
if (displayFunction != nullptr) {
lv_textarea_set_text(lvobj, displayFunction(currentValue).c_str());
} else {
char s[50];
BitmapBuffer::formatNumberAsString(s, 49, currentValue, textFlags, 0, prefix.c_str(), suffix.c_str());
lv_textarea_set_text(lvobj, s);
}
}
}
void setPrefix(std::string value)
{
prefix = std::move(value);
setValue(currentValue);
}
void setSuffix(std::string value)
{
suffix = std::move(value);
setValue(currentValue);
}
void setZeroText(std::string value)
{
zeroText = std::move(value);
setValue(currentValue);
} }
void enableInstantChange(bool value) void enableInstantChange(bool value)
@@ -117,6 +147,12 @@ class BaseNumberEdit: public FormField
} }
} }
void setDisplayHandler(std::function<std::string(int value)> function)
{
displayFunction = std::move(function);
setValue(currentValue);
}
protected: protected:
int vdefault = 0; int vdefault = 0;
int vmin; int vmin;
@@ -124,6 +160,10 @@ class BaseNumberEdit: public FormField
int step = 1; int step = 1;
int currentValue; int currentValue;
bool instantChange = true; bool instantChange = true;
std::string prefix;
std::string suffix;
std::string zeroText;
std::function<int()> _getValue; std::function<int()> _getValue;
std::function<void(int)> _setValue; std::function<void(int)> _setValue;
std::function<std::string(int)> displayFunction;
}; };
+7 -12
View File
@@ -25,19 +25,14 @@
void Button::onPress() void Button::onPress()
{ {
bool check = (pressHandler && pressHandler()); bool check = (pressHandler && pressHandler());
if (check != bool(windowFlags & BUTTON_CHECKED)) { windowFlags = check ? windowFlags | BUTTON_CHECKED : windowFlags & ~BUTTON_CHECKED;
windowFlags ^= BUTTON_CHECKED; if(lvobj == nullptr)
return;
if(lvobj == nullptr) if (! (windowFlags & BUTTON_CHECKED))
return; lv_obj_clear_state(lvobj, LV_STATE_CHECKED);
else
if (! (windowFlags & BUTTON_CHECKED)) lv_obj_add_state(lvobj, LV_STATE_CHECKED);
lv_obj_clear_state(lvobj, LV_STATE_CHECKED);
else
lv_obj_add_state(lvobj, LV_STATE_CHECKED);
// invalidate();
}
} }
#if defined(HARDWARE_KEYS) #if defined(HARDWARE_KEYS)
+10 -12
View File
@@ -17,9 +17,9 @@
* Lesser General Public License for more details. * Lesser General Public License for more details.
*/ */
#include "keyboard_base.h" #include "keyboard_base.h"
#include "mainwindow.h"
Keyboard * Keyboard::activeKeyboard = nullptr; Keyboard * Keyboard::activeKeyboard = nullptr;
lv_obj_t *Keyboard::keyboard = nullptr;
static void keyboard_event_cb(lv_event_t * e) static void keyboard_event_cb(lv_event_t * e)
{ {
@@ -33,20 +33,18 @@ static void keyboard_event_cb(lv_event_t * e)
Keyboard::Keyboard(coord_t height) : Keyboard::Keyboard(coord_t height) :
FormWindow(nullptr, {0, LCD_H - height, LCD_W, height}, OPAQUE) FormWindow(MainWindow::instance(), {0, LCD_H - height, LCD_W, height}, OPAQUE)
{ {
// set the background of the window and opacity to 100% // set the background of the window and opacity to 100%
lv_obj_set_style_bg_color(lvobj, makeLvColor(COLOR_THEME_SECONDARY1), LV_PART_MAIN); lv_obj_set_parent(lvobj, lv_layer_top()); // the keyboard is always on top
lv_obj_set_style_bg_color(lvobj, lv_color_make(0xE0, 0xE0, 0xE0), LV_PART_MAIN);
lv_obj_set_style_bg_opa(lvobj, LV_OPA_100, LV_PART_MAIN); lv_obj_set_style_bg_opa(lvobj, LV_OPA_100, LV_PART_MAIN);
if (keyboard == nullptr) { keyboard = lv_keyboard_create(this->getLvObj());
keyboard = lv_keyboard_create(lv_scr_act()); lv_obj_add_event_cb(keyboard, keyboard_event_cb, LV_EVENT_ALL, this);
lv_obj_add_event_cb(keyboard, keyboard_event_cb, LV_EVENT_ALL, this); lv_obj_set_pos(keyboard, 0, 0);
lv_obj_set_size(keyboard, LCD_W, height);
lv_obj_clear_flag(keyboard, LV_OBJ_FLAG_HIDDEN);
lv_obj_set_size(keyboard, LCD_W, LCD_H * 2 / 5);
lv_obj_clear_flag(keyboard, LV_OBJ_FLAG_HIDDEN);
}
} }
void Keyboard::clearField() void Keyboard::clearField()
@@ -76,6 +74,7 @@ void Keyboard::hide()
{ {
if (activeKeyboard) { if (activeKeyboard) {
activeKeyboard->clearField(); activeKeyboard->clearField();
lv_obj_add_flag(activeKeyboard->lvobj, LV_OBJ_FLAG_HIDDEN);
activeKeyboard = nullptr; activeKeyboard = nullptr;
} }
} }
@@ -106,7 +105,6 @@ bool Keyboard::attachKeyboard()
} }
activeKeyboard = this; activeKeyboard = this;
attach(MainWindow::instance());
return true; return true;
} }
+1 -2
View File
@@ -19,7 +19,6 @@
#pragma once #pragma once
#include "mainwindow.h"
#include "form.h" #include "form.h"
class Keyboard: public FormWindow class Keyboard: public FormWindow
@@ -31,7 +30,7 @@ class Keyboard: public FormWindow
protected: protected:
static Keyboard * activeKeyboard; static Keyboard * activeKeyboard;
static lv_obj_t * keyboard; lv_obj_t * keyboard = nullptr;
FormField *field = nullptr; FormField *field = nullptr;
Window *fieldContainer = nullptr; Window *fieldContainer = nullptr;
+42 -40
View File
@@ -27,53 +27,55 @@ NumberKeyboard * NumberKeyboard::_instance = nullptr;
NumberKeyboard::NumberKeyboard() : NumberKeyboard::NumberKeyboard() :
Keyboard(KEYBOARD_HEIGHT) Keyboard(KEYBOARD_HEIGHT)
{ {
// new TextButton(this, {LCD_W / 2 - 115, 10, 50, 30}, "<<", lv_obj_add_flag(keyboard, LV_OBJ_FLAG_HIDDEN); // ToDo: for now we are still using the old keyboard
// [=]() -> uint8_t {
// pushEvent(EVT_VIRTUAL_KEY_BACKWARD);
// return 0;
// }, BUTTON_BACKGROUND | OPAQUE | NO_FOCUS);
// new TextButton(this, {LCD_W / 2 - 55, 10, 50, 30}, "-", new TextButton(this, {LCD_W / 2 - 115, 10, 50, 30}, "<<",
// [=]() -> uint8_t { [=]() -> uint8_t {
// pushEvent(EVT_VIRTUAL_KEY_MINUS); pushEvent(EVT_VIRTUAL_KEY_BACKWARD);
// return 0; return 0;
// }, BUTTON_BACKGROUND | OPAQUE | NO_FOCUS); }, BUTTON_BACKGROUND | OPAQUE | NO_FOCUS);
// new TextButton(this, {LCD_W / 2 + 5, 10, 50, 30}, "+", new TextButton(this, {LCD_W / 2 - 55, 10, 50, 30}, "-",
// [=]() -> uint8_t { [=]() -> uint8_t {
// pushEvent(EVT_VIRTUAL_KEY_PLUS); pushEvent(EVT_VIRTUAL_KEY_MINUS);
// return 0; return 0;
// }, BUTTON_BACKGROUND | OPAQUE | NO_FOCUS); }, BUTTON_BACKGROUND | OPAQUE | NO_FOCUS);
// new TextButton(this, {LCD_W / 2 + 65, 10, 50, 30}, ">>", new TextButton(this, {LCD_W / 2 + 5, 10, 50, 30}, "+",
// [=]() -> uint8_t { [=]() -> uint8_t {
// pushEvent(EVT_VIRTUAL_KEY_FORWARD); pushEvent(EVT_VIRTUAL_KEY_PLUS);
// return 0; return 0;
// }, BUTTON_BACKGROUND | OPAQUE | NO_FOCUS); }, BUTTON_BACKGROUND | OPAQUE | NO_FOCUS);
// new TextButton(this, {LCD_W / 2 - 115, 50, 50, 30}, "MIN", new TextButton(this, {LCD_W / 2 + 65, 10, 50, 30}, ">>",
// [=]() -> uint8_t { [=]() -> uint8_t {
// pushEvent(EVT_VIRTUAL_KEY_MIN); pushEvent(EVT_VIRTUAL_KEY_FORWARD);
// return 0; return 0;
// }, BUTTON_BACKGROUND | OPAQUE | NO_FOCUS); }, BUTTON_BACKGROUND | OPAQUE | NO_FOCUS);
// new TextButton(this, {LCD_W / 2 + 65, 50, 50, 30}, "MAX", new TextButton(this, {LCD_W / 2 - 115, 50, 50, 30}, "MIN",
// [=]() -> uint8_t { [=]() -> uint8_t {
// pushEvent(EVT_VIRTUAL_KEY_MAX); pushEvent(EVT_VIRTUAL_KEY_MIN);
// return 0; return 0;
// }, BUTTON_BACKGROUND | OPAQUE | NO_FOCUS); }, BUTTON_BACKGROUND | OPAQUE | NO_FOCUS);
// new TextButton(this, { LCD_W/2 - 55, 50, 50, 30 }, "DEF", new TextButton(this, {LCD_W / 2 + 65, 50, 50, 30}, "MAX",
// [=]() -> uint8_t { [=]() -> uint8_t {
// pushEvent(EVT_VIRTUAL_KEY_DEFAULT); pushEvent(EVT_VIRTUAL_KEY_MAX);
// return 0; return 0;
// }, BUTTON_BACKGROUND | OPAQUE | NO_FOCUS); }, BUTTON_BACKGROUND | OPAQUE | NO_FOCUS);
new TextButton(this, { LCD_W/2 - 55, 50, 50, 30 }, "DEF",
[=]() -> uint8_t {
pushEvent(EVT_VIRTUAL_KEY_DEFAULT);
return 0;
}, BUTTON_BACKGROUND | OPAQUE | NO_FOCUS);
// new TextButton(this, { LCD_W/2 + 5, 50, 50, 30 }, "+/-", new TextButton(this, { LCD_W/2 + 5, 50, 50, 30 }, "+/-",
// [=]() -> uint8_t { [=]() -> uint8_t {
// pushEvent(EVT_VIRTUAL_KEY_SIGN); pushEvent(EVT_VIRTUAL_KEY_SIGN);
// return 0; return 0;
// }, BUTTON_BACKGROUND | OPAQUE | NO_FOCUS); }, BUTTON_BACKGROUND | OPAQUE | NO_FOCUS);
} }
NumberKeyboard::~NumberKeyboard() NumberKeyboard::~NumberKeyboard()
+9 -3
View File
@@ -38,10 +38,16 @@ class NumberKeyboard : public Keyboard {
{ {
if (!_instance) if (!_instance)
_instance = new NumberKeyboard(); _instance = new NumberKeyboard();
else // else
lv_obj_clear_flag(_instance->keyboard, LV_OBJ_FLAG_HIDDEN); // lv_obj_clear_flag(_instance->keyboard, LV_OBJ_FLAG_HIDDEN);
// lv_keyboard_set_mode(keyboard, LV_KEYBOARD_MODE_NUMBER);
lv_obj_clear_flag(_instance->lvobj, LV_OBJ_FLAG_HIDDEN);
// lv_obj_clear_flag(_instance->keyboard, LV_OBJ_FLAG_HIDDEN);
lv_keyboard_set_mode(_instance->keyboard, LV_KEYBOARD_MODE_NUMBER);
lv_obj_move_foreground(_instance->getLvObj());
lv_keyboard_set_mode(keyboard, LV_KEYBOARD_MODE_NUMBER);
_instance->setField(field); _instance->setField(field);
} }
+3 -3
View File
@@ -53,10 +53,10 @@ class TextKeyboard : public Keyboard {
{ {
if (!_instance) if (!_instance)
_instance = new TextKeyboard(); _instance = new TextKeyboard();
else
lv_obj_clear_flag(_instance->keyboard, LV_OBJ_FLAG_HIDDEN);
lv_keyboard_set_mode(keyboard, LV_KEYBOARD_MODE_TEXT_LOWER); lv_obj_clear_flag(_instance->lvobj, LV_OBJ_FLAG_HIDDEN);
lv_obj_clear_flag(_instance->keyboard, LV_OBJ_FLAG_HIDDEN);
lv_keyboard_set_mode(_instance->keyboard, LV_KEYBOARD_MODE_TEXT_LOWER);
_instance->setField(field); _instance->setField(field);
} }
+1 -3
View File
@@ -35,11 +35,9 @@ NumberEdit::NumberEdit(Window * parent, const rect_t & rect, int vmin, int vmax,
lv_obj_set_scrollbar_mode(lvobj, LV_SCROLLBAR_MODE_OFF); lv_obj_set_scrollbar_mode(lvobj, LV_SCROLLBAR_MODE_OFF);
lv_textarea_set_password_mode(lvobj, false); lv_textarea_set_password_mode(lvobj, false);
lv_textarea_set_one_line(lvobj, true); lv_textarea_set_one_line(lvobj, true);
lv_textarea_set_accepted_chars(lvobj, "0123456789");
auto value = _getValue(); auto value = _getValue();
auto strValue = std::to_string(value); this->setValue(value);
lv_textarea_set_text(lvobj, strValue.c_str());
// LV_PART_MAIN // LV_PART_MAIN
lv_style_init(&style_main); lv_style_init(&style_main);
-24
View File
@@ -39,26 +39,6 @@ class NumberEdit : public BaseNumberEdit
isValueAvailable = std::move(handler); isValueAvailable = std::move(handler);
} }
void setPrefix(std::string value)
{
prefix = std::move(value);
}
void setSuffix(std::string value)
{
suffix = std::move(value);
}
void setZeroText(std::string value)
{
zeroText = std::move(value);
}
void setDisplayHandler(std::function<void(BitmapBuffer *, LcdFlags, int)> function)
{
displayFunction = std::move(function);
}
void onEvent(event_t event) override; void onEvent(event_t event) override;
void onFocusLost() override; void onFocusLost() override;
@@ -67,10 +47,6 @@ class NumberEdit : public BaseNumberEdit
#endif #endif
protected: protected:
std::function<void(BitmapBuffer *, LcdFlags, int)> displayFunction;
std::string prefix;
std::string suffix;
std::string zeroText;
std::function<bool(int)> isValueAvailable; std::function<bool(int)> isValueAvailable;
}; };
+5 -2
View File
@@ -147,6 +147,11 @@ Window::Window(Window *parent, const rect_t &rect, WindowFlags windowFlags,
invalidate(); invalidate();
} }
} }
// honor the no focus flag of libopenui
if (this->windowFlags & NO_FOCUS) {
lv_obj_clear_flag(lvobj, LV_OBJ_FLAG_CLICK_FOCUSABLE);
}
} }
extern lv_obj_t * canvas; extern lv_obj_t * canvas;
Window::~Window() Window::~Window()
@@ -181,8 +186,6 @@ void Window::detach()
if (parent) { if (parent) {
parent->removeChild(this); parent->removeChild(this);
parent = nullptr; parent = nullptr;
if(lvobj != nullptr)
lv_obj_set_parent(lvobj, nullptr);
} }
} }