diff --git a/egui/src/memory.rs b/egui/src/memory.rs index d59af77b2..da42ce4fc 100644 --- a/egui/src/memory.rs +++ b/egui/src/memory.rs @@ -190,6 +190,7 @@ impl Focus { } ) { self.id = None; + self.is_focus_locked = false; break; } @@ -293,9 +294,17 @@ impl Memory { self.interaction.focus.id == Some(id) } - pub(crate) fn lock_focus(&mut self, id: Id) { + pub(crate) fn lock_focus(&mut self, id: Id, b: bool) { if self.had_focus_last_frame(id) && self.has_focus(id) { - self.interaction.focus.is_focus_locked = true; + self.interaction.focus.is_focus_locked = b; + } + } + + pub(crate) fn has_lock_focus(&mut self, id: Id) -> bool { + if self.had_focus_last_frame(id) && self.has_focus(id) { + self.interaction.focus.is_focus_locked + } else { + false } } diff --git a/egui/src/widgets/text_edit.rs b/egui/src/widgets/text_edit.rs index fadf7b209..235f07282 100644 --- a/egui/src/widgets/text_edit.rs +++ b/egui/src/widgets/text_edit.rs @@ -136,7 +136,7 @@ pub struct TextEdit<'t> { desired_width: Option, desired_height_rows: usize, tab_as_spaces: bool, - tab_size: usize, + tab_moves_focus: bool, } impl<'t> TextEdit<'t> { pub fn cursor(ui: &Ui, id: Id) -> Option { @@ -167,8 +167,8 @@ impl<'t> TextEdit<'t> { enabled: true, desired_width: None, desired_height_rows: 1, - tab_as_spaces: true, - tab_size: 4, + tab_as_spaces: false, + tab_moves_focus: true, } } @@ -186,8 +186,8 @@ impl<'t> TextEdit<'t> { enabled: true, desired_width: None, desired_height_rows: 4, - tab_as_spaces: true, - tab_size: 4, + tab_as_spaces: false, + tab_moves_focus: true, } } @@ -202,14 +202,18 @@ impl<'t> TextEdit<'t> { self } - /// Registers if this widget will insert spaces instead of tab char + /// When this is true, then pass focus to the next + /// widget. + /// + /// When this is false, then insert identation based on the value of + /// `tab_as_spaces` property. /// /// ```rust, ignore /// ui.add(egui::TextEdit::multiline(&mut self.multiline_text_input) - /// .tab_size(4)); + /// .tab_moves_focus(true)); /// ``` - pub fn tab_size(mut self, size: usize) -> Self { - self.tab_size = size; + pub fn tab_moves_focus(mut self, b: bool) -> Self { + self.tab_moves_focus = b; self } @@ -326,7 +330,7 @@ impl<'t> TextEdit<'t> { desired_width, desired_height_rows, tab_as_spaces, - tab_size, + tab_moves_focus, } = self; let text_style = text_style.unwrap_or_else(|| ui.style().body_text_style); @@ -413,9 +417,7 @@ impl<'t> TextEdit<'t> { } if ui.memory().has_focus(id) && enabled { - if multiline { - ui.memory().lock_focus(id); - } + ui.memory().lock_focus(id, !tab_moves_focus); let mut cursorp = state .cursorp @@ -481,16 +483,12 @@ impl<'t> TextEdit<'t> { if multiline { let mut ccursor = delete_selected(text, &cursorp); - if tab_as_spaces { - let mut spaces = String::with_capacity(tab_size); - - for _ in 0..tab_size { - spaces.push(' '); + if ui.memory().has_lock_focus(id) { + if tab_as_spaces { + insert_text(&mut ccursor, text, " "); + } else { + insert_text(&mut ccursor, text, "\t"); } - - insert_text(&mut ccursor, text, &spaces); - } else { - insert_text(&mut ccursor, text, "\t"); } Some(CCursorPair::one(ccursor)) diff --git a/egui_demo_lib/src/apps/demo/widgets.rs b/egui_demo_lib/src/apps/demo/widgets.rs index de3a5fc8a..5c7450cff 100644 --- a/egui_demo_lib/src/apps/demo/widgets.rs +++ b/egui_demo_lib/src/apps/demo/widgets.rs @@ -25,6 +25,7 @@ pub struct Widgets { single_line_text_input: String, tab_size: usize, tab_as_spaces: bool, + tab_moves_focus: bool, multiline_text_input: String, } @@ -40,6 +41,7 @@ impl Default for Widgets { tab_size: 4, tab_as_spaces: true, + tab_moves_focus: false, multiline_text_input: r#"Text can both be so wide that it needs a line break, but you can also add manual line break by pressing enter, creating new paragraphs. This is the start of the next paragraph. @@ -149,17 +151,14 @@ impl Widgets { ui.separator(); - ui.add(egui::Slider::usize(&mut self.tab_size, 2..=8).text("Tab size")); - - ui.separator(); - ui.checkbox(&mut self.tab_as_spaces, "Tabs as spaces"); + ui.checkbox(&mut self.tab_moves_focus, "Tabs moves focus"); }); ui.add( egui::TextEdit::multiline(&mut self.multiline_text_input) - .tab_as_spaces(self.tab_as_spaces) - .tab_size(self.tab_size), + .tab_moves_focus(self.tab_moves_focus) + .tab_as_spaces(self.tab_as_spaces), ); } }