Rename again, add fun cx APIs using new traits

This commit is contained in:
Mikayla 2023-11-17 10:06:41 -08:00
parent 01d9d53f4a
commit 17d53d0e38
No known key found for this signature in database
10 changed files with 77 additions and 35 deletions

View File

@ -2,7 +2,7 @@ use collections::{CommandPaletteFilter, HashMap};
use fuzzy::{StringMatch, StringMatchCandidate};
use gpui::{
actions, div, prelude::*, Action, AppContext, Component, Div, EventEmitter, FocusHandle,
FocusableView, Keystroke, ManagedView, ParentComponent, Render, Styled, View, ViewContext,
FocusableView, Keystroke, ManagedEvent, ParentComponent, Render, Styled, View, ViewContext,
VisualContext, WeakView,
};
use picker::{Picker, PickerDelegate};
@ -69,7 +69,7 @@ impl CommandPalette {
}
}
impl EventEmitter<ManagedView> for CommandPalette {}
impl EventEmitter<ManagedEvent> for CommandPalette {}
impl FocusableView for CommandPalette {
fn focus_handle(&self, cx: &AppContext) -> FocusHandle {
@ -268,7 +268,7 @@ impl PickerDelegate for CommandPaletteDelegate {
fn dismissed(&mut self, cx: &mut ViewContext<Picker<Self>>) {
self.command_palette
.update(cx, |_, cx| cx.emit(ManagedView::Dismiss))
.update(cx, |_, cx| cx.emit(ManagedEvent::Dismiss))
.log_err();
}

View File

@ -3,7 +3,7 @@ use editor::{scroll::autoscroll::Autoscroll, Bias, Editor};
use fuzzy::{CharBag, PathMatch, PathMatchCandidate};
use gpui::{
actions, div, AppContext, Component, Div, EventEmitter, FocusHandle, FocusableView,
InteractiveComponent, ManagedView, Model, ParentComponent, Render, Styled, Task, View,
InteractiveComponent, ManagedEvent, Model, ParentComponent, Render, Styled, Task, View,
ViewContext, VisualContext, WeakView,
};
use picker::{Picker, PickerDelegate};
@ -111,7 +111,7 @@ impl FileFinder {
}
}
impl EventEmitter<ManagedView> for FileFinder {}
impl EventEmitter<ManagedEvent> for FileFinder {}
impl FocusableView for FileFinder {
fn focus_handle(&self, cx: &AppContext) -> FocusHandle {
self.picker.focus_handle(cx)
@ -690,7 +690,7 @@ impl PickerDelegate for FileFinderDelegate {
}
}
finder
.update(&mut cx, |_, cx| cx.emit(ManagedView::Dismiss))
.update(&mut cx, |_, cx| cx.emit(ManagedEvent::Dismiss))
.ok()?;
Some(())
@ -702,7 +702,7 @@ impl PickerDelegate for FileFinderDelegate {
fn dismissed(&mut self, cx: &mut ViewContext<Picker<FileFinderDelegate>>) {
self.file_finder
.update(cx, |_, cx| cx.emit(ManagedView::Dismiss))
.update(cx, |_, cx| cx.emit(ManagedEvent::Dismiss))
.log_err();
}

View File

@ -1,7 +1,7 @@
use editor::{display_map::ToDisplayPoint, scroll::autoscroll::Autoscroll, Editor};
use gpui::{
actions, div, prelude::*, AppContext, Div, EventEmitter, FocusHandle, FocusableView,
ManagedView, ParentComponent, Render, SharedString, Styled, Subscription, View, ViewContext,
ManagedEvent, ParentComponent, Render, SharedString, Styled, Subscription, View, ViewContext,
VisualContext, WindowContext,
};
use text::{Bias, Point};
@ -29,7 +29,7 @@ impl FocusableView for GoToLine {
self.active_editor.focus_handle(cx)
}
}
impl EventEmitter<ManagedView> for GoToLine {}
impl EventEmitter<ManagedEvent> for GoToLine {}
impl GoToLine {
fn register(workspace: &mut Workspace, _: &mut ViewContext<Workspace>) {
@ -89,7 +89,7 @@ impl GoToLine {
) {
match event {
// todo!() this isn't working...
editor::Event::Blurred => cx.emit(ManagedView::Dismiss),
editor::Event::Blurred => cx.emit(ManagedEvent::Dismiss),
editor::Event::BufferEdited { .. } => self.highlight_current_line(cx),
_ => {}
}
@ -124,7 +124,7 @@ impl GoToLine {
}
fn cancel(&mut self, _: &menu::Cancel, cx: &mut ViewContext<Self>) {
cx.emit(ManagedView::Dismiss);
cx.emit(ManagedEvent::Dismiss);
}
fn confirm(&mut self, _: &menu::Confirm, cx: &mut ViewContext<Self>) {
@ -141,7 +141,7 @@ impl GoToLine {
self.prev_scroll_position.take();
}
cx.emit(ManagedView::Dismiss);
cx.emit(ManagedEvent::Dismiss);
}
}

View File

@ -1,6 +1,6 @@
use crate::{
AnyView, AnyWindowHandle, AppCell, AppContext, BackgroundExecutor, Context, FocusableView,
ForegroundExecutor, Model, ModelContext, Render, Result, Task, View, ViewContext,
ForegroundExecutor, ManagedEvent, Model, ModelContext, Render, Result, Task, View, ViewContext,
VisualContext, WindowContext, WindowHandle,
};
use anyhow::{anyhow, Context as _};
@ -320,4 +320,13 @@ impl VisualContext for AsyncWindowContext {
view.read(cx).focus_handle(cx).clone().focus(cx);
})
}
fn dismiss_view<V>(&mut self, view: &View<V>) -> Self::Result<()>
where
V: crate::ManagedView,
{
self.window.update(self, |_, cx| {
view.update(cx, |_, cx| cx.emit(ManagedEvent::Dismiss))
})
}
}

View File

@ -579,6 +579,17 @@ impl<'a> VisualContext for VisualTestContext<'a> {
})
.unwrap()
}
fn dismiss_view<V>(&mut self, view: &View<V>) -> Self::Result<()>
where
V: crate::ManagedView,
{
self.window
.update(self.cx, |_, cx| {
view.update(cx, |_, cx| cx.emit(crate::ManagedEvent::Dismiss))
})
.unwrap()
}
}
impl AnyWindowHandle {

View File

@ -141,6 +141,10 @@ pub trait VisualContext: Context {
fn focus_view<V>(&mut self, view: &View<V>) -> Self::Result<()>
where
V: FocusableView;
fn dismiss_view<V>(&mut self, view: &View<V>) -> Self::Result<()>
where
V: ManagedView;
}
pub trait Entity<T>: Sealed {

View File

@ -193,11 +193,11 @@ pub trait FocusableView: Render {
/// ManagedView is a view (like a Modal, Popover, Menu, etc.)
/// where the lifecycle of the view is handled by another view.
pub trait Managed: FocusableView + EventEmitter<ManagedView> {}
pub trait ManagedView: FocusableView + EventEmitter<ManagedEvent> {}
impl<M: FocusableView + EventEmitter<ManagedView>> Managed for M {}
impl<M: FocusableView + EventEmitter<ManagedEvent>> ManagedView for M {}
pub enum ManagedView {
pub enum ManagedEvent {
Dismiss,
}
@ -1577,6 +1577,13 @@ impl VisualContext for WindowContext<'_> {
view.focus_handle(cx).clone().focus(cx);
})
}
fn dismiss_view<V>(&mut self, view: &View<V>) -> Self::Result<()>
where
V: ManagedView,
{
self.update_view(view, |_, cx| cx.emit(ManagedEvent::Dismiss))
}
}
impl<'a> std::ops::Deref for WindowContext<'a> {
@ -2270,6 +2277,13 @@ impl<'a, V: 'static> ViewContext<'a, V> {
{
self.defer(|view, cx| view.focus_handle(cx).focus(cx))
}
pub fn dismiss_self(&mut self)
where
V: ManagedView,
{
self.defer(|_, cx| cx.emit(ManagedEvent::Dismiss))
}
}
impl<V> Context for ViewContext<'_, V> {
@ -2349,6 +2363,10 @@ impl<V: 'static> VisualContext for ViewContext<'_, V> {
fn focus_view<W: FocusableView>(&mut self, view: &View<W>) -> Self::Result<()> {
self.window_cx.focus_view(view)
}
fn dismiss_view<W: ManagedView>(&mut self, view: &View<W>) -> Self::Result<()> {
self.window_cx.dismiss_view(view)
}
}
impl<'a, V> std::ops::Deref for ViewContext<'a, V> {

View File

@ -5,7 +5,7 @@ use crate::prelude::*;
use crate::{v_stack, Label, List, ListEntry, ListItem, ListSeparator, ListSubHeader};
use gpui::{
overlay, px, Action, AnchorCorner, AnyElement, AppContext, Bounds, DispatchPhase, Div,
EventEmitter, FocusHandle, FocusableView, LayoutId, Managed, ManagedView, MouseButton,
EventEmitter, FocusHandle, FocusableView, LayoutId, ManagedEvent, ManagedView, MouseButton,
MouseDownEvent, Pixels, Point, Render, View,
};
@ -20,7 +20,7 @@ impl FocusableView for ContextMenu {
}
}
impl EventEmitter<ManagedView> for ContextMenu {}
impl EventEmitter<ManagedEvent> for ContextMenu {}
impl ContextMenu {
pub fn new(cx: &mut WindowContext) -> Self {
@ -47,11 +47,11 @@ impl ContextMenu {
pub fn confirm(&mut self, _: &menu::Confirm, cx: &mut ViewContext<Self>) {
// todo!()
cx.emit(ManagedView::Dismiss);
cx.emit(ManagedEvent::Dismiss);
}
pub fn cancel(&mut self, _: &menu::Cancel, cx: &mut ViewContext<Self>) {
cx.emit(ManagedView::Dismiss);
cx.emit(ManagedEvent::Dismiss);
}
}
@ -79,7 +79,7 @@ impl Render for ContextMenu {
}
}
pub struct MenuHandle<V: 'static, M: Managed> {
pub struct MenuHandle<V: 'static, M: ManagedView> {
id: Option<ElementId>,
child_builder: Option<Box<dyn FnOnce(bool) -> AnyElement<V> + 'static>>,
menu_builder: Option<Rc<dyn Fn(&mut V, &mut ViewContext<V>) -> View<M> + 'static>>,
@ -88,7 +88,7 @@ pub struct MenuHandle<V: 'static, M: Managed> {
attach: Option<AnchorCorner>,
}
impl<V: 'static, M: Managed> MenuHandle<V, M> {
impl<V: 'static, M: ManagedView> MenuHandle<V, M> {
pub fn id(mut self, id: impl Into<ElementId>) -> Self {
self.id = Some(id.into());
self
@ -118,7 +118,7 @@ impl<V: 'static, M: Managed> MenuHandle<V, M> {
}
}
pub fn menu_handle<V: 'static, M: Managed>() -> MenuHandle<V, M> {
pub fn menu_handle<V: 'static, M: ManagedView>() -> MenuHandle<V, M> {
MenuHandle {
id: None,
child_builder: None,
@ -135,7 +135,7 @@ pub struct MenuHandleState<V, M> {
child_element: Option<AnyElement<V>>,
menu_element: Option<AnyElement<V>>,
}
impl<V: 'static, M: Managed> Element<V> for MenuHandle<V, M> {
impl<V: 'static, M: ManagedView> Element<V> for MenuHandle<V, M> {
type ElementState = MenuHandleState<V, M>;
fn element_id(&self) -> Option<gpui::ElementId> {
@ -229,7 +229,7 @@ impl<V: 'static, M: Managed> Element<V> for MenuHandle<V, M> {
let new_menu = (builder)(view_state, cx);
let menu2 = menu.clone();
cx.subscribe(&new_menu, move |this, modal, e, cx| match e {
&ManagedView::Dismiss => {
&ManagedEvent::Dismiss => {
*menu2.borrow_mut() = None;
cx.notify();
}
@ -250,7 +250,7 @@ impl<V: 'static, M: Managed> Element<V> for MenuHandle<V, M> {
}
}
impl<V: 'static, M: Managed> Component<V> for MenuHandle<V, M> {
impl<V: 'static, M: ManagedView> Component<V> for MenuHandle<V, M> {
fn render(self) -> AnyElement<V> {
AnyElement::new(self)
}

View File

@ -1,5 +1,5 @@
use gpui::{
div, prelude::*, px, AnyView, Div, FocusHandle, Managed, Render, Subscription, View,
div, prelude::*, px, AnyView, Div, FocusHandle, ManagedView, Render, Subscription, View,
ViewContext,
};
use ui::{h_stack, v_stack};
@ -22,7 +22,7 @@ impl ModalLayer {
pub fn toggle_modal<V, B>(&mut self, cx: &mut ViewContext<Self>, build_view: B)
where
V: Managed,
V: ManagedView,
B: FnOnce(&mut ViewContext<V>) -> V,
{
if let Some(active_modal) = &self.active_modal {
@ -38,7 +38,7 @@ impl ModalLayer {
pub fn show_modal<V>(&mut self, new_modal: View<V>, cx: &mut ViewContext<Self>)
where
V: Managed,
V: ManagedView,
{
self.active_modal = Some(ActiveModal {
modal: new_modal.clone().into(),

View File

@ -31,10 +31,10 @@ use futures::{
use gpui::{
actions, div, point, size, Action, AnyModel, AnyView, AnyWeakView, AppContext, AsyncAppContext,
AsyncWindowContext, Bounds, Context, Div, Entity, EntityId, EventEmitter, FocusHandle,
FocusableView, GlobalPixels, InteractiveComponent, KeyContext, Managed, Model, ModelContext,
ParentComponent, PathPromptOptions, Point, PromptLevel, Render, Size, Styled, Subscription,
Task, View, ViewContext, VisualContext, WeakView, WindowBounds, WindowContext, WindowHandle,
WindowOptions,
FocusableView, GlobalPixels, InteractiveComponent, KeyContext, ManagedView, Model,
ModelContext, ParentComponent, PathPromptOptions, Point, PromptLevel, Render, Size, Styled,
Subscription, Task, View, ViewContext, VisualContext, WeakView, WindowBounds, WindowContext,
WindowHandle, WindowOptions,
};
use item::{FollowableItem, FollowableItemHandle, Item, ItemHandle, ItemSettings, ProjectItem};
use itertools::Itertools;
@ -3364,14 +3364,14 @@ impl Workspace {
div
}
pub fn active_modal<V: Managed + 'static>(
pub fn active_modal<V: ManagedView + 'static>(
&mut self,
cx: &ViewContext<Self>,
) -> Option<View<V>> {
self.modal_layer.read(cx).active_modal()
}
pub fn toggle_modal<V: Managed, B>(&mut self, cx: &mut ViewContext<Self>, build: B)
pub fn toggle_modal<V: ManagedView, B>(&mut self, cx: &mut ViewContext<Self>, build: B)
where
B: FnOnce(&mut ViewContext<V>) -> V,
{