Ejemplo n.º 1
0
Archivo: others.go Proyecto: sqp/godock
func onValuePairChanged(updated *gtk.SpinButton, vp *valuePair) {
	if !vp.toggle.GetActive() {
		return
	}
	newval := updated.GetValue()
	if vp.linked.GetValue() != newval {
		vp.linked.SetValue(newval)
	}
}
Ejemplo n.º 2
0
// IntegerSize adds an integer selector widget.
//
func IntegerSize(key *cftype.Key) {
	if key.NbElements > 1 { // TODO: remove temp test
		key.Log().Info("IntegerSize multi", key.NbElements, key.Type.String())
	}

	toggle := newgtk.ToggleButton()
	img := newgtk.ImageFromIconName("media-playback-pause", gtk.ICON_SIZE_MENU) // get better image.
	toggle.SetImage(img)

	values := key.Value().ListInt()
	minValue, maxValue := minMaxValues(key)

	var valuers []WidgetValuer

	key.NbElements *= 2 // Two widgets to add.

	// Value pair data.
	var firstValue int
	var firstWidget *gtk.SpinButton
	var cbBlock func() func()

	for k := 0; k < key.NbElements; k++ {
		var value int
		if k < len(values) {
			value = values[k]
		}

		w := newgtk.SpinButtonWithRange(minValue, maxValue, 1)
		w.SetValue(float64(value))
		key.PackSubWidget(w)
		valuers = append(valuers, w)

		if k&1 == 0 { // first value, separator
			label := newgtk.Label("x")
			key.PackSubWidget(label)

			firstWidget = w
			firstValue = value

		} else { // second value. connect both spin values.
			if firstValue == value {
				toggle.SetActive(true)
			}

			cb0, e := firstWidget.Connect("value-changed", onValuePairChanged, &valuePair{
				linked: w,
				toggle: toggle})
			key.Log().Err(e, "IntegerSize connect value-changed 1")
			cb1, e := w.Connect("value-changed", onValuePairChanged, &valuePair{
				linked: firstWidget,
				toggle: toggle})
			key.Log().Err(e, "IntegerSize connect value-changed 2")

			cbBlock = func() func() {
				firstWidget.HandlerBlock(cb0)
				w.HandlerBlock(cb1)
				return func() {
					firstWidget.HandlerUnblock(cb0)
					w.HandlerUnblock(cb1)
				}
			}
		}
	}

	setValue := func(uncast interface{}) {
		if cbBlock == nil {
			key.Log().NewErr("no valuePair callbacks", "IntegerSize", key.Name)
		} else {
			defer cbBlock()() // This disables now and reenables during defer.
		}
		values := uncast.([]int)
		if len(values) < 2 {
			key.Log().NewErr("not enough values provided", "IntegerSize set value", key.Name, values)
			values = []int{0, 0}
		}

		listValuerSet(valuers, cast.IntsToFloats(values))
		toggle.SetActive(values[0] == values[1])
	}

	key.PackKeyWidget(key,
		func() interface{} { return cast.FloatsToInts(listValuerGet(valuers)) },
		setValue,
		toggle,
	)

	oldval, e := key.Storage().Default(key.Group, key.Name)
	key.Log().Err(e, "IntegerSize original value")
	if e == nil {
		PackReset(key, oldval.ListInt())
	}
}