feat(plugins): add api to close current plugin instance (#3228)

This commit is contained in:
Aram Drevekenin 2024-03-27 20:24:18 +01:00 committed by GitHub
parent c83b6cc38d
commit 9d2e7fe2c1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 38 additions and 0 deletions

View file

@ -264,6 +264,7 @@ fn host_run_plugin_command(env: FunctionEnvMut<ForeignFunctionEnv>) {
}, },
PluginCommand::WatchFilesystem => watch_filesystem(env), PluginCommand::WatchFilesystem => watch_filesystem(env),
PluginCommand::DumpSessionLayout => dump_session_layout(env), PluginCommand::DumpSessionLayout => dump_session_layout(env),
PluginCommand::CloseSelf => close_self(env),
}, },
(PermissionStatus::Denied, permission) => { (PermissionStatus::Denied, permission) => {
log::error!( log::error!(
@ -813,6 +814,22 @@ fn show_self(env: &ForeignFunctionEnv, should_float_if_hidden: bool) {
apply_action!(action, error_msg, env); apply_action!(action, error_msg, env);
} }
fn close_self(env: &ForeignFunctionEnv) {
env.plugin_env
.senders
.send_to_screen(ScreenInstruction::ClosePane(
PaneId::Plugin(env.plugin_env.plugin_id),
None,
))
.with_context(|| format!("failed to close self"))
.non_fatal();
env.plugin_env
.senders
.send_to_plugin(PluginInstruction::Unload(env.plugin_env.plugin_id))
.with_context(|| format!("failed to close self"))
.non_fatal();
}
fn switch_to_mode(env: &ForeignFunctionEnv, input_mode: InputMode) { fn switch_to_mode(env: &ForeignFunctionEnv, input_mode: InputMode) {
let action = Action::SwitchToMode(input_mode); let action = Action::SwitchToMode(input_mode);
let error_msg = || { let error_msg = || {

View file

@ -249,6 +249,14 @@ pub fn show_self(should_float_if_hidden: bool) {
unsafe { host_run_plugin_command() }; unsafe { host_run_plugin_command() };
} }
/// Close this plugin pane
pub fn close_self() {
let plugin_command = PluginCommand::CloseSelf;
let protobuf_plugin_command: ProtobufPluginCommand = plugin_command.try_into().unwrap();
object_to_stdout(&protobuf_plugin_command.encode_to_vec());
unsafe { host_run_plugin_command() };
}
/// Switch to the specified Input Mode (eg. `Normal`, `Tab`, `Pane`) /// Switch to the specified Input Mode (eg. `Normal`, `Tab`, `Pane`)
pub fn switch_to_input_mode(mode: &InputMode) { pub fn switch_to_input_mode(mode: &InputMode) {
let plugin_command = PluginCommand::SwitchToMode(*mode); let plugin_command = PluginCommand::SwitchToMode(*mode);

View file

@ -421,6 +421,7 @@ pub enum CommandName {
ScanHostFolder = 82, ScanHostFolder = 82,
WatchFilesystem = 83, WatchFilesystem = 83,
DumpSessionLayout = 84, DumpSessionLayout = 84,
CloseSelf = 85,
} }
impl CommandName { impl CommandName {
/// String value of the enum field names used in the ProtoBuf definition. /// String value of the enum field names used in the ProtoBuf definition.
@ -514,6 +515,7 @@ impl CommandName {
CommandName::ScanHostFolder => "ScanHostFolder", CommandName::ScanHostFolder => "ScanHostFolder",
CommandName::WatchFilesystem => "WatchFilesystem", CommandName::WatchFilesystem => "WatchFilesystem",
CommandName::DumpSessionLayout => "DumpSessionLayout", CommandName::DumpSessionLayout => "DumpSessionLayout",
CommandName::CloseSelf => "CloseSelf",
} }
} }
/// Creates an enum from field names used in the ProtoBuf definition. /// Creates an enum from field names used in the ProtoBuf definition.
@ -604,6 +606,7 @@ impl CommandName {
"ScanHostFolder" => Some(Self::ScanHostFolder), "ScanHostFolder" => Some(Self::ScanHostFolder),
"WatchFilesystem" => Some(Self::WatchFilesystem), "WatchFilesystem" => Some(Self::WatchFilesystem),
"DumpSessionLayout" => Some(Self::DumpSessionLayout), "DumpSessionLayout" => Some(Self::DumpSessionLayout),
"CloseSelf" => Some(Self::CloseSelf),
_ => None, _ => None,
} }
} }

View file

@ -1379,4 +1379,5 @@ pub enum PluginCommand {
ScanHostFolder(PathBuf), // TODO: rename to ScanHostFolder ScanHostFolder(PathBuf), // TODO: rename to ScanHostFolder
WatchFilesystem, WatchFilesystem,
DumpSessionLayout, DumpSessionLayout,
CloseSelf,
} }

View file

@ -96,6 +96,7 @@ enum CommandName {
ScanHostFolder = 82; ScanHostFolder = 82;
WatchFilesystem = 83; WatchFilesystem = 83;
DumpSessionLayout = 84; DumpSessionLayout = 84;
CloseSelf = 85;
} }
message PluginCommand { message PluginCommand {

View file

@ -871,6 +871,10 @@ impl TryFrom<ProtobufPluginCommand> for PluginCommand {
Some(_) => Err("DumpSessionLayout should have no payload, found a payload"), Some(_) => Err("DumpSessionLayout should have no payload, found a payload"),
None => Ok(PluginCommand::DumpSessionLayout), None => Ok(PluginCommand::DumpSessionLayout),
}, },
Some(CommandName::CloseSelf) => match protobuf_plugin_command.payload {
Some(_) => Err("CloseSelf should have no payload, found a payload"),
None => Ok(PluginCommand::CloseSelf),
},
None => Err("Unrecognized plugin command"), None => Err("Unrecognized plugin command"),
} }
} }
@ -1389,6 +1393,10 @@ impl TryFrom<PluginCommand> for ProtobufPluginCommand {
name: CommandName::DumpSessionLayout as i32, name: CommandName::DumpSessionLayout as i32,
payload: None, payload: None,
}), }),
PluginCommand::CloseSelf => Ok(ProtobufPluginCommand {
name: CommandName::CloseSelf as i32,
payload: None,
}),
} }
} }
} }