Skip to content

refactor: hotkey handling#1118

Merged
alandtse merged 2 commits into
devfrom
hotkey_refactor
Jun 7, 2025
Merged

refactor: hotkey handling#1118
alandtse merged 2 commits into
devfrom
hotkey_refactor

Conversation

@alandtse
Copy link
Copy Markdown
Collaborator

@alandtse alandtse commented Jun 5, 2025

closes #1108

Summary by CodeRabbit

  • Refactor
    • Improved the responsiveness and reliability of keyboard hotkey assignments and actions within the menu interface.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 5, 2025

Walkthrough

The Menu::ProcessInputEventQueue() method in src/Menu.cpp has been refactored to replace repetitive if-else chains for hotkey handling with structured arrays and lambda functions. This data-driven approach organizes hotkey assignment and action execution logic, improving modularity while retaining the original input processing flow.

Changes

File(s) Change Summary
src/Menu.cpp Refactored hotkey handling in ProcessInputEventQueue() by replacing if-else chains with structured arrays (HotkeyAction, KeyAction) and lambda functions for modularity and maintainability. No changes to public/exported declarations.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Menu
    participant Settings

    User->>Menu: Press/Release Key
    Menu->>Menu: ProcessInputEventQueue()
    alt Pending Hotkey Assignment
        Menu->>Settings: Assign new hotkey via HotkeyAction lambda
        Menu->>Menu: Reset assignment flag
    else Normal Key Action
        Menu->>Menu: Execute action via KeyAction lambda
    end
    Menu->>Menu: Continue with remaining input processing
Loading

Assessment against linked issues

Objective Addressed Explanation
Refactor repetitive hotkey handling code in ProcessInputEventQueue (#1108)
Reduce code duplication in hotkey handling (#1108)
Make it easier to add new hotkeys in the future (#1108)
Improve code maintainability (#1108)

Poem

In fields of code where hotkeys grew,
The if-else chains were quite a few.
With lambdas now, the logic’s neat—
Arrays march in, code’s trim and fleet!
A rabbit hops with nimble glee,
For future keys, maintainability!
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Automated formatting by clang-format, prettier, and other hooks.
See https://pre-commit.ci for details.
@alandtse alandtse marked this pull request as ready for review June 5, 2025 04:17
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/Menu.cpp (1)

1927-1940: Consider moving arrays to static/member variables for better performance.

The HotkeyAction array is recreated on every key release event, which could impact performance if this method is called frequently. Consider moving these arrays to static variables or class members to avoid repeated allocations.

+	// Move to class members or static variables
+	static const HotkeyAction hotkeyActions[] = {
+		{ &settings.ToggleKey, &settingToggleKey, [this](uint32_t key) { settings.ToggleKey = key; settingToggleKey = false; } },
+		{ &settings.SkipCompilationKey, &settingSkipCompilationKey, [this](uint32_t key) { settings.SkipCompilationKey = key; settingSkipCompilationKey = false; } },
+		{ &settings.EffectToggleKey, &settingsEffectsToggle, [this](uint32_t key) { settings.EffectToggleKey = key; settingsEffectsToggle = false; } },
+		{ &settings.PerfOverlay.OverlayToggleKey, &settingOverlayToggleKey, [this](uint32_t key) { settings.PerfOverlay.OverlayToggleKey = key; settingOverlayToggleKey = false; } },
+	};
-				struct HotkeyAction
-				{
-					uint32_t* settingKey;
-					bool* settingFlag;
-					std::function<void(uint32_t)> action;
-				};
-				auto shaderCache = globals::shaderCache;
-				auto devMode = globals::state->IsDeveloperMode();
-				HotkeyAction hotkeyActions[] = {
-					{ &settings.ToggleKey, &settingToggleKey, [this](uint32_t key) { settings.ToggleKey = key; settingToggleKey = false; } },
-					{ &settings.SkipCompilationKey, &settingSkipCompilationKey, [this](uint32_t key) { settings.SkipCompilationKey = key; settingSkipCompilationKey = false; } },
-					{ &settings.EffectToggleKey, &settingsEffectsToggle, [this](uint32_t key) { settings.EffectToggleKey = key; settingsEffectsToggle = false; } },
-					{ &settings.PerfOverlay.OverlayToggleKey, &settingOverlayToggleKey, [this](uint32_t key) { settings.PerfOverlay.OverlayToggleKey = key; settingOverlayToggleKey = false; } },
-				};
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 447957a and 3f879ce.

📒 Files selected for processing (1)
  • src/Menu.cpp (1 hunks)
🔇 Additional comments (2)
src/Menu.cpp (2)

1927-1969: Excellent refactoring approach for maintainability.

The data-driven approach using structured arrays and lambda functions significantly improves code maintainability compared to repetitive if-else chains. The logic correctly handles both hotkey setting and regular key actions with proper precedence.


1955-1962:

Details

✅ Verification successful

Verify undefined variables and consider performance optimization.

Two concerns with this code segment:

  1. priorShaderKey and nextShaderKey are referenced but not defined in the visible scope. Please ensure these variables are properly declared.

  2. Similar to the HotkeyAction array, consider moving KeyAction array to avoid repeated allocations.


🏁 Script executed:

#!/bin/bash
# Search for priorShaderKey and nextShaderKey definitions
echo "Searching for priorShaderKey and nextShaderKey definitions..."
rg -A 2 -B 2 "priorShaderKey|nextShaderKey" --type cpp

Length of output: 1107


priorShaderKey/nextShaderKey are properly declared
Both keys are defined in src/Menu.h:

  • uint32_t priorShaderKey = VK_PRIOR;
  • uint32_t nextShaderKey = VK_NEXT;

The KeyAction array in Menu.cpp is a fixed‐size local array allocated on the stack; its construction (including lambdas) has minimal overhead. If this code path is executed every frame and you observe a performance hotspot, you could hoist it to a static scope or cache it between calls. Otherwise, no changes are required here.

@alandtse
Copy link
Copy Markdown
Collaborator Author

alandtse commented Jun 5, 2025

@coderabbitai Does the refactor still allow the setting of the key to something else?

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 5, 2025

Tip

For best results, initiate chat on the files or code changes.

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Read the Menu.cpp file to examine the refactored hotkey handling implementation
cat src/Menu.cpp

Length of output: 65555


@alandtse Yes, the refactor absolutely still allows setting keys to something else!

Looking at the refactored code, the hotkey assignment functionality has been preserved and actually improved with the new structure. Here's how it works:

The new HotkeyAction array contains entries like:

HotkeyAction hotkeyActions[] = {
    { &settings.ToggleKey, &settingToggleKey, [this](uint32_t key) { settings.ToggleKey = key; settingToggleKey = false; } },
    { &settings.SkipCompilationKey, &settingSkipCompilationKey, [this](uint32_t key) { settings.SkipCompilationKey = key; settingSkipCompilationKey = false; } },
    { &settings.EffectToggleKey, &settingsEffectsToggle, [this](uint32_t key) { settings.EffectToggleKey = key; settingsEffectsToggle = false; } },
    { &settings.PerfOverlay.OverlayToggleKey, &settingOverlayToggleKey, [this](uint32_t key) { settings.PerfOverlay.OverlayToggleKey = key; settingOverlayToggleKey = false; } },
};

Each entry has:

  1. A pointer to the setting (e.g., &settings.ToggleKey)
  2. A flag pointer indicating when we're in "assignment mode" (e.g., &settingToggleKey)
  3. A lambda function that updates the setting with the new key and resets the assignment flag

The UI in DrawGeneralSettings() also confirms this functionality is intact - you can still click "Change" buttons to enter assignment mode, and the system will capture the next key press to assign as the new hotkey.

So the refactor successfully modernized the code structure while maintaining full hotkey customization capabilities.

@doodlum
Copy link
Copy Markdown
Collaborator

doodlum commented Jun 7, 2025

Does this affect behaviour

@alandtse
Copy link
Copy Markdown
Collaborator Author

alandtse commented Jun 7, 2025

Everything works for me.

@alandtse alandtse merged commit d22a4c4 into dev Jun 7, 2025
13 checks passed
soda3000 pushed a commit to soda3000/skyrim-community-shaders that referenced this pull request Jun 8, 2025
davo0411 pushed a commit to davo0411/skyrim-community-shaders that referenced this pull request Jun 21, 2025
alandtse added a commit to alandtse/open-shaders that referenced this pull request Jul 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor repetitive hotkey handling code in ProcessInputEventQueue

3 participants