feat: scroll implemented by facing keydown bug

This commit is contained in:
imgurbot12 2024-07-04 15:57:02 -07:00
parent 965d53263d
commit 69e72ae6bc
2 changed files with 27 additions and 3 deletions

View file

@ -50,7 +50,7 @@ fn gui_entry(mut row: Row) -> Element {
div {
class: "result-entry",
div {
id: "result-{row.entry_index}",
id: "result-{row.search_index}",
class: "result {rclass}",
// actions
onmouseenter: move |_| {
@ -89,6 +89,13 @@ fn gui_entry(mut row: Row) -> Element {
}
}
const FUCKED: &'static str = r#"
document.getElementById('results').addEventListener("keydown", (e) => {
console.log('prevented scroll!');
e.preventDefault();
});
"#;
fn gui_main() -> Element {
// build context and signals for state
let ctx = use_context::<Ctx>();
@ -110,9 +117,9 @@ fn gui_main() -> Element {
// calculate current entry
let pos = position.with(|p| p.pos);
let index = results.with(|r| r[pos]);
let entry = context.get_entry(index);
// let entry = context.get_entry(index);
// update keybinds
context.handle_keybinds(e, entry, &mut position);
context.handle_keybinds(e, index, &mut position);
// scroll when required
let script = format!("document.getElementById(`result-{index}`).scrollIntoView(false)");
eval(&script);
@ -130,6 +137,7 @@ fn gui_main() -> Element {
id: "content",
class: "content",
onkeydown: keydown,
prevent_default: "keydown",
div {
id: "navbar",
class: "navbar",
@ -139,11 +147,13 @@ fn gui_main() -> Element {
pattern: pattern,
maxlength: maxlength,
oninput: move |e| search.set(e.value()),
prevent_default: "keydown",
}
}
div {
id: "results",
class: "results",
prevent_default: "keydown",
for (pos, index) in results().iter().take(max_result).enumerate() {
gui_entry {
key: "{pos}-{index}",
@ -154,5 +164,6 @@ fn gui_main() -> Element {
}
}
}
script { "{FUCKED}" }
}
}

View file

@ -97,6 +97,16 @@ impl Context {
bind.iter().any(|b| mods.contains(b.mods) && &b.key == key)
}
fn scroll(&self, pos: usize) {
let js = format!(
r#"
let element = document.getElementById('result-{pos}');
setTimeout(() => element.scrollIntoView(false), 1000);
"#
);
eval(&js);
}
pub fn handle_keybinds(&self, event: KeyboardEvent, index: usize, pos: &mut Pos) {
let code = event.code();
let modifiers = event.modifiers();
@ -107,8 +117,11 @@ impl Context {
std::process::exit(0);
} else if self.matches(&keybinds.move_next, &modifiers, &code) {
self.move_next(index, pos);
self.scroll(pos.with(|p| p.pos) + 3);
} else if self.matches(&keybinds.move_prev, &modifiers, &code) {
self.move_prev(pos);
let pos = pos.with(|p| p.pos);
self.scroll(if pos <= 3 { pos } else { pos + 3 })
} else if self.matches(&keybinds.open_menu, &modifiers, &code) {
} else if self.matches(&keybinds.close_menu, &modifiers, &code) {
} else if self.matches(&keybinds.jump_next, &modifiers, &code) {