2022-10-24 08:56:48 -06:00
|
|
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
|
|
|
|
2022-10-18 12:07:22 -06:00
|
|
|
use cosmic::{
|
2022-11-09 08:03:13 -07:00
|
|
|
Element,
|
2022-10-18 12:07:22 -06:00
|
|
|
iced::{
|
|
|
|
|
self,
|
2022-10-29 10:26:41 -07:00
|
|
|
Color,
|
2022-10-18 14:35:16 -06:00
|
|
|
Alignment,
|
2022-10-18 12:07:22 -06:00
|
|
|
Application,
|
|
|
|
|
Command,
|
2022-10-19 13:15:07 -06:00
|
|
|
Length,
|
2022-10-18 14:35:16 -06:00
|
|
|
widget::{
|
|
|
|
|
column,
|
2022-10-19 13:15:07 -06:00
|
|
|
horizontal_space,
|
2022-10-18 14:35:16 -06:00
|
|
|
pick_list,
|
|
|
|
|
row,
|
|
|
|
|
},
|
2022-10-18 12:07:22 -06:00
|
|
|
},
|
|
|
|
|
settings,
|
2022-11-09 08:03:13 -07:00
|
|
|
theme::Theme,
|
2022-10-19 13:15:07 -06:00
|
|
|
widget::{
|
|
|
|
|
button,
|
2022-10-25 16:29:33 -06:00
|
|
|
toggler,
|
2022-10-19 13:15:07 -06:00
|
|
|
},
|
2022-10-18 12:07:22 -06:00
|
|
|
};
|
|
|
|
|
use cosmic_text::{
|
2022-10-27 09:56:53 -06:00
|
|
|
Attrs,
|
|
|
|
|
AttrsList,
|
2022-10-31 11:24:36 -06:00
|
|
|
Buffer,
|
|
|
|
|
Editor,
|
2022-10-18 12:07:22 -06:00
|
|
|
FontSystem,
|
2022-10-31 11:24:36 -06:00
|
|
|
Metrics,
|
2022-10-18 12:07:22 -06:00
|
|
|
};
|
|
|
|
|
use std::{
|
|
|
|
|
env,
|
|
|
|
|
fs,
|
2022-10-19 14:05:14 -06:00
|
|
|
path::PathBuf,
|
2022-10-19 08:05:22 -06:00
|
|
|
sync::Mutex,
|
2022-10-18 12:07:22 -06:00
|
|
|
};
|
|
|
|
|
|
2022-10-31 12:04:33 -06:00
|
|
|
use self::text::text;
|
|
|
|
|
mod text;
|
|
|
|
|
|
2022-10-18 12:07:22 -06:00
|
|
|
use self::text_box::text_box;
|
|
|
|
|
mod text_box;
|
|
|
|
|
|
|
|
|
|
lazy_static::lazy_static! {
|
2022-11-02 19:25:18 -07:00
|
|
|
static ref FONT_SYSTEM: FontSystem = FontSystem::new();
|
2022-10-18 12:07:22 -06:00
|
|
|
}
|
|
|
|
|
|
2022-10-31 11:24:36 -06:00
|
|
|
static FONT_SIZES: &'static [Metrics] = &[
|
|
|
|
|
Metrics::new(10, 14), // Caption
|
|
|
|
|
Metrics::new(14, 20), // Body
|
|
|
|
|
Metrics::new(20, 28), // Title 4
|
|
|
|
|
Metrics::new(24, 32), // Title 3
|
|
|
|
|
Metrics::new(28, 36), // Title 2
|
|
|
|
|
Metrics::new(32, 44), // Title 1
|
2022-10-18 14:35:16 -06:00
|
|
|
];
|
|
|
|
|
|
2022-10-18 12:07:22 -06:00
|
|
|
fn main() -> cosmic::iced::Result {
|
|
|
|
|
env_logger::init();
|
|
|
|
|
|
|
|
|
|
let mut settings = settings();
|
|
|
|
|
settings.window.min_size = Some((400, 100));
|
|
|
|
|
Window::run(settings)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct Window {
|
2022-10-18 14:35:16 -06:00
|
|
|
theme: Theme,
|
2022-10-19 14:05:14 -06:00
|
|
|
path_opt: Option<PathBuf>,
|
2022-10-27 09:56:53 -06:00
|
|
|
attrs: Attrs<'static>,
|
2022-10-31 11:24:36 -06:00
|
|
|
editor: Mutex<Editor<'static>>,
|
2022-10-18 12:07:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
2022-10-18 14:35:16 -06:00
|
|
|
pub enum Message {
|
2022-10-19 13:15:07 -06:00
|
|
|
Open,
|
2022-10-19 14:13:05 -06:00
|
|
|
Save,
|
2022-10-25 16:29:33 -06:00
|
|
|
Bold(bool),
|
|
|
|
|
Italic(bool),
|
|
|
|
|
Monospaced(bool),
|
2022-10-31 11:24:36 -06:00
|
|
|
MetricsChanged(Metrics),
|
2022-10-18 14:35:16 -06:00
|
|
|
ThemeChanged(&'static str),
|
|
|
|
|
}
|
2022-10-18 12:07:22 -06:00
|
|
|
|
2022-10-19 14:05:14 -06:00
|
|
|
impl Window {
|
|
|
|
|
pub fn open(&mut self, path: PathBuf) {
|
2022-10-31 11:24:36 -06:00
|
|
|
let mut editor = self.editor.lock().unwrap();
|
2022-10-19 14:05:14 -06:00
|
|
|
match fs::read_to_string(&path) {
|
|
|
|
|
Ok(text) => {
|
2022-10-19 14:13:05 -06:00
|
|
|
log::info!("opened '{}'", path.display());
|
2022-10-31 11:24:36 -06:00
|
|
|
editor.buffer.set_text(&text, self.attrs);
|
2022-10-19 14:05:14 -06:00
|
|
|
self.path_opt = Some(path);
|
|
|
|
|
},
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::error!("failed to open '{}': {}", path.display(), err);
|
2022-10-31 11:24:36 -06:00
|
|
|
editor.buffer.set_text("", self.attrs);
|
2022-10-19 14:05:14 -06:00
|
|
|
self.path_opt = None;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-18 12:07:22 -06: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>) {
|
2022-10-26 10:59:00 -06:00
|
|
|
let attrs = cosmic_text::Attrs::new()
|
|
|
|
|
.monospaced(true)
|
|
|
|
|
.family(cosmic_text::Family::Monospace);
|
2022-10-27 09:56:53 -06:00
|
|
|
|
2022-10-31 11:24:36 -06:00
|
|
|
let mut editor = Editor::new(Buffer::new(
|
2022-10-25 16:13:07 -06:00
|
|
|
&FONT_SYSTEM,
|
2022-10-27 09:56:53 -06:00
|
|
|
FONT_SIZES[1 /* Body */],
|
2022-10-31 11:24:36 -06:00
|
|
|
));
|
|
|
|
|
update_attrs(&mut editor, attrs);
|
2022-10-19 13:15:07 -06:00
|
|
|
|
2022-10-19 14:05:14 -06:00
|
|
|
let mut window = Window {
|
2022-10-18 14:35:16 -06:00
|
|
|
theme: Theme::Dark,
|
2022-10-19 14:05:14 -06:00
|
|
|
path_opt: None,
|
2022-10-27 09:56:53 -06:00
|
|
|
attrs,
|
2022-10-31 11:24:36 -06:00
|
|
|
editor: Mutex::new(editor),
|
2022-10-18 12:07:22 -06:00
|
|
|
};
|
2022-10-19 14:05:14 -06:00
|
|
|
if let Some(arg) = env::args().nth(1) {
|
|
|
|
|
window.open(PathBuf::from(arg));
|
|
|
|
|
}
|
2022-10-18 12:07:22 -06:00
|
|
|
(window, Command::none())
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-18 14:35:16 -06:00
|
|
|
fn theme(&self) -> Theme {
|
|
|
|
|
self.theme
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-18 12:07:22 -06:00
|
|
|
fn title(&self) -> String {
|
2022-10-19 14:05:14 -06:00
|
|
|
if let Some(path) = &self.path_opt {
|
2022-11-02 19:25:18 -07:00
|
|
|
format!("COSMIC Text - {} - {}", FONT_SYSTEM.locale(), path.display())
|
2022-10-19 14:05:14 -06:00
|
|
|
} else {
|
2022-11-02 19:25:18 -07:00
|
|
|
format!("COSMIC Text - {}", FONT_SYSTEM.locale())
|
2022-10-19 14:05:14 -06:00
|
|
|
}
|
2022-10-18 12:07:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn update(&mut self, message: Message) -> iced::Command<Self::Message> {
|
2022-10-18 14:35:16 -06:00
|
|
|
match message {
|
2022-10-19 13:15:07 -06:00
|
|
|
Message::Open => {
|
|
|
|
|
if let Some(path) = rfd::FileDialog::new().pick_file() {
|
2022-10-19 14:05:14 -06:00
|
|
|
self.open(path);
|
2022-10-19 13:15:07 -06:00
|
|
|
}
|
|
|
|
|
},
|
2022-10-19 14:13:05 -06:00
|
|
|
Message::Save => {
|
|
|
|
|
if let Some(path) = &self.path_opt {
|
2022-10-31 11:24:36 -06:00
|
|
|
let editor = self.editor.lock().unwrap();
|
2022-10-20 19:34:56 -06:00
|
|
|
let mut text = String::new();
|
2022-10-31 11:24:36 -06:00
|
|
|
for line in editor.buffer.lines.iter() {
|
2022-10-20 19:34:56 -06:00
|
|
|
text.push_str(line.text());
|
|
|
|
|
text.push('\n');
|
|
|
|
|
}
|
2022-10-19 14:13:05 -06:00
|
|
|
match fs::write(path, text) {
|
|
|
|
|
Ok(()) => {
|
|
|
|
|
log::info!("saved '{}'", path.display());
|
|
|
|
|
},
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::error!("failed to save '{}': {}", path.display(), err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
2022-10-25 16:29:33 -06:00
|
|
|
Message::Bold(bold) => {
|
2022-10-27 09:56:53 -06:00
|
|
|
self.attrs = self.attrs.weight(if bold {
|
2022-10-25 16:29:33 -06:00
|
|
|
cosmic_text::Weight::BOLD
|
|
|
|
|
} else {
|
|
|
|
|
cosmic_text::Weight::NORMAL
|
|
|
|
|
});
|
|
|
|
|
|
2022-10-31 11:24:36 -06:00
|
|
|
let mut editor = self.editor.lock().unwrap();
|
|
|
|
|
update_attrs(&mut editor, self.attrs);
|
2022-10-27 09:56:53 -06:00
|
|
|
},
|
|
|
|
|
Message::Italic(italic) => {
|
|
|
|
|
self.attrs = self.attrs.style(if italic {
|
2022-10-25 16:29:33 -06:00
|
|
|
cosmic_text::Style::Italic
|
|
|
|
|
} else {
|
|
|
|
|
cosmic_text::Style::Normal
|
|
|
|
|
});
|
|
|
|
|
|
2022-10-31 11:24:36 -06:00
|
|
|
let mut editor = self.editor.lock().unwrap();
|
|
|
|
|
update_attrs(&mut editor, self.attrs);
|
2022-10-27 09:56:53 -06:00
|
|
|
},
|
|
|
|
|
Message::Monospaced(monospaced) => {
|
|
|
|
|
self.attrs = self.attrs
|
2022-10-25 22:09:49 -06:00
|
|
|
.family(if monospaced {
|
|
|
|
|
cosmic_text::Family::Monospace
|
|
|
|
|
} else {
|
|
|
|
|
cosmic_text::Family::SansSerif
|
|
|
|
|
})
|
|
|
|
|
.monospaced(monospaced);
|
2022-10-27 09:56:53 -06:00
|
|
|
|
2022-10-31 11:24:36 -06:00
|
|
|
let mut editor = self.editor.lock().unwrap();
|
|
|
|
|
update_attrs(&mut editor, self.attrs);
|
2022-10-25 16:29:33 -06:00
|
|
|
},
|
2022-10-18 17:04:22 -06:00
|
|
|
Message::MetricsChanged(metrics) => {
|
2022-10-31 11:24:36 -06:00
|
|
|
let mut editor = self.editor.lock().unwrap();
|
|
|
|
|
editor.buffer.set_metrics(metrics);
|
2022-10-18 14:35:16 -06:00
|
|
|
},
|
2022-10-29 10:26:41 -07:00
|
|
|
Message::ThemeChanged(theme) => {
|
|
|
|
|
self.theme = match theme {
|
|
|
|
|
"Dark" => Theme::Dark,
|
|
|
|
|
"Light" => Theme::Light,
|
|
|
|
|
_ => return Command::none(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let Color { r, g, b, a } = self.theme.palette().text;
|
|
|
|
|
let as_u8 = |component: f32| (component * 255.0) as u8;
|
|
|
|
|
self.attrs = self.attrs.color(cosmic_text::Color::rgba(as_u8(r), as_u8(g), as_u8(b), as_u8(a)));
|
|
|
|
|
|
2022-10-31 11:24:36 -06:00
|
|
|
let mut editor = self.editor.lock().unwrap();
|
|
|
|
|
update_attrs(&mut editor, self.attrs);
|
2022-10-18 14:35:16 -06:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-18 12:07:22 -06:00
|
|
|
Command::none()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn view(&self) -> Element<Message> {
|
2022-10-18 14:35:16 -06:00
|
|
|
static THEMES: &'static [&'static str] = &["Dark", "Light"];
|
|
|
|
|
let theme_picker = pick_list(
|
|
|
|
|
THEMES,
|
|
|
|
|
Some(match self.theme {
|
|
|
|
|
Theme::Dark => THEMES[0],
|
|
|
|
|
Theme::Light => THEMES[1],
|
|
|
|
|
}),
|
|
|
|
|
Message::ThemeChanged
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let font_size_picker = {
|
2022-10-31 11:24:36 -06:00
|
|
|
let editor = self.editor.lock().unwrap();
|
2022-10-18 14:35:16 -06:00
|
|
|
pick_list(
|
|
|
|
|
FONT_SIZES,
|
2022-10-31 11:24:36 -06:00
|
|
|
Some(editor.buffer.metrics()),
|
2022-10-18 17:04:22 -06:00
|
|
|
Message::MetricsChanged
|
2022-10-18 14:35:16 -06:00
|
|
|
)
|
|
|
|
|
};
|
|
|
|
|
|
2022-10-27 16:16:28 -06:00
|
|
|
let content: Element<_> = column![
|
2022-10-18 14:35:16 -06:00
|
|
|
row![
|
2022-10-19 13:15:07 -06:00
|
|
|
button!("Open").on_press(Message::Open),
|
2022-10-19 14:13:05 -06:00
|
|
|
button!("Save").on_press(Message::Save),
|
2022-10-19 13:15:07 -06:00
|
|
|
horizontal_space(Length::Fill),
|
2022-10-31 12:04:33 -06:00
|
|
|
text("Bold:"),
|
2022-10-27 09:56:53 -06:00
|
|
|
toggler(None, self.attrs.weight == cosmic_text::Weight::BOLD, Message::Bold),
|
2022-10-31 12:04:33 -06:00
|
|
|
text("Italic:"),
|
2022-10-27 09:56:53 -06:00
|
|
|
toggler(None, self.attrs.style == cosmic_text::Style::Italic, Message::Italic),
|
2022-10-31 12:04:33 -06:00
|
|
|
text("Monospaced:"),
|
2022-10-27 09:56:53 -06:00
|
|
|
toggler(None, self.attrs.monospaced, Message::Monospaced),
|
2022-10-31 12:04:33 -06:00
|
|
|
text("Theme:"),
|
2022-10-18 14:35:16 -06:00
|
|
|
theme_picker,
|
2022-10-31 12:04:33 -06:00
|
|
|
text("Font Size:"),
|
2022-10-18 14:35:16 -06:00
|
|
|
font_size_picker,
|
|
|
|
|
]
|
|
|
|
|
.align_items(Alignment::Center)
|
|
|
|
|
.spacing(8)
|
|
|
|
|
,
|
2022-10-31 11:24:36 -06:00
|
|
|
text_box(&self.editor)
|
2022-10-18 14:35:16 -06:00
|
|
|
]
|
|
|
|
|
.spacing(8)
|
|
|
|
|
.padding(16)
|
2022-10-27 16:16:28 -06:00
|
|
|
.into();
|
|
|
|
|
|
|
|
|
|
// Uncomment to debug layout: content.explain(Color::WHITE)
|
|
|
|
|
content
|
2022-10-18 12:07:22 -06:00
|
|
|
}
|
|
|
|
|
}
|
2022-10-29 10:15:09 -07:00
|
|
|
|
2022-10-31 11:24:36 -06:00
|
|
|
fn update_attrs<'a>(editor: &mut Editor<'a>, attrs: Attrs<'a>) {
|
|
|
|
|
editor.buffer.lines.iter_mut().for_each(|line| {
|
2022-10-29 10:15:09 -07:00
|
|
|
line.set_attrs_list(AttrsList::new(attrs));
|
|
|
|
|
});
|
|
|
|
|
}
|