pub struct CodeEditor { /* private fields */ }Expand description
Canvas-based high-performance text editor.
Implementations§
Source§impl CodeEditor
impl CodeEditor
Source§impl CodeEditor
impl CodeEditor
Source§impl CodeEditor
impl CodeEditor
Sourcepub fn set_font_size(&mut self, size: f32, auto_adjust_line_height: bool)
pub fn set_font_size(&mut self, size: f32, auto_adjust_line_height: bool)
Sets the font size and recalculates character dimensions.
If auto_adjust_line_height is true, line_height will also be scaled to maintain
the default proportion (Line Height ~ 1.43x).
§Arguments
size- The font size in pixelsauto_adjust_line_height- Whether to automatically adjust the line height
Sourcepub fn char_width(&self) -> f32
pub fn char_width(&self) -> f32
Sourcepub fn full_char_width(&self) -> f32
pub fn full_char_width(&self) -> f32
Returns the width of a wide character (e.g. CJK) in pixels.
§Returns
The full character width in pixels
Sourcepub fn set_line_height(&mut self, height: f32)
pub fn set_line_height(&mut self, height: f32)
Sourcepub fn line_height(&self) -> f32
pub fn line_height(&self) -> f32
Sourcepub fn with_viewport_height(self, height: f32) -> Self
pub fn with_viewport_height(self, height: f32) -> Self
Sets the viewport height for the editor.
This determines the minimum height of the canvas, ensuring proper background rendering even when content is smaller than the viewport.
§Arguments
height- The viewport height in pixels
§Returns
Self for method chaining
§Example
use iced_code_editor::CodeEditor;
let editor = CodeEditor::new("fn main() {}", "rs")
.with_viewport_height(500.0);Sourcepub fn set_language(&mut self, language: Language)
pub fn set_language(&mut self, language: Language)
Sets the language for UI translations.
This changes the language used for all UI text elements in the editor, including search dialog tooltips, placeholders, and labels.
§Arguments
language- The language to use for UI text
§Example
use iced_code_editor::{CodeEditor, Language};
let mut editor = CodeEditor::new("fn main() {}", "rs");
editor.set_language(Language::French);Sourcepub fn request_focus(&self)
pub fn request_focus(&self)
Requests focus for this editor.
This method programmatically sets the focus to this editor instance, allowing it to receive keyboard events. Other editors will automatically lose focus.
§Example
use iced_code_editor::CodeEditor;
let mut editor1 = CodeEditor::new("fn main() {}", "rs");
let mut editor2 = CodeEditor::new("fn test() {}", "rs");
// Give focus to editor2
editor2.request_focus();Sourcepub fn is_focused(&self) -> bool
pub fn is_focused(&self) -> bool
Checks if this editor currently has focus.
Returns true if this editor will receive keyboard events,
false otherwise.
§Returns
true if focused, false otherwise
§Example
use iced_code_editor::CodeEditor;
let editor = CodeEditor::new("fn main() {}", "rs");
if editor.is_focused() {
println!("Editor has focus");
}Sourcepub fn reset(&mut self, content: &str) -> Task<Message>
pub fn reset(&mut self, content: &str) -> Task<Message>
Resets the editor with new content.
This method replaces the buffer content and resets all editor state
(cursor position, selection, scroll, history) to initial values.
Use this instead of creating a new CodeEditor instance to ensure
proper widget tree updates in iced.
Returns a Task that scrolls the editor to the top, which also
forces a redraw of the canvas.
§Arguments
content- The new text content
§Returns
A Task<Message> that should be returned from your update function
§Example
use iced_code_editor::CodeEditor;
let mut editor = CodeEditor::new("initial content", "lua");
// Later, reset with new content and get the task
let task = editor.reset("new content");
// Return task.map(YourMessage::Editor) from your update functionSourcepub fn is_modified(&self) -> bool
pub fn is_modified(&self) -> bool
Returns whether the editor has unsaved changes.
§Returns
true if there are unsaved modifications, false otherwise
Sourcepub fn mark_saved(&mut self)
pub fn mark_saved(&mut self)
Marks the current state as saved.
Call this after successfully saving the file to reset the modified state.
Sourcepub fn set_wrap_enabled(&mut self, enabled: bool)
pub fn set_wrap_enabled(&mut self, enabled: bool)
Sets whether line wrapping is enabled.
When enabled, long lines will wrap at the viewport width or at a configured column width.
§Arguments
enabled- Whether to enable line wrapping
§Example
use iced_code_editor::CodeEditor;
let mut editor = CodeEditor::new("fn main() {}", "rs");
editor.set_wrap_enabled(false); // Disable wrappingSourcepub fn wrap_enabled(&self) -> bool
pub fn wrap_enabled(&self) -> bool
Returns whether line wrapping is enabled.
§Returns
true if line wrapping is enabled, false otherwise
Sourcepub fn set_search_replace_enabled(&mut self, enabled: bool)
pub fn set_search_replace_enabled(&mut self, enabled: bool)
Enables or disables the search/replace functionality.
When disabled, search/replace keyboard shortcuts (Ctrl+F, Ctrl+H, F3) will be ignored. If the search dialog is currently open, it will be closed.
§Arguments
enabled- Whether to enable search/replace functionality
§Example
use iced_code_editor::CodeEditor;
let mut editor = CodeEditor::new("fn main() {}", "rs");
editor.set_search_replace_enabled(false); // Disable search/replaceSourcepub fn search_replace_enabled(&self) -> bool
pub fn search_replace_enabled(&self) -> bool
Returns whether search/replace functionality is enabled.
§Returns
true if search/replace is enabled, false otherwise
Sourcepub fn with_wrap_enabled(self, enabled: bool) -> Self
pub fn with_wrap_enabled(self, enabled: bool) -> Self
Sourcepub fn with_wrap_column(self, column: Option<usize>) -> Self
pub fn with_wrap_column(self, column: Option<usize>) -> Self
Sets the wrap column (fixed width wrapping).
When set to Some(n), lines will wrap at column n.
When set to None, lines will wrap at the viewport width.
§Arguments
column- The column to wrap at, or None for viewport-based wrapping
§Example
use iced_code_editor::CodeEditor;
let editor = CodeEditor::new("fn main() {}", "rs")
.with_wrap_column(Some(80)); // Wrap at 80 charactersSourcepub fn set_line_numbers_enabled(&mut self, enabled: bool)
pub fn set_line_numbers_enabled(&mut self, enabled: bool)
Sets whether line numbers are displayed.
When disabled, the gutter is completely removed (0px width), providing more space for code display.
§Arguments
enabled- Whether to display line numbers
§Example
use iced_code_editor::CodeEditor;
let mut editor = CodeEditor::new("fn main() {}", "rs");
editor.set_line_numbers_enabled(false); // Hide line numbersSourcepub fn line_numbers_enabled(&self) -> bool
pub fn line_numbers_enabled(&self) -> bool
Returns whether line numbers are displayed.
§Returns
true if line numbers are displayed, false otherwise
Sourcepub fn with_line_numbers_enabled(self, enabled: bool) -> Self
pub fn with_line_numbers_enabled(self, enabled: bool) -> Self
Sourcepub fn lose_focus(&mut self)
pub fn lose_focus(&mut self)
Removes canvas focus from this editor.
This method programmatically removes focus from the canvas, preventing it from receiving keyboard events. The cursor will be hidden, but the selection will remain visible.
Call this when focus should move to another widget (e.g., text input).
§Example
use iced_code_editor::CodeEditor;
let mut editor = CodeEditor::new("fn main() {}", "rs");
editor.lose_focus();Trait Implementations§
Source§impl Program<Message> for CodeEditor
impl Program<Message> for CodeEditor
Source§fn draw(
&self,
_state: &Self::State,
renderer: &Renderer,
_theme: &Theme,
bounds: Rectangle,
_cursor: Cursor,
) -> Vec<Geometry> ⓘ
fn draw( &self, _state: &Self::State, renderer: &Renderer, _theme: &Theme, bounds: Rectangle, _cursor: Cursor, ) -> Vec<Geometry> ⓘ
Source§fn update(
&self,
_state: &mut Self::State,
event: &Event,
bounds: Rectangle,
cursor: Cursor,
) -> Option<Action<Message>>
fn update( &self, _state: &mut Self::State, event: &Event, bounds: Rectangle, cursor: Cursor, ) -> Option<Action<Message>>
Source§fn mouse_interaction(
&self,
_state: &Self::State,
_bounds: Rectangle,
_cursor: Cursor,
) -> Interaction
fn mouse_interaction( &self, _state: &Self::State, _bounds: Rectangle, _cursor: Cursor, ) -> Interaction
Auto Trait Implementations§
impl !Freeze for CodeEditor
impl !RefUnwindSafe for CodeEditor
impl Send for CodeEditor
impl !Sync for CodeEditor
impl Unpin for CodeEditor
impl UnwindSafe for CodeEditor
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<State, Message> IntoBoot<State, Message> for State
impl<State, Message> IntoBoot<State, Message> for State
Source§fn into_boot(self) -> (State, Task<Message>)
fn into_boot(self) -> (State, Task<Message>)
Application.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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