From 513d0f42e0a00f14419fbccb8ce9abac5dbb4f52 Mon Sep 17 00:00:00 2001 From: Penwing Date: Mon, 22 Jan 2024 21:23:07 +0100 Subject: [PATCH] fixed scroll behaviour --- src/calcifer.rs | 10 +++------- src/calcifer/code_editor/mod.rs | 16 ++++++++++------ src/main.rs | 8 ++++++++ 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/calcifer.rs b/src/calcifer.rs index b4868b3..ed1256f 100644 --- a/src/calcifer.rs +++ b/src/calcifer.rs @@ -2,7 +2,6 @@ use eframe::egui; //use egui::{text::CCursor, text_edit::CCursorRange}; use std::{env, path::Path, path::PathBuf, cmp::max, io, fs, cmp::min}; use crate::tools; -//use tools::themes::CustomColorTheme; pub mod code_editor; use code_editor::CodeEditor; @@ -141,17 +140,14 @@ impl super::Calcifer { let current_tab = &mut self.tabs[self.selected_tab.to_index()]; let lines = current_tab.code.chars().filter(|&c| c == '\n').count() + 1; - //let scroll_area = egui::ScrollArea::vertical() - //.vertical_scroll_offset(current_tab.scroll_offset) - //.show(ui, |ui| { - let mut _output = CodeEditor::default() + let _output = CodeEditor::default() .id_source("code editor") .with_rows(max(80, lines)) .with_fontsize(14.0) .with_theme(self.theme) .with_syntax(tools::to_syntax(¤t_tab.language)) .with_numlines(true) - .show(ui, &mut current_tab.code); + .show(ui, &mut current_tab.code, &mut current_tab.scroll_offset); //if !self.search.result_selected { //output.state.set_ccursor_range(Some(CCursorRange::two( //CCursor::new(self.search.get_cursor_start()), @@ -161,7 +157,7 @@ impl super::Calcifer { //self.search.result_selected = true; //} //}); - //ui.label(format!("Scroll : {}", scroll_area.state.offset.y.clone())); + ui.label(format!("Scroll : {}", current_tab.scroll_offset)); if current_tab.history.len() < 1 { current_tab.history.push(current_tab.code.clone()); diff --git a/src/calcifer/code_editor/mod.rs b/src/calcifer/code_editor/mod.rs index 3375cc1..e9723ce 100644 --- a/src/calcifer/code_editor/mod.rs +++ b/src/calcifer/code_editor/mod.rs @@ -187,8 +187,8 @@ impl CodeEditor { //#[cfg(feature = "egui")] /// Show Code Editor - pub fn show(&mut self, ui: &mut egui::Ui, text: &mut String) -> TextEditOutput { - let mut text_edit_output: Option = None; + pub fn show(&mut self, ui: &mut egui::Ui, text: &mut String, vertical_offset: &mut f32) -> f32 { + //let mut text_edit_output: Option = None; let mut code_editor = |ui: &mut egui::Ui| { ui.horizontal_top(|h| { self.theme.modify_style(h, self.fontsize); @@ -202,7 +202,7 @@ impl CodeEditor { let layout_job = highlight(ui.ctx(), self, string); ui.fonts(|f| f.layout_job(layout_job)) }; - let output = egui::TextEdit::multiline(text) + let _output = egui::TextEdit::multiline(text) .id_source(&self.id) .lock_focus(true) .desired_rows(self.rows) @@ -210,19 +210,23 @@ impl CodeEditor { .desired_width(if self.shrink { 0.0 } else { f32::MAX }) .layouter(&mut layouter) .show(ui); - text_edit_output = Some(output); + //text_edit_output = Some(output); }); }); }; if self.vscroll { - egui::ScrollArea::vertical() + let scroll_area = egui::ScrollArea::vertical() .id_source(format!("{}_outer_scroll", self.id)) .stick_to_bottom(self.stick_to_bottom) + .vertical_scroll_offset(vertical_offset.clone()) + //.enable_scrolling(false) .show(ui, code_editor); + *vertical_offset = scroll_area.state.offset.y.clone(); } else { code_editor(ui); } - text_edit_output.expect("TextEditOutput should exist at this point") + //text_edit_output.expect("TextEditOutput should exist at this point") + *vertical_offset } } diff --git a/src/main.rs b/src/main.rs index d3b1a34..d2bfcaa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -94,6 +94,14 @@ impl eframe::App for Calcifer { if ctx.input( |i| i.key_pressed(egui::Key::F) && i.modifiers.ctrl) { self.searching = !self.searching.clone(); } + + //if ctx.input( |i| i.scroll_delta.y > 0.0) { + //self.tabs[self.selected_tab.to_index()].scroll_offset += 80.0; + //} + + //if ctx.input( |i| i.scroll_delta.y < 0.0) { + //self.tabs[self.selected_tab.to_index()].scroll_offset -= 80.0; + //} self.draw_settings(ctx); self.draw_tree_panel(ctx);