Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(plugins): API to temporarily bind keys to send a message to a specific plugin id #3561

Merged
merged 1 commit into from
Aug 21, 2024

Conversation

imsnif
Copy link
Member

@imsnif imsnif commented Aug 21, 2024

This adds the MessagePluginId bindable keybind action. Similar to MessagePlugin, this will create a pipe that will send a message to a specific plugin - only here it will be done by this plugin's runtime ID.

The purpose is to use this with the reconfigure plugin API to essentially allow plugins to temporarily bind global keys in the session to send messages to themselves.

Example:

let own_plugin_id = get_plugin_ids().plugin_id;
let save_configuration_to_file = false;
reconfigure(format!(r#"
    keybinds {{
        shared {{
            bind "F1" {{
                MessagePluginId {own_plugin_id}  {{
                    name "F1"
                }}
            }}
            bind "F2" {{
                MessagePluginId {own_plugin_id} {{
                    name "F2"
                }}
            }}
            bind "F3" {{
                MessagePluginId {own_plugin_id} {{
                    name "F3"
                }}
            }}
        }}
    }}
"#), save_configuration_to_file);
// ...
impl State {
    fn pipe(&mut self, pipe_message: PipeMessage) -> bool {
        let mut should_render = false;
        if pipe_message.name == "F1" {
            eprintln!("F1 key pressed globally");
        }
        if pipe_message.name == "F2" {
            eprintln!("F2 key pressed globally");
        }
        if pipe_message.name == "F3" {
            eprintln!("F3 key pressed globally");
        }
        should_render
    }
}

Note: this sort of keybind action should only be used in this case. It is not serialized with the user's config and should not be trusted to persist across sessions. If placed statically in a configuration it might have unintended consequences (seeing as plugin ids are assigned at runtime).

@Lockszmith-GH
Copy link

Note: this sort of keybind action should only be used in this case. It is not serialized with the user's config and should not be trusted to persist across sessions. If placed statically in a configuration it might have unintended consequences (seeing as plugin ids are assigned at runtime).

Just asking for clarification about this:
Does this means that if a plugin wants to allow it's users to have configuratble keybindings - it should manage it's own configuration and assign keybindings through this new mechanism?

@imsnif
Copy link
Member Author

imsnif commented Aug 21, 2024

Just asking for clarification about this: Does this means that if a plugin wants to allow it's users to have configuratble keybindings - it should manage it's own configuration and assign keybindings through this new mechanism?

I think it depends on what the plugin wants to do. This mechanism is best used if the plugin wants to assign/change keys at runtime. Otherwise the plugin can use the "classic" plugin configuration (either through layouts, cli or pipes).

@imsnif imsnif merged commit 08b6072 into main Aug 21, 2024
6 checks passed
Tomcat-42 pushed a commit to Tomcat-42/zellij that referenced this pull request Nov 9, 2024
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.

2 participants