From 20a1508ce244c576c3eaca9ede1456b1d3a32e06 Mon Sep 17 00:00:00 2001 From: "John R. Patek" Date: Fri, 17 Apr 2026 11:01:33 -0500 Subject: [PATCH] added window close listener --- example/main.cpp | 14 ++++++++++++++ include/webframe/context.hpp | 11 +++++++++++ src/runtimes/desktop/include/desktop/window.hpp | 7 ++++++- src/runtimes/desktop/window.cpp | 15 +++++++++++++++ 4 files changed, 46 insertions(+), 1 deletion(-) diff --git a/example/main.cpp b/example/main.cpp index 1f15c6d..673889a 100644 --- a/example/main.cpp +++ b/example/main.cpp @@ -4,6 +4,8 @@ #include +#include + static std::string get_archive_path() { std::string result(ARCHIVE_DIRECTORY); @@ -80,6 +82,16 @@ class greeting_ipc_handler : public webframe::handler } }; +class window_listener : public webframe::window_listener +{ +public: + void on_close(webframe::window *source) override + { + source->set_title("Goodbye!"); + std::this_thread::sleep_for(std::chrono::milliseconds(200)); + } +}; + class example_application : public webframe::application { public: @@ -95,8 +107,10 @@ class example_application : public webframe::application webframe::window *win = context->create_window(nullptr, 800, 600); win->set_title("WebFrame Example"); win->load_path("index.html"); + win->add_listener(&_window_listener); } private: + window_listener _window_listener; archive_handler _archive_handler; greeting_ipc_handler _greeting_ipc_handler; }; diff --git a/include/webframe/context.hpp b/include/webframe/context.hpp index cd31722..c865924 100644 --- a/include/webframe/context.hpp +++ b/include/webframe/context.hpp @@ -32,12 +32,23 @@ namespace webframe { + class window; + + class window_listener + { + public: + virtual void on_close(window *source) = 0; + }; + class window { public: virtual void set_title(const std::string& title) = 0; virtual void load_url(const std::string& url) = 0; virtual void load_path(const std::string& path) = 0; + + virtual void add_listener(window_listener *listener) = 0; + virtual std::string get_id() const = 0; }; diff --git a/src/runtimes/desktop/include/desktop/window.hpp b/src/runtimes/desktop/include/desktop/window.hpp index b539f83..86ddbde 100644 --- a/src/runtimes/desktop/include/desktop/window.hpp +++ b/src/runtimes/desktop/include/desktop/window.hpp @@ -13,14 +13,19 @@ namespace webframe::desktop void load_path(const std::string &path) override; void load_url(const std::string &url) override; - std::string get_id() const override; + wxFrame *get_frame() const; + void add_listener(window_listener *listener) override; + void set_title(const std::string &title) override; private: + void on_close(wxCloseEvent &event); + std::unique_ptr _frame; + std::vector _listeners; wxWebView *_webview; struct webview_data { diff --git a/src/runtimes/desktop/window.cpp b/src/runtimes/desktop/window.cpp index ed33c06..b9017ef 100644 --- a/src/runtimes/desktop/window.cpp +++ b/src/runtimes/desktop/window.cpp @@ -19,6 +19,7 @@ namespace webframe::desktop } _webview_data.created = true; }); + _frame->Bind(wxEVT_CLOSE_WINDOW, &window::on_close, this); _frame->Show(); } @@ -46,11 +47,25 @@ namespace webframe::desktop return std::to_string(_frame->GetId()); } + void window::add_listener(window_listener *listener) + { + _listeners.push_back(listener); + } + wxFrame *window::get_frame() const { return _frame.get(); } + void window::on_close(wxCloseEvent &event) + { + for (window_listener *listener : _listeners) + { + listener->on_close(this); + } + event.Skip(); + } + void window::set_title(const std::string &title) { wxString wx_title = wxString(title.c_str());