Remove focused field and use FocusHandle instead in Editor

This commit is contained in:
Antonio Scandurra 2023-11-07 12:46:31 +01:00
parent a866370dc1
commit 6ae09634ce
5 changed files with 276 additions and 278 deletions

View file

@ -634,7 +634,6 @@ pub struct Editor {
// override_text_style: Option<Box<OverrideTextStyle>>,
project: Option<Model<Project>>,
collaboration_hub: Option<Box<dyn CollaborationHub>>,
focused: bool,
blink_manager: Model<BlinkManager>,
pub show_local_selections: bool,
mode: EditorMode,
@ -1940,7 +1939,6 @@ impl Editor {
// get_field_editor_theme,
collaboration_hub: project.clone().map(|project| Box::new(project) as _),
project,
focused: false,
blink_manager: blink_manager.clone(),
show_local_selections: true,
mode,
@ -2211,7 +2209,7 @@ impl Editor {
old_cursor_position: &Anchor,
cx: &mut ViewContext<Self>,
) {
if self.focused && self.leader_peer_id.is_none() {
if self.focus_handle.is_focused(cx) && self.leader_peer_id.is_none() {
self.buffer.update(cx, |buffer, cx| {
buffer.set_active_selections(
&self.selections.disjoint_anchors(),
@ -2458,7 +2456,7 @@ impl Editor {
click_count: usize,
cx: &mut ViewContext<Self>,
) {
if !self.focused {
if !self.focus_handle.is_focused(cx) {
cx.focus(&self.focus_handle);
}
@ -2524,7 +2522,7 @@ impl Editor {
goal_column: u32,
cx: &mut ViewContext<Self>,
) {
if !self.focused {
if !self.focus_handle.is_focused(cx) {
cx.focus(&self.focus_handle);
}
@ -3631,7 +3629,7 @@ impl Editor {
_ => return,
}
if this.focused && menu.is_some() {
if this.focus_handle.is_focused(cx) && menu.is_some() {
let menu = menu.unwrap();
*context_menu = Some(ContextMenu::Completions(menu));
drop(context_menu);
@ -8693,8 +8691,8 @@ impl Editor {
}
}
pub fn show_local_cursors(&self, cx: &AppContext) -> bool {
self.blink_manager.read(cx).visible() && self.focused
pub fn show_local_cursors(&self, cx: &WindowContext) -> bool {
self.blink_manager.read(cx).visible() && self.focus_handle.is_focused(cx)
}
fn on_buffer_changed(&mut self, _: Model<MultiBuffer>, cx: &mut ViewContext<Self>) {

View file

@ -774,7 +774,6 @@ impl EditorElement {
let line_end_overshoot = 0.15 * layout.position_map.line_height;
let whitespace_setting = editor.buffer.read(cx).settings_at(0, cx).show_whitespaces;
cx.with_content_mask(ContentMask { bounds }, |cx| {
// todo!("cursor region")
// cx.scene().push_cursor_region(CursorRegion {
// bounds,
@ -871,6 +870,7 @@ impl EditorElement {
if selection.is_local && !selection.range.is_empty() {
invisible_display_ranges.push(selection.range.clone());
}
if !selection.is_local || editor.show_local_cursors(cx) {
let cursor_position = selection.head;
if layout
@ -883,8 +883,8 @@ impl EditorElement {
let cursor_column = cursor_position.column() as usize;
let cursor_character_x = cursor_row_layout.x_for_index(cursor_column);
let mut block_width = cursor_row_layout.x_for_index(cursor_column + 1)
- cursor_character_x;
let mut block_width =
cursor_row_layout.x_for_index(cursor_column + 1) - cursor_character_x;
if block_width == Pixels::ZERO {
block_width = layout.position_map.em_width;
}
@ -951,10 +951,13 @@ impl EditorElement {
)
}
// cx.scene().push_layer(Some(bounds));
cx.stack(9999, |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() {
@ -1062,7 +1065,6 @@ impl EditorElement {
// cx.scene().pop_stacking_context();
// }
})
}
fn scrollbar_left(&self, bounds: &Bounds<Pixels>) -> Pixels {
@ -2543,7 +2545,7 @@ impl Element<Editor> for EditorElement {
fn initialize(
&mut self,
view_state: &mut Editor,
editor: &mut Editor,
element_state: Option<Self::ElementState>,
cx: &mut gpui::ViewContext<Editor>,
) -> Self::ElementState {
@ -2552,7 +2554,7 @@ impl Element<Editor> for EditorElement {
fn layout(
&mut self,
view_state: &mut Editor,
editor: &mut Editor,
element_state: &mut Self::ElementState,
cx: &mut gpui::ViewContext<Editor>,
) -> gpui::LayoutId {

View file

@ -159,16 +159,14 @@ impl FollowableItem for Editor {
self.buffer.update(cx, |buffer, cx| {
buffer.remove_active_selections(cx);
});
} else {
} else if self.focus_handle.is_focused(cx) {
self.buffer.update(cx, |buffer, cx| {
if self.focused {
buffer.set_active_selections(
&self.selections.disjoint_anchors(),
self.selections.line_mode,
self.cursor_shape,
cx,
);
}
});
}
cx.notify();

View file

@ -581,7 +581,7 @@ fn go_to_fetched_definition_of_kind(
let is_correct_kind = cached_definitions_kind == Some(kind);
if !cached_definitions.is_empty() && is_correct_kind {
if !editor.focused {
if !editor.focus_handle.is_focused(cx) {
cx.focus(&editor.focus_handle);
}

View file

@ -59,24 +59,24 @@ impl Default for PlayerColors {
fn default() -> Self {
Self(vec![
PlayerColor {
cursor: hsla(0.0, 0.0, 0.0, 0.0),
background: hsla(0.0, 0.0, 0.0, 0.0),
selection: hsla(0.0, 0.0, 0.0, 0.0),
cursor: hsla(0.0, 0.0, 0.0, 1.0),
background: hsla(0.0, 0.0, 0.0, 1.0),
selection: hsla(0.0, 0.0, 0.0, 1.0),
},
PlayerColor {
cursor: hsla(0.0, 0.0, 0.0, 0.0),
background: hsla(0.0, 0.0, 0.0, 0.0),
selection: hsla(0.0, 0.0, 0.0, 0.0),
cursor: hsla(0.0, 0.0, 0.0, 1.0),
background: hsla(0.0, 0.0, 0.0, 1.0),
selection: hsla(0.0, 0.0, 0.0, 1.0),
},
PlayerColor {
cursor: hsla(0.0, 0.0, 0.0, 0.0),
background: hsla(0.0, 0.0, 0.0, 0.0),
selection: hsla(0.0, 0.0, 0.0, 0.0),
cursor: hsla(0.0, 0.0, 0.0, 1.0),
background: hsla(0.0, 0.0, 0.0, 1.0),
selection: hsla(0.0, 0.0, 0.0, 1.0),
},
PlayerColor {
cursor: hsla(0.0, 0.0, 0.0, 0.0),
background: hsla(0.0, 0.0, 0.0, 0.0),
selection: hsla(0.0, 0.0, 0.0, 0.0),
cursor: hsla(0.0, 0.0, 0.0, 1.0),
background: hsla(0.0, 0.0, 0.0, 1.0),
selection: hsla(0.0, 0.0, 0.0, 1.0),
},
])
}