accelhint

package module
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 27, 2023 License: Apache-2.0 Imports: 6 Imported by: 1

README

accelhint

A Go library for setting keyboard accelerators in a list of strings.

This library's functionality is also available for interactive use in the go-fltk GUI minicalc application.

For example:

editMenuStrings := []string{
    "Undo",
    "Redo",
    "Copy",
    "Cu&t", // preset
    "Paste",
    "Find",
    "Find Again",
    "Find && Replace", // literal '&'
}
hinted, err := accelhint.Hinted(editMenuStrings) 
// hinted is:
[]string{
    "&Undo",
    "&Redo",
    "&Copy",
    "Cu&t",
    "&Paste",
    "&Find",
    "Find &Again",
    "F&ind && Replace"
}

Use HintedX to control the marker and alphabet. Use Accelerators or AcceleratorsX to get a slice of the accelerator runes.

For example, to populate a dynamically created menu, use something like this:

items := make([]string, len(menuItems)) // assumes menuItems
for _, menuItem := range menuItems {
    items = append(items, menuItem.Text())
}
hinted, err := accelhint.Hinted(items)
if err != nil {
    log.Fatal(err)
}
accels := accelhint.Accelerators(hinted)
for i := 0; i < len(menuItems); i++ {
    accel := accels[i]
    if accel != 0 { // has an accelerator
        chars := []rune(items[i])
        j := slices.Index(chars, accel)
        if j > -1 { // should always be true
            chars = slices.Insert(chars, j, '&') // or '_' for Gtk
            menuItems[i].SetText(string(chars))
        }
    }
}

License

Apache-2.0


Documentation

Index

Constants

View Source
const (
	Alphabet  = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789" // MUST be UPPERCASE
	Marker    = '&'
	GtkMarker = '_'
)

Variables

View Source
var Version string

Functions

func Accelerators added in v0.6.0

func Accelerators(hinted []string) []rune

Returns the accelerated chars from the hinted strings assuming '&' is the accelerator marker. rune(0) indicates no accelerator. See also AcceleratorsX.

func AcceleratorsX added in v0.6.0

func AcceleratorsX(hinted []string, marker byte) []rune

Returns the accelerated chars from the hinted strings using the given accelerator marker. rune(0) indicates no accelerator. See also Accelerators.

func Hinted added in v0.7.0

func Hinted(items []string) ([]string, int, error)

Returns items with '&'s to indicate accelerators, and the number accelerated. Only characters in the Alphabet are candidates. Use '&&' for literal '&'s. See also HintedX.

func HintedX added in v0.7.0

func HintedX(items []string, marker byte, alphabet string) ([]string,
	int, error)

Returns items with marker's (only ASCII allowed) to indicate accelerators with characters from the given alphabet (of unique uppercase characters) as candidates, and how many were accelerated. Use marker + marker for literal markers. See also Hinted.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL