From 2d8653754d62ea840df50d6ce355ff15dcb4f6cd Mon Sep 17 00:00:00 2001 From: Allen Webster Date: Sat, 19 Oct 2019 18:32:38 -0700 Subject: [PATCH] Deduplicated fallback dispatch code --- custom/4coder_default_framework.cpp | 45 +++++++++++++++++++++++++++- custom/4coder_default_framework.h | 13 ++++++-- custom/4coder_lister_base.cpp | 26 ++-------------- custom/4coder_log_parser.cpp | 25 ++++++++++++++++ custom/4coder_metadata_generator.cpp | 3 -- custom/4coder_profile_inspect.cpp | 22 ++------------ custom/4coder_types.h | 4 +-- custom/generated/command_metadata.h | 28 ++++++++--------- 8 files changed, 100 insertions(+), 66 deletions(-) diff --git a/custom/4coder_default_framework.cpp b/custom/4coder_default_framework.cpp index 965aa2a5..f52269f6 100644 --- a/custom/4coder_default_framework.cpp +++ b/custom/4coder_default_framework.cpp @@ -150,13 +150,56 @@ get_next_view_after_active(Application_Links *app, Access_Flag access){ //////////////////////////////// static void -call_after_ui_shutdown(Application_Links *app, View_ID view, Custom_Command_Function *func){ +call_after_ctx_shutdown(Application_Links *app, View_ID view, Custom_Command_Function *func){ Managed_Scope scope = view_get_managed_scope(app, view); Custom_Command_Function **call_next = scope_attachment(app, scope, view_call_next, Custom_Command_Function*); *call_next = func; } +function Fallback_Dispatch_Result +fallback_command_dispatch(Application_Links *app, Mapping *mapping, Command_Map *map, + User_Input *in){ + Fallback_Dispatch_Result result = {}; + if (mapping != 0 && map != 0){ + Command_Binding binding = map_get_binding_recursive(mapping, map, &in->event); + if (binding.custom != 0){ + Command_Metadata *metadata = get_command_metadata(binding.custom); + if (metadata != 0){ + if (metadata->is_ui){ + result.code = FallbackDispatch_DelayedUICall; + result.func = binding.custom; + } + else{ + binding.custom(app); + result.code = FallbackDispatch_DidCall; + } + } + else{ + binding.custom(app); + result.code = FallbackDispatch_DidCall; + } + } + } + return(result); +} + +function b32 +ui_fallback_command_dispatch(Application_Links *app, View_ID view, + Mapping *mapping, Command_Map *map, User_Input *in){ + b32 result = false; + Fallback_Dispatch_Result disp_result = + fallback_command_dispatch(app, mapping, map, in); + if (disp_result.code == FallbackDispatch_DelayedUICall){ + call_after_ctx_shutdown(app, view, disp_result.func); + result = true; + } + if (disp_result.code == FallbackDispatch_Unhandled){ + leave_current_input_unhandled(app); + } + return(result); +} + //////////////////////////////// static void diff --git a/custom/4coder_default_framework.h b/custom/4coder_default_framework.h index cae3ed55..8ccef7e5 100644 --- a/custom/4coder_default_framework.h +++ b/custom/4coder_default_framework.h @@ -54,9 +54,16 @@ struct ID_Pos_Jump_Location_Array{ //////////////////////////////// -struct Named_Mapping{ - String_Const_u8 name; - Custom_Command_Function *remap_command; +typedef i32 Fallback_Dispatch_Result_Code; +enum{ + FallbackDispatch_Unhandled, + FallbackDispatch_DidCall, + FallbackDispatch_DelayedUICall, +}; + +struct Fallback_Dispatch_Result{ + Fallback_Dispatch_Result_Code code; + Custom_Command_Function *func; }; //////////////////////////////// diff --git a/custom/4coder_lister_base.cpp b/custom/4coder_lister_base.cpp index d2227800..e6ba2a5d 100644 --- a/custom/4coder_lister_base.cpp +++ b/custom/4coder_lister_base.cpp @@ -649,30 +649,8 @@ lister_run(Application_Links *app, View_ID view, Lister *lister){ // TODO(allen): dedup this stuff. Mapping *mapping = lister->mapping; Command_Map *map = lister->map; - if (mapping != 0 && map != 0){ - Command_Binding binding = - map_get_binding_recursive(mapping, map, &in.event); - if (binding.custom != 0){ - Command_Metadata *metadata = get_command_metadata(binding.custom); - if (metadata != 0){ - if (metadata->is_ui){ - call_after_ui_shutdown(app, view, binding.custom); - break; - } - else{ - binding.custom(app); - } - } - else{ - binding.custom(app); - } - } - else{ - leave_current_input_unhandled(app); - } - } - else{ - leave_current_input_unhandled(app); + if (ui_fallback_command_dispatch(app, view, mapping, map, &in)){ + break; } } } diff --git a/custom/4coder_log_parser.cpp b/custom/4coder_log_parser.cpp index ef386ef5..8a7c6074 100644 --- a/custom/4coder_log_parser.cpp +++ b/custom/4coder_log_parser.cpp @@ -1009,6 +1009,7 @@ CUSTOM_DOC("Parses *log* and displays the 'log graph' UI") break; } + b32 handled = true; switch (in.event.kind){ case InputEventKind_KeyStroke: { @@ -1022,6 +1023,11 @@ CUSTOM_DOC("Parses *log* and displays the 'log graph' UI") { log_graph.y_scroll += get_page_jump(app, log_view); }break; + + default: + { + handled = false; + }break; } }break; @@ -1038,6 +1044,11 @@ CUSTOM_DOC("Parses *log* and displays the 'log graph' UI") { log_graph__click_select_event(app, m_p); }break; + + default: + { + handled = false; + }break; } }break; @@ -1046,6 +1057,20 @@ CUSTOM_DOC("Parses *log* and displays the 'log graph' UI") f32 value = in.event.mouse_wheel.value; log_graph.y_scroll += f32_round32(value); }break; + + default: + { + handled = false; + }break; + } + + if (!handled){ + // TODO(allen): get mapping and map from a more flexible source. + Mapping *mapping = &framework_mapping; + Command_Map *map = mapping_get_map(mapping, mapid_global); + if (ui_fallback_command_dispatch(app, view, mapping, map, &in)){ + break; + } } } diff --git a/custom/4coder_metadata_generator.cpp b/custom/4coder_metadata_generator.cpp index c4b37f8e..f33c1f57 100644 --- a/custom/4coder_metadata_generator.cpp +++ b/custom/4coder_metadata_generator.cpp @@ -667,9 +667,6 @@ parse_text(Arena *arena, Meta_Command_Entry_Arrays *entry_arrays, u8 *source_nam } } } - else if (string_match(lexeme, string_u8_litexpr("CUSTOM_UI_COMMAND"))){ - - } } if (token.kind == TokenBaseKind_EOF){ diff --git a/custom/4coder_profile_inspect.cpp b/custom/4coder_profile_inspect.cpp index 5d4c943f..9bff6662 100644 --- a/custom/4coder_profile_inspect.cpp +++ b/custom/4coder_profile_inspect.cpp @@ -374,7 +374,7 @@ profile_render(Application_Links *app, Frame_Info frame_info, View_ID view){ draw_set_clip(app, prev_clip); } -CUSTOM_COMMAND_SIG(profile_inspect) +CUSTOM_UI_COMMAND_SIG(profile_inspect) CUSTOM_DOC("Inspect all currently collected profiling information in 4coder's self profiler.") { if (HasFlag(global_prof_list.disable_bits, ProfileEnable_InspectBit)){ @@ -426,27 +426,11 @@ CUSTOM_DOC("Inspect all currently collected profiling information in 4coder's se } if (!handled){ - // TODO(allen): dedup this stuff. // TODO(allen): get mapping and map from a more flexible source. Mapping *mapping = &framework_mapping; Command_Map *map = mapping_get_map(mapping, mapid_global); - if (mapping != 0 && map != 0){ - Command_Binding binding = - map_get_binding_recursive(mapping, map, &in.event); - if (binding.custom != 0){ - i64 old_num = get_current_input_sequence_number(app); - binding.custom(app); - i64 num = get_current_input_sequence_number(app); - if (old_num < num){ - break; - } - } - else{ - leave_current_input_unhandled(app); - } - } - else{ - leave_current_input_unhandled(app); + if (ui_fallback_command_dispatch(app, view, mapping, map, &in)){ + break; } } } diff --git a/custom/4coder_types.h b/custom/4coder_types.h index 361fbacb..c6aa7288 100644 --- a/custom/4coder_types.h +++ b/custom/4coder_types.h @@ -454,8 +454,8 @@ STRUCT Record_Info{ TYPEDEF void Custom_Command_Function(struct Application_Links *app); -#if defined(CUSTOM_COMMAND_SIG) || defined(CUSTOM_UI_COMMAND) || defined(CUSTOM_DOC) || defined(CUSTOM_COMMAND) -#error Please do not define CUSTOM_COMMAND_SIG, CUSTOM_DOC, or CUSTOM_UI_COMMAND +#if defined(CUSTOM_COMMAND_SIG) || defined(CUSTOM_UI_COMMAND_SIG) || defined(CUSTOM_DOC) || defined(CUSTOM_COMMAND) +#error Please do not define CUSTOM_COMMAND_SIG, CUSTOM_DOC, CUSTOM_UI_COMMAND_SIG, or CUSTOM_COMMAND #endif #if !defined(META_PASS) diff --git a/custom/generated/command_metadata.h b/custom/generated/command_metadata.h index abb93af2..79a50c6c 100644 --- a/custom/generated/command_metadata.h +++ b/custom/generated/command_metadata.h @@ -243,19 +243,19 @@ static Command_Metadata fcoder_metacmd_table[211] = { { PROC_LINKS(seek_end_of_line, 0), false, "seek_end_of_line", 16, "Seeks the cursor to the end of the visual line.", 47, "w:\\4ed\\code\\custom\\4coder_helper.cpp", 36, 2124 }, { PROC_LINKS(goto_beginning_of_file, 0), false, "goto_beginning_of_file", 22, "Sets the cursor to the beginning of the file.", 45, "w:\\4ed\\code\\custom\\4coder_helper.cpp", 36, 2130 }, { PROC_LINKS(goto_end_of_file, 0), false, "goto_end_of_file", 16, "Sets the cursor to the end of the file.", 39, "w:\\4ed\\code\\custom\\4coder_helper.cpp", 36, 2138 }, -{ PROC_LINKS(change_active_panel, 0), false, "change_active_panel", 19, "Change the currently active panel, moving to the panel with the next highest view_id.", 85, "w:\\4ed\\code\\custom\\4coder_default_framework.cpp", 47, 213 }, -{ PROC_LINKS(change_active_panel_backwards, 0), false, "change_active_panel_backwards", 29, "Change the currently active panel, moving to the panel with the next lowest view_id.", 84, "w:\\4ed\\code\\custom\\4coder_default_framework.cpp", 47, 223 }, -{ PROC_LINKS(open_panel_vsplit, 0), false, "open_panel_vsplit", 17, "Create a new panel by vertically splitting the active panel.", 60, "w:\\4ed\\code\\custom\\4coder_default_framework.cpp", 47, 233 }, -{ PROC_LINKS(open_panel_hsplit, 0), false, "open_panel_hsplit", 17, "Create a new panel by horizontally splitting the active panel.", 62, "w:\\4ed\\code\\custom\\4coder_default_framework.cpp", 47, 243 }, -{ PROC_LINKS(suppress_mouse, 0), false, "suppress_mouse", 14, "Hides the mouse and causes all mosue input (clicks, position, wheel) to be ignored.", 83, "w:\\4ed\\code\\custom\\4coder_default_framework.cpp", 47, 308 }, -{ PROC_LINKS(allow_mouse, 0), false, "allow_mouse", 11, "Shows the mouse and causes all mouse input to be processed normally.", 68, "w:\\4ed\\code\\custom\\4coder_default_framework.cpp", 47, 314 }, -{ PROC_LINKS(toggle_mouse, 0), false, "toggle_mouse", 12, "Toggles the mouse suppression mode, see suppress_mouse and allow_mouse.", 71, "w:\\4ed\\code\\custom\\4coder_default_framework.cpp", 47, 320 }, -{ PROC_LINKS(set_mode_to_original, 0), false, "set_mode_to_original", 20, "Sets the edit mode to 4coder original.", 38, "w:\\4ed\\code\\custom\\4coder_default_framework.cpp", 47, 326 }, -{ PROC_LINKS(set_mode_to_notepad_like, 0), false, "set_mode_to_notepad_like", 24, "Sets the edit mode to Notepad like.", 35, "w:\\4ed\\code\\custom\\4coder_default_framework.cpp", 47, 332 }, -{ PROC_LINKS(toggle_highlight_line_at_cursor, 0), false, "toggle_highlight_line_at_cursor", 31, "Toggles the line highlight at the cursor.", 41, "w:\\4ed\\code\\custom\\4coder_default_framework.cpp", 47, 338 }, -{ PROC_LINKS(toggle_highlight_enclosing_scopes, 0), false, "toggle_highlight_enclosing_scopes", 33, "In code files scopes surrounding the cursor are highlighted with distinguishing colors.", 87, "w:\\4ed\\code\\custom\\4coder_default_framework.cpp", 47, 344 }, -{ PROC_LINKS(toggle_paren_matching_helper, 0), false, "toggle_paren_matching_helper", 28, "In code files matching parentheses pairs are colored with distinguishing colors.", 80, "w:\\4ed\\code\\custom\\4coder_default_framework.cpp", 47, 350 }, -{ PROC_LINKS(toggle_fullscreen, 0), false, "toggle_fullscreen", 17, "Toggle fullscreen mode on or off. The change(s) do not take effect until the next frame.", 89, "w:\\4ed\\code\\custom\\4coder_default_framework.cpp", 47, 356 }, +{ PROC_LINKS(change_active_panel, 0), false, "change_active_panel", 19, "Change the currently active panel, moving to the panel with the next highest view_id.", 85, "w:\\4ed\\code\\custom\\4coder_default_framework.cpp", 47, 256 }, +{ PROC_LINKS(change_active_panel_backwards, 0), false, "change_active_panel_backwards", 29, "Change the currently active panel, moving to the panel with the next lowest view_id.", 84, "w:\\4ed\\code\\custom\\4coder_default_framework.cpp", 47, 266 }, +{ PROC_LINKS(open_panel_vsplit, 0), false, "open_panel_vsplit", 17, "Create a new panel by vertically splitting the active panel.", 60, "w:\\4ed\\code\\custom\\4coder_default_framework.cpp", 47, 276 }, +{ PROC_LINKS(open_panel_hsplit, 0), false, "open_panel_hsplit", 17, "Create a new panel by horizontally splitting the active panel.", 62, "w:\\4ed\\code\\custom\\4coder_default_framework.cpp", 47, 286 }, +{ PROC_LINKS(suppress_mouse, 0), false, "suppress_mouse", 14, "Hides the mouse and causes all mosue input (clicks, position, wheel) to be ignored.", 83, "w:\\4ed\\code\\custom\\4coder_default_framework.cpp", 47, 351 }, +{ PROC_LINKS(allow_mouse, 0), false, "allow_mouse", 11, "Shows the mouse and causes all mouse input to be processed normally.", 68, "w:\\4ed\\code\\custom\\4coder_default_framework.cpp", 47, 357 }, +{ PROC_LINKS(toggle_mouse, 0), false, "toggle_mouse", 12, "Toggles the mouse suppression mode, see suppress_mouse and allow_mouse.", 71, "w:\\4ed\\code\\custom\\4coder_default_framework.cpp", 47, 363 }, +{ PROC_LINKS(set_mode_to_original, 0), false, "set_mode_to_original", 20, "Sets the edit mode to 4coder original.", 38, "w:\\4ed\\code\\custom\\4coder_default_framework.cpp", 47, 369 }, +{ PROC_LINKS(set_mode_to_notepad_like, 0), false, "set_mode_to_notepad_like", 24, "Sets the edit mode to Notepad like.", 35, "w:\\4ed\\code\\custom\\4coder_default_framework.cpp", 47, 375 }, +{ PROC_LINKS(toggle_highlight_line_at_cursor, 0), false, "toggle_highlight_line_at_cursor", 31, "Toggles the line highlight at the cursor.", 41, "w:\\4ed\\code\\custom\\4coder_default_framework.cpp", 47, 381 }, +{ PROC_LINKS(toggle_highlight_enclosing_scopes, 0), false, "toggle_highlight_enclosing_scopes", 33, "In code files scopes surrounding the cursor are highlighted with distinguishing colors.", 87, "w:\\4ed\\code\\custom\\4coder_default_framework.cpp", 47, 387 }, +{ PROC_LINKS(toggle_paren_matching_helper, 0), false, "toggle_paren_matching_helper", 28, "In code files matching parentheses pairs are colored with distinguishing colors.", 80, "w:\\4ed\\code\\custom\\4coder_default_framework.cpp", 47, 393 }, +{ PROC_LINKS(toggle_fullscreen, 0), false, "toggle_fullscreen", 17, "Toggle fullscreen mode on or off. The change(s) do not take effect until the next frame.", 89, "w:\\4ed\\code\\custom\\4coder_default_framework.cpp", 47, 399 }, { PROC_LINKS(write_text_input, 0), false, "write_text_input", 16, "Inserts whatever character was used to trigger this command.", 60, "w:\\4ed\\code\\custom\\4coder_base_commands.cpp", 43, 58 }, { PROC_LINKS(write_space, 0), false, "write_space", 11, "Inserts an underscore.", 22, "w:\\4ed\\code\\custom\\4coder_base_commands.cpp", 43, 66 }, { PROC_LINKS(write_underscore, 0), false, "write_underscore", 16, "Inserts an underscore.", 22, "w:\\4ed\\code\\custom\\4coder_base_commands.cpp", 43, 72 }, @@ -441,7 +441,7 @@ static Command_Metadata fcoder_metacmd_table[211] = { { PROC_LINKS(miblo_decrement_time_stamp, 0), false, "miblo_decrement_time_stamp", 26, "Decrement a time stamp under the cursor by one second. (format [m]m:ss or h:mm:ss", 81, "w:\\4ed\\code\\custom\\4coder_miblo_numbers.cpp", 43, 237 }, { PROC_LINKS(miblo_increment_time_stamp_minute, 0), false, "miblo_increment_time_stamp_minute", 33, "Increment a time stamp under the cursor by one minute. (format [m]m:ss or h:mm:ss", 81, "w:\\4ed\\code\\custom\\4coder_miblo_numbers.cpp", 43, 243 }, { PROC_LINKS(miblo_decrement_time_stamp_minute, 0), false, "miblo_decrement_time_stamp_minute", 33, "Decrement a time stamp under the cursor by one minute. (format [m]m:ss or h:mm:ss", 81, "w:\\4ed\\code\\custom\\4coder_miblo_numbers.cpp", 43, 249 }, -{ PROC_LINKS(profile_inspect, 0), false, "profile_inspect", 15, "Inspect all currently collected profiling information in 4coder's self profiler.", 80, "w:\\4ed\\code\\custom\\4coder_profile_inspect.cpp", 45, 377 }, +{ PROC_LINKS(profile_inspect, 0), true, "profile_inspect", 15, "Inspect all currently collected profiling information in 4coder's self profiler.", 80, "w:\\4ed\\code\\custom\\4coder_profile_inspect.cpp", 45, 377 }, { PROC_LINKS(default_startup, 0), false, "default_startup", 15, "Default command for responding to a startup event", 49, "w:\\4ed\\code\\custom\\4coder_default_hooks.cpp", 43, 7 }, { PROC_LINKS(default_try_exit, 0), false, "default_try_exit", 16, "Default command for responding to a try-exit event", 50, "w:\\4ed\\code\\custom\\4coder_default_hooks.cpp", 43, 21 }, };