Skip to content

Commit 21194c4

Browse files
committed
Refactor the code
1 parent 15a8cb4 commit 21194c4

File tree

2 files changed

+40
-35
lines changed

2 files changed

+40
-35
lines changed

skeleton.go

+40-7
Original file line numberDiff line numberDiff line change
@@ -308,25 +308,41 @@ func (s *Skeleton) deletePage(key string) {
308308

309309
// AddWidget adds a new widget to the Skeleton.
310310
func (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.
317322
func (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.
328339
func (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,23 @@ 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+
367400
func (s *Skeleton) updateSkeleton(msg tea.Msg, cmd tea.Cmd, cmds []tea.Cmd) []tea.Cmd {
368401
s.header, cmd = s.header.Update(msg)
369402
cmds = append(cmds, cmd)

widget.go

-28
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,6 @@ func (w *widget) SetRightPadding(padding int) *widget {
102102
return w
103103
}
104104

105-
func (w *widget) AddWidget(key string, value string) {
106-
go func() {
107-
w.updateChan <- AddNewWidget{
108-
Key: key,
109-
Value: value,
110-
}
111-
}()
112-
}
113-
114105
// GetWidget returns the Value by the given key.
115106
func (w *widget) GetWidget(key string) *commonWidget {
116107
for _, widget := range w.widgets {
@@ -122,25 +113,6 @@ func (w *widget) GetWidget(key string) *commonWidget {
122113
return nil
123114
}
124115

125-
// UpdateWidgetValue updates the Value content by the given key.
126-
func (w *widget) UpdateWidgetValue(key string, value string) {
127-
go func() {
128-
w.updateChan <- UpdateWidgetContent{
129-
Key: key,
130-
Value: value,
131-
}
132-
}()
133-
}
134-
135-
// DeleteWidget deletes the Value by the given key.
136-
func (w *widget) DeleteWidget(key string) {
137-
go func() {
138-
w.updateChan <- DeleteWidget{
139-
Key: key,
140-
}
141-
}()
142-
}
143-
144116
// DeleteAllWidgets deletes all the widgets.
145117
func (w *widget) DeleteAllWidgets() {
146118
w.widgets = nil

0 commit comments

Comments
 (0)