Runs Kate's syntax definitions directly, on a hand-written editor core. Spring-driven UI with a pop-out scrolling preview, baked-in terminal grown from a morphing button, jelly tabs, command palette, side panels, git, language servers and hot exit. Co-Authored-By: Claude Opus 5.5 <[email protected]>
257 lines
13 KiB
Rust
257 lines
13 KiB
Rust
//! Pop-out menus and dialogs. They live inside the window, so they can scale out from where they
|
|
//! were opened instead of appearing as separate windows.
|
|
|
|
use super::commands::{self, Cmd};
|
|
use super::{fonts, icons, widgets::*};
|
|
use crate::app::App;
|
|
use crate::motion::{self, spring_from, Preset};
|
|
use crate::theme::alpha;
|
|
use eframe::egui::{self, emath::TSTransform, pos2, vec2, Align2, Area, Id, Key, Order, Pos2, Rect, Sense, Ui};
|
|
use std::path::PathBuf;
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub enum Act {
|
|
Cmd(Cmd),
|
|
CloseOthers(usize, usize),
|
|
CloseTabAt(usize, usize),
|
|
NewFile(PathBuf),
|
|
NewFolder(PathBuf),
|
|
Rename(PathBuf),
|
|
Trash(PathBuf),
|
|
CopyPath(PathBuf),
|
|
TerminalAt(PathBuf),
|
|
SetRoot(PathBuf),
|
|
OpenRecent(PathBuf),
|
|
}
|
|
|
|
pub struct MenuItem {
|
|
pub ic: &'static str,
|
|
pub label: String,
|
|
pub keys: String,
|
|
pub act: Option<Act>,
|
|
}
|
|
|
|
pub struct Menu {
|
|
pub pos: Pos2,
|
|
pub items: Vec<MenuItem>,
|
|
pub seq: u64,
|
|
}
|
|
|
|
pub enum Dialog {
|
|
Prompt { title: String, value: String, act: Act },
|
|
Confirm { title: String, body: String, pane: usize, tab: usize },
|
|
ConfirmTrash(PathBuf),
|
|
ConfirmQuit,
|
|
}
|
|
|
|
fn item(cmd: Cmd) -> MenuItem {
|
|
let e = commands::all().into_iter().find(|e| e.cmd == cmd);
|
|
match e {
|
|
Some(e) => MenuItem { ic: e.ic, label: e.label.to_string(), keys: e.keys.to_string(), act: Some(Act::Cmd(cmd)) },
|
|
None => MenuItem { ic: "more_horiz", label: format!("{cmd:?}"), keys: String::new(), act: Some(Act::Cmd(cmd)) },
|
|
}
|
|
}
|
|
|
|
fn sep() -> MenuItem {
|
|
MenuItem { ic: "", label: String::new(), keys: String::new(), act: None }
|
|
}
|
|
|
|
fn act(ic: &'static str, label: &str, a: Act) -> MenuItem {
|
|
MenuItem { ic, label: label.to_string(), keys: String::new(), act: Some(a) }
|
|
}
|
|
|
|
impl App {
|
|
pub fn open_menu(&mut self, pos: Pos2, items: Vec<MenuItem>) {
|
|
self.menu_seq += 1;
|
|
self.menu = Some(Menu { pos, items, seq: self.menu_seq });
|
|
}
|
|
|
|
pub fn editor_menu(&mut self, pos: Pos2) {
|
|
use Cmd::*;
|
|
self.open_menu(pos, vec![item(Cut), item(Copy), item(Paste), sep(), item(GotoDefinition), item(Hover), sep(), item(Comment), item(DuplicateLines), item(SelectNext), sep(), item(SelectAll), item(ToggleBookmark)]);
|
|
}
|
|
|
|
pub fn tab_menu(&mut self, pane: usize, tab: usize, pos: Pos2) {
|
|
let mut v = vec![act("close", "Close", Act::CloseTabAt(pane, tab)), act("close_fullscreen", "Close others", Act::CloseOthers(pane, tab)), sep()];
|
|
let path = self.panes[pane].tabs.get(tab).and_then(|t| self.docs.get(&t.doc)).and_then(|d| d.path.clone());
|
|
if let Some(p) = path {
|
|
v.push(act("content_copy", "Copy path", Act::CopyPath(p.clone())));
|
|
if let Some(dir) = p.parent() { v.push(act("terminal", "Terminal here", Act::TerminalAt(dir.to_path_buf()))) }
|
|
v.push(sep());
|
|
}
|
|
v.push(item(Cmd::SplitRight));
|
|
v.push(item(Cmd::SplitDown));
|
|
self.open_menu(pos, v);
|
|
}
|
|
|
|
pub fn app_menu(&mut self, pos: Pos2) {
|
|
use Cmd::*;
|
|
let mut v = vec![item(New), item(Open), item(OpenFolder), item(Save), item(SaveAs), sep(), item(Palette), item(QuickOpen), item(Find), item(FindInFiles), sep(), item(ToggleTerminal), item(SplitRight), item(Zen)];
|
|
let recent: Vec<String> = self.s.recent.iter().take(4).cloned().collect();
|
|
if !recent.is_empty() {
|
|
v.push(sep());
|
|
for r in recent {
|
|
let name = std::path::Path::new(&r).file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or(r.clone());
|
|
v.push(act("history", &name, Act::OpenRecent(PathBuf::from(r))));
|
|
}
|
|
}
|
|
v.push(sep());
|
|
v.push(item(Settings));
|
|
v.push(item(About));
|
|
v.push(item(Quit));
|
|
self.open_menu(pos, v);
|
|
}
|
|
|
|
pub fn file_menu(&mut self, path: PathBuf, is_dir: bool, pos: Pos2) {
|
|
let dir = if is_dir { path.clone() } else { path.parent().map(PathBuf::from).unwrap_or_default() };
|
|
let mut v = vec![act("add", "New file", Act::NewFile(dir.clone())), act("folder", "New folder", Act::NewFolder(dir.clone())), sep(), act("edit", "Rename", Act::Rename(path.clone())), act("delete", "Move to trash", Act::Trash(path.clone())), sep(), act("content_copy", "Copy path", Act::CopyPath(path.clone())), act("terminal", "Terminal here", Act::TerminalAt(dir))];
|
|
if is_dir { v.push(act("home", "Make this the project folder", Act::SetRoot(path))) }
|
|
self.open_menu(pos, v);
|
|
}
|
|
|
|
pub fn menu_ui(&mut self, ctx: &egui::Context) {
|
|
let Some(menu) = &self.menu else { return };
|
|
let t = self.t.clone();
|
|
let key = format!("menu/{}", menu.seq);
|
|
let k = spring_from(&key, 0.0, 1.0, Preset { stiffness: 800.0, damping: 0.66 });
|
|
let row = 36.0;
|
|
let height: f32 = menu.items.iter().map(|i| if i.act.is_none() { 11.0 } else { row }).sum::<f32>() + 14.0;
|
|
let width = 268.0;
|
|
let screen = ctx.content_rect();
|
|
let x = menu.pos.x.min(screen.right() - width - 10.0);
|
|
let y = menu.pos.y.min(screen.bottom() - height - 10.0).max(8.0);
|
|
let rect = Rect::from_min_size(pos2(x, y), vec2(width, height));
|
|
let anchor = menu.pos;
|
|
|
|
let mut chosen: Option<Act> = None;
|
|
let mut dismiss = false;
|
|
let scrim = Area::new(Id::unique("menu-scrim")).order(Order::Foreground).fixed_pos(screen.min).show(ctx, |ui| {
|
|
ui.allocate_rect(screen, Sense::click())
|
|
}).inner;
|
|
if scrim.clicked() || scrim.secondary_clicked() || ctx.input(|i| i.key_pressed(Key::Escape)) {
|
|
dismiss = true;
|
|
}
|
|
let area = Area::new(Id::unique("menu-body")).order(Order::Tooltip).fixed_pos(rect.min).show(ctx, |ui| {
|
|
ui.set_min_size(rect.size());
|
|
let p = ui.painter();
|
|
shadow(p, rect, 22.0, 1.0, t.dark);
|
|
p.rect_filled(rect, cr(22.0), t.high);
|
|
let mut yy = rect.top() + 7.0;
|
|
for (i, it) in menu.items.iter().enumerate() {
|
|
let Some(a) = &it.act else {
|
|
let ly = yy + 5.5;
|
|
p.line_segment([pos2(rect.left() + 16.0, ly), pos2(rect.right() - 16.0, ly)], egui::Stroke::new(1.0, t.outline_variant));
|
|
yy += 11.0;
|
|
continue;
|
|
};
|
|
let r = Rect::from_min_size(pos2(rect.left() + 7.0, yy), vec2(width - 14.0, row));
|
|
let ikey = format!("{key}/{i}");
|
|
let resp = ui.interact(r, Id::unique(&ikey), Sense::click());
|
|
let (hov, press) = states(&ikey, &resp);
|
|
let k2 = motion::stagger(motion::spring_from(format!("{ikey}/t"), 0.0, 1.0, motion::SLOW), i as f32 * 0.035, 0.6);
|
|
let dx = (1.0 - k2) * -10.0;
|
|
let col = alpha(t.on_surface, k2.clamp(0.0, 1.0));
|
|
if hov > 0.01 { p.rect_filled(scale_rect(r, 1.0 - 0.02 * press), cr(14.0), alpha(t.primary, 0.13 * hov)); }
|
|
if !it.ic.is_empty() { icons::paint(ui.ctx(), p, pos2(r.left() + 20.0 + dx, r.center().y), 18.0, it.ic, alpha(t.on_variant, k2.clamp(0.0, 1.0))) }
|
|
p.text(pos2(r.left() + 42.0 + dx, r.center().y), Align2::LEFT_CENTER, &it.label, fonts::ui(13.5), col);
|
|
if !it.keys.is_empty() { p.text(pos2(r.right() - 12.0, r.center().y), Align2::RIGHT_CENTER, &it.keys, fonts::ui(11.5), alpha(t.outline, k2.clamp(0.0, 1.0))); }
|
|
if resp.clicked() { chosen = Some(a.clone()) }
|
|
yy += row;
|
|
}
|
|
});
|
|
let s = k.max(0.02);
|
|
let scale = 0.72 + 0.28 * s;
|
|
let tf = TSTransform::from_translation(anchor.to_vec2()) * TSTransform::from_scaling(scale) * TSTransform::from_translation(-anchor.to_vec2());
|
|
ctx.set_transform_layer(area.response.layer_id, tf);
|
|
if let Some(a) = chosen {
|
|
self.menu = None;
|
|
self.act(ctx, a);
|
|
} else if dismiss {
|
|
self.menu = None;
|
|
}
|
|
}
|
|
|
|
pub fn dialog_ui(&mut self, ctx: &egui::Context) {
|
|
let Some(mut dlg) = self.dialog.take() else { return };
|
|
let t = self.t.clone();
|
|
let screen = ctx.content_rect();
|
|
let k = spring_from("dialog", 0.0, 1.0, motion::DEFAULT);
|
|
Area::new(Id::unique("dlg-scrim")).order(Order::Foreground).fixed_pos(screen.min).show(ctx, |ui| {
|
|
ui.allocate_rect(screen, Sense::click());
|
|
ui.painter().rect_filled(screen, cr(0.0), alpha(egui::Color32::BLACK, 0.4 * k.clamp(0.0, 1.0)));
|
|
});
|
|
let rect = Rect::from_center_size(screen.center(), vec2(420.0, 190.0));
|
|
let mut close = ctx.input(|i| i.key_pressed(Key::Escape));
|
|
let mut after: Option<Box<dyn FnOnce(&mut App, &egui::Context)>> = None;
|
|
let area = Area::new(Id::unique("dlg")).order(Order::Tooltip).fixed_pos(rect.min).show(ctx, |ui: &mut Ui| {
|
|
ui.set_min_size(rect.size());
|
|
shadow(ui.painter(), rect, 28.0, 1.0, t.dark);
|
|
ui.painter().rect_filled(rect, cr(28.0), t.high);
|
|
let title = match &dlg {
|
|
Dialog::Prompt { title, .. } | Dialog::Confirm { title, .. } => title.clone(),
|
|
Dialog::ConfirmTrash(p) => format!("Move {} to the trash?", p.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default()),
|
|
Dialog::ConfirmQuit => "Quit with unsaved changes?".to_string(),
|
|
};
|
|
ui.painter().text(rect.left_top() + vec2(26.0, 34.0), Align2::LEFT_CENTER, title, fonts::med(18.0), t.on_surface);
|
|
let btn_y = rect.bottom() - 48.0;
|
|
let primary = Rect::from_min_size(pos2(rect.right() - 126.0, btn_y), vec2(104.0, 36.0));
|
|
let cancel = Rect::from_min_size(pos2(rect.right() - 226.0, btn_y), vec2(92.0, 36.0));
|
|
if pill(ui, &t, cancel, "dlg/cancel", "", "Cancel", Tone::Text).clicked() { close = true }
|
|
match &mut dlg {
|
|
Dialog::Prompt { value, act, .. } => {
|
|
let field = Rect::from_min_size(rect.left_top() + vec2(24.0, 66.0), vec2(rect.width() - 48.0, 42.0));
|
|
ui.painter().rect_filled(field, cr(16.0), t.highest);
|
|
let te = ui.put(field.shrink2(vec2(14.0, 10.0)), egui::TextEdit::singleline(value).frame(egui::Frame::NONE).font(fonts::ui(14.0)).text_color(t.on_surface));
|
|
te.request_focus();
|
|
let enter = ctx.input(|i| i.key_pressed(Key::Enter));
|
|
if pill(ui, &t, primary, "dlg/ok", "", "OK", Tone::Filled).clicked() || enter {
|
|
let (v, a) = (value.clone(), act.clone());
|
|
after = Some(Box::new(move |app, ctx| app.prompt_done(ctx, a, v)));
|
|
close = true;
|
|
}
|
|
}
|
|
Dialog::Confirm { body, pane, tab, .. } => {
|
|
ui.painter().text(rect.left_top() + vec2(26.0, 72.0), Align2::LEFT_CENTER, body.as_str(), fonts::ui(14.0), t.on_variant);
|
|
let (pane, tab) = (*pane, *tab);
|
|
let discard = Rect::from_min_size(pos2(rect.left() + 18.0, btn_y), vec2(100.0, 36.0));
|
|
if pill(ui, &t, discard, "dlg/discard", "", "Don't save", Tone::Text).clicked() {
|
|
after = Some(Box::new(move |app, _| app.force_close(pane, tab)));
|
|
close = true;
|
|
}
|
|
if pill(ui, &t, primary, "dlg/ok", "", "Save", Tone::Filled).clicked() {
|
|
after = Some(Box::new(move |app, ctx| {
|
|
app.focus_pane = pane;
|
|
app.panes[pane].active = tab;
|
|
app.run(ctx, Cmd::Save);
|
|
if !app.cur_doc().is_some_and(|d| d.buf.is_modified()) { app.force_close(pane, tab) }
|
|
}));
|
|
close = true;
|
|
}
|
|
}
|
|
Dialog::ConfirmTrash(p) => {
|
|
let p = p.clone();
|
|
if pill(ui, &t, primary, "dlg/ok", "", "Move", Tone::Filled).clicked() {
|
|
after = Some(Box::new(move |app, _| app.trash(&p)));
|
|
close = true;
|
|
}
|
|
}
|
|
Dialog::ConfirmQuit => {
|
|
ui.painter().text(rect.left_top() + vec2(26.0, 72.0), Align2::LEFT_CENTER, "They stay saved as recovery copies for next time.", fonts::ui(14.0), t.on_variant);
|
|
if pill(ui, &t, primary, "dlg/ok", "", "Quit", Tone::Filled).clicked() {
|
|
after = Some(Box::new(|app, ctx| { app.quit_now = true; ctx.send_viewport_cmd(egui::ViewportCommand::Close) }));
|
|
close = true;
|
|
}
|
|
}
|
|
}
|
|
});
|
|
let a = screen.center();
|
|
let s = 0.8 + 0.2 * k;
|
|
ctx.set_transform_layer(area.response.layer_id, TSTransform::from_translation(a.to_vec2()) * TSTransform::from_scaling(s) * TSTransform::from_translation(-a.to_vec2()));
|
|
if close {
|
|
motion::forget("dialog");
|
|
} else {
|
|
self.dialog = Some(dlg);
|
|
}
|
|
if let Some(f) = after { f(self, ctx) }
|
|
}
|
|
}
|