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 { div {
class: "result-entry", class: "result-entry",
div { div {
id: "result-{row.entry_index}", id: "result-{row.search_index}",
class: "result {rclass}", class: "result {rclass}",
// actions // actions
onmouseenter: move |_| { 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 { fn gui_main() -> Element {
// build context and signals for state // build context and signals for state
let ctx = use_context::<Ctx>(); let ctx = use_context::<Ctx>();
@ -110,9 +117,9 @@ fn gui_main() -> Element {
// calculate current entry // calculate current entry
let pos = position.with(|p| p.pos); let pos = position.with(|p| p.pos);
let index = results.with(|r| r[pos]); let index = results.with(|r| r[pos]);
let entry = context.get_entry(index); // let entry = context.get_entry(index);
// update keybinds // update keybinds
context.handle_keybinds(e, entry, &mut position); context.handle_keybinds(e, index, &mut position);
// scroll when required // scroll when required
let script = format!("document.getElementById(`result-{index}`).scrollIntoView(false)"); let script = format!("document.getElementById(`result-{index}`).scrollIntoView(false)");
eval(&script); eval(&script);
@ -130,6 +137,7 @@ fn gui_main() -> Element {
id: "content", id: "content",
class: "content", class: "content",
onkeydown: keydown, onkeydown: keydown,
prevent_default: "keydown",
div { div {
id: "navbar", id: "navbar",
class: "navbar", class: "navbar",
@ -139,11 +147,13 @@ fn gui_main() -> Element {
pattern: pattern, pattern: pattern,
maxlength: maxlength, maxlength: maxlength,
oninput: move |e| search.set(e.value()), oninput: move |e| search.set(e.value()),
prevent_default: "keydown",
} }
} }
div { div {
id: "results", id: "results",
class: "results", class: "results",
prevent_default: "keydown",
for (pos, index) in results().iter().take(max_result).enumerate() { for (pos, index) in results().iter().take(max_result).enumerate() {
gui_entry { gui_entry {
key: "{pos}-{index}", 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) 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) { pub fn handle_keybinds(&self, event: KeyboardEvent, index: usize, pos: &mut Pos) {
let code = event.code(); let code = event.code();
let modifiers = event.modifiers(); let modifiers = event.modifiers();
@ -107,8 +117,11 @@ impl Context {
std::process::exit(0); std::process::exit(0);
} else if self.matches(&keybinds.move_next, &modifiers, &code) { } else if self.matches(&keybinds.move_next, &modifiers, &code) {
self.move_next(index, pos); self.move_next(index, pos);
self.scroll(pos.with(|p| p.pos) + 3);
} else if self.matches(&keybinds.move_prev, &modifiers, &code) { } else if self.matches(&keybinds.move_prev, &modifiers, &code) {
self.move_prev(pos); 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.open_menu, &modifiers, &code) {
} else if self.matches(&keybinds.close_menu, &modifiers, &code) { } else if self.matches(&keybinds.close_menu, &modifiers, &code) {
} else if self.matches(&keybinds.jump_next, &modifiers, &code) { } else if self.matches(&keybinds.jump_next, &modifiers, &code) {