Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat: add an update download policy

Ilya90564b45f91d
diff --git a/apps/desktop-gpui/src/settings_window.rs b/apps/desktop-gpui/src/settings_window.rs
index d39d24b5a..ccebc63e5 100644
--- a/apps/desktop-gpui/src/settings_window.rs
+++ b/apps/desktop-gpui/src/settings_window.rs
@@ -38,7 +38,7 @@ use crate::{
store::{
self, AppTheme, DEFAULT_PROJECT_NAME_TEMPLATE, DEFAULT_SERVER_URL, GENERAL_SETTINGS,
GeneralSettings, MainWindowStartBehaviour, PostDeletionBehaviour, RECORDING_START_SAFETY,
- SettingsEnum, StudioQuality, UpdateChannel, WindowExclusion,
+ SettingsEnum, StudioQuality, UpdateChannel, UpdateDownloadPolicy, WindowExclusion,
},
theme::Theme,
ui,
@@ -4392,6 +4392,7 @@ impl SettingsWindow {
fn render_updates(&self, cx: &mut Context<Self>) -> impl IntoElement {
let theme = self.theme;
let current = self.settings.update_channel;
+ let current_policy = self.settings.update_download_policy;
let description = UPDATE_CHANNEL_DESCRIPTIONS
.iter()
.find(|(channel, _)| *channel == current)
@@ -4447,6 +4448,42 @@ impl SettingsWindow {
},
)),
)
+ .child(
+ div()
+ .flex()
+ .flex_row()
+ .justify_between()
+ .items_start()
+ .gap(px(16.))
+ .child(
+ div()
+ .flex()
+ .flex_col()
+ .gap(px(2.))
+ .min_w_0()
+ .child(div().text_size(px(13.)).child("Update downloads"))
+ .child(
+ div()
+ .text_size(px(12.))
+ .line_height(px(16.))
+ .text_color(theme.settings_muted())
+ .child("Choose whether Cap downloads updates automatically."),
+ ),
+ )
+ .child(self.segmented::<UpdateDownloadPolicy>(
+ "update-download-policy",
+ current_policy,
+ cx,
+ |this, value, cx| {
+ if this.settings.update_download_policy == value {
+ return;
+ }
+ this.settings.update_download_policy = value;
+ this.write_enum("updateDownloadPolicy", value, cx);
+ crate::updates::update_download_policy_changed(value, cx);
+ },
+ )),
+ )
.child(
self.note_box()
.child(div().text_size(px(12.)).child(description))
diff --git a/apps/desktop-gpui/src/store.rs b/apps/desktop-gpui/src/store.rs
index 5701c4e4d..5d10069aa 100644
--- a/apps/desktop-gpui/src/store.rs
+++ b/apps/desktop-gpui/src/store.rs
@@ -843,6 +843,16 @@ settings_enum! {
default = Stable
}
+settings_enum! {
+ /// How Cap reacts to an available update.
+ UpdateDownloadPolicy {
+ Manual = "manual", "Manual";
+ Auto = "auto", "Auto";
+ Never = "never", "Never";
+ }
+ default = Manual
+}
+
settings_enum! {
EditorPreviewQuality {
Quarter = "quarter", "Quarter";
@@ -1029,6 +1039,7 @@ pub struct GeneralSettings {
pub default_project_name_template: Option<String>,
pub excluded_windows: Vec<WindowExclusion>,
pub update_channel: UpdateChannel,
+ pub update_download_policy: UpdateDownloadPolicy,
pub server_url: String,
pub enable_telemetry: bool,
pub editor_preview_quality: EditorPreviewQuality,
@@ -1379,6 +1390,7 @@ impl GeneralSettings {
})
.unwrap_or_else(default_excluded_windows),
update_channel: enum_at(general, "updateChannel"),
+ update_download_policy: enum_at(general, "updateDownloadPolicy"),
server_url: string_at(general, "serverUrl", DEFAULT_SERVER_URL),
enable_telemetry: bool_at(general, "enableTelemetry", true),
editor_preview_quality: enum_at(general, "editorPreviewQuality"),
diff --git a/apps/desktop-gpui/src/updates.rs b/apps/desktop-gpui/src/updates.rs
index 3c94078ed..c00b8cda3 100644
--- a/apps/desktop-gpui/src/updates.rs
+++ b/apps/desktop-gpui/src/updates.rs
@@ -7,7 +7,7 @@ use semver::Version;
use crate::{
installer::{self, ArtifactKind, Release},
session::RecordingSession,
- store::{GeneralSettings, UpdateChannel},
+ store::{GeneralSettings, UpdateChannel, UpdateDownloadPolicy},
};
const STABLE_FIRST_CHECK_DELAY: Duration = Duration::from_secs(10);
@@ -71,6 +71,7 @@ struct UpdateScheduler {
pending: PendingUpdateRequests,
manual_in_flight: bool,
status: UpdateStatus,
+ download_policy: UpdateDownloadPolicy,
staged: Option<StagedUpdate>,
download: Option<gpui::Task<()>>,
download_epoch: u64,
@@ -291,6 +292,10 @@ fn simulate_update(version: Version, cx: &mut App) {
},
cx,
);
+ if GeneralSettings::load().update_download_policy != UpdateDownloadPolicy::Auto {
+ tracing::info!("CAP_GPUI_UPDATE_SIMULATION_VERSION: manual policy, waiting");
+ return;
+ }
cx.spawn(async move |cx| {
cx.background_executor().timer(Duration::from_secs(3)).await;
cx.update(|cx| {
@@ -332,6 +337,7 @@ pub(crate) fn schedule_startup_check(cx: &mut App) {
pending: PendingUpdateRequests::default(),
manual_in_flight: false,
status: UpdateStatus::Idle,
+ download_policy: GeneralSettings::load().update_download_policy,
staged: None,
download: None,
download_epoch: 0,
@@ -352,7 +358,13 @@ pub(crate) fn schedule_startup_check(cx: &mut App) {
.background_executor()
.spawn(async { GeneralSettings::load().update_channel })
.await;
- let mut delay = (!cfg!(debug_assertions)).then(|| first_check_delay(channel));
+ let mut policy = cx
+ .update(|cx| cx.global::<UpdateScheduler>().download_policy);
+ let mut delay = if policy == UpdateDownloadPolicy::Never {
+ None
+ } else {
+ (!cfg!(debug_assertions)).then(|| first_check_delay(channel))
+ };
let mut ignored_version: Option<Version> = None;
loop {
@@ -380,6 +392,16 @@ pub(crate) fn schedule_startup_check(cx: &mut App) {
std::mem::take(&mut scheduler.pending)
});
+ let policy_now = cx.update(|cx| cx.global::<UpdateScheduler>().download_policy);
+ if policy_now != policy {
+ policy = policy_now;
+ delay = if policy == UpdateDownloadPolicy::Never {
+ None
+ } else {
+ next_check_delay(channel)
+ };
+ }
+
if signaled && request == PendingUpdateRequests::default() {
continue;
}
@@ -391,6 +413,10 @@ pub(crate) fn schedule_startup_check(cx: &mut App) {
}
let manual = request.manual;
+ if policy == UpdateDownloadPolicy::Never && !manual {
+ delay = None;
+ continue;
+ }
if cfg!(debug_assertions) && !manual {
delay = None;
continue;
@@ -489,6 +515,12 @@ pub(crate) fn schedule_startup_check(cx: &mut App) {
continue;
}
+ if policy == UpdateDownloadPolicy::Auto {
+ cx.update(|cx| finish_manual_check(cx, manual));
+ cx.update(|cx| start_download(release, cx));
+ continue;
+ }
+
cx.update(|cx| set_status(UpdateStatus::Idle, cx));
crate::platform::activate_app();
@@ -513,6 +545,14 @@ pub(crate) fn schedule_startup_check(cx: &mut App) {
.detach();
}
+pub(crate) fn update_download_policy_changed(policy: UpdateDownloadPolicy, cx: &mut App) {
+ if !cx.has_global::<UpdateScheduler>() {
+ return;
+ }
+ cx.global_mut::<UpdateScheduler>().download_policy = policy;
+ let _ = cx.global::<UpdateScheduler>().wake.try_send(());
+}
+
fn start_download(release: Release, cx: &mut App) {
let kind = match installer::update_platform()
.and_then(|platform| ArtifactKind::for_platform(&platform))