From 1255bcb8a311c7f382f0bf66bbe791c6dfd77b30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Sun, 9 May 2021 18:02:31 +0900 Subject: [PATCH] Simplify the compositor callback. --- helix-term/src/commands.rs | 6 +++--- helix-term/src/compositor.rs | 6 +++--- helix-term/src/ui/menu.rs | 10 ++++------ helix-term/src/ui/picker.rs | 10 ++++------ helix-term/src/ui/popup.rs | 10 ++++------ helix-term/src/ui/prompt.rs | 10 ++++------ 6 files changed, 22 insertions(+), 30 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index fe160ab84..e26dbbaad 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -63,9 +63,9 @@ impl<'a> Context<'a> { /// Push a new component onto the compositor. pub fn push_layer(&mut self, mut component: Box) { - self.callback = Some(Box::new( - |compositor: &mut Compositor, editor: &mut Editor| compositor.push(component), - )); + self.callback = Some(Box::new(|compositor: &mut Compositor| { + compositor.push(component) + })); } #[inline] diff --git a/helix-term/src/compositor.rs b/helix-term/src/compositor.rs index 7b8283585..3a17904d6 100644 --- a/helix-term/src/compositor.rs +++ b/helix-term/src/compositor.rs @@ -6,7 +6,7 @@ use crossterm::event::Event; use helix_core::Position; use tui::{buffer::Buffer as Surface, layout::Rect}; -pub type Callback = Box; +pub type Callback = Box; // --> EventResult should have a callback that takes a context with methods like .popup(), // .prompt() etc. That way we can abstract it from the renderer. @@ -22,7 +22,7 @@ pub enum EventResult { Consumed(Option), } -use helix_view::{Editor, View}; +use helix_view::Editor; use crate::application::LspCallbacks; @@ -111,7 +111,7 @@ impl Compositor { for layer in self.layers.iter_mut().rev() { match layer.handle_event(event, cx) { EventResult::Consumed(Some(callback)) => { - callback(self, cx.editor); + callback(self); return true; } EventResult::Consumed(None) => return true, diff --git a/helix-term/src/ui/menu.rs b/helix-term/src/ui/menu.rs index d055d0d08..f9bfdd352 100644 --- a/helix-term/src/ui/menu.rs +++ b/helix-term/src/ui/menu.rs @@ -142,12 +142,10 @@ impl Component for Menu { _ => return EventResult::Ignored, }; - let close_fn = EventResult::Consumed(Some(Box::new( - |compositor: &mut Compositor, editor: &mut Editor| { - // remove the layer - compositor.pop(); - }, - ))); + let close_fn = EventResult::Consumed(Some(Box::new(|compositor: &mut Compositor| { + // remove the layer + compositor.pop(); + }))); match event { // esc or ctrl-c aborts the completion and closes the menu diff --git a/helix-term/src/ui/picker.rs b/helix-term/src/ui/picker.rs index 73cbf07f4..6d77bf1d4 100644 --- a/helix-term/src/ui/picker.rs +++ b/helix-term/src/ui/picker.rs @@ -126,12 +126,10 @@ impl Component for Picker { _ => return EventResult::Ignored, }; - let close_fn = EventResult::Consumed(Some(Box::new( - |compositor: &mut Compositor, editor: &mut Editor| { - // remove the layer - compositor.pop(); - }, - ))); + let close_fn = EventResult::Consumed(Some(Box::new(|compositor: &mut Compositor| { + // remove the layer + compositor.pop(); + }))); match key_event { KeyEvent { diff --git a/helix-term/src/ui/popup.rs b/helix-term/src/ui/popup.rs index 44e79c4f0..7ce7166c4 100644 --- a/helix-term/src/ui/popup.rs +++ b/helix-term/src/ui/popup.rs @@ -66,12 +66,10 @@ impl Component for Popup { _ => return EventResult::Ignored, }; - let close_fn = EventResult::Consumed(Some(Box::new( - |compositor: &mut Compositor, editor: &mut Editor| { - // remove the layer - compositor.pop(); - }, - ))); + let close_fn = EventResult::Consumed(Some(Box::new(|compositor: &mut Compositor| { + // remove the layer + compositor.pop(); + }))); match key { // esc or ctrl-c aborts the completion and closes the menu diff --git a/helix-term/src/ui/prompt.rs b/helix-term/src/ui/prompt.rs index 0c2c1d8a4..b09b8e140 100644 --- a/helix-term/src/ui/prompt.rs +++ b/helix-term/src/ui/prompt.rs @@ -211,12 +211,10 @@ impl Component for Prompt { _ => return EventResult::Ignored, }; - let close_fn = EventResult::Consumed(Some(Box::new( - |compositor: &mut Compositor, editor: &mut Editor| { - // remove the layer - compositor.pop(); - }, - ))); + let close_fn = EventResult::Consumed(Some(Box::new(|compositor: &mut Compositor| { + // remove the layer + compositor.pop(); + }))); match event { // char or shift char