Add Context menu.
This commit is contained in:
+29
-18
@@ -4,7 +4,7 @@
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use eframe::egui::{
|
||||
self, Button, Context, Key, KeyboardShortcut, Modifiers, RichText, Ui, ViewportCommand,
|
||||
self, Button, Context, Key, KeyboardShortcut, Modifiers, Pos2, RichText, Ui, ViewportCommand,
|
||||
};
|
||||
|
||||
use crate::document::Document;
|
||||
@@ -12,7 +12,7 @@ use crate::geometry::{bounds_of, node_rect};
|
||||
use crate::inspector;
|
||||
use crate::model::{Canvas, Node, NodeKind};
|
||||
use crate::theme::Palette;
|
||||
use crate::view::{CanvasView, Clipboard};
|
||||
use crate::view::{CanvasView, Clipboard, InsertRequest};
|
||||
|
||||
/// How long a status message stays on screen.
|
||||
const STATUS_SECONDS: f64 = 5.0;
|
||||
@@ -225,8 +225,17 @@ impl App {
|
||||
|
||||
// -- editing actions --------------------------------------------------
|
||||
|
||||
fn insert_text_node(&mut self, ctx: &Context) {
|
||||
let at = self.view.centre_in_canvas();
|
||||
/// Adds the node the canvas context menu asked for, where it asked for it.
|
||||
fn insert_requested(&mut self, kind: InsertRequest, at: Pos2, ctx: &Context) {
|
||||
match kind {
|
||||
InsertRequest::Text => self.insert_text_node(at, ctx),
|
||||
InsertRequest::File => self.insert_file_node(at, ctx),
|
||||
InsertRequest::Link => self.insert_link_node(at, ctx),
|
||||
InsertRequest::Group => self.insert_group(at, ctx),
|
||||
}
|
||||
}
|
||||
|
||||
fn insert_text_node(&mut self, at: Pos2, ctx: &Context) {
|
||||
let id = self
|
||||
.view
|
||||
.insert_node(&mut self.doc, NodeKind::Text { text: String::new() }, at);
|
||||
@@ -234,7 +243,7 @@ impl App {
|
||||
self.set_status("Added a text node", ctx);
|
||||
}
|
||||
|
||||
fn insert_file_node(&mut self, ctx: &Context) {
|
||||
fn insert_file_node(&mut self, at: Pos2, ctx: &Context) {
|
||||
let mut dialog = rfd::FileDialog::new();
|
||||
if let Some(dir) = self.doc.base_dir() {
|
||||
dialog = dialog.set_directory(dir);
|
||||
@@ -243,7 +252,6 @@ impl App {
|
||||
return;
|
||||
};
|
||||
let file = inspector::relative_to(self.doc.base_dir().as_deref(), &path);
|
||||
let at = self.view.centre_in_canvas();
|
||||
self.view.insert_node(
|
||||
&mut self.doc,
|
||||
NodeKind::File {
|
||||
@@ -261,8 +269,7 @@ impl App {
|
||||
}
|
||||
}
|
||||
|
||||
fn insert_link_node(&mut self, ctx: &Context) {
|
||||
let at = self.view.centre_in_canvas();
|
||||
fn insert_link_node(&mut self, at: Pos2, ctx: &Context) {
|
||||
self.view.insert_node(
|
||||
&mut self.doc,
|
||||
NodeKind::Link {
|
||||
@@ -278,8 +285,7 @@ impl App {
|
||||
);
|
||||
}
|
||||
|
||||
fn insert_group(&mut self, ctx: &Context) {
|
||||
let at = self.view.centre_in_canvas();
|
||||
fn insert_group(&mut self, at: Pos2, ctx: &Context) {
|
||||
self.view.insert_node(
|
||||
&mut self.doc,
|
||||
NodeKind::Group {
|
||||
@@ -596,20 +602,21 @@ impl App {
|
||||
});
|
||||
|
||||
ui.menu_button("Insert", |ui| {
|
||||
let at = self.view.centre_in_canvas();
|
||||
if ui.button("Text node").clicked() {
|
||||
self.insert_text_node(ctx);
|
||||
self.insert_text_node(at, ctx);
|
||||
ui.close();
|
||||
}
|
||||
if ui.button("File node…").clicked() {
|
||||
self.insert_file_node(ctx);
|
||||
self.insert_file_node(at, ctx);
|
||||
ui.close();
|
||||
}
|
||||
if ui.button("Link node").clicked() {
|
||||
self.insert_link_node(ctx);
|
||||
self.insert_link_node(at, ctx);
|
||||
ui.close();
|
||||
}
|
||||
if ui.button("Group").clicked() {
|
||||
self.insert_group(ctx);
|
||||
self.insert_group(at, ctx);
|
||||
ui.close();
|
||||
}
|
||||
ui.separator();
|
||||
@@ -684,17 +691,18 @@ impl App {
|
||||
self.redo(ctx);
|
||||
}
|
||||
ui.separator();
|
||||
let at = self.view.centre_in_canvas();
|
||||
if ui.button("Note").on_hover_text("Add a text node").clicked() {
|
||||
self.insert_text_node(ctx);
|
||||
self.insert_text_node(at, ctx);
|
||||
}
|
||||
if ui.button("File").on_hover_text("Add a file node").clicked() {
|
||||
self.insert_file_node(ctx);
|
||||
self.insert_file_node(at, ctx);
|
||||
}
|
||||
if ui.button("Link").on_hover_text("Add a link node").clicked() {
|
||||
self.insert_link_node(ctx);
|
||||
self.insert_link_node(at, ctx);
|
||||
}
|
||||
if ui.button("Group").on_hover_text("Add a group").clicked() {
|
||||
self.insert_group(ctx);
|
||||
self.insert_group(at, ctx);
|
||||
}
|
||||
ui.separator();
|
||||
if ui.button("−").on_hover_text("Zoom out (Ctrl+-)").clicked() {
|
||||
@@ -918,6 +926,9 @@ impl eframe::App for App {
|
||||
if let Some(target) = out.open_target {
|
||||
self.open_target(&target, &ctx);
|
||||
}
|
||||
if let Some((kind, at)) = out.insert {
|
||||
self.insert_requested(kind, at, &ctx);
|
||||
}
|
||||
});
|
||||
|
||||
self.dialogs(&ctx);
|
||||
|
||||
Reference in New Issue
Block a user