mirror of
https://github.com/zed-industries/zed.git
synced 2025-01-12 13:24:19 +00:00
Remove unnecessary Mutex
from scroll-related state in Editor
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
parent
694ab0d69d
commit
3d07be34f8
1 changed files with 28 additions and 32 deletions
|
@ -20,7 +20,6 @@ use gpui::{
|
||||||
ElementBox, Entity, FontCache, ModelHandle, MutableAppContext, Task, TextLayoutCache, View,
|
ElementBox, Entity, FontCache, ModelHandle, MutableAppContext, Task, TextLayoutCache, View,
|
||||||
ViewContext, WeakViewHandle,
|
ViewContext, WeakViewHandle,
|
||||||
};
|
};
|
||||||
use parking_lot::Mutex;
|
|
||||||
use postage::watch;
|
use postage::watch;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use smallvec::SmallVec;
|
use smallvec::SmallVec;
|
||||||
|
@ -375,8 +374,8 @@ pub struct Editor {
|
||||||
next_selection_id: usize,
|
next_selection_id: usize,
|
||||||
add_selections_state: Option<AddSelectionsState>,
|
add_selections_state: Option<AddSelectionsState>,
|
||||||
select_larger_syntax_node_stack: Vec<Vec<Selection>>,
|
select_larger_syntax_node_stack: Vec<Vec<Selection>>,
|
||||||
scroll_position: Mutex<Vector2F>,
|
scroll_position: Vector2F,
|
||||||
autoscroll_requested: Mutex<bool>,
|
autoscroll_requested: bool,
|
||||||
settings: watch::Receiver<Settings>,
|
settings: watch::Receiver<Settings>,
|
||||||
focused: bool,
|
focused: bool,
|
||||||
cursors_visible: bool,
|
cursors_visible: bool,
|
||||||
|
@ -446,8 +445,8 @@ impl Editor {
|
||||||
next_selection_id,
|
next_selection_id,
|
||||||
add_selections_state: None,
|
add_selections_state: None,
|
||||||
select_larger_syntax_node_stack: Vec::new(),
|
select_larger_syntax_node_stack: Vec::new(),
|
||||||
scroll_position: Mutex::new(Vector2F::zero()),
|
scroll_position: Vector2F::zero(),
|
||||||
autoscroll_requested: Mutex::new(false),
|
autoscroll_requested: false,
|
||||||
settings,
|
settings,
|
||||||
focused: false,
|
focused: false,
|
||||||
cursors_visible: false,
|
cursors_visible: false,
|
||||||
|
@ -467,7 +466,7 @@ impl Editor {
|
||||||
Snapshot {
|
Snapshot {
|
||||||
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: !self.single_line,
|
gutter_visible: !self.single_line,
|
||||||
scroll_position: *self.scroll_position.lock(),
|
scroll_position: self.scroll_position,
|
||||||
theme: settings.theme.clone(),
|
theme: settings.theme.clone(),
|
||||||
font_family: settings.buffer_font_family,
|
font_family: settings.buffer_font_family,
|
||||||
font_size: settings.buffer_font_size,
|
font_size: settings.buffer_font_size,
|
||||||
|
@ -475,18 +474,17 @@ impl Editor {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn scroll(&mut self, scroll_position: &Vector2F, cx: &mut ViewContext<Self>) {
|
fn scroll(&mut self, scroll_position: &Vector2F, cx: &mut ViewContext<Self>) {
|
||||||
*self.scroll_position.lock() = *scroll_position;
|
self.scroll_position = *scroll_position;
|
||||||
cx.notify();
|
cx.notify();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn scroll_position(&self) -> Vector2F {
|
pub fn scroll_position(&self) -> Vector2F {
|
||||||
*self.scroll_position.lock()
|
self.scroll_position
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn clamp_scroll_left(&mut self, max: f32) -> bool {
|
pub fn clamp_scroll_left(&mut self, max: f32) -> bool {
|
||||||
let mut scroll_position = self.scroll_position.lock();
|
if max < self.scroll_position.x() {
|
||||||
if max < scroll_position.x() {
|
self.scroll_position.set_x(max);
|
||||||
scroll_position.set_x(max);
|
|
||||||
true
|
true
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
|
@ -494,20 +492,18 @@ impl Editor {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn autoscroll_vertically(
|
pub fn autoscroll_vertically(
|
||||||
&self,
|
&mut self,
|
||||||
viewport_height: f32,
|
viewport_height: f32,
|
||||||
line_height: f32,
|
line_height: f32,
|
||||||
cx: &mut MutableAppContext,
|
cx: &mut MutableAppContext,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
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 = self.scroll_position.lock();
|
let scroll_top = self.scroll_position.y();
|
||||||
let scroll_top = scroll_position.y();
|
self.scroll_position
|
||||||
scroll_position
|
|
||||||
.set_y(scroll_top.min(display_map.max_point().row().saturating_sub(1) as f32));
|
.set_y(scroll_top.min(display_map.max_point().row().saturating_sub(1) as f32));
|
||||||
|
|
||||||
let mut autoscroll_requested = self.autoscroll_requested.lock();
|
if self.autoscroll_requested {
|
||||||
if *autoscroll_requested {
|
self.autoscroll_requested = false;
|
||||||
*autoscroll_requested = false;
|
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -538,20 +534,20 @@ impl Editor {
|
||||||
|
|
||||||
let target_top = (first_cursor_top - margin).max(0.0);
|
let target_top = (first_cursor_top - margin).max(0.0);
|
||||||
let target_bottom = last_cursor_bottom + margin;
|
let target_bottom = last_cursor_bottom + margin;
|
||||||
let start_row = scroll_position.y();
|
let start_row = self.scroll_position.y();
|
||||||
let end_row = start_row + visible_lines;
|
let end_row = start_row + visible_lines;
|
||||||
|
|
||||||
if target_top < start_row {
|
if target_top < start_row {
|
||||||
scroll_position.set_y(target_top);
|
self.scroll_position.set_y(target_top);
|
||||||
} else if target_bottom >= end_row {
|
} else if target_bottom >= end_row {
|
||||||
scroll_position.set_y(target_bottom - visible_lines);
|
self.scroll_position.set_y(target_bottom - visible_lines);
|
||||||
}
|
}
|
||||||
|
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn autoscroll_horizontally(
|
pub fn autoscroll_horizontally(
|
||||||
&self,
|
&mut self,
|
||||||
start_row: u32,
|
start_row: u32,
|
||||||
viewport_width: f32,
|
viewport_width: f32,
|
||||||
scroll_width: f32,
|
scroll_width: f32,
|
||||||
|
@ -579,15 +575,15 @@ impl Editor {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut scroll_position = self.scroll_position.lock();
|
let scroll_left = self.scroll_position.x() * max_glyph_width;
|
||||||
let scroll_left = scroll_position.x() * max_glyph_width;
|
|
||||||
let scroll_right = scroll_left + viewport_width;
|
let scroll_right = scroll_left + viewport_width;
|
||||||
|
|
||||||
if target_left < scroll_left {
|
if target_left < scroll_left {
|
||||||
scroll_position.set_x(target_left / max_glyph_width);
|
self.scroll_position.set_x(target_left / max_glyph_width);
|
||||||
true
|
true
|
||||||
} else if target_right > scroll_right {
|
} else if target_right > scroll_right {
|
||||||
scroll_position.set_x((target_right - viewport_width) / max_glyph_width);
|
self.scroll_position
|
||||||
|
.set_x((target_right - viewport_width) / max_glyph_width);
|
||||||
true
|
true
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
|
@ -645,7 +641,7 @@ impl Editor {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
*self.scroll_position.lock() = scroll_position;
|
self.scroll_position = scroll_position;
|
||||||
|
|
||||||
cx.notify();
|
cx.notify();
|
||||||
}
|
}
|
||||||
|
@ -2016,7 +2012,7 @@ impl Editor {
|
||||||
self.pause_cursor_blinking(cx);
|
self.pause_cursor_blinking(cx);
|
||||||
|
|
||||||
if autoscroll {
|
if autoscroll {
|
||||||
*self.autoscroll_requested.lock() = true;
|
self.autoscroll_requested = true;
|
||||||
cx.notify();
|
cx.notify();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2146,7 +2142,7 @@ impl Editor {
|
||||||
fn fold_ranges<T: ToOffset>(&mut self, ranges: Vec<Range<T>>, cx: &mut ViewContext<Self>) {
|
fn fold_ranges<T: ToOffset>(&mut self, ranges: Vec<Range<T>>, cx: &mut ViewContext<Self>) {
|
||||||
if !ranges.is_empty() {
|
if !ranges.is_empty() {
|
||||||
self.display_map.update(cx, |map, cx| map.fold(ranges, cx));
|
self.display_map.update(cx, |map, cx| map.fold(ranges, cx));
|
||||||
*self.autoscroll_requested.lock() = true;
|
self.autoscroll_requested = true;
|
||||||
cx.notify();
|
cx.notify();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2155,7 +2151,7 @@ impl Editor {
|
||||||
if !ranges.is_empty() {
|
if !ranges.is_empty() {
|
||||||
self.display_map
|
self.display_map
|
||||||
.update(cx, |map, cx| map.unfold(ranges, cx));
|
.update(cx, |map, cx| map.unfold(ranges, cx));
|
||||||
*self.autoscroll_requested.lock() = true;
|
self.autoscroll_requested = true;
|
||||||
cx.notify();
|
cx.notify();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2554,8 +2550,8 @@ impl workspace::ItemView for Editor {
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
{
|
{
|
||||||
let clone = Editor::for_buffer(self.buffer.clone(), self.settings.clone(), cx);
|
let mut clone = Editor::for_buffer(self.buffer.clone(), self.settings.clone(), cx);
|
||||||
*clone.scroll_position.lock() = *self.scroll_position.lock();
|
clone.scroll_position = self.scroll_position;
|
||||||
Some(clone)
|
Some(clone)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue