Pass how many lines the editor should expand to in auto height mode

This commit is contained in:
Antonio Scandurra 2021-09-06 12:50:04 +02:00
parent cb62d53b49
commit 77d1574679
3 changed files with 23 additions and 23 deletions

View file

@ -47,7 +47,7 @@ impl ChatPanel {
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> Self { ) -> Self {
let input_editor = cx.add_view(|cx| { let input_editor = cx.add_view(|cx| {
Editor::auto_height(settings.clone(), cx).with_style({ Editor::auto_height(4, settings.clone(), cx).with_style({
let settings = settings.clone(); let settings = settings.clone();
move |_| settings.borrow().theme.chat_panel.input_editor.as_editor() move |_| settings.borrow().theme.chat_panel.input_editor.as_editor()
}) })
@ -237,11 +237,7 @@ impl ChatPanel {
fn render_input_box(&self) -> ElementBox { fn render_input_box(&self) -> ElementBox {
let theme = &self.settings.borrow().theme; let theme = &self.settings.borrow().theme;
Container::new( Container::new(ChildView::new(self.input_editor.id()).boxed())
ConstrainedBox::new(ChildView::new(self.input_editor.id()).boxed())
.with_max_height(100.)
.boxed(),
)
.with_style(&theme.chat_panel.input_editor_container) .with_style(&theme.chat_panel.input_editor_container)
.boxed() .boxed()
} }

View file

@ -272,9 +272,10 @@ pub enum SelectPhase {
End, End,
} }
enum EditorMode { #[derive(Copy, Clone, PartialEq, Eq)]
pub enum EditorMode {
SingleLine, SingleLine,
AutoHeight, AutoHeight { max_lines: usize },
Full, Full,
} }
@ -301,10 +302,9 @@ pub struct Editor {
} }
pub struct Snapshot { pub struct Snapshot {
pub mode: EditorMode,
pub display_snapshot: DisplayMapSnapshot, pub display_snapshot: DisplayMapSnapshot,
pub placeholder_text: Option<Arc<str>>, pub placeholder_text: Option<Arc<str>>,
pub gutter_visible: bool,
pub auto_height: bool,
pub theme: Arc<Theme>, pub theme: Arc<Theme>,
pub font_family: FamilyId, pub font_family: FamilyId,
pub font_size: f32, pub font_size: f32,
@ -332,10 +332,14 @@ impl Editor {
view view
} }
pub fn auto_height(settings: watch::Receiver<Settings>, cx: &mut ViewContext<Self>) -> Self { pub fn auto_height(
max_lines: usize,
settings: watch::Receiver<Settings>,
cx: &mut ViewContext<Self>,
) -> Self {
let buffer = cx.add_model(|cx| Buffer::new(0, String::new(), cx)); let buffer = cx.add_model(|cx| Buffer::new(0, String::new(), cx));
let mut view = Self::for_buffer(buffer, settings, cx); let mut view = Self::for_buffer(buffer, settings, cx);
view.mode = EditorMode::AutoHeight; view.mode = EditorMode::AutoHeight { max_lines };
view view
} }
@ -407,9 +411,8 @@ impl Editor {
let settings = self.settings.borrow(); let settings = self.settings.borrow();
Snapshot { Snapshot {
mode: self.mode,
display_snapshot: self.display_map.update(cx, |map, cx| map.snapshot(cx)), display_snapshot: self.display_map.update(cx, |map, cx| map.snapshot(cx)),
gutter_visible: matches!(self.mode, EditorMode::Full),
auto_height: matches!(self.mode, EditorMode::AutoHeight),
scroll_position: self.scroll_position, scroll_position: self.scroll_position,
scroll_top_anchor: self.scroll_top_anchor.clone(), scroll_top_anchor: self.scroll_top_anchor.clone(),
theme: settings.theme.clone(), theme: settings.theme.clone(),
@ -464,7 +467,7 @@ impl Editor {
let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
let mut scroll_position = let mut scroll_position =
compute_scroll_position(&display_map, self.scroll_position, &self.scroll_top_anchor); compute_scroll_position(&display_map, self.scroll_position, &self.scroll_top_anchor);
let max_scroll_top = if matches!(self.mode, EditorMode::AutoHeight) { let max_scroll_top = if matches!(self.mode, EditorMode::AutoHeight { .. }) {
(display_map.max_point().row() as f32 - visible_lines + 1.).max(0.) (display_map.max_point().row() as f32 - visible_lines + 1.).max(0.)
} else { } else {
display_map.max_point().row().saturating_sub(1) as f32 display_map.max_point().row().saturating_sub(1) as f32
@ -496,7 +499,7 @@ impl Editor {
.row() as f32 .row() as f32
+ 1.0; + 1.0;
let margin = if matches!(self.mode, EditorMode::AutoHeight) { let margin = if matches!(self.mode, EditorMode::AutoHeight { .. }) {
0. 0.
} else { } else {
((visible_lines - (last_cursor_bottom - first_cursor_top)) / 2.0) ((visible_lines - (last_cursor_bottom - first_cursor_top)) / 2.0)
@ -760,7 +763,7 @@ impl Editor {
fn newline(&mut self, Newline(insert_newline): &Newline, cx: &mut ViewContext<Self>) { fn newline(&mut self, Newline(insert_newline): &Newline, cx: &mut ViewContext<Self>) {
match self.mode { match self.mode {
EditorMode::SingleLine => cx.propagate_action(), EditorMode::SingleLine => cx.propagate_action(),
EditorMode::AutoHeight => { EditorMode::AutoHeight { .. } => {
if *insert_newline { if *insert_newline {
self.insert(&Insert("\n".into()), cx); self.insert(&Insert("\n".into()), cx);
} else { } else {

View file

@ -396,7 +396,7 @@ impl Element for EditorElement {
let gutter_padding; let gutter_padding;
let gutter_width; let gutter_width;
if snapshot.gutter_visible { if snapshot.mode == EditorMode::Full {
gutter_padding = snapshot.em_width(cx.font_cache); gutter_padding = snapshot.em_width(cx.font_cache);
match snapshot.max_line_number_width(cx.font_cache, cx.text_layout_cache) { match snapshot.max_line_number_width(cx.font_cache, cx.text_layout_cache) {
Err(error) => { Err(error) => {
@ -424,11 +424,12 @@ impl Element for EditorElement {
}); });
let scroll_height = (snapshot.max_point().row() + 1) as f32 * line_height; let scroll_height = (snapshot.max_point().row() + 1) as f32 * line_height;
if snapshot.auto_height { if let EditorMode::AutoHeight { max_lines } = snapshot.mode {
size.set_y( size.set_y(
scroll_height scroll_height
.min(constraint.max_along(Axis::Vertical)) .min(constraint.max_along(Axis::Vertical))
.max(constraint.min_along(Axis::Vertical)), .max(constraint.min_along(Axis::Vertical))
.min(line_height * max_lines as f32),
) )
} else if size.y().is_infinite() { } else if size.y().is_infinite() {
size.set_y(scroll_height); size.set_y(scroll_height);
@ -485,7 +486,7 @@ impl Element for EditorElement {
} }
}); });
let line_number_layouts = if snapshot.gutter_visible { let line_number_layouts = if snapshot.mode == EditorMode::Full {
let settings = self let settings = self
.view .view
.upgrade(cx.app) .upgrade(cx.app)