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:
@@ -119,6 +119,7 @@ WindowDesc::WindowDesc(const char * const file, const int line, WindowPosition d
|
||||
default_height_trad(def_height_trad)
|
||||
{
|
||||
if (_window_descs == nullptr) _window_descs = new std::vector<WindowDesc*>();
|
||||
|
||||
_window_descs->push_back(this);
|
||||
}
|
||||
|
||||
@@ -1145,6 +1146,20 @@ Window *FindWindowByClass(WindowClass cls)
|
||||
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).
|
||||
* 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)
|
||||
{
|
||||
static uint64_t last_window_token = 0;
|
||||
last_window_token++;
|
||||
this->window_token = WindowToken(last_window_token);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -18,6 +18,7 @@
|
||||
|
||||
Window *FindWindowById(WindowClass cls, WindowNumber number);
|
||||
Window *FindWindowByClass(WindowClass cls);
|
||||
Window *FindWindowByToken(WindowToken token);
|
||||
Window *GetMainWindow();
|
||||
void ChangeWindowOwner(Owner old_owner, Owner new_owner);
|
||||
|
||||
|
@@ -255,6 +255,8 @@ struct Window : ZeroedMemoryAllocator {
|
||||
WindowClass window_class; ///< Window class
|
||||
|
||||
private:
|
||||
WindowToken window_token;
|
||||
|
||||
/**
|
||||
* Helper allocation function to disallow something.
|
||||
* Don't allow arrays; arrays of Windows are pointless as you need
|
||||
@@ -334,6 +336,8 @@ public:
|
||||
|
||||
void ChangeWindowClass(WindowClass cls);
|
||||
|
||||
WindowToken GetWindowToken() const { return this->window_token; }
|
||||
|
||||
/**
|
||||
* Set the timeout flag of the window and initiate the timer.
|
||||
*/
|
||||
|
@@ -10,6 +10,8 @@
|
||||
#ifndef WINDOW_TYPE_H
|
||||
#define WINDOW_TYPE_H
|
||||
|
||||
#include "core/strong_typedef_type.hpp"
|
||||
|
||||
/** %Window numbers. */
|
||||
enum WindowNumberEnum {
|
||||
WN_GAME_OPTIONS_AI = 0, ///< AI settings.
|
||||
@@ -819,4 +821,6 @@ enum EventState {
|
||||
ES_NOT_HANDLED, ///< The passed event is not handled.
|
||||
};
|
||||
|
||||
using WindowToken = StrongType::Typedef<uint64_t, struct WindowTokenTag, StrongType::Compare>;
|
||||
|
||||
#endif /* WINDOW_TYPE_H */
|
||||
|
Reference in New Issue
Block a user