@@ -308,25 +308,41 @@ func (s *Skeleton) deletePage(key string) {
308308
309309// AddWidget adds a new widget to the Skeleton.
310310func (s * Skeleton ) AddWidget (key string , value string ) * Skeleton {
311- s .widget .AddWidget (key , value )
311+ go func () {
312+ s .updateChan <- AddNewWidget {
313+ Key : key ,
314+ Value : value ,
315+ }
316+ }()
312317 return s
313318}
314319
315320// UpdateWidgetValue updates the Value content by the given key.
316321// Adds the widget if it doesn't exist.
317322func (s * Skeleton ) UpdateWidgetValue (key string , value string ) * Skeleton {
318- // if widget not exists, add it
319- if s .widget .GetWidget (key ) == nil {
320- s .widget .AddWidget (key , value )
321- }
323+ go func () {
324+ // if widget not exists, add it
325+ if s .widget .GetWidget (key ) == nil {
326+ s .AddWidget (key , value )
327+ }
328+
329+ s .updateChan <- UpdateWidgetContent {
330+ Key : key ,
331+ Value : value ,
332+ }
333+ }()
322334
323- s .widget .UpdateWidgetValue (key , value )
324335 return s
325336}
326337
327338// DeleteWidget deletes the Value by the given key.
328339func (s * Skeleton ) DeleteWidget (key string ) * Skeleton {
329- s .widget .DeleteWidget (key )
340+ go func () {
341+ s .updateChan <- DeleteWidget {
342+ Key : key ,
343+ }
344+ }()
345+
330346 return s
331347}
332348
@@ -364,6 +380,37 @@ func (s *Skeleton) IAMActivePageCmd() tea.Cmd {
364380 }
365381}
366382
383+ func (s * Skeleton ) switchPage (cmds []tea.Cmd , position string ) []tea.Cmd {
384+ switch position {
385+ case "left" :
386+ if ! s .IsTabsLocked () {
387+ s .currentTab = max (s .currentTab - 1 , 0 )
388+ cmds = append (cmds , s .IAMActivePageCmd ())
389+ }
390+ case "right" :
391+ if ! s .IsTabsLocked () {
392+ s .currentTab = min (s .currentTab + 1 , len (s .pages )- 1 )
393+ cmds = append (cmds , s .IAMActivePageCmd ())
394+ }
395+ }
396+
397+ return cmds
398+ }
399+
400+ func (s * Skeleton ) updateSkeleton (msg tea.Msg , cmd tea.Cmd , cmds []tea.Cmd ) []tea.Cmd {
401+ s .header , cmd = s .header .Update (msg )
402+ cmds = append (cmds , cmd )
403+
404+ s .widget , cmd = s .widget .Update (msg )
405+ cmds = append (cmds , cmd )
406+
407+ s .pages [s .currentTab ], cmd = s .pages [s .currentTab ].Update (msg )
408+ cmds = append (cmds , cmd )
409+
410+ cmds = append (cmds , s .Listen ()) // listen to the update channel
411+ return cmds
412+ }
413+
367414func (s * Skeleton ) Init () tea.Cmd {
368415 if len (s .pages ) == 0 {
369416 panic ("skeleton: no pages added, please add at least one page" )
@@ -387,47 +434,41 @@ func (s *Skeleton) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
387434 }
388435 s .viewport .Width = msg .Width
389436 s .viewport .Height = msg .Height
437+
438+ cmds = s .updateSkeleton (msg , cmd , cmds )
390439 case tea.KeyMsg :
391440 switch {
392441 case key .Matches (msg , s .KeyMap .Quit ):
393442 return s , tea .Quit
394443 case key .Matches (msg , s .KeyMap .SwitchTabLeft ):
395- if ! s .IsTabsLocked () {
396- s .currentTab = max (s .currentTab - 1 , 0 )
397- cmds = append (cmds , s .IAMActivePageCmd ())
398- }
444+ cmds = s .switchPage (cmds , "left" )
399445 case key .Matches (msg , s .KeyMap .SwitchTabRight ):
400- if ! s .IsTabsLocked () {
401- s .currentTab = min (s .currentTab + 1 , len (s .pages )- 1 )
402- cmds = append (cmds , s .IAMActivePageCmd ())
403- }
446+ cmds = s .switchPage (cmds , "right" )
404447 }
448+ cmds = s .updateSkeleton (msg , cmd , cmds )
405449 case AddPage :
406450 cmds = append (cmds , msg .Page .Init ()) // init the page
451+ cmds = s .updateSkeleton (msg , cmd , cmds )
407452 case UpdatePageTitle :
408453 s .updatePageTitle (msg .Key , msg .Title )
454+ cmds = s .updateSkeleton (msg , cmd , cmds )
409455 case DeletePage :
410456 s .deletePage (msg .Key )
411457 cmds = append (cmds , s .IAMActivePageCmd ())
458+ cmds = s .updateSkeleton (msg , cmd , cmds )
412459 case DummyMsg :
413460 // do nothing, just to trigger the update
461+ cmds = s .updateSkeleton (msg , cmd , cmds )
414462 case HeaderSizeMsg :
415463 s .termSizeNotEnoughToHandleHeaders = msg .NotEnoughToHandleHeaders
416464 case WidgetSizeMsg :
417465 s .termSizeNotEnoughToHandleWidgets = msg .NotEnoughToHandleWidgets
466+ case AddNewWidget , UpdateWidgetContent , DeleteWidget :
467+ cmds = s .updateSkeleton (msg , cmd , cmds )
468+ default :
469+ cmds = s .updateSkeleton (msg , cmd , cmds )
418470 }
419471
420- s .header , cmd = s .header .Update (msg )
421- cmds = append (cmds , cmd )
422-
423- s .widget , cmd = s .widget .Update (msg )
424- cmds = append (cmds , cmd )
425-
426- s .pages [s .currentTab ], cmd = s .pages [s .currentTab ].Update (msg )
427- cmds = append (cmds , cmd )
428-
429- cmds = append (cmds , s .Listen ()) // listen to the update channel
430-
431472 return s , tea .Batch (cmds ... )
432473}
433474
0 commit comments