2019-02-10 11:46:18 +08:00
|
|
|
// Copyright 2019 Google Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package button
|
|
|
|
|
|
|
|
// options.go contains configurable options for Button.
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-02-23 15:18:06 +08:00
|
|
|
"time"
|
2019-02-10 11:46:18 +08:00
|
|
|
|
|
|
|
"github.com/mum4k/termdash/cell"
|
2019-02-23 13:07:54 +08:00
|
|
|
"github.com/mum4k/termdash/cell/runewidth"
|
2019-02-10 11:46:18 +08:00
|
|
|
"github.com/mum4k/termdash/keyboard"
|
2019-02-22 14:07:53 +08:00
|
|
|
"github.com/mum4k/termdash/widgetapi"
|
2019-02-10 11:46:18 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Option is used to provide options.
|
|
|
|
type Option interface {
|
|
|
|
// set sets the provided option.
|
|
|
|
set(*options)
|
|
|
|
}
|
|
|
|
|
|
|
|
// option implements Option.
|
|
|
|
type option func(*options)
|
|
|
|
|
|
|
|
// set implements Option.set.
|
|
|
|
func (o option) set(opts *options) {
|
|
|
|
o(opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// options holds the provided options.
|
|
|
|
type options struct {
|
|
|
|
fillColor cell.Color
|
|
|
|
textColor cell.Color
|
|
|
|
shadowColor cell.Color
|
|
|
|
height int
|
|
|
|
width int
|
|
|
|
key keyboard.Key
|
2019-02-22 14:07:53 +08:00
|
|
|
keyScope widgetapi.KeyScope
|
2019-02-23 15:18:06 +08:00
|
|
|
keyUpDelay time.Duration
|
2019-02-10 11:46:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// validate validates the provided options.
|
|
|
|
func (o *options) validate() error {
|
|
|
|
if min := 1; o.height < min {
|
|
|
|
return fmt.Errorf("invalid height %d, must be %d <= height", o.height, min)
|
|
|
|
}
|
|
|
|
if min := 1; o.width < min {
|
|
|
|
return fmt.Errorf("invalid width %d, must be %d <= width", o.width, min)
|
|
|
|
}
|
2019-02-23 15:18:06 +08:00
|
|
|
if min := time.Duration(0); o.keyUpDelay < min {
|
|
|
|
return fmt.Errorf("invalid keyUpDelay %v, must be %v <= keyUpDelay", o.keyUpDelay, min)
|
|
|
|
}
|
2019-02-10 11:46:18 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// newOptions returns options with the default values set.
|
2019-02-23 13:07:54 +08:00
|
|
|
func newOptions(text string) *options {
|
2019-02-10 11:46:18 +08:00
|
|
|
return &options{
|
2019-02-23 13:07:54 +08:00
|
|
|
fillColor: cell.ColorNumber(117),
|
|
|
|
textColor: cell.ColorBlack,
|
|
|
|
shadowColor: cell.ColorNumber(240),
|
2019-02-10 11:46:18 +08:00
|
|
|
height: DefaultHeight,
|
2019-02-23 13:07:54 +08:00
|
|
|
width: widthFor(text),
|
2019-02-23 15:18:06 +08:00
|
|
|
keyUpDelay: DefaultKeyUpDelay,
|
2019-02-10 11:46:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FillColor sets the fill color of the button.
|
|
|
|
func FillColor(c cell.Color) Option {
|
|
|
|
return option(func(opts *options) {
|
|
|
|
opts.fillColor = c
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// TextColor sets the color of the text label in the button.
|
|
|
|
func TextColor(c cell.Color) Option {
|
|
|
|
return option(func(opts *options) {
|
|
|
|
opts.textColor = c
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ShadowColor sets the color of the shadow under the button.
|
|
|
|
func ShadowColor(c cell.Color) Option {
|
|
|
|
return option(func(opts *options) {
|
|
|
|
opts.shadowColor = c
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultHeight is the default for the Height option.
|
2019-02-22 14:07:53 +08:00
|
|
|
const DefaultHeight = 3
|
2019-02-10 11:46:18 +08:00
|
|
|
|
|
|
|
// Height sets the height of the button in cells.
|
|
|
|
// Must be a positive non-zero integer.
|
|
|
|
// Defaults to DefaultHeight.
|
|
|
|
func Height(cells int) Option {
|
|
|
|
return option(func(opts *options) {
|
|
|
|
opts.height = cells
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Width sets the width of the button in cells.
|
|
|
|
// Must be a positive non-zero integer.
|
|
|
|
// Defaults to the auto-width based on the length of the text label.
|
|
|
|
func Width(cells int) Option {
|
|
|
|
return option(func(opts *options) {
|
|
|
|
opts.width = cells
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-02-23 13:07:54 +08:00
|
|
|
// WidthFor sets the width of the button as if it was displaying the provided text.
|
|
|
|
// Useful when displaying multiple buttons with the intention to set all of
|
|
|
|
// their sizes equal to the one with the longest text.
|
|
|
|
func WidthFor(text string) Option {
|
|
|
|
return option(func(opts *options) {
|
|
|
|
opts.width = widthFor(text)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-02-10 11:46:18 +08:00
|
|
|
// Key configures the keyboard key that presses the button.
|
2019-02-22 14:07:53 +08:00
|
|
|
// The widget responds to this key only if its container if focused.
|
|
|
|
// When not provided, the widget ignores all keyboard events.
|
2019-02-10 11:46:18 +08:00
|
|
|
func Key(k keyboard.Key) Option {
|
|
|
|
return option(func(opts *options) {
|
|
|
|
opts.key = k
|
2019-02-22 14:07:53 +08:00
|
|
|
opts.keyScope = widgetapi.KeyScopeFocused
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// GlobalKey is like Key, but makes the widget respond to the key even if its
|
|
|
|
// container isn't focused.
|
|
|
|
// When not provided, the widget ignores all keyboard events.
|
|
|
|
func GlobalKey(k keyboard.Key) Option {
|
|
|
|
return option(func(opts *options) {
|
|
|
|
opts.key = k
|
2019-02-23 13:07:54 +08:00
|
|
|
opts.keyScope = widgetapi.KeyScopeGlobal
|
2019-02-10 11:46:18 +08:00
|
|
|
})
|
|
|
|
}
|
2019-02-23 13:07:54 +08:00
|
|
|
|
2019-02-23 15:18:06 +08:00
|
|
|
// DefaultKeyUpDelay is the default value for the KeyUpDelay option.
|
|
|
|
const DefaultKeyUpDelay = 250 * time.Millisecond
|
|
|
|
|
|
|
|
// KeyUpDelay is the amount of time the button will remain "pressed down" after
|
|
|
|
// triggered by the configured key. Termbox doesn't emit events for key
|
|
|
|
// releases so the button simulates it by timing it.
|
|
|
|
// This only works if the manual termdash redraw or the periodic redraw
|
|
|
|
// interval are reasonably close to this delay.
|
|
|
|
// The duration cannot be negative.
|
|
|
|
// Defaults to DefaultKeyUpDelay.
|
|
|
|
func KeyUpDelay(d time.Duration) Option {
|
|
|
|
return option(func(opts *options) {
|
|
|
|
opts.keyUpDelay = d
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-02-23 13:07:54 +08:00
|
|
|
// widthFor returns the required width for the specified text.
|
|
|
|
func widthFor(text string) int {
|
|
|
|
return runewidth.StringWidth(text) + 2 // One empty cell at each side.
|
|
|
|
}
|