NewAlertControl returns a new threshold and alarm control. The generated thresholds cover the inclusive range `[min, max]` in `step` increments. The selected threshold snaps to the nearest generated value. The onChange callback is the low-level threshold hook and runs after user-driven threshold ch
(min, max, step, selected int, onChange func(int) error)
| 61 | // onChange callback is the low-level threshold hook and runs after user-driven |
| 62 | // threshold changes. |
| 63 | func NewAlertControl(min, max, step, selected int, onChange func(int) error) (*AlertControl, error) { |
| 64 | values, err := alertThresholdValues(min, max, step) |
| 65 | if err != nil { |
| 66 | return nil, err |
| 67 | } |
| 68 | selectedIndex := nearestThresholdIndex(values, selected) |
| 69 | |
| 70 | var control *AlertControl |
| 71 | toggle, err := checkbox.New("", |
| 72 | checkbox.Checked(false), |
| 73 | checkbox.UseIndicatorSet(checkbox.IndicatorSets.Classic), |
| 74 | checkbox.CellOpts(cell.FgColor(cell.ColorNumber(245))), |
| 75 | checkbox.FocusedCellOpts(cell.FgColor(cell.ColorNumber(195))), |
| 76 | checkbox.CheckedCellOpts(cell.FgColor(cell.ColorNumber(118))), |
| 77 | ) |
| 78 | if err != nil { |
| 79 | return nil, err |
| 80 | } |
| 81 | |
| 82 | labels := make([]string, len(values)) |
| 83 | for i, value := range values { |
| 84 | labels[i] = fmt.Sprintf("%03d", value) |
| 85 | } |
| 86 | |
| 87 | menu, err := dropdown.New(labels, |
| 88 | dropdown.Selected(selectedIndex), |
| 89 | dropdown.Width(7), |
| 90 | dropdown.GlyphSet(dropdown.GlyphProfiles.Classic), |
| 91 | dropdown.CellOpts(cell.FgColor(cell.ColorNumber(87)), cell.BgColor(cell.ColorNumber(234))), |
| 92 | dropdown.FocusedCellOpts(cell.FgColor(cell.ColorNumber(195)), cell.BgColor(cell.ColorNumber(236))), |
| 93 | dropdown.SelectedCellOpts(cell.FgColor(cell.ColorWhite), cell.BgColor(cell.ColorNumber(60))), |
| 94 | dropdown.BorderCellOpts(cell.FgColor(cell.ColorNumber(81))), |
| 95 | dropdown.OnSelect(func(index int, label string) error { |
| 96 | _ = label |
| 97 | if control == nil { |
| 98 | return nil |
| 99 | } |
| 100 | value := control.valueForIndex(index) |
| 101 | control.setValue(value) |
| 102 | if control.onChange == nil { |
| 103 | return nil |
| 104 | } |
| 105 | return control.onChange(value) |
| 106 | }), |
| 107 | ) |
| 108 | if err != nil { |
| 109 | return nil, err |
| 110 | } |
| 111 | |
| 112 | control = &AlertControl{ |
| 113 | value: values[selectedIndex], |
| 114 | values: values, |
| 115 | toggle: toggle, |
| 116 | menu: menu, |
| 117 | onChange: onChange, |
| 118 | } |
| 119 | return control, nil |
| 120 | } |