#![cfg_attr(coverage_nightly, allow(unused_features))]
#![cfg_attr(coverage_nightly, feature(coverage_attribute))]
pub mod command;
pub mod ids;
use std::sync::Arc;
use {
reovim_driver_command::{CommandHandler, CommandHandlerStore, CommandProvider},
reovim_kernel::api::v1::{
EventResult, Module, ModuleContext, ModuleError, ModuleId, ProbeResult, Subscription,
Version,
events::kernel::{ViewportScrolled, WindowClosed, WindowCreated, WindowFocused, priority},
pr_info,
},
};
pub struct WindowOps {
subscriptions: Vec<Subscription>,
}
impl WindowOps {
#[must_use]
pub const fn new() -> Self {
Self {
subscriptions: Vec::new(),
}
}
}
impl Default for WindowOps {
fn default() -> Self {
Self::new()
}
}
impl Module for WindowOps {
fn id(&self) -> ModuleId {
ModuleId::new("window-ops")
}
fn name(&self) -> &'static str {
"Window Operations"
}
fn version(&self) -> Version {
Version::new(0, 1, 0)
}
#[cfg_attr(coverage_nightly, coverage(off))]
fn init(&mut self, ctx: &ModuleContext) -> ProbeResult {
let command_store = ctx.services.get_or_create::<CommandHandlerStore>();
for handler in self.command_handlers() {
command_store.add(handler);
}
let bus = Arc::clone(&ctx.kernel.event_bus);
let sub_created =
bus.subscribe_with_context::<WindowCreated, _>(priority::CORE, |event, _ctx| {
pr_info!("Window {} created", event.window_id);
EventResult::Handled
});
sub_created.detach();
self.subscriptions.push(sub_created);
let sub_closed =
bus.subscribe_with_context::<WindowClosed, _>(priority::LOW, |event, _ctx| {
pr_info!("Window {} closed", event.window_id);
EventResult::Handled
});
sub_closed.detach();
self.subscriptions.push(sub_closed);
let sub_focused =
bus.subscribe_with_context::<WindowFocused, _>(priority::CORE, |event, _ctx| {
pr_info!("Window focus: {:?} -> {}", event.from, event.to);
EventResult::Handled
});
sub_focused.detach();
self.subscriptions.push(sub_focused);
let sub_scrolled =
bus.subscribe_with_context::<ViewportScrolled, _>(priority::NORMAL, |event, _ctx| {
pr_info!(
"Window {} viewport scrolled: buffer={}, lines {}..{}",
event.window_id,
event.buffer_id,
event.top_line,
event.bottom_line
);
EventResult::Handled
});
sub_scrolled.detach();
self.subscriptions.push(sub_scrolled);
pr_info!(
"WindowOps module initialized with {} commands and {} subscriptions",
command::all_commands().len(),
self.subscriptions.len()
);
ProbeResult::Success
}
fn exit(&mut self) -> Result<(), ModuleError> {
let count = self.subscriptions.len();
self.subscriptions.clear();
pr_info!("WindowOps module exiting, cleared {} subscriptions", count);
Ok(())
}
}
impl CommandProvider for WindowOps {
fn command_handlers(&self) -> Vec<Box<dyn CommandHandler>> {
command::all_commands()
}
}
#[cfg(feature = "dynamic")]
reovim_module_macros::declare_module!(WindowOps);
#[cfg(test)]
#[path = "lib_tests.rs"]
mod tests;