diff --git a/Plans/08 WebKit engine/S057-small-ui-bugs.md b/Plans/08 WebKit engine/S057-small-ui-bugs.md index 4c8c717..c37ee58 100644 --- a/Plans/08 WebKit engine/S057-small-ui-bugs.md +++ b/Plans/08 WebKit engine/S057-small-ui-bugs.md @@ -27,7 +27,7 @@ Bugs the user and S048/S056 found that are small but visible every day. - [x] Wheel over floating egui surfaces scrolls them, never the page; Customize sheet scrolls to its last row; regression smoke (wheel over the sheet, assert scroll offset; page scroll unchanged). - [x] Colour picker chips render (font fallback or no emoji). - [x] beforeunload on window close / quit through Nacre's dialog; smoke with a page that sets it. -- [ ] Site colour from the favicon (dominant vivid colour, unit-tested on a few icons: red, blue, monochrome → none), applied when Colour source = Website; works on WebKit; `colours.txt` smoke updated; shots of two coloured sites and one monochrome site read. +- [x] Site colour from the favicon (dominant vivid colour, unit-tested on a few icons: red, blue, monochrome → none), applied when Colour source = Website; works on WebKit; `colours.txt` smoke updated; shots of two coloured sites and one monochrome site read. - [ ] Hidden smokes launch, basics, input, chrome, newtab green; shots read. - [ ] PROGRESS entry, INDEX line, tracker, commit `S057: …`, push. @@ -41,4 +41,4 @@ Bugs the user and S048/S056 found that are small but visible every day. Which surfaces now take the wheel. ## Resume -Boxes 1-3 done (beforeunload: App::begin_shutdown/shutdown_answered/cancel_shutdown in app.rs, smoke unload-quit.txt). Next: box 4 favicon colour. +Boxes 1-4 done (favicon colour: theme::dominant_vivid, Tab.favicon_colour, smoke favicon-colour.txt needs the loopback server). Next: box 5 smokes, box 6 close out. diff --git a/src/actions.rs b/src/actions.rs index 45a71b1..4a232a8 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -1601,7 +1601,7 @@ impl App { match message.kind.as_str() { "ready" => {}, "meta" => { - let color = crate::theme::parse_css_color(&s(&data, "theme_color")).or_else(|| crate::theme::parse_css_color(&s(&data, "background"))); + let color = crate::theme::parse_css_color(&s(&data, "theme_color")); let icon = data.get("icons").and_then(|v| v.as_array()).and_then(|a| a.first()).map(|i| s(i, "href")).unwrap_or_default(); let changed; { @@ -1616,11 +1616,7 @@ impl App { } } if changed && self.shared.settings.borrow().theme.source == crate::store::settings::ColourSource::Website && self.windows[&w].active == i { - if let Some(mut win) = self.windows.remove(&w) { - self.style_window(&mut win); - self.windows.insert(w, win); - } - self.redraw(w); + self.restyle(w); } }, "media" => { diff --git a/src/app.rs b/src/app.rs index 7601490..187f9ef 100644 --- a/src/app.rs +++ b/src/app.rs @@ -635,6 +635,7 @@ impl App { if host_changed { tab.arrived_ms = util::now_ms(); tab.theme_color = None; + tab.favicon_colour = None; tab.site_name.clear(); tab.favicon = None; tab.favicon_png = None; @@ -898,6 +899,9 @@ impl App { if rgba.len() != (width * height * 4) as usize || width == 0 { return; } + let colour = theme::dominant_vivid(&rgba); + let changed = tab.favicon_colour != colour; + tab.favicon_colour = colour; let color = egui::ColorImage::from_rgba_unmultiplied([width as usize, height as usize], &rgba); tab.favicon = Some(ctx.load_texture(format!("favicon-{}", tab.id), color, egui::TextureOptions::LINEAR)); let mut png = Vec::new(); @@ -907,6 +911,18 @@ impl App { tab.favicon_png = Some(png); } win.redraw(); + if changed && self.shared.settings.borrow().theme.source == crate::store::settings::ColourSource::Website && self.windows[&w].active == i { + self.restyle(w); + } + } + + /// Rebuilds a window's palette (the site colour changed). + pub fn restyle(&mut self, w: WindowId) { + if let Some(mut win) = self.windows.remove(&w) { + self.style_window(&mut win); + self.windows.insert(w, win); + } + self.redraw(w); } /// A pop-up opened as a tab next to its opener (WebKit already made its view, related to the opener's). diff --git a/src/selftest.rs b/src/selftest.rs index 5315cc6..a18400d 100644 --- a/src/selftest.rs +++ b/src/selftest.rs @@ -882,7 +882,7 @@ impl App { "favicon": t.favicon.is_some(), "crashed": t.crashed, "can_back": t.can_go_back(), "can_forward": t.can_go_forward(), "history_len": t.back_forward.len(), "find": [t.find.index, t.find.count], "prompts": t.prompts.len(), "dialog": t.dialog.is_some(), "media": t.media.as_ref().map(|(m, _)| serde_json::json!({"title": m.title, "paused": m.paused, "duration": m.duration, "position": m.position})), - "theme_color": t.theme_color.map(crate::theme::to_hex), "pip": t.pip_size.is_some(), + "theme_color": t.theme_color.map(crate::theme::to_hex), "favicon_colour": t.favicon_colour.map(crate::theme::to_hex), "site_colour": t.site_colour().map(crate::theme::to_hex), "pip": t.pip_size.is_some(), }) }) .collect() diff --git a/src/tabs.rs b/src/tabs.rs index 18be1bd..488786e 100644 --- a/src/tabs.rs +++ b/src/tabs.rs @@ -186,8 +186,10 @@ pub struct Tab { pub upgraded_from: Option, /// The certificate warning on show; its "Open anyway" (`nacre://allowcert`) trusts this one. pub cert_error: Option, - /// Page details from META_JS. + /// Page details from META_JS: the `theme-color` meta. pub theme_color: Option, + /// The icon's dominant vivid colour (`theme::dominant_vivid`). + pub favicon_colour: Option, pub site_name: String, pub page_image: String, pub manifest: String, @@ -240,6 +242,7 @@ impl Tab { upgraded_from: None, cert_error: None, theme_color: None, + favicon_colour: None, site_name: String::new(), page_image: String::new(), manifest: String::new(), @@ -266,9 +269,10 @@ impl Tab { self.page.is_some() } - /// The colour the "Website" colour source follows: none on an internal page, even one shown over a web page. + /// The colour the "Website" colour source follows: the favicon's, else the page's `theme-color`; none on an + /// internal page, even one shown over a web page. pub fn site_colour(&self) -> Option { - if self.is_internal() { None } else { self.theme_color } + if self.is_internal() { None } else { self.favicon_colour.or(self.theme_color) } } pub fn loading(&self) -> bool { @@ -333,6 +337,8 @@ mod tests { tab.page = None; tab.theme_color = Some(egui::Color32::from_rgb(0xDE, 0x58, 0x33)); assert_eq!(tab.site_colour(), tab.theme_color); + tab.favicon_colour = Some(egui::Color32::from_rgb(0x30, 0x60, 0xE0)); + assert_eq!(tab.site_colour(), tab.favicon_colour); tab.page = Some(Page::Settings(String::new())); assert_eq!(tab.site_colour(), None); } diff --git a/src/theme.rs b/src/theme.rs index 6eb754c..e680669 100644 --- a/src/theme.rs +++ b/src/theme.rs @@ -30,6 +30,38 @@ pub fn parse_hex(text: &str) -> Option { } } +/// The most common vivid colour of an icon (RGBA, not premultiplied), for the "Website" colour source: pixels that +/// are transparent, grey, near-white or near-black are skipped, the rest are counted in 12 hue buckets and the +/// winning bucket is averaged. None when too little of the icon is vivid (a monochrome icon has no colour). +pub fn dominant_vivid(rgba: &[u8]) -> Option { + let mut buckets = [(0u32, [0u32; 3]); 12]; + let mut opaque = 0u32; + for px in rgba.chunks_exact(4) { + if px[3] < 128 { + continue; + } + opaque += 1; + let (r, g, b) = (px[0] as f32 / 255.0, px[1] as f32 / 255.0, px[2] as f32 / 255.0); + let (max, min) = (r.max(g).max(b), r.min(g).min(b)); + let delta = max - min; + if max < 0.25 || delta / max < 0.35 { + continue; + } + let hue = if max == r { ((g - b) / delta).rem_euclid(6.0) } else if max == g { (b - r) / delta + 2.0 } else { (r - g) / delta + 4.0 }; + let bucket = &mut buckets[(hue * 2.0) as usize % 12]; + bucket.0 += 1; + for (sum, c) in bucket.1.iter_mut().zip(&px[..3]) { + *sum += *c as u32; + } + } + let (count, sum) = buckets.iter().max_by_key(|b| b.0).copied()?; + // A few stray pixels (anti-aliasing) don't make a colour. + if count < 4 || count * 20 < opaque { + return None; + } + Some(Color32::from_rgb((sum[0] / count) as u8, (sum[1] / count) as u8, (sum[2] / count) as u8)) +} + /// "rgb(1, 2, 3)", "rgba(...)" or "#abc" from CSS. pub fn parse_css_color(text: &str) -> Option { let t = text.trim(); @@ -611,6 +643,29 @@ pub fn visuals(p: &Palette, corner_radius: u8) -> Visuals { #[cfg(test)] mod tests { + fn icon(pixels: &[([u8; 4], usize)]) -> Vec { + pixels.iter().flat_map(|(px, n)| std::iter::repeat_n(*px, *n)).flatten().collect() + } + + #[test] + fn favicon_colour_is_the_dominant_vivid_one() { + // Mostly red with some blue, white, black and transparent: red. + let red = icon(&[([220, 30, 30, 255], 60), ([30, 30, 220, 255], 10), ([255, 255, 255, 255], 20), ([0, 0, 0, 255], 20), ([0, 0, 0, 0], 40)]); + assert_eq!(dominant_vivid(&red), Some(Color32::from_rgb(220, 30, 30))); + let blue = icon(&[([30, 80, 230, 255], 50), ([220, 30, 30, 255], 10)]); + assert_eq!(dominant_vivid(&blue), Some(Color32::from_rgb(30, 80, 230))); + } + + #[test] + fn monochrome_favicon_has_no_colour() { + let mono = icon(&[([0, 0, 0, 255], 50), ([255, 255, 255, 255], 40), ([128, 128, 128, 255], 30), ([200, 30, 30, 0], 99)]); + assert_eq!(dominant_vivid(&mono), None); + // A few coloured edge pixels in a grey icon don't count either. + let edges = icon(&[([120, 120, 120, 255], 200), ([220, 30, 30, 255], 3)]); + assert_eq!(dominant_vivid(&edges), None); + assert_eq!(dominant_vivid(&[]), None); + } + use super::*; /// Laying out text renders glyphs (epaint 0.37.0 once panicked rasterizing them). diff --git a/tests/smoke/colours.txt b/tests/smoke/colours.txt index a4efe08..45545a5 100644 --- a/tests/smoke/colours.txt +++ b/tests/smoke/colours.txt @@ -1,3 +1,4 @@ +# S057: site colour is the favicon's dominant vivid colour, else this page's theme-color (tests/smoke/favicon-colour.txt covers icons). # R1-11: colours still change after the welcome setup, also when Settings is typed into a web tab whose page has a # theme-color. Hidden 1280x1100 layout (tap positions): SMOKE_SIZE=1280x1100, fresh outdir. Expect: site-* orange # chrome (#DE5833 seed); s-website NOT orange (an internal page has no site colour); s-teal teal; back-site orange diff --git a/tests/smoke/favicon-colour.txt b/tests/smoke/favicon-colour.txt new file mode 100644 index 0000000..ee56c4c --- /dev/null +++ b/tests/smoke/favicon-colour.txt @@ -0,0 +1,30 @@ +# Site colour from the favicon (S057): Colour source "Website" follows the icon's dominant vivid colour; a +# monochrome icon gives none. Offline, pages from a loopback server (a file:// page gets no favicon): +# python3 -m http.server 8765 --bind 127.0.0.1 --directory tests/smoke/pages & then tools/smoke.sh +# Expect: shot-red chrome red, shot-blue chrome blue, shot-mono the default colour (System accent / palette, no tint). +wait 3 +resize 1280 900 +set /theme/source "website" +set /theme/mode "light" +open http://127.0.0.1:8765/favicon-red.html +waitload 30 +wait 2 +status red +expect /active/favicon_colour "#D62828" +expect /active/site_colour "#D62828" +shot shot-red +open http://127.0.0.1:8765/favicon-blue.html +waitload 30 +wait 2 +status blue +expect /active/favicon_colour "#1E5ADC" +shot shot-blue +open http://127.0.0.1:8765/favicon-mono.html +waitload 30 +wait 2 +status mono +expect /active/favicon_colour null +expect /active/site_colour null +shot shot-mono +status done +quit diff --git a/tests/smoke/pages/favicon-blue.html b/tests/smoke/pages/favicon-blue.html new file mode 100644 index 0000000..c1abc89 --- /dev/null +++ b/tests/smoke/pages/favicon-blue.html @@ -0,0 +1 @@ +Favicon blueFavicon test: blue diff --git a/tests/smoke/pages/favicon-blue.png b/tests/smoke/pages/favicon-blue.png new file mode 100644 index 0000000..a78a782 Binary files /dev/null and b/tests/smoke/pages/favicon-blue.png differ diff --git a/tests/smoke/pages/favicon-mono.html b/tests/smoke/pages/favicon-mono.html new file mode 100644 index 0000000..cd530e2 --- /dev/null +++ b/tests/smoke/pages/favicon-mono.html @@ -0,0 +1 @@ +Favicon monoFavicon test: mono diff --git a/tests/smoke/pages/favicon-mono.png b/tests/smoke/pages/favicon-mono.png new file mode 100644 index 0000000..cb7c557 Binary files /dev/null and b/tests/smoke/pages/favicon-mono.png differ diff --git a/tests/smoke/pages/favicon-red.html b/tests/smoke/pages/favicon-red.html new file mode 100644 index 0000000..c99d247 --- /dev/null +++ b/tests/smoke/pages/favicon-red.html @@ -0,0 +1 @@ +Favicon redFavicon test: red diff --git a/tests/smoke/pages/favicon-red.png b/tests/smoke/pages/favicon-red.png new file mode 100644 index 0000000..6153931 Binary files /dev/null and b/tests/smoke/pages/favicon-red.png differ