cosmic-edit/src/main.rs

427 lines
13 KiB
Rust
Raw Normal View History

2023-02-10 07:42:05 -07:00
// SPDX-License-Identifier: GPL-3.0-only
2023-02-07 13:00:49 -07:00
use cosmic::{
app::{Command, Core, Settings},
executor,
2023-02-07 13:00:49 -07:00
iced::{
widget::{column, row, text},
2023-10-11 19:32:04 -06:00
Alignment, Length, Limits,
2023-02-07 13:00:49 -07:00
},
2023-10-25 07:35:38 -06:00
theme,
widget::{
self, button, icon,
menu::{MenuBar, MenuTree},
segmented_button, view_switcher,
},
ApplicationExt, Element,
2023-02-07 13:00:49 -07:00
};
use cosmic_text::{
Attrs, Buffer, Edit, FontSystem, Metrics, SyntaxEditor, SyntaxSystem, ViEditor, ViMode,
};
2023-10-25 07:35:38 -06:00
use std::{
env, fs, io,
path::{Path, PathBuf},
sync::Mutex,
};
2023-02-07 13:00:49 -07:00
2023-02-09 15:13:38 -07:00
use self::menu_list::MenuList;
mod menu_list;
2023-02-07 13:00:49 -07:00
use self::text_box::text_box;
mod text_box;
lazy_static::lazy_static! {
2023-03-17 18:48:56 -06:00
static ref FONT_SYSTEM: Mutex<FontSystem> = Mutex::new(FontSystem::new());
2023-02-07 13:00:49 -07:00
static ref SYNTAX_SYSTEM: SyntaxSystem = SyntaxSystem::new();
}
static FONT_SIZES: &'static [Metrics] = &[
2023-03-17 18:48:56 -06:00
Metrics::new(10.0, 14.0), // Caption
Metrics::new(14.0, 20.0), // Body
Metrics::new(20.0, 28.0), // Title 4
Metrics::new(24.0, 32.0), // Title 3
Metrics::new(28.0, 36.0), // Title 2
Metrics::new(32.0, 44.0), // Title 1
2023-02-07 13:00:49 -07:00
];
fn main() -> Result<(), Box<dyn std::error::Error>> {
2023-02-09 15:13:38 -07:00
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
2023-02-07 13:00:49 -07:00
let settings = Settings::default().size_limits(Limits::NONE.min_width(400.0).min_height(200.0));
let flags = ();
cosmic::app::run::<App>(settings, flags)?;
Ok(())
2023-02-07 13:00:49 -07:00
}
2023-10-25 07:35:38 -06:00
pub struct Project {
path: PathBuf,
name: String,
}
impl Project {
pub fn new<P: AsRef<Path>>(path: P) -> io::Result<Self> {
let path = fs::canonicalize(path)?;
let name = path
.file_name()
.ok_or(io::Error::new(
io::ErrorKind::Other,
format!("Path {:?} has no file name", path),
))?
.to_str()
.ok_or(io::Error::new(
io::ErrorKind::Other,
format!("Path {:?} is not valid UTF-8", path),
))?
.to_string();
Ok(Self { path, name })
}
}
2023-02-09 15:13:38 -07:00
pub struct Tab {
2023-02-07 13:00:49 -07:00
path_opt: Option<PathBuf>,
attrs: Attrs<'static>,
editor: Mutex<ViEditor<'static>>,
2023-02-07 13:00:49 -07:00
}
2023-02-09 15:13:38 -07:00
impl Tab {
pub fn new() -> Self {
let attrs = cosmic_text::Attrs::new().family(cosmic_text::Family::Monospace);
2023-02-09 15:13:38 -07:00
let editor = SyntaxEditor::new(
2023-03-17 18:48:56 -06:00
Buffer::new(&mut FONT_SYSTEM.lock().unwrap(), FONT_SIZES[1 /* Body */]),
2023-02-09 15:13:38 -07:00
&SYNTAX_SYSTEM,
"base16-eighties.dark",
)
.unwrap();
let mut editor = ViEditor::new(editor);
editor.set_passthrough(false);
2023-02-09 15:13:38 -07:00
Self {
path_opt: None,
attrs,
editor: Mutex::new(editor),
}
}
2023-02-07 13:00:49 -07:00
pub fn open(&mut self, path: PathBuf) {
let mut editor = self.editor.lock().unwrap();
2023-03-17 18:48:56 -06:00
let mut font_system = FONT_SYSTEM.lock().unwrap();
let mut editor = editor.borrow_with(&mut font_system);
2023-02-07 13:00:49 -07:00
match editor.load_text(&path, self.attrs) {
Ok(()) => {
log::info!("opened '{}'", path.display());
self.path_opt = Some(path);
}
Err(err) => {
log::error!("failed to open '{}': {}", path.display(), err);
self.path_opt = None;
}
}
}
2023-02-09 15:13:38 -07:00
pub fn save(&mut self) {
if let Some(path) = &self.path_opt {
let editor = self.editor.lock().unwrap();
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(()) => {
log::info!("saved '{}'", path.display());
}
Err(err) => {
log::error!("failed to save '{}': {}", path.display(), err);
}
}
} else {
log::warn!("tab has no path yet");
}
}
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 {
"New document".to_string()
}
}
}
pub struct App {
core: Core,
2023-10-25 07:35:38 -06:00
projects: Vec<Project>,
2023-02-09 15:13:38 -07:00
tab_model: segmented_button::SingleSelectModel,
}
#[allow(dead_code)]
#[derive(Clone, Copy, Debug)]
pub enum Message {
Open,
Save,
2023-03-14 14:55:50 -06:00
TabActivate(segmented_button::Entity),
TabClose(segmented_button::Entity),
2023-02-09 15:13:38 -07:00
Todo,
}
impl App {
2023-02-09 15:13:38 -07:00
pub fn active_tab(&self) -> Option<&Tab> {
self.tab_model.active_data()
}
pub fn active_tab_mut(&mut self) -> Option<&mut Tab> {
self.tab_model.active_data_mut()
}
2023-02-07 13:00:49 -07:00
2023-10-25 07:35:38 -06:00
pub fn open_project<P: AsRef<Path>>(&mut self, path: P) {
match Project::new(&path) {
Ok(project) => self.projects.push(project),
Err(err) => {
log::error!("failed to open '{}': {}", path.as_ref().display(), err);
}
}
}
pub fn open_tab(&mut self, path_opt: Option<PathBuf>) {
2023-02-09 15:13:38 -07:00
let mut tab = Tab::new();
if let Some(path) = path_opt {
tab.open(path);
2023-02-07 13:00:49 -07:00
}
self.tab_model
2023-02-10 07:34:43 -07:00
.insert()
2023-02-09 15:13:38 -07:00
.text(tab.title())
.icon(icon::from_name("text-x-generic").icon())
2023-02-09 15:13:38 -07:00
.data(tab)
2023-03-14 14:55:50 -06:00
.closable()
2023-02-09 15:13:38 -07:00
.activate();
2023-02-07 13:00:49 -07:00
}
pub fn update_title(&mut self) -> Command<Message> {
let title = match self.active_tab() {
2023-02-09 15:13:38 -07:00
Some(tab) => tab.title(),
None => format!("No Open File"),
};
let window_title = format!("{title} - COSMIC Text Editor");
2023-10-19 10:33:42 -06:00
self.set_header_title(title.clone());
self.set_window_title(window_title)
}
}
/// Implement [`cosmic::Application`] to integrate with COSMIC.
impl cosmic::Application for App {
/// Default async executor to use with the app.
type Executor = executor::Default;
/// Argument received [`cosmic::Application::new`].
type Flags = ();
/// Message type specific to our [`App`].
type Message = Message;
/// The unique application ID to supply to the window manager.
const APP_ID: &'static str = "com.system76.CosmicTextEditor";
fn core(&self) -> &Core {
&self.core
}
fn core_mut(&mut self) -> &mut Core {
&mut self.core
}
/// Creates the application, and optionally emits command on initialize.
fn init(core: Core, _flags: Self::Flags) -> (Self, Command<Self::Message>) {
let mut app = App {
core,
2023-10-25 07:35:38 -06:00
projects: Vec::new(),
tab_model: segmented_button::Model::builder().build(),
};
2023-10-25 07:35:38 -06:00
for arg in env::args().skip(1) {
let path = PathBuf::from(arg);
if path.is_dir() {
app.open_project(path);
} else {
app.open_tab(Some(path));
}
2023-02-07 13:00:49 -07:00
}
// Open an empty file if no arguments provided
if app.tab_model.iter().next().is_none() {
app.open_tab(None);
}
let command = app.update_title();
(app, command)
2023-02-07 13:00:49 -07:00
}
fn update(&mut self, message: Message) -> Command<Self::Message> {
2023-02-07 13:00:49 -07:00
match message {
Message::Open => {
if let Some(path) = rfd::FileDialog::new().pick_file() {
self.open_tab(Some(path));
return self.update_title();
2023-02-07 13:00:49 -07:00
}
2023-02-10 07:34:43 -07:00
}
2023-02-07 13:00:49 -07:00
Message::Save => {
2023-02-09 16:18:13 -07:00
let mut title_opt = None;
2023-02-09 15:13:38 -07:00
match self.active_tab_mut() {
2023-02-09 16:18:13 -07:00
Some(tab) => {
if tab.path_opt.is_none() {
tab.path_opt = rfd::FileDialog::new().save_file();
title_opt = Some(tab.title());
}
tab.save();
2023-02-10 07:34:43 -07:00
}
2023-02-09 15:13:38 -07:00
None => {
log::warn!("TODO: NO TAB OPEN");
2023-02-10 07:34:43 -07:00
}
2023-02-07 13:00:49 -07:00
}
2023-02-09 16:18:13 -07:00
if let Some(title) = title_opt {
self.tab_model.text_set(self.tab_model.active(), title);
}
2023-02-10 07:34:43 -07:00
}
Message::TabActivate(entity) => {
self.tab_model.activate(entity);
return self.update_title();
}
Message::TabClose(entity) => {
// Activate closest item
if let Some(position) = self.tab_model.position(entity) {
if position > 0 {
self.tab_model.activate_position(position - 1);
} else {
self.tab_model.activate_position(position + 1);
}
}
// Remove item
self.tab_model.remove(entity);
// If that was the last tab, make a new empty one
if self.tab_model.iter().next().is_none() {
self.open_tab(None);
}
return self.update_title();
}
2023-02-09 15:13:38 -07:00
Message::Todo => {
log::warn!("TODO");
2023-02-10 07:34:43 -07:00
}
2023-02-07 13:00:49 -07:00
}
Command::none()
}
fn view(&self) -> Element<Message> {
2023-10-25 07:35:38 -06:00
/*
2023-02-09 15:13:38 -07:00
let menu_bar = row![
2023-10-25 07:35:38 -06:00
MenuList::new(
vec![
"New file",
"New window",
"Open file...",
"Save",
"Save as..."
],
None,
|item| {
match item {
"Open" => Message::Open,
"Save" => Message::Save,
_ => Message::Todo,
}
2023-02-09 15:13:38 -07:00
}
2023-10-25 07:35:38 -06:00
)
2023-02-10 07:34:43 -07:00
.padding(8)
.placeholder("File"),
MenuList::new(vec!["Todo"], None, |_| Message::Todo).placeholder("Edit"),
MenuList::new(vec!["Todo"], None, |_| Message::Todo).placeholder("View"),
MenuList::new(vec!["Todo"], None, |_| Message::Todo).placeholder("Help"),
2023-02-07 13:00:49 -07:00
]
2023-02-09 15:13:38 -07:00
.align_items(Alignment::Start)
.padding(4)
.spacing(16);
2023-10-25 07:35:38 -06:00
*/
//TODO: port macros menu_bar! and menu_tree!
let menu_bar: Element<_> = MenuBar::new(vec![MenuTree::with_children(
button("File"),
vec![MenuTree::new(button("New file"))],
)])
.into();
2023-02-09 15:13:38 -07:00
let mut tab_column = widget::column::with_capacity(3).padding([0, 16]);
tab_column = tab_column.push(
view_switcher::horizontal(&self.tab_model)
.on_activate(Message::TabActivate)
.on_close(Message::TabClose)
.width(Length::Shrink),
);
match self.active_tab() {
Some(tab) => {
tab_column = tab_column.push(text_box(&tab.editor).padding(8));
let status = match tab.editor.lock().unwrap().mode() {
ViMode::Passthrough => {
//TODO: status line
String::new()
}
ViMode::Normal => {
//TODO: status line
String::new()
}
ViMode::Insert => {
format!("-- INSERT --")
}
ViMode::Command { value } => {
format!(":{value}|")
}
ViMode::Search { value, forwards } => {
if *forwards {
format!("/{value}|")
} else {
format!("?{value}|")
}
}
};
tab_column = tab_column.push(text(status).font(cosmic::font::Font::MONOSPACE));
}
None => {
log::warn!("TODO: No tab open");
}
};
2023-10-25 07:35:38 -06:00
let mut project_row = widget::row::with_capacity(2);
if !self.projects.is_empty() {
/*TODO: project tree view
let mut project_list = widget::column::with_capacity(self.projects.len());
for project in self.projects.iter() {
project_list = project_list.push(widget::text(&project.name));
}
project_row = project_row.push(project_list);
*/
}
project_row = project_row.push(tab_column);
let content: Element<_> = column![menu_bar, project_row].into();
2023-02-09 15:13:38 -07:00
// Uncomment to debug layout:
2023-10-25 07:35:38 -06:00
content.explain(cosmic::iced::Color::WHITE)
//content
2023-02-07 13:00:49 -07:00
}
}