Skip to main content

TextModel

Struct TextModel 

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

A native plain-text editor. Cheap to clone (a handle); main thread only.

Implementations§

Source§

impl TextModel

Source

pub fn new(id: ViewId, text: &str) -> Self

Creates the editor (added to the window the first time the host draws it). Defaults: plain text, system font 13 pt, continuous spell checking, find bar, undo, Tab inserts a tab (Ctrl-Tab moves focus on).

Source

pub fn id(&self) -> ViewId

The view id the editor was created with.

Source

pub fn ensure_mounted(&self)

Adds the editor to the attached window if it isn’t yet (the host widget calls this before placing it).

Source

pub fn text(&self) -> String

The whole text.

Source

pub fn set_text(&self, text: &str)

Replaces the whole text (e.g. loading a document). Clears the undo history and emits no event.

Source

pub fn share_undo(&self, manager: Option<&NSUndoManager>)

Records edits in manager (e.g. the window’s, shared with other views and app-level changes) instead of the editor’s own history, or in a fresh history of its own again with None. Edits recorded so far are dropped.

Source

pub fn undo_manager(&self) -> Retained<NSUndoManager> ⓘ

The undo history the editor records in.

Source

pub fn replace_selection(&self, text: &str)

Replaces the selection as if typed: undoable, emits Changed.

Source

pub fn selection(&self) -> Range<usize> ⓘ

Selected byte range (empty: the caret).

Source

pub fn set_selection(&self, range: Range<usize>)

Selects a byte range (emits no event).

Source

pub fn composition(&self) -> Option<String>

Whether an input method is composing, and its marked text.

Source

pub fn set_editable(&self, editable: bool)

Read-only (false) or editable. Read-only text can still be selected and copied.

Source

pub fn is_editable(&self) -> bool

Whether the user can edit the text.

Source

pub fn set_font(&self, name: Option<&str>, size: f64)

Font: None for the system font, else a family or PostScript name.

Source

pub fn set_highlighter( &self, f: Option<impl Fn(&str) -> Vec<(Range<usize>, TextStyle)> + 'static>, )

Styles the text with f: given the text, it returns runs (UTF-8 byte ranges) and their styles; the rest is plain. It runs now and after every edit (typing, paste, undo, set_text), before the edit is drawn, so keep it fast: it sees the whole text each time. None makes the text plain again.

Source

pub fn restyle(&self)

Runs the highlighter again, for when what it depends on changed other than the text (a search term, a theme).

Source

pub fn style_at(&self, offset: usize) -> TextStyle

The style of the character at byte offset, as shown (for checks).

Source

pub fn set_rich(&self, rich: bool)

Rich text (true) or plain (false, the default). Rich, the user styles the text: fonts and sizes (the font panel, ⌘T), bold, italic and underline (⌘B, ⌘I, ⌘U), colours, alignment, spacing and lists (the ruler and the format bar); pasted text keeps its styles, and pictures can be pasted or dropped in. Don’t combine it with a highlighter, which restyles the text itself.

Source

pub fn is_rich(&self) -> bool

Whether the text is rich (see set_rich).

Source

pub fn set_format_bar(&self, shown: bool)

TextEdit’s format bar above the text (rich text): font, size, colours, bold, italic, underline, alignment, line spacing and lists.

Source

pub fn has_format_bar(&self) -> bool

Whether the format bar is shown.

Source

pub fn set_ruler_visible(&self, shown: bool)

The ruler above the text (rich text): margins, tab stops, alignment, spacing and lists. Format ▸ Text ▸ Show Ruler (⌃⌘R) toggles it too.

Source

pub fn is_ruler_visible(&self) -> bool

Whether the ruler is shown.

Source

pub fn rtf(&self) -> Vec<u8> ⓘ

The text with its styles, as RTF (what TextEdit saves). Pictures aren’t included.

Source

pub fn set_rtf(&self, rtf: &[u8]) -> bool

Replaces the text with RTF’s (from rtf, TextEdit or Word), styles and all. Clears the undo history and emits no event. False (and nothing changes) if rtf isn’t RTF.

Source

pub fn html(&self) -> String

The text with its styles as an HTML document (for the web, mail or the clipboard).

Source

pub fn set_html(&self, html: &str) -> bool

Replaces the text with HTML’s, styles and all (headings, bold, links, lists). Clears the undo history and emits no event. False if AppKit can’t read it.

Source

pub fn font_size(&self) -> Option<f64>

The current font size in points.

Source

pub fn set_insets(&self, horizontal: f64, vertical: f64)

Space between the text and the view’s edges, in points.

Source

pub fn set_spell_checking(&self, on: bool)

Continuous spell checking (on by default).

Source

pub fn set_grammar_checking(&self, on: bool)

Grammar checking (off by default).

Source

pub fn set_code_input(&self, code: bool)

Input for code (true) or prose (false, the default): code turns off what helps prose and breaks code: smart quotes and dashes (" becoming “), text replacement, spelling correction, spell checking and the spaces smart paste adds.

Source

pub fn set_accepts_tab(&self, accepts: bool)

Whether plain Tab inserts a tab (default) or moves focus on.

Source

pub fn show_find(&self)

Shows the find bar (as ⌘F does).

Source

pub fn is_find_visible(&self) -> bool

Whether the find bar is showing.

Source

pub fn can_undo(&self) -> bool

Whether there is something to undo.

Source

pub fn undo(&self)

Undoes the last edit (as ⌘Z does).

Source

pub fn drain_events(&self) -> Vec<TextEvent>

Takes the queued user events (for the host widget).

Source

pub fn scroll_view(&self) -> Retained<NSScrollView> ⓘ

The NSScrollView holding the text view, for hosts that place it themselves (no crate::attached window).

Source

pub fn text_view(&self) -> Retained<NSTextView> ⓘ

The underlying NSTextView (escape hatch).

Trait Implementations§

Source§

impl Clone for TextModel

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TextModel

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> AutoreleaseSafe for T
where T: ?Sized,

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<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.