Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Merge remote-tracking branch 'origin/main' into cache

Antonio Scandurra1c260e6dfd0e
diff --cc crates/copilot_ui/src/sign_in.rs
index 00000000000,ba6f54b634a..ae2085aaf3e
mode 000000,100644..100644
--- a/crates/copilot_ui/src/sign_in.rs
+++ b/crates/copilot_ui/src/sign_in.rs
@@@ -1,0 -1,183 +1,183 @@@
+ use copilot::{request::PromptUserDeviceFlow, Copilot, Status};
+ use gpui::{
+ div, svg, AppContext, ClipboardItem, DismissEvent, Element, EventEmitter, FocusHandle,
+ FocusableView, InteractiveElement, IntoElement, Model, ParentElement, Render, Styled,
+ Subscription, ViewContext,
+ };
+ use ui::{prelude::*, Button, IconName, Label};
+ use workspace::ModalView;
+
+ const COPILOT_SIGN_UP_URL: &'static str = "https://github.com/features/copilot";
+
+ pub struct CopilotCodeVerification {
+ status: Status,
+ connect_clicked: bool,
+ focus_handle: FocusHandle,
+ _subscription: Subscription,
+ }
+
+ impl FocusableView for CopilotCodeVerification {
+ fn focus_handle(&self, _: &AppContext) -> gpui::FocusHandle {
+ self.focus_handle.clone()
+ }
+ }
+
+ impl EventEmitter<DismissEvent> for CopilotCodeVerification {}
+ impl ModalView for CopilotCodeVerification {}
+
+ impl CopilotCodeVerification {
+ pub(crate) fn new(copilot: &Model<Copilot>, cx: &mut ViewContext<Self>) -> Self {
+ let status = copilot.read(cx).status();
+ Self {
+ status,
+ connect_clicked: false,
+ focus_handle: cx.focus_handle(),
+ _subscription: cx.observe(copilot, |this, copilot, cx| {
+ let status = copilot.read(cx).status();
+ match status {
+ Status::Authorized | Status::Unauthorized | Status::SigningIn { .. } => {
+ this.set_status(status, cx)
+ }
+ _ => cx.emit(DismissEvent),
+ }
+ }),
+ }
+ }
+
+ pub fn set_status(&mut self, status: Status, cx: &mut ViewContext<Self>) {
+ self.status = status;
+ cx.notify();
+ }
+
+ fn render_device_code(
+ data: &PromptUserDeviceFlow,
+ cx: &mut ViewContext<Self>,
+ ) -> impl IntoElement {
+ let copied = cx
+ .read_from_clipboard()
+ .map(|item| item.text() == &data.user_code)
+ .unwrap_or(false);
+ h_stack()
+ .w_full()
+ .p_1()
+ .border()
+ .border_muted(cx)
+ .rounded_md()
+ .cursor_pointer()
+ .justify_between()
+ .on_mouse_down(gpui::MouseButton::Left, {
+ let user_code = data.user_code.clone();
+ move |_, cx| {
+ cx.write_to_clipboard(ClipboardItem::new(user_code.clone()));
- cx.notify();
++ cx.refresh();
+ }
+ })
+ .child(div().flex_1().child(Label::new(data.user_code.clone())))
+ .child(div().flex_none().px_1().child(Label::new(if copied {
+ "Copied!"
+ } else {
+ "Copy"
+ })))
+ }
+
+ fn render_prompting_modal(
+ connect_clicked: bool,
+ data: &PromptUserDeviceFlow,
+ cx: &mut ViewContext<Self>,
+ ) -> impl Element {
+ let connect_button_label = if connect_clicked {
+ "Waiting for connection..."
+ } else {
+ "Connect to Github"
+ };
+ v_stack()
+ .flex_1()
+ .gap_2()
+ .items_center()
+ .child(Headline::new("Use Github Copilot in Zed.").size(HeadlineSize::Large))
+ .child(
+ Label::new("Using Copilot requres an active subscription on Github.")
+ .color(Color::Muted),
+ )
+ .child(Self::render_device_code(data, cx))
+ .child(
+ Label::new("Paste this code into GitHub after clicking the button below.")
+ .size(ui::LabelSize::Small),
+ )
+ .child(
+ Button::new("connect-button", connect_button_label)
+ .on_click({
+ let verification_uri = data.verification_uri.clone();
+ cx.listener(move |this, _, cx| {
+ cx.open_url(&verification_uri);
+ this.connect_clicked = true;
+ })
+ })
+ .full_width()
+ .style(ButtonStyle::Filled),
+ )
+ }
+ fn render_enabled_modal(cx: &mut ViewContext<Self>) -> impl Element {
+ v_stack()
+ .gap_2()
+ .child(Headline::new("Copilot Enabled!").size(HeadlineSize::Large))
+ .child(Label::new(
+ "You can update your settings or sign out from the Copilot menu in the status bar.",
+ ))
+ .child(
+ Button::new("copilot-enabled-done-button", "Done")
+ .full_width()
+ .on_click(cx.listener(|_, _, cx| cx.emit(DismissEvent))),
+ )
+ }
+
+ fn render_unauthorized_modal() -> impl Element {
+ v_stack()
+ .child(Headline::new("You must have an active GitHub Copilot subscription.").size(HeadlineSize::Large))
+
+ .child(Label::new(
+ "You can enable Copilot by connecting your existing license once you have subscribed or renewed your subscription.",
+ ).color(Color::Warning))
+ .child(
+ Button::new("copilot-subscribe-button", "Subscibe on Github")
+ .full_width()
+ .on_click(|_, cx| cx.open_url(COPILOT_SIGN_UP_URL)),
+ )
+ }
+ }
+
+ impl Render for CopilotCodeVerification {
+ fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
+ let prompt = match &self.status {
+ Status::SigningIn {
+ prompt: Some(prompt),
+ } => Self::render_prompting_modal(self.connect_clicked, &prompt, cx).into_any_element(),
+ Status::Unauthorized => {
+ self.connect_clicked = false;
+ Self::render_unauthorized_modal().into_any_element()
+ }
+ Status::Authorized => {
+ self.connect_clicked = false;
+ Self::render_enabled_modal(cx).into_any_element()
+ }
+ _ => div().into_any_element(),
+ };
+
+ v_stack()
+ .id("copilot code verification")
+ .elevation_3(cx)
+ .w_96()
+ .items_center()
+ .p_4()
+ .gap_2()
+ .child(
+ svg()
+ .w_32()
+ .h_16()
+ .flex_none()
+ .path(IconName::ZedXCopilot.path())
+ .text_color(cx.theme().colors().icon),
+ )
+ .child(prompt)
+ }
+ }
diff --cc crates/gpui/src/app/entity_map.rs
index 482a003fe76,0b213b20f76..1e593caf98a
--- a/crates/gpui/src/app/entity_map.rs
+++ b/crates/gpui/src/app/entity_map.rs
@@@ -19,14 -19,11 +19,17 @@@ use std::
#[cfg(any(test, feature = "test-support"))]
use collections::HashMap;
- slotmap::new_key_type! { pub struct EntityId; }
+ slotmap::new_key_type! {
+ /// A unique identifier for a model or view across the application.
+ pub struct EntityId;
+ }
+impl From<u64> for EntityId {
+ fn from(value: u64) -> Self {
+ Self(KeyData::from_ffi(value))
+ }
+}
+
impl EntityId {
pub fn as_u64(self) -> u64 {
self.0.as_ffi()
diff --cc crates/gpui/src/window.rs
index fc60f9c5c31,187f28c14b4..be739149517
--- a/crates/gpui/src/window.rs
+++ b/crates/gpui/src/window.rs
@@@ -1,8 -1,10 +1,10 @@@
+ #![deny(missing_docs)]
+
use crate::{
- px, size, transparent_black, Action, AnyDrag, AnyView, AppContext, Arena, ArenaBox, ArenaRef,
- AsyncWindowContext, AvailableSpace, Bounds, BoxShadow, Context, Corners, CursorStyle,
- DevicePixels, DispatchActionListener, DispatchNodeId, DispatchTree, DisplayId, Edges, Effect,
- Entity, EntityId, EventEmitter, FileDropEvent, Flatten, FontId, GlobalElementId, GlyphId, Hsla,
+ px, size, transparent_black, Action, AnyDrag, AnyView, AppContext, Arena, AsyncWindowContext,
+ AvailableSpace, Bounds, BoxShadow, Context, Corners, CursorStyle, DevicePixels,
+ DispatchActionListener, DispatchNodeId, DispatchTree, DisplayId, Edges, Effect, Entity,
+ EntityId, EventEmitter, FileDropEvent, Flatten, FontId, GlobalElementId, GlyphId, Hsla,
ImageData, InputEvent, IsZero, KeyBinding, KeyContext, KeyDownEvent, KeystrokeEvent, LayoutId,
Model, ModelContext, Modifiers, MonochromeSprite, MouseButton, MouseMoveEvent, MouseUpEvent,
Path, Pixels, PlatformAtlas, PlatformDisplay, PlatformInputHandler, PlatformWindow, Point,
@@@ -256,10 -266,10 +266,10 @@@ pub struct Window
pub(crate) element_id_stack: GlobalElementId,
pub(crate) rendered_frame: Frame,
pub(crate) next_frame: Frame,
- frame_arena: Arena,
+ pub(crate) dirty_views: FxHashSet<EntityId>,
pub(crate) focus_handles: Arc<RwLock<SlotMap<FocusId, AtomicUsize>>>,
focus_listeners: SubscriberSet<(), AnyWindowFocusListener>,
- blur_listeners: SubscriberSet<(), AnyObserver>,
+ focus_lost_listeners: SubscriberSet<(), AnyObserver>,
default_prevented: bool,
mouse_position: Point<Pixels>,
modifiers: Modifiers,
@@@ -288,11 -296,12 +298,12 @@@ pub(crate) struct ElementStateBox
pub(crate) struct Frame {
focus: Option<FocusId>,
+ window_active: bool,
pub(crate) element_states: FxHashMap<GlobalElementId, ElementStateBox>,
- mouse_listeners: FxHashMap<TypeId, Vec<(StackingOrder, AnyMouseListener)>>,
+ mouse_listeners: FxHashMap<TypeId, Vec<(StackingOrder, EntityId, AnyMouseListener)>>,
pub(crate) dispatch_tree: DispatchTree,
- pub(crate) scene_builder: SceneBuilder,
- pub(crate) depth_map: Vec<(StackingOrder, Bounds<Pixels>)>,
+ pub(crate) scene: Scene,
+ pub(crate) depth_map: Vec<(StackingOrder, EntityId, Bounds<Pixels>)>,
pub(crate) z_index_stack: StackingOrder,
pub(crate) next_stacking_order_id: u32,
content_mask_stack: Vec<ContentMask<Pixels>>,
@@@ -412,10 -416,10 +424,10 @@@ impl Window
element_id_stack: GlobalElementId::default(),
rendered_frame: Frame::new(DispatchTree::new(cx.keymap.clone(), cx.actions.clone())),
next_frame: Frame::new(DispatchTree::new(cx.keymap.clone(), cx.actions.clone())),
- frame_arena: Arena::new(1024 * 1024),
+ dirty_views: FxHashSet::default(),
focus_handles: Arc::new(RwLock::new(SlotMap::with_key())),
focus_listeners: SubscriberSet::new(),
- blur_listeners: SubscriberSet::new(),
+ focus_lost_listeners: SubscriberSet::new(),
default_prevented: true,
mouse_position,
modifiers,
@@@ -532,9 -535,10 +545,10 @@@ impl<'a> WindowContext<'a>
}
self.window.focus = None;
- self.notify();
+ self.refresh();
}
+ /// Blur the window and don't allow anything in it to be focused again.
pub fn disable_focus(&mut self) {
self.blur();
self.window.focus_enabled = false;
@@@ -764,14 -772,9 +782,17 @@@
.request_measured_layout(style, rem_size, measure)
}
- pub fn layout_style(&self, layout_id: LayoutId) -> Option<&Style> {
++ pub(crate) fn layout_style(&self, layout_id: LayoutId) -> Option<&Style> {
+ self.window
+ .layout_engine
+ .as_ref()
+ .unwrap()
+ .requested_style(layout_id)
+ }
+
+ /// Compute the layout for the given id within the given available space.
+ /// This method is called for its side effect, typically by the framework prior to painting.
+ /// After calling it, you can request the bounds of the given layout node id or any descendant.
pub fn compute_layout(&mut self, layout_id: LayoutId, available_space: Size<AvailableSpace>) {
let mut layout_engine = self.window.layout_engine.take().unwrap();
layout_engine.compute_layout(layout_id, available_space, self);
@@@ -938,9 -957,15 +967,10 @@@
self.window
.next_frame
.dispatch_tree
- .on_action(action_type, ArenaRef::from(listener));
+ .on_action(action_type, Rc::new(listener));
}
+ /// Determine whether the given action is available along the dispatch path to the currently focused element.
pub fn is_action_available(&self, action: &dyn Action) -> bool {
let target = self
.focused()
@@@ -1416,82 -1408,17 +1447,67 @@@
self.window.focus,
);
self.window.next_frame.focus = self.window.focus;
+ self.window.next_frame.window_active = self.window.active;
self.window.root_view = Some(root_view);
+ // Reuse mouse listeners that didn't change since the last frame.
+ for (type_id, listeners) in &mut self.window.rendered_frame.mouse_listeners {
+ let next_listeners = self
+ .window
+ .next_frame
+ .mouse_listeners
+ .entry(*type_id)
+ .or_default();
+ for (order, view_id, listener) in listeners.drain(..) {
+ if self.window.next_frame.reused_views.contains(&view_id) {
+ next_listeners.push((order, view_id, listener));
+ }
+ }
+ }
+
+ // Reuse entries in the depth map that didn't change since the last frame.
+ for (order, view_id, bounds) in self.window.rendered_frame.depth_map.drain(..) {
+ if self.window.next_frame.reused_views.contains(&view_id) {
+ self.window
+ .next_frame
+ .depth_map
+ .push((order, view_id, bounds));
+ }
+ }
+ self.window
+ .next_frame
+ .depth_map
+ .sort_by(|a, b| a.0.cmp(&b.0));
+
+ // Retain element states for views that didn't change since the last frame.
+ for (element_id, state) in self.window.rendered_frame.element_states.drain() {
+ if self
+ .window
+ .next_frame
+ .reused_views
+ .contains(&state.parent_view_id)
+ {
+ self.window
+ .next_frame
+ .element_states
+ .entry(element_id)
+ .or_insert(state);
+ }
+ }
+
+ // Reuse geometry that didn't change since the last frame.
+ self.window.next_frame.scene.insert_views_from_scene(
+ &self.window.next_frame.reused_views,
+ &mut self.window.rendered_frame.scene,
+ );
+ self.window.next_frame.scene.finish();
+
let previous_focus_path = self.window.rendered_frame.focus_path();
+ let previous_window_active = self.window.rendered_frame.window_active;
mem::swap(&mut self.window.rendered_frame, &mut self.window.next_frame);
let current_focus_path = self.window.rendered_frame.focus_path();
-
- if previous_focus_path != current_focus_path {
- if !previous_focus_path.is_empty() && current_focus_path.is_empty() {
- self.window
- .blur_listeners
- .clone()
- .retain(&(), |listener| listener(self));
- }
-
- let event = FocusEvent {
- previous_focus_path,
- current_focus_path,
- };
- self.window
- .focus_listeners
- .clone()
- .retain(&(), |listener| listener(&event, self));
- }
+ let current_window_active = self.window.rendered_frame.window_active;
- let scene = self.window.rendered_frame.scene_builder.build();
-
// Set the cursor only if we're the active window.
let cursor_style = self
.window
@@@ -1506,9 -1432,35 +1522,37 @@@
self.window.drawing = false;
ELEMENT_ARENA.with_borrow_mut(|element_arena| element_arena.clear());
+ if previous_focus_path != current_focus_path
+ || previous_window_active != current_window_active
+ {
+ if !previous_focus_path.is_empty() && current_focus_path.is_empty() {
+ self.window
+ .focus_lost_listeners
+ .clone()
+ .retain(&(), |listener| listener(self));
+ }
+
+ let event = FocusEvent {
+ previous_focus_path: if previous_window_active {
+ previous_focus_path
+ } else {
+ Default::default()
+ },
+ current_focus_path: if current_window_active {
+ current_focus_path
+ } else {
+ Default::default()
+ },
+ };
+ self.window
+ .focus_listeners
+ .clone()
+ .retain(&(), |listener| listener(&event, self));
+ }
+
- scene
+ self.window
+ .platform_window
+ .draw(&self.window.rendered_frame.scene);
}
/// Dispatch a mouse or keyboard event on the window.
@@@ -2574,20 -2541,11 +2649,22 @@@ impl<'a, V: 'static> ViewContext<'a, V
subscription
}
+ /// Indicate that this view has changed, which will invoke any observers and also mark the window as dirty.
+ /// If this view or any of its ancestors are *cached*, notifying it will cause it or its ancestors to be redrawn.
pub fn notify(&mut self) {
+ for view_id in self
+ .window
+ .rendered_frame
+ .dispatch_tree
+ .view_path(self.view.entity_id())
+ {
+ if !self.window.dirty_views.insert(view_id) {
+ break;
+ }
+ }
+
if !self.window.drawing {
- self.window_cx.notify();
+ self.window_cx.window.dirty = true;
self.window_cx.app.push_effect(Effect::Notify {
emitter: self.view.model.entity_id,
});
diff --cc crates/workspace/src/pane_group.rs
index 9ce191bc946,3dcdeec37f4..ce58e51678c
--- a/crates/workspace/src/pane_group.rs
+++ b/crates/workspace/src/pane_group.rs
@@@ -696,9 -698,10 +698,10 @@@ mod element
proposed_current_pixel_change -= current_pixel_change;
}
- // todo!(schedule serialize)
- // workspace.schedule_serialize(cx);
+ workspace
+ .update(cx, |this, cx| this.schedule_serialize(cx))
+ .log_err();
- cx.notify();
+ cx.refresh();
}
fn push_handle(
@@@ -742,9 -746,19 +746,19 @@@
cx.on_mouse_event({
let dragged_handle = dragged_handle.clone();
- move |e: &MouseDownEvent, phase, _cx| {
+ let flexes = flexes.clone();
+ let workspace = workspace.clone();
+ move |e: &MouseDownEvent, phase, cx| {
if phase.bubble() && handle_bounds.contains(&e.position) {
dragged_handle.replace(Some(ix));
+ if e.click_count >= 2 {
+ let mut borrow = flexes.lock();
+ *borrow = vec![1.; borrow.len()];
+ workspace
+ .update(cx, |this, cx| this.schedule_serialize(cx))
+ .log_err();
- cx.notify();
++ cx.refresh();
+ }
}
}
});