Skip to main content

InputHandler

Trait InputHandler 

Source
pub trait InputHandler: 'static {
Show 18 methods // Required methods fn selected_text_range( &mut self, ignore_disabled_input: bool, window: &mut Window, cx: &mut App, ) -> Option<UTF16Selection>; fn marked_text_range( &mut self, window: &mut Window, cx: &mut App, ) -> Option<Range<usize>>; fn text_for_range( &mut self, range_utf16: Range<usize>, adjusted_range: &mut Option<Range<usize>>, window: &mut Window, cx: &mut App, ) -> Option<String>; fn replace_text_in_range( &mut self, replacement_range: Option<Range<usize>>, text: &str, window: &mut Window, cx: &mut App, ); fn replace_and_mark_text_in_range( &mut self, range_utf16: Option<Range<usize>>, new_text: &str, new_selected_range: Option<Range<usize>>, window: &mut Window, cx: &mut App, ); fn unmark_text(&mut self, window: &mut Window, cx: &mut App); fn bounds_for_range( &mut self, range_utf16: Range<usize>, window: &mut Window, cx: &mut App, ) -> Option<Bounds<Pixels>>; fn character_index_for_point( &mut self, point: Point<Pixels>, window: &mut Window, cx: &mut App, ) -> Option<usize>; // Provided methods fn paste(&mut self, item: ClipboardItem, window: &mut Window, cx: &mut App) { ... } fn selection_bounds_for_range( &mut self, _range_utf16: Range<usize>, _window: &mut Window, _cx: &mut App, ) -> Vec<Bounds<Pixels>> { ... } fn set_selected_text_range( &mut self, _range_utf16: Range<usize>, _window: &mut Window, _cx: &mut App, ) { ... } fn element_bounds( &mut self, _window: &mut Window, _cx: &mut App, ) -> Option<Bounds<Pixels>> { ... } fn text_length_utf16( &mut self, _window: &mut Window, _cx: &mut App, ) -> Option<usize> { ... } fn apple_press_and_hold_enabled(&mut self) -> bool { ... } fn accepts_text_input( &mut self, _window: &mut Window, _cx: &mut App, ) -> bool { ... } fn text_input_editable_range( &mut self, _window: &mut Window, _cx: &mut App, ) -> Option<Range<usize>> { ... } fn prefers_ime_for_printable_keys( &mut self, _window: &mut Window, _cx: &mut App, ) -> bool { ... } fn text_input_configuration( &mut self, _window: &mut Window, _cx: &mut App, ) -> TextInputConfiguration { ... }
}
Expand description

Receives text from the platform’s input method (IME) on behalf of a focused element.

Implement this on the element that accepts text, and return it from Window::handle_input. The methods mirror the macOS NSTextInputClient protocol. All ranges are in UTF-16 code units, because that is what platforms use, not bytes or characters.

Methods without a default body must be implemented. The others have defaults, so override only what your text needs. See text input.

Required Methods§

Source

fn selected_text_range( &mut self, ignore_disabled_input: bool, window: &mut Window, cx: &mut App, ) -> Option<UTF16Selection>

Get the range of the user’s currently selected text, if any Corresponds to selectedRange()

Return value is in terms of UTF-16 characters, from 0 to the length of the document

Source

fn marked_text_range( &mut self, window: &mut Window, cx: &mut App, ) -> Option<Range<usize>>

Get the range of the currently marked text, if any Corresponds to markedRange()

Return value is in terms of UTF-16 characters, from 0 to the length of the document

Source

fn text_for_range( &mut self, range_utf16: Range<usize>, adjusted_range: &mut Option<Range<usize>>, window: &mut Window, cx: &mut App, ) -> Option<String>

Get the text for the given document range in UTF-16 characters Corresponds to attributedSubstring(forProposedRange: actualRange:)

range_utf16 is in terms of UTF-16 characters

Source

fn replace_text_in_range( &mut self, replacement_range: Option<Range<usize>>, text: &str, window: &mut Window, cx: &mut App, )

Replace the text in the given document range with the given text Corresponds to insertText(_:replacementRange:)

replacement_range is in terms of UTF-16 characters

Source

fn replace_and_mark_text_in_range( &mut self, range_utf16: Option<Range<usize>>, new_text: &str, new_selected_range: Option<Range<usize>>, window: &mut Window, cx: &mut App, )

Replace the text in the given document range with the given text, and mark the given text as part of an IME ‘composing’ state Corresponds to setMarkedText(_:selectedRange:replacementRange:)

