Skip to main content

fresh/app/
menu_context.rs

1//! Menu context computation.
2//!
3//! This module provides methods to compute menu context values that determine
4//! when menu items and commands should be enabled or disabled. Each context
5//! value has a dedicated method that encapsulates the logic for checking
6//! whether that feature is available.
7
8use super::Editor;
9use crate::view::ui::context_keys;
10
11impl Editor {
12    /// Return a clone of the current menu context (boolean state flags).
13    ///
14    /// This is used by the GUI layer to sync native menu item states
15    /// (enabled/disabled, checkmarks) without knowing about the editor's
16    /// internal state.
17    pub fn menu_context(&self) -> crate::view::ui::MenuContext {
18        self.menu_state.context.clone()
19    }
20
21    /// Return the fully-expanded menu definitions (with `DynamicSubmenu`
22    /// items resolved to `Submenu`).  Used by the GUI layer to build
23    /// platform-native menus.
24    pub fn expanded_menu_definitions(&self) -> Vec<fresh_core::menu::Menu> {
25        use crate::config::{MenuConfig, MenuExt};
26
27        let mut menus = MenuConfig::translated_menus();
28        let themes_dir = self.menu_state.themes_dir.clone();
29        for menu in &mut menus {
30            menu.expand_dynamic_items(&themes_dir);
31        }
32        menus
33    }
34
35    /// Update all menu context values based on current editor state.
36    /// This should be called before rendering the menu bar.
37    pub fn update_menu_context(&mut self) {
38        // Window-scoped lookups delegated to `Window`.
39        let line_numbers = self.active_window().is_line_numbers_visible();
40        let line_wrap = self.active_window().is_line_wrap_enabled();
41        let page_view = self.active_window().is_page_view();
42        let file_explorer_visible = self.file_explorer_visible();
43        let file_explorer_focused = self.active_window().is_file_explorer_focused();
44        let mouse_capture = self.active_window_mut().mouse_enabled;
45        let mouse_hover = self.config.editor.mouse_hover_enabled;
46        let inlay_hints = self.config.editor.enable_inlay_hints;
47        // True for any real buffer; false when the active buffer is the
48        // synthesized placeholder kept alive after a last-buffer close with
49        // `auto_create_empty_buffer_on_last_buffer_close` disabled.
50        let has_buffer = !self
51            .active_window()
52            .buffer_metadata
53            .get(&self.active_buffer())
54            .map(|m| m.synthetic_placeholder)
55            .unwrap_or(false);
56        let has_selection = has_buffer && self.has_active_selection();
57        let can_copy = has_selection
58            || file_explorer_focused
59            || self
60                .file_explorer()
61                .as_ref()
62                .map(|fe| fe.get_selected().is_some())
63                .unwrap_or(false);
64        // Paste is available in the explorer only when a file is in the clipboard,
65        // or in the editor only when no file is in the clipboard. There's no
66        // buffer to paste into in placeholder mode, so suppress it there.
67        let can_paste = if file_explorer_focused {
68            self.active_window().file_explorer_clipboard.is_some()
69        } else {
70            has_buffer && self.active_window().file_explorer_clipboard.is_none()
71        };
72        let menu_bar = self.active_window_mut().menu_bar_visible;
73        let vertical_scrollbar = self.config.editor.show_vertical_scrollbar;
74        let horizontal_scrollbar = self.config.editor.show_horizontal_scrollbar;
75
76        // File explorer state
77        let show_hidden = self.active_window().is_file_explorer_showing_hidden();
78        let show_gitignored = self.active_window().is_file_explorer_showing_gitignored();
79
80        // Language-dependent context values
81        let lsp_available = self.active_window().is_lsp_available();
82        let formatter_available = self.active_window().is_formatter_available();
83
84        // Session mode (for detach command availability)
85        let session_mode = self.session_mode;
86
87        // Scroll sync state
88        let scroll_sync = self.active_window().same_buffer_scroll_sync;
89        let has_same_buffer_splits = self.active_window().has_same_buffer_splits();
90
91        // Keybinding map state
92        let active_keymap: &str = &self.config.active_keybinding_map;
93
94        // Apply all context values
95        self.menu_state
96            .context
97            .set(context_keys::HAS_BUFFER, has_buffer)
98            .set(context_keys::KEYMAP_DEFAULT, active_keymap == "default")
99            .set(context_keys::KEYMAP_EMACS, active_keymap == "emacs")
100            .set(context_keys::KEYMAP_VSCODE, active_keymap == "vscode")
101            .set(context_keys::KEYMAP_MACOS_GUI, active_keymap == "macos-gui")
102            .set(context_keys::LINE_NUMBERS, line_numbers)
103            .set(context_keys::LINE_WRAP, line_wrap)
104            .set(context_keys::PAGE_VIEW, page_view)
105            // Keep backward-compatible key for existing keybindings/menus
106            .set(context_keys::COMPOSE_MODE, page_view)
107            .set(context_keys::FILE_EXPLORER, file_explorer_visible)
108            .set(context_keys::FILE_EXPLORER_FOCUSED, file_explorer_focused)
109            .set(context_keys::MOUSE_CAPTURE, mouse_capture)
110            .set(context_keys::MOUSE_HOVER, mouse_hover)
111            .set(context_keys::INLAY_HINTS, inlay_hints)
112            .set(context_keys::LSP_AVAILABLE, lsp_available)
113            .set(context_keys::FILE_EXPLORER_SHOW_HIDDEN, show_hidden)
114            .set(context_keys::FILE_EXPLORER_SHOW_GITIGNORED, show_gitignored)
115            .set(context_keys::HAS_SELECTION, has_selection)
116            .set(context_keys::CAN_COPY, can_copy)
117            .set(context_keys::CAN_PASTE, can_paste)
118            .set(context_keys::MENU_BAR, menu_bar)
119            .set(context_keys::FORMATTER_AVAILABLE, formatter_available)
120            .set(context_keys::SESSION_MODE, session_mode)
121            .set(context_keys::VERTICAL_SCROLLBAR, vertical_scrollbar)
122            .set(context_keys::HORIZONTAL_SCROLLBAR, horizontal_scrollbar)
123            .set(context_keys::SCROLL_SYNC, scroll_sync)
124            .set(context_keys::HAS_SAME_BUFFER_SPLITS, has_same_buffer_splits);
125    }
126}
127
128impl crate::app::window::Window {
129    /// Check if line numbers are visible in the active split.
130    pub(crate) fn is_line_numbers_visible(&self) -> bool {
131        let (mgr, vs) = self
132            .buffers
133            .splits()
134            .expect("active window must have a populated split layout");
135        vs.get(&mgr.active_split())
136            .map(|vs| vs.show_line_numbers)
137            .unwrap_or(true)
138    }
139
140    /// Check if line wrap is enabled in the active split.
141    pub(crate) fn is_line_wrap_enabled(&self) -> bool {
142        let (mgr, vs) = self
143            .buffers
144            .splits()
145            .expect("active window must have a populated split layout");
146        vs.get(&mgr.active_split())
147            .map(|vs| vs.viewport.line_wrap_enabled)
148            .unwrap_or(false)
149    }
150
151    /// Check if compose mode is active in the current buffer.
152    pub(crate) fn is_page_view(&self) -> bool {
153        let (mgr, vs) = self
154            .buffers
155            .splits()
156            .expect("active window must have a populated split layout");
157        vs.get(&mgr.active_split())
158            .map(|vs| vs.view_mode == crate::state::ViewMode::PageView)
159            .unwrap_or(false)
160    }
161
162    /// Check if the file explorer is currently focused.
163    pub(crate) fn is_file_explorer_focused(&self) -> bool {
164        self.key_context == crate::input::keybindings::KeyContext::FileExplorer
165    }
166
167    /// Check if the file explorer is showing hidden files.
168    pub(crate) fn is_file_explorer_showing_hidden(&self) -> bool {
169        self.file_explorer
170            .as_ref()
171            .map(|fe| fe.ignore_patterns().show_hidden())
172            .unwrap_or(false)
173    }
174
175    /// Check if the file explorer is showing gitignored files.
176    pub(crate) fn is_file_explorer_showing_gitignored(&self) -> bool {
177        self.file_explorer
178            .as_ref()
179            .map(|fe| fe.ignore_patterns().show_gitignored())
180            .unwrap_or(false)
181    }
182
183    /// Check if an LSP server is available and ready for the current buffer's language.
184    pub(crate) fn is_lsp_available(&self) -> bool {
185        let buffer_id = self.active_buffer();
186
187        // Check if LSP is enabled for this buffer
188        if let Some(metadata) = self.buffer_metadata.get(&buffer_id) {
189            if !metadata.lsp_enabled {
190                return false;
191            }
192        } else {
193            return false;
194        }
195
196        // Use buffer's stored language
197        self.buffers
198            .get(&buffer_id)
199            .map(|state| self.lsp.is_server_ready(&state.language))
200            .unwrap_or(false)
201    }
202
203    /// Check if the active buffer is shown in more than one visible split.
204    pub(crate) fn has_same_buffer_splits(&self) -> bool {
205        let (mgr, vs) = self
206            .buffers
207            .splits()
208            .expect("active window must have a populated split layout");
209        let active_split = mgr.active_split();
210        let active_buf_id = mgr.buffer_for_split(active_split);
211        if let Some(buf_id) = active_buf_id {
212            vs.keys()
213                .any(|&s| s != active_split && mgr.buffer_for_split(s) == Some(buf_id))
214        } else {
215            false
216        }
217    }
218
219    /// Check if a formatter is configured for the current buffer's language.
220    pub(crate) fn is_formatter_available(&self) -> bool {
221        let buffer_id = self.active_buffer();
222        self.buffers
223            .get(&buffer_id)
224            .and_then(|state| {
225                self.config()
226                    .languages
227                    .get(&state.language)
228                    .and_then(|lc| lc.formatter.as_ref())
229                    .map(|_| true)
230            })
231            .unwrap_or(false)
232    }
233}