Add cursor blink setting and replicate cursor shape to remote collaborators

This commit is contained in:
K Simmons
2022-10-17 16:20:47 -07:00
parent 318b923bac
commit 40c3e925ad
15 changed files with 128 additions and 63 deletions
+17 -7
View File
@@ -42,9 +42,9 @@ use hover_popover::{hide_hover, HoverState};
pub use items::MAX_TAB_TITLE_LEN;
pub use language::{char_kind, CharKind};
use language::{
AutoindentMode, BracketPair, Buffer, CodeAction, CodeLabel, Completion, Diagnostic,
DiagnosticSeverity, IndentKind, IndentSize, Language, OffsetRangeExt, OffsetUtf16, Point,
Selection, SelectionGoal, TransactionId,
AutoindentMode, BracketPair, Buffer, CodeAction, CodeLabel, Completion, CursorShape,
Diagnostic, DiagnosticSeverity, IndentKind, IndentSize, Language, OffsetRangeExt, OffsetUtf16,
Point, Selection, SelectionGoal, TransactionId,
};
use link_go_to_definition::{hide_link_definition, LinkGoToDefinitionState};
pub use multi_buffer::{
@@ -1478,6 +1478,7 @@ impl Editor {
buffer.set_active_selections(
&self.selections.disjoint_anchors(),
self.selections.line_mode,
self.cursor_shape,
cx,
)
});
@@ -6145,7 +6146,17 @@ impl Editor {
fn blink_cursors(&mut self, epoch: usize, cx: &mut ViewContext<Self>) {
if epoch == self.blink_epoch && self.focused && !self.blinking_paused {
self.show_local_cursors = !self.show_local_cursors;
let newest_head = self.selections.newest::<usize>(cx).head();
let language_name = self
.buffer
.read(cx)
.language_at(newest_head, cx)
.map(|l| l.name());
self.show_local_cursors = !self.show_local_cursors
|| !cx
.global::<Settings>()
.cursor_blink(language_name.as_deref());
cx.notify();
let epoch = self.next_blink_epoch();
@@ -6466,9 +6477,7 @@ impl View for Editor {
}
Stack::new()
.with_child(
EditorElement::new(self.handle.clone(), style.clone(), self.cursor_shape).boxed(),
)
.with_child(EditorElement::new(self.handle.clone(), style.clone()).boxed())
.with_child(ChildView::new(&self.mouse_context_menu, cx).boxed())
.boxed()
}
@@ -6491,6 +6500,7 @@ impl View for Editor {
buffer.set_active_selections(
&self.selections.disjoint_anchors(),
self.selections.line_mode,
self.cursor_shape,
cx,
);
}
+23 -37
View File
@@ -35,7 +35,7 @@ use gpui::{
WeakViewHandle,
};
use json::json;
use language::{Bias, DiagnosticSeverity, OffsetUtf16, Point, Selection};
use language::{Bias, CursorShape, DiagnosticSeverity, OffsetUtf16, Point, Selection};
use project::ProjectPath;
use settings::{GitGutter, Settings};
use smallvec::SmallVec;
@@ -56,6 +56,7 @@ struct DiffHunkLayout {
struct SelectionLayout {
head: DisplayPoint,
cursor_shape: CursorShape,
range: Range<DisplayPoint>,
}
@@ -63,6 +64,7 @@ impl SelectionLayout {
fn new<T: ToPoint + ToDisplayPoint + Clone>(
selection: Selection<T>,
line_mode: bool,
cursor_shape: CursorShape,
map: &DisplaySnapshot,
) -> Self {
if line_mode {
@@ -70,6 +72,7 @@ impl SelectionLayout {
let point_range = map.expand_to_line(selection.range());
Self {
head: selection.head().to_display_point(map),
cursor_shape,
range: point_range.start.to_display_point(map)
..point_range.end.to_display_point(map),
}
@@ -77,6 +80,7 @@ impl SelectionLayout {
let selection = selection.map(|p| p.to_display_point(map));
Self {
head: selection.head(),
cursor_shape,
range: selection.range(),
}
}
@@ -87,19 +91,13 @@ impl SelectionLayout {
pub struct EditorElement {
view: WeakViewHandle<Editor>,
style: Arc<EditorStyle>,
cursor_shape: CursorShape,
}
impl EditorElement {
pub fn new(
view: WeakViewHandle<Editor>,
style: EditorStyle,
cursor_shape: CursorShape,
) -> Self {
pub fn new(view: WeakViewHandle<Editor>, style: EditorStyle) -> Self {
Self {
view,
style: Arc::new(style),
cursor_shape,
}
}
@@ -723,7 +721,7 @@ impl EditorElement {
if block_width == 0.0 {
block_width = layout.position_map.em_width;
}
let block_text = if let CursorShape::Block = self.cursor_shape {
let block_text = if let CursorShape::Block = selection.cursor_shape {
layout
.position_map
.snapshot
@@ -759,7 +757,7 @@ impl EditorElement {
block_width,
origin: vec2f(x, y),
line_height: layout.position_map.line_height,
shape: self.cursor_shape,
shape: selection.cursor_shape,
block_text,
});
}
@@ -1648,7 +1646,7 @@ impl Element for EditorElement {
);
let mut remote_selections = HashMap::default();
for (replica_id, line_mode, selection) in display_map
for (replica_id, line_mode, cursor_shape, selection) in display_map
.buffer_snapshot
.remote_selections_in_range(&(start_anchor.clone()..end_anchor.clone()))
{
@@ -1659,7 +1657,12 @@ impl Element for EditorElement {
remote_selections
.entry(replica_id)
.or_insert(Vec::new())
.push(SelectionLayout::new(selection, line_mode, &display_map));
.push(SelectionLayout::new(
selection,
line_mode,
cursor_shape,
&display_map,
));
}
selections.extend(remote_selections);
@@ -1691,7 +1694,12 @@ impl Element for EditorElement {
local_selections
.into_iter()
.map(|selection| {
SelectionLayout::new(selection, view.selections.line_mode, &display_map)
SelectionLayout::new(
selection,
view.selections.line_mode,
view.cursor_shape,
&display_map,
)
})
.collect(),
));
@@ -2094,20 +2102,6 @@ fn layout_line(
)
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum CursorShape {
Bar,
Block,
Underscore,
Hollow,
}
impl Default for CursorShape {
fn default() -> Self {
CursorShape::Bar
}
}
#[derive(Debug)]
pub struct Cursor {
origin: Vector2F,
@@ -2348,11 +2342,7 @@ mod tests {
let (window_id, editor) = cx.add_window(Default::default(), |cx| {
Editor::new(EditorMode::Full, buffer, None, None, cx)
});
let element = EditorElement::new(
editor.downgrade(),
editor.read(cx).style(cx),
CursorShape::Bar,
);
let element = EditorElement::new(editor.downgrade(), editor.read(cx).style(cx));
let layouts = editor.update(cx, |editor, cx| {
let snapshot = editor.snapshot(cx);
@@ -2388,11 +2378,7 @@ mod tests {
cx.blur();
});
let mut element = EditorElement::new(
editor.downgrade(),
editor.read(cx).style(cx),
CursorShape::Bar,
);
let mut element = EditorElement::new(editor.downgrade(), editor.read(cx).style(cx));
let mut scene = Scene::new(1.0);
let mut presenter = cx.build_presenter(window_id, 30., Default::default());
+1
View File
@@ -120,6 +120,7 @@ impl FollowableItem for Editor {
buffer.set_active_selections(
&self.selections.disjoint_anchors(),
self.selections.line_mode,
self.cursor_shape,
cx,
);
}
+6 -4
View File
@@ -8,7 +8,7 @@ use git::diff::DiffHunk;
use gpui::{AppContext, Entity, ModelContext, ModelHandle, Task};
pub use language::Completion;
use language::{
char_kind, AutoindentMode, Buffer, BufferChunks, BufferSnapshot, CharKind, Chunk,
char_kind, AutoindentMode, Buffer, BufferChunks, BufferSnapshot, CharKind, Chunk, CursorShape,
DiagnosticEntry, Event, File, IndentSize, Language, OffsetRangeExt, OffsetUtf16, Outline,
OutlineItem, Point, PointUtf16, Selection, TextDimension, ToOffset as _, ToOffsetUtf16 as _,
ToPoint as _, ToPointUtf16 as _, TransactionId,
@@ -604,6 +604,7 @@ impl MultiBuffer {
&mut self,
selections: &[Selection<Anchor>],
line_mode: bool,
cursor_shape: CursorShape,
cx: &mut ModelContext<Self>,
) {
let mut selections_by_buffer: HashMap<usize, Vec<Selection<text::Anchor>>> =
@@ -668,7 +669,7 @@ impl MultiBuffer {
}
Some(selection)
}));
buffer.set_active_selections(merged_selections, line_mode, cx);
buffer.set_active_selections(merged_selections, line_mode, cursor_shape, cx);
});
}
}
@@ -2698,7 +2699,7 @@ impl MultiBufferSnapshot {
pub fn remote_selections_in_range<'a>(
&'a self,
range: &'a Range<Anchor>,
) -> impl 'a + Iterator<Item = (ReplicaId, bool, Selection<Anchor>)> {
) -> impl 'a + Iterator<Item = (ReplicaId, bool, CursorShape, Selection<Anchor>)> {
let mut cursor = self.excerpts.cursor::<Option<&ExcerptId>>();
cursor.seek(&Some(&range.start.excerpt_id), Bias::Left, &());
cursor
@@ -2715,7 +2716,7 @@ impl MultiBufferSnapshot {
excerpt
.buffer
.remote_selections_in_range(query_range)
.flat_map(move |(replica_id, line_mode, selections)| {
.flat_map(move |(replica_id, line_mode, cursor_shape, selections)| {
selections.map(move |selection| {
let mut start = Anchor {
buffer_id: Some(excerpt.buffer_id),
@@ -2737,6 +2738,7 @@ impl MultiBufferSnapshot {
(
replica_id,
line_mode,
cursor_shape,
Selection {
id: selection.id,
start,