fix(plugin): send mode updates to subscribed plugins on load

This commit is contained in:
Brooks J Rady 2021-03-31 12:13:00 +01:00
parent d818661c72
commit 138ba850eb
5 changed files with 34 additions and 25 deletions

View file

@ -15,7 +15,7 @@ use std::{
collections::{BTreeMap, HashSet}, collections::{BTreeMap, HashSet},
}; };
use std::{io::Write, sync::mpsc::channel}; use std::{io::Write, sync::mpsc::channel};
use zellij_tile::data::{Event, InputMode}; use zellij_tile::data::{Event, ModeInfo};
const CURSOR_HEIGHT_WIDTH_RATIO: usize = 4; // this is not accurate and kind of a magic number, TODO: look into this const CURSOR_HEIGHT_WIDTH_RATIO: usize = 4; // this is not accurate and kind of a magic number, TODO: look into this
const MIN_TERMINAL_HEIGHT: usize = 2; const MIN_TERMINAL_HEIGHT: usize = 2;
@ -66,7 +66,7 @@ pub struct Tab {
pub send_plugin_instructions: SenderWithContext<PluginInstruction>, pub send_plugin_instructions: SenderWithContext<PluginInstruction>,
pub send_app_instructions: SenderWithContext<AppInstruction>, pub send_app_instructions: SenderWithContext<AppInstruction>,
expansion_boundary: Option<PositionAndSize>, expansion_boundary: Option<PositionAndSize>,
pub input_mode: InputMode, pub mode_info: ModeInfo,
} }
// FIXME: Use a struct that has a pane_type enum, to reduce all of the duplication // FIXME: Use a struct that has a pane_type enum, to reduce all of the duplication
@ -183,7 +183,7 @@ impl Tab {
send_app_instructions: SenderWithContext<AppInstruction>, send_app_instructions: SenderWithContext<AppInstruction>,
max_panes: Option<usize>, max_panes: Option<usize>,
pane_id: Option<PaneId>, pane_id: Option<PaneId>,
input_mode: InputMode, mode_info: ModeInfo,
) -> Self { ) -> Self {
let panes = if let Some(PaneId::Terminal(pid)) = pane_id { let panes = if let Some(PaneId::Terminal(pid)) = pane_id {
let new_terminal = TerminalPane::new(pid, *full_screen_ws); let new_terminal = TerminalPane::new(pid, *full_screen_ws);
@ -213,7 +213,7 @@ impl Tab {
send_pty_instructions, send_pty_instructions,
send_plugin_instructions, send_plugin_instructions,
expansion_boundary: None, expansion_boundary: None,
input_mode, mode_info,
} }
} }
@ -267,6 +267,13 @@ impl Tab {
self.send_plugin_instructions.clone(), self.send_plugin_instructions.clone(),
); );
self.panes.insert(PaneId::Plugin(pid), Box::new(new_plugin)); self.panes.insert(PaneId::Plugin(pid), Box::new(new_plugin));
// Send an initial mode update to the newly loaded plugin only!
self.send_plugin_instructions
.send(PluginInstruction::Update(
Some(pid),
Event::ModeUpdate(self.mode_info.clone()),
))
.unwrap();
} else { } else {
// there are still panes left to fill, use the pids we received in this method // there are still panes left to fill, use the pids we received in this method
let pid = new_pids.next().unwrap(); // if this crashes it means we got less pids than there are panes in this layout let pid = new_pids.next().unwrap(); // if this crashes it means we got less pids than there are panes in this layout
@ -626,10 +633,12 @@ impl Tab {
for (kind, terminal) in self.panes.iter_mut() { for (kind, terminal) in self.panes.iter_mut() {
if !self.panes_to_hide.contains(&terminal.pid()) { if !self.panes_to_hide.contains(&terminal.pid()) {
match self.active_terminal.unwrap() == terminal.pid() { match self.active_terminal.unwrap() == terminal.pid() {
true => { true => boundaries.add_rect(
boundaries.add_rect(terminal.as_ref(), self.input_mode, Some(colors::GREEN)) terminal.as_ref(),
} self.mode_info.mode,
false => boundaries.add_rect(terminal.as_ref(), self.input_mode, None), Some(colors::GREEN),
),
false => boundaries.add_rect(terminal.as_ref(), self.mode_info.mode, None),
} }
if let Some(vte_output) = terminal.render() { if let Some(vte_output) = terminal.render() {
let vte_output = if let PaneId::Terminal(_) = kind { let vte_output = if let PaneId::Terminal(_) = kind {

View file

@ -200,7 +200,7 @@ pub enum ScreenContext {
CloseTab, CloseTab,
GoToTab, GoToTab,
UpdateTabName, UpdateTabName,
ChangeInputMode, ChangeMode,
} }
// FIXME: Just deriving EnumDiscriminants from strum will remove the need for any of this!!! // FIXME: Just deriving EnumDiscriminants from strum will remove the need for any of this!!!
@ -241,7 +241,7 @@ impl From<&ScreenInstruction> for ScreenContext {
ScreenInstruction::CloseTab => ScreenContext::CloseTab, ScreenInstruction::CloseTab => ScreenContext::CloseTab,
ScreenInstruction::GoToTab(_) => ScreenContext::GoToTab, ScreenInstruction::GoToTab(_) => ScreenContext::GoToTab,
ScreenInstruction::UpdateTabName(_) => ScreenContext::UpdateTabName, ScreenInstruction::UpdateTabName(_) => ScreenContext::UpdateTabName,
ScreenInstruction::ChangeInputMode(_) => ScreenContext::ChangeInputMode, ScreenInstruction::ChangeMode(_) => ScreenContext::ChangeMode,
} }
} }
} }

View file

@ -130,7 +130,7 @@ impl InputHandler {
)) ))
.unwrap(); .unwrap();
self.send_screen_instructions self.send_screen_instructions
.send(ScreenInstruction::ChangeInputMode(mode)) .send(ScreenInstruction::ChangeMode(get_mode_info(mode)))
.unwrap(); .unwrap();
self.send_screen_instructions self.send_screen_instructions
.send(ScreenInstruction::Render) .send(ScreenInstruction::Render)

View file

@ -37,7 +37,7 @@ use wasm_vm::PluginEnv;
use wasm_vm::{wasi_stdout, wasi_write_string, zellij_imports, PluginInstruction}; use wasm_vm::{wasi_stdout, wasi_write_string, zellij_imports, PluginInstruction};
use wasmer::{ChainableNamedResolver, Instance, Module, Store, Value}; use wasmer::{ChainableNamedResolver, Instance, Module, Store, Value};
use wasmer_wasi::{Pipe, WasiState}; use wasmer_wasi::{Pipe, WasiState};
use zellij_tile::data::{EventType, InputMode}; use zellij_tile::data::{EventType, ModeInfo};
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub enum ApiCommand { pub enum ApiCommand {
@ -256,7 +256,7 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
&full_screen_ws, &full_screen_ws,
os_input, os_input,
max_panes, max_panes,
InputMode::Normal, ModeInfo::default(),
); );
loop { loop {
let (event, mut err_ctx) = screen let (event, mut err_ctx) = screen
@ -389,8 +389,8 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
ScreenInstruction::UpdateTabName(c) => { ScreenInstruction::UpdateTabName(c) => {
screen.update_active_tab_name(c); screen.update_active_tab_name(c);
} }
ScreenInstruction::ChangeInputMode(input_mode) => { ScreenInstruction::ChangeMode(mode_info) => {
screen.change_input_mode(input_mode); screen.change_mode(mode_info);
} }
ScreenInstruction::Quit => { ScreenInstruction::Quit => {
break; break;

View file

@ -13,7 +13,7 @@ use crate::tab::Tab;
use crate::{errors::ErrorContext, wasm_vm::PluginInstruction}; use crate::{errors::ErrorContext, wasm_vm::PluginInstruction};
use crate::{layout::Layout, panes::PaneId}; use crate::{layout::Layout, panes::PaneId};
use zellij_tile::data::{Event, InputMode, TabInfo}; use zellij_tile::data::{Event, ModeInfo, TabInfo};
/// Instructions that can be sent to the [`Screen`]. /// Instructions that can be sent to the [`Screen`].
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -50,7 +50,7 @@ pub enum ScreenInstruction {
CloseTab, CloseTab,
GoToTab(u32), GoToTab(u32),
UpdateTabName(Vec<u8>), UpdateTabName(Vec<u8>),
ChangeInputMode(InputMode), ChangeMode(ModeInfo),
} }
/// A [`Screen`] holds multiple [`Tab`]s, each one holding multiple [`panes`](crate::client::panes). /// A [`Screen`] holds multiple [`Tab`]s, each one holding multiple [`panes`](crate::client::panes).
@ -74,7 +74,7 @@ pub struct Screen {
active_tab_index: Option<usize>, active_tab_index: Option<usize>,
/// The [`OsApi`] this [`Screen`] uses. /// The [`OsApi`] this [`Screen`] uses.
os_api: Box<dyn OsApi>, os_api: Box<dyn OsApi>,
input_mode: InputMode, mode_info: ModeInfo,
} }
impl Screen { impl Screen {
@ -87,7 +87,7 @@ impl Screen {
full_screen_ws: &PositionAndSize, full_screen_ws: &PositionAndSize,
os_api: Box<dyn OsApi>, os_api: Box<dyn OsApi>,
max_panes: Option<usize>, max_panes: Option<usize>,
input_mode: InputMode, mode_info: ModeInfo,
) -> Self { ) -> Self {
Screen { Screen {
receiver: receive_screen_instructions, receiver: receive_screen_instructions,
@ -99,7 +99,7 @@ impl Screen {
active_tab_index: None, active_tab_index: None,
tabs: BTreeMap::new(), tabs: BTreeMap::new(),
os_api, os_api,
input_mode, mode_info,
} }
} }
@ -119,7 +119,7 @@ impl Screen {
self.send_app_instructions.clone(), self.send_app_instructions.clone(),
self.max_panes, self.max_panes,
Some(PaneId::Terminal(pane_id)), Some(PaneId::Terminal(pane_id)),
self.input_mode, self.mode_info.clone(),
); );
self.active_tab_index = Some(tab_index); self.active_tab_index = Some(tab_index);
self.tabs.insert(tab_index, tab); self.tabs.insert(tab_index, tab);
@ -261,7 +261,7 @@ impl Screen {
self.send_app_instructions.clone(), self.send_app_instructions.clone(),
self.max_panes, self.max_panes,
None, None,
self.input_mode, self.mode_info.clone(),
); );
tab.apply_layout(layout, new_pids); tab.apply_layout(layout, new_pids);
self.active_tab_index = Some(tab_index); self.active_tab_index = Some(tab_index);
@ -301,10 +301,10 @@ impl Screen {
} }
self.update_tabs(); self.update_tabs();
} }
pub fn change_input_mode(&mut self, input_mode: InputMode) { pub fn change_mode(&mut self, mode_info: ModeInfo) {
self.input_mode = input_mode; self.mode_info = mode_info;
for tab in self.tabs.values_mut() { for tab in self.tabs.values_mut() {
tab.input_mode = self.input_mode; tab.mode_info = self.mode_info.clone();
} }
} }
} }