@@ -6,7 +6,7 @@ use garfield::core::{ |
| 6 | 6 | trash_files, restore_from_trash, |
| 7 | 7 | }; |
| 8 | 8 | use garfield::ui::pane::SplitDirection; |
| 9 | | -use garfield::ui::{AddressBar, Breadcrumb, ConfirmDialog, ConflictAction, ConflictDialog, DialogResult, HelpModal, Pane, ProgressDialog, Sidebar, StatusBar, TabBar, TabInfo, Toolbar, ToolbarAction, ViewMode, TAB_BAR_HEIGHT, TOOLBAR_HEIGHT}; |
| 9 | +use garfield::ui::{AddressBar, Breadcrumb, ConfirmDialog, ConflictAction, ConflictDialog, ContextMenu, ContextMenuAction, ContextType, DialogResult, HelpModal, Pane, ProgressDialog, Sidebar, StatusBar, TabBar, TabInfo, Toolbar, ToolbarAction, ViewMode, TAB_BAR_HEIGHT, TOOLBAR_HEIGHT}; |
| 10 | 10 | use anyhow::Result; |
| 11 | 11 | use gartk_core::{InputEvent, Key, MouseButton, Point, Rect, Theme}; |
| 12 | 12 | use gartk_render::{Renderer, Surface, TextStyle}; |
@@ -80,6 +80,8 @@ pub struct App { |
| 80 | 80 | conflict_dialog: ConflictDialog, |
| 81 | 81 | /// Progress dialog for long operations. |
| 82 | 82 | progress_dialog: ProgressDialog, |
| 83 | + /// Context menu for right-click actions. |
| 84 | + context_menu: ContextMenu, |
| 83 | 85 | /// Paths pending delete confirmation. |
| 84 | 86 | pending_delete_paths: Vec<PathBuf>, |
| 85 | 87 | /// Undo/redo stack for file operations. |
@@ -196,6 +198,9 @@ impl App { |
| 196 | 198 | // Create progress dialog (full window bounds) |
| 197 | 199 | let progress_dialog = ProgressDialog::new(Rect::new(0, 0, width, height)); |
| 198 | 200 | |
| 201 | + // Create context menu (full window bounds for positioning) |
| 202 | + let context_menu = ContextMenu::new(Rect::new(0, 0, width, height)); |
| 203 | + |
| 199 | 204 | // Content area bounds (for panes) |
| 200 | 205 | let content_bounds = Rect::new( |
| 201 | 206 | sidebar_w as i32, |
@@ -244,6 +249,7 @@ impl App { |
| 244 | 249 | confirm_dialog, |
| 245 | 250 | conflict_dialog, |
| 246 | 251 | progress_dialog, |
| 252 | + context_menu, |
| 247 | 253 | pending_delete_paths: Vec::new(), |
| 248 | 254 | undo_stack: UndoStack::new(), |
| 249 | 255 | pending_paste: None, |
@@ -358,6 +364,20 @@ impl App { |
| 358 | 364 | return; |
| 359 | 365 | } |
| 360 | 366 | |
| 367 | + // Check context menu |
| 368 | + if self.context_menu.is_visible() { |
| 369 | + if let Some(action) = self.context_menu.on_click(pos) { |
| 370 | + self.handle_context_menu_action(action); |
| 371 | + } |
| 372 | + return; |
| 373 | + } |
| 374 | + |
| 375 | + // Right-click shows context menu |
| 376 | + if button == Some(MouseButton::Right) { |
| 377 | + self.show_context_menu(pos); |
| 378 | + return; |
| 379 | + } |
| 380 | + |
| 361 | 381 | // Handle middle-click on tab bar to close tab |
| 362 | 382 | if button == Some(MouseButton::Middle) { |
| 363 | 383 | if let Some(index) = self.tab_bar.tab_at_point(pos) { |
@@ -592,6 +612,12 @@ impl App { |
| 592 | 612 | return; |
| 593 | 613 | } |
| 594 | 614 | |
| 615 | + // Handle context menu hover |
| 616 | + if self.context_menu.is_visible() { |
| 617 | + self.context_menu.on_mouse_move(pos); |
| 618 | + return; |
| 619 | + } |
| 620 | + |
| 595 | 621 | // Handle sidebar resize in progress |
| 596 | 622 | if self.sidebar_resizing { |
| 597 | 623 | let new_width = (pos.x - self.sidebar.bounds().x).max(0) as u32; |
@@ -695,6 +721,14 @@ impl App { |
| 695 | 721 | return; |
| 696 | 722 | } |
| 697 | 723 | |
| 724 | + // Handle context menu when visible |
| 725 | + if self.context_menu.is_visible() { |
| 726 | + if let Some(action) = self.context_menu.handle_key(key) { |
| 727 | + self.handle_context_menu_action(action); |
| 728 | + } |
| 729 | + return; |
| 730 | + } |
| 731 | + |
| 698 | 732 | // F1 toggles help |
| 699 | 733 | if *key == Key::F1 { |
| 700 | 734 | self.help_modal.show(); |
@@ -764,13 +798,21 @@ impl App { |
| 764 | 798 | } |
| 765 | 799 | } |
| 766 | 800 | |
| 767 | | - // Ctrl+Shift keybinds (splits, new folder) |
| 801 | + // Ctrl+Shift keybinds (splits, new folder, new file, duplicate) |
| 768 | 802 | if modifiers.ctrl && modifiers.shift { |
| 769 | 803 | match key { |
| 770 | 804 | Key::Char('n') | Key::Char('N') => { |
| 771 | 805 | self.create_new_folder(); |
| 772 | 806 | return; |
| 773 | 807 | } |
| 808 | + Key::Char('f') | Key::Char('F') => { |
| 809 | + self.create_new_file(); |
| 810 | + return; |
| 811 | + } |
| 812 | + Key::Char('d') | Key::Char('D') => { |
| 813 | + self.duplicate_selected(); |
| 814 | + return; |
| 815 | + } |
| 774 | 816 | Key::Char('h') | Key::Char('H') => { |
| 775 | 817 | self.split_horizontal(); |
| 776 | 818 | return; |
@@ -1699,6 +1741,122 @@ impl App { |
| 1699 | 1741 | let _ = self.window.connection().flush(); |
| 1700 | 1742 | } |
| 1701 | 1743 | |
| 1744 | + /// Show the context menu at the given position. |
| 1745 | + fn show_context_menu(&mut self, pos: Point) { |
| 1746 | + // Determine context type based on what's under the cursor |
| 1747 | + let (context_type, selected_count) = if let Some(pane) = self.focused_pane() { |
| 1748 | + if let Some(tab) = pane.active_tab() { |
| 1749 | + let selected = tab.selected_paths(); |
| 1750 | + |
| 1751 | + if let Some(entry) = tab.entry_at_point(pos) { |
| 1752 | + // Clicked on an item |
| 1753 | + if selected.len() > 1 && selected.contains(&entry.path) { |
| 1754 | + (ContextType::MultiSelection, selected.len()) |
| 1755 | + } else if entry.is_dir() { |
| 1756 | + (ContextType::Folder, 1) |
| 1757 | + } else { |
| 1758 | + (ContextType::File, 1) |
| 1759 | + } |
| 1760 | + } else { |
| 1761 | + // Clicked on empty space |
| 1762 | + (ContextType::EmptySpace, 0) |
| 1763 | + } |
| 1764 | + } else { |
| 1765 | + (ContextType::EmptySpace, 0) |
| 1766 | + } |
| 1767 | + } else { |
| 1768 | + (ContextType::EmptySpace, 0) |
| 1769 | + }; |
| 1770 | + |
| 1771 | + let has_clipboard = self.clipboard.has_files(); |
| 1772 | + self.context_menu.show(pos, context_type, selected_count, has_clipboard); |
| 1773 | + } |
| 1774 | + |
| 1775 | + /// Handle a context menu action. |
| 1776 | + fn handle_context_menu_action(&mut self, action: ContextMenuAction) { |
| 1777 | + match action { |
| 1778 | + ContextMenuAction::Open => self.enter_selected(), |
| 1779 | + ContextMenuAction::OpenWith(app) => self.open_with(&app), |
| 1780 | + ContextMenuAction::OpenInNewTab => self.open_in_new_tab(), |
| 1781 | + ContextMenuAction::Copy | ContextMenuAction::CopyAll => self.copy_selected(), |
| 1782 | + ContextMenuAction::Cut | ContextMenuAction::CutAll => self.cut_selected(), |
| 1783 | + ContextMenuAction::Duplicate => self.duplicate_selected(), |
| 1784 | + ContextMenuAction::Rename => self.start_rename(), |
| 1785 | + ContextMenuAction::Trash | ContextMenuAction::TrashAll => self.trash_selected(), |
| 1786 | + ContextMenuAction::Delete | ContextMenuAction::DeleteAll => self.delete_selected_permanently(), |
| 1787 | + ContextMenuAction::Properties => self.show_properties(), |
| 1788 | + ContextMenuAction::NewFile => self.create_new_file(), |
| 1789 | + ContextMenuAction::NewFolder => self.create_new_folder(), |
| 1790 | + ContextMenuAction::Paste => self.paste(), |
| 1791 | + ContextMenuAction::Refresh => self.refresh(), |
| 1792 | + ContextMenuAction::ViewList => self.set_view_mode(ViewMode::List), |
| 1793 | + ContextMenuAction::ViewGrid => self.set_view_mode(ViewMode::Grid), |
| 1794 | + ContextMenuAction::ViewColumns => self.set_view_mode(ViewMode::Columns), |
| 1795 | + ContextMenuAction::SortByName => self.set_sort_order(garfield::core::SortOrder::Name), |
| 1796 | + ContextMenuAction::SortBySize => self.set_sort_order(garfield::core::SortOrder::Size), |
| 1797 | + ContextMenuAction::SortByDate => self.set_sort_order(garfield::core::SortOrder::Modified), |
| 1798 | + ContextMenuAction::SortByType => self.set_sort_order(garfield::core::SortOrder::Type), |
| 1799 | + } |
| 1800 | + } |
| 1801 | + |
| 1802 | + /// Open selected item with a specific application. |
| 1803 | + fn open_with(&mut self, app: &str) { |
| 1804 | + if app.is_empty() { |
| 1805 | + self.status_bar.set_status_message("Application picker not implemented"); |
| 1806 | + return; |
| 1807 | + } |
| 1808 | + |
| 1809 | + let paths = self.get_selected_paths(); |
| 1810 | + if let Some(path) = paths.first() { |
| 1811 | + match std::process::Command::new(app).arg(path).spawn() { |
| 1812 | + Ok(_) => self.status_bar.set_status_message(format!("Opened with {}", app)), |
| 1813 | + Err(e) => self.status_bar.set_status_message(format!("Failed: {}", e)), |
| 1814 | + } |
| 1815 | + } |
| 1816 | + } |
| 1817 | + |
| 1818 | + /// Open folder in new tab. |
| 1819 | + fn open_in_new_tab(&mut self) { |
| 1820 | + let entry_path = self.focused_pane() |
| 1821 | + .and_then(|p| p.active_tab()) |
| 1822 | + .and_then(|t| t.selected_entry()) |
| 1823 | + .filter(|e| e.is_dir()) |
| 1824 | + .map(|e| e.path.clone()); |
| 1825 | + |
| 1826 | + if let Some(path) = entry_path { |
| 1827 | + if let Some(pane) = self.focused_pane_mut() { |
| 1828 | + pane.add_tab(path); |
| 1829 | + } |
| 1830 | + self.sync_tab_bar(); |
| 1831 | + self.sync_breadcrumb(); |
| 1832 | + self.update_status_bar(); |
| 1833 | + } |
| 1834 | + } |
| 1835 | + |
| 1836 | + /// Show properties dialog (placeholder). |
| 1837 | + fn show_properties(&mut self) { |
| 1838 | + self.status_bar.set_status_message("Properties dialog not implemented"); |
| 1839 | + } |
| 1840 | + |
| 1841 | + /// Set sort order from context menu. |
| 1842 | + fn set_sort_order(&mut self, order: garfield::core::SortOrder) { |
| 1843 | + if let Some(pane) = self.focused_pane_mut() { |
| 1844 | + if let Some(tab) = pane.active_tab_mut() { |
| 1845 | + let current_dir = tab.sort_direction(); |
| 1846 | + // Toggle direction if same order |
| 1847 | + let new_dir = if tab.sort_order() == order { |
| 1848 | + match current_dir { |
| 1849 | + garfield::core::SortDirection::Ascending => garfield::core::SortDirection::Descending, |
| 1850 | + garfield::core::SortDirection::Descending => garfield::core::SortDirection::Ascending, |
| 1851 | + } |
| 1852 | + } else { |
| 1853 | + garfield::core::SortDirection::Ascending |
| 1854 | + }; |
| 1855 | + tab.set_sort(order, new_dir); |
| 1856 | + } |
| 1857 | + } |
| 1858 | + } |
| 1859 | + |
| 1702 | 1860 | /// Create a new folder in the current directory. |
| 1703 | 1861 | fn create_new_folder(&mut self) { |
| 1704 | 1862 | let current_dir = self.focused_pane() |
@@ -1732,6 +1890,106 @@ impl App { |
| 1732 | 1890 | } |
| 1733 | 1891 | } |
| 1734 | 1892 | |
| 1893 | + /// Create a new empty file in the current directory. |
| 1894 | + fn create_new_file(&mut self) { |
| 1895 | + use garfield::core::make_unique_name; |
| 1896 | + |
| 1897 | + let current_dir = self.focused_pane() |
| 1898 | + .and_then(|p| p.active_tab()) |
| 1899 | + .map(|t| t.current_path().clone()); |
| 1900 | + |
| 1901 | + let current_dir = match current_dir { |
| 1902 | + Some(d) => d, |
| 1903 | + None => return, |
| 1904 | + }; |
| 1905 | + |
| 1906 | + // Generate unique name |
| 1907 | + let unique_name = make_unique_name(¤t_dir, "New File"); |
| 1908 | + let path = current_dir.join(&unique_name); |
| 1909 | + |
| 1910 | + match std::fs::File::create(&path) { |
| 1911 | + Ok(_) => { |
| 1912 | + self.status_bar.set_status_message(format!("Created '{}'", unique_name)); |
| 1913 | + self.refresh(); |
| 1914 | + |
| 1915 | + // Select the new file and start rename |
| 1916 | + if let Some(pane) = self.focused_pane_mut() { |
| 1917 | + if let Some(tab) = pane.active_tab_mut() { |
| 1918 | + if tab.select_by_name(&unique_name) { |
| 1919 | + tab.start_rename(); |
| 1920 | + } |
| 1921 | + } |
| 1922 | + } |
| 1923 | + } |
| 1924 | + Err(e) => { |
| 1925 | + self.status_bar.set_status_message(format!("Failed to create file: {}", e)); |
| 1926 | + } |
| 1927 | + } |
| 1928 | + } |
| 1929 | + |
| 1930 | + /// Duplicate the selected files/folders in the current directory. |
| 1931 | + fn duplicate_selected(&mut self) { |
| 1932 | + use garfield::core::make_unique_name; |
| 1933 | + |
| 1934 | + let selected = self.get_selected_paths(); |
| 1935 | + if selected.is_empty() { |
| 1936 | + self.status_bar.set_status_message("No items selected"); |
| 1937 | + return; |
| 1938 | + } |
| 1939 | + |
| 1940 | + let dest_dir = self.focused_pane() |
| 1941 | + .and_then(|p| p.active_tab()) |
| 1942 | + .map(|t| t.current_path().clone()); |
| 1943 | + |
| 1944 | + let dest_dir = match dest_dir { |
| 1945 | + Some(d) => d, |
| 1946 | + None => return, |
| 1947 | + }; |
| 1948 | + |
| 1949 | + let mut success_count = 0; |
| 1950 | + let mut last_created_name = String::new(); |
| 1951 | + |
| 1952 | + for path in &selected { |
| 1953 | + if let Some(name) = path.file_name() { |
| 1954 | + let name_str = name.to_string_lossy(); |
| 1955 | + let unique_name = make_unique_name(&dest_dir, &name_str); |
| 1956 | + let dest = dest_dir.join(&unique_name); |
| 1957 | + |
| 1958 | + let result = if path.is_dir() { |
| 1959 | + garfield::core::copy_to_path(path, &dest) |
| 1960 | + } else { |
| 1961 | + std::fs::copy(path, &dest).map(|_| dest.clone()) |
| 1962 | + }; |
| 1963 | + |
| 1964 | + if result.is_ok() { |
| 1965 | + success_count += 1; |
| 1966 | + last_created_name = unique_name; |
| 1967 | + } |
| 1968 | + } |
| 1969 | + } |
| 1970 | + |
| 1971 | + if success_count > 0 { |
| 1972 | + let msg = if success_count == 1 { |
| 1973 | + format!("Duplicated as '{}'", last_created_name) |
| 1974 | + } else { |
| 1975 | + format!("Duplicated {} items", success_count) |
| 1976 | + }; |
| 1977 | + self.status_bar.set_status_message(msg); |
| 1978 | + self.refresh(); |
| 1979 | + |
| 1980 | + // Select the last duplicated item |
| 1981 | + if !last_created_name.is_empty() { |
| 1982 | + if let Some(pane) = self.focused_pane_mut() { |
| 1983 | + if let Some(tab) = pane.active_tab_mut() { |
| 1984 | + tab.select_by_name(&last_created_name); |
| 1985 | + } |
| 1986 | + } |
| 1987 | + } |
| 1988 | + } else { |
| 1989 | + self.status_bar.set_status_message("Failed to duplicate items"); |
| 1990 | + } |
| 1991 | + } |
| 1992 | + |
| 1735 | 1993 | /// Undo the last file operation. |
| 1736 | 1994 | fn undo(&mut self) { |
| 1737 | 1995 | let op = match self.undo_stack.pop_undo() { |
@@ -2087,6 +2345,7 @@ impl App { |
| 2087 | 2345 | self.confirm_dialog.set_bounds(Rect::new(0, 0, width, height)); |
| 2088 | 2346 | self.conflict_dialog.set_bounds(Rect::new(0, 0, width, height)); |
| 2089 | 2347 | self.progress_dialog.set_bounds(Rect::new(0, 0, width, height)); |
| 2348 | + self.context_menu.set_bounds(Rect::new(0, 0, width, height)); |
| 2090 | 2349 | } |
| 2091 | 2350 | |
| 2092 | 2351 | /// Render the application. |
@@ -2156,6 +2415,9 @@ impl App { |
| 2156 | 2415 | // Draw conflict dialog overlay (on top of everything) |
| 2157 | 2416 | self.conflict_dialog.render(&self.renderer)?; |
| 2158 | 2417 | |
| 2418 | + // Draw context menu overlay |
| 2419 | + self.context_menu.render(&self.renderer)?; |
| 2420 | + |
| 2159 | 2421 | // Draw progress dialog overlay (on top of everything) |
| 2160 | 2422 | self.progress_dialog.render(&self.renderer)?; |
| 2161 | 2423 | |