11// Dev only: Scout searching in its browser in the fake preview, so the
2- // browser panel has something live to show. The "page" is drawn on a
3- // canvas and sent as JPEG frames, like the daemon's screencast.
2+ // browser panel has something live to show: three tabs, the search in the
3+ // active one. The "page" is drawn on a canvas, the size the panel asks for,
4+ // and sent as JPEG frames, like the daemon's screencast.
45
56import type { FakeBotloft } from "../lib/fake" ;
7+ import type { PageSize } from "../lib/fakePages" ;
68import type { BotId } from "../lib/protocol.gen" ;
9+ import { activeTab , drawPage } from "./seedPage" ;
710
8- const W = 1280 ;
9- const H = 800 ;
11+ const SITE = "https://recipes.example/" ;
12+
13+ const RECIPES = [ "Classic sourdough loaf" , "Starter in 5 days" , "No-knead rye" , "Focaccia" ] ;
1014
1115interface Scene {
1216 url : string ;
@@ -15,16 +19,16 @@ interface Scene {
1519 results : boolean ;
1620}
1721
18- function draw ( scene : Scene ) : string {
22+ function draw ( scene : Scene , size : PageSize ) : string {
1923 const canvas = document . createElement ( "canvas" ) ;
20- canvas . width = W ;
21- canvas . height = H ;
24+ canvas . width = size . width ;
25+ canvas . height = size . height ;
2226 const g = canvas . getContext ( "2d" ) ;
2327 if ( ! g ) {
2428 return "" ;
2529 }
2630 g . fillStyle = "#ffffff" ;
27- g . fillRect ( 0 , 0 , W , H ) ;
31+ g . fillRect ( 0 , 0 , size . width , size . height ) ;
2832 g . fillStyle = "#1f2937" ;
2933 g . font = "600 34px system-ui, sans-serif" ;
3034 g . fillText ( scene . results ? "Results" : "Find a recipe" , 120 , 150 ) ;
@@ -43,15 +47,12 @@ function draw(scene: Scene): string {
4347 g . fillStyle = "#ffffff" ;
4448 g . fillText ( "Search" , 938 , 226 ) ;
4549 if ( scene . results ) {
46- for ( let row = 0 ; row < 4 ; row ++ ) {
50+ // A taller page shows more of the list.
51+ for ( let row = 0 ; 300 + row * 110 < size . height - 60 ; row ++ ) {
4752 const y = 300 + row * 110 ;
4853 g . fillStyle = "#1d4ed8" ;
4954 g . font = "600 24px system-ui, sans-serif" ;
50- g . fillText (
51- [ "Classic sourdough loaf" , "Starter in 5 days" , "No-knead rye" , "Focaccia" ] [ row ] ?? "" ,
52- 120 ,
53- y ,
54- ) ;
55+ g . fillText ( RECIPES [ row % RECIPES . length ] ?? "" , 120 , y ) ;
5556 g . fillStyle = "#e5e7eb" ;
5657 g . fillRect ( 120 , y + 18 , 820 , 14 ) ;
5758 g . fillRect ( 120 , y + 42 , 560 , 14 ) ;
@@ -60,10 +61,10 @@ function draw(scene: Scene): string {
6061 return canvas . toDataURL ( "image/jpeg" , 0.7 ) . split ( "," ) [ 1 ] ?? "" ;
6162}
6263
63- /** Scout searches for a recipe, over and over. */
64+ /** Scout searches for a recipe, over and over, while the browser is its . */
6465export function seedBrowser ( fake : FakeBotloft , scout : BotId ) : void {
6566 const home : Scene = {
66- url : "https://recipes.example/" ,
67+ url : SITE ,
6768 title : "Recipes" ,
6869 query : "" ,
6970 results : false ,
@@ -116,15 +117,25 @@ export function seedBrowser(fake: FakeBotloft, scout: BotId): void {
116117 ) ;
117118 let scene = home ;
118119 let step = 0 ;
119- fake . browser . open ( scout , home . url , home . title ) ;
120- fake . browser . frame ( scout , draw ( scene ) ) ;
120+ // Two pages it opened on the way, then the one it works in.
121+ fake . browser . open ( scout , "https://bakers.example/forum/hydration" , "Hydration tips · Bakers" ) ;
122+ fake . browser . openTab ( scout , "https://mill.example/flour/rye" , "Rye flour · The Mill" ) ;
123+ fake . browser . openTab ( scout , home . url , home . title ) ;
124+ fake . browser . paint ( scout , ( size ) => {
125+ const tab = activeTab ( fake , scout ) ;
126+ return tab ?. url . startsWith ( SITE ) ? draw ( scene , size ) : drawPage ( tab , size ) ;
127+ } ) ;
121128 setInterval ( ( ) => {
129+ // In the owner's hands, the bot waits.
130+ if ( fake . browser . state ( scout ) . control === "owner" ) {
131+ return ;
132+ }
122133 const next = steps [ step % steps . length ] ;
123134 step += 1 ;
124135 if ( next ) {
125136 scene = next ( scene ) ;
126137 fake . browser . open ( scout , scene . url , scene . title ) ;
127- fake . browser . frame ( scout , draw ( scene ) ) ;
138+ fake . browser . repaint ( scout ) ;
128139 }
129140 } , 2200 ) ;
130141}
0 commit comments