pub struct TextModel { /* private fields */ }Expand description
A native plain-text editor. Cheap to clone (a handle); main thread only.
Implementations§
Source§impl TextModel
impl TextModel
Sourcepub fn new(text: &str) -> Self
pub fn new(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).
Sourcepub fn set_text(&self, text: &str)
pub fn set_text(&self, text: &str)
Replaces the whole text (e.g. loading a document). Clears the undo history and emits no event.
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.
Sourcepub fn undo_manager(&self) -> Retained<NSUndoManager> ⓘ
pub fn undo_manager(&self) -> Retained<NSUndoManager> ⓘ
The undo history the editor records in.
Sourcepub fn replace_selection(&self, text: &str)
pub fn replace_selection(&self, text: &str)
Replaces the selection as if typed: undoable, emits Changed.
Sourcepub fn set_selection(&self, range: Range<usize>)
pub fn set_selection(&self, range: Range<usize>)
Selects a byte range (emits no event).
Sourcepub fn composition(&self) -> Option<String>
pub fn composition(&self) -> Option<String>
Whether an input method is composing, and its marked text.
Sourcepub fn set_editable(&self, editable: bool)
pub fn set_editable(&self, editable: bool)
Read-only (false) or editable. Read-only text can still be selected and copied.
Sourcepub fn is_editable(&self) -> bool
pub fn is_editable(&self) -> bool
Whether the user can edit the text.
Sourcepub fn set_font(&self, name: Option<&str>, size: f64)
pub fn set_font(&self, name: Option<&str>, size: f64)
Font: None for the system font, else a family or PostScript name.
Sourcepub fn set_completer(
&self,
f: Option<impl Fn(&str, usize) -> Option<Completion> + 'static>,
)
pub fn set_completer( &self, f: Option<impl Fn(&str, usize) -> Option<Completion> + 'static>, )
Completes as the user types, in AppKit’s completion list: f gets
the text and the caret (a byte offset) and says what it completes
there and with which words (Completion), or None. Typing a
word character or a dot opens the list (⌥⎋ or F5 open it too);
typing on filters it (f is asked again), ↑ and ↓ move, Return or
Tab or a click puts the word in, and Escape closes it.
Sourcepub fn offered_completions(&self) -> Vec<String>
pub fn offered_completions(&self) -> Vec<String>
The words the completion list showed last (for checks).
Sourcepub fn set_highlighter(
&self,
f: Option<impl Fn(&str) -> Vec<(Range<usize>, TextStyle)> + 'static>,
)
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.
Sourcepub fn restyle(&self)
pub fn restyle(&self)
Runs the highlighter again, for when what it depends on changed other than the text (a search term, a theme).
Sourcepub fn style_at(&self, offset: usize) -> TextStyle
pub fn style_at(&self, offset: usize) -> TextStyle
The style of the character at byte offset, as shown (for checks).
Sourcepub fn set_rich(&self, rich: bool)
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.
Sourcepub fn set_format_bar(&self, shown: bool)
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.
Sourcepub fn has_format_bar(&self) -> bool
pub fn has_format_bar(&self) -> bool
Whether the format bar is shown.
Sourcepub fn set_ruler_visible(&self, shown: bool)
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.
Sourcepub fn is_ruler_visible(&self) -> bool
pub fn is_ruler_visible(&self) -> bool
Whether the ruler is shown.
Sourcepub fn rtf(&self) -> Vec<u8> ⓘ
pub fn rtf(&self) -> Vec<u8> ⓘ
The text with its styles, as RTF (what TextEdit saves). Pictures aren’t included.
Sourcepub fn set_rtf(&self, rtf: &[u8]) -> bool
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.
Sourcepub fn html(&self) -> String
pub fn html(&self) -> String
The text with its styles as an HTML document (for the web, mail or the clipboard).
Sourcepub fn set_html(&self, html: &str) -> bool
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.
Sourcepub fn set_insets(&self, horizontal: f64, vertical: f64)
pub fn set_insets(&self, horizontal: f64, vertical: f64)
Space between the text and the view’s edges, in points.
Sourcepub fn set_spell_checking(&self, on: bool)
pub fn set_spell_checking(&self, on: bool)
Continuous spell checking (on by default).
Sourcepub fn set_grammar_checking(&self, on: bool)
pub fn set_grammar_checking(&self, on: bool)
Grammar checking (off by default).
Sourcepub fn set_code_input(&self, code: bool)
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.
Sourcepub fn set_accepts_tab(&self, accepts: bool)
pub fn set_accepts_tab(&self, accepts: bool)
Whether plain Tab inserts a tab (default) or moves focus on.
Sourcepub fn show_find(&self)
pub fn show_find(&self)
Shows the find bar, as Edit ▸ Find ▸ Find… (⌘F) does in a Ferrokit app while the focus is in the editor.
Sourcepub fn set_wraps(&self, wraps: bool)
pub fn set_wraps(&self, wraps: bool)
Whether long lines wrap at the editor’s width (the default), or run on with a horizontal scroller (code, logs, tables of text).
Sourcepub fn is_find_visible(&self) -> bool
pub fn is_find_visible(&self) -> bool
Whether the find bar is showing.
Sourcepub fn scroll_view(&self) -> Retained<NSScrollView> ⓘ
pub fn scroll_view(&self) -> Retained<NSScrollView> ⓘ
The NSScrollView holding the text view, for hosts that place it
themselves (no crate::attached window).
Sourcepub fn text_view(&self) -> Retained<NSTextView> ⓘ
pub fn text_view(&self) -> Retained<NSTextView> ⓘ
The underlying NSTextView (escape hatch).