Skip to main content

TextField

Struct TextField 

Source
pub struct TextField { /* private fields */ }
Expand description

A text field. Shape decides whether it is one line or many; everything else about it is the same either way.

Implementations§

Source§

impl TextField

Source

pub fn new(cx: &mut Context<'_, Self>) -> Self

Source

pub fn with_undo_limit(self, limit: usize) -> Self

How many undo steps to keep. App-wide configuration would be a gpui global alongside crate::input::init, not a Theme field — the theme is rebuilt on every light/dark switch, which would quietly reset anything behavioural parked in it.

Source

pub fn with_mono(self) -> Self

Use the current theme’s monospace family for text and placeholder.

Source

pub fn with_placeholder(self, placeholder: impl Into<SharedString>) -> Self

Source

pub fn with_key_context(self, context: impl Into<SharedString>) -> Self

Claim an extra key context on this field, so an app can bind a key here that it does not want bound in every other field.

The composer case, and the reason this exists: enter sends a message and shift-enter breaks a line, while the notes field two panels over still takes enter as a newline. gpui resolves a keystroke to the binding whose context matches deepest in the focus path, and nothing is deeper than the focused field — so a container around it cannot win enter, however it is bound. Rebinding MULTILINE_KEY_CONTEXT globally would win, and would take the newline away from every other multi-line field in the app. A context of the field’s own is the only thing that is both deep enough and narrow enough.

ⓘ
const COMPOSER: &str = "Composer";
cx.bind_keys([
    KeyBinding::new("enter", Send, Some(COMPOSER)),
    KeyBinding::new("shift-enter", input::InsertNewline, Some(COMPOSER)),
]);
let field = cx.new(|cx| {
    TextField::new(cx)
        .with_shape(Shape::Grow { min: 3, max: 12 })
        .with_key_context(COMPOSER)
});
Source

pub fn with_frame(self, frame: bool) -> Self

Draw without the box, padding and focus ring. The caller owns the spacing then, and owns showing that the field is focused.

Source

pub fn with_metrics(self, metrics: Metrics) -> Self

Set the text in something other than body copy — cx.typography().h1 sets a field the way a document sets its own heading.

Source

pub fn set_metrics(&mut self, metrics: Metrics, cx: &mut Context<'_, Self>)

Update typography without replacing the editing state.

Source

pub fn with_shape(self, shape: Shape) -> Self

Source

pub fn with_case(self, case: Case) -> Self

Source

pub fn set_case(&mut self, case: Case)

The case from here on. What the field already holds is left as it is: the text came from somewhere else, and a caller switching the case of a shared field would otherwise rewrite the name it is showing.

Source

pub fn case(&self) -> Case

Source

pub fn shape(&self) -> Shape

Source

pub fn content(&self) -> &SharedString

Source

pub fn spans(&self) -> &[(Range<usize>, HighlightKind)]

The syntax spans this field is painting — see Self::set_spans.

Source

pub fn set_content( &mut self, content: impl Into<SharedString>, cx: &mut Context<'_, Self>, )

Replace the content, putting the cursor at the end. Clears the undo history.

Source

pub fn clear(&mut self, cx: &mut Context<'_, Self>)

Source

pub fn set_spans( &mut self, spans: Vec<(Range<usize>, HighlightKind)>, cx: &mut Context<'_, Self>, )

Paint these byte ranges in syntax colours, in document order. Whatever they leave uncovered stays the field’s own text colour, so a language nothing can colour is simply no spans at all.

This field holds text, not a document: it does not parse. Each edit it applies moves the spans through Edit::map, so until the caller hands over fresh ones — FieldEvent::Changed carries the edit to reparse from — they stay on the characters they were computed for.

set_content clears them: replacing the text replaces what the colours were about.

Source

pub fn set_matches( &mut self, matches: Vec<Range<usize>>, cx: &mut Context<'_, Self>, )

Wash these byte ranges behind the text, as search matches. Not kept in step with edits, and cleared by Self::set_content, the same as Self::set_spans. A range past the end or off a char boundary is not painted.

Source

pub fn set_chips(&mut self, chips: Vec<Chip>, cx: &mut Context<'_, Self>)

Paint these ranges of the content as chips — see Chip.

The content keeps the source text; only painting and editing change. The caret steps over a chip, a selection that cuts one takes all of it, and one delete removes it. Each edit moves the chips with the text, and an edit reaching inside one drops it. Undo and redo bring back the chips each step had.

A caret inside a chip moves to its end. Self::set_content clears them.

Source

pub fn chips(&self) -> &[Chip]

The chips this field is painting — see Self::set_chips.

Source

pub fn select(&mut self, range: Range<usize>, cx: &mut Context<'_, Self>)

Select range and scroll it into view, with the caret at its end. Clamped to the content and to char boundaries, and grown over any chip it cuts.

Source

pub fn set_placeholder( &mut self, placeholder: impl Into<SharedString>, cx: &mut Context<'_, Self>, )

Self::with_placeholder after construction, for a hint that follows something else — the field naming whichever agent, file or channel is selected rather than being rebuilt each time one is.

Source

pub fn cursor(&self) -> usize

Where the caret is, as a byte offset into Self::content.

What a caller needs to read the text behind the caret: the # an autocomplete triggers on, the word a lookup would act on. With a selection this is the moving end, which is where typing would land.

Source

pub fn offset_bounds(&self, offset: usize) -> Option<Bounds<Pixels>>

Where a byte offset sits on screen — the bounds of the row it is on, in window coordinates.

The anchor for anything that hangs off a position in the text rather than off the field: a mention picker under the # that opened it, in a box that is also growing a row at a time. Pass it to crate::popover::menu_at, which takes a window point.

None until the field has painted once — this is measured off the shaped layout, and there is none before then.

Trait Implementations§

Source§

impl EntityInputHandler for TextField

Source§

fn text_for_range( &mut self, range_utf16: Range<usize>, actual_range: &mut Option<Range<usize>>, _window: &mut Window, _cx: &mut Context<'_, Self>, ) -> Option<String>

Source§

fn selected_text_range( &mut self, _ignore_disabled_input: bool, _window: &mut Window, _cx: &mut Context<'_, Self>, ) -> Option<UTF16Selection>

Source§

fn marked_text_range( &self, _window: &mut Window, _cx: &mut Context<'_, Self>, ) -> Option<Range<usize>>

Source§

fn unmark_text(&mut self, _window: &mut Window, _cx: &mut Context<'_, Self>)

Source§

fn replace_text_in_range( &mut self, range_utf16: Option<Range<usize>>, new_text: &str, _: &mut Window, cx: &mut Context<'_, Self>, )

Source§

fn replace_and_mark_text_in_range( &mut self, range_utf16: Option<Range<usize>>, new_text: &str, new_selected_range_utf16: Option<Range<usize>>, _window: &mut Window, cx: &mut Context<'_, Self>, )

Source§

fn bounds_for_range( &mut self, range_utf16: Range<usize>, bounds: Bounds<Pixels>, _window: &mut Window, _cx: &mut Context<'_, Self>, ) -> Option<Bounds<Pixels>>

Source§

fn character_index_for_point( &mut self, point: Point<Pixels>, _window: &mut Window, _cx: &mut Context<'_, Self>, ) -> Option<usize>

Source§

fn paste( &mut self, item: ClipboardItem, window: &mut Window, cx: &mut Context<'_, Self>, )

See InputHandler::paste for details
Source§

fn set_selected_text_range( &mut self, _range_utf16: Range<usize>, _window: &mut Window, _cx: &mut Context<'_, Self>, )

Source§

fn text_length_utf16( &mut self, _window: &mut Window, _cx: &mut Context<'_, Self>, ) -> Option<usize>

Source§

fn accepts_text_input( &self, _window: &mut Window, _cx: &mut Context<'_, Self>, ) -> bool

Source§

fn text_input_configuration( &mut self, _window: &mut Window, _cx: &mut Context<'_, Self>, ) -> TextInputConfiguration

Source§

fn text_input_editable_range( &mut self, _window: &mut Window, _cx: &mut Context<'_, Self>, ) -> Option<Range<usize>>

Source§

impl EventEmitter<FieldEvent> for TextField

Source§

impl Focusable for TextField

Source§

fn focus_handle(&self, _: &App) -> FocusHandle

Returns the focus handle associated with this view.
Source§

impl Render for TextField

Source§

fn render( &mut self, _window: &mut Window, cx: &mut Context<'_, Self>, ) -> impl IntoElement

Render this view into an element tree.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more