feat: make dialogs a shared library
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* main.cpp
|
||||
* Shared modal dialogs for MontaukOS desktop apps
|
||||
* libdialogs entry point for shared modal dialogs
|
||||
* Copyright (c) 2026 Daniel Hammer
|
||||
*/
|
||||
|
||||
@@ -16,14 +16,11 @@
|
||||
#include "filedialog.hpp"
|
||||
#include "printdialog.hpp"
|
||||
|
||||
extern "C" {
|
||||
#include <stdio.h>
|
||||
}
|
||||
|
||||
using namespace gui;
|
||||
namespace dlg = gui::dialogs;
|
||||
|
||||
AppState g_app = {};
|
||||
static bool g_fonts_ready = false;
|
||||
|
||||
void dialog_finish(const char* status, const char* path, const char* job_id, const char* message,
|
||||
const char* printer_uri, const char* printer_name, uint32_t copies) {
|
||||
@@ -34,7 +31,6 @@ void dialog_finish(const char* status, const char* path, const char* job_id, con
|
||||
gui::dialogs::safe_copy(g_app.result.printer_name, sizeof(g_app.result.printer_name), printer_name);
|
||||
g_app.result.copies = copies;
|
||||
gui::dialogs::safe_copy(g_app.result.message, sizeof(g_app.result.message), message);
|
||||
gui::dialogs::write_result_file(g_app.request.result_path, &g_app.result);
|
||||
g_app.running = false;
|
||||
}
|
||||
|
||||
@@ -51,17 +47,37 @@ void cleanup() {
|
||||
g_app.win.destroy();
|
||||
}
|
||||
|
||||
extern "C" void _start() {
|
||||
if (!fonts::init()) montauk::exit(1);
|
||||
static void set_error_result(dlg::Result* result, const char* message) {
|
||||
if (!result) return;
|
||||
dlg::reset_result(result);
|
||||
dlg::safe_copy(result->status, sizeof(result->status), "error");
|
||||
dlg::safe_copy(result->message, sizeof(result->message), message);
|
||||
}
|
||||
|
||||
char argbuf[256] = {};
|
||||
if (montauk::getargs(argbuf, sizeof(argbuf)) <= 0 || !argbuf[0]) montauk::exit(1);
|
||||
if (!gui::dialogs::read_request_file(argbuf, &g_app.request)) montauk::exit(1);
|
||||
extern "C" bool dialogs_run_request(const dlg::Request* request, dlg::Result* out_result) {
|
||||
if (!request || !out_result) return false;
|
||||
|
||||
set_error_result(out_result, "dialog failed");
|
||||
|
||||
if (!g_fonts_ready) {
|
||||
if (!fonts::init()) {
|
||||
set_error_result(out_result, "failed to initialize dialog fonts");
|
||||
return true;
|
||||
}
|
||||
g_fonts_ready = true;
|
||||
}
|
||||
|
||||
if (request->kind != dlg::REQUEST_KIND_FILE && request->kind != dlg::REQUEST_KIND_PRINT) {
|
||||
set_error_result(out_result, "unsupported dialog request");
|
||||
return true;
|
||||
}
|
||||
|
||||
g_app = AppState {};
|
||||
g_app.request = *request;
|
||||
g_app.running = true;
|
||||
gui::dialogs::reset_result(&g_app.result);
|
||||
dlg::reset_result(&g_app.result);
|
||||
|
||||
g_app.is_print = g_app.request.kind == gui::dialogs::REQUEST_KIND_PRINT;
|
||||
g_app.is_print = g_app.request.kind == dlg::REQUEST_KIND_PRINT;
|
||||
const int init_w = g_app.is_print ? printdialog::PRINT_INIT_W : filedialog::FILE_INIT_W;
|
||||
const int init_h = g_app.is_print ? printdialog::PRINT_INIT_H : filedialog::FILE_INIT_H;
|
||||
const char* title = g_app.request.title[0] ? g_app.request.title : (g_app.is_print ? "Print" : "Open");
|
||||
@@ -69,7 +85,11 @@ extern "C" void _start() {
|
||||
if (g_app.is_print) printdialog::init(g_app.request);
|
||||
else filedialog::init(g_app.request);
|
||||
|
||||
if (!g_app.win.create(title, init_w, init_h)) montauk::exit(1);
|
||||
if (!g_app.win.create(title, init_w, init_h)) {
|
||||
cleanup();
|
||||
set_error_result(out_result, "failed to create dialog window");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (g_app.is_print) printdialog::draw();
|
||||
else filedialog::draw();
|
||||
@@ -113,5 +133,6 @@ extern "C" void _start() {
|
||||
}
|
||||
|
||||
cleanup();
|
||||
montauk::exit(0);
|
||||
}
|
||||
*out_result = g_app.result;
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user