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 ¶
- Variables
- func CurrentMode(a *tango.App) string
- func Serve(a *tango.App, h Handler) error
- func SetMode(a *tango.App, id string) error
- func SystemDrawsCandidates() bool
- type Candidate
- type Handler
- type Key
- type MenuItem
- type Mode
- type ModifierKey
- type Session
- func (s *Session) Caret() tango.Rect
- func (s *Session) ClientApp() string
- func (s *Session) Commit(text string)
- func (s *Session) ID() uint64
- func (s *Session) SelectCandidate(index int)
- func (s *Session) SetCandidates(list []Candidate, selected int)
- func (s *Session) SetComposition(text string, cursor int)
Constants ¶
This section is empty.
Variables ¶
var ErrServing = errors.New("ime: already serving")
ErrServing is returned when the process already serves an input method.
var ErrUnsupported = platform.ErrUnsupported
ErrUnsupported is returned when the app's backend cannot host an input method.
Functions ¶
func CurrentMode ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
ClientApp returns the macOS bundle ID, Windows executable name, or IBus client identification string, when the platform provides one.
func (*Session) Commit ¶
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) SelectCandidate ¶
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 ¶
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 ¶
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.