feat(automation): wxWindow automation-id registry

This commit is contained in:
SoftFever
2026-06-03 02:43:41 +08:00
parent 487e1cb205
commit 39a29cf865
3 changed files with 71 additions and 0 deletions

View File

@@ -239,6 +239,8 @@ set(SLIC3R_GUI_SOURCES
GUI/Automation/JsonRpcDispatcher.hpp
GUI/Automation/AutomationServer.cpp
GUI/Automation/AutomationServer.hpp
GUI/Automation/AutomationRegistry.cpp
GUI/Automation/AutomationRegistry.hpp
GUI/I18N.cpp
GUI/I18N.hpp
GUI/DragDropPanel.cpp

View File

@@ -0,0 +1,50 @@
#include "AutomationRegistry.hpp"
#include <wx/window.h>
#include <wx/event.h>
#include <mutex>
#include <unordered_map>
namespace Slic3r { namespace GUI { namespace Automation {
namespace {
std::mutex& mtx() { static std::mutex m; return m; }
std::unordered_map<const wxWindow*, std::string>& fwd() {
static std::unordered_map<const wxWindow*, std::string> m; return m;
}
std::unordered_map<std::string, wxWindow*>& rev() {
static std::unordered_map<std::string, wxWindow*> m; return m;
}
void erase_window(const wxWindow* w) {
std::lock_guard<std::mutex> lk(mtx());
auto it = fwd().find(w);
if (it != fwd().end()) { rev().erase(it->second); fwd().erase(it); }
}
} // namespace
void set_automation_id(wxWindow* window, const std::string& id) {
if (window == nullptr || id.empty()) return;
{
std::lock_guard<std::mutex> lk(mtx());
fwd()[window] = id;
rev()[id] = window;
}
// Prune on destruction.
window->Bind(wxEVT_DESTROY, [window](wxWindowDestroyEvent& e) {
erase_window(window);
e.Skip();
});
}
std::string automation_id_of(const wxWindow* window) {
std::lock_guard<std::mutex> lk(mtx());
auto it = fwd().find(window);
return it == fwd().end() ? std::string() : it->second;
}
wxWindow* window_for_automation_id(const std::string& id) {
std::lock_guard<std::mutex> lk(mtx());
auto it = rev().find(id);
return it == rev().end() ? nullptr : it->second;
}
}}} // namespace

View File

@@ -0,0 +1,19 @@
#pragma once
#include <cstdint>
#include <string>
class wxWindow;
namespace Slic3r { namespace GUI { namespace Automation {
// Process-wide wxWindow* <-> automation_id side map. Header is dependency-light so
// widget-construction code can call set_automation_id() unconditionally — it is a
// cheap, safe registration that no-ops when the window is null.
//
// Registration is pruned automatically when the window is destroyed (bound to
// wxEVT_DESTROY inside set_automation_id).
void set_automation_id(wxWindow* window, const std::string& id);
std::string automation_id_of(const wxWindow* window); // "" if none
wxWindow* window_for_automation_id(const std::string& id); // nullptr if none
}}} // namespace