Add selection toolbar and working delete action

This commit is contained in:
Enrico Lumetti 2021-03-05 11:39:24 +01:00
parent db713e6542
commit 70e5eb0c71
2 changed files with 42 additions and 1 deletions

View File

@ -120,6 +120,18 @@ fn build_ui() -> impl Widget<StilettoState> {
|_ctx: &mut EventCtx, data: &mut StilettoState, _env: &Env| data.canvas.perform_redo(), |_ctx: &mut EventCtx, data: &mut StilettoState, _env: &Env| data.canvas.perform_redo(),
)); ));
let selection_buttons = Flex::row()
.cross_axis_alignment(CrossAxisAlignment::Center)
.with_child(Button::new("Copy").on_click(
|_ctx: &mut EventCtx, data: &mut StilettoState, _env: &Env| {},
))
.with_child(Button::new("Cut").on_click(
|_ctx: &mut EventCtx, data: &mut StilettoState, _env: &Env| {},
))
.with_child(Button::new("Delete").on_click(
|_ctx: &mut EventCtx, data: &mut StilettoState, _env: &Env| data.canvas.delete_selected(),
));
let stlt = FileSpec::new("Stiletto notebook", &["stlt"]); let stlt = FileSpec::new("Stiletto notebook", &["stlt"]);
let save_dialog_options = FileDialogOptions::new() let save_dialog_options = FileDialogOptions::new()
.allowed_types(vec![stlt]) .allowed_types(vec![stlt])
@ -178,6 +190,8 @@ fn build_ui() -> impl Widget<StilettoState> {
.with_spacer(30.0) .with_spacer(30.0)
.with_flex_child(Align::left(history_buttons), 1.0) .with_flex_child(Align::left(history_buttons), 1.0)
.with_spacer(10.0) .with_spacer(10.0)
.with_flex_child(Align::left(selection_buttons), 1.0)
.with_spacer(10.0)
.with_flex_child(Align::left(tool_buttons), 2.0) .with_flex_child(Align::left(tool_buttons), 2.0)
.with_spacer(20.0) .with_spacer(20.0)
.with_flex_child(Align::right(save_buttons), 1.0) .with_flex_child(Align::right(save_buttons), 1.0)

View File

@ -17,7 +17,7 @@
use im::Vector; use im::Vector;
use super::tool_ctx::{CanvasToolCtx, CanvasToolState}; use super::tool_ctx::{CanvasToolCtx, CanvasToolState};
use crate::canvas::Canvas; use crate::canvas::{Canvas, CanvasElement};
use crate::tool::CanvasToolParams; use crate::tool::CanvasToolParams;
use crate::{DocumentSnapshot, VersionedCanvas}; use crate::{DocumentSnapshot, VersionedCanvas};
@ -79,6 +79,33 @@ impl CanvasState {
} }
} }
pub fn delete_selected(&mut self) {
let selected_elements = self.versioned_canvas.get().selected_elements();
if !selected_elements.is_empty() {
if !selected_elements.is_empty() {
let elements = self.versioned_canvas.get().elements();
// TODO(enrico): is this memory efficient?
// what is the difference between this and
// im::Vector::retain?
let new_elements = elements.iter().enumerate()
.filter_map(|(i, elem)| {
if !selected_elements.contains(&i) {
Some(elem)
} else {
None
}
})
.cloned()
.collect::<Vector<CanvasElement>>();
self.versioned_canvas.update(move |canvas: &mut Canvas| {
*canvas = Canvas::new_with_elements(new_elements);
});
}
}
}
/// Changes tool while mantaining the current CanvasToolCtx /// Changes tool while mantaining the current CanvasToolCtx
pub fn change_tool(&mut self, tool_params: CanvasToolParams) { pub fn change_tool(&mut self, tool_params: CanvasToolParams) {
self.tool_ctx.set_params(tool_params); self.tool_ctx.set_params(tool_params);