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): rebind keys at runtime #3422

Merged
merged 8 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
various cleanups
  • Loading branch information
imsnif committed Jun 13, 2024
commit b9e5ecbffc77f4171d95ae34d687843ac7b4fb08
1 change: 0 additions & 1 deletion default-plugins/status-bar/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ impl ZellijPlugin for State {
EventType::CopyToClipboard,
EventType::InputReceived,
EventType::SystemClipboardFailure,
EventType::Timer,
]);
}

Expand Down
2 changes: 1 addition & 1 deletion zellij-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::{
use zellij_utils::{
channels::{self, ChannelWithContext, SenderWithContext},
consts::{set_permissions, ZELLIJ_SOCK_DIR},
data::{ClientId, ConnectToSession, InputMode, KeyWithModifier, Style},
data::{ClientId, ConnectToSession, KeyWithModifier, Style},
envs,
errors::{ClientContext, ContextType, ErrorInstruction},
input::{config::Config, options::Options},
Expand Down
6 changes: 1 addition & 5 deletions zellij-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,7 @@ impl SessionMetaData {
}

}
pub fn rebind_keys(&mut self, client_id: ClientId, new_keybinds: String) -> Option<Keybinds> { // TODO:
// Result
pub fn rebind_keys(&mut self, client_id: ClientId, new_keybinds: String) -> Option<Keybinds> {
if let Some(current_keybinds) = self.client_keybinds.get_mut(&client_id) {
match Keybinds::from_string(new_keybinds, current_keybinds.clone(), &self.config_options) {
Ok(new_keybinds) => {
Expand Down Expand Up @@ -558,7 +557,6 @@ pub fn start_server(mut os_input: Box<dyn ServerOsApi>, socket_path: PathBuf) {
.unwrap();
let default_mode = options.default_mode.unwrap_or_default();
let mode_info = get_mode_info(default_mode, &attrs, session_data.capabilities);
let mode = mode_info.mode;
session_data
.senders
.send_to_screen(ScreenInstruction::ChangeMode(mode_info.clone(), client_id))
Expand Down Expand Up @@ -899,8 +897,6 @@ pub fn start_server(mut os_input: Box<dyn ServerOsApi>, socket_path: PathBuf) {
.change_mode_for_all_clients(input_mode);
},
ServerInstruction::RebindKeys(client_id, new_keybinds) => {
// TODO: CONTINUE HERE (12/06) - also update screen and its mode-info so that it
// sends an update to the plugins
let new_keybinds = session_data.write().unwrap().as_mut().unwrap().rebind_keys(client_id, new_keybinds).clone();
if let Some(new_keybinds) = new_keybinds {
session_data
Expand Down
1 change: 1 addition & 0 deletions zellij-server/src/plugins/zellij_exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1609,6 +1609,7 @@ fn check_command_permission(
| PluginCommand::CliPipeOutput(..) => PermissionType::ReadCliPipes,
PluginCommand::MessageToPlugin(..) => PermissionType::MessageAndLaunchOtherPlugins,
PluginCommand::DumpSessionLayout => PermissionType::ReadApplicationState,
PluginCommand::RebindKeys(..) => PermissionType::RebindKeys,
_ => return (PermissionStatus::Granted, None),
};

Expand Down
45 changes: 24 additions & 21 deletions zellij-server/src/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1001,29 +1001,32 @@ pub(crate) fn route_thread_main(
match instruction {
ClientToServerMsg::Key(key, raw_bytes, is_kitty_keyboard_protocol) => {
if let Some(rlocked_sessions) = rlocked_sessions.as_ref() {
if let Some((keybinds, input_mode)) = rlocked_sessions.get_client_keybinds_and_mode(&client_id) {
for action in keybinds.get_actions_for_key_in_mode_or_default_action(
&input_mode,
&key,
raw_bytes,
is_kitty_keyboard_protocol,
) {
if route_action(
action,
client_id,
None,
rlocked_sessions.senders.clone(),
rlocked_sessions.capabilities.clone(),
rlocked_sessions.client_attributes.clone(),
rlocked_sessions.default_shell.clone(),
rlocked_sessions.layout.clone(),
Some(&mut seen_cli_pipes),
)? {
should_break = true;
match rlocked_sessions.get_client_keybinds_and_mode(&client_id) {
Some((keybinds, input_mode)) => {
for action in keybinds.get_actions_for_key_in_mode_or_default_action(
&input_mode,
&key,
raw_bytes,
is_kitty_keyboard_protocol,
) {
if route_action(
action,
client_id,
None,
rlocked_sessions.senders.clone(),
rlocked_sessions.capabilities.clone(),
rlocked_sessions.client_attributes.clone(),
rlocked_sessions.default_shell.clone(),
rlocked_sessions.layout.clone(),
Some(&mut seen_cli_pipes),
)? {
should_break = true;
}
}
},
None => {
log::error!("Failed to get keybindings for client");
}
} else {
log::error!("Failed to get keybindings for client");
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions zellij-utils/assets/prost/api.plugin_permission.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub enum PermissionType {
WebAccess = 6,
ReadCliPipes = 7,
MessageAndLaunchOtherPlugins = 8,
RebindKeys = 9,
}
impl PermissionType {
/// String value of the enum field names used in the ProtoBuf definition.
Expand All @@ -29,6 +30,7 @@ impl PermissionType {
PermissionType::MessageAndLaunchOtherPlugins => {
"MessageAndLaunchOtherPlugins"
}
PermissionType::RebindKeys => "RebindKeys",
}
}
/// Creates an enum from field names used in the ProtoBuf definition.
Expand All @@ -43,6 +45,7 @@ impl PermissionType {
"WebAccess" => Some(Self::WebAccess),
"ReadCliPipes" => Some(Self::ReadCliPipes),
"MessageAndLaunchOtherPlugins" => Some(Self::MessageAndLaunchOtherPlugins),
"RebindKeys" => Some(Self::RebindKeys),
_ => None,
}
}
Expand Down
4 changes: 4 additions & 0 deletions zellij-utils/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,7 @@ pub enum Permission {
WebAccess,
ReadCliPipes,
MessageAndLaunchOtherPlugins,
RebindKeys,
}

impl PermissionType {
Expand All @@ -935,6 +936,9 @@ impl PermissionType {
PermissionType::MessageAndLaunchOtherPlugins => {
"Send messages to and launch other plugins".to_owned()
},
PermissionType::RebindKeys => {
"Rebind keys".to_owned()
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion zellij-utils/src/ipc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! IPC stuff for starting to split things into a client and server model.
use crate::{
cli::CliArgs,
data::{ClientId, ConnectToSession, InputMode, Style, KeyWithModifier},
data::{ClientId, ConnectToSession, Style, KeyWithModifier},
errors::{get_current_ctx, prelude::*, ErrorContext},
input::keybinds::Keybinds,
input::{actions::Action, layout::Layout, options::Options, plugins::PluginAliases},
Expand Down
9 changes: 5 additions & 4 deletions zellij-utils/src/kdl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1885,10 +1885,11 @@ impl Keybinds {
if let Some(kdl_keybinds) = document.get("keybinds") {
Keybinds::from_kdl(&kdl_keybinds, base_keybinds, config_options)
} else {
Err(ConfigError::DownloadError("Failed to parse kdl document".to_owned())) // TODO:
// better
// error
// type
Err(ConfigError::new_kdl_error(
format!("Could not find keybinds node"),
document.span().offset(),
document.span().len(),
))
}
}
}
Expand Down
1 change: 1 addition & 0 deletions zellij-utils/src/plugin_api/plugin_permission.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ enum PermissionType {
WebAccess = 6;
ReadCliPipes = 7;
MessageAndLaunchOtherPlugins = 8;
RebindKeys = 9;
}
2 changes: 2 additions & 0 deletions zellij-utils/src/plugin_api/plugin_permission.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ impl TryFrom<ProtobufPermissionType> for PermissionType {
ProtobufPermissionType::MessageAndLaunchOtherPlugins => {
Ok(PermissionType::MessageAndLaunchOtherPlugins)
},
ProtobufPermissionType::RebindKeys=> Ok(PermissionType::RebindKeys),
}
}
}
Expand All @@ -49,6 +50,7 @@ impl TryFrom<PermissionType> for ProtobufPermissionType {
PermissionType::MessageAndLaunchOtherPlugins => {
Ok(ProtobufPermissionType::MessageAndLaunchOtherPlugins)
},
PermissionType::RebindKeys => Ok(ProtobufPermissionType::RebindKeys),
}
}
}