Add menu prompt

This makes it possible to add a prompt to a menu. It will be shown above the
menu items, separated from them by a blank line.
This commit is contained in:
Stefan Haller 2024-06-11 15:49:19 +02:00
parent dbc21af3b1
commit 7e92dbfd3d
4 changed files with 63 additions and 4 deletions

View file

@ -373,7 +373,9 @@ func (self *ConfirmationHelper) resizeMenu() {
itemCount := self.c.Contexts().Menu.UnfilteredLen()
offset := 3
panelWidth := self.getPopupPanelWidth()
x0, y0, x1, y1 := self.getPopupPanelDimensionsForContentHeight(panelWidth, itemCount+offset)
contentWidth := panelWidth - 2 // minus 2 for the frame
promptLinesCount := self.layoutMenuPrompt(contentWidth)
x0, y0, x1, y1 := self.getPopupPanelDimensionsForContentHeight(panelWidth, itemCount+offset+promptLinesCount)
menuBottom := y1 - offset
_, _ = self.c.GocuiGui().SetView(self.c.Views().Menu.Name(), x0, y0, x1, menuBottom, 0)
@ -383,11 +385,39 @@ func (self *ConfirmationHelper) resizeMenu() {
if selectedItem != nil {
tooltip = self.TooltipForMenuItem(selectedItem)
}
contentWidth := panelWidth - 2 // minus 2 for the frame
tooltipHeight := getMessageHeight(true, tooltip, contentWidth) + 2 // plus 2 for the frame
_, _ = self.c.GocuiGui().SetView(self.c.Views().Tooltip.Name(), x0, tooltipTop, x1, tooltipTop+tooltipHeight-1, 0)
}
// Wraps the lines of the menu prompt to the available width and rerenders the
// menu if neeeded. Returns the number of lines the prompt takes up.
func (self *ConfirmationHelper) layoutMenuPrompt(contentWidth int) int {
oldPromptLines := self.c.Contexts().Menu.GetPromptLines()
var promptLines []string
prompt := self.c.Contexts().Menu.GetPrompt()
if len(prompt) > 0 {
promptLines = wrapMessageToWidth(true, prompt, contentWidth)
promptLines = append(promptLines, "")
}
self.c.Contexts().Menu.SetPromptLines(promptLines)
if len(oldPromptLines) != len(promptLines) {
// The number of lines in the prompt has changed; this happens either
// because we're now showing a menu that has a prompt, and the previous
// menu didn't (or vice versa), or because the user is resizing the
// terminal window while a menu with a prompt is open.
// We need to rerender to give the menu context a chance to update its
// non-model items, and reinitialize the data it uses for converting
// between view index and model index.
_ = self.c.Contexts().Menu.HandleRender()
// Then we need to refocus to ensure the cursor is in the right place in
// the view.
_ = self.c.Contexts().Menu.HandleFocus(types.OnFocusOpts{})
}
return len(promptLines)
}
func (self *ConfirmationHelper) resizeConfirmationPanel() {
suggestionsViewHeight := 0
if self.c.Views().Suggestions.Visible {