Clearwake / app · clearwake.ai / crates / clearwake-glimmer / src / lib.rs
| 1 | //! Clearwake on Glimmer: the same page engine as every other Clearwake, |
| 2 | //! drawn as a native Glimmer window by Glimmer's own kernel. |
| 3 | //! |
| 4 | //! The kernel owns the network. When the person opens an address, the |
| 5 | //! window asks for it ([`ClearwakeWindow::take_request`]); the kernel loads |
| 6 | //! it (after the same filter check) and hands the page back with |
| 7 | //! [`ClearwakeWindow::show_page`]. Until the kernel's network stack is |
| 8 | //! wired in, the window shows its built-in welcome page. |
| 9 | |
| 10 | #![no_std] |
| 11 | |
| 12 | extern crate alloc; |
| 13 | |
| 14 | use alloc::string::{String, ToString}; |
| 15 | use alloc::vec::Vec; |
| 16 | use clearwake_core::html::{self, Block, Document, Inline}; |
| 17 | use clearwake_core::{Filter, Url}; |
| 18 | use glimmer_ui::canvas::{Canvas, Rect}; |
| 19 | use glimmer_ui::font::{self, Font}; |
| 20 | use glimmer_ui::input::Key; |
| 21 | use glimmer_ui::theme::Theme; |
| 22 | use glimmer_ui::wm::{App, Ctx, WinEvent}; |
| 23 | |
| 24 | const BAR_H: i32 = 64; |
| 25 | const COLUMN: i32 = 760; |
| 26 | |
| 27 | pub const WELCOME: &str = "<html><head><title>Clearwake</title></head><body><main>\ |
| 28 | <h1>Clearwake</h1><p><em>Less noise. More web.</em></p>\ |
| 29 | <p>This is Clearwake running natively on Glimmer, drawn by Glimmer's own kernel. \ |
| 30 | It reads pages into clean text, links and pictures. Scripts never run, and ads and trackers are blocked before they load.</p>\ |
| 31 | <h2>Always on</h2><ul><li>No ad blocking switch and no exceptions.</li><li>No account. Bookmarks stay on this machine.</li>\ |
| 32 | <li>The same Rust engine as Clearwake for Mac, Windows, Linux, Android and the web.</li></ul>\ |
| 33 | <p>Type an address above and press Enter.</p></main></body></html>"; |
| 34 | |
| 35 | struct Hit { |
| 36 | rect: Rect, |
| 37 | href: String, |
| 38 | } |
| 39 | |
| 40 | pub struct ClearwakeWindow { |
| 41 | filter: Filter, |
| 42 | doc: Document, |
| 43 | address: String, |
| 44 | editing: bool, |
| 45 | scroll: i32, |
| 46 | content_h: i32, |
| 47 | hits: Vec<Hit>, |
| 48 | request: Option<String>, |
| 49 | status: String, |
| 50 | view_h: i32, |
| 51 | } |
| 52 | |
| 53 | impl ClearwakeWindow { |
| 54 | pub fn new() -> ClearwakeWindow { |
| 55 | let filter = Filter::new(); |
| 56 | let url = Url::parse("about:home").unwrap(); |
| 57 | let doc = html::parse(WELCOME, &url, &filter); |
| 58 | ClearwakeWindow { |
| 59 | filter, |
| 60 | doc, |
| 61 | address: String::new(), |
| 62 | editing: false, |
| 63 | scroll: 0, |
| 64 | content_h: 0, |
| 65 | hits: Vec::new(), |
| 66 | request: None, |
| 67 | status: String::from("Ad blocking is always on"), |
| 68 | view_h: 600, |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /// The address the person asked for, if any. The kernel loads it. |
| 73 | pub fn take_request(&mut self) -> Option<String> { |
| 74 | self.request.take() |
| 75 | } |
| 76 | |
| 77 | /// Show a page the kernel loaded. |
| 78 | pub fn show_page(&mut self, url: &str, source: &str) { |
| 79 | let u = Url::parse(url).unwrap_or_else(|| Url::parse("about:blank").unwrap()); |
| 80 | self.doc = html::parse(source, &u, &self.filter); |
| 81 | self.address = url.to_string(); |
| 82 | self.scroll = 0; |
| 83 | let s = &self.doc.stats; |
| 84 | self.status = alloc::format!("Blocked {} ads and trackers · {} scripts never ran", s.blocked + s.hidden, s.scripts_skipped); |
| 85 | } |
| 86 | |
| 87 | /// The kernel couldn't load the page. |
| 88 | pub fn show_error(&mut self, url: &str, message: &str) { |
| 89 | let page = alloc::format!("<h1>This page didn't open</h1><p>{}</p>", message); |
| 90 | self.show_page(url, &page); |
| 91 | self.status = String::from(message); |
| 92 | } |
| 93 | |
| 94 | fn open(&mut self, input: &str) { |
| 95 | match clearwake_core::resolve_input(input) { |
| 96 | Some(u) if self.filter.blocks(&u) => { |
| 97 | let s = u.to_string(); |
| 98 | self.show_error(&s, "Clearwake blocked this address because it serves ads or tracking."); |
| 99 | } |
| 100 | Some(u) if u.is_about() => { |
| 101 | *self = ClearwakeWindow::new(); |
| 102 | } |
| 103 | Some(u) => { |
| 104 | self.address = u.to_string(); |
| 105 | self.status = alloc::format!("Loading {}", u.display_host()); |
| 106 | self.request = Some(u.to_string()); |
| 107 | } |
| 108 | None => {} |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | impl Default for ClearwakeWindow { |
| 114 | fn default() -> Self { |
| 115 | ClearwakeWindow::new() |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /// Lay out and draw a run of styled words, wrapping at `max`. Returns the |
| 120 | /// height used. Link words are recorded as click targets. |
| 121 | #[allow(clippy::too_many_arguments)] |
| 122 | fn flow(cv: &mut Canvas, hits: &mut Vec<Hit>, spans: &[Inline], x: i32, y: i32, max: i32, f: &Font, bold: &Font, th: &Theme, color: u32) -> i32 { |
| 123 | let line_h = f.line() + 6; |
| 124 | let space = f.width(" "); |
| 125 | let (mut cx, mut cy) = (x, y); |
| 126 | for sp in spans { |
| 127 | let face = if sp.bold { bold } else { f }; |
| 128 | let c = if sp.link.is_some() { th.link } else { color }; |
| 129 | let dy = f.ascent() - face.ascent(); |
| 130 | for (n, para) in sp.text.split('\n').enumerate() { |
| 131 | if n > 0 { |
| 132 | cx = x; |
| 133 | cy += line_h; |
| 134 | } |
| 135 | let lead = para.starts_with(' '); |
| 136 | if lead && cx > x { |
| 137 | cx += space; |
| 138 | } |
| 139 | for word in para.split(' ').filter(|w| !w.is_empty()) { |
| 140 | let w = face.width(word); |
| 141 | if cx > x && cx + w > x + max { |
| 142 | cx = x; |
| 143 | cy += line_h; |
| 144 | } |
| 145 | face.draw_top(cv, cx, cy + dy, word, c); |
| 146 | if let Some(h) = &sp.link { |
| 147 | hits.push(Hit { rect: Rect::new(cx, cy, w + space, line_h), href: h.clone() }); |
| 148 | cv.fill(Rect::new(cx, cy + f.line(), w, 1), c); |
| 149 | } |
| 150 | cx += w + space; |
| 151 | } |
| 152 | if !para.ends_with(' ') && cx > x { |
| 153 | cx -= space; |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | cy - y + line_h |
| 158 | } |
| 159 | |
| 160 | impl App for ClearwakeWindow { |
| 161 | fn draw(&mut self, cv: &mut Canvas, th: &Theme) { |
| 162 | let (w, h) = (cv.w, cv.h); |
| 163 | self.view_h = h - BAR_H - 34; |
| 164 | cv.clear(th.bg); |
| 165 | self.hits.clear(); |
| 166 | |
| 167 | // Page. |
| 168 | let col = (w - 48).min(COLUMN); |
| 169 | let x = (w - col) / 2; |
| 170 | let top = BAR_H; |
| 171 | let old = cv.push_clip(Rect::new(0, top, w, h - top - 34)); |
| 172 | let mut y = top + 28 - self.scroll; |
| 173 | let body = &font::SERIF_21; |
| 174 | for b in &self.doc.blocks { |
| 175 | match b { |
| 176 | Block::Heading { level, spans } => { |
| 177 | let f = match level { |
| 178 | 1 => &font::SERIF_36_SEMI, |
| 179 | 2 => &font::SERIF_26_SEMI, |
| 180 | _ => &font::SANS_20_SEMI, |
| 181 | }; |
| 182 | y += if *level <= 2 { 18 } else { 10 }; |
| 183 | y += flow(cv, &mut self.hits, spans, x, y, col, f, f, th, th.ink) + 4; |
| 184 | } |
| 185 | Block::Paragraph { spans, quote } => { |
| 186 | y += 8; |
| 187 | if *quote { |
| 188 | let hgt = flow(cv, &mut self.hits, spans, x + 18, y, col - 18, &font::SERIF_21_ITALIC, &font::SERIF_21_ITALIC, th, th.ink2); |
| 189 | cv.fill(Rect::new(x, y, 3, hgt), th.line); |
| 190 | y += hgt; |
| 191 | } else { |
| 192 | y += flow(cv, &mut self.hits, spans, x, y, col, body, &font::SANS_17_SEMI, th, th.ink); |
| 193 | } |
| 194 | } |
| 195 | Block::ListItem { depth, marker, spans } => { |
| 196 | let indent = 22 * (*depth as i32).clamp(1, 6); |
| 197 | font::SANS_17.draw_top(cv, x + indent - 18, y + 2, marker, th.ink3); |
| 198 | y += flow(cv, &mut self.hits, spans, x + indent + 8, y, col - indent - 8, body, &font::SANS_17_SEMI, th, th.ink) + 2; |
| 199 | } |
| 200 | Block::Code(code) => { |
| 201 | y += 8; |
| 202 | let lines: Vec<&str> = code.lines().collect(); |
| 203 | let lh = font::MONO_16.line() + 4; |
| 204 | let box_h = lines.len() as i32 * lh + 24; |
| 205 | cv.rrect(Rect::new(x, y, col, box_h), 10, th.inset); |
| 206 | for (i, l) in lines.iter().enumerate() { |
| 207 | font::MONO_16.draw_top(cv, x + 14, y + 12 + i as i32 * lh, l, th.ink); |
| 208 | } |
| 209 | y += box_h + 8; |
| 210 | } |
| 211 | Block::Image { alt, width, height, .. } => { |
| 212 | // Pictures need a decoder the kernel doesn't have yet; show where they go. |
| 213 | let iw = width.map(|v| (v as i32).min(col)).unwrap_or(col); |
| 214 | let ih = match (width, height) { |
| 215 | (Some(w0), Some(h0)) if *w0 > 0 => (*h0 as i32) * iw / (*w0 as i32), |
| 216 | _ => 160, |
| 217 | } |
| 218 | .clamp(60, 420); |
| 219 | y += 8; |
| 220 | cv.rrect(Rect::new(x, y, iw, ih), 10, th.card); |
| 221 | cv.rrect_stroke(Rect::new(x, y, iw, ih), 10, 1, th.line); |
| 222 | let label = if alt.is_empty() { "Picture" } else { alt.as_str() }; |
| 223 | font::SANS_15.draw_centered(cv, x, iw, y + ih / 2 + 5, &font::SANS_15.ellipsize(label, iw - 24), th.ink3); |
| 224 | y += ih + 8; |
| 225 | } |
| 226 | Block::Rule => { |
| 227 | y += 12; |
| 228 | cv.fill(Rect::new(x, y, col, 1), th.line); |
| 229 | y += 12; |
| 230 | } |
| 231 | Block::Form(f) => { |
| 232 | y += 8; |
| 233 | let label = if f.placeholder.is_empty() { "Search" } else { f.placeholder.as_str() }; |
| 234 | cv.rrect(Rect::new(x, y, col, 40), 20, th.card); |
| 235 | cv.rrect_stroke(Rect::new(x, y, col, 40), 20, 1, th.line); |
| 236 | font::SANS_17.draw_top(cv, x + 18, y + 10, label, th.ink3); |
| 237 | y += 48; |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | self.content_h = y + self.scroll - top + 40; |
| 242 | cv.set_clip(old); |
| 243 | |
| 244 | // Address bar. |
| 245 | cv.fill(Rect::new(0, 0, w, BAR_H), th.sidebar); |
| 246 | cv.fill(Rect::new(0, BAR_H - 1, w, 1), th.sidebar_line); |
| 247 | let field = Rect::new(20, 12, w - 40, 40); |
| 248 | cv.rrect(field, 20, th.card); |
| 249 | cv.rrect_stroke(field, 20, if self.editing { 2 } else { 1 }, if self.editing { th.accent } else { th.line }); |
| 250 | let shown = if self.address.is_empty() { "Search or type a web address" } else { self.address.as_str() }; |
| 251 | let tc = if self.address.is_empty() { th.ink3 } else { th.ink }; |
| 252 | let text = font::SANS_17.ellipsize(shown, field.w - 40); |
| 253 | let end = font::SANS_17.draw_top(cv, field.x + 20, field.y + 10, &text, tc); |
| 254 | if self.editing { |
| 255 | let cx = if self.address.is_empty() { field.x + 20 } else { end + 1 }; |
| 256 | cv.fill(Rect::new(cx, field.y + 9, 2, 22), th.accent); |
| 257 | } |
| 258 | |
| 259 | // Status line. |
| 260 | cv.fill(Rect::new(0, h - 34, w, 34), th.sidebar); |
| 261 | cv.fill(Rect::new(0, h - 34, w, 1), th.sidebar_line); |
| 262 | cv.circle(26, h - 17, 5, th.good); |
| 263 | font::SANS_15.draw_top(cv, 40, h - 27, &self.status, th.ink2); |
| 264 | } |
| 265 | |
| 266 | fn event(&mut self, ev: &WinEvent, _cx: &mut Ctx) -> bool { |
| 267 | let max_scroll = (self.content_h - self.view_h).max(0); |
| 268 | match *ev { |
| 269 | WinEvent::Press { x, y, button: 0 } => { |
| 270 | if y < BAR_H { |
| 271 | self.editing = true; |
| 272 | return true; |
| 273 | } |
| 274 | self.editing = false; |
| 275 | if let Some(h) = self.hits.iter().find(|h| h.rect.contains(x, y)) { |
| 276 | let href = h.href.clone(); |
| 277 | self.open(&href); |
| 278 | } |
| 279 | true |
| 280 | } |
| 281 | WinEvent::Scroll { dy, .. } => { |
| 282 | self.scroll = (self.scroll + dy * 40).clamp(0, max_scroll); |
| 283 | true |
| 284 | } |
| 285 | WinEvent::Key { key, mods } => { |
| 286 | if self.editing { |
| 287 | match key { |
| 288 | Key::Char(c) if !mods.ctrl && !mods.meta => self.address.push(c), |
| 289 | Key::Backspace => { |
| 290 | self.address.pop(); |
| 291 | } |
| 292 | Key::Enter => { |
| 293 | self.editing = false; |
| 294 | let a = self.address.clone(); |
| 295 | self.open(&a); |
| 296 | } |
| 297 | Key::Escape => self.editing = false, |
| 298 | _ => return false, |
| 299 | } |
| 300 | return true; |
| 301 | } |
| 302 | let step = match key { |
| 303 | Key::Down => 40, |
| 304 | Key::Up => -40, |
| 305 | Key::PageDown | Key::Char(' ') => self.view_h - 60, |
| 306 | Key::PageUp => -(self.view_h - 60), |
| 307 | Key::Home => -self.scroll, |
| 308 | Key::End => max_scroll, |
| 309 | Key::Char('l') if mods.ctrl || mods.meta => { |
| 310 | self.editing = true; |
| 311 | return true; |
| 312 | } |
| 313 | _ => return false, |
| 314 | }; |
| 315 | self.scroll = (self.scroll + step).clamp(0, max_scroll); |
| 316 | true |
| 317 | } |
| 318 | _ => false, |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | fn wants_ticks(&self) -> bool { |
| 323 | false |
| 324 | } |
| 325 | } |