Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Animate Zeta button while generating completions #22899

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 14 additions & 9 deletions crates/copilot/src/copilot_completion_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ pub struct CopilotCompletionProvider {
completions: Vec<Completion>,
active_completion_index: usize,
file_extension: Option<String>,
pending_refresh: Task<Result<()>>,
pending_cycling_refresh: Task<Result<()>>,
pending_refresh: Option<Task<Result<()>>>,
pending_cycling_refresh: Option<Task<Result<()>>>,
copilot: Model<Copilot>,
}

Expand All @@ -30,8 +30,8 @@ impl CopilotCompletionProvider {
completions: Vec::new(),
active_completion_index: 0,
file_extension: None,
pending_refresh: Task::ready(Ok(())),
pending_cycling_refresh: Task::ready(Ok(())),
pending_refresh: None,
pending_cycling_refresh: None,
copilot,
}
}
Expand Down Expand Up @@ -67,6 +67,10 @@ impl InlineCompletionProvider for CopilotCompletionProvider {
false
}

fn is_refreshing(&self) -> bool {
self.pending_refresh.is_some()
}

fn is_enabled(
&self,
buffer: &Model<Buffer>,
Expand All @@ -92,7 +96,7 @@ impl InlineCompletionProvider for CopilotCompletionProvider {
cx: &mut ModelContext<Self>,
) {
let copilot = self.copilot.clone();
self.pending_refresh = cx.spawn(|this, mut cx| async move {
self.pending_refresh = Some(cx.spawn(|this, mut cx| async move {
if debounce {
cx.background_executor()
.timer(COPILOT_DEBOUNCE_TIMEOUT)
Expand All @@ -108,7 +112,8 @@ impl InlineCompletionProvider for CopilotCompletionProvider {
this.update(&mut cx, |this, cx| {
if !completions.is_empty() {
this.cycled = false;
this.pending_cycling_refresh = Task::ready(Ok(()));
this.pending_refresh = None;
this.pending_cycling_refresh = None;
this.completions.clear();
this.active_completion_index = 0;
this.buffer_id = Some(buffer.entity_id());
Expand All @@ -129,7 +134,7 @@ impl InlineCompletionProvider for CopilotCompletionProvider {
})?;

Ok(())
});
}));
}

fn cycle(
Expand Down Expand Up @@ -161,7 +166,7 @@ impl InlineCompletionProvider for CopilotCompletionProvider {
cx.notify();
} else {
let copilot = self.copilot.clone();
self.pending_cycling_refresh = cx.spawn(|this, mut cx| async move {
self.pending_cycling_refresh = Some(cx.spawn(|this, mut cx| async move {
let completions = copilot
.update(&mut cx, |copilot, cx| {
copilot.completions_cycling(&buffer, cursor_position, cx)
Expand All @@ -185,7 +190,7 @@ impl InlineCompletionProvider for CopilotCompletionProvider {
})?;

Ok(())
});
}));
}
}

Expand Down
4 changes: 4 additions & 0 deletions crates/editor/src/inline_completion_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,10 @@ impl InlineCompletionProvider for FakeInlineCompletionProvider {
true
}

fn is_refreshing(&self) -> bool {
false
}

fn refresh(
&mut self,
_buffer: gpui::Model<language::Buffer>,
Expand Down
6 changes: 6 additions & 0 deletions crates/inline_completion/src/inline_completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub trait InlineCompletionProvider: 'static + Sized {
cursor_position: language::Anchor,
cx: &AppContext,
) -> bool;
fn is_refreshing(&self) -> bool;
fn refresh(
&mut self,
buffer: Model<Buffer>,
Expand Down Expand Up @@ -63,6 +64,7 @@ pub trait InlineCompletionProviderHandle {
) -> bool;
fn show_completions_in_menu(&self) -> bool;
fn show_completions_in_normal_mode(&self) -> bool;
fn is_refreshing(&self, cx: &AppContext) -> bool;
fn refresh(
&self,
buffer: Model<Buffer>,
Expand Down Expand Up @@ -116,6 +118,10 @@ where
self.read(cx).is_enabled(buffer, cursor_position, cx)
}

fn is_refreshing(&self, cx: &AppContext) -> bool {
self.read(cx).is_refreshing()
}

fn refresh(
&self,
buffer: Model<Buffer>,
Expand Down
1 change: 1 addition & 0 deletions crates/inline_completion_button/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ editor.workspace = true
feature_flags.workspace = true
fs.workspace = true
gpui.workspace = true
inline_completion.workspace = true
language.workspace = true
paths.workspace = true
settings.workspace = true
Expand Down
47 changes: 34 additions & 13 deletions crates/inline_completion_button/src/inline_completion_button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use editor::{scroll::Autoscroll, Editor};
use feature_flags::{FeatureFlagAppExt, ZetaFeatureFlag};
use fs::Fs;
use gpui::{
actions, div, Action, AppContext, AsyncWindowContext, Corner, Entity, IntoElement,
ParentElement, Render, Subscription, View, ViewContext, WeakView, WindowContext,
actions, div, pulsating_between, Action, Animation, AnimationExt, AppContext,
AsyncWindowContext, Corner, Entity, IntoElement, ParentElement, Render, Subscription, View,
ViewContext, WeakView, WindowContext,
};
use language::{
language_settings::{
Expand All @@ -14,7 +15,7 @@ use language::{
File, Language,
};
use settings::{update_settings_file, Settings, SettingsStore};
use std::{path::Path, sync::Arc};
use std::{path::Path, sync::Arc, time::Duration};
use supermaven::{AccountStatus, Supermaven};
use workspace::{
create_and_open_local_file,
Expand All @@ -39,6 +40,7 @@ pub struct InlineCompletionButton {
editor_enabled: Option<bool>,
language: Option<Arc<Language>>,
file: Option<Arc<dyn File>>,
inline_completion_provider: Option<Arc<dyn inline_completion::InlineCompletionProviderHandle>>,
fs: Arc<dyn Fs>,
workspace: WeakView<Workspace>,
}
Expand Down Expand Up @@ -205,17 +207,34 @@ impl Render for InlineCompletionButton {
}

let this = cx.view().clone();
div().child(
PopoverMenu::new("zeta")
.menu(move |cx| {
Some(this.update(cx, |this, cx| this.build_zeta_context_menu(cx)))
})
.anchor(Corner::BottomRight)
.trigger(
IconButton::new("zeta", IconName::ZedPredict)
.tooltip(|cx| Tooltip::text("Zed Predict", cx)),
let button = IconButton::new("zeta", IconName::ZedPredict)
.tooltip(|cx| Tooltip::text("Zed Predict", cx));

let is_refreshing = self
.inline_completion_provider
.as_ref()
.map_or(false, |provider| provider.is_refreshing(cx));

let mut popover_menu = PopoverMenu::new("zeta")
.menu(move |cx| {
Some(this.update(cx, |this, cx| this.build_zeta_context_menu(cx)))
})
.anchor(Corner::BottomRight);
if is_refreshing {
popover_menu = popover_menu.trigger(
button.with_animation(
"pulsating-label",
Animation::new(Duration::from_secs(2))
.repeat()
.with_easing(pulsating_between(0.2, 1.0)),
|icon_button, delta| icon_button.alpha(delta),
),
)
);
} else {
popover_menu = popover_menu.trigger(button);
}

div().child(popover_menu.into_any_element())
}
}
}
Expand All @@ -239,6 +258,7 @@ impl InlineCompletionButton {
editor_enabled: None,
language: None,
file: None,
inline_completion_provider: None,
workspace,
fs,
}
Expand Down Expand Up @@ -390,6 +410,7 @@ impl InlineCompletionButton {
),
)
};
self.inline_completion_provider = editor.inline_completion_provider();
self.language = language.cloned();
self.file = file;

Expand Down
17 changes: 11 additions & 6 deletions crates/supermaven/src/supermaven_completion_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct SupermavenCompletionProvider {
buffer_id: Option<EntityId>,
completion_id: Option<SupermavenCompletionStateId>,
file_extension: Option<String>,
pending_refresh: Task<Result<()>>,
pending_refresh: Option<Task<Result<()>>>,
}

impl SupermavenCompletionProvider {
Expand All @@ -29,7 +29,7 @@ impl SupermavenCompletionProvider {
buffer_id: None,
completion_id: None,
file_extension: None,
pending_refresh: Task::ready(Ok(())),
pending_refresh: None,
}
}
}
Expand Down Expand Up @@ -122,6 +122,10 @@ impl InlineCompletionProvider for SupermavenCompletionProvider {
settings.inline_completions_enabled(language.as_ref(), file.map(|f| f.path().as_ref()), cx)
}

fn is_refreshing(&self) -> bool {
self.pending_refresh.is_some()
}

fn refresh(
&mut self,
buffer_handle: Model<Buffer>,
Expand All @@ -135,7 +139,7 @@ impl InlineCompletionProvider for SupermavenCompletionProvider {
return;
};

self.pending_refresh = cx.spawn(|this, mut cx| async move {
self.pending_refresh = Some(cx.spawn(|this, mut cx| async move {
if debounce {
cx.background_executor().timer(DEBOUNCE_TIMEOUT).await;
}
Expand All @@ -152,11 +156,12 @@ impl InlineCompletionProvider for SupermavenCompletionProvider {
.to_string(),
)
});
this.pending_refresh = None;
cx.notify();
})?;
}
Ok(())
});
}));
}

fn cycle(
Expand All @@ -169,12 +174,12 @@ impl InlineCompletionProvider for SupermavenCompletionProvider {
}

fn accept(&mut self, _cx: &mut ModelContext<Self>) {
self.pending_refresh = Task::ready(Ok(()));
self.pending_refresh = None;
self.completion_id = None;
}

fn discard(&mut self, _cx: &mut ModelContext<Self>) {
self.pending_refresh = Task::ready(Ok(()));
self.pending_refresh = None;
self.completion_id = None;
}

Expand Down
10 changes: 9 additions & 1 deletion crates/ui/src/components/button/icon_button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub struct IconButton {
icon_size: IconSize,
icon_color: Color,
selected_icon: Option<IconName>,
alpha: Option<f32>,
}

impl IconButton {
Expand All @@ -33,6 +34,7 @@ impl IconButton {
icon_size: IconSize::default(),
icon_color: Color::Default,
selected_icon: None,
alpha: None,
};
this.base.base = this.base.base.debug_selector(|| format!("ICON-{:?}", icon));
this
Expand All @@ -53,6 +55,11 @@ impl IconButton {
self
}

pub fn alpha(mut self, alpha: f32) -> Self {
self.alpha = Some(alpha);
self
}

pub fn selected_icon(mut self, icon: impl Into<Option<IconName>>) -> Self {
self.selected_icon = icon.into();
self
Expand Down Expand Up @@ -146,6 +153,7 @@ impl RenderOnce for IconButton {
let is_selected = self.base.selected;
let selected_style = self.base.selected_style;

let color = self.icon_color.color(cx).opacity(self.alpha.unwrap_or(1.0));
self.base
.map(|this| match self.shape {
IconButtonShape::Square => {
Expand All @@ -161,7 +169,7 @@ impl RenderOnce for IconButton {
.selected_icon(self.selected_icon)
.when_some(selected_style, |this, style| this.selected_style(style))
.size(self.icon_size)
.color(self.icon_color),
.color(Color::Custom(color)),
)
}
}
22 changes: 22 additions & 0 deletions crates/ui/src/components/popover_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,28 @@ pub trait PopoverTrigger: IntoElement + Clickable + Toggleable + 'static {}

impl<T: IntoElement + Clickable + Toggleable + 'static> PopoverTrigger for T {}

impl<T: Clickable> Clickable for gpui::AnimationElement<T>
where
T: Clickable + 'static,
{
fn on_click(self, handler: impl Fn(&gpui::ClickEvent, &mut WindowContext) + 'static) -> Self {
self.map_element(|e| e.on_click(handler))
}

fn cursor_style(self, cursor_style: gpui::CursorStyle) -> Self {
self.map_element(|e| e.cursor_style(cursor_style))
}
}

impl<T: Toggleable> Toggleable for gpui::AnimationElement<T>
where
T: Toggleable + 'static,
{
fn toggle_state(self, selected: bool) -> Self {
self.map_element(|e| e.toggle_state(selected))
}
}

pub struct PopoverMenuHandle<M>(Rc<RefCell<Option<PopoverMenuHandleState<M>>>>);

impl<M> Clone for PopoverMenuHandle<M> {
Expand Down
4 changes: 4 additions & 0 deletions crates/zeta/src/zeta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,10 @@ impl inline_completion::InlineCompletionProvider for ZetaInlineCompletionProvide
settings.inline_completions_enabled(language.as_ref(), file.map(|f| f.path().as_ref()), cx)
}

fn is_refreshing(&self) -> bool {
!self.pending_completions.is_empty()
}

fn refresh(
&mut self,
buffer: Model<Buffer>,
Expand Down
Loading