Barra degli indirizzi nel desktop (Ctrl+L)
Il guscio desktop sapeva aprire solo l'indirizzo passato con --url: per provare una pagina bisognava chiudere e riaprire. Ora Ctrl+L apre una barra in alto, si scrive l'indirizzo, Invio lo apre, Esc annulla. - apri_indirizzo(): normalizza quello che si scrive (senza schema si prova https, senza percorso si aggiunge la barra finale, che alcuni server pretendono) - l'indirizzo iniziale e quello scritto passano dalla stessa funzione: --url esempio.it ora funziona come dalla barra - la barra sospende l'interazione con la pagina mentre è aperta, altrimenti i tasti finirebbero anche nel campo del modulo; e con la barra aperta Esc e q non chiudono più la finestra (q e' una lettera, Esc annulla) - il disegno passa dal raster del motore (FontHolder) su una tela RGBA, poi convertita nel formato della finestra. Il cursore va disegnato DOPO la copia: alla prima stesura lo cancellavo io - --prova-barra TESTO disegna la barra nel PNG senza finestra: serve a verificare l'aspetto senza guardare lo schermo
This commit is contained in:
1 parent
cc449a548d
commit
71f45d8967
3 files changed
+235
-14
No files matched your search
@@ -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'AppImage e l'RPM Sailfish condividono lo stesso motore: quando si ricostruisce
|
||||||
l'uno va ricostruito anche l'altro, altrimenti le due piattaforme divergono.
|
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`.
|
||||||
+229
-13
@@ -350,6 +350,8 @@ fn main() {
|
|||||||
let mut args = std::env::args().skip(1);
|
let mut args = std::env::args().skip(1);
|
||||||
let mut html_path: Option<PathBuf> = None;
|
let mut html_path: Option<PathBuf> = None;
|
||||||
let mut url: Option<String> = None;
|
let mut url: Option<String> = None;
|
||||||
|
// solo per verifica: disegna la barra degli indirizzi nel PNG senza finestra
|
||||||
|
let mut prova_barra: Option<String> = None;
|
||||||
let mut rete_esterna = false;
|
let mut rete_esterna = false;
|
||||||
let mut css_path: Option<PathBuf> = None;
|
let mut css_path: Option<PathBuf> = None;
|
||||||
let mut zoom = 1.0f64;
|
let mut zoom = 1.0f64;
|
||||||
@@ -366,6 +368,7 @@ fn main() {
|
|||||||
|
|
||||||
while let Some(a) = args.next() {
|
while let Some(a) = args.next() {
|
||||||
match a.as_str() {
|
match a.as_str() {
|
||||||
|
"--prova-barra" => prova_barra = args.next(),
|
||||||
"--rete-esterna" => rete_esterna = true,
|
"--rete-esterna" => rete_esterna = true,
|
||||||
"--html" => html_path = args.next().map(PathBuf::from),
|
"--html" => html_path = args.next().map(PathBuf::from),
|
||||||
"--url" => url = args.next(),
|
"--url" => url = args.next(),
|
||||||
@@ -411,9 +414,11 @@ fn main() {
|
|||||||
println!(
|
println!(
|
||||||
"mosaic-desktop [--html FILE] [--css FILE] [--zoom N] [--size WxH]\n\
|
"mosaic-desktop [--html FILE] [--css FILE] [--zoom N] [--size WxH]\n\
|
||||||
\x20 [--font TTF] [--render-out PNG]\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\
|
Rotella: scroll · Ctrl+rotella o +/−: zoom · frecce/PgUp/PgDn/Home/End: scorri\n\
|
||||||
0: zoom 1.0 · R: ricarica\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"
|
Chiudi: pulsante della finestra, Esc, Q, Ctrl+Q, Ctrl+W"
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
@@ -458,13 +463,12 @@ fn main() {
|
|||||||
engine::net::Client::new()
|
engine::net::Client::new()
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut page = match load(
|
// l'indirizzo iniziale e quello scritto nella barra passano dalla stessa
|
||||||
&html_path,
|
// funzione: così `--url esempio.it` (senza schema) funziona come dalla barra
|
||||||
css_path.as_deref(),
|
let mut page = match match url.as_deref() {
|
||||||
&base_dir,
|
Some(u) => apri_indirizzo(u, &client),
|
||||||
url.as_deref(),
|
None => load(&html_path, css_path.as_deref(), &base_dir, None, &client),
|
||||||
&client,
|
} {
|
||||||
) {
|
|
||||||
Ok(p) => p,
|
Ok(p) => p,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
eprintln!("errore: {}", 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<u32> = 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);
|
eprintln!("errore scrittura PNG: {}", e);
|
||||||
std::process::exit(1);
|
std::process::exit(1);
|
||||||
}
|
}
|
||||||
println!(
|
println!(
|
||||||
"{{\"png\":\"{}\",\"size\":[{},{}],\"zoom\":{},\"ms\":{:.2}}}",
|
"{{\"png\":\"{}\",\"size\":[{},{}],\"zoom\":{},\"ms\":{:.2},\"barra\":{}}}",
|
||||||
out.display(),
|
out.display(),
|
||||||
r.width,
|
r.width,
|
||||||
r.height,
|
r.height,
|
||||||
zoom,
|
zoom,
|
||||||
r.ms
|
r.ms,
|
||||||
|
prova_barra.is_some()
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -633,6 +657,8 @@ fn main() {
|
|||||||
window.set_target_fps(60);
|
window.set_target_fps(60);
|
||||||
|
|
||||||
let mut zoom = zoom as f32;
|
let mut zoom = zoom as f32;
|
||||||
|
// barra degli indirizzi: `None` chiusa, `Some(testo)` aperta e in scrittura
|
||||||
|
let mut indirizzo: Option<String> = None;
|
||||||
let mut rendered: Option<Rendered> = None;
|
let mut rendered: Option<Rendered> = None;
|
||||||
let mut scroll = 0f32;
|
let mut scroll = 0f32;
|
||||||
let mut needs_render = true;
|
let mut needs_render = true;
|
||||||
@@ -640,12 +666,16 @@ fn main() {
|
|||||||
while window.is_open() {
|
while window.is_open() {
|
||||||
// Chiusura: pulsante della finestra (is_open torna falso), Esc, Q, Ctrl+Q, Ctrl+W.
|
// 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.
|
// `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_pressed(Key::Q, KeyRepeat::No)
|
||||||
|| (window.is_key_down(Key::Q)
|
|| (window.is_key_down(Key::Q)
|
||||||
&& (window.is_key_down(Key::LeftCtrl) || window.is_key_down(Key::RightCtrl)))
|
&& (window.is_key_down(Key::LeftCtrl) || window.is_key_down(Key::RightCtrl)))
|
||||||
|| (window.is_key_pressed(Key::W, KeyRepeat::No)
|
|| (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;
|
break;
|
||||||
}
|
}
|
||||||
@@ -658,6 +688,84 @@ fn main() {
|
|||||||
continue;
|
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 + / -
|
// zoom: rotella con Ctrl oppure + / -
|
||||||
if let Some((_, dy)) = window.get_scroll_wheel() {
|
if let Some((_, dy)) = window.get_scroll_wheel() {
|
||||||
let ctrl = window.is_key_down(Key::LeftCtrl) || window.is_key_down(Key::RightCtrl);
|
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).
|
/// Copia la parte visibile dell'immagine nel buffer della finestra (0RGB).
|
||||||
/// Lo scroll qui non costa nulla: l'immagine è già tutta rasterizzata.
|
/// 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<Page, String> {
|
||||||
|
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<engine::FontHolder>,
|
||||||
|
) {
|
||||||
|
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<u32> {
|
fn blit(img: &Rendered, w: u32, h: u32, scroll: &mut f32) -> Vec<u32> {
|
||||||
let max_scroll = (img.height as f32 - h as f32).max(0.0);
|
let max_scroll = (img.height as f32 - h as f32).max(0.0);
|
||||||
if *scroll < 0.0 {
|
if *scroll < 0.0 {
|
||||||
|
|||||||
+1
-1
@@ -27,7 +27,7 @@ pub mod treebuilder;
|
|||||||
pub use dom::{Document, NodeId};
|
pub use dom::{Document, NodeId};
|
||||||
pub use font::FontHolder;
|
pub use font::FontHolder;
|
||||||
pub use layout::{ControlKind, ControlSpec, Focusable, FormState, LayoutBox};
|
pub use layout::{ControlKind, ControlSpec, Focusable, FormState, LayoutBox};
|
||||||
pub use style::ComputedStyle;
|
pub use style::{Color, ComputedStyle};
|
||||||
|
|
||||||
/// Un documento pronto per il rendering.
|
/// Un documento pronto per il rendering.
|
||||||
pub struct Page {
|
pub struct Page {
|
||||||
|
|||||||
Reference in new issue
Block a user