diff --git a/desktop/README.md b/desktop/README.md index 7b1140e..e792ea1 100644 --- a/desktop/README.md +++ b/desktop/README.md @@ -66,3 +66,8 @@ APPIMAGE_EXTRACT_AND_RUN=1 ./Mosaic-desktop-0.1.1-x86_64.AppImage --render-out / L'AppImage e l'RPM Sailfish condividono lo stesso motore: quando si ricostruisce l'uno va ricostruito anche l'altro, altrimenti le due piattaforme divergono. + +## Uso + +**Ctrl+L** apre la barra degli indirizzi: si scrive l'indirizzo (anche senza +`https://`), Invio lo apre, Esc annulla. All'avvio: `--url esempio.it`. diff --git a/desktop/src/main.rs b/desktop/src/main.rs index 6ecbc22..27b87ad 100644 --- a/desktop/src/main.rs +++ b/desktop/src/main.rs @@ -350,6 +350,8 @@ fn main() { let mut args = std::env::args().skip(1); let mut html_path: Option = None; let mut url: Option = None; + // solo per verifica: disegna la barra degli indirizzi nel PNG senza finestra + let mut prova_barra: Option = None; let mut rete_esterna = false; let mut css_path: Option = None; let mut zoom = 1.0f64; @@ -366,6 +368,7 @@ fn main() { while let Some(a) = args.next() { match a.as_str() { + "--prova-barra" => prova_barra = args.next(), "--rete-esterna" => rete_esterna = true, "--html" => html_path = args.next().map(PathBuf::from), "--url" => url = args.next(), @@ -411,9 +414,11 @@ fn main() { println!( "mosaic-desktop [--html FILE] [--css FILE] [--zoom N] [--size WxH]\n\ \x20 [--font TTF] [--render-out PNG]\n\ - \x20--url URL carica una pagina da http:// (TLS non ancora incluso)\n\n\ + \x20--url URL apre una pagina: http, https o solo dominio (esempio.it)\n\ + \x20--rete-esterna la rete gira in un processo a parte (mosaic-net)\n\n\ Rotella: scroll · Ctrl+rotella o +/−: zoom · frecce/PgUp/PgDn/Home/End: scorri\n\ 0: zoom 1.0 · R: ricarica\n\ + Ctrl+L: scrivi un indirizzo (Invio apre, Esc annulla)\n\ Chiudi: pulsante della finestra, Esc, Q, Ctrl+Q, Ctrl+W" ); return; @@ -458,13 +463,12 @@ fn main() { engine::net::Client::new() }); - let mut page = match load( - &html_path, - css_path.as_deref(), - &base_dir, - url.as_deref(), - &client, - ) { + // l'indirizzo iniziale e quello scritto nella barra passano dalla stessa + // funzione: così `--url esempio.it` (senza schema) funziona come dalla barra + let mut page = match match url.as_deref() { + Some(u) => apri_indirizzo(u, &client), + None => load(&html_path, css_path.as_deref(), &base_dir, None, &client), + } { Ok(p) => p, Err(e) => { eprintln!("errore: {}", e); @@ -597,17 +601,37 @@ fn main() { } } } - if let Err(e) = pngio::write_rgba(&out, r.width, r.height, &r.rgba) { + if let Some(testo) = &prova_barra { + let (w, h) = (r.width, r.height); + let mut buf: Vec = r + .rgba + .chunks_exact(4) + .map(|p| ((p[0] as u32) << 16) | ((p[1] as u32) << 8) | p[2] as u32) + .collect(); + disegna_barra(&mut buf, w, h, testo, &metri); + let mut rgba = Vec::with_capacity(r.rgba.len()); + for pixel in &buf { + rgba.push((pixel >> 16) as u8); + rgba.push((pixel >> 8) as u8); + rgba.push(*pixel as u8); + rgba.push(255); + } + if let Err(e) = pngio::write_rgba(&out, w, h, &rgba) { + eprintln!("errore scrittura PNG: {}", e); + std::process::exit(1); + } + } else if let Err(e) = pngio::write_rgba(&out, r.width, r.height, &r.rgba) { eprintln!("errore scrittura PNG: {}", e); std::process::exit(1); } println!( - "{{\"png\":\"{}\",\"size\":[{},{}],\"zoom\":{},\"ms\":{:.2}}}", + "{{\"png\":\"{}\",\"size\":[{},{}],\"zoom\":{},\"ms\":{:.2},\"barra\":{}}}", out.display(), r.width, r.height, zoom, - r.ms + r.ms, + prova_barra.is_some() ); return; } @@ -633,6 +657,8 @@ fn main() { window.set_target_fps(60); let mut zoom = zoom as f32; + // barra degli indirizzi: `None` chiusa, `Some(testo)` aperta e in scrittura + let mut indirizzo: Option = None; let mut rendered: Option = None; let mut scroll = 0f32; let mut needs_render = true; @@ -640,12 +666,16 @@ fn main() { while window.is_open() { // Chiusura: pulsante della finestra (is_open torna falso), Esc, Q, Ctrl+Q, Ctrl+W. // `is_key_pressed` intercetta anche il tocco singolo; `is_key_down` copre Q tenuto. - if window.is_key_pressed(Key::Escape, KeyRepeat::No) + // Con la barra degli indirizzi aperta questi tasti non chiudono nulla: `q` + // è una lettera e `Esc` annulla la scrittura. + let in_scrittura = indirizzo.is_some(); + if !in_scrittura + && (window.is_key_pressed(Key::Escape, KeyRepeat::No) || window.is_key_pressed(Key::Q, KeyRepeat::No) || (window.is_key_down(Key::Q) && (window.is_key_down(Key::LeftCtrl) || window.is_key_down(Key::RightCtrl))) || (window.is_key_pressed(Key::W, KeyRepeat::No) - && (window.is_key_down(Key::LeftCtrl) || window.is_key_down(Key::RightCtrl))) + && (window.is_key_down(Key::LeftCtrl) || window.is_key_down(Key::RightCtrl)))) { break; } @@ -658,6 +688,84 @@ fn main() { continue; } + // --- barra degli indirizzi (Ctrl+L) ------------------------------- + // Aperta, prende tutti i tasti e sospende l'interazione con la pagina: + // altrimenti scrivere l'indirizzo finirebbe anche nel campo del modulo. + let ctrl = window.is_key_down(Key::LeftCtrl) || window.is_key_down(Key::RightCtrl); + if indirizzo.is_none() && ctrl && window.is_key_pressed(Key::L, KeyRepeat::No) { + indirizzo = Some( + page.base_url + .clone() + .or_else(|| url.clone()) + .unwrap_or_default(), + ); + } + if let Some(mut testo) = indirizzo.take() { + let shift = window.is_key_down(Key::LeftShift); + let invio = window.is_key_pressed(Key::Enter, KeyRepeat::No); + let annulla = window.is_key_pressed(Key::Escape, KeyRepeat::No); + if annulla { + needs_render = true; + } else if invio { + match apri_indirizzo(&testo, &client) { + Ok(p) => { + page = p; + url = page.base_url.clone(); + contenuto = engine::ContenutoPagina::da_html(&page.html); + macchina = engine::script::Macchina::nuova().unwrap_or_else(|e| { + eprintln!("mosaic: interprete non disponibile: {}", e); + macchina + }); + avvia_script( + &mut contenuto, + &mut macchina, + &page.base_dir, + page.base_url.as_deref(), + ); + form = FormState::default(); + scroll = 0.0; + needs_render = true; + } + Err(e) => { + // la barra resta aperta: l'indirizzo sbagliato si corregge + eprintln!("mosaic: {}", e); + indirizzo = Some(testo.clone()); + } + } + } else { + if window.is_key_pressed(Key::Backspace, KeyRepeat::Yes) { + testo.pop(); + } + if window.is_key_pressed(Key::Space, KeyRepeat::Yes) { + testo.push(' '); + } + for k in window.get_keys_pressed(KeyRepeat::No) { + // Ctrl+L e i tasti di servizio non devono finire nel testo + if k == Key::L && ctrl { + continue; + } + if let Some(c) = key_char(k, shift) { + testo.push(c); + } + } + indirizzo = Some(testo); + } + } + if let Some(testo) = &indirizzo { + // mentre si scrive l'indirizzo la pagina non reagisce + let buf = match &rendered { + Some(r) => blit(r, w, h, &mut scroll), + None => vec![0x00ff_ffff; (w * h) as usize], + }; + let mut buf = buf; + disegna_barra(&mut buf, w, h, testo, &metri); + if let Err(e) = window.update_with_buffer(&buf, w as usize, h as usize) { + eprintln!("errore di disegno: {}", e); + break; + } + continue; + } + // zoom: rotella con Ctrl oppure + / - if let Some((_, dy)) = window.get_scroll_wheel() { let ctrl = window.is_key_down(Key::LeftCtrl) || window.is_key_down(Key::RightCtrl); @@ -1171,6 +1279,114 @@ fn main() { /// Copia la parte visibile dell'immagine nel buffer della finestra (0RGB). /// Lo scroll qui non costa nulla: l'immagine è già tutta rasterizzata. +/// Carica un indirizzo scritto a mano. Senza schema si prova https, che è la +/// norma; un indirizzo senza percorso riceve `/`, altrimenti il server riceve +/// una richiesta vuota. +fn apri_indirizzo(testo: &str, client: &engine::net::Client) -> Result { + let scritto = testo.trim(); + if scritto.is_empty() { + return Err("indirizzo vuoto".to_string()); + } + let mut indirizzo = if scritto.starts_with("http://") || scritto.starts_with("https://") { + scritto.to_string() + } else if scritto.starts_with("file://") { + let percorso = scritto.trim_start_matches("file://"); + return load( + &PathBuf::from(percorso), + None, + &PathBuf::from("."), + None, + client, + ); + } else { + format!("https://{}", scritto) + }; + // "https://esempio.it" senza percorso: la barra finale non è un dettaglio, + // perché alcuni server rispondono 400 a un percorso vuoto + let dopo_schema = indirizzo.find("://").map(|i| i + 3).unwrap_or(0); + if !indirizzo[dopo_schema..].contains('/') { + indirizzo.push('/'); + } + load(&PathBuf::new(), None, &PathBuf::from("."), Some(&indirizzo), client) +} + +/// Disegna la barra degli indirizzi sopra la pagina: una banda scura e il testo. +/// Il testo si disegna su una tela RGBA perché il raster del motore lavora in +/// RGBA, mentre la finestra vuole 0x00RRGGBB. +fn disegna_barra( + buf: &mut [u32], + w: u32, + h: u32, + testo: &str, + font: &Option, +) { + const ALTEZZA: u32 = 38; + const MARGINE: f32 = 12.0; + const CORPO: f32 = 16.0; + let banda = 0x001b_2b_3a; // blu notte + let altezza = ALTEZZA.min(h); + for y in 0..altezza { + for x in 0..w { + buf[(y * w + x) as usize] = banda; + } + } + // filo chiaro in fondo alla banda, per staccarla dalla pagina + if altezza < h { + for x in 0..w { + buf[((altezza) * w + x) as usize] = 0x0055_6b_7f; + } + } + + let Some(font) = font.as_ref() else { + return; + }; + let mut tela = vec![0u8; (w as usize) * (altezza as usize) * 4]; + // la tela parte dal colore della banda: il testo ci si sovrappone + for pixel in tela.chunks_exact_mut(4) { + pixel[0] = ((banda >> 16) & 0xff) as u8; + pixel[1] = ((banda >> 8) & 0xff) as u8; + pixel[2] = (banda & 0xff) as u8; + pixel[3] = 255; + } + let sagoma = font.shape(testo, CORPO); + let bianco = engine::Color { + r: 240, + g: 244, + b: 248, + a: 255, + }; + font.draw_shaped( + &mut tela, + w, + altezza, + MARGINE, + altezza as f32 * 0.68, + &sagoma, + CORPO, + CORPO, + bianco, + 400, + ); + for y in 0..altezza { + for x in 0..w { + let i = ((y * w + x) * 4) as usize; + buf[(y * w + x) as usize] = + ((tela[i] as u32) << 16) | ((tela[i + 1] as u32) << 8) | tela[i + 2] as u32; + } + } + + // il cursore va disegnato DOPO aver riportato la tela, altrimenti la copia + // lo cancella: è quello che era successo alla prima stesura + let fine = MARGINE + font.advance(testo, CORPO); + let x_cursore = (fine as u32).min(w.saturating_sub(1)); + for y in (altezza as f32 * 0.25) as u32..(altezza as f32 * 0.78) as u32 { + let indice = (y * w + x_cursore) as usize; + if indice < buf.len() { + buf[indice] = 0x00f0_f4_f8; + } + } +} + fn blit(img: &Rendered, w: u32, h: u32, scroll: &mut f32) -> Vec { let max_scroll = (img.height as f32 - h as f32).max(0.0); if *scroll < 0.0 { diff --git a/engine/src/lib.rs b/engine/src/lib.rs index e43d39c..a45dc0c 100644 --- a/engine/src/lib.rs +++ b/engine/src/lib.rs @@ -27,7 +27,7 @@ pub mod treebuilder; pub use dom::{Document, NodeId}; pub use font::FontHolder; pub use layout::{ControlKind, ControlSpec, Focusable, FormState, LayoutBox}; -pub use style::ComputedStyle; +pub use style::{Color, ComputedStyle}; /// Un documento pronto per il rendering. pub struct Page {