-
Notifications
You must be signed in to change notification settings - Fork 77
/
text_editor.rs
87 lines (73 loc) · 2.53 KB
/
text_editor.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
use std::collections::BTreeMap;
use eframe::{egui, NativeOptions};
use egui_dock::{DockArea, DockState, Style, TabViewer};
/// We identify tabs by the title of the file we are editing.
type Title = String;
fn main() -> eframe::Result<()> {
let options = NativeOptions::default();
eframe::run_native(
"Text editor examples",
options,
Box::new(|_cc| Ok(Box::<MyApp>::default())),
)
}
struct Buffers {
buffers: BTreeMap<Title, String>,
}
impl TabViewer for Buffers {
type Tab = Title;
fn title(&mut self, title: &mut Title) -> egui::WidgetText {
egui::WidgetText::from(&*title)
}
fn ui(&mut self, ui: &mut egui::Ui, title: &mut Title) {
let text = self.buffers.entry(title.clone()).or_default();
egui::TextEdit::multiline(text)
.desired_width(f32::INFINITY)
.show(ui);
}
}
struct MyApp {
buffers: Buffers,
tree: DockState<String>,
}
impl Default for MyApp {
fn default() -> Self {
let mut buffers = BTreeMap::default();
buffers.insert(
"CHANGELOG.md".to_owned(),
include_str!("../CHANGELOG.md").to_owned(),
);
buffers.insert("LICENSE".to_owned(), include_str!("../LICENSE").to_owned());
buffers.insert(
"README.md".to_owned(),
include_str!("../README.md").to_owned(),
);
let tree = DockState::new(vec!["README.md".to_owned(), "CHANGELOG.md".to_owned()]);
Self {
buffers: Buffers { buffers },
tree,
}
}
}
impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::SidePanel::left("documents").show(ctx, |ui| {
for title in self.buffers.buffers.keys() {
let tab_location = self.tree.find_tab(title);
let is_open = tab_location.is_some();
if ui.selectable_label(is_open, title).clicked() {
if let Some(tab_location) = tab_location {
self.tree.set_active_tab(tab_location);
} else {
// Open the file for editing:
self.tree.push_to_focused_leaf(title.clone());
}
}
}
});
DockArea::new(&mut self.tree)
.style(Style::from_egui(ctx.style().as_ref()))
.show(ctx, &mut self.buffers);
}
}