Add basic command palette
This commit is contained in:
60
src/app.rs
60
src/app.rs
@@ -1,6 +1,5 @@
|
||||
use std::{
|
||||
fs,
|
||||
io::Read,
|
||||
path::PathBuf,
|
||||
sync::{Arc, mpsc},
|
||||
thread::JoinHandle,
|
||||
@@ -8,11 +7,12 @@ use std::{
|
||||
};
|
||||
|
||||
use crate::{
|
||||
command_palette::{self, CommandPalette},
|
||||
file_editor::{FileEditor, SaveStatus},
|
||||
folder::Folder,
|
||||
preferences::Preferences,
|
||||
text_styles::{H1, H1_MONO, H2, H2_MONO, H3, H3_MONO, H4, H4_MONO, H5, H5_MONO, H6, H6_MONO},
|
||||
util::{GuiSender, file_mtime, log_error},
|
||||
util::{GuiSender, log_error},
|
||||
};
|
||||
use egui::{
|
||||
Align, Button, Context, FontData, FontDefinitions, FontFamily, FontId, Frame, Image, Key,
|
||||
@@ -40,6 +40,9 @@ pub struct App {
|
||||
open_tab_index: Option<usize>,
|
||||
|
||||
next_tab_id: TabId,
|
||||
|
||||
#[serde(skip)]
|
||||
command_palette: Option<CommandPalette>,
|
||||
}
|
||||
|
||||
pub struct Jobs {
|
||||
@@ -129,6 +132,7 @@ impl Default for App {
|
||||
next_tab_id: 2,
|
||||
show_folders: false,
|
||||
folders: vec![],
|
||||
command_palette: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -292,6 +296,10 @@ impl eframe::App for App {
|
||||
if input.consume_key(Modifiers::CTRL, Key::S) {
|
||||
self.save_active_tab(ctx);
|
||||
}
|
||||
|
||||
if input.consume_key(Modifiers::CTRL, Key::K) {
|
||||
self.command_palette = Some(Default::default());
|
||||
}
|
||||
});
|
||||
|
||||
egui::TopBottomPanel::top("top_panel").show(ctx, |ui| {
|
||||
@@ -310,19 +318,10 @@ impl eframe::App for App {
|
||||
self.jobs.start(ui.ctx(), move || {
|
||||
let file_path = rfd::FileDialog::new().pick_file()?;
|
||||
|
||||
let mut file = fs::File::open(&file_path)
|
||||
.inspect_err(|e| log::error!("Failed to open {file_path:?}: {e}"))
|
||||
.ok()?;
|
||||
|
||||
let mtime = log_error(eyre!("file_path:?"), || file_mtime(&file))?;
|
||||
|
||||
let mut text = String::new();
|
||||
file.read_to_string(&mut text)
|
||||
.inspect_err(|e| log::error!("Failed to read {file_path:?}: {e}"))
|
||||
.ok()?;
|
||||
|
||||
let editor = FileEditor::from_file(file_path, &text, mtime);
|
||||
Some(Action::OpenFile(editor))
|
||||
log_error(eyre!("Failed to open file {file_path:?}"), || {
|
||||
FileEditor::open_file(file_path)
|
||||
})
|
||||
.map(Action::OpenFile)
|
||||
});
|
||||
}
|
||||
|
||||
@@ -445,23 +444,10 @@ impl eframe::App for App {
|
||||
if let Some(file_path) = response.open_file {
|
||||
let file_path = file_path.to_owned();
|
||||
self.jobs.start(ui.ctx(), move || {
|
||||
let mut file = fs::File::open(&file_path)
|
||||
.inspect_err(|e| {
|
||||
log::error!("Failed to open {file_path:?}: {e}")
|
||||
})
|
||||
.ok()?;
|
||||
|
||||
let mtime = log_error(eyre!("file_path:?"), || file_mtime(&file))?;
|
||||
|
||||
let mut text = String::new();
|
||||
file.read_to_string(&mut text)
|
||||
.inspect_err(|e| {
|
||||
log::error!("Failed to read {file_path:?}: {e}")
|
||||
})
|
||||
.ok()?;
|
||||
|
||||
let editor = FileEditor::from_file(file_path, &text, mtime);
|
||||
Some(Action::OpenFile(editor))
|
||||
log_error(eyre!("Failed to open file {file_path:?}"), || {
|
||||
FileEditor::open_file(file_path)
|
||||
})
|
||||
.map(Action::OpenFile)
|
||||
});
|
||||
}
|
||||
|
||||
@@ -489,6 +475,16 @@ impl eframe::App for App {
|
||||
egui::warn_if_debug_build(ui);
|
||||
});
|
||||
});
|
||||
|
||||
if let Some(command_palette) = &mut self.command_palette {
|
||||
match command_palette.show(ctx, &mut self.jobs, &mut self.folders) {
|
||||
command_palette::Response::None => {}
|
||||
command_palette::Response::Close => {
|
||||
self.command_palette = None;
|
||||
ctx.request_repaint();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user