Basic Open/Save mechanism #6
97
src/main.rs
97
src/main.rs
|
|
@ -14,15 +14,18 @@
|
|||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use log::warn;
|
||||
use log::{info, warn};
|
||||
|
||||
use druid::im::{vector, Vector};
|
||||
use druid::kurbo::BezPath;
|
||||
use druid::widget::prelude::*;
|
||||
use druid::{AppLauncher, Color, Data, Event, LocalizedString, WindowDesc};
|
||||
use druid::{AppLauncher, Color, Data, Event, LocalizedString, WindowDesc, AppDelegate, Target, Env, DelegateCtx, FileDialogOptions, FileSpec, Command};
|
||||
use druid::commands;
|
||||
|
||||
use stiletto::{CanvasElement, VersionedCanvas, Canvas, DocumentSnapshot};
|
||||
|
||||
struct Delegate;
|
||||
|
||||
#[derive(Clone, Data)]
|
||||
struct CanvasData {
|
||||
current_element: Option<CanvasElement>,
|
||||
|
|
@ -89,7 +92,7 @@ impl Widget<CanvasData> for CanvasWidget {
|
|||
data.elements.update(move |canvas: &Canvas| -> Canvas {
|
||||
let mut new_canvas = canvas.clone();
|
||||
new_canvas.push_back(current_element);
|
||||
return new_canvas;
|
||||
new_canvas
|
||||
});
|
||||
|
||||
}
|
||||
|
|
@ -173,38 +176,38 @@ fn build_ui() -> impl Widget<CanvasData> {
|
|||
)
|
||||
.with_child(Button::new("Redo").on_click(|_ctx: &mut EventCtx, data: &mut CanvasData, _env: &Env| data.perform_redo()));
|
||||
|
||||
let stlt = FileSpec::new("Stiletto notebook", &["stlt"]);
|
||||
let save_dialog_options = FileDialogOptions::new()
|
||||
.allowed_types(vec![stlt])
|
||||
.default_type(stlt);
|
||||
let open_dialog_options = save_dialog_options.clone();
|
||||
|
||||
let save_buttons = Flex::row()
|
||||
.cross_axis_alignment(CrossAxisAlignment::Center)
|
||||
.with_child(
|
||||
Button::new("Open")
|
||||
.on_click(|_ctx, data: &mut CanvasData, _env| {
|
||||
use std::fs::File;
|
||||
|
||||
if let Ok(f) = File::open("test.stlt") {
|
||||
let res_snapshot: Result<DocumentSnapshot, serde_json::Error> = serde_json::from_reader(f);
|
||||
if let Ok(document_snapshot) = res_snapshot {
|
||||
data.set_from_snapshot(document_snapshot);
|
||||
} else {
|
||||
warn!("didn't work: {:?}", res_snapshot.err());
|
||||
}
|
||||
}
|
||||
.on_click(move |ctx, _, _| {
|
||||
ctx.submit_command(
|
||||
Command::new(
|
||||
druid::commands::SHOW_OPEN_PANEL,
|
||||
open_dialog_options.clone(),
|
||||
),
|
||||
None,
|
||||
)
|
||||
}))
|
||||
.with_child(
|
||||
Button::new("Save")
|
||||
.on_click(|_ctx, data: &mut CanvasData, _env| {
|
||||
use std::fs::File;
|
||||
|
||||
let res_file = File::create("test.stlt");
|
||||
if let Ok(f) = res_file {
|
||||
let write_res = serde_json::to_writer_pretty(f, &data.get_document_snapshot());
|
||||
if write_res.is_err() {
|
||||
warn!("Error while saving: {:?}", write_res.err());
|
||||
}
|
||||
} else {
|
||||
warn!("Cannot create file: {:?}", res_file.err());
|
||||
}
|
||||
.on_click(move |ctx, _, _| {
|
||||
ctx.submit_command(
|
||||
Command::new(
|
||||
druid::commands::SHOW_SAVE_PANEL,
|
||||
save_dialog_options.clone(),
|
||||
),
|
||||
None,
|
||||
)
|
||||
}));
|
||||
|
||||
|
||||
let toolbar = Flex::row()
|
||||
.cross_axis_alignment(CrossAxisAlignment::Center)
|
||||
.with_spacer(30.0)
|
||||
|
|
@ -231,6 +234,48 @@ pub fn main() {
|
|||
};
|
||||
AppLauncher::with_window(window)
|
||||
.use_simple_logger()
|
||||
.delegate(Delegate)
|
||||
.launch(canvas_data)
|
||||
.expect("launch failed");
|
||||
}
|
||||
|
||||
impl AppDelegate<CanvasData> for Delegate {
|
||||
fn command(
|
||||
&mut self,
|
||||
_ctx: &mut DelegateCtx,
|
||||
_target: Target,
|
||||
cmd: &Command,
|
||||
data: &mut CanvasData,
|
||||
_env: &Env,
|
||||
) -> bool {
|
||||
use std::fs::File;
|
||||
|
||||
if let Some(Some(file_info)) = cmd.get(commands::SAVE_FILE) {
|
||||
let res_file = File::create(file_info.path());
|
||||
if let Ok(f) = res_file {
|
||||
let write_res = serde_json::to_writer_pretty(f, &data.get_document_snapshot());
|
||||
if write_res.is_err() {
|
||||
warn!("Error while saving: {:?}", write_res.err());
|
||||
} else {
|
||||
info!("Written to file: {}", file_info.path().display());
|
||||
}
|
||||
} else {
|
||||
warn!("Cannot create file: {:?}", res_file.err());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if let Some(file_info) = cmd.get(commands::OPEN_FILE) {
|
||||
if let Ok(f) = File::open(file_info.path()) {
|
||||
let res_snapshot: Result<DocumentSnapshot, serde_json::Error> = serde_json::from_reader(f);
|
||||
if let Ok(document_snapshot) = res_snapshot {
|
||||
data.set_from_snapshot(document_snapshot);
|
||||
info!("Loaded file {}", file_info.path().display());
|
||||
} else {
|
||||
warn!("didn't work: {:?}", res_snapshot.err());
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
false
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue