2023-10-26 10:15:09 -06:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
|
|
2023-11-13 10:33:26 -07:00
|
|
|
use cosmic::widget::{icon, Icon};
|
2023-11-03 19:00:41 -06:00
|
|
|
use cosmic_text::{Attrs, Buffer, Edit, Shaping, SyntaxEditor, ViEditor, Wrap};
|
2023-10-26 10:15:09 -06:00
|
|
|
use std::{fs, path::PathBuf, sync::Mutex};
|
|
|
|
|
|
2023-11-13 10:33:26 -07:00
|
|
|
use crate::{fl, mime_icon, Config, FALLBACK_MIME_ICON, FONT_SYSTEM, SYNTAX_SYSTEM};
|
2023-10-26 10:15:09 -06:00
|
|
|
|
|
|
|
|
pub struct Tab {
|
|
|
|
|
pub path_opt: Option<PathBuf>,
|
|
|
|
|
attrs: Attrs<'static>,
|
|
|
|
|
pub editor: Mutex<ViEditor<'static>>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Tab {
|
2023-11-03 11:02:25 -06:00
|
|
|
pub fn new(config: &Config) -> Self {
|
|
|
|
|
//TODO: do not repeat, used in App::init
|
2023-10-26 10:15:09 -06:00
|
|
|
let attrs = cosmic_text::Attrs::new().family(cosmic_text::Family::Monospace);
|
|
|
|
|
|
2023-11-03 19:00:41 -06:00
|
|
|
let mut buffer = Buffer::new_empty(config.metrics());
|
|
|
|
|
buffer.set_text(
|
|
|
|
|
&mut FONT_SYSTEM.lock().unwrap(),
|
|
|
|
|
"",
|
|
|
|
|
attrs,
|
|
|
|
|
Shaping::Advanced,
|
|
|
|
|
);
|
|
|
|
|
|
2023-11-13 09:08:31 -07:00
|
|
|
let editor = SyntaxEditor::new(buffer, &SYNTAX_SYSTEM, config.syntax_theme()).unwrap();
|
2023-10-26 10:15:09 -06:00
|
|
|
|
2023-11-03 11:02:25 -06:00
|
|
|
let mut tab = Self {
|
2023-10-26 10:15:09 -06:00
|
|
|
path_opt: None,
|
|
|
|
|
attrs,
|
2023-11-03 11:02:25 -06:00
|
|
|
editor: Mutex::new(ViEditor::new(editor)),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Update any other config settings
|
|
|
|
|
tab.set_config(config);
|
|
|
|
|
|
|
|
|
|
tab
|
2023-10-26 10:15:09 -06:00
|
|
|
}
|
|
|
|
|
|
2023-10-27 09:30:52 -06:00
|
|
|
pub fn set_config(&mut self, config: &Config) {
|
|
|
|
|
let mut editor = self.editor.lock().unwrap();
|
|
|
|
|
let mut font_system = FONT_SYSTEM.lock().unwrap();
|
|
|
|
|
let mut editor = editor.borrow_with(&mut font_system);
|
2023-11-16 09:00:48 -07:00
|
|
|
editor.set_auto_indent(config.auto_indent);
|
2023-11-01 14:19:39 -06:00
|
|
|
editor.set_passthrough(!config.vim_bindings);
|
2023-11-16 08:44:23 -07:00
|
|
|
editor.set_tab_width(config.tab_width);
|
2023-11-01 09:44:11 -06:00
|
|
|
editor.buffer_mut().set_wrap(if config.word_wrap {
|
|
|
|
|
Wrap::Word
|
|
|
|
|
} else {
|
|
|
|
|
Wrap::None
|
|
|
|
|
});
|
2023-11-03 11:02:25 -06:00
|
|
|
//TODO: dynamically discover light/dark changes
|
2023-11-13 09:08:31 -07:00
|
|
|
editor.update_theme(config.syntax_theme());
|
2023-10-27 09:30:52 -06:00
|
|
|
}
|
|
|
|
|
|
2023-10-26 10:15:09 -06:00
|
|
|
pub fn open(&mut self, path: PathBuf) {
|
|
|
|
|
let mut editor = self.editor.lock().unwrap();
|
|
|
|
|
let mut font_system = FONT_SYSTEM.lock().unwrap();
|
|
|
|
|
let mut editor = editor.borrow_with(&mut font_system);
|
|
|
|
|
match editor.load_text(&path, self.attrs) {
|
|
|
|
|
Ok(()) => {
|
2023-10-27 13:41:48 -06:00
|
|
|
log::info!("opened {:?}", path);
|
|
|
|
|
self.path_opt = match fs::canonicalize(&path) {
|
|
|
|
|
Ok(ok) => Some(ok),
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::error!("failed to canonicalize {:?}: {}", path, err);
|
|
|
|
|
Some(path)
|
|
|
|
|
}
|
|
|
|
|
};
|
2023-10-26 10:15:09 -06:00
|
|
|
}
|
|
|
|
|
Err(err) => {
|
2023-10-27 13:41:48 -06:00
|
|
|
log::error!("failed to open {:?}: {}", path, err);
|
2023-10-26 10:15:09 -06:00
|
|
|
self.path_opt = None;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn save(&mut self) {
|
|
|
|
|
if let Some(path) = &self.path_opt {
|
2023-11-13 14:47:17 -07:00
|
|
|
let mut editor = self.editor.lock().unwrap();
|
2023-10-26 10:15:09 -06:00
|
|
|
let mut text = String::new();
|
|
|
|
|
for line in editor.buffer().lines.iter() {
|
|
|
|
|
text.push_str(line.text());
|
|
|
|
|
text.push('\n');
|
|
|
|
|
}
|
|
|
|
|
match fs::write(path, text) {
|
|
|
|
|
Ok(()) => {
|
2023-11-13 14:47:17 -07:00
|
|
|
editor.set_changed(false);
|
2023-10-27 13:41:48 -06:00
|
|
|
log::info!("saved {:?}", path);
|
2023-10-26 10:15:09 -06:00
|
|
|
}
|
|
|
|
|
Err(err) => {
|
2023-10-27 13:41:48 -06:00
|
|
|
log::error!("failed to save {:?}: {}", path, err);
|
2023-10-26 10:15:09 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
log::warn!("tab has no path yet");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-13 14:47:17 -07:00
|
|
|
pub fn changed(&self) -> bool {
|
|
|
|
|
let editor = self.editor.lock().unwrap();
|
|
|
|
|
editor.changed()
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-13 10:33:26 -07:00
|
|
|
pub fn icon(&self, size: u16) -> Icon {
|
|
|
|
|
match &self.path_opt {
|
|
|
|
|
Some(path) => mime_icon(path, size),
|
|
|
|
|
None => icon::from_name(FALLBACK_MIME_ICON).size(size).icon(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-26 10:15:09 -06:00
|
|
|
pub fn title(&self) -> String {
|
|
|
|
|
//TODO: show full title when there is a conflict
|
|
|
|
|
if let Some(path) = &self.path_opt {
|
|
|
|
|
match path.file_name() {
|
|
|
|
|
Some(file_name_os) => match file_name_os.to_str() {
|
|
|
|
|
Some(file_name) => file_name.to_string(),
|
|
|
|
|
None => format!("{}", path.display()),
|
|
|
|
|
},
|
|
|
|
|
None => format!("{}", path.display()),
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2023-10-30 09:34:36 -06:00
|
|
|
fl!("new-document")
|
2023-10-26 10:15:09 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|