main.go_ raw

   1  package main
   2  
   3  import (
   4  	"log"
   5  	"os"
   6  	
   7  	"github.com/p9c/p9/pkg/gel/gio/app"
   8  	"github.com/p9c/p9/pkg/gel/gio/io/system"
   9  	"github.com/p9c/p9/pkg/gel/gio/layout"
  10  	"github.com/p9c/p9/pkg/gel/gio/op"
  11  	"github.com/p9c/p9/pkg/gel/gio/op/paint"
  12  	"github.com/p9c/p9/pkg/gel/gio/unit"
  13  	
  14  	"github.com/p9c/p9/pkg/gui"
  15  	"github.com/p9c/p9/pkg/gui/fonts/p9fonts"
  16  	"github.com/p9c/p9/pkg/gui/toast"
  17  )
  18  
  19  var (
  20  	th         = gui.NewTheme(p9fonts.Collection(), nil)
  21  	btnDanger  = th.Clickable()
  22  	btnWarning = th.Clickable()
  23  	btnSuccess = th.Clickable()
  24  )
  25  
  26  func main() {
  27  	go func() {
  28  		w := app.NewWindow(app.Size(unit.Px(150*6+50), unit.Px(150*6-50)))
  29  		if e := loop(w); dbg.Chk(e) {
  30  			log.ftl.Ln(err)
  31  		}
  32  		os.Exit(0)
  33  	}()
  34  	app.Main()
  35  }
  36  
  37  func loop(w *app.Window) (e error) {
  38  	var ops op.Ops
  39  	t := toast.New(th)
  40  	for {
  41  		e := <-w.Events()
  42  		switch e := e.(type) {
  43  		case system.DestroyEvent:
  44  			return e.Err
  45  		case system.FrameEvent:
  46  			gtx := layout.NewContext(&ops, e)
  47  			paint.Fill(gtx.Ops, gui.HexNRGB("e5e5e5FF"))
  48  			op.InvalidateOp{}.Add(gtx.Ops)
  49  			
  50  			th.Inset(
  51  				0.25,
  52  				th.VFlex().
  53  					Rigid(
  54  						th.Inset(
  55  							0.1,
  56  							th.Button(btnDanger).Text("Danger").Background("Gray").Color("Danger").Fn,
  57  						).Fn,
  58  					).
  59  					Rigid(
  60  						th.Inset(
  61  							0.1,
  62  							th.Button(btnWarning).Text("Warning").Background("Gray").Color("Warning").Fn,
  63  						).Fn,
  64  					).
  65  					Rigid(
  66  						th.Inset(
  67  							0.1,
  68  							th.Button(btnSuccess).Text("Success").Background("Gray").Color("Success").Fn,
  69  						).Fn,
  70  					).Fn,
  71  			).Fn(gtx)
  72  			
  73  			for btnDanger.Clicked() {
  74  				t.AddToast("Danger", "Danger content", "Danger")
  75  			}
  76  			for btnSuccess.Clicked() {
  77  				t.AddToast("Success", "Success content", "Success")
  78  			}
  79  			for btnWarning.Clicked() {
  80  				t.AddToast("Warning", "Warning content", "Warning")
  81  			}
  82  			
  83  			t.DrawToasts()(gtx)
  84  			e.Frame(gtx.Ops)
  85  			w.Invalidate()
  86  		}
  87  	}
  88  }
  89