simplification of autocomplete code
This commit is contained in:
parent
6041da764b
commit
e427e7a04e
@ -79,28 +79,9 @@ impl HintEnum<'static> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_multi(&self) -> bool { matches!(self, HintEnum::Many(_)) }
|
|
||||||
|
|
||||||
pub fn ensure_many(&self) -> &[&str] {
|
|
||||||
match self {
|
|
||||||
HintEnum::Many(data) => data,
|
|
||||||
_ => panic!("ensure_many called on non-Many value"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn is_some(&self) -> bool { !matches!(self, HintEnum::None) }
|
pub fn is_some(&self) -> bool { !matches!(self, HintEnum::None) }
|
||||||
|
|
||||||
pub fn is_none(&self) -> bool { !self.is_some() }
|
pub fn is_none(&self) -> bool { !self.is_some() }
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub fn ensure_single(&self) -> &&str {
|
|
||||||
match self {
|
|
||||||
HintEnum::Single(data) => data,
|
|
||||||
_ => panic!("ensure_single called on non-Single value"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub fn is_single(&self) -> bool { !self.is_multi() }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
include!(concat!(env!("OUT_DIR"), "/codegen.rs"));
|
include!(concat!(env!("OUT_DIR"), "/codegen.rs"));
|
||||||
|
|||||||
@ -66,61 +66,65 @@ impl AutoComplete {
|
|||||||
|
|
||||||
// If in focus and right arrow key was pressed, apply hint
|
// If in focus and right arrow key was pressed, apply hint
|
||||||
if func_edit_focus {
|
if func_edit_focus {
|
||||||
let mut push_cursor: bool = false;
|
|
||||||
let apply_key = ui.input().key_pressed(Key::ArrowRight) | enter_pressed | tab_pressed;
|
let apply_key = ui.input().key_pressed(Key::ArrowRight) | enter_pressed | tab_pressed;
|
||||||
|
|
||||||
if apply_key && let Some(single_hint) = self.hint.get_single() {
|
let push_cursor: bool = match self.hint {
|
||||||
push_cursor = true;
|
HintEnum::Single(hint) => {
|
||||||
new_string = string + &single_hint;
|
if apply_key {
|
||||||
} else if self.hint.is_multi() {
|
new_string = string + &hint;
|
||||||
let selections = self.hint.ensure_many();
|
true
|
||||||
|
} else {
|
||||||
let max_i = selections.len() as i16 - 1;
|
false
|
||||||
|
|
||||||
let mut i = self.i as i16;
|
|
||||||
|
|
||||||
if ui.input().key_pressed(Key::ArrowDown) {
|
|
||||||
i += 1;
|
|
||||||
if i > max_i {
|
|
||||||
i = 0;
|
|
||||||
}
|
|
||||||
} else if ui.input().key_pressed(Key::ArrowUp) {
|
|
||||||
i -= 1;
|
|
||||||
if 0 > i {
|
|
||||||
i = max_i
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
HintEnum::Many(hints) => {
|
||||||
|
let max_i = hints.len() as i16 - 1;
|
||||||
|
|
||||||
self.i = i as usize;
|
let mut i = self.i as i16;
|
||||||
|
|
||||||
// Doesn't need to have a number in id as there should only be 1 autocomplete popup in entire gui
|
if ui.input().key_pressed(Key::ArrowDown) {
|
||||||
let popup_id = ui.make_persistent_id("autocomplete_popup");
|
i += 1;
|
||||||
|
if i > max_i {
|
||||||
let mut clicked = false;
|
i = 0;
|
||||||
|
}
|
||||||
egui::popup_below_widget(ui, popup_id, &re, |ui| {
|
} else if ui.input().key_pressed(Key::ArrowUp) {
|
||||||
for (i, candidate) in selections.iter().enumerate() {
|
i -= 1;
|
||||||
if ui
|
if 0 > i {
|
||||||
.selectable_label(i == self.i, *candidate)
|
i = max_i
|
||||||
.clicked()
|
|
||||||
{
|
|
||||||
clicked = true;
|
|
||||||
self.i = i;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
if clicked | apply_key {
|
self.i = i as usize;
|
||||||
new_string += selections[self.i];
|
|
||||||
push_cursor = true;
|
|
||||||
|
|
||||||
|
// Doesn't need to have a number in id as there should only be 1 autocomplete
|
||||||
|
// popup in entire gui
|
||||||
|
let popup_id = ui.make_persistent_id("autocomplete_popup");
|
||||||
|
|
||||||
// don't need this here as it simply won't be display next frame in `math_app.rs`
|
let mut clicked = false;
|
||||||
// ui.memory().close_popup();
|
|
||||||
} else {
|
egui::popup_below_widget(ui, popup_id, &re, |ui| {
|
||||||
ui.memory().open_popup(popup_id);
|
for (i, candidate) in hints.iter().enumerate() {
|
||||||
|
if ui.selectable_label(i == self.i, *candidate).clicked() {
|
||||||
|
clicked = true;
|
||||||
|
self.i = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if clicked | apply_key {
|
||||||
|
new_string += hints[self.i];
|
||||||
|
|
||||||
|
// don't need this here as it simply won't be display next
|
||||||
|
// frame in `math_app.rs` ui.memory().close_popup();
|
||||||
|
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
ui.memory().open_popup(popup_id);
|
||||||
|
false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
_ => false,
|
||||||
|
};
|
||||||
|
|
||||||
// Push cursor to end if needed
|
// Push cursor to end if needed
|
||||||
if push_cursor {
|
if push_cursor {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user