Add back some window events for workspace

Co-Authored-By: Antonio <me@as-cii.com>
This commit is contained in:
Conrad Irwin 2023-11-02 10:03:03 -06:00
parent 0bab1a92df
commit 634aba89d2
4 changed files with 231 additions and 219 deletions

View file

@ -4,11 +4,11 @@ use crate::{
Entity, EntityId, EventEmitter, FileDropEvent, FocusEvent, FontId, GlobalElementId, GlyphId, Entity, EntityId, EventEmitter, FileDropEvent, FocusEvent, FontId, GlobalElementId, GlyphId,
Hsla, ImageData, InputEvent, IsZero, KeyListener, KeyMatch, KeyMatcher, Keystroke, LayoutId, Hsla, ImageData, InputEvent, IsZero, KeyListener, KeyMatch, KeyMatcher, Keystroke, LayoutId,
Model, ModelContext, Modifiers, MonochromeSprite, MouseButton, MouseDownEvent, MouseMoveEvent, Model, ModelContext, Modifiers, MonochromeSprite, MouseButton, MouseDownEvent, MouseMoveEvent,
MouseUpEvent, Path, Pixels, PlatformAtlas, PlatformWindow, Point, PolychromeSprite, MouseUpEvent, Path, Pixels, PlatformAtlas, PlatformDisplay, PlatformWindow, Point,
PromptLevel, Quad, Render, RenderGlyphParams, RenderImageParams, RenderSvgParams, ScaledPixels, PolychromeSprite, PromptLevel, Quad, Render, RenderGlyphParams, RenderImageParams,
SceneBuilder, Shadow, SharedString, Size, Style, SubscriberSet, Subscription, RenderSvgParams, ScaledPixels, SceneBuilder, Shadow, SharedString, Size, Style, SubscriberSet,
TaffyLayoutEngine, Task, Underline, UnderlineStyle, View, VisualContext, WeakView, Subscription, TaffyLayoutEngine, Task, Underline, UnderlineStyle, View, VisualContext,
WindowBounds, WindowOptions, SUBPIXEL_VARIANTS, WeakView, WindowBounds, WindowOptions, SUBPIXEL_VARIANTS,
}; };
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use collections::HashMap; use collections::HashMap;
@ -25,6 +25,7 @@ use std::{
hash::{Hash, Hasher}, hash::{Hash, Hasher},
marker::PhantomData, marker::PhantomData,
mem, mem,
rc::Rc,
sync::{ sync::{
atomic::{AtomicUsize, Ordering::SeqCst}, atomic::{AtomicUsize, Ordering::SeqCst},
Arc, Arc,
@ -570,6 +571,17 @@ impl<'a> WindowContext<'a> {
self.window.bounds self.window.bounds
} }
pub fn is_window_active(&self) -> bool {
self.window.active
}
pub fn display(&self) -> Option<Rc<dyn PlatformDisplay>> {
self.platform
.displays()
.into_iter()
.find(|display| display.id() == self.window.display_id)
}
/// The scale factor of the display associated with the window. For example, it could /// The scale factor of the display associated with the window. For example, it could
/// return 2.0 for a "retina" display, indicating that each logical pixel should actually /// return 2.0 for a "retina" display, indicating that each logical pixel should actually
/// be rendered as two pixels on screen. /// be rendered as two pixels on screen.

View file

@ -104,32 +104,32 @@ pub enum SaveIntent {
const MAX_NAVIGATION_HISTORY_LEN: usize = 1024; const MAX_NAVIGATION_HISTORY_LEN: usize = 1024;
// todo!() pub fn init(cx: &mut AppContext) {
// pub fn init(cx: &mut AppContext) { // todo!()
// cx.add_action(Pane::toggle_zoom); // cx.add_action(Pane::toggle_zoom);
// cx.add_action(|pane: &mut Pane, action: &ActivateItem, cx| { // cx.add_action(|pane: &mut Pane, action: &ActivateItem, cx| {
// pane.activate_item(action.0, true, true, cx); // pane.activate_item(action.0, true, true, cx);
// }); // });
// cx.add_action(|pane: &mut Pane, _: &ActivateLastItem, cx| { // cx.add_action(|pane: &mut Pane, _: &ActivateLastItem, cx| {
// pane.activate_item(pane.items.len() - 1, true, true, cx); // pane.activate_item(pane.items.len() - 1, true, true, cx);
// }); // });
// cx.add_action(|pane: &mut Pane, _: &ActivatePrevItem, cx| { // cx.add_action(|pane: &mut Pane, _: &ActivatePrevItem, cx| {
// pane.activate_prev_item(true, cx); // pane.activate_prev_item(true, cx);
// }); // });
// cx.add_action(|pane: &mut Pane, _: &ActivateNextItem, cx| { // cx.add_action(|pane: &mut Pane, _: &ActivateNextItem, cx| {
// pane.activate_next_item(true, cx); // pane.activate_next_item(true, cx);
// }); // });
// cx.add_async_action(Pane::close_active_item); // cx.add_async_action(Pane::close_active_item);
// cx.add_async_action(Pane::close_inactive_items); // cx.add_async_action(Pane::close_inactive_items);
// cx.add_async_action(Pane::close_clean_items); // cx.add_async_action(Pane::close_clean_items);
// cx.add_async_action(Pane::close_items_to_the_left); // cx.add_async_action(Pane::close_items_to_the_left);
// cx.add_async_action(Pane::close_items_to_the_right); // cx.add_async_action(Pane::close_items_to_the_right);
// cx.add_async_action(Pane::close_all_items); // cx.add_async_action(Pane::close_all_items);
// cx.add_action(|pane: &mut Pane, _: &SplitLeft, cx| pane.split(SplitDirection::Left, cx)); // cx.add_action(|pane: &mut Pane, _: &SplitLeft, cx| pane.split(SplitDirection::Left, cx));
// cx.add_action(|pane: &mut Pane, _: &SplitUp, cx| pane.split(SplitDirection::Up, cx)); // cx.add_action(|pane: &mut Pane, _: &SplitUp, cx| pane.split(SplitDirection::Up, cx));
// cx.add_action(|pane: &mut Pane, _: &SplitRight, cx| pane.split(SplitDirection::Right, cx)); // cx.add_action(|pane: &mut Pane, _: &SplitRight, cx| pane.split(SplitDirection::Right, cx));
// cx.add_action(|pane: &mut Pane, _: &SplitDown, cx| pane.split(SplitDirection::Down, cx)); // cx.add_action(|pane: &mut Pane, _: &SplitDown, cx| pane.split(SplitDirection::Down, cx));
// } }
pub enum Event { pub enum Event {
AddItem { item: Box<dyn ItemHandle> }, AddItem { item: Box<dyn ItemHandle> },

View file

@ -60,7 +60,7 @@ use std::{
pub use toolbar::{ToolbarItemLocation, ToolbarItemView}; pub use toolbar::{ToolbarItemLocation, ToolbarItemView};
use util::ResultExt; use util::ResultExt;
use uuid::Uuid; use uuid::Uuid;
use workspace_settings::WorkspaceSettings; use workspace_settings::{AutosaveSetting, WorkspaceSettings};
lazy_static! { lazy_static! {
static ref ZED_WINDOW_SIZE: Option<Size<GlobalPixels>> = env::var("ZED_WINDOW_SIZE") static ref ZED_WINDOW_SIZE: Option<Size<GlobalPixels>> = env::var("ZED_WINDOW_SIZE")
@ -233,129 +233,129 @@ pub fn init_settings(cx: &mut AppContext) {
ItemSettings::register(cx); ItemSettings::register(cx);
} }
// pub fn init(app_state: Arc<AppState>, cx: &mut AppContext) { pub fn init(app_state: Arc<AppState>, cx: &mut AppContext) {
// init_settings(cx); init_settings(cx);
// pane::init(cx); pane::init(cx);
// notifications::init(cx); notifications::init(cx);
// cx.add_global_action({ // cx.add_global_action({
// let app_state = Arc::downgrade(&app_state); // let app_state = Arc::downgrade(&app_state);
// move |_: &Open, cx: &mut AppContext| { // move |_: &Open, cx: &mut AppContext| {
// let mut paths = cx.prompt_for_paths(PathPromptOptions { // let mut paths = cx.prompt_for_paths(PathPromptOptions {
// files: true, // files: true,
// directories: true, // directories: true,
// multiple: true, // multiple: true,
// }); // });
// if let Some(app_state) = app_state.upgrade() { // if let Some(app_state) = app_state.upgrade() {
// cx.spawn(move |mut cx| async move { // cx.spawn(move |mut cx| async move {
// if let Some(paths) = paths.recv().await.flatten() { // if let Some(paths) = paths.recv().await.flatten() {
// cx.update(|cx| { // cx.update(|cx| {
// open_paths(&paths, &app_state, None, cx).detach_and_log_err(cx) // open_paths(&paths, &app_state, None, cx).detach_and_log_err(cx)
// }); // });
// } // }
// }) // })
// .detach(); // .detach();
// } // }
// } // }
// }); // });
// cx.add_async_action(Workspace::open); // cx.add_async_action(Workspace::open);
// cx.add_async_action(Workspace::follow_next_collaborator); // cx.add_async_action(Workspace::follow_next_collaborator);
// cx.add_async_action(Workspace::close); // cx.add_async_action(Workspace::close);
// cx.add_async_action(Workspace::close_inactive_items_and_panes); // cx.add_async_action(Workspace::close_inactive_items_and_panes);
// cx.add_async_action(Workspace::close_all_items_and_panes); // cx.add_async_action(Workspace::close_all_items_and_panes);
// cx.add_global_action(Workspace::close_global); // cx.add_global_action(Workspace::close_global);
// cx.add_global_action(restart); // cx.add_global_action(restart);
// cx.add_async_action(Workspace::save_all); // cx.add_async_action(Workspace::save_all);
// cx.add_action(Workspace::add_folder_to_project); // cx.add_action(Workspace::add_folder_to_project);
// cx.add_action( // cx.add_action(
// |workspace: &mut Workspace, _: &Unfollow, cx: &mut ViewContext<Workspace>| { // |workspace: &mut Workspace, _: &Unfollow, cx: &mut ViewContext<Workspace>| {
// let pane = workspace.active_pane().clone(); // let pane = workspace.active_pane().clone();
// workspace.unfollow(&pane, cx); // workspace.unfollow(&pane, cx);
// }, // },
// ); // );
// cx.add_action( // cx.add_action(
// |workspace: &mut Workspace, action: &Save, cx: &mut ViewContext<Workspace>| { // |workspace: &mut Workspace, action: &Save, cx: &mut ViewContext<Workspace>| {
// workspace // workspace
// .save_active_item(action.save_intent.unwrap_or(SaveIntent::Save), cx) // .save_active_item(action.save_intent.unwrap_or(SaveIntent::Save), cx)
// .detach_and_log_err(cx); // .detach_and_log_err(cx);
// }, // },
// ); // );
// cx.add_action( // cx.add_action(
// |workspace: &mut Workspace, _: &SaveAs, cx: &mut ViewContext<Workspace>| { // |workspace: &mut Workspace, _: &SaveAs, cx: &mut ViewContext<Workspace>| {
// workspace // workspace
// .save_active_item(SaveIntent::SaveAs, cx) // .save_active_item(SaveIntent::SaveAs, cx)
// .detach_and_log_err(cx); // .detach_and_log_err(cx);
// }, // },
// ); // );
// cx.add_action(|workspace: &mut Workspace, _: &ActivatePreviousPane, cx| { // cx.add_action(|workspace: &mut Workspace, _: &ActivatePreviousPane, cx| {
// workspace.activate_previous_pane(cx) // workspace.activate_previous_pane(cx)
// }); // });
// cx.add_action(|workspace: &mut Workspace, _: &ActivateNextPane, cx| { // cx.add_action(|workspace: &mut Workspace, _: &ActivateNextPane, cx| {
// workspace.activate_next_pane(cx) // workspace.activate_next_pane(cx)
// }); // });
// cx.add_action( // cx.add_action(
// |workspace: &mut Workspace, action: &ActivatePaneInDirection, cx| { // |workspace: &mut Workspace, action: &ActivatePaneInDirection, cx| {
// workspace.activate_pane_in_direction(action.0, cx) // workspace.activate_pane_in_direction(action.0, cx)
// }, // },
// ); // );
// cx.add_action( // cx.add_action(
// |workspace: &mut Workspace, action: &SwapPaneInDirection, cx| { // |workspace: &mut Workspace, action: &SwapPaneInDirection, cx| {
// workspace.swap_pane_in_direction(action.0, cx) // workspace.swap_pane_in_direction(action.0, cx)
// }, // },
// ); // );
// cx.add_action(|workspace: &mut Workspace, _: &ToggleLeftDock, cx| { // cx.add_action(|workspace: &mut Workspace, _: &ToggleLeftDock, cx| {
// workspace.toggle_dock(DockPosition::Left, cx); // workspace.toggle_dock(DockPosition::Left, cx);
// }); // });
// cx.add_action(|workspace: &mut Workspace, _: &ToggleRightDock, cx| { // cx.add_action(|workspace: &mut Workspace, _: &ToggleRightDock, cx| {
// workspace.toggle_dock(DockPosition::Right, cx); // workspace.toggle_dock(DockPosition::Right, cx);
// }); // });
// cx.add_action(|workspace: &mut Workspace, _: &ToggleBottomDock, cx| { // cx.add_action(|workspace: &mut Workspace, _: &ToggleBottomDock, cx| {
// workspace.toggle_dock(DockPosition::Bottom, cx); // workspace.toggle_dock(DockPosition::Bottom, cx);
// }); // });
// cx.add_action(|workspace: &mut Workspace, _: &CloseAllDocks, cx| { // cx.add_action(|workspace: &mut Workspace, _: &CloseAllDocks, cx| {
// workspace.close_all_docks(cx); // workspace.close_all_docks(cx);
// }); // });
// cx.add_action(Workspace::activate_pane_at_index); // cx.add_action(Workspace::activate_pane_at_index);
// cx.add_action(|workspace: &mut Workspace, _: &ReopenClosedItem, cx| { // cx.add_action(|workspace: &mut Workspace, _: &ReopenClosedItem, cx| {
// workspace.reopen_closed_item(cx).detach(); // workspace.reopen_closed_item(cx).detach();
// }); // });
// cx.add_action(|workspace: &mut Workspace, _: &GoBack, cx| { // cx.add_action(|workspace: &mut Workspace, _: &GoBack, cx| {
// workspace // workspace
// .go_back(workspace.active_pane().downgrade(), cx) // .go_back(workspace.active_pane().downgrade(), cx)
// .detach(); // .detach();
// }); // });
// cx.add_action(|workspace: &mut Workspace, _: &GoForward, cx| { // cx.add_action(|workspace: &mut Workspace, _: &GoForward, cx| {
// workspace // workspace
// .go_forward(workspace.active_pane().downgrade(), cx) // .go_forward(workspace.active_pane().downgrade(), cx)
// .detach(); // .detach();
// }); // });
// cx.add_action(|_: &mut Workspace, _: &install_cli::Install, cx| { // cx.add_action(|_: &mut Workspace, _: &install_cli::Install, cx| {
// cx.spawn(|workspace, mut cx| async move { // cx.spawn(|workspace, mut cx| async move {
// let err = install_cli::install_cli(&cx) // let err = install_cli::install_cli(&cx)
// .await // .await
// .context("Failed to create CLI symlink"); // .context("Failed to create CLI symlink");
// workspace.update(&mut cx, |workspace, cx| { // workspace.update(&mut cx, |workspace, cx| {
// if matches!(err, Err(_)) { // if matches!(err, Err(_)) {
// err.notify_err(workspace, cx); // err.notify_err(workspace, cx);
// } else { // } else {
// workspace.show_notification(1, cx, |cx| { // workspace.show_notification(1, cx, |cx| {
// cx.build_view(|_| { // cx.build_view(|_| {
// MessageNotification::new("Successfully installed the `zed` binary") // MessageNotification::new("Successfully installed the `zed` binary")
// }) // })
// }); // });
// } // }
// }) // })
// }) // })
// .detach(); // .detach();
// }); // });
// } }
type ProjectItemBuilders = type ProjectItemBuilders =
HashMap<TypeId, fn(Model<Project>, AnyModel, &mut ViewContext<Pane>) -> Box<dyn ItemHandle>>; HashMap<TypeId, fn(Model<Project>, AnyModel, &mut ViewContext<Pane>) -> Box<dyn ItemHandle>>;
@ -553,7 +553,7 @@ pub struct Workspace {
panes_by_item: HashMap<EntityId, WeakView<Pane>>, panes_by_item: HashMap<EntityId, WeakView<Pane>>,
active_pane: View<Pane>, active_pane: View<Pane>,
last_active_center_pane: Option<WeakView<Pane>>, last_active_center_pane: Option<WeakView<Pane>>,
// last_active_view_id: Option<proto::ViewId>, last_active_view_id: Option<proto::ViewId>,
// status_bar: View<StatusBar>, // status_bar: View<StatusBar>,
// titlebar_item: Option<AnyViewHandle>, // titlebar_item: Option<AnyViewHandle>,
notifications: Vec<(TypeId, usize, Box<dyn NotificationHandle>)>, notifications: Vec<(TypeId, usize, Box<dyn NotificationHandle>)>,
@ -725,24 +725,25 @@ impl Workspace {
} }
let subscriptions = vec![ let subscriptions = vec![
// todo!() cx.observe_window_activation(Self::on_window_activation_changed),
// cx.observe_fullscreen(|_, _, cx| cx.notify()), cx.observe_window_bounds(move |_, cx| {
// cx.observe_window_activation(Self::on_window_activation_changed), if let Some(display) = cx.display() {
// cx.observe_window_bounds(move |_, mut bounds, display, cx| { // Transform fixed bounds to be stored in terms of the containing display
// // Transform fixed bounds to be stored in terms of the containing display let mut bounds = cx.window_bounds();
// if let WindowBounds::Fixed(mut window_bounds) = bounds { if let WindowBounds::Fixed(window_bounds) = &mut bounds {
// if let Some(screen) = cx.platform().screen_by_id(display) { let display_bounds = display.bounds();
// let screen_bounds = screen.bounds(); window_bounds.origin.x -= display_bounds.origin.x;
// window_bounds.origin.x -= screen_bounds.origin.x; window_bounds.origin.y -= display_bounds.origin.y;
// window_bounds.origin.y -= screen_bounds.origin.y; }
// bounds = WindowBounds::Fixed(window_bounds);
// }
// }
// cx.background() if let Some(display_uuid) = display.uuid().log_err() {
// .spawn(DB.set_window_bounds(workspace_id, bounds, display)) cx.background_executor()
// .detach_and_log_err(cx); .spawn(DB.set_window_bounds(workspace_id, bounds, display_uuid))
// }), .detach_and_log_err(cx);
}
}
cx.notify();
}),
cx.observe(&left_dock, |this, _, cx| { cx.observe(&left_dock, |this, _, cx| {
this.serialize_workspace(cx); this.serialize_workspace(cx);
cx.notify(); cx.notify();
@ -768,7 +769,7 @@ impl Workspace {
panes_by_item: Default::default(), panes_by_item: Default::default(),
active_pane: center_pane.clone(), active_pane: center_pane.clone(),
last_active_center_pane: Some(center_pane.downgrade()), last_active_center_pane: Some(center_pane.downgrade()),
// last_active_view_id: None, last_active_view_id: None,
// status_bar, // status_bar,
// titlebar_item: None, // titlebar_item: None,
notifications: Default::default(), notifications: Default::default(),
@ -3018,33 +3019,33 @@ impl Workspace {
Ok(()) Ok(())
} }
// fn update_active_view_for_followers(&mut self, cx: &AppContext) { fn update_active_view_for_followers(&mut self, cx: &mut ViewContext<Self>) {
// let mut is_project_item = true; let mut is_project_item = true;
// let mut update = proto::UpdateActiveView::default(); let mut update = proto::UpdateActiveView::default();
// if self.active_pane.read(cx).has_focus() { if self.active_pane.read(cx).has_focus() {
// let item = self let item = self
// .active_item(cx) .active_item(cx)
// .and_then(|item| item.to_followable_item_handle(cx)); .and_then(|item| item.to_followable_item_handle(cx));
// if let Some(item) = item { if let Some(item) = item {
// is_project_item = item.is_project_item(cx); is_project_item = item.is_project_item(cx);
// update = proto::UpdateActiveView { update = proto::UpdateActiveView {
// id: item id: item
// .remote_id(&self.app_state.client, cx) .remote_id(&self.app_state.client, cx)
// .map(|id| id.to_proto()), .map(|id| id.to_proto()),
// leader_id: self.leader_for_pane(&self.active_pane), leader_id: self.leader_for_pane(&self.active_pane),
// }; };
// } }
// } }
// if update.id != self.last_active_view_id { if update.id != self.last_active_view_id {
// self.last_active_view_id = update.id.clone(); self.last_active_view_id = update.id.clone();
// self.update_followers( self.update_followers(
// is_project_item, is_project_item,
// proto::update_followers::Variant::UpdateActiveView(update), proto::update_followers::Variant::UpdateActiveView(update),
// cx, cx,
// ); );
// } }
// } }
fn update_followers( fn update_followers(
&self, &self,
@ -3154,31 +3155,31 @@ impl Workspace {
// Some(cx.build_view(|cx| SharedScreen::new(&track, peer_id, user.clone(), cx))) // Some(cx.build_view(|cx| SharedScreen::new(&track, peer_id, user.clone(), cx)))
// } // }
// pub fn on_window_activation_changed(&mut self, active: bool, cx: &mut ViewContext<Self>) { pub fn on_window_activation_changed(&mut self, cx: &mut ViewContext<Self>) {
// if active { if cx.is_window_active() {
// self.update_active_view_for_followers(cx); self.update_active_view_for_followers(cx);
// cx.background() cx.background_executor()
// .spawn(persistence::DB.update_timestamp(self.database_id())) .spawn(persistence::DB.update_timestamp(self.database_id()))
// .detach(); .detach();
// } else { } else {
// for pane in &self.panes { for pane in &self.panes {
// pane.update(cx, |pane, cx| { pane.update(cx, |pane, cx| {
// if let Some(item) = pane.active_item() { if let Some(item) = pane.active_item() {
// item.workspace_deactivated(cx); item.workspace_deactivated(cx);
// } }
// if matches!( if matches!(
// settings::get::<WorkspaceSettings>(cx).autosave, WorkspaceSettings::get_global(cx).autosave,
// AutosaveSetting::OnWindowChange | AutosaveSetting::OnFocusChange AutosaveSetting::OnWindowChange | AutosaveSetting::OnFocusChange
// ) { ) {
// for item in pane.items() { for item in pane.items() {
// Pane::autosave_item(item.as_ref(), self.project.clone(), cx) Pane::autosave_item(item.as_ref(), self.project.clone(), cx)
// .detach_and_log_err(cx); .detach_and_log_err(cx);
// } }
// } }
// }); });
// } }
// } }
// } }
fn active_call(&self) -> Option<&Model<ActiveCall>> { fn active_call(&self) -> Option<&Model<ActiveCall>> {
self.active_call.as_ref().map(|(call, _)| call) self.active_call.as_ref().map(|(call, _)| call)

View file

@ -188,8 +188,7 @@ fn main() {
// audio::init(Assets, cx); // audio::init(Assets, cx);
// auto_update::init(http.clone(), client::ZED_SERVER_URL.clone(), cx); // auto_update::init(http.clone(), client::ZED_SERVER_URL.clone(), cx);
// todo!("workspace") workspace2::init(app_state.clone(), cx);
// workspace::init(app_state.clone(), cx);
// recent_projects::init(cx); // recent_projects::init(cx);
// journal2::init(app_state.clone(), cx); // journal2::init(app_state.clone(), cx);