Exemplo n.º 1
0
func buildTimerDemo(event gwu.Event) gwu.Comp {
	p := gwu.NewPanel()
	p.SetCellPadding(3)

	// Add timers to a panel which is always attached instead of our panel
	// because the user can switch to another component demo causing this panel to be removed
	// and that way timer events would address components that are not part of the window (returning error).
	hiddenPan := event.Session().Attr("hiddenPan").(gwu.Panel)

	p.Add(gwu.NewLabel("A Timer is used to detonate a bomb after 3 seconds."))
	p.AddVSpace(10)
	defText := "You can defuse the bomb with the button below. Tick... Tack..."
	l := gwu.NewLabel(defText)
	p.Add(l)
	t := gwu.NewTimer(3 * time.Second)
	b := gwu.NewButton("Defuse!")
	t.AddEHandlerFunc(func(e gwu.Event) {
		l.SetText("BOOOOM! You were too slow!")
		l.Style().SetColor(gwu.CLR_RED)
		b.SetEnabled(false)
		e.MarkDirty(l, b)
	}, gwu.ETYPE_STATE_CHANGE)
	hiddenPan.Add(t)
	row := gwu.NewHorizontalPanel()
	b.AddEHandlerFunc(func(e gwu.Event) {
		t.SetActive(false)
		l.SetText("Bomb defused! Phew! Good Job!")
		l.Style().SetColor(gwu.CLR_GREEN)
		b.SetEnabled(false)
		e.MarkDirty(t, l, b)
	}, gwu.ETYPE_CLICK)
	row.Add(b)
	b2 := gwu.NewButton("Plant a new Bomb!")
	b2.AddEHandlerFunc(func(e gwu.Event) {
		t.SetActive(true)
		t.Reset()
		l.SetText(defText)
		l.Style().SetColor("")
		b.SetEnabled(true)
		e.MarkDirty(t, l, b)
	}, gwu.ETYPE_CLICK)
	row.Add(b2)
	p.Add(row)

	p.AddVSpace(20)
	p.Add(gwu.NewLabel("A Timer is used to refresh the time below repeatedly in every second for half a minute."))
	tl := gwu.NewLabel("")
	p.Add(tl)
	t2 := gwu.NewTimer(time.Second)
	t2.SetRepeat(true)
	counter := 30
	t2.AddEHandlerFunc(func(e gwu.Event) {
		counter--
		tl.SetText(fmt.Sprintf("%s (%d remaining)", time.Now().Format("2006-01-02 15:04:05"), counter))
		e.MarkDirty(tl)
		if counter <= 0 {
			t2.SetActive(false)
			e.MarkDirty(t2)
		}
	}, gwu.ETYPE_STATE_CHANGE)
	hiddenPan.Add(t2)
	b3 := gwu.NewButton("Restart")
	b3.AddEHandlerFunc(func(e gwu.Event) {
		counter = 30
		t2.SetActive(true)
		e.MarkDirty(t2)
	}, gwu.ETYPE_CLICK)
	p.Add(b3)

	event.MarkDirty(hiddenPan)

	return p
}