dialog.go_ raw

   1  package dialog
   2  
   3  import (
   4  	"image"
   5  	"image/color"
   6  	
   7  	"github.com/p9c/gio/io/pointer"
   8  	l "github.com/p9c/gio/layout"
   9  	"github.com/p9c/gio/op"
  10  	"github.com/p9c/gio/op/paint"
  11  	"github.com/p9c/gio/unit"
  12  	
  13  	"github.com/p9c/pod/pkg/gui"
  14  )
  15  
  16  type Dialog struct {
  17  	theme              *gui.Theme
  18  	duration           int
  19  	singleCornerRadius unit.Value
  20  	singleElevation    unit.Value
  21  	
  22  	content          *content
  23  	headerBackground color.NRGBA
  24  	bodyBackground   color.NRGBA
  25  	icon             *[]byte
  26  	ticker           float32
  27  	hideTitle        bool
  28  	close            *gui.Clickable
  29  	cornerRadius     unit.Value
  30  	elevation        unit.Value
  31  }
  32  
  33  type content struct {
  34  	title, level string
  35  	content      interface{}
  36  }
  37  
  38  func New(th *gui.Theme) *Dialog {
  39  	return &Dialog{
  40  		theme:          th,
  41  		duration:       100,
  42  		close:          th.Clickable(),
  43  		bodyBackground: gui.HexNRGB("ee000000"),
  44  		// singleSize:         image.Pt(300, 80),
  45  		singleCornerRadius: unit.Dp(5),
  46  		singleElevation:    unit.Dp(5),
  47  	}
  48  }
  49  func (d *Dialog) ShowDialog(title, level string, contentInterface interface{}) func() {
  50  	return func() {
  51  		c := &content{
  52  			title:   title,
  53  			content: contentInterface,
  54  			level:   level,
  55  		}
  56  		d.content = c
  57  	}
  58  }
  59  
  60  func (d *Dialog) DrawDialog() func(gtx l.Context) {
  61  	// switch d.content.level {
  62  	// case "Warning":
  63  	//	//ic = &icons2.AlertWarning
  64  	// case "Success":
  65  	//	//ic = &icons2.NavigationCheck
  66  	// case "Danger":
  67  	//	//ic = &icons2.AlertError
  68  	// case "Info":
  69  	//	//ic = &icons2.ActionInfo
  70  	// }
  71  	
  72  	return func(gtx l.Context) {
  73  		if d.content != nil {
  74  			var content func(gtx l.Context) l.Dimensions
  75  			switch c := d.content.content.(type) {
  76  			case string:
  77  				content = d.theme.Body1(c).Color("PanelText").Fn
  78  			case func(gtx l.Context) l.Dimensions:
  79  				content = c
  80  			}
  81  			
  82  			defer op.Push(gtx.Ops).Pop()
  83  			gtx.Constraints.Min = gtx.Constraints.Max
  84  			d.theme.Stack().Alignment(l.Center).Expanded(
  85  				func(gtx l.Context) l.Dimensions {
  86  					paint.Fill(gtx.Ops, d.bodyBackground)
  87  					pointer.Rect(
  88  						image.Rectangle{Max: gtx.Constraints.Max},
  89  					).Add(gtx.Ops)
  90  					return l.Dimensions{Size: gtx.Constraints.Max}
  91  				},
  92  			).Stacked(
  93  				d.theme.Fill("DocBg", d.theme.Inset(
  94  					0.25,
  95  					d.theme.Fill(
  96  						"PanelBg",
  97  						d.theme.Inset(
  98  							1,
  99  							d.theme.VFlex().
 100  								Rigid(
 101  									d.theme.Body1(d.content.title).Color("PanelText").Fn,
 102  								).
 103  								Rigid(content).
 104  								Rigid(
 105  									d.theme.Button(d.close).Text("CLOSE").Color("Warning").SetClick(d.Close).Fn,
 106  								).Fn,
 107  						).Fn,
 108  						l.Center).Fn,
 109  				).Fn, 0).Fn,
 110  			).Fn(gtx)
 111  		}
 112  	}
 113  }
 114  
 115  func (d *Dialog) Close() {
 116  	d.content = nil
 117  }
 118