From 69e72ae6bcb3a9db58fe65939bcd001a8632b071 Mon Sep 17 00:00:00 2001 From: imgurbot12 Date: Thu, 4 Jul 2024 15:57:02 -0700 Subject: [PATCH] feat: scroll implemented by facing keydown bug --- rmenu/src/gui/mod.rs | 17 ++++++++++++++--- rmenu/src/gui/state.rs | 13 +++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/rmenu/src/gui/mod.rs b/rmenu/src/gui/mod.rs index b466973..5dd538f 100644 --- a/rmenu/src/gui/mod.rs +++ b/rmenu/src/gui/mod.rs @@ -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::(); @@ -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}" } } } diff --git a/rmenu/src/gui/state.rs b/rmenu/src/gui/state.rs index c7db812..84b2afc 100644 --- a/rmenu/src/gui/state.rs +++ b/rmenu/src/gui/state.rs @@ -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) {