ime

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: MIT Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package ime hosts a system input method on macOS, Windows, and Linux. Experimental. Call Serve from OnReady to handle input sessions on the app's main thread:

a.OnReady(func() {
	if err := ime.Serve(a, ime.Handler{Key: handleKey}); err != nil {
		log.Fatal(err)
	}
})

macOS uses InputMethodKit, Windows uses a Text Services Framework DLL, and Linux uses IBus or Fcitx5. The input method example shows how to build and install each host.

Use Handler.End to release per-session state such as a candidate window. See Handler.Connection for the name expected by the platform's host.

Index

Constants

This section is empty.

Variables

View Source
var ErrServing = errors.New("ime: already serving")

ErrServing is returned when the process already serves an input method.

View Source
var ErrUnsupported = platform.ErrUnsupported

ErrUnsupported is returned when the app's backend cannot host an input method.

Functions

func CurrentMode

func CurrentMode(a *tango.App) string

CurrentMode returns the app's process-wide mode ID, or an empty string when no mode is active or the platform does not support modes.

func Serve

func Serve(a *tango.App, h Handler) error

Serve starts the process's input method server. Call it from OnReady. A successful server runs until process exit. Startup errors such as an unavailable IBus daemon can be retried. macOS and Linux cache native IME loading errors for the life of the process, so retry those after restarting.

func SetMode

func SetMode(a *tango.App, id string) error

SetMode sets the app's process-wide input mode after Serve. The ID must be declared in tan.json. Platforms without modes return ErrUnsupported.

func SystemDrawsCandidates

func SystemDrawsCandidates() bool

SystemDrawsCandidates reports whether the platform backend provides a native candidate panel. Linux uses the IBus or Fcitx5 panel.

Types

type Candidate

type Candidate struct {
	Text, Comment string
}

Candidate is an entry in a native candidate panel.

type Handler

type Handler struct {
	// Connection overrides the platform default. On macOS the default comes
	// from InputMethodConnectionName in Info.plist, or the bundle identifier
	// followed by _Connection if the plist omits it.
	//
	// On Windows the default comes from the packaged IME configuration.
	// Without a configured name, Windows and Linux use App.ID() followed
	// by _Connection. App.ID() uses Options.Name when Options.ID is empty.
	// The resolved value must match the name configured for the TSF bridge
	// on Windows. IBus uses it without the _Connection suffix as the engine
	// name.
	Connection string
	// Key is required. Return true to consume the key.
	Key func(s *Session, k Key) bool
	// Activate runs when the session becomes active.
	Activate func(s *Session)
	// Deactivate runs when focus leaves or the input method changes.
	// IBus commits the last marked text itself before sending Deactivate.
	Deactivate func(s *Session)
	// CommitComposition runs when the system requests an immediate end to
	// composition. A nil handler leaves composition up to the app.
	CommitComposition func(s *Session)
	// End runs once when an activated session or one seen by a handler ends.
	// The session is already closed. Use End to release associated state.
	// End may run without a preceding Deactivate. On macOS it runs when
	// InputMethodKit releases the controller at the end of the client
	// connection. Focus changes between fields in the same client use
	// Activate and Deactivate.
	End func(s *Session)
	// Modifier receives modifier changes on desktop backends. Return true to consume
	// the event. With no handler the event is passed through.
	Modifier func(s *Session, m ModifierKey) bool
	// ModeChanged receives a system-initiated change of the global input mode
	// on desktop backends. SetMode does not invoke it.
	ModeChanged func(mode string)
	// Menu supplies items for the system input method menu.
	Menu func() []MenuItem
	// MenuClicked receives the ID of a selected input method menu item.
	MenuClicked func(id string)
	// CandidateSelected receives the zero-based index in the session's
	// current candidate list when a user selects an entry from a panel.
	CandidateSelected func(s *Session, index int)
}

Handler receives events on the app's main thread. Callback panics are recovered and reported to tango.Options.OnPanic when set. The process keeps running regardless of its return value. A panicking Key leaves the key unhandled.

type Key

type Key struct {
	Code                   string
	Text                   string
	Shift, Ctrl, Alt, Meta bool
	CapsLock, Repeat       bool
}

Key is a key-down event. Code follows KeyboardEvent.code naming, such as "KeyA", "Digit1", or "ArrowLeft", and is empty for an unknown key. Text holds the printable character produced with modifiers applied, if any. The desktop backends report CapsLock and Repeat.

type MenuItem struct {
	ID, Title                    string
	Checked, Disabled, Separator bool
}

MenuItem describes a command in the input method menu. A separator ignores all other fields. Command IDs are passed to Handler.MenuClicked.

type Mode

type Mode struct {
	ID, Label, Short string
}

Mode describes a declared input mode from the application's packaging config.

type ModifierKey

type ModifierKey struct {
	Code                   string
	Down, CapsLock         bool
	Shift, Ctrl, Alt, Meta bool
}

ModifierKey reports a change to a modifier, with the side in Code where applicable (for example, "ShiftLeft", "ControlRight", or "CapsLock"). The flags reflect the state after the event. macOS reports CapsLock as a lock-state change, so Down on a CapsLock event is the new lock state.

type Session

type Session struct {
	// contains filtered or unexported fields
}

Session is the input method's connection to a client text-entry session. Its methods can be called from any goroutine.

func (*Session) Caret

func (s *Session) Caret() tango.Rect

Caret returns the cursor rectangle in logical desktop pixels from the top-left of the primary display. It returns the zero rectangle if unknown, including on Wayland and when using Fcitx5.

func (*Session) ClientApp

func (s *Session) ClientApp() string

ClientApp returns the macOS bundle ID, Windows executable name, or IBus client identification string, when the platform provides one.

func (*Session) Commit

func (s *Session) Commit(text string)

Commit replaces the marked text with final text and clears the session's candidate list. Invalid UTF-8 is replaced with U+FFFD. Calls from other goroutines are posted to the main thread.

func (*Session) ID

func (s *Session) ID() uint64

ID identifies this session for the life of the process.

func (*Session) SelectCandidate

func (s *Session) SelectCandidate(index int)

SelectCandidate reports a user selection from a custom panel to Handler.CandidateSelected. Index is zero-based in the last list passed to SetCandidates. Closed or inactive sessions and out-of-range indexes are ignored. Calls from other goroutines are posted to the main thread.

func (*Session) SetCandidates

func (s *Session) SetCandidates(list []Candidate, selected int)

SetCandidates replaces the session's candidate list. Pass an empty list to clear it and use -1 for no highlighted entry. On backends with a native panel this also updates the panel. Calls from other goroutines are posted to the main thread.

func (*Session) SetComposition

func (s *Session) SetComposition(text string, cursor int)

SetComposition replaces the marked text, or clears it when text is empty. Cursor counts runes and is clamped to the text. Invalid UTF-8 is replaced with U+FFFD. Calls from other goroutines are posted to the main thread.

Jump to

Keyboard shortcuts

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