Files
MujicaUI/chat/search_progress.go
T

98 lines
3.2 KiB
Go

package chat
import (
"fmt"
"log/slog"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/core"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/data"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/icons"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/theme"
)
// SearchStage describes one caller-owned search step.
type SearchStage struct {
Label string
Done bool
}
// SearchProgressOptions describe the caller's search.
type SearchProgressOptions struct {
// Query is the keyword searched; required.
Query string
// Read and Total count read and discovered sources.
Read, Total int
Stages []SearchStage
// Done folds progress into a summary users can reopen.
Done bool
}
// searchProgressState tracks completion and manual folding.
type searchProgressState struct {
open, done bool
}
// SearchProgress displays keywords, read counts and collapsible stages.
func SearchProgress(c *ui.Context, opts SearchProgressOptions) core.ChoiceResult {
if opts.Query == "" || opts.Read < 0 || opts.Total < 0 || opts.Read > opts.Total {
panic("mujica: SearchProgress needs a Query and 0 <= Read <= Total")
}
r := core.ChoiceResult{Element: ui.Column(c).MinWidth(0)}
st := ui.Local(r.Element, "search", func() searchProgressState { return searchProgressState{open: true} })
if opts.Done && !st.done {
// Finishing folds it once; the user may open it again.
st.open = false
slog.Debug("mujica: search_progress complete", "read", opts.Read)
}
if st.done && !opts.Done {
st.open = true
slog.Debug("mujica: search_progress restart")
}
st.done = opts.Done
title := fmt.Sprintf(core.Msg(c, "search_progress.searching"), opts.Query)
if opts.Done {
title = fmt.Sprintf(core.Msg(c, "search_progress.done"), opts.Query, opts.Read)
}
r.Element.Children(func() { r = core.NewChoiceResult(r.Element, searchProgressBody(c, st, title, opts)) })
if r.Changed() {
slog.Debug("mujica: search_progress fold", "open", st.open)
}
return r
}
// searchProgressBody is the collapsible header and the stages.
func searchProgressBody(c *ui.Context, st *searchProgressState, title string, opts SearchProgressOptions) bool {
k := core.Tokens(c)
u := core.Density(c).Unit()
small := core.FontSize(c, theme.CaptionSize)
return data.Collapsible(c, &st.open, data.CollapsibleOptions{Title: title, Trigger: func() {
ui.Row(c).Gap(u * 2).AlignItems(ui.Center).TextColor(k.TextMuted).Children(func() {
if opts.Done {
ui.Icon(c, icons.Must("search")).Size(14, 14)
} else {
core.Spinner(c, core.SpinnerOptions{Size: 14, Label: title})
}
ui.Text(c, title).FontSize(small).SingleLine()
})
}}, func() {
ui.Text(c, fmt.Sprintf(core.Msg(c, "search_progress.read"), opts.Read, opts.Total)).FontSize(small).FontFeatures("tnum").TextColor(k.TextMuted)
ui.Column(c).Gap(u).Role(ui.RoleList).Children(func() {
for _, s := range opts.Stages {
ui.Row(c).Gap(u * 2).AlignItems(ui.Center).Role(ui.RoleListItem).Children(func() {
if s.Done {
ui.Icon(c, icons.Must("check")).Size(14, 14).TextColor(k.Success)
} else {
ui.Icon(c, icons.Must("circle")).Size(14, 14).TextColor(k.TextMuted)
}
t := ui.Text(c, s.Label).FontSize(small)
if !s.Done {
t.TextColor(k.TextMuted)
}
})
}
})
}).Changed()
}