on master help sign in
Simplify the compositor callback.
1255bcbBlaž Hrastnik
helix-term/src/commands.rs+3−3
@@ -63,9 +63,9 @@ impl<'a> Context<'a> {
63
63
64 /// Push a new component onto the compositor.
64 /// Push a new component onto the compositor.
65 pub fn push_layer(&mut self, mut component: Box<dyn Component>) {
65 pub fn push_layer(&mut self, mut component: Box<dyn Component>) {
66− self.callback = Some(Box::new(
66+ self.callback = Some(Box::new(|compositor: &mut Compositor| {
67− |compositor: &mut Compositor, editor: &mut Editor| compositor.push(component),
67+ compositor.push(component)
68− ));
68+ }));
69 }
69 }
70
70
71 #[inline]
71 #[inline]
helix-term/src/compositor.rs+3−3
@@ -6,7 +6,7 @@ use crossterm::event::Event;
6use helix_core::Position;
6use helix_core::Position;
7use tui::{buffer::Buffer as Surface, layout::Rect};
7use tui::{buffer::Buffer as Surface, layout::Rect};
8
8
9−pub type Callback = Box<dyn FnOnce(&mut Compositor, &mut Editor)>;
9+pub type Callback = Box<dyn FnOnce(&mut Compositor)>;
10
10
11// --> EventResult should have a callback that takes a context with methods like .popup(),
11// --> EventResult should have a callback that takes a context with methods like .popup(),
12// .prompt() etc. That way we can abstract it from the renderer.
12// .prompt() etc. That way we can abstract it from the renderer.
@@ -22,7 +22,7 @@ pub enum EventResult {
22 Consumed(Option<Callback>),
22 Consumed(Option<Callback>),
23}
23}
24
24
25−use helix_view::{Editor, View};
25+use helix_view::Editor;
26
26
27use crate::application::LspCallbacks;
27use crate::application::LspCallbacks;
28
28
@@ -111,7 +111,7 @@ impl Compositor {
111 for layer in self.layers.iter_mut().rev() {
111 for layer in self.layers.iter_mut().rev() {
112 match layer.handle_event(event, cx) {
112 match layer.handle_event(event, cx) {
113 EventResult::Consumed(Some(callback)) => {
113 EventResult::Consumed(Some(callback)) => {
114− callback(self, cx.editor);
114+ callback(self);
115 return true;
115 return true;
116 }
116 }
117 EventResult::Consumed(None) => return true,
117 EventResult::Consumed(None) => return true,
helix-term/src/ui/menu.rs+4−6
@@ -142,12 +142,10 @@ impl<T: 'static> Component for Menu<T> {
142 _ => return EventResult::Ignored,
142 _ => return EventResult::Ignored,
143 };
143 };
144
144
145− let close_fn = EventResult::Consumed(Some(Box::new(
145+ let close_fn = EventResult::Consumed(Some(Box::new(|compositor: &mut Compositor| {
146− |compositor: &mut Compositor, editor: &mut Editor| {
147− // remove the layer
146+ // remove the layer
148− compositor.pop();
147+ compositor.pop();
149− },
150− )));
148+ })));
151
149
152 match event {
150 match event {
153 // esc or ctrl-c aborts the completion and closes the menu
151 // esc or ctrl-c aborts the completion and closes the menu
helix-term/src/ui/picker.rs+4−6
@@ -126,12 +126,10 @@ impl<T: 'static> Component for Picker<T> {
126 _ => return EventResult::Ignored,
126 _ => return EventResult::Ignored,
127 };
127 };
128
128
129− let close_fn = EventResult::Consumed(Some(Box::new(
129+ let close_fn = EventResult::Consumed(Some(Box::new(|compositor: &mut Compositor| {
130− |compositor: &mut Compositor, editor: &mut Editor| {
131− // remove the layer
130+ // remove the layer
132− compositor.pop();
131+ compositor.pop();
133− },
134− )));
132+ })));
135
133
136 match key_event {
134 match key_event {
137 KeyEvent {
135 KeyEvent {
helix-term/src/ui/popup.rs+4−6
@@ -66,12 +66,10 @@ impl<T: Component> Component for Popup<T> {
66 _ => return EventResult::Ignored,
66 _ => return EventResult::Ignored,
67 };
67 };
68
68
69− let close_fn = EventResult::Consumed(Some(Box::new(
69+ let close_fn = EventResult::Consumed(Some(Box::new(|compositor: &mut Compositor| {
70− |compositor: &mut Compositor, editor: &mut Editor| {
71− // remove the layer
70+ // remove the layer
72− compositor.pop();
71+ compositor.pop();
73− },
74− )));
72+ })));
75
73
76 match key {
74 match key {
77 // esc or ctrl-c aborts the completion and closes the menu
75 // esc or ctrl-c aborts the completion and closes the menu
helix-term/src/ui/prompt.rs+4−6
@@ -211,12 +211,10 @@ impl Component for Prompt {
211 _ => return EventResult::Ignored,
211 _ => return EventResult::Ignored,
212 };
212 };
213
213
214− let close_fn = EventResult::Consumed(Some(Box::new(
214+ let close_fn = EventResult::Consumed(Some(Box::new(|compositor: &mut Compositor| {
215− |compositor: &mut Compositor, editor: &mut Editor| {
216− // remove the layer
215+ // remove the layer
217− compositor.pop();
216+ compositor.pop();
218− },
219− )));
217+ })));
220
218
221 match event {
219 match event {
222 // char or shift char
220 // char or shift char