Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/app-pages.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,40 @@ static const char * ZONOS2_APP_ERROR_PAGE = R"html(
<button class="primary" onclick="saucer.exposed.show_setup()">Back to setup</button>
</div>
)html";

// ---------------------------------------------------------------- native file-input shim
// saucer's webviews implement no open-panel delegate, so HTML <input type="file"> 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";
45 changes: 45 additions & 0 deletions src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@
#include <cctype>
#include <chrono>
#include <cstdio>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <iterator>
#include <mutex>
#include <sstream>
#include <string>
Expand Down Expand Up @@ -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 + "\"";
Expand Down Expand Up @@ -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).
Expand All @@ -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 <input type="file"> (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<fs::path> paths;
if (multiple) {
auto r = desktop.pick<saucer::modules::picker::type::files>({});
if (r) paths.assign(r->begin(), r->end());
} else {
auto r = desktop.pick<saucer::modules::picker::type::file>({});
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<char>(f)), std::istreambuf_iterator<char>());
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,
Expand Down
Loading