Windows destruction refactoring

This commit is contained in:
Bertrand Songis
2020-11-25 23:48:45 +01:00
parent 7a9c3405aa
commit 322055b34a
9 changed files with 50 additions and 23 deletions
+14 -5
View File
@@ -22,7 +22,8 @@
#include <vector>
#include "button.h"
class CarouselItem {
class CarouselItem
{
public:
CarouselItem(Window * front, Window * back):
front(front),
@@ -43,7 +44,8 @@ class CarouselItem {
std::function<void()> selectHandler;
};
class CarouselWindow: public Window {
class CarouselWindow: public Window
{
friend class Carousel;
public:
@@ -53,9 +55,13 @@ class CarouselWindow: public Window {
{
}
~CarouselWindow() override
void deleteLater(bool detach = true, bool trash = true) override
{
if (_deleted)
return;
clear();
Window::deleteLater(detach, trash);
}
void addItem(CarouselItem * item)
@@ -66,13 +72,15 @@ class CarouselWindow: public Window {
void clear()
{
// children will be deleted later (front and back)
children.clear();
for (auto & item: items) {
item->front->deleteLater();
item->back->deleteLater();
delete item;
}
items.clear();
Window::clear();
}
void select(int index, bool scroll = true)
@@ -92,7 +100,8 @@ class CarouselWindow: public Window {
void update();
};
class Carousel: public Window {
class Carousel: public Window
{
public:
Carousel(Window * parent, const rect_t & rect):
Window(parent, rect, NO_SCROLLBAR)
+5 -3
View File
@@ -24,15 +24,17 @@
class Dialog;
class DialogWindowContent: public ModalWindowContent {
class DialogWindowContent: public ModalWindowContent
{
friend class Dialog;
public:
DialogWindowContent(Dialog * parent, const rect_t & rect);
~DialogWindowContent() override
void deleteLater(bool detach = true, bool trash = true) override
{
form.detach();
form.deleteLater(true, false);
ModalWindowContent::deleteLater(detach, trash);
}
#if defined(DEBUG_WINDOWS)
+3 -2
View File
@@ -139,9 +139,10 @@ class MenuWindowContent: public ModalWindowContent
public:
explicit MenuWindowContent(Menu * parent);
~MenuWindowContent() override
void deleteLater(bool detach = true, bool trash = true) override
{
body.detach();
body.deleteLater(true, false);
ModalWindowContent::deleteLater(detach, trash);
}
#if defined(DEBUG_WINDOWS)
+2 -2
View File
@@ -28,14 +28,14 @@ ModalWindow::ModalWindow(Window * parent):
Layer::push(this);
}
void ModalWindow::deleteLater(bool detach)
void ModalWindow::deleteLater(bool detach, bool trash)
{
if (_deleted)
return;
Layer::pop(this);
Window::deleteLater(detach);
Window::deleteLater(detach, trash);
}
void ModalWindow::paint(BitmapBuffer * dc)
+1 -1
View File
@@ -33,7 +33,7 @@ class ModalWindow: public Window
}
#endif
void deleteLater(bool detach=true) override;
void deleteLater(bool detach = true, bool trash = true) override;
void paint(BitmapBuffer * dc) override;
+3
View File
@@ -39,6 +39,9 @@ void Table::Body::checkEvents()
{
Window::checkEvents();
if (_deleted)
return;
coord_t y = 0;
for (auto line: lines) {
coord_t x = 10;
+10 -4
View File
@@ -140,7 +140,8 @@ class Table: public FormField
void paint(BitmapBuffer * dc) override;
};
class Body: public Window {
class Body: public Window
{
friend class Table;
public:
@@ -244,10 +245,15 @@ class Table: public FormField
}
#endif
~Table() override
void deleteLater(bool detach = true, bool trash = true) override
{
header.detach();
body.detach();
if (_deleted)
return;
header.deleteLater(true, false);
body.deleteLater(true, false);
FormField::deleteLater(detach, trash);
}
void setColumnsWidth(const coord_t values[])
+11 -5
View File
@@ -67,13 +67,15 @@ void Window::detach()
}
}
void Window::deleteLater(bool detach)
void Window::deleteLater(bool detach, bool trash)
{
if (_deleted)
return;
TRACE_WINDOWS("Delete %p %s", this, getWindowDebugString().c_str());
_deleted = true;
if (static_cast<Window *>(focusWindow) == static_cast<Window *>(this)) {
focusWindow = nullptr;
}
@@ -83,13 +85,15 @@ void Window::deleteLater(bool detach)
else
parent = nullptr;
deleteChildren();
if (closeHandler) {
closeHandler();
}
trash.push_back(this);
_deleted = true;
if (trash) {
Window::trash.push_back(this);
}
}
void Window::clear()
@@ -325,7 +329,9 @@ void Window::checkEvents()
{
auto copy = children;
for (auto child: copy) {
child->checkEvents();
if (!child->deleted()) {
child->checkEvents();
}
}
if (this == Window::focusWindow) {
+1 -1
View File
@@ -137,7 +137,7 @@ class Window
focusHandler = std::move(handler);
}
virtual void deleteLater(bool detach=true);
virtual void deleteLater(bool detach = true, bool trash = true);
void clear();