diff options
author | Robin Jarry <robin@jarry.cc> | 2022-12-22 00:21:46 +0100 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2023-01-04 22:57:31 +0100 |
commit | 1eb4c2c1e79ea8d66d53caaca24dd1b8ba32b02f (patch) | |
tree | 958bbaa52d752ca748abf20d83a205a85fc925dd /widgets | |
parent | a9cba9b6e60ff4aec081d48b0d86ec175c3e8e99 (diff) | |
download | aerc-1eb4c2c1e79ea8d66d53caaca24dd1b8ba32b02f.tar.gz |
selector: implement dynamic height
Depending on the number of lines in the prompt text, allow the selector
dialog to grow in height.
Signed-off-by: Robin Jarry <robin@jarry.cc>
Acked-by: Tim Culverhouse <tim@timculverhouse.com>
Diffstat (limited to 'widgets')
-rw-r--r-- | widgets/selector.go | 20 |
1 files changed, 17 insertions, 3 deletions
diff --git a/widgets/selector.go b/widgets/selector.go index 66dfb8b5..deefdfbf 100644 --- a/widgets/selector.go +++ b/widgets/selector.go @@ -2,6 +2,7 @@ package widgets import ( "fmt" + "strings" "github.com/gdamore/tcell/v2" "github.com/mattn/go-runewidth" @@ -178,7 +179,7 @@ func NewSelectorDialog(title string, prompt string, options []string, focus int, sd := &SelectorDialog{ callback: cb, title: title, - prompt: prompt, + prompt: strings.TrimSpace(prompt), uiConfig: uiConfig, selector: NewSelector(options, focus, uiConfig).Chooser(true), } @@ -193,8 +194,21 @@ func (gp *SelectorDialog) Draw(ctx *ui.Context) { ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', defaultStyle) ctx.Fill(0, 0, ctx.Width(), 1, ' ', titleStyle) ctx.Printf(1, 0, titleStyle, "%s", gp.title) - ctx.Printf(1, 1, defaultStyle, gp.prompt) - gp.selector.Draw(ctx.Subcontext(1, 3, ctx.Width()-2, 1)) + var i int + lines := strings.Split(gp.prompt, "\n") + for i = 0; i < len(lines); i++ { + ctx.Printf(1, 2+i, defaultStyle, "%s", lines[i]) + } + gp.selector.Draw(ctx.Subcontext(1, ctx.Height()-1, ctx.Width()-2, 1)) +} + +func (gp *SelectorDialog) ContextHeight() (func(int) int, func(int) int) { + totalHeight := 2 // title + empty line + totalHeight += strings.Count(gp.prompt, "\n") + 1 + totalHeight += 2 // empty line + selector + start := func(h int) int { return h/2 - totalHeight/2 } + height := func(h int) int { return totalHeight } + return start, height } func (gp *SelectorDialog) Invalidate() { |