@@ -10,8 +10,9 @@ use crate::render::Shape;
1010use crate :: samples;
1111
1212use super :: common;
13+ use super :: sprites;
1314
14- const SHAPES : & [ Shape ] = & [ Shape :: Text ] ;
15+ const SHAPES : & [ Shape ] = & [ Shape :: Text , Shape :: PixelArt ] ;
1516
1617pub struct ClockDerivedFetcher ;
1718
@@ -70,6 +71,9 @@ impl RealtimeFetcher for ClockDerivedFetcher {
7071 fn sample_body ( & self , shape : Shape ) -> Option < Body > {
7172 match shape {
7273 Shape :: Text => Some ( samples:: text ( "day 113 of 2026" ) ) ,
74+ // Pick the most recognizable sprite (Full moon) so docs previews lead with a
75+ // visually striking sample rather than an empty New-moon disk.
76+ Shape :: PixelArt => Some ( Body :: PixelArt ( sprites:: moon_sprite ( 4 , "Full" ) ) ) ,
7377 _ => None ,
7478 }
7579 }
@@ -79,11 +83,17 @@ impl RealtimeFetcher for ClockDerivedFetcher {
7983 Err ( msg) => return common:: placeholder ( & msg) ,
8084 } ;
8185 let now = common:: now_in ( opts. timezone . as_deref ( ) ) ;
82- let line = invoke (
83- & now,
84- opts. kind . unwrap_or ( Kind :: TimeOfDay ) ,
85- opts. hemisphere . unwrap_or_default ( ) ,
86- ) ;
86+ let kind = opts. kind . unwrap_or ( Kind :: TimeOfDay ) ;
87+ let hemisphere = opts. hemisphere . unwrap_or_default ( ) ;
88+ if matches ! ( ctx. shape, Some ( Shape :: PixelArt ) ) {
89+ return Payload {
90+ icon : None ,
91+ status : None ,
92+ format : None ,
93+ body : Body :: PixelArt ( sprite_for_kind ( & now, kind, hemisphere) ) ,
94+ } ;
95+ }
96+ let line = invoke ( & now, kind, hemisphere) ;
8797 Payload {
8898 icon : None ,
8999 status : None ,
@@ -93,6 +103,52 @@ impl RealtimeFetcher for ClockDerivedFetcher {
93103 }
94104}
95105
106+ /// Pick a sprite for the kind the user configured. Only moon-phase and season have visual
107+ /// translations today; numeric / textual kinds (`iso_week`, `day_of_year`, `julian_day`,
108+ /// `unix_epoch`, `time_of_day`, `rokuyou`, `jp_season`, `zodiac`, `chinese_zodiac`) fall
109+ /// through to a labelled placeholder so the misconfiguration is visible rather than silent.
110+ fn sprite_for_kind (
111+ now : & DateTime < FixedOffset > ,
112+ kind : Kind ,
113+ hemisphere : Hemisphere ,
114+ ) -> crate :: payload:: PixelArtData {
115+ match kind {
116+ Kind :: MoonPhase => {
117+ let idx = moon_phase_index ( now) ;
118+ sprites:: moon_sprite ( idx, MOON_LABELS [ idx] )
119+ }
120+ Kind :: Season => sprites:: season_sprite ( & season ( now, hemisphere) ) ,
121+ other => sprites:: placeholder ( kind_label ( other) ) ,
122+ }
123+ }
124+
125+ fn kind_label ( k : Kind ) -> & ' static str {
126+ match k {
127+ Kind :: TimeOfDay => "time_of_day" ,
128+ Kind :: MoonPhase => "moon_phase" ,
129+ Kind :: Zodiac => "zodiac" ,
130+ Kind :: ChineseZodiac => "chinese_zodiac" ,
131+ Kind :: Season => "season" ,
132+ Kind :: JpSeason => "jp_season" ,
133+ Kind :: Rokuyou => "rokuyou" ,
134+ Kind :: IsoWeek => "iso_week" ,
135+ Kind :: DayOfYear => "day_of_year" ,
136+ Kind :: JulianDay => "julian_day" ,
137+ Kind :: UnixEpoch => "unix_epoch" ,
138+ }
139+ }
140+
141+ const MOON_LABELS : [ & str ; 8 ] = [
142+ "New" ,
143+ "Waxing Crescent" ,
144+ "First Quarter" ,
145+ "Waxing Gibbous" ,
146+ "Full" ,
147+ "Waning Gibbous" ,
148+ "Last Quarter" ,
149+ "Waning Crescent" ,
150+ ] ;
151+
96152/// Dispatch a single kind against an already-resolved `now`. Shared with the
97153/// `clock_almanac` rollup so both fetchers stay in sync on formatting.
98154pub ( crate ) fn invoke ( now : & DateTime < FixedOffset > , kind : Kind , hemisphere : Hemisphere ) -> String {
@@ -120,8 +176,10 @@ fn time_of_day(now: &DateTime<FixedOffset>) -> &'static str {
120176 }
121177}
122178
123- fn moon_phase ( now : & DateTime < FixedOffset > ) -> String {
124- // Conway's moon-phase approximation. Accuracy ±1 day, adequate for a splash widget.
179+ /// Conway's moon-phase approximation expressed as an index 0..=7. Shared with the
180+ /// `PixelArt` shape so the sprite stays consistent with the text label. ±1 day accuracy,
181+ /// adequate for a splash widget.
182+ pub ( crate ) fn moon_phase_index ( now : & DateTime < FixedOffset > ) -> usize {
125183 let ( mut year, month, day) = ( now. year ( ) , now. month ( ) as i32 , now. day ( ) as i32 ) ;
126184 let m = if month < 3 {
127185 year -= 1 ;
@@ -136,18 +194,13 @@ fn moon_phase(now: &DateTime<FixedOffset>) -> String {
136194 + c
137195 - 37.5 ;
138196 let phase = ( ( jdn - 2_451_550.1 ) / 29.530_588_853 ) . rem_euclid ( 1.0 ) ;
139- let idx = ( phase * 8.0 ) . floor ( ) as usize % 8 ;
140- let ( glyph, name) = match idx {
141- 0 => ( "🌑" , "New" ) ,
142- 1 => ( "🌒" , "Waxing Crescent" ) ,
143- 2 => ( "🌓" , "First Quarter" ) ,
144- 3 => ( "🌔" , "Waxing Gibbous" ) ,
145- 4 => ( "🌕" , "Full" ) ,
146- 5 => ( "🌖" , "Waning Gibbous" ) ,
147- 6 => ( "🌗" , "Last Quarter" ) ,
148- _ => ( "🌘" , "Waning Crescent" ) ,
149- } ;
150- format ! ( "{glyph} {name}" )
197+ ( phase * 8.0 ) . floor ( ) as usize % 8
198+ }
199+
200+ fn moon_phase ( now : & DateTime < FixedOffset > ) -> String {
201+ let idx = moon_phase_index ( now) ;
202+ let glyph = [ "🌑" , "🌒" , "🌓" , "🌔" , "🌕" , "🌖" , "🌗" , "🌘" ] [ idx] ;
203+ format ! ( "{glyph} {}" , MOON_LABELS [ idx] )
151204}
152205
153206fn zodiac ( now : & DateTime < FixedOffset > ) -> String {
@@ -477,4 +530,69 @@ mod tests {
477530 assert ! ( !d. value. is_empty( ) ) ;
478531 }
479532 }
533+
534+ fn pixel_ctx ( opts : & str ) -> FetchContext {
535+ FetchContext {
536+ widget_id : "d" . into ( ) ,
537+ timeout : Duration :: from_secs ( 1 ) ,
538+ shape : Some ( Shape :: PixelArt ) ,
539+ options : Some ( toml:: from_str ( opts) . unwrap ( ) ) ,
540+ ..Default :: default ( )
541+ }
542+ }
543+
544+ #[ test]
545+ fn pixel_art_moon_phase_emits_16x16_sprite_with_label ( ) {
546+ let p = ClockDerivedFetcher . compute ( & pixel_ctx ( "kind = \" moon_phase\" " ) ) ;
547+ let Body :: PixelArt ( d) = p. body else {
548+ panic ! ( "expected PixelArt" ) ;
549+ } ;
550+ assert_eq ! ( d. pixels. len( ) , 16 ) ;
551+ assert_eq ! ( d. pixels[ 0 ] . len( ) , 16 ) ;
552+ let label = d. label . expect ( "sprite carries phase label" ) ;
553+ assert ! ( MOON_LABELS . contains( & label. as_str( ) ) , "{label}" ) ;
554+ }
555+
556+ #[ test]
557+ fn pixel_art_season_uses_hemisphere_to_pick_sprite ( ) {
558+ let p =
559+ ClockDerivedFetcher . compute ( & pixel_ctx ( "kind = \" season\" \n hemisphere = \" south\" " ) ) ;
560+ let Body :: PixelArt ( d) = p. body else {
561+ panic ! ( "expected PixelArt" ) ;
562+ } ;
563+ assert_eq ! ( d. pixels. len( ) , 16 ) ;
564+ let label = d. label . unwrap ( ) ;
565+ assert ! ( [ "Spring" , "Summer" , "Autumn" , "Winter" ] . contains( & label. as_str( ) ) ) ;
566+ }
567+
568+ #[ test]
569+ fn pixel_art_unsupported_kind_falls_back_to_labelled_placeholder ( ) {
570+ let p = ClockDerivedFetcher . compute ( & pixel_ctx ( "kind = \" iso_week\" " ) ) ;
571+ let Body :: PixelArt ( d) = p. body else {
572+ panic ! ( "expected PixelArt" ) ;
573+ } ;
574+ // 1×1 transparent placeholder; the meaningful signal is in the label.
575+ assert_eq ! ( d. pixels. len( ) , 1 ) ;
576+ assert_eq ! ( d. pixels[ 0 ] . len( ) , 1 ) ;
577+ let label = d. label . unwrap ( ) ;
578+ assert ! ( label. contains( "iso_week" ) , "label = {label}" ) ;
579+ }
580+
581+ #[ test]
582+ fn moon_phase_index_matches_text_label_table_indices ( ) {
583+ let now = at ( 2026 , 1 , 21 , 0 ) ;
584+ let idx = moon_phase_index ( & now) ;
585+ let text = moon_phase ( & now) ;
586+ assert ! ( text. contains( MOON_LABELS [ idx] ) ) ;
587+ }
588+
589+ #[ test]
590+ fn sample_body_for_pixel_art_returns_full_moon_sprite ( ) {
591+ let body = ClockDerivedFetcher . sample_body ( Shape :: PixelArt ) . unwrap ( ) ;
592+ let Body :: PixelArt ( d) = body else {
593+ panic ! ( "expected PixelArt" ) ;
594+ } ;
595+ assert_eq ! ( d. label. as_deref( ) , Some ( "Full" ) ) ;
596+ assert_eq ! ( d. pixels. len( ) , 16 ) ;
597+ }
480598}
0 commit comments