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::{
|
|
|
|
|
iced::{
|
2023-08-18 09:39:37 -06:00
|
|
|
self, settings,
|
2023-02-09 15:13:38 -07:00
|
|
|
widget::{column, container, horizontal_space, pick_list, row, text},
|
2023-02-07 13:00:49 -07:00
|
|
|
Alignment, Application, Color, Command, Length,
|
|
|
|
|
},
|
2023-03-14 13:41:49 -06:00
|
|
|
theme::{self, Theme, ThemeType},
|
2023-02-09 15:13:38 -07:00
|
|
|
widget::{button, segmented_button, toggler, view_switcher},
|
2023-02-07 13:00:49 -07:00
|
|
|
Element,
|
|
|
|
|
};
|
|
|
|
|
use cosmic_text::{
|
|
|
|
|
Attrs, AttrsList, Buffer, Edit, FontSystem, Metrics, SyntaxEditor, SyntaxSystem, Wrap,
|
|
|
|
|
};
|
|
|
|
|
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
|
|
|
];
|
|
|
|
|
|
|
|
|
|
fn main() -> cosmic::iced::Result {
|
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-08-18 09:39:37 -06:00
|
|
|
let mut settings = settings::Settings::default();
|
2023-02-07 13:00:49 -07:00
|
|
|
settings.window.min_size = Some((400, 100));
|
|
|
|
|
Window::run(settings)
|
|
|
|
|
}
|
|
|
|
|
|
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()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct Window {
|
|
|
|
|
theme: Theme,
|
|
|
|
|
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 Window {
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Application for Window {
|
|
|
|
|
type Executor = iced::executor::Default;
|
|
|
|
|
type Flags = ();
|
|
|
|
|
type Message = Message;
|
|
|
|
|
type Theme = Theme;
|
|
|
|
|
|
|
|
|
|
fn new(_flags: ()) -> (Self, Command<Self::Message>) {
|
2023-02-10 07:34:43 -07:00
|
|
|
let mut tab_model = segmented_button::Model::builder().build();
|
2023-02-07 13:00:49 -07:00
|
|
|
|
2023-02-09 15:13:38 -07:00
|
|
|
let mut tab = Tab::new();
|
2023-02-07 13:00:49 -07:00
|
|
|
if let Some(arg) = env::args().nth(1) {
|
2023-02-09 15:13:38 -07:00
|
|
|
tab.open(PathBuf::from(arg));
|
2023-02-07 13:00:49 -07:00
|
|
|
}
|
2023-02-09 15:13:38 -07:00
|
|
|
|
2023-02-10 07:34:43 -07:00
|
|
|
tab_model
|
|
|
|
|
.insert()
|
2023-02-09 15:13:38 -07:00
|
|
|
.text(tab.title())
|
|
|
|
|
.icon("text-x-generic")
|
2023-03-14 14:55:50 -06:00
|
|
|
.icon_color(None)
|
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();
|
|
|
|
|
|
|
|
|
|
(
|
|
|
|
|
Window {
|
2023-03-14 13:41:49 -06:00
|
|
|
theme: Theme::dark(),
|
2023-02-09 15:13:38 -07:00
|
|
|
tab_model,
|
|
|
|
|
},
|
2023-02-10 07:34:43 -07:00
|
|
|
Command::none(),
|
2023-02-09 15:13:38 -07:00
|
|
|
)
|
2023-02-07 13:00:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn title(&self) -> String {
|
2023-02-09 15:13:38 -07:00
|
|
|
match self.active_tab() {
|
|
|
|
|
Some(tab) => tab.title(),
|
|
|
|
|
None => format!("COSMIC Text Editor"),
|
2023-02-07 13:00:49 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn update(&mut self, message: Message) -> iced::Command<Self::Message> {
|
|
|
|
|
match message {
|
|
|
|
|
Message::Open => {
|
|
|
|
|
if let Some(path) = rfd::FileDialog::new().pick_file() {
|
2023-02-09 15:13:38 -07:00
|
|
|
let mut tab = Tab::new();
|
|
|
|
|
tab.open(path);
|
|
|
|
|
|
2023-02-10 07:34:43 -07:00
|
|
|
self.tab_model
|
|
|
|
|
.insert()
|
2023-02-09 15:13:38 -07:00
|
|
|
.text(tab.title())
|
|
|
|
|
.icon("text-x-generic")
|
2023-03-14 14:55:50 -06:00
|
|
|
.icon_color(None)
|
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-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::info!("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-03-14 14:55:50 -06:00
|
|
|
Message::TabActivate(entity) => self.tab_model.activate(entity),
|
|
|
|
|
Message::TabClose(entity) => self.tab_model.remove(entity),
|
2023-02-09 15:13:38 -07:00
|
|
|
Message::Todo => {
|
|
|
|
|
log::info!("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-02-09 15:13:38 -07:00
|
|
|
let content: Element<_> = column![
|
|
|
|
|
menu_bar,
|
|
|
|
|
column![
|
|
|
|
|
tab_bar,
|
|
|
|
|
match self.active_tab() {
|
|
|
|
|
Some(tab) => {
|
2023-02-10 07:34:43 -07:00
|
|
|
text_box(&tab.editor).padding(8)
|
|
|
|
|
}
|
2023-02-09 15:13:38 -07:00
|
|
|
None => {
|
|
|
|
|
panic!("TODO: No tab open");
|
2023-02-10 07:34:43 -07:00
|
|
|
}
|
2023-02-09 15:13:38 -07:00
|
|
|
}
|
2023-02-10 07:34:43 -07:00
|
|
|
]
|
|
|
|
|
.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
|
|
|
|
|
}
|
|
|
|
|
}
|