tins2020/largedialog.go

101 lines
2.6 KiB
Go
Raw Permalink Normal View History

package tins2020
import (
"image/color"
"opslag.de/schobers/geom"
"opslag.de/schobers/zntg"
"opslag.de/schobers/zntg/ui"
)
const titleBarHeight = 64
type LargeDialog struct {
ui.StackPanel
titleBar *LargeDialogTitleBar
content ui.Proxy
closeRequested ui.Events
2020-05-11 12:41:42 +00:00
}
func NewLargeDialog(title string, content ui.Control) *LargeDialog {
dialog := &LargeDialog{}
2020-05-11 12:41:42 +00:00
dialog.Orientation = ui.OrientationVertical
dialog.titleBar = NewLargeDialogTitleBar(title, func(ctx ui.Context, state interface{}) {
dialog.closeRequested.Notify(ctx, state)
})
dialog.content.Content = ui.Margins(content, titleBarHeight, 20, titleBarHeight, 0)
dialog.Children = []ui.Control{dialog.titleBar, &dialog.content}
return dialog
}
func (d *LargeDialog) CloseRequested() ui.EventHandler { return &d.closeRequested }
func (d *LargeDialog) Handle(ctx ui.Context, e ui.Event) bool {
if d.StackPanel.Handle(ctx, e) {
return true
}
switch e := e.(type) {
case *ui.KeyDownEvent:
switch e.Key {
case ui.KeyEscape:
d.closeRequested.Notify(ctx, nil)
return true
case ui.KeyEnter:
d.closeRequested.Notify(ctx, nil)
return true
}
}
return false
}
func (d *LargeDialog) Hidden() { d.content.Hidden() }
2020-05-14 06:29:23 +00:00
func (d *LargeDialog) Render(ctx ui.Context) {
ctx.Renderer().Clear(zntg.MustHexColor("#356DAD"))
d.StackPanel.Render(ctx)
}
func (d *LargeDialog) Shown() { d.content.Shown() }
type LargeDialogTitleBar struct {
ui.ContainerBase
title ui.Label
close ui.Button
}
func NewLargeDialogTitleBar(title string, closeRequested ui.EventFn) *LargeDialogTitleBar {
titleBar := &LargeDialogTitleBar{}
titleBar.Children = []ui.Control{&titleBar.title, &titleBar.close}
titleBar.close.ButtonClicked().AddHandler(func(ctx ui.Context, args ui.ControlClickedArgs) {
closeRequested(ctx, args)
})
titleBar.title.Font.Color = color.White
titleBar.title.Font.Name = "title"
titleBar.title.Text = title
titleBar.title.TextAlignment = ui.AlignCenter
titleBar.close.Icon = "control-cancel"
titleBar.close.IconHeight = 32
titleBar.close.Type = ui.ButtonTypeIcon
titleBar.close.HoverColor = zntg.MustHexColor(`#ABCAED`)
return titleBar
}
func (b *LargeDialogTitleBar) Arrange(ctx ui.Context, bounds geom.RectangleF32, offset geom.PointF32, parent ui.Control) {
b.ControlBase.Arrange(ctx, bounds, offset, parent)
b.title.Arrange(ctx, bounds, offset, parent)
height := bounds.Dy()
b.close.Arrange(ctx, geom.RectRelF32(bounds.Max.X-height, bounds.Min.Y, height, height), offset, parent)
}
2020-05-11 12:41:42 +00:00
func (b *LargeDialogTitleBar) DesiredSize(ctx ui.Context, size geom.PointF32) geom.PointF32 {
return geom.PtF32(size.X, titleBarHeight)
}