# HG changeset patch # User Olaf Wintermann # Date 1706466836 -3600 # Node ID 9036b346cd661864f35b66925a65f894c35e358e # Parent 84665f0a9ab29d1991f2d186fa31a81d1cb9d63e implement ui_job() and add ui_call_mainthread (WinUI3) diff -r 84665f0a9ab2 -r 9036b346cd66 make/vs/testapp/main.c --- a/make/vs/testapp/main.c Sun Jan 28 17:10:30 2024 +0100 +++ b/make/vs/testapp/main.c Sun Jan 28 19:33:56 2024 +0100 @@ -55,6 +55,21 @@ UiList* menuList; +void event_mt(UiEvent* event, void* data) { + char* mt_str = data; + + printf("%s\n", mt_str); +} + +int test_threadfunc(void *data) { + char* str = data; + + return 0; +} + +void action_thread_test(UiEvent* event, void* data) { + ui_job(event->obj, test_threadfunc, "testdata", event_mt, "testdata2"); +} void action1(UiEvent* event, void* data) { char* action = data; @@ -247,7 +262,7 @@ ui_scrolledwindow0(obj) { ui_grid(obj, .margin = 10, .columnspacing = 5, .rowspacing = 20) { - ui_button(obj, .label = "Button1", .onclick = action1, .onclickdata = "action1"); + ui_button(obj, .label = "Thread Test", .onclick = action_thread_test, .onclickdata = "action1"); ui_button(obj, .label = "Button2", .icon = "Back", .onclick = action1, .onclickdata = "action2"); ui_button(obj, .icon = "Forward", .onclick = action1, .onclickdata = "action3", .hexpand = true); ui_newline(obj); diff -r 84665f0a9ab2 -r 9036b346cd66 ui/ui/toolkit.h --- a/ui/ui/toolkit.h Sun Jan 28 17:10:30 2024 +0100 +++ b/ui/ui/toolkit.h Sun Jan 28 19:33:56 2024 +0100 @@ -371,6 +371,7 @@ UIEXPORT void ui_close(UiObject *obj); UIEXPORT void ui_job(UiObject *obj, ui_threadfunc tf, void *td, ui_callback f, void *fd); +UIEXPORT void ui_call_mainthread(ui_threadfunc tf, void* td); UIEXPORT void* ui_document_new(size_t size); UIEXPORT void ui_document_destroy(void *doc); diff -r 84665f0a9ab2 -r 9036b346cd66 ui/winui/pch.h --- a/ui/winui/pch.h Sun Jan 28 17:10:30 2024 +0100 +++ b/ui/winui/pch.h Sun Jan 28 19:33:56 2024 +0100 @@ -33,6 +33,7 @@ #include #include #include +#include #include diff -r 84665f0a9ab2 -r 9036b346cd66 ui/winui/toolkit.cpp --- a/ui/winui/toolkit.cpp Sun Jan 28 17:10:30 2024 +0100 +++ b/ui/winui/toolkit.cpp Sun Jan 28 19:33:56 2024 +0100 @@ -43,6 +43,8 @@ #include "App.xaml.h" +#include + using namespace winrt; using namespace Microsoft::UI::Xaml; using namespace Microsoft::UI::Xaml::Controls; @@ -50,6 +52,7 @@ using namespace Microsoft::UI::Xaml::Markup; using namespace Windows::UI::Xaml::Interop; using namespace winrt::Windows::Foundation; +using namespace Windows::UI::Core; static const char* application_name; @@ -69,7 +72,11 @@ static UiObject* active_window; +static winrt::Microsoft::UI::Dispatching::DispatcherQueue uiDispatcherQueue = { nullptr }; + void ui_app_run_startup() { + uiDispatcherQueue = winrt::Microsoft::UI::Dispatching::DispatcherQueue::GetForCurrentThread(); + if (startup_func) { startup_func(NULL, startup_data); } @@ -233,3 +240,58 @@ } +static void ui_job_finished(UiJob *job) { + UiEvent event; + event.obj = job->obj; + event.window = job->obj->window; + event.document = job->obj->ctx->document; + event.intval = 0; + event.eventdata = NULL; + job->finish_callback(&event, job->finish_data); +} + +static void ui_job_thread(UiJob* job) { + if (!job->job_func(job->job_data)) { + bool isQueued = uiDispatcherQueue.TryEnqueue([job]() + { + UiEvent event; + event.obj = job->obj; + event.window = job->obj->window; + event.document = job->obj->ctx->document; + event.intval = 0; + event.eventdata = NULL; + job->finish_callback(&event, job->finish_data); + delete job; + }); + if (!isQueued) { + // TODO: error or try again? + exit(-1); + } + } + else { + delete job; + } +} + +UIEXPORT void ui_job(UiObject* obj, ui_threadfunc tf, void* td, ui_callback f, void* fd) { + UiJob* job = new UiJob; + job->obj = obj; + job->job_func = tf; + job->job_data = td; + job->finish_callback = f; + job->finish_data = fd; + + std::thread jobThread(ui_job_thread, job); + jobThread.detach(); +} + +UIEXPORT void ui_call_mainthread(ui_threadfunc tf, void* td) { + bool isQueued = uiDispatcherQueue.TryEnqueue([tf, td]() + { + (void)tf(td); + }); + if (!isQueued) { + // TODO: error or try again? + exit(-1); + } +} diff -r 84665f0a9ab2 -r 9036b346cd66 ui/winui/toolkit.h --- a/ui/winui/toolkit.h Sun Jan 28 17:10:30 2024 +0100 +++ b/ui/winui/toolkit.h Sun Jan 28 19:33:56 2024 +0100 @@ -30,6 +30,14 @@ #include "../ui/toolkit.h" +typedef struct UiJob { + UiObject* obj; + ui_threadfunc job_func; + void* job_data; + ui_callback finish_callback; + void* finish_data; +} UiJob; + typedef void(*ui_eventfunc)(void*, void*); void ui_app_run_startup();