1+ #include < desktop.hpp>
2+
3+ class wfApp : public wxApp
4+ {
5+ public:
6+ void Init (webframe::application *application, webframe::router *router)
7+ {
8+ _application = application;
9+ _router = router;
10+ }
11+ bool OnInit () override
12+ {
13+ _application->configure_desktop ();
14+ _application->configure_router (_router);
15+ _frame = std::make_unique<wxFrame>(nullptr , wxID_ANY, " WebFrame" );
16+ _webview = wxWebView::New (_frame.get (), wxID_ANY, " about:blank" , wxDefaultPosition, wxDefaultSize, wxWebViewBackendEdge);
17+ #ifdef __WXMSW__
18+ _webview->Bind (wxEVT_WEBVIEW_NAVIGATING, [](wxWebViewEvent &event)
19+ {
20+ const wxString url = event.GetURL ();
21+ if (!url.StartsWith (" https://webframe.ipc" ))
22+ {
23+ event.Skip ();
24+ } });
25+ #endif
26+ _webview->Bind (wxEVT_WEBVIEW_CREATED, [this ](wxWebViewEvent &event)
27+ {
28+ wxWebView *webview = reinterpret_cast <wxWebView *>(event.GetEventObject ());
29+ webview->RegisterHandler (wxSharedPtr<wxWebViewHandler>(new webframe::desktop::webview_handler (_router)));
30+ webview->LoadURL (" https://webframe.ipc/index.html" );
31+ });
32+ _frame->Show ();
33+ return true ;
34+ }
35+
36+ private:
37+ std::unique_ptr<wxFrame> _frame;
38+ wxWebView *_webview;
39+ webframe::application *_application;
40+ webframe::router *_router;
41+ };
42+
43+ namespace webframe
44+ {
45+ namespace desktop
46+ {
47+ int runtime::launch_wx_app (wfApp *app, webframe::application *a, webframe::router *r)
48+ {
49+ int result (0 );
50+ app->Init (a, r);
51+ if (app->CallOnInit ())
52+ {
53+ a->on_dispatch ();
54+ result = app->OnRun ();
55+ }
56+ else
57+ {
58+ result = 1 ;
59+ }
60+ wxEntryCleanup ();
61+ return result;
62+ }
63+ }
64+
65+ runtime *webframe_init ()
66+ {
67+ return new desktop::runtime ();
68+ }
69+ }
70+
71+ // keep platform specific garbage down here
72+ wxIMPLEMENT_APP_NO_MAIN (wfApp);
73+ #ifdef WEBFRAME_WIN32_APP
74+ #include < wx/msw/init.h>
75+ int webframe::desktop::runtime::dispatch (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow, webframe::application *a, webframe::router *r)
76+ {
77+ wxEntryStart (hInstance);
78+ return launch_wx_app (reinterpret_cast <wfApp *>(wxTheApp), a, r);
79+ }
80+ #else
81+ int webframe::desktop::runtime::dispatch (int argc, const char **argv, webframe::application *a, webframe::router *r)
82+ {
83+ wxEntryStart (argc, const_cast <char **>(argv));
84+ return launch_wx_app (reinterpret_cast <wfApp *>(wxTheApp), a, r);
85+ }
86+ #endif
0 commit comments