Implement close project, fixes #126

This commit is contained in:
Jeremy Soller 2024-02-15 12:21:43 -07:00
parent 83637bbfd2
commit 375de393d8
No known key found for this signature in database
GPG key ID: D02FD439211AF56F
3 changed files with 52 additions and 13 deletions

View file

@ -43,6 +43,10 @@ check *args:
# Runs a clippy check with JSON message format # Runs a clippy check with JSON message format
check-json: (check '--message-format=json') check-json: (check '--message-format=json')
dev *args:
cargo fmt
just run {{args}}
# Run with debug logs # Run with debug logs
run *args: run *args:
env RUST_LOG=cosmic_edit=info RUST_BACKTRACE=full cargo run --release {{args}} env RUST_LOG=cosmic_edit=info RUST_BACKTRACE=full cargo run --release {{args}}

View file

@ -165,7 +165,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
pub enum Action { pub enum Action {
Todo, Todo,
CloseFile, CloseFile,
CloseProject, CloseProject(usize),
Copy, Copy,
Cut, Cut,
Find, Find,
@ -206,7 +206,7 @@ impl Action {
match self { match self {
Self::Todo => Message::Todo, Self::Todo => Message::Todo,
Self::CloseFile => Message::CloseFile, Self::CloseFile => Message::CloseFile,
Self::CloseProject => Message::CloseProject, Self::CloseProject(project_i) => Message::CloseProject(*project_i),
Self::Copy => Message::Copy, Self::Copy => Message::Copy,
Self::Cut => Message::Cut, Self::Cut => Message::Cut,
Self::Find => Message::Find(Some(false)), Self::Find => Message::Find(Some(false)),
@ -275,7 +275,7 @@ pub enum Message {
AppTheme(AppTheme), AppTheme(AppTheme),
Config(Config), Config(Config),
CloseFile, CloseFile,
CloseProject, CloseProject(usize),
Copy, Copy,
Cut, Cut,
DefaultFont(usize), DefaultFont(usize),
@ -1255,8 +1255,33 @@ impl Application for App {
Message::CloseFile => { Message::CloseFile => {
return self.update(Message::TabClose(self.tab_model.active())); return self.update(Message::TabClose(self.tab_model.active()));
} }
Message::CloseProject => { Message::CloseProject(project_i) => {
log::info!("TODO"); if project_i < self.projects.len() {
let (_project_name, project_path) = self.projects.remove(project_i);
let mut position = 0;
let mut closing = false;
while let Some(id) = self.nav_model.entity_at(position) {
match self.nav_model.data::<ProjectNode>(id) {
Some(node) => {
if let ProjectNode::Folder { path, root, .. } = node {
if path == &project_path {
// Found the project root node, closing
closing = true;
} else if *root && closing {
// Found another project root node after closing, breaking
break;
}
}
}
None => break,
}
if closing {
self.nav_model.remove(id);
} else {
position += 1;
}
}
}
} }
Message::Copy => { Message::Copy => {
if let Some(Tab::Editor(tab)) = self.active_tab() { if let Some(Tab::Editor(tab)) = self.active_tab() {
@ -1935,7 +1960,7 @@ impl Application for App {
} }
fn header_start(&self) -> Vec<Element<Message>> { fn header_start(&self) -> Vec<Element<Message>> {
vec![menu_bar(&self.config, &self.key_binds)] vec![menu_bar(&self.config, &self.key_binds, &self.projects)]
} }
fn view(&self) -> Element<Message> { fn view(&self) -> Element<Message> {

View file

@ -15,7 +15,7 @@ use cosmic::{
}, },
Element, Element,
}; };
use std::collections::HashMap; use std::{collections::HashMap, path::PathBuf};
use crate::{fl, icon_cache_get, Action, Config, KeyBind, Message}; use crate::{fl, icon_cache_get, Action, Config, KeyBind, Message};
@ -84,7 +84,11 @@ pub fn context_menu<'a>(
.into() .into()
} }
pub fn menu_bar<'a>(config: &Config, key_binds: &HashMap<KeyBind, Action>) -> Element<'a, Message> { pub fn menu_bar<'a>(
config: &Config,
key_binds: &HashMap<KeyBind, Action>,
projects: &Vec<(String, PathBuf)>,
) -> Element<'a, Message> {
//TODO: port to libcosmic //TODO: port to libcosmic
let menu_root = |label| { let menu_root = |label| {
widget::button(widget::text(label)) widget::button(widget::text(label))
@ -96,14 +100,15 @@ pub fn menu_bar<'a>(config: &Config, key_binds: &HashMap<KeyBind, Action>) -> El
|label| menu_button!(widget::text(label), horizontal_space(Length::Fill), ">"); |label| menu_button!(widget::text(label), horizontal_space(Length::Fill), ">");
let find_key = |action: &Action| -> String { let find_key = |action: &Action| -> String {
let mut key = String::new();
for (key_bind, key_action) in key_binds.iter() { for (key_bind, key_action) in key_binds.iter() {
if action == key_action { if action == key_action {
key = key_bind.to_string(); return key_bind.to_string();
break;
} }
} }
key if action == &Action::Todo {
return fl!("todo");
}
String::new()
}; };
let menu_item = |label, action| { let menu_item = |label, action| {
@ -153,6 +158,11 @@ pub fn menu_bar<'a>(config: &Config, key_binds: &HashMap<KeyBind, Action>) -> El
) )
}; };
let mut close_projects = Vec::with_capacity(projects.len());
for (project_i, (name, _path)) in projects.iter().enumerate() {
close_projects.push(menu_item(name.clone(), Action::CloseProject(project_i)));
}
MenuBar::new(vec![ MenuBar::new(vec![
MenuTree::with_children( MenuTree::with_children(
menu_root(fl!("file")), menu_root(fl!("file")),
@ -172,7 +182,7 @@ pub fn menu_bar<'a>(config: &Config, key_binds: &HashMap<KeyBind, Action>) -> El
menu_folder(fl!("open-recent-project")), menu_folder(fl!("open-recent-project")),
vec![menu_item(fl!("todo"), Action::Todo)], vec![menu_item(fl!("todo"), Action::Todo)],
), ),
menu_item(fl!("close-project"), Action::CloseProject), MenuTree::with_children(menu_folder(fl!("close-project")), close_projects),
MenuTree::new(horizontal_rule(1)), MenuTree::new(horizontal_rule(1)),
menu_item(fl!("save"), Action::Save), menu_item(fl!("save"), Action::Save),
menu_key(fl!("save-as"), "Ctrl + Shift + S", Action::Todo), menu_key(fl!("save-as"), "Ctrl + Shift + S", Action::Todo),