-
-
Save vadz/917ce401d2e5bf7a4b552f991da12c7c to your computer and use it in GitHub Desktop.
A simple example showing using wxWebViewChromium from a non-main thread
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "wx/app.h" | |
#include "wx/button.h" | |
#include "wx/frame.h" | |
#include "wx/sizer.h" | |
#include "wx/splitter.h" | |
#include "wx/webview_chromium.h" | |
#include <glib.h> | |
#include <thread> | |
struct Frame : wxFrame { | |
Frame() : wxFrame(nullptr, wxID_ANY, "wxWebView Test") { | |
splitter_ = new wxSplitterWindow{this}; | |
button_ = new wxButton(splitter_, wxID_ANY, "Create web view"); | |
button_->Bind(wxEVT_BUTTON, [this](wxCommandEvent&) { | |
auto webview = wxWebView::New(wxWebViewBackendChromium); | |
if ( !webview->Create(splitter_, wxID_ANY, "chrome://version/") ) | |
{ | |
wxLogError("Creating web view failed"); | |
delete webview; | |
return; | |
} | |
splitter_->SplitVertically(button_, webview); | |
splitter_->UpdateSize(); | |
CallAfter([this]() { button_->Disable(); }); | |
}); | |
splitter_->Initialize(button_); | |
} | |
wxButton* button_ = nullptr; | |
wxSplitterWindow* splitter_ = nullptr; | |
}; | |
struct App : wxApp { | |
bool OnInit() override { | |
if ( !wxApp::OnInit() ) | |
return false; | |
Bind(wxEVT_IDLE, [this](wxIdleEvent& event) { | |
auto const thread_context = g_main_context_get_thread_default(); | |
if ( thread_context != thread_context_ ) | |
{ | |
if ( thread_context ) | |
{ | |
if ( !g_main_context_acquire(thread_context) ) | |
{ | |
wxLogDebug("Failed to acquire thread context."); | |
return; | |
} | |
wxLogDebug("Acquired thread context"); | |
thread_context_ = thread_context; | |
} | |
else | |
{ | |
g_main_context_release(thread_context_); | |
thread_context_ = nullptr; | |
wxLogDebug("Released thread context"); | |
} | |
} | |
if ( thread_context_ && g_main_context_iteration(thread_context_, FALSE) ) | |
event.RequestMore(); | |
}); | |
auto f = new Frame{}; | |
f->Show(); | |
return true; | |
} | |
GMainContext* thread_context_ = nullptr; | |
}; | |
wxIMPLEMENT_APP_NO_MAIN(App); | |
int main(int argc, char* argv[]) | |
{ | |
std::thread t{[&]() { | |
wxEntry(argc, argv); | |
}}; | |
t.join(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment