Window: Add window "token" type, unique for each window instance

Not recycled even for windows with same class/ID
Token may outlive window, unlike pointer
This commit is contained in:
Jonathan G Rennison
2023-12-28 02:24:36 +00:00
parent 117deb0c62
commit ea1ba56ec4
4 changed files with 27 additions and 0 deletions

View File

@@ -119,6 +119,7 @@ WindowDesc::WindowDesc(const char * const file, const int line, WindowPosition d
default_height_trad(def_height_trad) default_height_trad(def_height_trad)
{ {
if (_window_descs == nullptr) _window_descs = new std::vector<WindowDesc*>(); if (_window_descs == nullptr) _window_descs = new std::vector<WindowDesc*>();
_window_descs->push_back(this); _window_descs->push_back(this);
} }
@@ -1145,6 +1146,20 @@ Window *FindWindowByClass(WindowClass cls)
return nullptr; return nullptr;
} }
/**
* Find any window by its token.
* @param token Window token
* @return Pointer to the found window, or \c nullptr if not available
*/
Window *FindWindowByToken(WindowToken token)
{
for (Window *w : Window::IterateFromBack()) {
if (w->GetWindowToken() == token) return w;
}
return nullptr;
}
/** /**
* Get the main window, i.e. FindWindowById(WC_MAIN_WINDOW, 0). * Get the main window, i.e. FindWindowById(WC_MAIN_WINDOW, 0).
* If the main window is not available, this function will trigger an assert. * If the main window is not available, this function will trigger an assert.
@@ -1891,6 +1906,9 @@ void Window::InitNested(WindowNumber window_number)
*/ */
Window::Window(WindowDesc *desc) : window_desc(desc), scale(_gui_scale), mouse_capture_widget(-1) Window::Window(WindowDesc *desc) : window_desc(desc), scale(_gui_scale), mouse_capture_widget(-1)
{ {
static uint64_t last_window_token = 0;
last_window_token++;
this->window_token = WindowToken(last_window_token);
} }
/** /**

View File

@@ -18,6 +18,7 @@
Window *FindWindowById(WindowClass cls, WindowNumber number); Window *FindWindowById(WindowClass cls, WindowNumber number);
Window *FindWindowByClass(WindowClass cls); Window *FindWindowByClass(WindowClass cls);
Window *FindWindowByToken(WindowToken token);
Window *GetMainWindow(); Window *GetMainWindow();
void ChangeWindowOwner(Owner old_owner, Owner new_owner); void ChangeWindowOwner(Owner old_owner, Owner new_owner);

View File

@@ -255,6 +255,8 @@ struct Window : ZeroedMemoryAllocator {
WindowClass window_class; ///< Window class WindowClass window_class; ///< Window class
private: private:
WindowToken window_token;
/** /**
* Helper allocation function to disallow something. * Helper allocation function to disallow something.
* Don't allow arrays; arrays of Windows are pointless as you need * Don't allow arrays; arrays of Windows are pointless as you need
@@ -334,6 +336,8 @@ public:
void ChangeWindowClass(WindowClass cls); void ChangeWindowClass(WindowClass cls);
WindowToken GetWindowToken() const { return this->window_token; }
/** /**
* Set the timeout flag of the window and initiate the timer. * Set the timeout flag of the window and initiate the timer.
*/ */

View File

@@ -10,6 +10,8 @@
#ifndef WINDOW_TYPE_H #ifndef WINDOW_TYPE_H
#define WINDOW_TYPE_H #define WINDOW_TYPE_H
#include "core/strong_typedef_type.hpp"
/** %Window numbers. */ /** %Window numbers. */
enum WindowNumberEnum { enum WindowNumberEnum {
WN_GAME_OPTIONS_AI = 0, ///< AI settings. WN_GAME_OPTIONS_AI = 0, ///< AI settings.
@@ -819,4 +821,6 @@ enum EventState {
ES_NOT_HANDLED, ///< The passed event is not handled. ES_NOT_HANDLED, ///< The passed event is not handled.
}; };
using WindowToken = StrongType::Typedef<uint64_t, struct WindowTokenTag, StrongType::Compare>;
#endif /* WINDOW_TYPE_H */ #endif /* WINDOW_TYPE_H */