1
0
mirror of https://github.com/emilk/egui.git synced 2026-09-01 06:10:06 -04:00

Clean up new code editor code

This commit is contained in:
Emil Ernerfeldt
2021-05-02 19:28:02 +02:00
parent 10b60d5361
commit 7da9928548
6 changed files with 33 additions and 69 deletions

View File

@@ -943,13 +943,6 @@ impl Ui {
self.add(TextEdit::multiline(text).code_editor())
}
/// A `TextEdit` for code editing with configurable `Tab` management.
///
/// Se also [`TextEdit::code_editor_with_config`].
pub fn code_editor_with_config(&mut self, text: &mut String, config: CodingConfig) -> Response {
self.add(TextEdit::multiline(text).code_editor_with_config(config))
}
/// Usage: `if ui.button("Click me").clicked() { … }`
///
/// Shortcut for `add(Button::new(text))`

View File

@@ -108,12 +108,6 @@ impl CCursorPair {
}
}
#[derive(Clone, Copy, Debug, Default)]
#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))]
pub struct CodingConfig {
pub tab_moves_focus: bool,
}
/// A text region that the user can edit the contents of.
///
/// See also [`Ui::text_edit_singleline`] and [`Ui::text_edit_multiline`].
@@ -146,7 +140,7 @@ pub struct TextEdit<'t> {
enabled: bool,
desired_width: Option<f32>,
desired_height_rows: usize,
tab_moves_focus: bool,
lock_focus: bool,
}
impl<'t> TextEdit<'t> {
pub fn cursor(ui: &Ui, id: Id) -> Option<CursorPair> {
@@ -178,7 +172,7 @@ impl<'t> TextEdit<'t> {
enabled: true,
desired_width: None,
desired_height_rows: 1,
tab_moves_focus: true,
lock_focus: true,
}
}
@@ -197,37 +191,19 @@ impl<'t> TextEdit<'t> {
enabled: true,
desired_width: None,
desired_height_rows: 4,
tab_moves_focus: true,
lock_focus: true,
}
}
/// When this is true, then pass focus to the next
/// widget.
pub fn tab_moves_focus(mut self, b: bool) -> Self {
self.tab_moves_focus = b;
self
}
/// Build a `TextEdit` focused on code editing.
/// By default it comes with:
/// - monospaced font
/// - focus lock
pub fn code_editor(self) -> Self {
self.text_style(TextStyle::Monospace).tab_moves_focus(false)
}
/// Build a `TextEdit` focused on code editing with configurable `Tab` management.
///
/// Shortcut for:
/// ```rust, ignore
/// egui::TextEdit::multiline(code_snippet)
/// .code_editor()
/// .tab_moves_focus(tab_moves_focus);
/// ```
pub fn code_editor_with_config(self, config: CodingConfig) -> Self {
self.code_editor().tab_moves_focus(config.tab_moves_focus)
self.text_style(TextStyle::Monospace).lock_focus(true)
}
/// Use if you want to set an explicit `Id` for this widget.
pub fn id(mut self, id: Id) -> Self {
self.id = Some(id);
self
@@ -292,6 +268,16 @@ impl<'t> TextEdit<'t> {
self.desired_height_rows = desired_height_rows;
self
}
/// When `false` (default), pressing TAB will move focus
/// to the next widget.
///
/// When `true`, the widget will keep the focus and pressing TAB
/// will insert the `'\t'` character.
pub fn lock_focus(mut self, b: bool) -> Self {
self.lock_focus = b;
self
}
}
impl<'t> Widget for TextEdit<'t> {
@@ -348,7 +334,7 @@ impl<'t> TextEdit<'t> {
enabled,
desired_width,
desired_height_rows,
tab_moves_focus,
lock_focus,
} = self;
let text_style = text_style.unwrap_or_else(|| ui.style().body_text_style);
@@ -454,7 +440,7 @@ impl<'t> TextEdit<'t> {
let mut text_cursor = None;
if ui.memory().has_focus(id) && enabled {
ui.memory().lock_focus(id, !tab_moves_focus);
ui.memory().lock_focus(id, lock_focus);
let mut cursorp = state
.cursorp