Fix: viewport MSPACE activation and navigation

Four bugs prevented viewports from working like AutoCAD model-space windows:

1. Dead-code double-click block: `!is_down2` was always false because
   `is_down2 = sel.left_down` is captured while the button is still held
   (before the selection block clears it). Changed to `is_down` so the
   double-click handler actually fires.

2. Inverted pan direction: pan_active_viewport used `-=` on the delta
   returned by screen_delta_to_world, which is the same delta that cam.pan
   *adds* to its target — causing reversed drag. Changed to `+=`.

3. Zoom not cursor-centered: zoom_active_viewport now accepts an optional
   paper-space cursor position and adjusts view_target so the model point
   under the cursor stays fixed (same as cam.zoom_about_point).

4. ESC did not exit MSPACE: CommandEscape now calls ExitViewport when
   active_viewport is set and no command is running.

Also added MS/MSPACE and PSPACE command aliases, and MspaceCommand /
PspaceCommand messages so MS auto-enters the first viewport.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
This commit is contained in:
Hakan SevenandClaude Sonnet 4.6 committed 2026-03-31 12:52:07 +03:00
1 parent 0b8f9bfa5b
commit 0beb589f2c
4 files changed
+168 -30

No files matched your search

+9 -1
View File
@@ -901,11 +901,19 @@ impl H7CAD {
}
}
// ── MSPACE / PSPACE ───────────────────────────────────────────
"MS"|"MSPACE" => {
return Task::done(Message::MspaceCommand);
}
"PSPACE" => {
return Task::done(Message::PspaceCommand);
}
// ── Plot / Page Setup ──────────────────────────────────────────
"PRINT"|"PLOT"|"EXPORT" => {
return Task::done(Message::PlotExport);
}
"PAGESETUP"|"PS" => {
"PAGESETUP" => {
if self.tabs[i].scene.current_layout == "Model" {
self.command_line.push_error("PAGESETUP: switch to a paper space layout first.");
} else {
+4
View File
@@ -224,6 +224,10 @@ pub enum Message {
EnterViewport(acadrust::Handle),
/// Exit MSPACE and return to paper-space editing (PSPACE).
ExitViewport,
/// MS command: enter MSPACE for the first available viewport.
MspaceCommand,
/// PS command: exit MSPACE (PSPACE).
PspaceCommand,
/// Switch to a named layout ("Model" or paper space layout name).
LayoutSwitch(String),
/// Create a new paper space layout.
+75 -19
View File
@@ -312,6 +312,9 @@ impl H7CAD {
if let Some(r) = result {
return self.apply_cmd_result(r);
}
} else if self.tabs[i].scene.active_viewport.is_some() {
// ESC while in MSPACE → exit back to paper space.
return Task::done(Message::ExitViewport);
} else {
self.tabs[i].scene.deselect_all();
self.refresh_properties();
@@ -697,9 +700,10 @@ impl H7CAD {
let (vw, vh) = vp_size;
let bounds = iced::Rectangle { x: 0.0, y: 0.0, width: vw, height: vh };
let cam = self.tabs[i].scene.camera.borrow();
let raw = cam.pick_on_target_plane(p, bounds);
let raw_paper = cam.pick_on_target_plane(p, bounds);
let vp_mat = cam.view_proj(bounds);
drop(cam);
let raw = self.tabs[i].scene.paper_to_model(raw_paper);
let edited_name = grip.handle.value().to_string();
let all_wires = self.tabs[i].scene.entity_wires();
@@ -738,9 +742,12 @@ impl H7CAD {
let (vw, vh) = vp_size;
let bounds = iced::Rectangle { x: 0.0, y: 0.0, width: vw, height: vh };
let cam = self.tabs[i].scene.camera.borrow();
let cursor_world = cam.pick_on_target_plane(p, bounds);
let cursor_paper = cam.pick_on_target_plane(p, bounds);
let view_proj = cam.view_proj(bounds);
drop(cam);
// In MSPACE, map paper-space cursor to model space so that
// command previews and snapping work in the correct coordinate space.
let cursor_world = self.tabs[i].scene.paper_to_model(cursor_paper);
let all_wires = self.tabs[i].scene.entity_wires();
let needs_tan = self.tabs[i]
@@ -1075,7 +1082,7 @@ impl H7CAD {
// ── Double-click: enter/exit MSPACE ───────────────────────
// Only when no command is running, no drag, and we're in paper space.
if is_click
&& !is_down2
&& is_down // ensures there was a matching left-press
&& self.tabs[i].active_cmd.is_none()
&& self.tabs[i].scene.current_layout != "Model"
{
@@ -1096,22 +1103,42 @@ impl H7CAD {
if is_double {
let (vw, vh) = self.tabs[i].scene.selection.borrow().vp_size;
let bounds = iced::Rectangle { x: 0.0, y: 0.0, width: vw, height: vh };
let vp_mat = self.tabs[i].scene.camera.borrow().view_proj(bounds);
let all_wires = self.tabs[i].scene.entity_wires();
let hit = scene::hit_test::click_hit(p, &all_wires, vp_mat, bounds)
.and_then(|s| Scene::handle_from_wire_name(s));
if let Some(handle) = hit {
// Double-clicked on a user viewport → enter MSPACE.
if let Some(AcadEntityType::Viewport(vp)) =
self.tabs[i].scene.document.get_entity(handle)
{
if vp.id > 1 {
return Task::done(Message::EnterViewport(handle));
}
}
// 1) Try direct wire hit — works when the border is clicked.
let hit_vp: Option<acadrust::Handle> = {
let vp_mat = self.tabs[i].scene.camera.borrow().view_proj(bounds);
let all_wires = self.tabs[i].scene.entity_wires();
scene::hit_test::click_hit(p, &all_wires, vp_mat, bounds)
.and_then(|s| Scene::handle_from_wire_name(s))
.and_then(|h| {
if let Some(AcadEntityType::Viewport(vp)) =
self.tabs[i].scene.document.get_entity(h)
{
if vp.id > 1 { Some(h) } else { None }
} else {
None
}
})
};
// 2) Geometric fallback: check if the cursor is inside any
// viewport's bounding rectangle in paper space. This handles
// double-clicks on model-entity content wires or empty areas.
let hit_vp = hit_vp.or_else(|| {
let paper_pt = self.tabs[i]
.scene
.camera
.borrow()
.pick_on_target_plane(p, bounds);
self.tabs[i]
.scene
.viewport_at_paper_point(paper_pt.x, paper_pt.y)
});
if let Some(handle) = hit_vp {
return Task::done(Message::EnterViewport(handle));
} else if self.tabs[i].scene.active_viewport.is_some() {
// Double-clicked on empty area while in MSPACE → exit.
// Double-clicked outside all viewports while in MSPACE → exit.
return Task::done(Message::ExitViewport);
}
}
@@ -1189,8 +1216,14 @@ impl H7CAD {
let (vw, vh) = self.tabs[i].scene.selection.borrow().vp_size;
let bounds = iced::Rectangle { x: 0.0, y: 0.0, width: vw, height: vh };
if self.tabs[i].scene.active_viewport.is_some() {
// In MSPACE: zoom the active viewport's model-space view.
self.tabs[i].scene.zoom_active_viewport(s);
// In MSPACE: zoom the active viewport's model-space view,
// keeping the model point under the cursor stationary.
let cursor_paper = cursor.map(|cp| {
let pt = self.tabs[i].scene.camera.borrow()
.pick_on_target_plane(cp, bounds);
glam::Vec2::new(pt.x, pt.y)
});
self.tabs[i].scene.zoom_active_viewport(s, cursor_paper);
} else {
let mut cam = self.tabs[i].scene.camera.borrow_mut();
if let Some(cursor) = cursor {
@@ -1658,6 +1691,29 @@ impl H7CAD {
Task::none()
}
Message::MspaceCommand => {
let i = self.active_tab;
if self.tabs[i].scene.current_layout == "Model" {
self.command_line.push_error("MS is only available in paper space layouts.");
return Task::none();
}
if self.tabs[i].scene.active_viewport.is_some() {
// Already in MSPACE — nothing to do.
return Task::none();
}
match self.tabs[i].scene.first_user_viewport() {
Some(handle) => Task::done(Message::EnterViewport(handle)),
None => {
self.command_line.push_error("No viewport found in this layout.");
Task::none()
}
}
}
Message::PspaceCommand => {
Task::done(Message::ExitViewport)
}
Message::Undo => { self.undo_active_tab(); Task::none() }
Message::Redo => { self.redo_active_tab(); Task::none() }