fixed scroll behaviour

This commit is contained in:
Penwing 2024-01-22 21:23:07 +01:00
parent 6c9aac62bd
commit 513d0f42e0
3 changed files with 21 additions and 13 deletions

View file

@ -2,7 +2,6 @@ use eframe::egui;
//use egui::{text::CCursor, text_edit::CCursorRange}; //use egui::{text::CCursor, text_edit::CCursorRange};
use std::{env, path::Path, path::PathBuf, cmp::max, io, fs, cmp::min}; use std::{env, path::Path, path::PathBuf, cmp::max, io, fs, cmp::min};
use crate::tools; use crate::tools;
//use tools::themes::CustomColorTheme;
pub mod code_editor; pub mod code_editor;
use code_editor::CodeEditor; use code_editor::CodeEditor;
@ -141,17 +140,14 @@ impl super::Calcifer {
let current_tab = &mut self.tabs[self.selected_tab.to_index()]; let current_tab = &mut self.tabs[self.selected_tab.to_index()];
let lines = current_tab.code.chars().filter(|&c| c == '\n').count() + 1; let lines = current_tab.code.chars().filter(|&c| c == '\n').count() + 1;
//let scroll_area = egui::ScrollArea::vertical() let _output = CodeEditor::default()
//.vertical_scroll_offset(current_tab.scroll_offset)
//.show(ui, |ui| {
let mut _output = CodeEditor::default()
.id_source("code editor") .id_source("code editor")
.with_rows(max(80, lines)) .with_rows(max(80, lines))
.with_fontsize(14.0) .with_fontsize(14.0)
.with_theme(self.theme) .with_theme(self.theme)
.with_syntax(tools::to_syntax(&current_tab.language)) .with_syntax(tools::to_syntax(&current_tab.language))
.with_numlines(true) .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 { //if !self.search.result_selected {
//output.state.set_ccursor_range(Some(CCursorRange::two( //output.state.set_ccursor_range(Some(CCursorRange::two(
//CCursor::new(self.search.get_cursor_start()), //CCursor::new(self.search.get_cursor_start()),
@ -161,7 +157,7 @@ impl super::Calcifer {
//self.search.result_selected = true; //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 { if current_tab.history.len() < 1 {
current_tab.history.push(current_tab.code.clone()); current_tab.history.push(current_tab.code.clone());

View file

@ -187,8 +187,8 @@ impl CodeEditor {
//#[cfg(feature = "egui")] //#[cfg(feature = "egui")]
/// Show Code Editor /// Show Code Editor
pub fn show(&mut self, ui: &mut egui::Ui, text: &mut String) -> TextEditOutput { pub fn show(&mut self, ui: &mut egui::Ui, text: &mut String, vertical_offset: &mut f32) -> f32 {
let mut text_edit_output: Option<TextEditOutput> = None; //let mut text_edit_output: Option<TextEditOutput> = None;
let mut code_editor = |ui: &mut egui::Ui| { let mut code_editor = |ui: &mut egui::Ui| {
ui.horizontal_top(|h| { ui.horizontal_top(|h| {
self.theme.modify_style(h, self.fontsize); self.theme.modify_style(h, self.fontsize);
@ -202,7 +202,7 @@ impl CodeEditor {
let layout_job = highlight(ui.ctx(), self, string); let layout_job = highlight(ui.ctx(), self, string);
ui.fonts(|f| f.layout_job(layout_job)) ui.fonts(|f| f.layout_job(layout_job))
}; };
let output = egui::TextEdit::multiline(text) let _output = egui::TextEdit::multiline(text)
.id_source(&self.id) .id_source(&self.id)
.lock_focus(true) .lock_focus(true)
.desired_rows(self.rows) .desired_rows(self.rows)
@ -210,19 +210,23 @@ impl CodeEditor {
.desired_width(if self.shrink { 0.0 } else { f32::MAX }) .desired_width(if self.shrink { 0.0 } else { f32::MAX })
.layouter(&mut layouter) .layouter(&mut layouter)
.show(ui); .show(ui);
text_edit_output = Some(output); //text_edit_output = Some(output);
}); });
}); });
}; };
if self.vscroll { if self.vscroll {
egui::ScrollArea::vertical() let scroll_area = egui::ScrollArea::vertical()
.id_source(format!("{}_outer_scroll", self.id)) .id_source(format!("{}_outer_scroll", self.id))
.stick_to_bottom(self.stick_to_bottom) .stick_to_bottom(self.stick_to_bottom)
.vertical_scroll_offset(vertical_offset.clone())
//.enable_scrolling(false)
.show(ui, code_editor); .show(ui, code_editor);
*vertical_offset = scroll_area.state.offset.y.clone();
} else { } else {
code_editor(ui); 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
} }
} }

View file

@ -94,6 +94,14 @@ impl eframe::App for Calcifer {
if ctx.input( |i| i.key_pressed(egui::Key::F) && i.modifiers.ctrl) { if ctx.input( |i| i.key_pressed(egui::Key::F) && i.modifiers.ctrl) {
self.searching = !self.searching.clone(); 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_settings(ctx);
self.draw_tree_panel(ctx); self.draw_tree_panel(ctx);