:feat: Add hooks for mode and layout changes

pull/560/head
emanuel 1 year ago committed by Arijit Basu
parent 078da205ca
commit d6e33e68e3

@ -381,8 +381,8 @@ impl App {
self.msg_out.push_back(MsgOut::Enque(task));
self
}
pub fn handle_batch_external_msgs(mut self, msgs: Vec<ExternalMsg>) -> Result<Self> {
pub fn handle_batch_external_msgs(self, msgs: Vec<ExternalMsg>) -> Result<Self> {
for task in msgs
.into_iter()
.map(|msg| Task::new(MsgIn::External(msg), None))
@ -1112,6 +1112,13 @@ impl App {
self.config.general.read_only,
self.config.general.global_key_bindings.to_owned(),
);
// Hooks
if !self.hooks.on_mode_switch.is_empty() {
let msgs = self.hooks.on_mode_switch.clone();
self = self.handle_batch_external_msgs(msgs)?
}
Ok(self)
} else {
self.log_error(format!("Mode not found: {}", mode))
@ -1130,6 +1137,13 @@ impl App {
self.config.general.read_only,
self.config.general.global_key_bindings.to_owned(),
);
// Hooks
if !self.hooks.on_mode_switch.is_empty() {
let msgs = self.hooks.on_mode_switch.clone();
self = self.handle_batch_external_msgs(msgs)?
}
Ok(self)
} else {
self.log_error(format!("Builtin mode not found: {}", mode))
@ -1148,6 +1162,13 @@ impl App {
self.config.general.read_only,
self.config.general.global_key_bindings.to_owned(),
);
// Hooks
if !self.hooks.on_mode_switch.is_empty() {
let msgs = self.hooks.on_mode_switch.clone();
self = self.handle_batch_external_msgs(msgs)?
}
Ok(self)
} else {
self.log_error(format!("Custom mode not found: {}", mode))
@ -1157,6 +1178,13 @@ impl App {
fn switch_layout(mut self, layout: &str) -> Result<Self> {
if let Some(l) = self.config.layouts.get(layout) {
self.layout = l.to_owned();
// Hooks
if !self.hooks.on_layout_switch.is_empty() {
let msgs = self.hooks.on_layout_switch.clone();
self = self.handle_batch_external_msgs(msgs)?
}
Ok(self)
} else {
self.log_error(format!("Layout not found: {}", layout))
@ -1166,6 +1194,13 @@ impl App {
fn switch_layout_builtin(mut self, layout: &str) -> Result<Self> {
if let Some(l) = self.config.layouts.builtin.get(layout) {
self.layout = l.to_owned();
// Hooks
if !self.hooks.on_layout_switch.is_empty() {
let msgs = self.hooks.on_layout_switch.clone();
self = self.handle_batch_external_msgs(msgs)?
}
Ok(self)
} else {
self.log_error(format!("Builtin layout not found: {}", layout))
@ -1175,6 +1210,13 @@ impl App {
fn switch_layout_custom(mut self, layout: &str) -> Result<Self> {
if let Some(l) = self.config.layouts.get_custom(layout) {
self.layout = l.to_owned();
// Hooks
if !self.hooks.on_layout_switch.is_empty() {
let msgs = self.hooks.on_layout_switch.clone();
self = self.handle_batch_external_msgs(msgs)?
}
Ok(self)
} else {
self.log_error(format!("Custom layout not found: {}", layout))

@ -624,6 +624,12 @@ pub struct Hooks {
#[serde(default)]
pub on_focus_change: Vec<ExternalMsg>,
#[serde(default)]
pub on_mode_switch: Vec<ExternalMsg>,
#[serde(default)]
pub on_layout_switch: Vec<ExternalMsg>,
// TODO After cleanup or Runner::run
// #[serde(default)]
// pub before_quit: Vec<ExternalMsg>,
@ -634,6 +640,8 @@ impl Hooks {
self.on_load.extend(other.on_load);
self.on_directory_change.extend(other.on_directory_change);
self.on_focus_change.extend(other.on_focus_change);
self.on_mode_switch.extend(other.on_mode_switch);
self.on_layout_switch.extend(other.on_layout_switch);
self
}
}

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save