summaryrefslogtreecommitdiff
path: root/durationSlider.go
blob: 0824b63822e1da30bfc8c85d5e6719fda26e6845 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
	A slider that goes between a High and Low time
*/

package gadgets

import (
	"fmt"
	"time"

	"go.wit.com/gui"
	"go.wit.com/log"
)

type Duration struct {
	p *gui.Node // parent widget
	l *gui.Node // label widget
	s *gui.Node // slider widget

	Label    string
	Low      time.Duration
	High     time.Duration
	Duration time.Duration

	Custom func()
}

func (n *Duration) Set(d time.Duration) {
	var timeRange, step, offset time.Duration

	if d > n.High {
		d = n.High
	}
	if d < n.Low {
		d = n.Low
	}

	// set the duration
	n.Duration = d

	// figure out the integer offset for the Slider GUI Widget
	timeRange = n.High - n.Low
	step = timeRange / 1000
	if step == 0 {
		log.Log(INFO, "duration.Set() division by step == 0", n.Low, n.High, timeRange, step)
		n.s.Set(0)
		return
	}
	offset = d - n.Low
	i := int(offset / step)
	log.Log(INFO, "duration.Set() =", n.Low, n.High, d, "i =", i)
	// n.s.I = i
	n.s.Set(i)
	n.s.Custom()
}

func NewDurationSlider(n *gui.Node, label string, low time.Duration, high time.Duration) *Duration {
	d := Duration{
		p:     n,
		Label: label,
		High:  high,
		Low:   low,
	}

	// various timeout settings
	d.l = n.NewLabel(label)
	d.s = n.NewSlider(label, 0, 1000)
	d.s.Custom = func() {
		d.Duration = low + (high-low)*time.Duration(d.s.Int())/1000
		log.Println("d.Duration =", d.Duration)
		s := fmt.Sprintf("%s (%v)", d.Label, d.Duration)
		d.l.SetText(s)
		if d.Custom != nil {
			d.Custom()
		}
	}

	return &d
}