Components / controls
RichTextEditor view
The visible projection over a caller-owned rich-text editing session.
A view survives a frame. Hold it in an Entity with cx.new(..) and reach it with .update(..).
use gpui_kit::controls::rich_text_editor::RichTextEditor;
Reviewed in editor-options rich-text-editor.
Construct
new(ident: impl Into<Ident>, session: Entity<RichTextEditSession>, new_block_id: impl Fn() -> RichTextBlockId + 'static, window: &mut Window, cx: &mut Context<Self>) -> Self
Options
Chain onto the value.
placeholder(placeholder: impl Into<SharedString>) -> Self
name(name: impl Into<SharedString>) -> Self
frame(frame: Frame) -> Self
toolbar(toolbar: bool) -> Self
rows(rows: usize) -> Self
max_rows(max_rows: usize) -> Self
invalid(invalid: bool) -> Self
required(required: bool) -> Self
read_only(read_only: bool) -> Self
diagnostics(diagnostics: impl IntoIterator<Item = RichTextDiagnostic>) -> Self
Commands
Need a Context, so they need a view.
set_name(name: impl Into<SharedString>, cx: &mut Context<Self>)
set_placeholder(placeholder: impl Into<SharedString>, cx: &mut Context<Self>)
set_frame(frame: Frame, cx: &mut Context<Self>)
set_toolbar(toolbar: bool, cx: &mut Context<Self>)
set_rows(rows: usize, cx: &mut Context<Self>)
set_max_rows(max_rows: Option<usize>, cx: &mut Context<Self>)
set_diagnostics(diagnostics: impl IntoIterator<Item = RichTextDiagnostic>, cx: &mut Context<Self>)
set_disabled(disabled: bool, cx: &mut Context<Self>)
set_read_only(read_only: bool, cx: &mut Context<Self>)
set_invalid(invalid: bool, cx: &mut Context<Self>)
set_required(required: bool, cx: &mut Context<Self>)
apply_intent(intent: RichTextIntent, cx: &mut Context<Self>)
Queries
Only answer; they change nothing.
session() -> &Entity<RichTextEditSession>
is_disabled() -> bool
is_read_only() -> bool
Reports
The variants of the event it emits. It never applies the change itself.
- IntentApplied
- IntentRefused
- LinkRequested
- Focus
- Blur
Example ยท editor-options
fn editor_options(window: &mut Window, cx: &mut App) -> AnyElement {
if !cx.has_global::<SceneEditorOptions>() {
let area = cx.new(|cx| {
TextArea::new("scene.options.area", window, cx)
.text("Caller text survives layout changes. ็ ๐\nA retained second line.")
});
area.update(cx, |area, cx| {
area.set_control_size(ControlSize::Lg, cx);
area.set_autosize(Some((2, 4)), cx);
area.set_required(true, cx);
});
let editor = cx.new(|cx| {
Editor::new(
"scene.options.editor",
"Retained source fixture",
"let unchanged = 13;\n// same entity, new options",
window,
cx,
)
});
editor.update(cx, |editor, cx| {
editor.set_rows(3, cx);
editor.set_line_numbers(false, cx);
editor.set_read_only(true, cx);
});
let document = RichTextDocument::new([RichTextBlock::new(
"options-body",
"Caller-owned rich text remains attached to the same session.",
)])
.expect("fixture document is valid");
let session = cx.new(|_| RichTextEditSession::new(document));
let next_id = Rc::new(std::cell::Cell::new(0_u64));
let rich = cx.new(|cx| {
RichTextEditor::new(
"scene.options.rich",
session,
move || {
let id = next_id.get() + 1;
next_id.set(id);
RichTextBlockId::new(format!("options-{id}"))
},
window,
cx,
)
});
rich.update(cx, |editor, cx| {
editor.set_rows(2, cx);
editor.set_max_rows(None, cx);
editor.set_toolbar(false, cx);
});
cx.set_global(SceneEditorOptions(area, editor, rich));
}
let theme = cx.theme().clone();
let editors = cx.global::<SceneEditorOptions>();
stack(&theme).w(px(720.0))
.child(caption(&theme, "fixture ยท retained options: large autosizing text / read-only source without gutter / fixed rich text without toolbar"))
.child(editors.0.clone()).child(editors.1.clone()).child(editors.2.clone())
.into_any_element()
}
Source: crates/gpui-kit/src/controls/rich_text_editor/mod.rs