1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-29 12:50:04 -04:00

Make the "tab-as-character" behaviour opt-in.

Added `tab_moves_focus()` to `TextEdit` so we can opt-in to the
"tab-as-character" behaviour.

Also because we decided to lock the tab size to 4 spaces, we gave
up on the `tab_size` property, but we kept the opt-in for the
"tab-as-spaces" behaviour.

An updated example of how to use `TextEdit` with those two opt-in
behaviours can be found in the "Misc Demos" demo.
This commit is contained in:
Cristian Dinu
2021-03-24 12:43:50 +02:00
parent 13ed86d932
commit 19e2608a56
3 changed files with 36 additions and 30 deletions

View File

@@ -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
}
}

View File

@@ -136,7 +136,7 @@ pub struct TextEdit<'t> {
desired_width: Option<f32>,
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<CursorPair> {
@@ -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))

View File

@@ -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),
);
}
}