From 579aace30666e6f18098d7757e66dbebb2a9c177 Mon Sep 17 00:00:00 2001 From: Joakim Hulthe Date: Tue, 22 Jul 2025 12:58:53 +0200 Subject: [PATCH] Add buttons to scroll file editor --- src/file_editor.rs | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/file_editor.rs b/src/file_editor.rs index 679e804..d2ef204 100644 --- a/src/file_editor.rs +++ b/src/file_editor.rs @@ -40,6 +40,12 @@ pub struct FileEditor { #[serde(skip)] inner: Option, + /// The distance to scroll when paging up or down. + /// + /// This is calculated when the view is rendered. + #[serde(skip)] + scroll_delta: f32, + /// Whether the file has been edited since it was last saved to disk. is_dirty: bool, } @@ -89,6 +95,7 @@ impl FileEditor { buffer, file_mtime: None, buffer_mtime: Local::now(), + scroll_delta: 0.0, is_dirty: false, inner: None, } @@ -162,9 +169,21 @@ impl FileEditor { const MAX_NOTE_WIDTH: f32 = 600.0; + // distance to scroll when paging up or down. + let mut scroll_delta = 0.0; + + ui.input_mut(|input| { + if input.consume_key(egui::Modifiers::NONE, egui::Key::PageUp) { + scroll_delta += self.scroll_delta; + } + if input.consume_key(egui::Modifiers::NONE, egui::Key::PageDown) { + scroll_delta -= self.scroll_delta; + } + }); + ui.horizontal(|ui| { ui.label("new"); - if ui.button("text").clicked() { + if ui.button(" text ").clicked() { self.is_dirty = true; self.buffer.push(BufferItem::Text(Default::default())); } @@ -173,9 +192,23 @@ impl FileEditor { self.buffer .push(BufferItem::Handwriting(Default::default())); } + + ui.add_space(16.0); + + ui.label("scroll"); + if ui.button(" up ").clicked() { + scroll_delta += self.scroll_delta; + } + if ui.button("down").clicked() { + scroll_delta -= self.scroll_delta; + } }); - ScrollArea::vertical().show(ui, |ui| { + let scroll_area = ScrollArea::vertical().show(ui, |ui| { + if scroll_delta != 0.0 { + ui.scroll_with_delta(Vec2::new(0.0, scroll_delta)); + } + ui.horizontal(|ui| { let side_padding = ui.available_width().sub(MAX_NOTE_WIDTH).max(0.0).div(2.0); ui.add_space(side_padding); @@ -186,6 +219,8 @@ impl FileEditor { ui.add_space(side_padding); }); }); + + self.scroll_delta = scroll_area.inner_rect.height() * 0.9; }); }