From b029083441bcb130762467e14f931d206498d489 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Thu, 9 Nov 2023 19:03:10 +0100 Subject: [PATCH] Start on rendering context menu in editor2 Co-Authored-By: Nathan Co-Authored-By: Mikayla --- crates/editor2/src/editor.rs | 59 ++++++++--------- crates/editor2/src/element.rs | 118 +++++++++++++++++----------------- crates/gpui2/src/window.rs | 22 ++++--- 3 files changed, 100 insertions(+), 99 deletions(-) diff --git a/crates/editor2/src/editor.rs b/crates/editor2/src/editor.rs index 752696bcaef3752a8acf0df0516c1ed742978a9e..11ec3e0879c0c0f2a4071e76e0949ab869bc9927 100644 --- a/crates/editor2/src/editor.rs +++ b/crates/editor2/src/editor.rs @@ -920,15 +920,14 @@ impl ContextMenu { fn render( &self, cursor_position: DisplayPoint, - style: EditorStyle, + style: &EditorStyle, workspace: Option>, cx: &mut ViewContext, ) -> (DisplayPoint, AnyElement) { - todo!() - // match self { - // ContextMenu::Completions(menu) => (cursor_position, menu.render(style, workspace, cx)), - // ContextMenu::CodeActions(menu) => menu.render(cursor_position, style, cx), - // } + match self { + ContextMenu::Completions(menu) => (cursor_position, menu.render(style, workspace, cx)), + ContextMenu::CodeActions(menu) => menu.render(cursor_position, style, cx), + } } } @@ -1253,13 +1252,13 @@ impl CompletionsMenu { fn render( &self, - style: EditorStyle, + style: &EditorStyle, workspace: Option>, cx: &mut ViewContext, - ) { + ) -> AnyElement { todo!("old implementation below") } - // ) -> AnyElement { + // enum CompletionTag {} // let settings = EditorSettings>(cx); @@ -1572,7 +1571,7 @@ impl CodeActionsMenu { fn render( &self, mut cursor_position: DisplayPoint, - style: EditorStyle, + style: &EditorStyle, cx: &mut ViewContext, ) -> (DisplayPoint, AnyElement) { todo!("old version below") @@ -4480,29 +4479,27 @@ impl Editor { // } pub fn context_menu_visible(&self) -> bool { - false - // todo!("context menu") - // self.context_menu - // .read() - // .as_ref() - // .map_or(false, |menu| menu.visible()) + self.context_menu + .read() + .as_ref() + .map_or(false, |menu| menu.visible()) } - // pub fn render_context_menu( - // &self, - // cursor_position: DisplayPoint, - // style: EditorStyle, - // cx: &mut ViewContext, - // ) -> Option<(DisplayPoint, AnyElement)> { - // self.context_menu.read().as_ref().map(|menu| { - // menu.render( - // cursor_position, - // style, - // self.workspace.as_ref().map(|(w, _)| w.clone()), - // cx, - // ) - // }) - // } + pub fn render_context_menu( + &self, + cursor_position: DisplayPoint, + style: &EditorStyle, + cx: &mut ViewContext, + ) -> Option<(DisplayPoint, AnyElement)> { + self.context_menu.read().as_ref().map(|menu| { + menu.render( + cursor_position, + style, + self.workspace.as_ref().map(|(w, _)| w.clone()), + cx, + ) + }) + } fn hide_context_menu(&mut self, cx: &mut ViewContext) -> Option { cx.notify(); diff --git a/crates/editor2/src/element.rs b/crates/editor2/src/element.rs index a447b5647c48eefd9b9c10f9e130d7d8e4d705a6..c444da718cbccb177c91b5241f53cc125c6c6430 100644 --- a/crates/editor2/src/element.rs +++ b/crates/editor2/src/element.rs @@ -603,7 +603,7 @@ impl EditorElement { fn paint_text( &mut self, bounds: Bounds, - layout: &LayoutState, + layout: &mut LayoutState, editor: &mut Editor, cx: &mut ViewContext, ) { @@ -794,48 +794,46 @@ impl EditorElement { ) } - cx.stack(0, |cx| { + cx.with_z_index(0, |cx| { for cursor in cursors { cursor.paint(content_origin, cx); } }); - // cx.scene().push_layer(Some(bounds)); - - // cx.scene().pop_layer(); - - // if let Some((position, context_menu)) = layout.context_menu.as_mut() { - // cx.scene().push_stacking_context(None, None); - // let cursor_row_layout = - // &layout.position_map.line_layouts[(position.row() - start_row) as usize].line; - // let x = cursor_row_layout.x_for_index(position.column() as usize) - scroll_left; - // let y = (position.row() + 1) as f32 * layout.position_map.line_height - scroll_top; - // let mut list_origin = content_origin + point(x, y); - // let list_width = context_menu.size().x; - // let list_height = context_menu.size().y; - - // // Snap the right edge of the list to the right edge of the window if - // // its horizontal bounds overflow. - // if list_origin.x + list_width > cx.window_size().x { - // list_origin.set_x((cx.window_size().x - list_width).max(0.)); - // } - // if list_origin.y + list_height > bounds.max_y { - // list_origin - // .set_y(list_origin.y - layout.position_map.line_height - list_height); - // } + if let Some((position, context_menu)) = layout.context_menu.as_mut() { + cx.with_z_index(1, |cx| { + let line_height = self.style.text.line_height_in_pixels(cx.rem_size()); + let available_space = size( + AvailableSpace::Definite(cx.viewport_size().width * 0.7), + AvailableSpace::Definite( + (12. * line_height).min((bounds.size.height - line_height) / 2.), + ), + ); + let context_menu_size = context_menu.measure(available_space, editor, cx); + + let cursor_row_layout = &layout.position_map.line_layouts + [(position.row() - start_row) as usize] + .line; + let x = cursor_row_layout.x_for_index(position.column() as usize) - scroll_left; + let y = + (position.row() + 1) as f32 * layout.position_map.line_height - scroll_top; + let mut list_origin = content_origin + point(x, y); + let list_width = context_menu_size.width; + let list_height = context_menu_size.height; + + // Snap the right edge of the list to the right edge of the window if + // its horizontal bounds overflow. + if list_origin.x + list_width > cx.viewport_size().width { + list_origin.x = (cx.viewport_size().width - list_width).max(Pixels::ZERO); + } - // context_menu.paint( - // list_origin, - // Bounds::::from_points( - // gpui::Point::::zero(), - // point(f32::MAX, f32::MAX), - // ), // Let content bleed outside of editor - // editor, - // cx, - // ); + if list_origin.y + list_height > bounds.lower_right().y { + list_origin.y -= layout.position_map.line_height - list_height; + } - // cx.scene().pop_stacking_context(); - // } + context_menu.draw(list_origin, available_space, editor, cx); + }) + } // if let Some((position, hover_popovers)) = layout.hover_popovers.as_mut() { // cx.scene().push_stacking_context(None, None); @@ -1781,15 +1779,14 @@ impl EditorElement { snapshot = editor.snapshot(cx); } - // todo!("context menu") - // let mut context_menu = None; + let mut context_menu = None; let mut code_actions_indicator = None; if let Some(newest_selection_head) = newest_selection_head { if (start_row..end_row).contains(&newest_selection_head.row()) { - // if editor.context_menu_visible() { - // context_menu = - // editor.render_context_menu(newest_selection_head, style.clone(), cx); - // } + if editor.context_menu_visible() { + context_menu = + editor.render_context_menu(newest_selection_head, &self.style, cx); + } let active = matches!( editor.context_menu.read().as_ref(), @@ -1939,7 +1936,7 @@ impl EditorElement { display_hunks, // blocks, selections, - // context_menu, + context_menu, code_actions_indicator, // fold_indicators, tab_invisible, @@ -2501,21 +2498,24 @@ impl Element for EditorElement { size: layout.text_size, }; - cx.with_content_mask(ContentMask { bounds }, |cx| { - self.paint_mouse_listeners( - bounds, - gutter_bounds, - text_bounds, - &layout.position_map, - cx, - ); - self.paint_background(gutter_bounds, text_bounds, &layout, cx); - if layout.gutter_size.width > Pixels::ZERO { - self.paint_gutter(gutter_bounds, &mut layout, editor, cx); - } - self.paint_text(text_bounds, &layout, editor, cx); - let input_handler = ElementInputHandler::new(bounds, cx); - cx.handle_input(&editor.focus_handle, input_handler); + // We call with_z_index to establish a new stacking context. + cx.with_z_index(0, |cx| { + cx.with_content_mask(ContentMask { bounds }, |cx| { + self.paint_mouse_listeners( + bounds, + gutter_bounds, + text_bounds, + &layout.position_map, + cx, + ); + self.paint_background(gutter_bounds, text_bounds, &layout, cx); + if layout.gutter_size.width > Pixels::ZERO { + self.paint_gutter(gutter_bounds, &mut layout, editor, cx); + } + self.paint_text(text_bounds, &mut layout, editor, cx); + let input_handler = ElementInputHandler::new(bounds, cx); + cx.handle_input(&editor.focus_handle, input_handler); + }); }); } } @@ -3141,7 +3141,7 @@ pub struct LayoutState { show_scrollbars: bool, is_singleton: bool, max_row: u32, - // context_menu: Option<(DisplayPoint, AnyElement)>, + context_menu: Option<(DisplayPoint, AnyElement)>, code_actions_indicator: Option, // hover_popovers: Option<(DisplayPoint, Vec>)>, // fold_indicators: Vec>>, diff --git a/crates/gpui2/src/window.rs b/crates/gpui2/src/window.rs index 7b5ce5b7fc418d612a90f64c23ebf44ecc6a1b10..4c81bae5f329358fe6b9ae4349a636f56fc34b6c 100644 --- a/crates/gpui2/src/window.rs +++ b/crates/gpui2/src/window.rs @@ -200,7 +200,7 @@ pub struct Window { display_id: DisplayId, sprite_atlas: Arc, rem_size: Pixels, - content_size: Size, + viewport_size: Size, pub(crate) layout_engine: TaffyLayoutEngine, pub(crate) root_view: Option, pub(crate) element_id_stack: GlobalElementId, @@ -299,7 +299,7 @@ impl Window { display_id, sprite_atlas, rem_size: px(16.), - content_size, + viewport_size: content_size, layout_engine: TaffyLayoutEngine::new(), root_view: None, element_id_stack: GlobalElementId::default(), @@ -609,7 +609,7 @@ impl<'a> WindowContext<'a> { fn window_bounds_changed(&mut self) { self.window.scale_factor = self.window.platform_window.scale_factor(); - self.window.content_size = self.window.platform_window.content_size(); + self.window.viewport_size = self.window.platform_window.content_size(); self.window.bounds = self.window.platform_window.bounds(); self.window.display_id = self.window.platform_window.display().id(); self.window.dirty = true; @@ -624,6 +624,10 @@ impl<'a> WindowContext<'a> { self.window.bounds } + pub fn viewport_size(&self) -> Size { + self.window.viewport_size + } + pub fn is_window_active(&self) -> bool { self.window.active } @@ -717,7 +721,7 @@ impl<'a> WindowContext<'a> { /// Called during painting to invoke the given closure in a new stacking context. The given /// z-index is interpreted relative to the previous call to `stack`. - pub fn stack(&mut self, z_index: u32, f: impl FnOnce(&mut Self) -> R) -> R { + pub fn with_z_index(&mut self, z_index: u32, f: impl FnOnce(&mut Self) -> R) -> R { self.window.current_frame.z_index_stack.push(z_index); let result = f(self); self.window.current_frame.z_index_stack.pop(); @@ -1015,13 +1019,13 @@ impl<'a> WindowContext<'a> { self.start_frame(); - self.stack(0, |cx| { - let available_space = cx.window.content_size.map(Into::into); + self.with_z_index(0, |cx| { + let available_space = cx.window.viewport_size.map(Into::into); root_view.draw(available_space, cx); }); if let Some(active_drag) = self.app.active_drag.take() { - self.stack(1, |cx| { + self.with_z_index(1, |cx| { let offset = cx.mouse_position() - active_drag.cursor_offset; cx.with_element_offset(Some(offset), |cx| { let available_space = @@ -1031,7 +1035,7 @@ impl<'a> WindowContext<'a> { }); }); } else if let Some(active_tooltip) = self.app.active_tooltip.take() { - self.stack(1, |cx| { + self.with_z_index(1, |cx| { cx.with_element_offset(Some(active_tooltip.cursor_offset), |cx| { let available_space = size(AvailableSpace::MinContent, AvailableSpace::MinContent); @@ -1686,7 +1690,7 @@ pub trait BorrowWindow: BorrowMut + BorrowMut { .unwrap_or_else(|| ContentMask { bounds: Bounds { origin: Point::default(), - size: self.window().content_size, + size: self.window().viewport_size, }, }) }