//! Clearwake on Glimmer: the same page engine as every other Clearwake,
//! drawn as a native Glimmer window by Glimmer's own kernel.
//!
//! The kernel owns the network. When the person opens an address, the
//! window asks for it ([`ClearwakeWindow::take_request`]); the kernel loads
//! it (after the same filter check) and hands the page back with
//! [`ClearwakeWindow::show_page`]. Until the kernel's network stack is
//! wired in, the window shows its built-in welcome page.
#![no_std]
extern crate alloc;
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use clearwake_core::html::{self, Block, Document, Inline};
use clearwake_core::{Filter, Url};
use glimmer_ui::canvas::{Canvas, Rect};
use glimmer_ui::font::{self, Font};
use glimmer_ui::input::Key;
use glimmer_ui::theme::Theme;
use glimmer_ui::wm::{App, Ctx, WinEvent};
const BAR_H: i32 = 64;
const COLUMN: i32 = 760;
pub const WELCOME: &str = "
Clearwake\
Clearwake
Less noise. More web.
\
This is Clearwake running natively on Glimmer, drawn by Glimmer's own kernel. \
It reads pages into clean text, links and pictures. Scripts never run, and ads and trackers are blocked before they load.
\
Always on
- No ad blocking switch and no exceptions.
- No account. Bookmarks stay on this machine.
\
- The same Rust engine as Clearwake for Mac, Windows, Linux, Android and the web.
\
Type an address above and press Enter.
";
struct Hit {
rect: Rect,
href: String,
}
pub struct ClearwakeWindow {
filter: Filter,
doc: Document,
address: String,
editing: bool,
scroll: i32,
content_h: i32,
hits: Vec,
request: Option,
status: String,
view_h: i32,
}
impl ClearwakeWindow {
pub fn new() -> ClearwakeWindow {
let filter = Filter::new();
let url = Url::parse("about:home").unwrap();
let doc = html::parse(WELCOME, &url, &filter);
ClearwakeWindow {
filter,
doc,
address: String::new(),
editing: false,
scroll: 0,
content_h: 0,
hits: Vec::new(),
request: None,
status: String::from("Ad blocking is always on"),
view_h: 600,
}
}
/// The address the person asked for, if any. The kernel loads it.
pub fn take_request(&mut self) -> Option {
self.request.take()
}
/// Show a page the kernel loaded.
pub fn show_page(&mut self, url: &str, source: &str) {
let u = Url::parse(url).unwrap_or_else(|| Url::parse("about:blank").unwrap());
self.doc = html::parse(source, &u, &self.filter);
self.address = url.to_string();
self.scroll = 0;
let s = &self.doc.stats;
self.status = alloc::format!("Blocked {} ads and trackers ยท {} scripts never ran", s.blocked + s.hidden, s.scripts_skipped);
}
/// The kernel couldn't load the page.
pub fn show_error(&mut self, url: &str, message: &str) {
let page = alloc::format!("This page didn't open
{}
", message);
self.show_page(url, &page);
self.status = String::from(message);
}
fn open(&mut self, input: &str) {
match clearwake_core::resolve_input(input) {
Some(u) if self.filter.blocks(&u) => {
let s = u.to_string();
self.show_error(&s, "Clearwake blocked this address because it serves ads or tracking.");
}
Some(u) if u.is_about() => {
*self = ClearwakeWindow::new();
}
Some(u) => {
self.address = u.to_string();
self.status = alloc::format!("Loading {}", u.display_host());
self.request = Some(u.to_string());
}
None => {}
}
}
}
impl Default for ClearwakeWindow {
fn default() -> Self {
ClearwakeWindow::new()
}
}
/// Lay out and draw a run of styled words, wrapping at `max`. Returns the
/// height used. Link words are recorded as click targets.
#[allow(clippy::too_many_arguments)]
fn flow(cv: &mut Canvas, hits: &mut Vec, spans: &[Inline], x: i32, y: i32, max: i32, f: &Font, bold: &Font, th: &Theme, color: u32) -> i32 {
let line_h = f.line() + 6;
let space = f.width(" ");
let (mut cx, mut cy) = (x, y);
for sp in spans {
let face = if sp.bold { bold } else { f };
let c = if sp.link.is_some() { th.link } else { color };
let dy = f.ascent() - face.ascent();
for (n, para) in sp.text.split('\n').enumerate() {
if n > 0 {
cx = x;
cy += line_h;
}
let lead = para.starts_with(' ');
if lead && cx > x {
cx += space;
}
for word in para.split(' ').filter(|w| !w.is_empty()) {
let w = face.width(word);
if cx > x && cx + w > x + max {
cx = x;
cy += line_h;
}
face.draw_top(cv, cx, cy + dy, word, c);
if let Some(h) = &sp.link {
hits.push(Hit { rect: Rect::new(cx, cy, w + space, line_h), href: h.clone() });
cv.fill(Rect::new(cx, cy + f.line(), w, 1), c);
}
cx += w + space;
}
if !para.ends_with(' ') && cx > x {
cx -= space;
}
}
}
cy - y + line_h
}
impl App for ClearwakeWindow {
fn draw(&mut self, cv: &mut Canvas, th: &Theme) {
let (w, h) = (cv.w, cv.h);
self.view_h = h - BAR_H - 34;
cv.clear(th.bg);
self.hits.clear();
// Page.
let col = (w - 48).min(COLUMN);
let x = (w - col) / 2;
let top = BAR_H;
let old = cv.push_clip(Rect::new(0, top, w, h - top - 34));
let mut y = top + 28 - self.scroll;
let body = &font::SERIF_21;
for b in &self.doc.blocks {
match b {
Block::Heading { level, spans } => {
let f = match level {
1 => &font::SERIF_36_SEMI,
2 => &font::SERIF_26_SEMI,
_ => &font::SANS_20_SEMI,
};
y += if *level <= 2 { 18 } else { 10 };
y += flow(cv, &mut self.hits, spans, x, y, col, f, f, th, th.ink) + 4;
}
Block::Paragraph { spans, quote } => {
y += 8;
if *quote {
let hgt = flow(cv, &mut self.hits, spans, x + 18, y, col - 18, &font::SERIF_21_ITALIC, &font::SERIF_21_ITALIC, th, th.ink2);
cv.fill(Rect::new(x, y, 3, hgt), th.line);
y += hgt;
} else {
y += flow(cv, &mut self.hits, spans, x, y, col, body, &font::SANS_17_SEMI, th, th.ink);
}
}
Block::ListItem { depth, marker, spans } => {
let indent = 22 * (*depth as i32).clamp(1, 6);
font::SANS_17.draw_top(cv, x + indent - 18, y + 2, marker, th.ink3);
y += flow(cv, &mut self.hits, spans, x + indent + 8, y, col - indent - 8, body, &font::SANS_17_SEMI, th, th.ink) + 2;
}
Block::Code(code) => {
y += 8;
let lines: Vec<&str> = code.lines().collect();
let lh = font::MONO_16.line() + 4;
let box_h = lines.len() as i32 * lh + 24;
cv.rrect(Rect::new(x, y, col, box_h), 10, th.inset);
for (i, l) in lines.iter().enumerate() {
font::MONO_16.draw_top(cv, x + 14, y + 12 + i as i32 * lh, l, th.ink);
}
y += box_h + 8;
}
Block::Image { alt, width, height, .. } => {
// Pictures need a decoder the kernel doesn't have yet; show where they go.
let iw = width.map(|v| (v as i32).min(col)).unwrap_or(col);
let ih = match (width, height) {
(Some(w0), Some(h0)) if *w0 > 0 => (*h0 as i32) * iw / (*w0 as i32),
_ => 160,
}
.clamp(60, 420);
y += 8;
cv.rrect(Rect::new(x, y, iw, ih), 10, th.card);
cv.rrect_stroke(Rect::new(x, y, iw, ih), 10, 1, th.line);
let label = if alt.is_empty() { "Picture" } else { alt.as_str() };
font::SANS_15.draw_centered(cv, x, iw, y + ih / 2 + 5, &font::SANS_15.ellipsize(label, iw - 24), th.ink3);
y += ih + 8;
}
Block::Rule => {
y += 12;
cv.fill(Rect::new(x, y, col, 1), th.line);
y += 12;
}
Block::Form(f) => {
y += 8;
let label = if f.placeholder.is_empty() { "Search" } else { f.placeholder.as_str() };
cv.rrect(Rect::new(x, y, col, 40), 20, th.card);
cv.rrect_stroke(Rect::new(x, y, col, 40), 20, 1, th.line);
font::SANS_17.draw_top(cv, x + 18, y + 10, label, th.ink3);
y += 48;
}
}
}
self.content_h = y + self.scroll - top + 40;
cv.set_clip(old);
// Address bar.
cv.fill(Rect::new(0, 0, w, BAR_H), th.sidebar);
cv.fill(Rect::new(0, BAR_H - 1, w, 1), th.sidebar_line);
let field = Rect::new(20, 12, w - 40, 40);
cv.rrect(field, 20, th.card);
cv.rrect_stroke(field, 20, if self.editing { 2 } else { 1 }, if self.editing { th.accent } else { th.line });
let shown = if self.address.is_empty() { "Search or type a web address" } else { self.address.as_str() };
let tc = if self.address.is_empty() { th.ink3 } else { th.ink };
let text = font::SANS_17.ellipsize(shown, field.w - 40);
let end = font::SANS_17.draw_top(cv, field.x + 20, field.y + 10, &text, tc);
if self.editing {
let cx = if self.address.is_empty() { field.x + 20 } else { end + 1 };
cv.fill(Rect::new(cx, field.y + 9, 2, 22), th.accent);
}
// Status line.
cv.fill(Rect::new(0, h - 34, w, 34), th.sidebar);
cv.fill(Rect::new(0, h - 34, w, 1), th.sidebar_line);
cv.circle(26, h - 17, 5, th.good);
font::SANS_15.draw_top(cv, 40, h - 27, &self.status, th.ink2);
}
fn event(&mut self, ev: &WinEvent, _cx: &mut Ctx) -> bool {
let max_scroll = (self.content_h - self.view_h).max(0);
match *ev {
WinEvent::Press { x, y, button: 0 } => {
if y < BAR_H {
self.editing = true;
return true;
}
self.editing = false;
if let Some(h) = self.hits.iter().find(|h| h.rect.contains(x, y)) {
let href = h.href.clone();
self.open(&href);
}
true
}
WinEvent::Scroll { dy, .. } => {
self.scroll = (self.scroll + dy * 40).clamp(0, max_scroll);
true
}
WinEvent::Key { key, mods } => {
if self.editing {
match key {
Key::Char(c) if !mods.ctrl && !mods.meta => self.address.push(c),
Key::Backspace => {
self.address.pop();
}
Key::Enter => {
self.editing = false;
let a = self.address.clone();
self.open(&a);
}
Key::Escape => self.editing = false,
_ => return false,
}
return true;
}
let step = match key {
Key::Down => 40,
Key::Up => -40,
Key::PageDown | Key::Char(' ') => self.view_h - 60,
Key::PageUp => -(self.view_h - 60),
Key::Home => -self.scroll,
Key::End => max_scroll,
Key::Char('l') if mods.ctrl || mods.meta => {
self.editing = true;
return true;
}
_ => return false,
};
self.scroll = (self.scroll + step).clamp(0, max_scroll);
true
}
_ => false,
}
}
fn wants_ticks(&self) -> bool {
false
}
}