From 375de393d893aee69fa8d5d70d3d67b555d66c20 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Thu, 15 Feb 2024 12:21:43 -0700 Subject: [PATCH] Implement close project, fixes #126 --- justfile | 4 ++++ src/main.rs | 37 +++++++++++++++++++++++++++++++------ src/menu.rs | 24 +++++++++++++++++------- 3 files changed, 52 insertions(+), 13 deletions(-) diff --git a/justfile b/justfile index 76b58ab..d68c259 100644 --- a/justfile +++ b/justfile @@ -43,6 +43,10 @@ check *args: # Runs a clippy check with JSON message format check-json: (check '--message-format=json') +dev *args: + cargo fmt + just run {{args}} + # Run with debug logs run *args: env RUST_LOG=cosmic_edit=info RUST_BACKTRACE=full cargo run --release {{args}} diff --git a/src/main.rs b/src/main.rs index cadd977..f991e7f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -165,7 +165,7 @@ fn main() -> Result<(), Box> { pub enum Action { Todo, CloseFile, - CloseProject, + CloseProject(usize), Copy, Cut, Find, @@ -206,7 +206,7 @@ impl Action { match self { Self::Todo => Message::Todo, Self::CloseFile => Message::CloseFile, - Self::CloseProject => Message::CloseProject, + Self::CloseProject(project_i) => Message::CloseProject(*project_i), Self::Copy => Message::Copy, Self::Cut => Message::Cut, Self::Find => Message::Find(Some(false)), @@ -275,7 +275,7 @@ pub enum Message { AppTheme(AppTheme), Config(Config), CloseFile, - CloseProject, + CloseProject(usize), Copy, Cut, DefaultFont(usize), @@ -1255,8 +1255,33 @@ impl Application for App { Message::CloseFile => { return self.update(Message::TabClose(self.tab_model.active())); } - Message::CloseProject => { - log::info!("TODO"); + Message::CloseProject(project_i) => { + 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::(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 => { if let Some(Tab::Editor(tab)) = self.active_tab() { @@ -1935,7 +1960,7 @@ impl Application for App { } fn header_start(&self) -> Vec> { - vec![menu_bar(&self.config, &self.key_binds)] + vec![menu_bar(&self.config, &self.key_binds, &self.projects)] } fn view(&self) -> Element { diff --git a/src/menu.rs b/src/menu.rs index d09e4a0..3447365 100644 --- a/src/menu.rs +++ b/src/menu.rs @@ -15,7 +15,7 @@ use cosmic::{ }, Element, }; -use std::collections::HashMap; +use std::{collections::HashMap, path::PathBuf}; use crate::{fl, icon_cache_get, Action, Config, KeyBind, Message}; @@ -84,7 +84,11 @@ pub fn context_menu<'a>( .into() } -pub fn menu_bar<'a>(config: &Config, key_binds: &HashMap) -> Element<'a, Message> { +pub fn menu_bar<'a>( + config: &Config, + key_binds: &HashMap, + projects: &Vec<(String, PathBuf)>, +) -> Element<'a, Message> { //TODO: port to libcosmic let menu_root = |label| { widget::button(widget::text(label)) @@ -96,14 +100,15 @@ pub fn menu_bar<'a>(config: &Config, key_binds: &HashMap) -> El |label| menu_button!(widget::text(label), horizontal_space(Length::Fill), ">"); let find_key = |action: &Action| -> String { - let mut key = String::new(); for (key_bind, key_action) in key_binds.iter() { if action == key_action { - key = key_bind.to_string(); - break; + return key_bind.to_string(); } } - key + if action == &Action::Todo { + return fl!("todo"); + } + String::new() }; let menu_item = |label, action| { @@ -153,6 +158,11 @@ pub fn menu_bar<'a>(config: &Config, key_binds: &HashMap) -> 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![ MenuTree::with_children( menu_root(fl!("file")), @@ -172,7 +182,7 @@ pub fn menu_bar<'a>(config: &Config, key_binds: &HashMap) -> El menu_folder(fl!("open-recent-project")), 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)), menu_item(fl!("save"), Action::Save), menu_key(fl!("save-as"), "Ctrl + Shift + S", Action::Todo),