mirror of
https://github.com/storytold/texturecraft.git
synced 2026-11-26 01:10:48 +01:00
The top menu strip is now the window's title bar wherever the app draws the frame (crates/ui-egui/src/titlebar.rs), using craft-window for the decision and for the draggable empty area: - Windows: no OS frame. App icon at the very left, the menus, empty space that moves the window (double-click maximizes), the mode label, then minimize, maximize/restore and close (46 pt wide, crisp pixel-snapped glyphs, close turns red on hover, dimmed while inactive). Invisible edge and corner zones resize the window. A per-frame guard ported from VectorCraft keeps the undecorated window borderless and a maximized one clear of the taskbar (winsafe, no unsafe), and the taskbar gets the big app icon. - macOS: the traffic lights over the strip (full-size content view, hidden title), centred on it with eframe's traffic-light API, the app icon after their inset, and AppKit's movable flag synced by craft-window. - Linux and the BSDs: craft-window's probe decides; where the compositor leaves the frame to the app (GNOME) the strip draws the buttons in the desktop's button layout. - Wherever the system draws the frame (and on the web) the strip stays the menu bar, with the app icon at its left. No line under the strip. TEXTURECRAFT_TITLEBAR=app|system overrides the decision. Settings › Interface › System title bar (applies on restart) brings the OS frame back; it is read from the saved settings before the window opens and is settable over the control channel as prefs.system_title_bar. ui.inspect reports titleBar. New strings are translated in every language, and the offscreen snapshots render the app-drawn and macOS variants.
168 lines
7.3 KiB
Rust
168 lines
7.3 KiB
Rust
//! The JSON control channel: agents drive the running app with the same commands as the menus,
|
|
//! inspect session and UI state, invoke menu items, paint strokes and take screenshots.
|
|
//!
|
|
//! Methods (params as JSON objects):
|
|
//! - `command.run {id, params?}`: run an engine command (programmatic: never opens a dialog).
|
|
//! - `command.list {filter?}`: every command with params docs and enabled state.
|
|
//! - `session.inspect`: project, texture sets, layer stack, tool, camera, display, history.
|
|
//! - `ui.inspect`: the UI state (panels, windows, mode, layout, viewport rect, who draws the title bar).
|
|
//! - `ui.menu.list`: the menu catalog with ids, shortcuts and whether each is live.
|
|
//! - `ui.menu.invoke {id}`: behave like a menu click (may open a window).
|
|
//! - `ui.set {...}`: merge fields into the UI state (e.g. `{"layout":"Only3D"}`).
|
|
//! - `viewport.screenshot {view?: "3d"|"2d"}`: PNG of a viewport, base64.
|
|
//! - `ui.screenshot`: PNG of the whole window (replies after the next frame).
|
|
|
|
use std::sync::mpsc::{Receiver, Sender, channel};
|
|
|
|
use base64::Engine as _;
|
|
use serde_json::{Value, json};
|
|
|
|
use crate::TextureCraftApp;
|
|
|
|
pub struct ControlRequest {
|
|
pub method: String,
|
|
pub params: Value,
|
|
pub reply: Sender<Value>,
|
|
}
|
|
|
|
impl ControlRequest {
|
|
pub fn new(method: String, params: Value) -> (ControlRequest, Receiver<Value>) {
|
|
let (tx, rx) = channel();
|
|
(ControlRequest { method, params, reply: tx }, rx)
|
|
}
|
|
}
|
|
|
|
fn ok(v: Value) -> Value {
|
|
json!({ "ok": true, "result": v })
|
|
}
|
|
|
|
fn err(e: impl std::fmt::Display) -> Value {
|
|
json!({ "ok": false, "error": e.to_string() })
|
|
}
|
|
|
|
pub fn poll(app: &mut TextureCraftApp, ctx: &egui::Context) {
|
|
let Some(rx) = app.control.as_ref() else { return };
|
|
let reqs: Vec<ControlRequest> = rx.try_iter().collect();
|
|
for r in reqs {
|
|
if r.method == "ui.screenshot" {
|
|
// Replies when the next frame has been captured.
|
|
let reply = r.reply;
|
|
let repaint = ctx.clone();
|
|
let callback = egui::ScreenshotCallback::new(move |img| {
|
|
let answer = encode_png(img.size[0] as u32, img.size[1] as u32, img.pixels.iter().flat_map(|c| c.to_array()).collect())
|
|
.map(|(w, h, png)| ok(json!({ "width": w, "height": h, "png": base64::engine::general_purpose::STANDARD.encode(png) })))
|
|
.unwrap_or_else(|| err("could not encode the screenshot"));
|
|
let _ = reply.send(answer);
|
|
repaint.request_repaint();
|
|
});
|
|
ctx.send_viewport_cmd(egui::ViewportCommand::Screenshot(callback));
|
|
ctx.request_repaint();
|
|
continue;
|
|
}
|
|
let reply = handle(app, ctx, &r.method, &r.params);
|
|
let _ = r.reply.send(reply);
|
|
ctx.request_repaint();
|
|
}
|
|
}
|
|
|
|
pub(crate) fn encode_png(w: u32, h: u32, rgba: Vec<u8>) -> Option<(u32, u32, Vec<u8>)> {
|
|
let img = image::RgbaImage::from_raw(w, h, rgba)?;
|
|
let mut png = Vec::new();
|
|
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).ok()?;
|
|
Some((w, h, png))
|
|
}
|
|
|
|
pub fn handle(app: &mut TextureCraftApp, ctx: &egui::Context, method: &str, p: &Value) -> Value {
|
|
match method {
|
|
"ping" => ok(json!("pong")),
|
|
"ui.resize" => {
|
|
let size = |k: &str| p.get(k).and_then(Value::as_f64).filter(|v| v.is_finite()).map(|v| v.clamp(640.0, 7680.0) as f32);
|
|
match (size("width"), size("height")) {
|
|
(Some(w), Some(h)) => {
|
|
ctx.send_viewport_cmd(egui::ViewportCommand::Maximized(false));
|
|
ctx.send_viewport_cmd(egui::ViewportCommand::InnerSize(egui::vec2(w, h)));
|
|
ok(Value::Null)
|
|
}
|
|
_ => err("`width` and `height` (points, 640..7680) are required"),
|
|
}
|
|
}
|
|
"debug.frames" => ok(app.profiler.report()),
|
|
"debug.paintTest" => match crate::profile::start_paint_test(app, p) {
|
|
Ok(()) => ok(Value::Null),
|
|
Err(e) => err(e),
|
|
},
|
|
"command.run" => {
|
|
let Some(id) = p.get("id").and_then(Value::as_str) else { return err("`id` is required") };
|
|
let params = p.get("params").cloned().unwrap_or(Value::Null);
|
|
match texturecraft_engine::execute(&mut app.session, id, ¶ms) {
|
|
Ok(v) => ok(v),
|
|
Err(e) => err(e),
|
|
}
|
|
}
|
|
"command.list" => ok(texturecraft_engine::commands::list(&app.session, p.get("filter").and_then(Value::as_str))),
|
|
"session.inspect" => ok(texturecraft_engine::inspect::session(&app.session)),
|
|
"ui.inspect" => {
|
|
let r = app.viewport_rect;
|
|
ok(json!({
|
|
"state": app.ui,
|
|
"viewport": [r.min.x, r.min.y, r.width(), r.height()],
|
|
"fps": app.fps,
|
|
"uiMs": app.ui_ms,
|
|
"titleBar": app.title_bar.name(),
|
|
"baking": app.bake_progress(),
|
|
"toasts": app.toasts.iter().map(|t| json!({"text": t.text, "error": t.error})).collect::<Vec<_>>(),
|
|
}))
|
|
}
|
|
"ui.menu.list" => ok(json!(
|
|
crate::menus::CATALOG
|
|
.iter()
|
|
.filter(|i| i.label != "-")
|
|
.map(|i| json!({
|
|
"menu": i.menu, "submenu": i.sub, "label": i.label, "id": i.id, "shortcut": crate::keymap::shortcut_of(&app.ui.keymap, i.id),
|
|
"implemented": crate::menus::implemented(i.id), "enabled": crate::menus::enabled(app, i.id),
|
|
}))
|
|
.collect::<Vec<_>>()
|
|
)),
|
|
"ui.menu.invoke" => {
|
|
let Some(id) = p.get("id").and_then(Value::as_str) else { return err("`id` is required") };
|
|
if !crate::menus::implemented(id) {
|
|
return err(format!("menu item `{id}` is not implemented"));
|
|
}
|
|
crate::menus::invoke(app, ctx, id);
|
|
ok(Value::Null)
|
|
}
|
|
"ui.set" => {
|
|
let mut cur = serde_json::to_value(&app.ui).unwrap_or(Value::Null);
|
|
if let (Some(obj), Some(patch)) = (cur.as_object_mut(), p.as_object()) {
|
|
for (k, v) in patch {
|
|
obj.insert(k.clone(), v.clone());
|
|
}
|
|
}
|
|
match serde_json::from_value(cur) {
|
|
Ok(ui) => {
|
|
app.ui = ui;
|
|
ok(Value::Null)
|
|
}
|
|
Err(e) => err(format!("invalid UI state: {e}")),
|
|
}
|
|
}
|
|
"viewport.screenshot" => {
|
|
let view = if p.get("view").and_then(Value::as_str) == Some("2d") { crate::viewport::VIEW_2D } else { crate::viewport::VIEW_3D };
|
|
match screenshot(app, view) {
|
|
Some((w, h, png)) => ok(json!({ "width": w, "height": h, "png": base64::engine::general_purpose::STANDARD.encode(png) })),
|
|
None => err("no rendered view to capture yet"),
|
|
}
|
|
}
|
|
other => err(format!("unknown method `{other}`")),
|
|
}
|
|
}
|
|
|
|
/// Encode a rendered view as PNG.
|
|
pub fn screenshot(app: &TextureCraftApp, view: u64) -> Option<(u32, u32, Vec<u8>)> {
|
|
let rs = app.render_state.as_ref()?;
|
|
let renderer = rs.renderer.read();
|
|
let gpu = renderer.callback_resources.get::<crate::gpu::GpuState>()?;
|
|
let (w, h, rgba) = gpu.renderer.read_view(&rs.device, &rs.queue, view)?;
|
|
encode_png(w, h, rgba)
|
|
}
|