editor viewer panels
This commit is contained in:
+107
-12
@@ -7,6 +7,36 @@ use eframe::egui::{self, Align2, Color32, FontId, Pos2, Sense, Stroke, Vec2};
|
||||
use std::f32::consts::PI;
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
|
||||
const SPLIT_MIN_WIDTH: f32 = 900.0; // AUTO splits when the center area is at least this wide
|
||||
|
||||
/// the one place the rendered view is drawn; swap in PDF/markdown rendering here later
|
||||
fn view_pane(ui: &mut egui::Ui, text: &str, selectable: bool) {
|
||||
render_view(ui, text, selectable);
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq)]
|
||||
enum Mode {
|
||||
Edit,
|
||||
Auto, // viewer until clicked, then editor
|
||||
Viewer,
|
||||
}
|
||||
|
||||
/// minimal read-only rendering: "# " and "## " headings, blank lines, plain text
|
||||
fn render_view(ui: &mut egui::Ui, text: &str, selectable: bool) {
|
||||
for line in text.lines() {
|
||||
if let Some(h) = line.strip_prefix("# ") {
|
||||
ui.add(egui::Label::new(egui::RichText::new(h).heading()).selectable(selectable));
|
||||
} else if let Some(h) = line.strip_prefix("## ") {
|
||||
ui.add(egui::Label::new(egui::RichText::new(h).strong().size(17.0)).selectable(selectable));
|
||||
} else if line.trim().is_empty() {
|
||||
ui.add_space(6.0);
|
||||
} else {
|
||||
ui.add(egui::Label::new(line).selectable(selectable));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct Dir {
|
||||
dirs: BTreeMap<String, Dir>,
|
||||
@@ -51,13 +81,16 @@ struct App {
|
||||
yaw: f32,
|
||||
pitch: f32,
|
||||
zoom: f32,
|
||||
heat: f32, // simulation energy, cools over time
|
||||
heat: f32,
|
||||
auto_spin: bool,
|
||||
|
||||
tree: Dir,
|
||||
open: Option<usize>, // None = show the graph
|
||||
}
|
||||
open: Option<usize>,
|
||||
|
||||
mode: Mode,
|
||||
editing: bool, // only used in Auto mode
|
||||
last_open: Option<usize>, // detects when the open note changes
|
||||
}
|
||||
|
||||
|
||||
impl App {
|
||||
@@ -138,23 +171,78 @@ impl App {
|
||||
|
||||
|
||||
fn editor_ui(&mut self, ui: &mut egui::Ui, i: usize) {
|
||||
|
||||
let split = self.mode == Mode::Auto && ui.available_width() >= SPLIT_MIN_WIDTH;
|
||||
if split {
|
||||
self.editing = false; // click-to-edit state doesn't apply while split
|
||||
ui.columns(2, |cols| {
|
||||
egui::ScrollArea::vertical()
|
||||
.id_salt("edit_pane")
|
||||
.show(&mut cols[0], |ui| {
|
||||
ui.add(
|
||||
egui::TextEdit::multiline(&mut self.vault.notes[i].raw)
|
||||
.desired_width(f32::INFINITY)
|
||||
.desired_rows(30)
|
||||
.code_editor(),
|
||||
);
|
||||
});
|
||||
egui::ScrollArea::vertical()
|
||||
.id_salt("view_pane")
|
||||
.show(&mut cols[1], |ui| {
|
||||
view_pane(ui, &self.vault.notes[i].raw, true);
|
||||
});
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
if ui.button("← Graph").clicked() {
|
||||
self.open = None;
|
||||
}
|
||||
ui.heading(&self.vault.notes[i].title);
|
||||
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
|
||||
ui.selectable_value(&mut self.mode, Mode::Viewer, "VIEWER");
|
||||
ui.selectable_value(&mut self.mode, Mode::Auto, "AUTO");
|
||||
ui.selectable_value(&mut self.mode, Mode::Edit, "EDIT");
|
||||
});
|
||||
});
|
||||
ui.separator();
|
||||
egui::ScrollArea::vertical().show(ui, |ui| {
|
||||
ui.add(
|
||||
egui::TextEdit::multiline(&mut self.vault.notes[i].raw)
|
||||
.desired_width(f32::INFINITY)
|
||||
.desired_rows(30)
|
||||
.code_editor(),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
let id = egui::Id::new(("note_edit", i));
|
||||
let show_editor = match self.mode {
|
||||
Mode::Edit => true,
|
||||
Mode::Viewer => false,
|
||||
Mode::Auto => self.editing,
|
||||
};
|
||||
|
||||
egui::ScrollArea::vertical().show(ui, |ui| {
|
||||
if show_editor {
|
||||
let out = ui.add(
|
||||
egui::TextEdit::multiline(&mut self.vault.notes[i].raw)
|
||||
.id(id)
|
||||
.desired_width(f32::INFINITY)
|
||||
.desired_rows(30)
|
||||
.code_editor(),
|
||||
);
|
||||
if self.mode == Mode::Auto
|
||||
&& (out.lost_focus() || ui.input(|inp| inp.key_pressed(egui::Key::Escape)))
|
||||
{
|
||||
self.editing = false;
|
||||
}
|
||||
} else {
|
||||
let selectable = self.mode == Mode::Viewer; // Auto: unselectable so clicks reach us
|
||||
let scope = ui.scope(|ui| {
|
||||
ui.set_min_height(ui.available_height().max(200.0));
|
||||
ui.set_min_width(ui.available_width());
|
||||
render_view(ui, &self.vault.notes[i].raw, selectable);
|
||||
});
|
||||
if self.mode == Mode::Auto && scope.response.interact(Sense::click()).clicked() {
|
||||
self.editing = true;
|
||||
ui.ctx().memory_mut(|m| m.request_focus(id)); // cursor lands in the editor
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fn new(vault: Vault) -> Self {
|
||||
|
||||
@@ -194,6 +282,9 @@ impl App {
|
||||
auto_spin: false,
|
||||
tree,
|
||||
open: None,
|
||||
mode: Mode::Auto,
|
||||
editing: false,
|
||||
last_open: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -265,6 +356,10 @@ fn add_to(a: &mut V3, b: V3) { a[0] += b[0]; a[1] += b[1]; a[2] += b[2]; }
|
||||
|
||||
impl eframe::App for App {
|
||||
fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) {
|
||||
if self.open != self.last_open {
|
||||
self.editing = false;
|
||||
self.last_open = self.open;
|
||||
}
|
||||
egui::Panel::left("files")
|
||||
.resizable(true)
|
||||
.default_size(220.0)
|
||||
|
||||
Reference in New Issue
Block a user