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