Add save button

This commit is contained in:
Jeremy Soller 2022-10-19 14:13:05 -06:00
parent bfdc9a6d66
commit 3035bad29a
No known key found for this signature in database
GPG key ID: 87F211AF2BE4C2FE

View file

@ -102,6 +102,7 @@ pub struct Window {
#[derive(Clone, Copy, Debug)]
pub enum Message {
Open,
Save,
MetricsChanged(TextMetrics),
ThemeChanged(&'static str),
}
@ -111,6 +112,7 @@ impl Window {
let mut buffer = self.buffer.lock().unwrap();
match fs::read_to_string(&path) {
Ok(text) => {
log::info!("opened '{}'", path.display());
buffer.set_text(&text);
self.path_opt = Some(path);
},
@ -131,7 +133,7 @@ impl Application for Window {
fn new(_flags: ()) -> (Self, Command<Self::Message>) {
let font_size_i = 1; // Body
let mut buffer = TextBuffer::new(
let buffer = TextBuffer::new(
unsafe { FONT_MATCHES.as_ref().unwrap() },
FONT_SIZES[font_size_i],
);
@ -166,6 +168,20 @@ impl Application for Window {
self.open(path);
}
},
Message::Save => {
if let Some(path) = &self.path_opt {
let buffer = self.buffer.lock().unwrap();
let text = buffer.text_lines().join("\n");
match fs::write(path, text) {
Ok(()) => {
log::info!("saved '{}'", path.display());
},
Err(err) => {
log::error!("failed to save '{}': {}", path.display(), err);
}
}
}
},
Message::MetricsChanged(metrics) => {
let mut buffer = self.buffer.lock().unwrap();
buffer.set_metrics(metrics);
@ -203,6 +219,7 @@ impl Application for Window {
column![
row![
button!("Open").on_press(Message::Open),
button!("Save").on_press(Message::Save),
horizontal_space(Length::Fill),
text("Theme:"),
theme_picker,