Test zooming panels

This commit is contained in:
Antonio Scandurra 2023-05-22 19:30:31 +02:00
parent 3ca95678f1
commit 048498e39b
2 changed files with 68 additions and 24 deletions

View file

@ -577,10 +577,29 @@ pub(crate) mod test {
PositionChanged, PositionChanged,
Activated, Activated,
Closed, Closed,
ZoomIn,
ZoomOut,
Focus,
} }
pub struct TestPanel { pub struct TestPanel {
pub position: DockPosition, pub position: DockPosition,
pub zoomed: bool,
pub active: bool,
pub has_focus: bool,
pub size: f32,
}
impl TestPanel {
pub fn new(position: DockPosition) -> Self {
Self {
position,
zoomed: false,
active: false,
has_focus: false,
size: 300.,
}
}
} }
impl Entity for TestPanel { impl Entity for TestPanel {
@ -595,6 +614,14 @@ pub(crate) mod test {
fn render(&mut self, _: &mut ViewContext<'_, '_, Self>) -> AnyElement<Self> { fn render(&mut self, _: &mut ViewContext<'_, '_, Self>) -> AnyElement<Self> {
Empty::new().into_any() Empty::new().into_any()
} }
fn focus_in(&mut self, _: AnyViewHandle, _: &mut ViewContext<Self>) {
self.has_focus = true;
}
fn focus_out(&mut self, _: AnyViewHandle, _: &mut ViewContext<Self>) {
self.has_focus = false;
}
} }
impl Panel for TestPanel { impl Panel for TestPanel {
@ -612,26 +639,23 @@ pub(crate) mod test {
} }
fn is_zoomed(&self, _: &WindowContext) -> bool { fn is_zoomed(&self, _: &WindowContext) -> bool {
unimplemented!() self.zoomed
} }
fn set_zoomed(&mut self, _zoomed: bool, _cx: &mut ViewContext<Self>) { fn set_zoomed(&mut self, zoomed: bool, _cx: &mut ViewContext<Self>) {
unimplemented!() self.zoomed = zoomed;
} }
fn set_active(&mut self, _active: bool, _cx: &mut ViewContext<Self>) { fn set_active(&mut self, active: bool, _cx: &mut ViewContext<Self>) {
unimplemented!() self.active = active;
} }
fn size(&self, _: &WindowContext) -> f32 { fn size(&self, _: &WindowContext) -> f32 {
match self.position.axis() { self.size
Axis::Horizontal => 300.,
Axis::Vertical => 200.,
}
} }
fn set_size(&mut self, _: f32, _: &mut ViewContext<Self>) { fn set_size(&mut self, size: f32, _: &mut ViewContext<Self>) {
unimplemented!() self.size = size;
} }
fn icon_path(&self) -> &'static str { fn icon_path(&self) -> &'static str {
@ -646,12 +670,12 @@ pub(crate) mod test {
matches!(event, TestPanelEvent::PositionChanged) matches!(event, TestPanelEvent::PositionChanged)
} }
fn should_zoom_in_on_event(_: &Self::Event) -> bool { fn should_zoom_in_on_event(event: &Self::Event) -> bool {
false matches!(event, TestPanelEvent::ZoomIn)
} }
fn should_zoom_out_on_event(_: &Self::Event) -> bool { fn should_zoom_out_on_event(event: &Self::Event) -> bool {
false matches!(event, TestPanelEvent::ZoomOut)
} }
fn should_activate_on_event(event: &Self::Event) -> bool { fn should_activate_on_event(event: &Self::Event) -> bool {
@ -663,11 +687,11 @@ pub(crate) mod test {
} }
fn has_focus(&self, _cx: &WindowContext) -> bool { fn has_focus(&self, _cx: &WindowContext) -> bool {
unimplemented!() self.has_focus
} }
fn is_focus_event(_: &Self::Event) -> bool { fn is_focus_event(event: &Self::Event) -> bool {
unimplemented!() matches!(event, TestPanelEvent::Focus)
} }
} }
} }

View file

@ -3931,16 +3931,12 @@ mod tests {
let (panel_1, panel_2) = workspace.update(cx, |workspace, cx| { let (panel_1, panel_2) = workspace.update(cx, |workspace, cx| {
// Add panel_1 on the left, panel_2 on the right. // Add panel_1 on the left, panel_2 on the right.
let panel_1 = cx.add_view(|_| TestPanel { let panel_1 = cx.add_view(|_| TestPanel::new(DockPosition::Left));
position: DockPosition::Left,
});
workspace.add_panel(panel_1.clone(), cx); workspace.add_panel(panel_1.clone(), cx);
workspace workspace
.left_dock() .left_dock()
.update(cx, |left_dock, cx| left_dock.set_open(true, cx)); .update(cx, |left_dock, cx| left_dock.set_open(true, cx));
let panel_2 = cx.add_view(|_| TestPanel { let panel_2 = cx.add_view(|_| TestPanel::new(DockPosition::Right));
position: DockPosition::Right,
});
workspace.add_panel(panel_2.clone(), cx); workspace.add_panel(panel_2.clone(), cx);
workspace workspace
.right_dock() .right_dock()
@ -4060,6 +4056,30 @@ mod tests {
); );
}); });
// Emitting a ZoomIn event shows the panel as zoomed.
panel_1.update(cx, |_, cx| cx.emit(TestPanelEvent::ZoomIn));
workspace.read_with(cx, |workspace, cx| {
assert_eq!(workspace.zoomed(cx), Some(panel_1.clone().into_any()));
});
// If focus is transferred elsewhere in the workspace, the panel is no longer zoomed.
workspace.update(cx, |_, cx| cx.focus_self());
workspace.read_with(cx, |workspace, cx| {
assert_eq!(workspace.zoomed(cx), None);
});
// When focus is transferred back to the panel, it is zoomed again.
panel_1.update(cx, |_, cx| cx.focus_self());
workspace.read_with(cx, |workspace, cx| {
assert_eq!(workspace.zoomed(cx), Some(panel_1.clone().into_any()));
});
// Emitting a ZoomOut event unzooms the panel.
panel_1.update(cx, |_, cx| cx.emit(TestPanelEvent::ZoomOut));
workspace.read_with(cx, |workspace, cx| {
assert_eq!(workspace.zoomed(cx), None);
});
// Emit closed event on panel 1, which is active // Emit closed event on panel 1, which is active
panel_1.update(cx, |_, cx| cx.emit(TestPanelEvent::Closed)); panel_1.update(cx, |_, cx| cx.emit(TestPanelEvent::Closed));