range_utf16 is in terms of UTF-16 characters new_selected_range is in terms of UTF-16 characters

Source

fn unmark_text(&mut self, window: &mut Window, cx: &mut App)

Remove the IME ‘composing’ state from the document Corresponds to unmarkText()

Source

fn bounds_for_range( &mut self, range_utf16: Range<usize>, window: &mut Window, cx: &mut App, ) -> Option<Bounds<Pixels>>

Get the bounds of the given document range in window coordinates. Corresponds to firstRect(forCharacterRange:actualRange:)

This is used for positioning the IME candidate window

Source

fn character_index_for_point( &mut self, point: Point<Pixels>, window: &mut Window, cx: &mut App, ) -> Option<usize>

Get the character offset for the given point in terms of UTF16 characters

Corresponds to characterIndexForPoint:

Provided Methods§

Source

fn paste(&mut self, item: ClipboardItem, window: &mut Window, cx: &mut App)

Insert a platform-initiated paste at the current selection.

Platforms that deliver paste as an input event rather than through an application-defined action (e.g. the DOM paste event on web) call this with the full clipboard contents. The default implementation inserts only the plain-text portion of the item.

Source

fn selection_bounds_for_range( &mut self, _range_utf16: Range<usize>, _window: &mut Window, _cx: &mut App, ) -> Vec<Bounds<Pixels>>

Returns selection rectangles in document order, in window coordinates.

Return line or shaped-run fragments, not one rectangle per character. The first and last rectangles contain the range’s start and end respectively. Empty means native selection geometry is unavailable; caret/IME geometry can still be supplied by bounds_for_range.

Source

fn set_selected_text_range( &mut self, _range_utf16: Range<usize>, _window: &mut Window, _cx: &mut App, )

Set the range of the user’s currently selected text.

This is the reverse data-flow direction from Self::selected_text_range: platforms call it when the system text machinery moves the selection on the application’s behalf — e.g. the user drags a system selection handle or invokes Select All from system UI (iOS UITextInput setSelectedTextRange:, Android InputConnection.setSelection).

range_utf16 is in terms of UTF-16 characters, from 0 to the length of the document

Source

fn element_bounds( &mut self, _window: &mut Window, _cx: &mut App, ) -> Option<Bounds<Pixels>>

Get the bounds of the focused text element in window coordinates, if known.

This is the pull counterpart to the PlatformWindow::update_ime_position push: mobile platforms ask for the focused element’s geometry when they need it (e.g. to frame system text-interaction UI overlaid on the focused element).

Source

fn text_length_utf16( &mut self, _window: &mut Window, _cx: &mut App, ) -> Option<usize>

Get the length of the document in UTF-16 characters, if known.

Source

fn apple_press_and_hold_enabled(&mut self) -> bool

Allows a given input context to opt into getting raw key repeats instead of sending these to the platform. TODO: Ideally we should be able to set ApplePressAndHoldEnabled in NSUserDefaults (which is how iTerm does it) but it doesn’t seem to work for me.

Source

fn accepts_text_input(&mut self, _window: &mut Window, _cx: &mut App) -> bool

Returns whether this handler is accepting text input to be inserted.

Source

fn text_input_editable_range( &mut self, _window: &mut Window, _cx: &mut App, ) -> Option<Range<usize>>

The contiguous range of text, in UTF-16 code units, that platform text input may read and edit around the current selection.

Platforms that mirror document text into an IME-editable buffer clamp the mirrored window to this range, so multi-step IME edit gestures (word deletion, autocorrect rewrites, suggestion picks) cannot reach content outside it. The range should contain the current selection; when it cannot (a selection spanning a region boundary), platforms degrade the mirrored IME context rather than widening the range. None places no bound.

Source

fn prefers_ime_for_printable_keys( &mut self, _window: &mut Window, _cx: &mut App, ) -> bool

Returns whether printable keys should be routed to the IME before keybinding matching when a non-ASCII input source (e.g. Japanese, Korean, Chinese IME) is active. This prevents multi-stroke keybindings like jj from intercepting keys that the IME should compose.

Defaults to false. The editor overrides this based on whether it expects character input (e.g. Vim insert mode returns true, normal mode returns false). The terminal keeps the default false so that raw keys reach the terminal process.

Source

fn text_input_configuration( &mut self, _window: &mut Window, _cx: &mut App, ) -> TextInputConfiguration

Get this handler’s preferences for platform text assistance.

GPUI re-queries this every frame and forwards it to the platform window only when it changes, so implementations must be cheap and may vary the result with application state (e.g. with the cursor’s position).

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§