Implement Editor::single_line

This commit is contained in:
Conrad Irwin 2023-11-07 11:12:16 -07:00
parent 4bf3a4e3d4
commit 3a72f2122a
3 changed files with 25 additions and 21 deletions

View file

@ -36,10 +36,10 @@ pub use element::{
use futures::FutureExt; use futures::FutureExt;
use fuzzy::{StringMatch, StringMatchCandidate}; use fuzzy::{StringMatch, StringMatchCandidate};
use gpui::{ use gpui::{
actions, div, px, AnyElement, AppContext, BackgroundExecutor, Context, DispatchContext, Div, actions, div, px, relative, AnyElement, AppContext, BackgroundExecutor, Context,
Element, Entity, EventEmitter, FocusHandle, FontStyle, FontWeight, Hsla, Model, Pixels, Render, DispatchContext, Div, Element, Entity, EventEmitter, FocusHandle, FontStyle, FontWeight, Hsla,
Styled, Subscription, Task, TextStyle, View, ViewContext, VisualContext, WeakView, Model, Pixels, Render, Styled, Subscription, Task, TextStyle, View, ViewContext, VisualContext,
WindowContext, WeakView, WindowContext,
}; };
use highlight_matching_bracket::refresh_matching_bracket_highlights; use highlight_matching_bracket::refresh_matching_bracket_highlights;
use hover_popover::{hide_hover, HoverState}; use hover_popover::{hide_hover, HoverState};
@ -593,7 +593,6 @@ pub struct EditorStyle {
pub background: Hsla, pub background: Hsla,
pub local_player: PlayerColor, pub local_player: PlayerColor,
pub text: TextStyle, pub text: TextStyle,
pub line_height_scalar: f32,
pub scrollbar_width: Pixels, pub scrollbar_width: Pixels,
pub syntax: Arc<SyntaxTheme>, pub syntax: Arc<SyntaxTheme>,
pub diagnostic_style: DiagnosticStyle, pub diagnostic_style: DiagnosticStyle,
@ -1795,14 +1794,11 @@ impl InlayHintRefreshReason {
} }
impl Editor { impl Editor {
// pub fn single_line( pub fn single_line(cx: &mut ViewContext<Self>) -> Self {
// field_editor_style: Option<Arc<GetFieldEditorTheme>>, let buffer = cx.build_model(|cx| Buffer::new(0, cx.entity_id().as_u64(), String::new()));
// cx: &mut ViewContext<Self>, let buffer = cx.build_model(|cx| MultiBuffer::singleton(buffer, cx));
// ) -> Self { Self::new(EditorMode::SingleLine, buffer, None, cx)
// let buffer = cx.build_model(|cx| Buffer::new(0, cx.model_id() as u64, String::new())); }
// let buffer = cx.build_model(|cx| MultiBuffer::singleton(buffer, cx));
// Self::new(EditorMode::SingleLine, buffer, None, field_editor_style, cx)
// }
// pub fn multi_line( // pub fn multi_line(
// field_editor_style: Option<Arc<GetFieldEditorTheme>>, // field_editor_style: Option<Arc<GetFieldEditorTheme>>,
@ -9372,14 +9368,13 @@ impl Render for Editor {
font_size: settings.buffer_font_size.into(), font_size: settings.buffer_font_size.into(),
font_weight: FontWeight::NORMAL, font_weight: FontWeight::NORMAL,
font_style: FontStyle::Normal, font_style: FontStyle::Normal,
line_height: Default::default(), line_height: relative(settings.buffer_line_height.value()),
underline: None, underline: None,
}; };
EditorElement::new(EditorStyle { EditorElement::new(EditorStyle {
background: cx.theme().colors().editor_background, background: cx.theme().colors().editor_background,
local_player: cx.theme().players().local(), local_player: cx.theme().players().local(),
text: text_style, text: text_style,
line_height_scalar: settings.buffer_line_height.value(),
scrollbar_width: px(12.), scrollbar_width: px(12.),
syntax: cx.theme().syntax().clone(), syntax: cx.theme().syntax().clone(),
diagnostic_style: cx.theme().diagnostic_style(), diagnostic_style: cx.theme().diagnostic_style(),

View file

@ -8,10 +8,11 @@ use crate::{
use anyhow::Result; use anyhow::Result;
use collections::{BTreeMap, HashMap}; use collections::{BTreeMap, HashMap};
use gpui::{ use gpui::{
black, hsla, point, px, relative, size, transparent_black, Action, AnyElement, BorrowWindow, black, hsla, point, px, relative, size, transparent_black, Action, AnyElement,
Bounds, ContentMask, Corners, DispatchContext, DispatchPhase, Edges, Element, ElementId, BorrowAppContext, BorrowWindow, Bounds, ContentMask, Corners, DispatchContext, DispatchPhase,
Entity, Hsla, KeyDownEvent, KeyListener, KeyMatch, Line, Pixels, ScrollWheelEvent, ShapedGlyph, Edges, Element, ElementId, Entity, Hsla, KeyDownEvent, KeyListener, KeyMatch, Line, Pixels,
Size, StatefulInteraction, Style, TextRun, TextStyle, TextSystem, ViewContext, WindowContext, ScrollWheelEvent, ShapedGlyph, Size, StatefulInteraction, Style, TextRun, TextStyle,
TextSystem, ViewContext, WindowContext,
}; };
use itertools::Itertools; use itertools::Itertools;
use language::language_settings::ShowWhitespaceSetting; use language::language_settings::ShowWhitespaceSetting;
@ -1605,7 +1606,7 @@ impl EditorElement {
let style = self.style.clone(); let style = self.style.clone();
let font_id = cx.text_system().font_id(&style.text.font()).unwrap(); let font_id = cx.text_system().font_id(&style.text.font()).unwrap();
let font_size = style.text.font_size.to_pixels(cx.rem_size()); let font_size = style.text.font_size.to_pixels(cx.rem_size());
let line_height = (font_size * style.line_height_scalar).round(); let line_height = style.text.line_height_in_pixels(cx.rem_size());
let em_width = cx let em_width = cx
.text_system() .text_system()
.typographic_bounds(font_id, font_size, 'm') .typographic_bounds(font_id, font_size, 'm')
@ -2593,7 +2594,11 @@ impl Element<Editor> for EditorElement {
let rem_size = cx.rem_size(); let rem_size = cx.rem_size();
let mut style = Style::default(); let mut style = Style::default();
style.size.width = relative(1.).into(); style.size.width = relative(1.).into();
style.size.height = relative(1.).into(); style.size.height = match editor.mode {
EditorMode::SingleLine => self.style.text.line_height_in_pixels(cx.rem_size()).into(),
EditorMode::AutoHeight { .. } => todo!(),
EditorMode::Full => relative(1.).into(),
};
cx.request_layout(&style, None) cx.request_layout(&style, None)
} }

View file

@ -189,6 +189,10 @@ impl TextStyle {
} }
} }
pub fn line_height_in_pixels(&self, rem_size: Pixels) -> Pixels {
self.line_height.to_pixels(self.font_size, rem_size)
}
pub fn to_run(&self, len: usize) -> TextRun { pub fn to_run(&self, len: usize) -> TextRun {
TextRun { TextRun {
len, len,