1mod completion;
10mod console;
11mod filter_builder;
12mod form_apply;
13mod intent;
14mod menu;
15mod modal_open;
16mod names;
17mod overlay_actions;
18mod palette_run;
19mod script;
20mod size_move;
21mod submit;
22mod template;
23mod widget_rows;
24
25use crate::app::{App, MenuId};
26use crate::focus::{FocusTarget, current_focus};
27use crate::keymap::{self, VimState};
28use crate::modal::Modal;
29use crate::pane::PaneId;
30use crate::tabs::list::ListTab;
31use crate::tabs::nms::ConsoleState;
32use crate::view::{Cmd, Handled, Tab};
33use crate::viewer::Source;
34use crate::window::WindowId;
35pub use filter_builder::BuilderKey;
36use form_apply::{apply_editor_intent, apply_modal_intent};
37pub use intent::Intent;
38use modal_open::{
39 confirm_delete_selected_tag, open_account_edit_for_selected, open_account_merge_for_selected,
40 open_commodity_create_modal, open_commodity_edit_for_selected, open_config_edit_for_selected,
41 open_config_set_modal, open_ssh_key_add_modal, open_ssh_key_remove, open_tag_create_modal,
42 open_tag_edit_for_selected, open_transaction_create_modal,
43 open_transaction_create_modal_for_selected, open_transaction_filter_modal,
44};
45use overlay_actions::{
46 close_topmost, confirm_topmost, submit_modal, template_form_open, toggles_split_tags,
47};
48pub use script::ScriptKey;
49pub use template::TemplateKey;
50
51pub fn apply(app: &mut App, intent: Intent) {
56 apply_at(app, current_focus(app), intent);
57}
58
59pub(crate) fn apply_to_view(app: &mut App, intent: Intent) {
62 apply_at(app, FocusTarget::ViewPane, intent);
63}
64
65pub(crate) fn apply_to_window(app: &mut App, id: WindowId, intent: Intent) {
68 match app.front_window() == Some(id) {
69 true => apply_to_view(app, intent),
70 false => {
71 handle_view(app, id, intent);
72 }
73 }
74}
75
76fn handle_view(app: &mut App, id: WindowId, intent: Intent) -> bool {
78 let edge = match intent {
79 Intent::ListSelectNext => Some(true),
80 Intent::ListSelectPrev => Some(false),
81 _ => None,
82 };
83 if let Some(forward) = edge
84 && app.window_view(id) == Some(Tab::Transactions)
85 && app.continue_transactions_page(id, forward)
86 {
87 return true;
88 }
89 let Some(Handled::Consumed(cmds)) = app.view_mut(id).map(|view| view.handle(intent)) else {
90 return false;
91 };
92 execute(app, cmds);
93 true
94}
95
96fn apply_at(app: &mut App, focus: FocusTarget, intent: Intent) {
97 if apply_global(app, intent) {
98 return;
99 }
100 let intent = match focus {
101 FocusTarget::Overlay => resolve_overlay(app, intent),
102 _ => intent,
103 };
104 if focus != FocusTarget::Pending && intent.starts_action() {
105 app.clear_status();
106 }
107 match focus {
108 FocusTarget::Pending => {}
109 FocusTarget::Overlay => {
110 handle_overlay(app, intent);
111 app.fetch_tag_hints();
112 app.preview_options();
113 }
114 FocusTarget::Drag => size_move::handle(app, intent),
115 FocusTarget::Menu => menu::handle(app, intent),
116 FocusTarget::CmdLine => handle_command_line(app, intent),
117 FocusTarget::ConsoleInput => console::handle(app, intent),
118 FocusTarget::ViewPane => handle_tab(app, intent),
119 }
120}
121
122#[must_use]
125pub fn acts(app: &App, intent: Intent) -> bool {
126 let focus = current_focus(app);
127 if focus == FocusTarget::Drag {
128 return matches!(
129 intent,
130 Intent::Drag(_) | Intent::DragDone | Intent::DragCancel
131 );
132 }
133 match intent {
134 Intent::Quit | Intent::CloseOrQuit => {
136 !matches!(focus, FocusTarget::Overlay | FocusTarget::Pending)
137 }
138 Intent::OpenHelp => focus != FocusTarget::Pending,
139 Intent::OpenOptions => windows_free(focus),
140 Intent::OpenMenu => matches!(focus, FocusTarget::ViewPane | FocusTarget::ConsoleInput),
141 Intent::CloseWindow | Intent::SizeWindow => {
142 focus == FocusTarget::Overlay || on_front_window(app, focus)
143 }
144 Intent::NextWindow
145 | Intent::PrevWindow
146 | Intent::ToggleZoom
147 | Intent::Tile
148 | Intent::Cascade
149 | Intent::CloseAll => on_front_window(app, focus),
150 Intent::Drag(_) | Intent::DragDone | Intent::DragCancel => false,
151 Intent::NewWindow(_) => windows_free(focus),
152 Intent::SelectWindow(_) | Intent::RaiseWindow(_) => on_front_window(app, focus),
153 Intent::CloseMenu
154 | Intent::MenuNext
155 | Intent::MenuPrev
156 | Intent::MenuFirst
157 | Intent::MenuLast
158 | Intent::MenuLeft
159 | Intent::MenuRight
160 | Intent::MenuSelect => focus == FocusTarget::Menu,
161 Intent::NewRow => app.front_kind().and_then(new_row_action).is_some(),
162 Intent::EditRow => app.front_kind().and_then(edit_row_action).is_some(),
163 Intent::DeleteRow => app.front_kind().and_then(delete_row_action).is_some(),
164 Intent::OpenFilterBuilder => filter_builder::filter_text(app).is_some(),
165 Intent::ToggleSplitTags => toggles_split_tags(app),
166 _ => true,
167 }
168}
169
170fn on_front_window(app: &App, focus: FocusTarget) -> bool {
172 windows_free(focus) && app.front_window().is_some()
173}
174
175fn windows_free(focus: FocusTarget) -> bool {
176 matches!(
177 focus,
178 FocusTarget::ViewPane | FocusTarget::ConsoleInput | FocusTarget::Menu
179 )
180}
181
182pub(crate) fn open_menu(app: &mut App, menu: MenuId) {
184 if acts(app, Intent::OpenMenu) {
185 app.open_menu_of(menu);
186 }
187}
188
189fn apply_global(app: &mut App, intent: Intent) -> bool {
191 match intent {
192 Intent::Quit if acts(app, intent) => app.confirm_quit(),
193 Intent::QuitNow => app.request_quit(),
194 Intent::CloseOrQuit if acts(app, intent) => close_or_quit(app),
195 Intent::OpenHelp if acts(app, intent) => {
196 let (topic, lines) = (keymap::help_topic(app), keymap::help_lines(app));
197 app.clear_status();
198 app.show_viewer(Source::Help { topic }, lines);
199 }
200 Intent::OpenMenu if acts(app, intent) => open_menu(app, MenuId::View),
201 Intent::OpenOptions if acts(app, intent) => app.open_options(),
202 Intent::CloseWindow if acts(app, intent) => close_window_or_dialog(app),
203 Intent::NextWindow
204 | Intent::PrevWindow
205 | Intent::SizeWindow
206 | Intent::ToggleZoom
207 | Intent::Tile
208 | Intent::Cascade
209 | Intent::CloseAll
210 | Intent::NewWindow(_)
211 | Intent::SelectWindow(_)
212 | Intent::RaiseWindow(_)
213 if acts(app, intent) =>
214 {
215 window_command(app, intent);
216 }
217 _ => return false,
218 }
219 true
220}
221
222fn close_or_quit(app: &mut App) {
224 match app.front_window() {
225 Some(_) => close_window_or_dialog(app),
226 None => app.confirm_quit(),
227 }
228}
229
230fn close_window_or_dialog(app: &mut App) {
232 match app.overlays.is_empty() {
233 true => {
234 app.close_menu();
235 app.close_front_window();
236 }
237 false => close_topmost(app),
238 }
239}
240
241fn window_command(app: &mut App, intent: Intent) {
242 app.close_menu();
243 match intent {
244 Intent::NextWindow => app.next_window(),
245 Intent::PrevWindow => app.prev_window(),
246 Intent::Tile => app.tile_windows(),
247 Intent::Cascade => app.cascade_windows(),
248 Intent::CloseAll => app.close_all_windows(),
249 Intent::SizeWindow => app.begin_size_move(),
250 Intent::NewWindow(kind) => app.open_view(kind),
251 Intent::SelectWindow(number) => app.select_window_number(number),
252 Intent::RaiseWindow(id) => app.raise_listed(id),
253 _ => app.toggle_zoom(),
254 }
255}
256
257pub(crate) fn resolve_overlay(app: &App, intent: Intent) -> Intent {
259 match intent {
260 Intent::Press => app
261 .overlays
262 .top()
263 .map_or(Intent::Submit, Modal::press_intent),
264 Intent::ToggleSplitTags if template_form_open(app) => Intent::Template(TemplateKey::Test),
265 other => other,
266 }
267}
268
269fn handle_overlay(app: &mut App, intent: Intent) {
270 match intent {
271 Intent::CloseTopmost => close_topmost(app),
272 Intent::ConfirmYes => confirm_topmost(app),
275 Intent::OpenFilterBuilder => filter_builder::open(app),
276 Intent::Builder(key) => filter_builder::apply_key(app, key),
277 Intent::ApplyOptions => app.apply_options(),
278 Intent::CsvCopy => app.copy_csv(),
279 Intent::CsvSave => app.begin_csv_save(),
280 Intent::Submit => submit_modal(app),
281 Intent::Template(TemplateKey::Test) if template_form_open(app) => app.test_template_form(),
282 _ => apply_to_top(app, intent),
283 }
284}
285
286fn apply_to_top(app: &mut App, intent: Intent) {
287 if let Some(top) = app.overlays.top_mut() {
288 apply_modal_intent(top, intent);
289 }
290}
291
292fn handle_command_line(app: &mut App, intent: Intent) {
293 match intent {
294 Intent::CloseTopmost => app.cmdline.active = false,
295 Intent::EnterNormalMode => app.keys.set_vim(VimState::Normal),
296 Intent::InsertAtCursor
297 | Intent::InsertAfterCursor
298 | Intent::InsertAtLineStart
299 | Intent::InsertAtLineEnd => start_typing(app, intent),
300 Intent::Submit => {
301 let buffer = app.cmdline.editor.buffer().to_string();
302 app.close_command_line();
303 palette_run::submit_palette(app, &buffer);
304 }
305 Intent::CompleteCommandLine => completion::apply_command_completion(app),
306 _ => {
308 app.cmdline.completions.clear();
309 apply_editor_intent(&mut app.cmdline.editor, intent);
310 }
311 }
312}
313
314fn start_typing(app: &mut App, intent: Intent) {
315 let editor = &mut app.cmdline.editor;
316 match intent {
317 Intent::InsertAfterCursor => editor.move_right(),
318 Intent::InsertAtLineStart => editor.move_home(),
319 Intent::InsertAtLineEnd => editor.move_end(),
320 _ => {}
321 }
322 app.keys.set_vim(VimState::Insert);
323}
324
325fn execute(app: &mut App, cmds: Vec<Cmd>) {
326 for cmd in cmds {
327 match cmd {
328 Cmd::Status(msg) => app.set_status(msg),
329 Cmd::SwitchView(id) => app.switch_tab(id),
330 }
331 }
332}
333
334fn handle_tab(app: &mut App, intent: Intent) {
335 let needs_window = !matches!(intent, Intent::SelectTab(_) | Intent::OpenCommandLine);
336 if needs_window && app.front_window().is_none() {
337 app.say_no_window(None);
338 return;
339 }
340 if let Some(front) = app.front_window()
341 && handle_view(app, front, intent)
342 {
343 return;
344 }
345 match intent {
346 Intent::SelectTab(t) => app.switch_tab(t),
347 Intent::NewRow => new_row(app),
348 Intent::EditRow => edit_row(app),
349 Intent::DeleteRow => delete_row(app),
350 Intent::OpenCommandLine => app.open_command_line(),
351 Intent::DetailEdit => app.edit_from_detail(),
352 Intent::ConsoleFocus if app.front_is(Tab::Console) => {
353 if let Some(console) = app.front_mut::<ConsoleState>() {
354 console.panes.focus(PaneId::Prompt);
355 }
356 app.set_console_focused(true);
357 }
358 Intent::RefreshTab => {
359 if let Some(tab) = app.front_kind() {
360 app.refresh_windows(tab);
361 }
362 }
363 Intent::ReportNew(kind) if app.front_is(Tab::Reports) => app.new_report(kind),
364 Intent::ReportChart if app.front_is(Tab::Reports) => app.toggle_report_chart(),
365 Intent::ReportCsv if app.front_is(Tab::Reports) => app.request_report_csv(),
366 Intent::ListPageNext if app.front_is(Tab::Transactions) => {
367 app.page_transactions(true);
368 }
369 Intent::ListPagePrev if app.front_is(Tab::Transactions) => {
370 app.page_transactions(false);
371 }
372 Intent::ListFilter if app.front_is(Tab::Transactions) => {
373 open_transaction_filter_modal(app);
374 }
375 Intent::ListFilterClear if app.front_is(Tab::Transactions) => {
376 if let Some(window) = app.front_window() {
377 app.clear_transaction_filter(window);
378 }
379 }
380 Intent::Template(key) => template::apply_tab(app, key),
381 Intent::Script(key) => script::apply_tab(app, key),
382 Intent::ListMark if app.front_is(Tab::Transactions) => {
383 if app.front_mut::<ListTab>().is_some_and(ListTab::toggle_mark) {
384 let front = app.front_window();
385 if !front.is_some_and(|front| app.continue_transactions_page(front, true))
386 && let Some(list) = app.front_mut::<ListTab>()
387 {
388 list.select_next();
389 }
390 } else {
391 app.set_status("no transaction selected");
392 }
393 }
394 Intent::ListUnmarkAll if app.front_is(Tab::Transactions) => {
395 if let Some(list) = app.front_mut::<ListTab>() {
396 list.clear_marks();
397 }
398 }
399 Intent::ListDeleteMarked if app.front_is(Tab::Transactions) => {
400 app.open_delete_marked();
401 }
402 Intent::ListDetail if app.front_is(Tab::Transactions) => {
403 app.open_transaction_detail_selected();
404 }
405 Intent::ListMergeAccount if app.front_is(Tab::Accounts) => {
406 open_account_merge_for_selected(app);
407 }
408 Intent::ListAccountTransactions if app.front_is(Tab::Accounts) => {
409 app.filter_transactions_of_selected_account();
410 }
411 _ => {}
412 }
413}
414
415type RowAction = fn(&mut App);
416
417fn new_row_action(tab: Tab) -> Option<RowAction> {
418 match tab {
419 Tab::Accounts => Some(open_transaction_create_modal_for_selected),
420 Tab::Transactions => Some(open_transaction_create_modal),
421 Tab::Commodities => Some(open_commodity_create_modal),
422 Tab::Config => Some(|app| open_config_set_modal(app, &[])),
423 Tab::SshKeys => Some(|app| open_ssh_key_add_modal(app, &[])),
424 Tab::Tags => Some(open_tag_create_modal),
425 Tab::Templates => Some(template::open_create),
426 Tab::Scripts => Some(script::open_create),
427 _ => None,
428 }
429}
430
431fn edit_row_action(tab: Tab) -> Option<RowAction> {
432 match tab {
433 Tab::Transactions => Some(open_selected_transaction_edit),
434 Tab::Accounts => Some(open_account_edit_for_selected),
435 Tab::Commodities => Some(open_commodity_edit_for_selected),
436 Tab::Config => Some(open_config_edit_for_selected),
437 Tab::Tags => Some(open_tag_edit_for_selected),
438 Tab::Templates => Some(template::open_edit_for_selected),
439 Tab::Scripts => Some(script::open_edit_for_selected),
440 Tab::Reports => Some(App::edit_last_report),
441 _ => None,
442 }
443}
444
445fn delete_row_action(tab: Tab) -> Option<RowAction> {
446 match tab {
447 Tab::Transactions => Some(confirm_delete_selected_transaction),
448 Tab::SshKeys => Some(|app| open_ssh_key_remove(app, None)),
449 Tab::Tags => Some(confirm_delete_selected_tag),
450 Tab::Templates => Some(template::confirm_delete_selected),
451 Tab::Scripts => Some(script::confirm_delete_selected),
452 _ => None,
453 }
454}
455
456fn new_row(app: &mut App) {
457 if let Some(action) = app.front_kind().and_then(new_row_action) {
458 action(app);
459 }
460}
461
462fn edit_row(app: &mut App) {
463 if let Some(action) = app.front_kind().and_then(edit_row_action) {
464 action(app);
465 }
466}
467
468fn delete_row(app: &mut App) {
469 if let Some(action) = app.front_kind().and_then(delete_row_action) {
470 action(app);
471 }
472}
473
474fn open_selected_transaction_edit(app: &mut App) {
475 match app.selected_id(Tab::Transactions) {
476 Some(id) => app.open_transaction_edit_async(&id),
477 None => app.set_status("no transaction selected"),
478 }
479}
480
481fn confirm_delete_selected_transaction(app: &mut App) {
482 let maybe_id = app.selected_id(Tab::Transactions);
483 if let Some(id) = maybe_id {
484 app.open_dialog_for(
485 Tab::Transactions,
486 Modal::confirm(
487 format!("Delete transaction {id}?"),
488 crate::modal::ConfirmAction::DeleteTransaction(id),
489 ),
490 );
491 }
492}
493
494#[cfg(test)]
495mod acts_tests;
496#[cfg(test)]
497mod tests;