Refactorings

This commit is contained in:
Conrad Irwin 2023-11-17 13:23:12 -07:00
parent eb04160d2d
commit ceb20dea96
5 changed files with 119 additions and 64 deletions

View file

@ -191,6 +191,10 @@ impl AnyView {
self.model.entity_type self.model.entity_type
} }
pub fn entity_id(&self) -> EntityId {
self.model.entity_id()
}
pub(crate) fn draw( pub(crate) fn draw(
&self, &self,
origin: Point<Pixels>, origin: Point<Pixels>,

View file

@ -1,6 +1,6 @@
pub mod file_associations; pub mod file_associations;
mod project_panel_settings; mod project_panel_settings;
use settings::Settings; use settings::{Settings, SettingsStore};
use db::kvp::KEY_VALUE_STORE; use db::kvp::KEY_VALUE_STORE;
use editor::{scroll::autoscroll::Autoscroll, Cancel, Editor}; use editor::{scroll::autoscroll::Autoscroll, Cancel, Editor};
@ -34,7 +34,7 @@ use ui::{h_stack, v_stack, IconElement, Label};
use unicase::UniCase; use unicase::UniCase;
use util::{maybe, ResultExt, TryFutureExt}; use util::{maybe, ResultExt, TryFutureExt};
use workspace::{ use workspace::{
dock::{DockPosition, PanelEvent}, dock::{DockPosition, Panel, PanelEvent},
Workspace, Workspace,
}; };
@ -148,7 +148,6 @@ pub enum Event {
SplitEntry { SplitEntry {
entry_id: ProjectEntryId, entry_id: ProjectEntryId,
}, },
DockPositionChanged,
Focus, Focus,
NewSearchInDirectory { NewSearchInDirectory {
dir_entry: Entry, dir_entry: Entry,
@ -244,16 +243,17 @@ impl ProjectPanel {
this.update_visible_entries(None, cx); this.update_visible_entries(None, cx);
// Update the dock position when the setting changes. // Update the dock position when the setting changes.
// todo!() let mut old_dock_position = this.position(cx);
// let mut old_dock_position = this.position(cx); ProjectPanelSettings::register(cx);
// cx.observe_global::<SettingsStore, _>(move |this, cx| { cx.observe_global::<SettingsStore>(move |this, cx| {
// let new_dock_position = this.position(cx); dbg!("OLA!");
// if new_dock_position != old_dock_position { let new_dock_position = this.position(cx);
// old_dock_position = new_dock_position; if new_dock_position != old_dock_position {
// cx.emit(Event::DockPositionChanged); old_dock_position = new_dock_position;
// } cx.emit(PanelEvent::ChangePosition);
// }) }
// .detach(); })
.detach();
this this
}); });
@ -1485,7 +1485,7 @@ impl EventEmitter<Event> for ProjectPanel {}
impl EventEmitter<PanelEvent> for ProjectPanel {} impl EventEmitter<PanelEvent> for ProjectPanel {}
impl workspace::dock::Panel for ProjectPanel { impl Panel for ProjectPanel {
fn position(&self, cx: &WindowContext) -> DockPosition { fn position(&self, cx: &WindowContext) -> DockPosition {
match ProjectPanelSettings::get_global(cx).dock { match ProjectPanelSettings::get_global(cx).dock {
ProjectPanelDockPosition::Left => DockPosition::Left, ProjectPanelDockPosition::Left => DockPosition::Left,

View file

@ -77,6 +77,7 @@ pub fn handle_settings_file_changes(
}); });
cx.spawn(move |mut cx| async move { cx.spawn(move |mut cx| async move {
while let Some(user_settings_content) = user_settings_file_rx.next().await { while let Some(user_settings_content) = user_settings_file_rx.next().await {
eprintln!("settings file changed");
let result = cx.update_global(|store: &mut SettingsStore, cx| { let result = cx.update_global(|store: &mut SettingsStore, cx| {
store store
.set_user_settings(&user_settings_content, cx) .set_user_settings(&user_settings_content, cx)

View file

@ -42,7 +42,7 @@ pub trait Panel: FocusableView + EventEmitter<PanelEvent> {
} }
pub trait PanelHandle: Send + Sync { pub trait PanelHandle: Send + Sync {
fn id(&self) -> EntityId; fn entity_id(&self) -> EntityId;
fn persistent_name(&self) -> &'static str; fn persistent_name(&self) -> &'static str;
fn position(&self, cx: &WindowContext) -> DockPosition; fn position(&self, cx: &WindowContext) -> DockPosition;
fn position_is_valid(&self, position: DockPosition, cx: &WindowContext) -> bool; fn position_is_valid(&self, position: DockPosition, cx: &WindowContext) -> bool;
@ -64,8 +64,8 @@ impl<T> PanelHandle for View<T>
where where
T: Panel, T: Panel,
{ {
fn id(&self) -> EntityId { fn entity_id(&self) -> EntityId {
self.entity_id() Entity::entity_id(self)
} }
fn persistent_name(&self) -> &'static str { fn persistent_name(&self) -> &'static str {
@ -256,20 +256,19 @@ impl Dock {
} }
} }
// todo!() pub fn set_panel_zoomed(&mut self, panel: &AnyView, zoomed: bool, cx: &mut ViewContext<Self>) {
// pub fn set_panel_zoomed(&mut self, panel: &AnyView, zoomed: bool, cx: &mut ViewContext<Self>) { for entry in &mut self.panel_entries {
// for entry in &mut self.panel_entries { if entry.panel.entity_id() == panel.entity_id() {
// if entry.panel.as_any() == panel { if zoomed != entry.panel.is_zoomed(cx) {
// if zoomed != entry.panel.is_zoomed(cx) { entry.panel.set_zoomed(zoomed, cx);
// entry.panel.set_zoomed(zoomed, cx); }
// } } else if entry.panel.is_zoomed(cx) {
// } else if entry.panel.is_zoomed(cx) { entry.panel.set_zoomed(false, cx);
// entry.panel.set_zoomed(false, cx); }
// } }
// }
// cx.notify(); cx.notify();
// } }
pub fn zoom_out(&mut self, cx: &mut ViewContext<Self>) { pub fn zoom_out(&mut self, cx: &mut ViewContext<Self>) {
for entry in &mut self.panel_entries { for entry in &mut self.panel_entries {
@ -279,42 +278,91 @@ impl Dock {
} }
} }
pub(crate) fn add_panel<T: Panel>(&mut self, panel: View<T>, cx: &mut ViewContext<Self>) { pub(crate) fn add_panel<T: Panel>(
&mut self,
panel: View<T>,
workspace: WeakView<Workspace>,
cx: &mut ViewContext<Self>,
) {
let subscriptions = [ let subscriptions = [
cx.observe(&panel, |_, _, cx| cx.notify()), cx.observe(&panel, |_, _, cx| cx.notify()),
cx.subscribe(&panel, |this, panel, event, cx| { cx.subscribe(&panel, move |this, panel, event, cx| match event {
match event { PanelEvent::ChangePosition => {
PanelEvent::ChangePosition => { let new_position = panel.read(cx).position(cx);
//todo!()
// see: Workspace::add_panel_with_extra_event_handler let Ok(new_dock) = workspace.update(cx, |workspace, cx| {
} if panel.is_zoomed(cx) {
PanelEvent::ZoomIn => { workspace.zoomed_position = Some(new_position);
//todo!()
// see: Workspace::add_panel_with_extra_event_handler
}
PanelEvent::ZoomOut => {
// todo!()
// // see: Workspace::add_panel_with_extra_event_handler
}
PanelEvent::Activate => {
if let Some(ix) = this
.panel_entries
.iter()
.position(|entry| entry.panel.id() == panel.id())
{
this.set_open(true, cx);
this.activate_panel(ix, cx);
//` todo!()
// cx.focus(&panel);
} }
} match new_position {
PanelEvent::Close => { DockPosition::Left => &workspace.left_dock,
if this.visible_panel().map_or(false, |p| p.id() == panel.id()) { DockPosition::Bottom => &workspace.bottom_dock,
this.set_open(false, cx); DockPosition::Right => &workspace.right_dock,
} }
} .clone()
PanelEvent::Focus => todo!(), }) else {
return;
};
let was_visible = this.is_open()
&& this.visible_panel().map_or(false, |active_panel| {
active_panel.entity_id() == Entity::entity_id(&panel)
});
this.remove_panel(&panel, cx);
new_dock.update(cx, |new_dock, cx| {
new_dock.add_panel(panel.clone(), workspace.clone(), cx);
if was_visible {
new_dock.set_open(true, cx);
new_dock.activate_panel(this.panels_len() - 1, cx);
}
});
} }
PanelEvent::ZoomIn => {
this.set_panel_zoomed(&panel.to_any(), true, cx);
if !panel.has_focus(cx) {
cx.focus_view(&panel);
}
workspace
.update(cx, |workspace, cx| {
workspace.zoomed = Some(panel.downgrade().into());
workspace.zoomed_position = Some(panel.read(cx).position(cx));
})
.ok();
}
PanelEvent::ZoomOut => {
this.set_panel_zoomed(&panel.to_any(), false, cx);
workspace
.update(cx, |workspace, cx| {
if workspace.zoomed_position == Some(this.position) {
workspace.zoomed = None;
workspace.zoomed_position = None;
}
cx.notify();
})
.ok();
}
PanelEvent::Activate => {
if let Some(ix) = this
.panel_entries
.iter()
.position(|entry| entry.panel.entity_id() == Entity::entity_id(&panel))
{
this.set_open(true, cx);
this.activate_panel(ix, cx);
cx.focus_view(&panel);
}
}
PanelEvent::Close => {
if this
.visible_panel()
.map_or(false, |p| p.entity_id() == Entity::entity_id(&panel))
{
this.set_open(false, cx);
}
}
PanelEvent::Focus => todo!(),
}), }),
]; ];
@ -337,7 +385,7 @@ impl Dock {
if let Some(panel_ix) = self if let Some(panel_ix) = self
.panel_entries .panel_entries
.iter() .iter()
.position(|entry| entry.panel.id() == panel.id()) .position(|entry| entry.panel.entity_id() == Entity::entity_id(panel))
{ {
if panel_ix == self.active_panel_index { if panel_ix == self.active_panel_index {
self.active_panel_index = 0; self.active_panel_index = 0;
@ -398,7 +446,7 @@ impl Dock {
pub fn panel_size(&self, panel: &dyn PanelHandle, cx: &WindowContext) -> Option<f32> { pub fn panel_size(&self, panel: &dyn PanelHandle, cx: &WindowContext) -> Option<f32> {
self.panel_entries self.panel_entries
.iter() .iter()
.find(|entry| entry.panel.id() == panel.id()) .find(|entry| entry.panel.entity_id() == panel.entity_id())
.map(|entry| entry.panel.size(cx)) .map(|entry| entry.panel.size(cx))
} }

View file

@ -831,7 +831,9 @@ impl Workspace {
DockPosition::Right => &self.right_dock, DockPosition::Right => &self.right_dock,
}; };
dock.update(cx, |dock, cx| dock.add_panel(panel, cx)); dock.update(cx, |dock, cx| {
dock.add_panel(panel, self.weak_self.clone(), cx)
});
} }
pub fn status_bar(&self) -> &View<StatusBar> { pub fn status_bar(&self) -> &View<StatusBar> {