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::{
|
2023-10-11 15:52:46 -06:00
|
|
|
app::{Command, Core, Settings},
|
2023-10-11 14:15:46 -06:00
|
|
|
executor,
|
2023-02-07 13:00:49 -07:00
|
|
|
iced::{
|
2023-10-11 15:52:46 -06:00
|
|
|
widget::{column, row, text},
|
|
|
|
|
Alignment, Length,
|
2023-02-07 13:00:49 -07:00
|
|
|
},
|
2023-10-11 15:52:46 -06:00
|
|
|
widget::{icon, segmented_button, view_switcher},
|
2023-10-11 14:15:46 -06:00
|
|
|
ApplicationExt, Element,
|
2023-02-07 13:00:49 -07:00
|
|
|
};
|
2023-10-11 15:52:46 -06:00
|
|
|
use cosmic_text::{Attrs, Buffer, Edit, FontSystem, Metrics, SyntaxEditor, SyntaxSystem};
|
2023-02-07 13:00:49 -07:00
|
|
|
use std::{env, fs, path::PathBuf, sync::Mutex};
|
|
|
|
|
|
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
|
|
|
];
|
|
|
|
|
|
2023-10-11 14:15:46 -06: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
|
|
|
|
2023-10-11 14:15:46 -06:00
|
|
|
let settings = Settings::default();
|
|
|
|
|
//TODO: settings.window.min_size = Some((400, 100));
|
|
|
|
|
let flags = ();
|
|
|
|
|
cosmic::app::run::<App>(settings, flags)?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
2023-02-07 13:00:49 -07:00
|
|
|
}
|
|
|
|
|
|
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>,
|
|
|
|
|
#[cfg(not(feature = "vi"))]
|
|
|
|
|
editor: Mutex<SyntaxEditor<'static>>,
|
|
|
|
|
#[cfg(feature = "vi")]
|
|
|
|
|
editor: Mutex<cosmic_text::ViEditor<'static>>,
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 15:13:38 -07:00
|
|
|
impl Tab {
|
|
|
|
|
pub fn new() -> Self {
|
2023-08-18 09:39:37 -06:00
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "vi")]
|
|
|
|
|
let editor = cosmic_text::ViEditor::new(editor);
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-11 14:15:46 -06:00
|
|
|
pub struct App {
|
|
|
|
|
core: Core,
|
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,
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-11 14:15:46 -06:00
|
|
|
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-11 14:15:46 -06:00
|
|
|
pub fn open_tab(&mut self, path_opt: Option<PathBuf>) {
|
2023-02-09 15:13:38 -07:00
|
|
|
let mut tab = Tab::new();
|
2023-10-11 14:15:46 -06:00
|
|
|
if let Some(path) = path_opt {
|
|
|
|
|
tab.open(path);
|
2023-02-07 13:00:49 -07:00
|
|
|
}
|
2023-10-11 14:15:46 -06:00
|
|
|
self.tab_model
|
2023-02-10 07:34:43 -07:00
|
|
|
.insert()
|
2023-02-09 15:13:38 -07:00
|
|
|
.text(tab.title())
|
2023-10-11 14:15:46 -06:00
|
|
|
.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
|
|
|
}
|
|
|
|
|
|
2023-10-11 14:15:46 -06: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(),
|
2023-10-11 14:15:46 -06:00
|
|
|
None => format!("No Open File"),
|
|
|
|
|
};
|
|
|
|
|
let window_title = format!("{title} - COSMIC Text Editor");
|
|
|
|
|
self.core.window.header_title = title.clone();
|
|
|
|
|
self.set_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>) {
|
2023-10-11 15:52:46 -06:00
|
|
|
let mut app = App {
|
|
|
|
|
core,
|
|
|
|
|
tab_model: segmented_button::Model::builder().build(),
|
|
|
|
|
};
|
2023-10-11 14:15:46 -06:00
|
|
|
|
|
|
|
|
for path in env::args().skip(1) {
|
|
|
|
|
app.open_tab(Some(PathBuf::from(path)));
|
2023-02-07 13:00:49 -07:00
|
|
|
}
|
2023-10-11 14:15:46 -06:00
|
|
|
|
2023-10-11 15:52:46 -06:00
|
|
|
// Open an empty file if no arguments provided
|
|
|
|
|
if app.tab_model.iter().next().is_none() {
|
|
|
|
|
app.open_tab(None);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-11 14:15:46 -06:00
|
|
|
let command = app.update_title();
|
|
|
|
|
(app, command)
|
2023-02-07 13:00:49 -07:00
|
|
|
}
|
|
|
|
|
|
2023-10-11 14:15:46 -06: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() {
|
2023-10-11 14:15:46 -06:00
|
|
|
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 => {
|
2023-10-11 14:15:46 -06:00
|
|
|
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
|
|
|
}
|
2023-10-11 14:15:46 -06: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 => {
|
2023-10-11 14:15:46 -06:00
|
|
|
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-02-09 15:13:38 -07:00
|
|
|
let menu_bar = row![
|
|
|
|
|
MenuList::new(vec!["Open", "Save"], None, |item| {
|
|
|
|
|
match item {
|
|
|
|
|
"Open" => Message::Open,
|
|
|
|
|
"Save" => Message::Save,
|
2023-02-10 07:34:43 -07:00
|
|
|
_ => Message::Todo,
|
2023-02-09 15:13:38 -07:00
|
|
|
}
|
|
|
|
|
})
|
2023-02-10 07:34:43 -07:00
|
|
|
.padding(8)
|
|
|
|
|
.placeholder("File"),
|
2023-08-18 09:39:37 -06:00
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
let tab_bar = view_switcher::horizontal(&self.tab_model)
|
2023-03-14 14:55:50 -06:00
|
|
|
.on_activate(Message::TabActivate)
|
|
|
|
|
.on_close(Message::TabClose)
|
2023-02-09 15:13:38 -07:00
|
|
|
.width(Length::Shrink);
|
2023-02-07 13:00:49 -07:00
|
|
|
|
2023-10-11 14:15:46 -06:00
|
|
|
let active_tab: Element<_> = match self.active_tab() {
|
|
|
|
|
Some(tab) => text_box(&tab.editor).padding(8).into(),
|
|
|
|
|
None => {
|
|
|
|
|
log::warn!("TODO: No tab open");
|
|
|
|
|
text("no tab active").into()
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let content: Element<_> =
|
|
|
|
|
column![menu_bar, column![tab_bar, active_tab,].padding([0, 16])].into();
|
2023-02-09 15:13:38 -07:00
|
|
|
|
|
|
|
|
// Uncomment to debug layout:
|
|
|
|
|
//content.explain(Color::WHITE)
|
2023-02-07 13:00:49 -07:00
|
|
|
content
|
|
|
|
|
}
|
|
|
|
|
}
|