From 3b595ec4cb33966dc0929fd8f80b31cead083cd2 Mon Sep 17 00:00:00 2001 From: Sofian Mejjoute Date: Wed, 15 Jul 2026 10:44:57 -0600 Subject: [PATCH] fix: file inputs in the app webview open a native picker (speaker uploads) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ported from zonos1.cpp c4837ab — the same saucer gap exists here. saucer's webviews implement no open-panel delegate on any platform (no runOpenPanelWithParameters / run-file-chooser), so every HTML in the served web UI — the speaker audio and speaker embedding uploads used for voice cloning — silently did nothing in zonos2-app: clicking "choose file" showed no picker. Inject a small script into every page load that intercepts clicks on file inputs in the capture phase and routes them to a new exposed pick_upload_files(multiple): native picker (saucer desktop, single or multi-select), read the bytes, return [{name, b64}]. The shim decodes the payload into File objects and sets them on the input via DataTransfer, then dispatches a change event — the page's existing upload logic sees a normal file selection. The web UI stays untouched (it must keep working in plain browsers, where file inputs are fine). Verified: ubuntu:24.04 container build green with the app download + segmented smokes; the picker interaction itself needs a display, so verify by clicking the speaker upload in the app. Co-Authored-By: Claude Opus 4.8 (1M context) --- src/app-pages.h | 37 +++++++++++++++++++++++++++++++++++++ src/app.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/src/app-pages.h b/src/app-pages.h index ebbcfa7..5ac6338 100644 --- a/src/app-pages.h +++ b/src/app-pages.h @@ -201,3 +201,40 @@ static const char * ZONOS2_APP_ERROR_PAGE = R"html( )html"; + +// ---------------------------------------------------------------- native file-input shim +// saucer's webviews implement no open-panel delegate, so HTML does +// nothing in the app (the served web UI uses it for speaker audio/embedding uploads). +// Injected into every page: intercept clicks on file inputs, run the native picker via +// the exposed pick_upload_files(), and materialize the picked files back into the input +// through a DataTransfer so the page's existing change/read logic sees a normal selection. + +static const char * ZONOS2_APP_FILE_PICKER_JS = R"js( +(function () { + if (window.__zonos2NativePicker) return; + window.__zonos2NativePicker = true; + document.addEventListener('click', function (ev) { + const el = ev.target; + if (!el || !(el instanceof HTMLInputElement) || el.type !== 'file') return; + ev.preventDefault(); + ev.stopImmediatePropagation(); + (async () => { + try { + const res = JSON.parse(await saucer.exposed.pick_upload_files(!!el.multiple)); + if (!res.length) return; // cancelled + const dt = new DataTransfer(); + for (const f of res) { + const bin = atob(f.b64); + const bytes = new Uint8Array(bin.length); + for (let i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i); + dt.items.add(new File([bytes], f.name)); + } + el.files = dt.files; + el.dispatchEvent(new Event('change', { bubbles: true })); + } catch (e) { + console.error('native file picker failed:', e); + } + })(); + }, true); +})(); +)js"; diff --git a/src/app.cpp b/src/app.cpp index 389b386..c1d004e 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -26,10 +26,12 @@ #include #include #include +#include #include #include #include #include +#include #include #include #include @@ -157,6 +159,22 @@ static void set_env(const char * k, const std::string & v) { #endif } +static std::string b64_encode(const std::string & in) { + static const char * A = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + std::string out; + out.reserve((in.size() + 2) / 3 * 4); + for (size_t i = 0; i < in.size(); i += 3) { + uint32_t v = (uint8_t) in[i] << 16; + if (i + 1 < in.size()) v |= (uint8_t) in[i + 1] << 8; + if (i + 2 < in.size()) v |= (uint8_t) in[i + 2]; + out.push_back(A[(v >> 18) & 63]); + out.push_back(A[(v >> 12) & 63]); + out.push_back(i + 1 < in.size() ? A[(v >> 6) & 63] : '='); + out.push_back(i + 2 < in.size() ? A[v & 63] : '='); + } + return out; +} + static std::string shq(const std::string & s) { // quote a path for the system() shell #ifdef _WIN32 return "\"" + s + "\""; @@ -610,6 +628,9 @@ coco::stray start(saucer::application * app) { window->set_title("Zonos2"); window->set_size({.w = 1100, .h = 800}); + + // File-input shim on every page load (setup page and the served web UI alike). + webview->inject({.code = ZONOS2_APP_FILE_PICKER_JS, .run_at = saucer::script::time::creation}); { // Window icon where the platform supports it (no-op on WebKitGTK; the // Windows taskbar/exe icon comes from the .rc resource, macOS from the bundle). @@ -626,6 +647,30 @@ coco::stray start(saucer::application * app) { return r ? file_uri_to_path(r->string()) : std::string{}; }); + // Backs the injected file-input shim (ZONOS2_APP_FILE_PICKER_JS): saucer's webviews + // implement no open-panel delegate, so the served UI's (speaker + // audio/embedding uploads) would silently do nothing. Pick natively, read the bytes, + // and hand them to the page as [{name, b64}]. + webview->expose("pick_upload_files", [&desktop](bool multiple) -> std::string { + std::vector paths; + if (multiple) { + auto r = desktop.pick({}); + if (r) paths.assign(r->begin(), r->end()); + } else { + auto r = desktop.pick({}); + if (r) paths.push_back(*r); + } + json arr = json::array(); + for (const auto & p : paths) { + const std::string path = file_uri_to_path(p.string()); + std::ifstream f(path, std::ios::binary); + if (!f) continue; + std::string bytes((std::istreambuf_iterator(f)), std::istreambuf_iterator()); + arr.push_back({{"name", fs::path(path).filename().string()}, {"b64", b64_encode(bytes)}}); + } + return arr.dump(); + }); + webview->expose("get_config", []() -> std::string { return load_config().dump(); }); webview->expose("launch", [webview](const std::string & model_in, const std::string & dac_in,