mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-06-10 22:12:49 +00:00
feat(automation): wxWindow automation-id registry
This commit is contained in:
@@ -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
|
||||
|
||||
50
src/slic3r/GUI/Automation/AutomationRegistry.cpp
Normal file
50
src/slic3r/GUI/Automation/AutomationRegistry.cpp
Normal 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
|
||||
19
src/slic3r/GUI/Automation/AutomationRegistry.hpp
Normal file
19
src/slic3r/GUI/Automation/AutomationRegistry.hpp
Normal 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
|
||||
Reference in New Issue
Block a user