mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-20 11:28:30 +08:00
Multi-document management (create new document; tab switching) (#210)
* Multi-docs WIP * Change to number * Add new document and switch documents * Remove keybind for previous document. Change keybind for next document. * Switch documents by clicking tabs * Remove keybind for previous document. Change keybind for next document. * multi-docs * Update package-lock.json * Hook up File>New to add new document * Remove console logs and empty lines. Start new documents from 2 instead of 1. * Fix formatting
This commit is contained in:
@@ -14,7 +14,17 @@ impl Default for Document {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
document: InteralDocument::default(),
|
||||
name: String::from("Unnamed Document"),
|
||||
name: String::from("Untitled Document"),
|
||||
layer_data: vec![(vec![], LayerData { selected: false, expanded: true })].into_iter().collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Document {
|
||||
pub fn with_name(name: String) -> Self {
|
||||
Self {
|
||||
document: InteralDocument::default(),
|
||||
name,
|
||||
layer_data: vec![(vec![], LayerData { selected: false, expanded: true })].into_iter().collect(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,9 @@ pub enum DocumentMessage {
|
||||
ToggleLayerVisibility(Vec<LayerId>),
|
||||
ToggleLayerExpansion(Vec<LayerId>),
|
||||
SelectDocument(usize),
|
||||
NewDocument,
|
||||
NextDocument,
|
||||
PrevDocument,
|
||||
ExportDocument,
|
||||
RenderDocument,
|
||||
Undo,
|
||||
@@ -85,6 +88,51 @@ impl MessageHandler<DocumentMessage, ()> for DocumentMessageHandler {
|
||||
SelectDocument(id) => {
|
||||
assert!(id < self.documents.len(), "Tried to select a document that was not initialized");
|
||||
self.active_document = id;
|
||||
responses.push_back(FrontendMessage::SetActiveDocument { document_index: self.active_document }.into());
|
||||
responses.push_back(
|
||||
FrontendMessage::UpdateCanvas {
|
||||
document: self.active_document_mut().document.render_root(),
|
||||
}
|
||||
.into(),
|
||||
);
|
||||
}
|
||||
NewDocument => {
|
||||
self.active_document = self.documents.len();
|
||||
let new_document = Document::with_name(format!("Untitled Document {}", self.active_document + 1));
|
||||
self.documents.push(new_document);
|
||||
responses.push_back(
|
||||
FrontendMessage::NewDocument {
|
||||
document_name: self.active_document().name.clone(),
|
||||
}
|
||||
.into(),
|
||||
);
|
||||
responses.push_back(FrontendMessage::SetActiveDocument { document_index: self.active_document }.into());
|
||||
responses.push_back(
|
||||
FrontendMessage::UpdateCanvas {
|
||||
document: self.active_document_mut().document.render_root(),
|
||||
}
|
||||
.into(),
|
||||
);
|
||||
}
|
||||
NextDocument => {
|
||||
self.active_document = (self.active_document + 1) % self.documents.len();
|
||||
responses.push_back(FrontendMessage::SetActiveDocument { document_index: self.active_document }.into());
|
||||
responses.push_back(
|
||||
FrontendMessage::UpdateCanvas {
|
||||
document: self.active_document_mut().document.render_root(),
|
||||
}
|
||||
.into(),
|
||||
);
|
||||
}
|
||||
PrevDocument => {
|
||||
self.active_document = (self.active_document + self.documents.len() - 1) % self.documents.len();
|
||||
responses.push_back(FrontendMessage::SetActiveDocument { document_index: self.active_document }.into());
|
||||
responses.push_back(
|
||||
FrontendMessage::UpdateCanvas {
|
||||
document: self.active_document_mut().document.render_root(),
|
||||
}
|
||||
.into(),
|
||||
);
|
||||
}
|
||||
ExportDocument => responses.push_back(
|
||||
FrontendMessage::ExportDocument {
|
||||
@@ -160,9 +208,9 @@ impl MessageHandler<DocumentMessage, ()> for DocumentMessageHandler {
|
||||
}
|
||||
fn actions(&self) -> ActionList {
|
||||
if self.active_document().layer_data.values().any(|data| data.selected) {
|
||||
actions!(DocumentMessageDiscriminant; Undo, DeleteSelectedLayers, RenderDocument, ExportDocument)
|
||||
actions!(DocumentMessageDiscriminant; Undo, DeleteSelectedLayers, RenderDocument, ExportDocument, NewDocument, NextDocument, PrevDocument)
|
||||
} else {
|
||||
actions!(DocumentMessageDiscriminant; Undo, RenderDocument, ExportDocument)
|
||||
actions!(DocumentMessageDiscriminant; Undo, RenderDocument, ExportDocument, NewDocument, NextDocument, PrevDocument)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user