141 lines
3.2 KiB
Go
141 lines
3.2 KiB
Go
|
package tins2020
|
||
|
|
||
|
import (
|
||
|
"opslag.de/schobers/geom"
|
||
|
"opslag.de/schobers/zntg"
|
||
|
"opslag.de/schobers/zntg/ui"
|
||
|
)
|
||
|
|
||
|
type confirmationDialog struct {
|
||
|
ui.Proxy
|
||
|
|
||
|
active string
|
||
|
cancel ui.Button
|
||
|
confirm ui.Button
|
||
|
question ui.Paragraph
|
||
|
|
||
|
userDecided ui.Events
|
||
|
}
|
||
|
|
||
|
func newConfirmationDialog(caption, question string) *confirmationDialog {
|
||
|
dialog := &confirmationDialog{}
|
||
|
|
||
|
dialog.active = "confirm"
|
||
|
dialog.cancel.Text = "Cancel"
|
||
|
dialog.cancel.ButtonClicked().AddHandler(func(ctx ui.Context, _ ui.ControlClickedArgs) { dialog.userCanceled(ctx) })
|
||
|
dialog.cancel.Type = ui.ButtonTypeText
|
||
|
dialog.cancel.HoverColor = zntg.MustHexColor(`FFFFFF1F`)
|
||
|
dialog.confirm.Text = "OK"
|
||
|
dialog.confirm.ButtonClicked().AddHandler(func(ctx ui.Context, _ ui.ControlClickedArgs) { dialog.userConfirmed(ctx) })
|
||
|
dialog.confirm.Type = ui.ButtonTypeText
|
||
|
dialog.confirm.HoverColor = zntg.MustHexColor(`FFFFFF1F`)
|
||
|
dialog.question.Text = question
|
||
|
dialog.updateActive()
|
||
|
|
||
|
responses := ui.BuildStackPanel(ui.OrientationHorizontal, func(p *ui.StackPanel) {
|
||
|
p.AddChild(ui.FixedWidth(&dialog.cancel, 160))
|
||
|
p.AddChild(ui.FixedWidth(&dialog.confirm, 160))
|
||
|
})
|
||
|
|
||
|
content := &dialogBase{}
|
||
|
content.Background = zntg.MustHexColor(`#0000007F`)
|
||
|
content.Init(caption, ui.BuildStackPanel(ui.OrientationVertical, func(p *ui.StackPanel) {
|
||
|
p.AddChild(&dialog.question)
|
||
|
p.AddChild(responses)
|
||
|
}))
|
||
|
|
||
|
dialog.Content = ui.Background(ui.BuildSpacing(content, func(s *ui.Spacing) {
|
||
|
s.Width = ui.Fixed(320)
|
||
|
s.Center()
|
||
|
}), zntg.MustHexColor(`#0000007F`))
|
||
|
|
||
|
return dialog
|
||
|
}
|
||
|
|
||
|
func (d *confirmationDialog) toggleActive() {
|
||
|
if d.active == "confirm" {
|
||
|
d.active = "cancel"
|
||
|
} else {
|
||
|
d.active = "confirm"
|
||
|
}
|
||
|
d.updateActive()
|
||
|
}
|
||
|
|
||
|
func (d *confirmationDialog) updateActive() {
|
||
|
d.cancel.Background = nil
|
||
|
d.confirm.Background = nil
|
||
|
switch d.active {
|
||
|
case "cancel":
|
||
|
d.cancel.Background = hoverTransparentColor
|
||
|
case "confirm":
|
||
|
d.confirm.Background = hoverTransparentColor
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (d *confirmationDialog) userCanceled(ctx ui.Context) {
|
||
|
d.userDecided.Notify(ctx, false)
|
||
|
}
|
||
|
|
||
|
func (d *confirmationDialog) userConfirmed(ctx ui.Context) {
|
||
|
d.userDecided.Notify(ctx, true)
|
||
|
}
|
||
|
|
||
|
func (d *confirmationDialog) Handle(ctx ui.Context, e ui.Event) bool {
|
||
|
if d.Proxy.Handle(ctx, e) {
|
||
|
return true
|
||
|
}
|
||
|
switch e := e.(type) {
|
||
|
case *ui.MouseButtonDownEvent:
|
||
|
if e.Button == ui.MouseButtonRight {
|
||
|
d.userCanceled(ctx)
|
||
|
return true
|
||
|
}
|
||
|
case *ui.MouseMoveEvent:
|
||
|
if d.cancel.IsOver() {
|
||
|
d.active = "cancel"
|
||
|
}
|
||
|
if d.confirm.IsOver() {
|
||
|
d.active = "confirm"
|
||
|
}
|
||
|
d.updateActive()
|
||
|
case *ui.KeyDownEvent:
|
||
|
switch e.Key {
|
||
|
case ui.KeyEscape:
|
||
|
d.userCanceled(ctx)
|
||
|
return true
|
||
|
case ui.KeyEnter:
|
||
|
switch d.active {
|
||
|
case "cancel":
|
||
|
d.userCanceled(ctx)
|
||
|
case "confirm":
|
||
|
d.userConfirmed(ctx)
|
||
|
}
|
||
|
return true
|
||
|
case ui.KeyLeft:
|
||
|
d.toggleActive()
|
||
|
case ui.KeyRight:
|
||
|
d.toggleActive()
|
||
|
case ui.KeyTab:
|
||
|
d.toggleActive()
|
||
|
}
|
||
|
}
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
type dialogBase struct {
|
||
|
ui.StackPanel
|
||
|
|
||
|
caption ui.Label
|
||
|
content ui.Proxy
|
||
|
}
|
||
|
|
||
|
func (d *dialogBase) DesiredSize(ctx ui.Context, size geom.PointF32) geom.PointF32 {
|
||
|
return d.StackPanel.DesiredSize(ctx, size)
|
||
|
}
|
||
|
|
||
|
func (d *dialogBase) Init(caption string, content ui.Control) {
|
||
|
d.caption.Text = caption
|
||
|
d.content.Content = content
|
||
|
d.Children = []ui.Control{&d.caption, &d.content}
|
||
|
}
|