Skip to content

Commit 4db2bef

Browse files
authored
added window close listener (#28)
1 parent 5cfe131 commit 4db2bef

4 files changed

Lines changed: 46 additions & 1 deletion

File tree

example/main.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include <yyjson.h>
66

7+
#include <thread>
8+
79
static std::string get_archive_path()
810
{
911
std::string result(ARCHIVE_DIRECTORY);
@@ -80,6 +82,16 @@ class greeting_ipc_handler : public webframe::handler
8082
}
8183
};
8284

85+
class window_listener : public webframe::window_listener
86+
{
87+
public:
88+
void on_close(webframe::window *source) override
89+
{
90+
source->set_title("Goodbye!");
91+
std::this_thread::sleep_for(std::chrono::milliseconds(200));
92+
}
93+
};
94+
8395
class example_application : public webframe::application
8496
{
8597
public:
@@ -95,8 +107,10 @@ class example_application : public webframe::application
95107
webframe::window *win = context->create_window(nullptr, 800, 600);
96108
win->set_title("WebFrame Example");
97109
win->load_path("index.html");
110+
win->add_listener(&_window_listener);
98111
}
99112
private:
113+
window_listener _window_listener;
100114
archive_handler _archive_handler;
101115
greeting_ipc_handler _greeting_ipc_handler;
102116
};

include/webframe/context.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,23 @@
3232

3333
namespace webframe
3434
{
35+
class window;
36+
37+
class window_listener
38+
{
39+
public:
40+
virtual void on_close(window *source) = 0;
41+
};
42+
3543
class window
3644
{
3745
public:
3846
virtual void set_title(const std::string& title) = 0;
3947
virtual void load_url(const std::string& url) = 0;
4048
virtual void load_path(const std::string& path) = 0;
49+
50+
virtual void add_listener(window_listener *listener) = 0;
51+
4152
virtual std::string get_id() const = 0;
4253
};
4354

src/runtimes/desktop/include/desktop/window.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,19 @@ namespace webframe::desktop
1313

1414
void load_path(const std::string &path) override;
1515
void load_url(const std::string &url) override;
16-
1716
std::string get_id() const override;
17+
1818
wxFrame *get_frame() const;
1919

20+
void add_listener(window_listener *listener) override;
21+
2022
void set_title(const std::string &title) override;
2123

2224
private:
25+
void on_close(wxCloseEvent &event);
26+
2327
std::unique_ptr<wxFrame> _frame;
28+
std::vector<window_listener*> _listeners;
2429
wxWebView *_webview;
2530
struct webview_data
2631
{

src/runtimes/desktop/window.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace webframe::desktop
1919
}
2020
_webview_data.created = true;
2121
});
22+
_frame->Bind(wxEVT_CLOSE_WINDOW, &window::on_close, this);
2223
_frame->Show();
2324
}
2425

@@ -46,11 +47,25 @@ namespace webframe::desktop
4647
return std::to_string(_frame->GetId());
4748
}
4849

50+
void window::add_listener(window_listener *listener)
51+
{
52+
_listeners.push_back(listener);
53+
}
54+
4955
wxFrame *window::get_frame() const
5056
{
5157
return _frame.get();
5258
}
5359

60+
void window::on_close(wxCloseEvent &event)
61+
{
62+
for (window_listener *listener : _listeners)
63+
{
64+
listener->on_close(this);
65+
}
66+
event.Skip();
67+
}
68+
5469
void window::set_title(const std::string &title)
5570
{
5671
wxString wx_title = wxString(title.c_str());

0 commit comments

Comments
 (0)