11/* clayterm.c — WASM terminal rendering engine for Clay UI
22 *
33 * Public API (exported to WASM):
4- * clayterm_size — compute arena size for given dimensions
5- * init — initialize a Clayterm instance in provided memory
6- * reduce — decode command buffer, run Clay layout, render to ANSI
7- * output — pointer to output byte buffer
8- * length — length of output byte buffer
9- * measure — Clay text measurement callback
4+ * clayterm_size — compute arena size for given dimensions
5+ * clayterm_init — initialize a Clayterm instance in provided memory
6+ * clayterm_resize — rebuild dimension-dependent storage in place
7+ * reduce — decode command buffer, run Clay layout, render to ANSI
8+ * output — pointer to output byte buffer
9+ * length — length of output byte buffer
10+ * measure — Clay text measurement callback
1011 * error_count, error_type, error_message_length, error_message_ptr
11- * — per-render Clay error accessors
12+ * — per-render Clay error accessors
1213 */
1314
1415#include "clayterm.h"
@@ -71,8 +72,8 @@ struct Clayterm {
7172 Clay_ErrorData errors [MAX_ERRORS ];
7273 int error_count ;
7374 int animating_count ;
74- /* CAP_ * bits pushed by the host via set_capabilities(); init() zeroes
75- * them, so the host re-pushes after every re-init */
75+ /* RENDER_CAP_ * bits pushed by the host via set_capabilities(), retained
76+ * across resize updates. */
7677 uint32_t caps ;
7778 /* Pointer shape (renderer-spec 7.10): hover_shape is what this frame's
7879 * hit test resolved, pointer_shape is what was last emitted. Both index
@@ -647,8 +648,8 @@ static int align64(int n) { return (n + 63) & ~63; }
647648 * with the grid instead of shipping Clay's fixed 8192-element default. 2x cells
648649 * bounds sane trees (leaves >= 1 cell, branching >= 2); the ceiling preserves
649650 * today's capacity for large grids, the floor keeps headroom on tiny ones.
650- * Must run before Clay_MinMemorySize in both clayterm_size and init so the
651- * size the host queries always matches the arena init consumes . */
651+ * Must run before Clay_MinMemorySize in both clayterm_size and
652+ * clayterm_resize so the queried size matches the arena setup . */
652653static void set_clay_capacity (int w , int h ) {
653654 int elems = w * h * 2 ;
654655 if (elems < 2048 )
@@ -720,41 +721,55 @@ int error_message_ptr(struct Clayterm *ct, int index) {
720721 return (int )ct -> errors [index ].errorText .chars ;
721722}
722723
723- struct Clayterm * init (void * mem , int w , int h ) {
724- set_clay_capacity (w , h );
724+ struct Clayterm * clayterm_init (void * mem , int w , int h ) {
725725 struct Clayterm * ct = (struct Clayterm * )mem ;
726+ * ct = (struct Clayterm ){0 };
727+ clayterm_resize (ct , w , h );
728+ return ct ;
729+ }
730+
731+ /* Rebuild viewport storage without resetting instance state such as
732+ * capabilities and the emitted pointer shape. */
733+ void clayterm_resize (struct Clayterm * ct , int w , int h ) {
734+ set_clay_capacity (w , h );
726735 int cell_count = w * h ;
727736 int cell_bytes = align8 (cell_count * (int )sizeof (Cell ));
728737 int out_bytes = align8 (cell_count * OUT_BYTES_PER_CELL + OUT_FRAME_OVERHEAD );
729- char * base = (char * )mem + align8 ((int )sizeof (struct Clayterm ));
738+ char * base = (char * )ct + align8 ((int )sizeof (struct Clayterm ));
739+
740+ ct -> w = w ;
741+ ct -> h = h ;
742+ ct -> front = (Cell * )base ;
743+ ct -> back = (Cell * )(base + cell_bytes );
744+ ct -> out = (Buffer ){base + cell_bytes * 2 , 0 ,
745+ cell_count * OUT_BYTES_PER_CELL + OUT_FRAME_OVERHEAD };
746+ ct -> lastfg = 0xffffffff ;
747+ ct -> lastbg = 0xffffffff ;
748+ ct -> lastx = -1 ;
749+ ct -> lasty = -1 ;
750+
751+ /* Discard results from the old layout. Traversal scratch is reset by
752+ * reduce() before use; the caret text pointer may refer to reused storage. */
753+ ct -> error_count = 0 ;
754+ ct -> animating_count = 0 ;
755+ ct -> caret_text_chars = NULL ;
756+ ct -> had_caret_last_frame = 0 ;
730757
731758 char * clay_mem = base + cell_bytes * 2 + out_bytes ;
732759 int clay_bytes = align64 ((int )Clay_MinMemorySize ());
733760 Clay_Arena arena =
734761 Clay_CreateArenaWithCapacityAndMemory (clay_bytes , clay_mem );
762+ /* Clay reads its previous context during initialization. Fill cells only
763+ * afterward: the resized cell buffers may overlap that old context. */
735764 Clay_Initialize (arena , (Clay_Dimensions ){(float )w , (float )h },
736765 (Clay_ErrorHandler ){clay_error , ct });
737766
738- * ct = (struct Clayterm ){
739- .w = w ,
740- .h = h ,
741- .front = (Cell * )base ,
742- .back = (Cell * )(base + cell_bytes ),
743- .out = {base + cell_bytes * 2 , 0 ,
744- cell_count * OUT_BYTES_PER_CELL + OUT_FRAME_OVERHEAD },
745- .lastfg = 0xffffffff ,
746- .lastbg = 0xffffffff ,
747- .lastx = -1 ,
748- .lasty = -1 ,
749- };
750-
751767 // initialize back buffer with spaces and default fg/bg
752768 cells_fill (ct -> back , w , h , ' ' , ATTR_DEFAULT , ATTR_DEFAULT );
753769
754770 // initialize front buffer with zeros. Every cell will be
755771 // redrawn on the first render.
756772 cells_fill (ct -> front , w , h , 0 , 0 , 0 );
757- return ct ;
758773}
759774
760775int set_capabilities (struct Clayterm * ct , uint32_t caps ) {
@@ -767,12 +782,6 @@ int set_capabilities(struct Clayterm *ct, uint32_t caps) {
767782 return restore ;
768783}
769784
770- int pointer_shape (struct Clayterm * ct ) { return ct -> pointer_shape ; }
771-
772- void set_pointer_shape (struct Clayterm * ct , int shape ) {
773- ct -> pointer_shape = shape ;
774- }
775-
776785void reduce (struct Clayterm * ct , uint32_t * buf , int len , int mode , int row ,
777786 float deltaTime , float px , float py , int pointer ) {
778787 int i = 0 ;
0 commit comments