Detailed changes
@@ -1361,7 +1361,7 @@ pub mod tests {
use super::*;
use anyhow::anyhow;
use collections::BTreeMap;
- use gpui::executor::Background;
+ use gpui::executor::{Background, Deterministic};
use lazy_static::lazy_static;
use parking_lot::Mutex;
use rand::prelude::*;
@@ -1376,7 +1376,7 @@ pub mod tests {
async fn test_get_users_by_ids() {
for test_db in [
TestDb::postgres().await,
- TestDb::fake(Arc::new(gpui::executor::Background::new())),
+ TestDb::fake(build_background_executor()),
] {
let db = test_db.db();
@@ -1687,7 +1687,7 @@ pub mod tests {
async fn test_recent_channel_messages() {
for test_db in [
TestDb::postgres().await,
- TestDb::fake(Arc::new(gpui::executor::Background::new())),
+ TestDb::fake(build_background_executor()),
] {
let db = test_db.db();
let user = db.create_user("user", None, false).await.unwrap();
@@ -1726,7 +1726,7 @@ pub mod tests {
async fn test_channel_message_nonces() {
for test_db in [
TestDb::postgres().await,
- TestDb::fake(Arc::new(gpui::executor::Background::new())),
+ TestDb::fake(build_background_executor()),
] {
let db = test_db.db();
let user = db.create_user("user", None, false).await.unwrap();
@@ -1834,7 +1834,7 @@ pub mod tests {
async fn test_add_contacts() {
for test_db in [
TestDb::postgres().await,
- TestDb::fake(Arc::new(gpui::executor::Background::new())),
+ TestDb::fake(build_background_executor()),
] {
let db = test_db.db();
@@ -2836,4 +2836,8 @@ pub mod tests {
Some(self)
}
}
+
+ fn build_background_executor() -> Arc<Background> {
+ Deterministic::new(0).build_background()
+ }
}
@@ -17,6 +17,7 @@ use collections::{BTreeMap, Bound, HashMap, HashSet, VecDeque};
pub use display_map::DisplayPoint;
use display_map::*;
pub use element::*;
+use futures::{channel::oneshot, FutureExt};
use fuzzy::{StringMatch, StringMatchCandidate};
use gpui::{
actions,
@@ -28,8 +29,8 @@ use gpui::{
impl_actions, impl_internal_actions,
platform::CursorStyle,
text_layout, AppContext, AsyncAppContext, ClipboardItem, Element, ElementBox, Entity,
- ModelHandle, MutableAppContext, RenderContext, Task, View, ViewContext, ViewHandle,
- WeakViewHandle,
+ ModelHandle, MutableAppContext, RenderContext, Subscription, Task, View, ViewContext,
+ ViewHandle, WeakViewHandle,
};
use hover_popover::{hide_hover, HoverState};
pub use language::{char_kind, CharKind};
@@ -48,7 +49,7 @@ use ordered_float::OrderedFloat;
use project::{LocationLink, Project, ProjectPath, ProjectTransaction};
use selections_collection::{resolve_multiple, MutableSelectionsCollection, SelectionsCollection};
use serde::{Deserialize, Serialize};
-use settings::Settings;
+use settings::{Autosave, Settings};
use smallvec::SmallVec;
use smol::Timer;
use snippet::Snippet;
@@ -436,6 +437,9 @@ pub struct Editor {
leader_replica_id: Option<u16>,
hover_state: HoverState,
link_go_to_definition_state: LinkGoToDefinitionState,
+ pending_autosave: Option<Task<Option<()>>>,
+ cancel_pending_autosave: Option<oneshot::Sender<()>>,
+ _subscriptions: Vec<Subscription>,
}
pub struct EditorSnapshot {
@@ -973,17 +977,13 @@ impl Editor {
cx,
)
});
- cx.observe(&buffer, Self::on_buffer_changed).detach();
- cx.subscribe(&buffer, Self::on_buffer_event).detach();
- cx.observe(&display_map, Self::on_display_map_changed)
- .detach();
let selections = SelectionsCollection::new(display_map.clone(), buffer.clone());
let mut this = Self {
handle: cx.weak_handle(),
- buffer,
- display_map,
+ buffer: buffer.clone(),
+ display_map: display_map.clone(),
selections,
columnar_selection_tail: None,
add_selections_state: None,
@@ -1026,6 +1026,14 @@ impl Editor {
leader_replica_id: None,
hover_state: Default::default(),
link_go_to_definition_state: Default::default(),
+ pending_autosave: Default::default(),
+ cancel_pending_autosave: Default::default(),
+ _subscriptions: vec![
+ cx.observe(&buffer, Self::on_buffer_changed),
+ cx.subscribe(&buffer, Self::on_buffer_event),
+ cx.observe(&display_map, Self::on_display_map_changed),
+ cx.observe_window_activation(Self::on_window_activation_changed),
+ ],
};
this.end_selection(cx);
@@ -2148,7 +2156,10 @@ impl Editor {
.iter()
.zip(autoclose_pair.ranges.iter().map(|r| r.to_offset(&buffer)))
{
- if selection.is_empty() && autoclose_range.is_empty() && selection.start == autoclose_range.start {
+ if selection.is_empty()
+ && autoclose_range.is_empty()
+ && selection.start == autoclose_range.start
+ {
new_selections.push(Selection {
id: selection.id,
start: selection.start - autoclose_pair.pair.start.len(),
@@ -5570,6 +5581,33 @@ impl Editor {
self.refresh_active_diagnostics(cx);
self.refresh_code_actions(cx);
cx.emit(Event::BufferEdited);
+ if let Autosave::AfterDelay { milliseconds } = cx.global::<Settings>().autosave {
+ let pending_autosave =
+ self.pending_autosave.take().unwrap_or(Task::ready(None));
+ if let Some(cancel_pending_autosave) = self.cancel_pending_autosave.take() {
+ let _ = cancel_pending_autosave.send(());
+ }
+
+ let (cancel_tx, mut cancel_rx) = oneshot::channel();
+ self.cancel_pending_autosave = Some(cancel_tx);
+ self.pending_autosave = Some(cx.spawn_weak(|this, mut cx| async move {
+ let mut timer = cx
+ .background()
+ .timer(Duration::from_millis(milliseconds))
+ .fuse();
+ pending_autosave.await;
+ futures::select_biased! {
+ _ = cancel_rx => return None,
+ _ = timer => {}
+ }
+
+ this.upgrade(&cx)?
+ .update(&mut cx, |this, cx| this.autosave(cx))
+ .await
+ .log_err();
+ None
+ }));
+ }
}
language::Event::Reparsed => cx.emit(Event::Reparsed),
language::Event::DirtyChanged => cx.emit(Event::DirtyChanged),
@@ -5588,6 +5626,25 @@ impl Editor {
cx.notify();
}
+ fn on_window_activation_changed(&mut self, active: bool, cx: &mut ViewContext<Self>) {
+ if !active && cx.global::<Settings>().autosave == Autosave::OnWindowChange {
+ self.autosave(cx).detach_and_log_err(cx);
+ }
+ }
+
+ fn autosave(&mut self, cx: &mut ViewContext<Self>) -> Task<Result<()>> {
+ if let Some(project) = self.project.clone() {
+ if self.buffer.read(cx).is_dirty(cx)
+ && !self.buffer.read(cx).has_conflict(cx)
+ && workspace::Item::can_save(self, cx)
+ {
+ return workspace::Item::save(self, project, cx);
+ }
+ }
+
+ Task::ready(Ok(()))
+ }
+
pub fn set_searchable(&mut self, searchable: bool) {
self.searchable = searchable;
}
@@ -5805,6 +5862,10 @@ impl View for Editor {
hide_hover(self, cx);
cx.emit(Event::Blurred);
cx.notify();
+
+ if cx.global::<Settings>().autosave == Autosave::OnFocusChange {
+ self.autosave(cx).detach_and_log_err(cx);
+ }
}
fn keymap_context(&self, _: &AppContext) -> gpui::keymap::Context {
@@ -6218,22 +6279,23 @@ mod tests {
use super::*;
use futures::StreamExt;
use gpui::{
+ executor::Deterministic,
geometry::rect::RectF,
platform::{WindowBounds, WindowOptions},
};
use indoc::indoc;
use language::{FakeLspAdapter, LanguageConfig};
use lsp::FakeLanguageServer;
- use project::FakeFs;
+ use project::{FakeFs, Fs};
use settings::LanguageSettings;
- use std::{cell::RefCell, rc::Rc, time::Instant};
+ use std::{cell::RefCell, path::Path, rc::Rc, time::Instant};
use text::Point;
use unindent::Unindent;
use util::{
assert_set_eq,
test::{marked_text_by, marked_text_ranges, marked_text_ranges_by, sample_text},
};
- use workspace::{FollowableItem, ItemHandle};
+ use workspace::{FollowableItem, Item, ItemHandle};
#[gpui::test]
fn test_edit_events(cx: &mut MutableAppContext) {
@@ -6572,8 +6634,8 @@ mod tests {
editor.read(cx).selections.ranges(cx)
);
assert_set_eq!(
- cloned_editor.update(cx, |e, cx| dbg!(e.selections.display_ranges(cx))),
- editor.update(cx, |e, cx| dbg!(e.selections.display_ranges(cx)))
+ cloned_editor.update(cx, |e, cx| e.selections.display_ranges(cx)),
+ editor.update(cx, |e, cx| e.selections.display_ranges(cx))
);
}
@@ -9497,6 +9559,72 @@ mod tests {
save.await.unwrap();
}
+ #[gpui::test]
+ async fn test_autosave(deterministic: Arc<Deterministic>, cx: &mut gpui::TestAppContext) {
+ deterministic.forbid_parking();
+
+ let fs = FakeFs::new(cx.background().clone());
+ fs.insert_file("/file.rs", Default::default()).await;
+
+ let project = Project::test(fs.clone(), ["/file.rs".as_ref()], cx).await;
+ let buffer = project
+ .update(cx, |project, cx| project.open_local_buffer("/file.rs", cx))
+ .await
+ .unwrap();
+
+ let (_, editor) = cx.add_window(|cx| Editor::for_buffer(buffer, Some(project), cx));
+
+ // Autosave on window change.
+ editor.update(cx, |editor, cx| {
+ cx.update_global(|settings: &mut Settings, _| {
+ settings.autosave = Autosave::OnWindowChange;
+ });
+ editor.insert("X", cx);
+ assert!(editor.is_dirty(cx))
+ });
+
+ // Deactivating the window saves the file.
+ cx.simulate_window_activation(None);
+ deterministic.run_until_parked();
+ assert_eq!(fs.load(Path::new("/file.rs")).await.unwrap(), "X");
+ editor.read_with(cx, |editor, cx| assert!(!editor.is_dirty(cx)));
+
+ // Autosave on focus change.
+ editor.update(cx, |editor, cx| {
+ cx.focus_self();
+ cx.update_global(|settings: &mut Settings, _| {
+ settings.autosave = Autosave::OnFocusChange;
+ });
+ editor.insert("X", cx);
+ assert!(editor.is_dirty(cx))
+ });
+
+ // Blurring the editor saves the file.
+ editor.update(cx, |_, cx| cx.blur());
+ deterministic.run_until_parked();
+ assert_eq!(fs.load(Path::new("/file.rs")).await.unwrap(), "XX");
+ editor.read_with(cx, |editor, cx| assert!(!editor.is_dirty(cx)));
+
+ // Autosave after delay.
+ editor.update(cx, |editor, cx| {
+ cx.update_global(|settings: &mut Settings, _| {
+ settings.autosave = Autosave::AfterDelay { milliseconds: 500 };
+ });
+ editor.insert("X", cx);
+ assert!(editor.is_dirty(cx))
+ });
+
+ // Delay hasn't fully expired, so the file is still dirty and unsaved.
+ deterministic.advance_clock(Duration::from_millis(250));
+ assert_eq!(fs.load(Path::new("/file.rs")).await.unwrap(), "XX");
+ editor.read_with(cx, |editor, cx| assert!(editor.is_dirty(cx)));
+
+ // After delay expires, the file is saved.
+ deterministic.advance_clock(Duration::from_millis(250));
+ assert_eq!(fs.load(Path::new("/file.rs")).await.unwrap(), "XXX");
+ editor.read_with(cx, |editor, cx| assert!(!editor.is_dirty(cx)));
+ }
+
#[gpui::test]
async fn test_completion(cx: &mut gpui::TestAppContext) {
let mut language = Language::new(
@@ -401,9 +401,12 @@ impl TestAppContext {
T: View,
F: FnOnce(&mut ViewContext<T>) -> T,
{
- self.cx
+ let (window_id, view) = self
+ .cx
.borrow_mut()
- .add_window(Default::default(), build_root_view)
+ .add_window(Default::default(), build_root_view);
+ self.simulate_window_activation(Some(window_id));
+ (window_id, view)
}
pub fn window_ids(&self) -> Vec<usize> {
@@ -551,6 +554,35 @@ impl TestAppContext {
}
}
+ pub fn simulate_window_activation(&self, to_activate: Option<usize>) {
+ let mut handlers = BTreeMap::new();
+ {
+ let mut cx = self.cx.borrow_mut();
+ for (window_id, (_, window)) in &mut cx.presenters_and_platform_windows {
+ let window = window
+ .as_any_mut()
+ .downcast_mut::<platform::test::Window>()
+ .unwrap();
+ handlers.insert(
+ *window_id,
+ mem::take(&mut window.active_status_change_handlers),
+ );
+ }
+ };
+ let mut handlers = handlers.into_iter().collect::<Vec<_>>();
+ handlers.sort_unstable_by_key(|(window_id, _)| Some(*window_id) == to_activate);
+
+ for (window_id, mut window_handlers) in handlers {
+ for window_handler in &mut window_handlers {
+ window_handler(Some(window_id) == to_activate);
+ }
+
+ self.window_mut(window_id)
+ .active_status_change_handlers
+ .extend(window_handlers);
+ }
+ }
+
pub fn is_window_edited(&self, window_id: usize) -> bool {
self.window_mut(window_id).edited
}
@@ -783,6 +815,7 @@ type FocusObservationCallback = Box<dyn FnMut(&mut MutableAppContext) -> bool>;
type GlobalObservationCallback = Box<dyn FnMut(&mut MutableAppContext)>;
type ReleaseObservationCallback = Box<dyn FnOnce(&dyn Any, &mut MutableAppContext)>;
type ActionObservationCallback = Box<dyn FnMut(TypeId, &mut MutableAppContext)>;
+type WindowActivationCallback = Box<dyn FnMut(bool, &mut MutableAppContext) -> bool>;
type DeserializeActionCallback = fn(json: &str) -> anyhow::Result<Box<dyn Action>>;
type WindowShouldCloseSubscriptionCallback = Box<dyn FnMut(&mut MutableAppContext) -> bool>;
@@ -809,6 +842,8 @@ pub struct MutableAppContext {
global_observations:
Arc<Mutex<HashMap<TypeId, BTreeMap<usize, Option<GlobalObservationCallback>>>>>,
release_observations: Arc<Mutex<HashMap<usize, BTreeMap<usize, ReleaseObservationCallback>>>>,
+ window_activation_observations:
+ Arc<Mutex<HashMap<usize, BTreeMap<usize, Option<WindowActivationCallback>>>>>,
action_dispatch_observations: Arc<Mutex<BTreeMap<usize, ActionObservationCallback>>>,
presenters_and_platform_windows:
HashMap<usize, (Rc<RefCell<Presenter>>, Box<dyn platform::Window>)>,
@@ -862,6 +897,7 @@ impl MutableAppContext {
focus_observations: Default::default(),
release_observations: Default::default(),
global_observations: Default::default(),
+ window_activation_observations: Default::default(),
action_dispatch_observations: Default::default(),
presenters_and_platform_windows: HashMap::new(),
foreground,
@@ -1358,6 +1394,24 @@ impl MutableAppContext {
}
}
+ fn observe_window_activation<F>(&mut self, window_id: usize, callback: F) -> Subscription
+ where
+ F: 'static + FnMut(bool, &mut MutableAppContext) -> bool,
+ {
+ let subscription_id = post_inc(&mut self.next_subscription_id);
+ self.pending_effects
+ .push_back(Effect::WindowActivationObservation {
+ window_id,
+ subscription_id,
+ callback: Box::new(callback),
+ });
+ Subscription::WindowActivationObservation {
+ id: subscription_id,
+ window_id,
+ observations: Some(Arc::downgrade(&self.window_activation_observations)),
+ }
+ }
+
pub fn defer(&mut self, callback: impl 'static + FnOnce(&mut MutableAppContext)) {
self.pending_effects.push_back(Effect::Deferred {
callback: Box::new(callback),
@@ -1715,19 +1769,16 @@ impl MutableAppContext {
self.update(|this| {
let window_id = post_inc(&mut this.next_window_id);
let root_view = this.add_view(window_id, build_root_view);
-
this.cx.windows.insert(
window_id,
Window {
root_view: root_view.clone().into(),
focused_view_id: Some(root_view.id()),
- is_active: true,
+ is_active: false,
invalidation: None,
},
);
- root_view.update(this, |view, cx| {
- view.on_focus(cx);
- });
+ root_view.update(this, |view, cx| view.on_focus(cx));
this.open_platform_window(window_id, window_options);
(window_id, root_view)
@@ -1995,10 +2046,19 @@ impl MutableAppContext {
.get_or_insert(WindowInvalidation::default());
}
}
+ Effect::WindowActivationObservation {
+ window_id,
+ subscription_id,
+ callback,
+ } => self.handle_window_activation_observation_effect(
+ window_id,
+ subscription_id,
+ callback,
+ ),
Effect::ActivateWindow {
window_id,
is_active,
- } => self.handle_activation_effect(window_id, is_active),
+ } => self.handle_window_activation_effect(window_id, is_active),
Effect::RefreshWindows => {
refreshing = true;
}
@@ -2361,7 +2421,31 @@ impl MutableAppContext {
}
}
- fn handle_activation_effect(&mut self, window_id: usize, active: bool) {
+ fn handle_window_activation_observation_effect(
+ &mut self,
+ window_id: usize,
+ subscription_id: usize,
+ callback: WindowActivationCallback,
+ ) {
+ match self
+ .window_activation_observations
+ .lock()
+ .entry(window_id)
+ .or_default()
+ .entry(subscription_id)
+ {
+ btree_map::Entry::Vacant(entry) => {
+ entry.insert(Some(callback));
+ }
+ // Observation was dropped before effect was processed
+ btree_map::Entry::Occupied(entry) => {
+ debug_assert!(entry.get().is_none());
+ entry.remove();
+ }
+ }
+ }
+
+ fn handle_window_activation_effect(&mut self, window_id: usize, active: bool) {
if self
.cx
.windows
@@ -2385,6 +2469,34 @@ impl MutableAppContext {
this.cx.views.insert((window_id, view_id), view);
}
+ let callbacks = this
+ .window_activation_observations
+ .lock()
+ .remove(&window_id);
+ if let Some(callbacks) = callbacks {
+ for (id, callback) in callbacks {
+ if let Some(mut callback) = callback {
+ let alive = callback(active, this);
+ if alive {
+ match this
+ .window_activation_observations
+ .lock()
+ .entry(window_id)
+ .or_default()
+ .entry(id)
+ {
+ btree_map::Entry::Vacant(entry) => {
+ entry.insert(Some(callback));
+ }
+ btree_map::Entry::Occupied(entry) => {
+ entry.remove();
+ }
+ }
+ }
+ }
+ }
+ }
+
Some(())
});
}
@@ -2842,6 +2954,11 @@ pub enum Effect {
window_id: usize,
is_active: bool,
},
+ WindowActivationObservation {
+ window_id: usize,
+ subscription_id: usize,
+ callback: WindowActivationCallback,
+ },
RefreshWindows,
ActionDispatchNotification {
action_id: TypeId,
@@ -2934,6 +3051,15 @@ impl Debug for Effect {
.debug_struct("Effect::RefreshWindow")
.field("window_id", window_id)
.finish(),
+ Effect::WindowActivationObservation {
+ window_id,
+ subscription_id,
+ ..
+ } => f
+ .debug_struct("Effect::WindowActivationObservation")
+ .field("window_id", window_id)
+ .field("subscription_id", subscription_id)
+ .finish(),
Effect::ActivateWindow {
window_id,
is_active,
@@ -3518,6 +3644,24 @@ impl<'a, T: View> ViewContext<'a, T> {
})
}
+ pub fn observe_window_activation<F>(&mut self, mut callback: F) -> Subscription
+ where
+ F: 'static + FnMut(&mut T, bool, &mut ViewContext<T>),
+ {
+ let observer = self.weak_handle();
+ self.app
+ .observe_window_activation(self.window_id(), move |active, cx| {
+ if let Some(observer) = observer.upgrade(cx) {
+ observer.update(cx, |observer, cx| {
+ callback(observer, active, cx);
+ });
+ true
+ } else {
+ false
+ }
+ })
+ }
+
pub fn emit(&mut self, payload: T::Event) {
self.app.pending_effects.push_back(Effect::Event {
entity_id: self.view_id,
@@ -4451,7 +4595,7 @@ impl AnyViewHandle {
handle_id: self.handle_id,
});
unsafe {
- Arc::decrement_strong_count(&self.ref_counts);
+ Arc::decrement_strong_count(Arc::as_ptr(&self.ref_counts));
}
std::mem::forget(self);
result
@@ -4517,8 +4661,9 @@ impl<T: View> From<ViewHandle<T>> for AnyViewHandle {
#[cfg(any(test, feature = "test-support"))]
handle_id: handle.handle_id,
};
+
unsafe {
- Arc::decrement_strong_count(&handle.ref_counts);
+ Arc::decrement_strong_count(Arc::as_ptr(&handle.ref_counts));
}
std::mem::forget(handle);
any_handle
@@ -4580,7 +4725,7 @@ impl AnyModelHandle {
handle_id: self.handle_id,
});
unsafe {
- Arc::decrement_strong_count(&self.ref_counts);
+ Arc::decrement_strong_count(Arc::as_ptr(&self.ref_counts));
}
std::mem::forget(self);
result
@@ -4832,6 +4977,12 @@ pub enum Subscription {
id: usize,
observations: Option<Weak<Mutex<BTreeMap<usize, ActionObservationCallback>>>>,
},
+ WindowActivationObservation {
+ id: usize,
+ window_id: usize,
+ observations:
+ Option<Weak<Mutex<HashMap<usize, BTreeMap<usize, Option<WindowActivationCallback>>>>>>,
+ },
}
impl Subscription {
@@ -4858,6 +5009,9 @@ impl Subscription {
Subscription::ActionObservation { observations, .. } => {
observations.take();
}
+ Subscription::WindowActivationObservation { observations, .. } => {
+ observations.take();
+ }
}
}
}
@@ -4971,6 +5125,27 @@ impl Drop for Subscription {
observations.lock().remove(&id);
}
}
+ Subscription::WindowActivationObservation {
+ id,
+ window_id,
+ observations,
+ } => {
+ if let Some(observations) = observations.as_ref().and_then(Weak::upgrade) {
+ match observations
+ .lock()
+ .entry(*window_id)
+ .or_default()
+ .entry(*id)
+ {
+ btree_map::Entry::Vacant(entry) => {
+ entry.insert(None);
+ }
+ btree_map::Entry::Occupied(entry) => {
+ entry.remove();
+ }
+ }
+ }
+ }
}
}
}
@@ -6849,4 +7024,81 @@ mod tests {
);
assert_eq!(presenter.borrow().rendered_views.len(), 1);
}
+
+ #[crate::test(self)]
+ async fn test_window_activation(cx: &mut TestAppContext) {
+ struct View(&'static str);
+
+ impl super::Entity for View {
+ type Event = ();
+ }
+
+ impl super::View for View {
+ fn ui_name() -> &'static str {
+ "test view"
+ }
+
+ fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
+ Empty::new().boxed()
+ }
+ }
+
+ let events = Rc::new(RefCell::new(Vec::new()));
+ let (window_1, _) = cx.add_window(|cx: &mut ViewContext<View>| {
+ cx.observe_window_activation({
+ let events = events.clone();
+ move |this, active, _| events.borrow_mut().push((this.0, active))
+ })
+ .detach();
+ View("window 1")
+ });
+ assert_eq!(mem::take(&mut *events.borrow_mut()), [("window 1", true)]);
+
+ let (window_2, _) = cx.add_window(|cx: &mut ViewContext<View>| {
+ cx.observe_window_activation({
+ let events = events.clone();
+ move |this, active, _| events.borrow_mut().push((this.0, active))
+ })
+ .detach();
+ View("window 2")
+ });
+ assert_eq!(
+ mem::take(&mut *events.borrow_mut()),
+ [("window 1", false), ("window 2", true)]
+ );
+
+ let (window_3, _) = cx.add_window(|cx: &mut ViewContext<View>| {
+ cx.observe_window_activation({
+ let events = events.clone();
+ move |this, active, _| events.borrow_mut().push((this.0, active))
+ })
+ .detach();
+ View("window 3")
+ });
+ assert_eq!(
+ mem::take(&mut *events.borrow_mut()),
+ [("window 2", false), ("window 3", true)]
+ );
+
+ cx.simulate_window_activation(Some(window_2));
+ assert_eq!(
+ mem::take(&mut *events.borrow_mut()),
+ [("window 3", false), ("window 2", true)]
+ );
+
+ cx.simulate_window_activation(Some(window_1));
+ assert_eq!(
+ mem::take(&mut *events.borrow_mut()),
+ [("window 2", false), ("window 1", true)]
+ );
+
+ cx.simulate_window_activation(Some(window_3));
+ assert_eq!(
+ mem::take(&mut *events.borrow_mut()),
+ [("window 1", false), ("window 3", true)]
+ );
+
+ cx.simulate_window_activation(Some(window_3));
+ assert_eq!(mem::take(&mut *events.borrow_mut()), []);
+ }
}
@@ -675,9 +675,7 @@ impl Background {
}
}
_ => {
- log::info!("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
-
- // panic!("this method can only be called on a deterministic executor")
+ panic!("this method can only be called on a deterministic executor")
}
}
}
@@ -37,6 +37,7 @@ pub struct Window {
event_handlers: Vec<Box<dyn FnMut(super::Event) -> bool>>,
resize_handlers: Vec<Box<dyn FnMut()>>,
close_handlers: Vec<Box<dyn FnOnce()>>,
+ pub(crate) active_status_change_handlers: Vec<Box<dyn FnMut(bool)>>,
pub(crate) should_close_handler: Option<Box<dyn FnMut() -> bool>>,
pub(crate) title: Option<String>,
pub(crate) edited: bool,
@@ -191,6 +192,7 @@ impl Window {
resize_handlers: Default::default(),
close_handlers: Default::default(),
should_close_handler: Default::default(),
+ active_status_change_handlers: Default::default(),
scale_factor: 1.0,
current_scene: None,
title: None,
@@ -241,7 +243,9 @@ impl super::Window for Window {
self.event_handlers.push(callback);
}
- fn on_active_status_change(&mut self, _: Box<dyn FnMut(bool)>) {}
+ fn on_active_status_change(&mut self, callback: Box<dyn FnMut(bool)>) {
+ self.active_status_change_handlers.push(callback);
+ }
fn on_resize(&mut self, callback: Box<dyn FnMut()>) {
self.resize_handlers.push(callback);
@@ -25,6 +25,7 @@ pub struct Settings {
pub default_buffer_font_size: f32,
pub hover_popover_enabled: bool,
pub vim_mode: bool,
+ pub autosave: Autosave,
pub language_settings: LanguageSettings,
pub language_defaults: HashMap<Arc<str>, LanguageSettings>,
pub language_overrides: HashMap<Arc<str>, LanguageSettings>,
@@ -49,6 +50,15 @@ pub enum SoftWrap {
PreferredLineLength,
}
+#[derive(Copy, Clone, Debug, Deserialize, PartialEq, Eq, JsonSchema)]
+#[serde(rename_all = "snake_case")]
+pub enum Autosave {
+ Off,
+ AfterDelay { milliseconds: u64 },
+ OnFocusChange,
+ OnWindowChange,
+}
+
#[derive(Clone, Debug, Default, Deserialize, JsonSchema)]
pub struct SettingsFileContent {
#[serde(default)]
@@ -64,6 +74,8 @@ pub struct SettingsFileContent {
#[serde(default)]
pub format_on_save: Option<bool>,
#[serde(default)]
+ pub autosave: Option<Autosave>,
+ #[serde(default)]
pub enable_language_server: Option<bool>,
#[serde(flatten)]
pub editor: LanguageSettings,
@@ -85,6 +97,7 @@ impl Settings {
default_buffer_font_size: 15.,
hover_popover_enabled: true,
vim_mode: false,
+ autosave: Autosave::Off,
language_settings: Default::default(),
language_defaults: Default::default(),
language_overrides: Default::default(),
@@ -157,6 +170,7 @@ impl Settings {
default_buffer_font_size: 14.,
hover_popover_enabled: true,
vim_mode: false,
+ autosave: Autosave::Off,
language_settings: Default::default(),
language_defaults: Default::default(),
language_overrides: Default::default(),
@@ -198,6 +212,7 @@ impl Settings {
merge(&mut self.default_buffer_font_size, data.buffer_font_size);
merge(&mut self.hover_popover_enabled, data.hover_popover_enabled);
merge(&mut self.vim_mode, data.vim_mode);
+ merge(&mut self.autosave, data.autosave);
merge_option(
&mut self.language_settings.format_on_save,
data.format_on_save,
@@ -97,7 +97,6 @@ pub fn init(app_state: &Arc<AppState>, cx: &mut gpui::MutableAppContext) {
cx.add_action({
let app_state = app_state.clone();
move |_: &mut Workspace, _: &OpenSettings, cx: &mut ViewContext<Workspace>| {
- println!("open settings");
open_config_file(&SETTINGS_PATH, app_state.clone(), cx);
}
});