Reduce the frequency writing window size and position information to the database (#10315)

Release Notes:

- N/A
This commit is contained in:
张小白 2024-04-11 02:56:51 +08:00 committed by GitHub
parent 081e9b9a60
commit 3648d79ddb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -571,6 +571,7 @@ pub struct Workspace {
_schedule_serialize: Option<Task<()>>, _schedule_serialize: Option<Task<()>>,
pane_history_timestamp: Arc<AtomicUsize>, pane_history_timestamp: Arc<AtomicUsize>,
bounds: Bounds<Pixels>, bounds: Bounds<Pixels>,
bounds_save_task_queued: Option<Task<()>>,
} }
impl EventEmitter<Event> for Workspace {} impl EventEmitter<Event> for Workspace {}
@ -737,33 +738,45 @@ impl Workspace {
let subscriptions = vec![ let subscriptions = vec![
cx.observe_window_activation(Self::on_window_activation_changed), cx.observe_window_activation(Self::on_window_activation_changed),
cx.observe_window_bounds(move |_, cx| { cx.observe_window_bounds(move |this, cx| {
if let Some(display) = cx.display() { if this.bounds_save_task_queued.is_some() {
let window_bounds = cx.window_bounds(); return;
let fullscreen = cx.is_fullscreen();
if let Some(display_uuid) = display.uuid().log_err() {
// Only update the window bounds when not full screen,
// so we can remember the last non-fullscreen bounds
// across restarts
if fullscreen {
cx.background_executor()
.spawn(DB.set_fullscreen(workspace_id, true))
.detach_and_log_err(cx);
} else if !cx.is_minimized() {
cx.background_executor()
.spawn(DB.set_fullscreen(workspace_id, false))
.detach_and_log_err(cx);
cx.background_executor()
.spawn(DB.set_window_bounds(
workspace_id,
SerializedWindowsBounds(window_bounds),
display_uuid,
))
.detach_and_log_err(cx);
}
}
} }
this.bounds_save_task_queued = Some(cx.spawn(|this, mut cx| async move {
cx.background_executor()
.timer(Duration::from_millis(100))
.await;
this.update(&mut cx, |this, cx| {
if let Some(display) = cx.display() {
let window_bounds = cx.window_bounds();
let fullscreen = cx.is_fullscreen();
if let Some(display_uuid) = display.uuid().log_err() {
// Only update the window bounds when not full screen,
// so we can remember the last non-fullscreen bounds
// across restarts
if fullscreen {
cx.background_executor()
.spawn(DB.set_fullscreen(workspace_id, true))
.detach_and_log_err(cx);
} else if !cx.is_minimized() {
cx.background_executor()
.spawn(DB.set_fullscreen(workspace_id, false))
.detach_and_log_err(cx);
cx.background_executor()
.spawn(DB.set_window_bounds(
workspace_id,
SerializedWindowsBounds(window_bounds),
display_uuid,
))
.detach_and_log_err(cx);
}
}
}
this.bounds_save_task_queued.take();
})
.ok();
}));
cx.notify(); cx.notify();
}), }),
cx.observe_window_appearance(|_, cx| { cx.observe_window_appearance(|_, cx| {
@ -830,6 +843,7 @@ impl Workspace {
workspace_actions: Default::default(), workspace_actions: Default::default(),
// This data will be incorrect, but it will be overwritten by the time it needs to be used. // This data will be incorrect, but it will be overwritten by the time it needs to be used.
bounds: Default::default(), bounds: Default::default(),
bounds_save_task_queued: None,
} }
} }