edit_prediction_ui.rs

  1mod edit_prediction_button;
  2mod edit_prediction_context_view;
  3mod external_provider_api_token_modal;
  4mod rate_prediction_modal;
  5
  6use std::any::{Any as _, TypeId};
  7
  8use command_palette_hooks::CommandPaletteFilter;
  9use edit_prediction::{ResetOnboarding, Zeta2FeatureFlag};
 10use edit_prediction_context_view::EditPredictionContextView;
 11use feature_flags::FeatureFlagAppExt as _;
 12use gpui::actions;
 13use project::DisableAiSettings;
 14use rate_prediction_modal::RatePredictionsModal;
 15use settings::{Settings as _, SettingsStore};
 16use ui::{App, prelude::*};
 17use workspace::{SplitDirection, Workspace};
 18
 19pub use edit_prediction_button::{EditPredictionButton, ToggleMenu};
 20pub use external_provider_api_token_modal::ExternalProviderApiKeyModal;
 21
 22use crate::rate_prediction_modal::PredictEditsRatePredictionsFeatureFlag;
 23
 24actions!(
 25    dev,
 26    [
 27        /// Opens the edit prediction context view.
 28        OpenEditPredictionContextView,
 29    ]
 30);
 31
 32actions!(
 33    edit_prediction,
 34    [
 35        /// Opens the rate completions modal.
 36        RatePredictions,
 37    ]
 38);
 39
 40pub fn init(cx: &mut App) {
 41    feature_gate_predict_edits_actions(cx);
 42
 43    cx.observe_new(move |workspace: &mut Workspace, _, _cx| {
 44        workspace.register_action(|workspace, _: &RatePredictions, window, cx| {
 45            if cx.has_flag::<PredictEditsRatePredictionsFeatureFlag>() {
 46                RatePredictionsModal::toggle(workspace, window, cx);
 47            }
 48        });
 49
 50        workspace.register_action_renderer(|div, _, _, cx| {
 51            let has_flag = cx.has_flag::<Zeta2FeatureFlag>();
 52            div.when(has_flag, |div| {
 53                div.on_action(cx.listener(
 54                    move |workspace, _: &OpenEditPredictionContextView, window, cx| {
 55                        let project = workspace.project();
 56                        workspace.split_item(
 57                            SplitDirection::Right,
 58                            Box::new(cx.new(|cx| {
 59                                EditPredictionContextView::new(
 60                                    project.clone(),
 61                                    workspace.client(),
 62                                    workspace.user_store(),
 63                                    window,
 64                                    cx,
 65                                )
 66                            })),
 67                            window,
 68                            cx,
 69                        );
 70                    },
 71                ))
 72            })
 73        });
 74    })
 75    .detach();
 76}
 77
 78fn feature_gate_predict_edits_actions(cx: &mut App) {
 79    let rate_completion_action_types = [TypeId::of::<RatePredictions>()];
 80    let reset_onboarding_action_types = [TypeId::of::<ResetOnboarding>()];
 81    let all_action_types = [
 82        TypeId::of::<RatePredictions>(),
 83        TypeId::of::<edit_prediction::ResetOnboarding>(),
 84        zed_actions::OpenZedPredictOnboarding.type_id(),
 85        TypeId::of::<edit_prediction::ClearHistory>(),
 86        TypeId::of::<rate_prediction_modal::ThumbsUpActivePrediction>(),
 87        TypeId::of::<rate_prediction_modal::ThumbsDownActivePrediction>(),
 88        TypeId::of::<rate_prediction_modal::NextEdit>(),
 89        TypeId::of::<rate_prediction_modal::PreviousEdit>(),
 90    ];
 91
 92    CommandPaletteFilter::update_global(cx, |filter, _cx| {
 93        filter.hide_action_types(&rate_completion_action_types);
 94        filter.hide_action_types(&reset_onboarding_action_types);
 95        filter.hide_action_types(&[zed_actions::OpenZedPredictOnboarding.type_id()]);
 96    });
 97
 98    cx.observe_global::<SettingsStore>(move |cx| {
 99        let is_ai_disabled = DisableAiSettings::get_global(cx).disable_ai;
100        let has_feature_flag = cx.has_flag::<PredictEditsRatePredictionsFeatureFlag>();
101
102        CommandPaletteFilter::update_global(cx, |filter, _cx| {
103            if is_ai_disabled {
104                filter.hide_action_types(&all_action_types);
105            } else if has_feature_flag {
106                filter.show_action_types(&rate_completion_action_types);
107            } else {
108                filter.hide_action_types(&rate_completion_action_types);
109            }
110        });
111    })
112    .detach();
113
114    cx.observe_flag::<PredictEditsRatePredictionsFeatureFlag, _>(move |is_enabled, cx| {
115        if !DisableAiSettings::get_global(cx).disable_ai {
116            if is_enabled {
117                CommandPaletteFilter::update_global(cx, |filter, _cx| {
118                    filter.show_action_types(&rate_completion_action_types);
119                });
120            } else {
121                CommandPaletteFilter::update_global(cx, |filter, _cx| {
122                    filter.hide_action_types(&rate_completion_action_types);
123                });
124            }
125        }
126    })
127    .detach();
128}