Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Modal improvement #1

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions _demos/modal/main.go
Expand Up @@ -20,9 +20,11 @@ func main() {
maxX, maxY := gui.Size()
x, y, w := maxX/3, maxY/3, maxX/3*2

modal := component.NewModal(gui, x, y, w).
SetText("Do you want MacBook Pro?")

modal := component.NewModal(gui, x, y, w)
modal.SetText("Do you want MacBook Pro?")
// optional parameter:
// modal.Frame = true
// modal.Title = "Question"
modal.AddButton("No", gocui.KeyEnter, quit)
modal.AddButton("Yes", gocui.KeyEnter, quit)

Expand Down
20 changes: 18 additions & 2 deletions modal.go
Expand Up @@ -9,6 +9,9 @@ import (
)

type Modal struct {
Frame bool
Title string

*gocui.Gui
name string
textArea *textArea
Expand Down Expand Up @@ -38,6 +41,8 @@ func NewModal(gui *gocui.Gui, x, y, w int) *Modal {
return &Modal{
Gui: gui,
name: "modal",
Frame: false,
Title: "Please Answer",
activeButton: 0,
Attributes: &Attributes{
textColor: gocui.ColorWhite,
Expand Down Expand Up @@ -100,6 +105,8 @@ func (m *Modal) AddButton(label string, key Key, handler Handler) *Button {

button := NewButton(m.Gui, label, x, y, len(label)).
AddHandler(gocui.KeyTab, m.nextButton).
AddHandler(gocui.KeyArrowRight, m.nextButton).
AddHandler(gocui.KeyArrowLeft, m.previousButton).
AddHandler(key, handler).
SetTextColor(gocui.ColorWhite, gocui.ColorBlack).
SetHilightColor(gocui.ColorBlack, gocui.ColorWhite)
Expand All @@ -121,7 +128,8 @@ func (m *Modal) Draw() {
panic(err)
}

v.Frame = false
v.Frame = m.Frame
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please be sure to set the optional parameters in this way.

func (m * Modal) SetText (text string) * Modal {}

v.Title = m.Title
v.FgColor = m.textColor
v.BgColor = m.textBgColor
}
Expand Down Expand Up @@ -174,14 +182,22 @@ func (m *Modal) Close() {
}
}

// nextButton focus netxt button
// nextButton focus next button
func (m *Modal) nextButton(g *gocui.Gui, v *gocui.View) error {
m.buttons[m.activeButton].UnFocus()
m.activeButton = (m.activeButton + 1) % len(m.buttons)
m.buttons[m.activeButton].Focus()
return nil
}

// previousButton focus netxt button
func (m *Modal) previousButton(g *gocui.Gui, v *gocui.View) error {
m.buttons[m.activeButton].UnFocus()
m.activeButton = ((m.activeButton - 1) + len(m.buttons)) % len(m.buttons)
m.buttons[m.activeButton].Focus()
return nil
}

func roundUp(num, places float64) float64 {
shift := math.Pow(10, places)
return roundUpInt(num*shift) / shift
Expand Down