From c04826f9ba939e645ac541ec3cc1dab3f7bbd658 Mon Sep 17 00:00:00 2001 From: Bertrand Songis Date: Tue, 24 Nov 2020 17:48:42 +0100 Subject: [PATCH] Added Layer function (get rid of previous focus functions) --- .../thirdparty/libopenui/src/CMakeLists.txt | 1 + radio/src/thirdparty/libopenui/src/layer.cpp | 22 +++++++ radio/src/thirdparty/libopenui/src/layer.h | 65 +++++++++++++++++++ .../src/thirdparty/libopenui/src/mainwindow.h | 6 +- radio/src/thirdparty/libopenui/src/menu.cpp | 2 - .../thirdparty/libopenui/src/modal_window.cpp | 15 ++++- .../thirdparty/libopenui/src/modal_window.h | 26 +------- 7 files changed, 106 insertions(+), 31 deletions(-) create mode 100644 radio/src/thirdparty/libopenui/src/layer.cpp create mode 100644 radio/src/thirdparty/libopenui/src/layer.h diff --git a/radio/src/thirdparty/libopenui/src/CMakeLists.txt b/radio/src/thirdparty/libopenui/src/CMakeLists.txt index f6712eea4..98bcf5f78 100644 --- a/radio/src/thirdparty/libopenui/src/CMakeLists.txt +++ b/radio/src/thirdparty/libopenui/src/CMakeLists.txt @@ -15,6 +15,7 @@ set(LIBOPENUI_SRC libopenui_file.cpp bitmapbuffer.cpp window.cpp + layer.cpp form.cpp button.cpp static.cpp diff --git a/radio/src/thirdparty/libopenui/src/layer.cpp b/radio/src/thirdparty/libopenui/src/layer.cpp new file mode 100644 index 000000000..b40c72bad --- /dev/null +++ b/radio/src/thirdparty/libopenui/src/layer.cpp @@ -0,0 +1,22 @@ +/* + * Copyright (C) OpenTX + * + * Source: + * https://github.com/opentx/libopenui + * + * This file is a part of libopenui library. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + */ + +#include "layer.h" + +std::list Layer::stack; diff --git a/radio/src/thirdparty/libopenui/src/layer.h b/radio/src/thirdparty/libopenui/src/layer.h new file mode 100644 index 000000000..d0c8692c5 --- /dev/null +++ b/radio/src/thirdparty/libopenui/src/layer.h @@ -0,0 +1,65 @@ +/* + * Copyright (C) OpenTX + * + * Source: + * https://github.com/opentx/libopenui + * + * This file is a part of libopenui library. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + */ + +#pragma once + +#include "window.h" + +class Layer +{ + public: + explicit Layer(Window * main): + main(main) + { + } + + Window * main; + Window * focus = nullptr; + + static std::list stack; + + static void push(Window * window) + { + if (!stack.empty()) { + Layer &parentLayer = stack.back(); + parentLayer.focus = Window::getFocus(); + } + stack.emplace_back(Layer(window)); + } + + static void pop(Window * window) + { + if (stack.back().main == window) { + stack.pop_back(); + const auto & back = stack.back(); + if (back.focus) { + back.focus->setFocus(SET_FOCUS_DEFAULT); + } + } + else { + // TODO it would be better to do it in reverse order + for (auto it = stack.begin(); it != stack.end(); it++) { + if (it->main == window) { + stack.erase(it); + break; + } + } + } + } +}; diff --git a/radio/src/thirdparty/libopenui/src/mainwindow.h b/radio/src/thirdparty/libopenui/src/mainwindow.h index 4091beb65..375ec052e 100644 --- a/radio/src/thirdparty/libopenui/src/mainwindow.h +++ b/radio/src/thirdparty/libopenui/src/mainwindow.h @@ -20,15 +20,17 @@ #pragma once #include -#include "window.h" +#include "layer.h" #include "bitmapbuffer.h" -class MainWindow: public Window { +class MainWindow: public Window +{ public: MainWindow(): Window(nullptr, {0, 0, LCD_W, LCD_H}), invalidatedRect(rect) { + Layer::push(this); } ~MainWindow() override diff --git a/radio/src/thirdparty/libopenui/src/menu.cpp b/radio/src/thirdparty/libopenui/src/menu.cpp index 965ad4708..f09629896 100644 --- a/radio/src/thirdparty/libopenui/src/menu.cpp +++ b/radio/src/thirdparty/libopenui/src/menu.cpp @@ -55,7 +55,6 @@ void MenuBody::onEvent(event_t event) menu->invalidate(); } else { - menu->revertPreviousFocus(); lines[selectedIndex].onPress(); menu->deleteLater(); } @@ -87,7 +86,6 @@ bool MenuBody::onTouchEnd(coord_t /*x*/, coord_t y) menu->invalidate(); } else { - menu->revertPreviousFocus(); lines[index].onPress(); } } diff --git a/radio/src/thirdparty/libopenui/src/modal_window.cpp b/radio/src/thirdparty/libopenui/src/modal_window.cpp index 303071027..6d5b73d44 100644 --- a/radio/src/thirdparty/libopenui/src/modal_window.cpp +++ b/radio/src/thirdparty/libopenui/src/modal_window.cpp @@ -20,11 +20,22 @@ #include "modal_window.h" #include "font.h" +#include "layer.h" ModalWindow::ModalWindow(Window * parent): - Window(parent->getFullScreenWindow(), {0, 0, LCD_W, LCD_H}), - previousFocus(focusWindow) + Window(parent->getFullScreenWindow(), {0, 0, LCD_W, LCD_H}) { + Layer::push(this); +} + +void ModalWindow::deleteLater(bool detach) +{ + if (_deleted) + return; + + Layer::pop(this); + + Window::deleteLater(detach); } void ModalWindow::paint(BitmapBuffer * dc) diff --git a/radio/src/thirdparty/libopenui/src/modal_window.h b/radio/src/thirdparty/libopenui/src/modal_window.h index 5219818af..3fb396fb4 100644 --- a/radio/src/thirdparty/libopenui/src/modal_window.h +++ b/radio/src/thirdparty/libopenui/src/modal_window.h @@ -33,17 +33,7 @@ class ModalWindow: public Window } #endif - void deleteLater(bool detach=true) override - { - if (_deleted) - return; - - if (previousFocus && (!focusWindow || focusWindow->isChild(this))) { - previousFocus->setFocus(SET_FOCUS_DEFAULT); - } - - Window::deleteLater(detach); - } + void deleteLater(bool detach=true) override; void paint(BitmapBuffer * dc) override; @@ -73,21 +63,7 @@ class ModalWindow: public Window closeAllowed = value; } - void forgetPreviousFocus() - { - previousFocus = nullptr; - } - - void revertPreviousFocus() - { - if (previousFocus) { - previousFocus->setFocus(SET_FOCUS_DEFAULT); - previousFocus = nullptr; - } - } - protected: - Window * previousFocus; bool closeAllowed = true; };