Skip to content

Commit

Permalink
Refactor: TabViewer
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Sep 1, 2022
1 parent 2fb8248 commit 122542f
Show file tree
Hide file tree
Showing 5 changed files with 292 additions and 108 deletions.
86 changes: 86 additions & 0 deletions examples/text_editor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release

use std::collections::BTreeMap;

use eframe::{egui, NativeOptions};

/// We identify tabs by the title of the file we are editing.
type Title = String;

fn main() {
let options = NativeOptions::default();
eframe::run_native(
"Text editor examples",
options,
Box::new(|_cc| Box::new(MyApp::default())),
);
}

struct Buffers {
buffers: BTreeMap<Title, String>,
}

impl egui_dock::TabViewer for Buffers {
type Tab = 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);
}

fn title(&mut self, title: &mut Title) -> egui::WidgetText {
egui::WidgetText::from(&*title)
}
}

struct MyApp {
buffers: Buffers,
tree: egui_dock::Tree<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 = egui_dock::Tree::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((node_index, tab_index)) = tab_location {
self.tree.set_active_tab(node_index, tab_index);
} else {
// Open the file for editing:
self.tree.push_to_focused_leaf(title.clone());
}
}
}
});

egui_dock::DockArea::new(&mut self.tree)
.style(egui_dock::Style::from_egui(ctx.style().as_ref()))
.show(ctx, &mut self.buffers);
}
}
4 changes: 2 additions & 2 deletions examples/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use egui::{
Window,
};

use egui_dock::{DockArea, NodeIndex, Tab, TabBuilder, Tree};
use egui_dock::{DockArea, NodeIndex, TabBuilder, TabTrait, Tree};

fn main() {
let options = NativeOptions::default();
Expand Down Expand Up @@ -100,7 +100,7 @@ impl Editor {
}
}

impl Tab for Editor {
impl TabTrait for Editor {
fn ui(&mut self, ui: &mut Ui) {
if self.show_save {
Window::new("Save")
Expand Down
Loading

0 comments on commit 122542f

Please sign in to comment.