Skip to main content

CompletionPopup

Struct CompletionPopup 

Source
pub struct CompletionPopup<'a> { /* private fields */ }
Available on crate feature egui only.
Expand description

A code completion popup over a TextEdit.

Shows a list of suggestions for the word under the cursor. Use the arrow keys to select one, and Enter or Tab to accept it. Escape closes the popup until the word changes. Clicking a suggestion also accepts it.

Keyboard focus stays in the TextEdit the whole time.

The popup starts intercepting keys the frame after it opens, so an accept key pressed in the same frame as the popup appears goes to the TextEdit.

let commands = ["/help", "/clear", "/quit"];
let output = egui::CompletionPopup::new(ui.make_persistent_id("prompt")).show(
    ui,
    egui::TextEdit::singleline(&mut text).hint_text("Type / for commands"),
    |query| {
        if !query.is_at_start() {
            return vec![];
        }
        commands
            .iter()
            .filter(|command| command.starts_with(query.word))
            .map(|command| egui::Suggestion::new(format!("{command} ")))
            .collect()
    },
);
if let Some(accepted) = output.accepted {
    println!("Accepted {}", accepted.insert);
}

Implementations§

Source§

impl<'a> CompletionPopup<'a>

Source

pub const CLASS_SUGGESTION: ClassName

Present on every suggestion button in the popup, so they can be styled.

Source

pub fn new(id: Id) -> CompletionPopup<'a>

The id is given to the TextEdit, and is also used to store the popup state.

It must be unique, so if you show several completion popups (e.g. one per tab), use e.g. Ui::make_persistent_id rather than a constant Id.

Source

pub fn word_boundary( self, is_word_boundary: impl Fn(char) -> bool + 'a, ) -> CompletionPopup<'a>

Which characters separate words? The current word is the text between the last such character and the cursor.

Default: char::is_whitespace.

Source

pub fn align(self, align: RectAlign) -> CompletionPopup<'a>

Where to place the popup relative to the TextEdit. If there is no room, the vertically flipped alignment is used instead.

Default: RectAlign::TOP_START.

Source

pub fn max_height(self, max_height: f32) -> CompletionPopup<'a>

Maximum height of the popup before it starts to scroll.

Default: 200.0.

Source

pub fn accept_keys( self, accept_keys: impl Into<Vec<Key>>, ) -> CompletionPopup<'a>

Which keys accept the selected suggestion?

Default: Enter and Tab.

Source

pub fn show( self, ui: &mut Ui, text_edit: TextEdit<'_>, suggest: impl FnOnce(&CompletionQuery<'_>) -> Vec<Suggestion>, ) -> CompletionOutput

Show the TextEdit and, when suggest returns anything for the word under the cursor, the popup.

The TextEdit is given the Id of this popup. Its TextEdit::event_filter is respected, except that Tab and Escape are also captured while the popup is open.

suggest is called once per frame, after the TextEdit has handled input.

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for CompletionPopup<'a>

§

impl<'a> !Send for CompletionPopup<'a>

§

impl<'a> !Sync for CompletionPopup<'a>

§

impl<'a> !UnwindSafe for CompletionPopup<'a>

§

impl<'a> Forget for CompletionPopup<'a>

§

impl<'a> Freeze for CompletionPopup<'a>
where NonZero<u64>: Freeze, *const dyn Fn(char) -> bool + 'a is !null: Freeze, PhantomData<dyn Fn(char) -> bool + 'a>: Freeze, *const u8 is !null: Freeze, PhantomData<Key>: Freeze,

§

impl<'a> Move for CompletionPopup<'a>

§

impl<'a> Unpin for CompletionPopup<'a>
where NonZero<u64>: Unpin, Box<dyn Fn(char) -> bool + 'a>: Unpin, *const u8 is !null: Unpin,

§

impl<'a> UnsafeUnpin for CompletionPopup<'a>
where NonZeroU64Inner: UnsafeUnpin, *const dyn Fn(char) -> bool + 'a is !null: UnsafeUnpin, PhantomData<dyn Fn(char) -> bool + 'a>: UnsafeUnpin, *const u8 is !null: UnsafeUnpin, PhantomData<Key>: UnsafeUnpin,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, S> SimdFrom<T, S> for T
where S: Simd,

Source§

fn simd_from(_simd: S, value: T) -> T

Source§

impl<F, T, S> SimdInto<T, S> for F
where T: SimdFrom<F, S>, S: Simd,

Source§

fn simd_into(self, simd: S) -> T

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<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