Merge pull request #80 from mf4633/feature/plugin-host

Add plugin host (Phase 1)
This commit is contained in:
Hakan Seven 2026-06-10 00:37:37 +03:00 • committed by GitHub
commit 6a70e99dd5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
38 changed files with 1264 additions and 20 deletions

View file

@ -251,6 +251,11 @@ impl OpenCADStudio {
}
CmdResult::ReplaceMany(replacements, additions) => {
let label = self.history_label_from_active_cmd(i, "FILLET");
let was_catchment = self
.tabs[i]
.active_cmd
.as_ref()
.is_some_and(|c| c.name() == "SS_CATCHMENT");
self.push_undo_snapshot(i, label);
for (handle, entities) in replacements {
self.tabs[i].scene.erase_entities(&[handle]);
@ -265,6 +270,10 @@ impl OpenCADStudio {
self.tabs[i].scene.clear_preview_wire();
self.tabs[i].active_cmd = None;
self.tabs[i].snap_result = None;
if was_catchment {
self.command_line
.push_info("Catchment tagged successfully.");
}
self.refresh_properties();
}
CmdResult::ReplaceEntity(handle, new_entities) => {

View file

@ -28,6 +28,10 @@ impl OpenCADStudio {
return Task::done(Message::OpenPathPicked(Some((path, size))));
}
if crate::plugin::try_dispatch(self, i, cmd) {
return Task::none();
}
match cmd {
"NEW" => return Task::done(Message::TabNew),
"OPEN" => return Task::done(Message::OpenFile),

View file

@ -9,6 +9,8 @@ use crate::ui::{LayerPanel, PropertiesPanel};
use acadrust::tables::Ucs;
use acadrust::{CadDocument, Handle};
use iced;
use std::any::{Any, TypeId};
use std::collections::HashMap;
use std::path::PathBuf;
// ── Dynamic input ──────────────────────────────────────────────────────────
@ -100,6 +102,8 @@ pub(super) struct DocumentTab {
/// of the model-space shader. The scene is still constructed so the
/// rest of the code can treat it as a normal tab when reading.
pub(super) is_start: bool,
/// Per-plugin document state (`plugin::BuiltinPlugin` manifest id → state).
pub(super) plugin_state: HashMap<&'static str, Box<dyn Any + Send + Sync>>,
}
impl DocumentTab {
@ -146,9 +150,41 @@ impl DocumentTab {
active_mleader_style: "Standard".to_string(),
last_synced_camera_gen: 0,
is_start: false,
plugin_state: HashMap::new(),
}
}
pub(super) fn plugin_state<T: Any + Send + Sync + 'static>(
&self,
plugin_id: &'static str,
_type_id: TypeId,
) -> Option<&T> {
self.plugin_state.get(plugin_id)?.downcast_ref::<T>()
}
pub(super) fn plugin_state_mut<T: Any + Send + Sync + 'static>(
&mut self,
plugin_id: &'static str,
_type_id: TypeId,
) -> Option<&mut T> {
self.plugin_state.get_mut(plugin_id)?.downcast_mut::<T>()
}
pub(super) fn ensure_plugin_state<T: Any + Send + Sync + 'static>(
&mut self,
plugin_id: &'static str,
init: impl FnOnce() -> T,
) -> &mut T {
if !self.plugin_state.contains_key(plugin_id) {
self.plugin_state.insert(plugin_id, Box::new(init()));
}
self.plugin_state
.get_mut(plugin_id)
.expect("just inserted")
.downcast_mut::<T>()
.expect("plugin_id type mismatch")
}
/// Welcome / Start tab. Carries a dummy Scene so the rest of the app
/// can read tab state uniformly; the viewport renderer detects
/// `is_start` and shows a welcome page instead.

View file

@ -1,5 +1,6 @@
mod cmd_result;
mod commands;
pub mod plugin_host;
mod document;
mod expr_eval;
mod helpers;
@ -1493,6 +1494,22 @@ impl OpenCADStudio {
app
}
#[cfg(test)]
pub(crate) fn new_for_test() -> Self {
Self::new()
}
#[cfg(test)]
pub(crate) fn command_history_info(&self) -> Vec<String> {
use crate::ui::command_line::EntryKind;
self.command_line
.history
.iter()
.filter(|e| e.kind == EntryKind::Info)
.map(|e| e.text.clone())
.collect()
}
/// Boot function for `iced::daemon`: returns initial state plus a task that
/// opens the primary application window.
fn boot() -> (Self, Task<Message>) {

94
src/app/plugin_host.rs Normal file
View file

@ -0,0 +1,94 @@
// HostSession — plugin-facing API implemented inside `app` (private field access).
use std::any::{Any, TypeId};
use acadrust::{CadDocument, EntityType, Handle};
use super::OpenCADStudio;
use crate::command::CadCommand;
/// Session adapter: one active document tab, command line, undo.
pub(crate) struct HostSession<'a> {
app: &'a mut OpenCADStudio,
tab: usize,
}
impl<'a> HostSession<'a> {
pub(crate) fn new(app: &'a mut OpenCADStudio, tab: usize) -> Self {
Self { app, tab }
}
pub fn tab_index(&self) -> usize {
self.tab
}
pub fn document(&self) -> &CadDocument {
&self.app.tabs[self.tab].scene.document
}
pub fn document_mut(&mut self) -> &mut CadDocument {
&mut self.app.tabs[self.tab].scene.document
}
pub fn entities(&self) -> impl Iterator<Item = &EntityType> {
self.document().entities()
}
pub fn entities_mut(&mut self) -> impl Iterator<Item = &mut EntityType> {
self.document_mut().entities_mut()
}
pub fn add_entity(&mut self, entity: EntityType) -> Handle {
self.app.tabs[self.tab].scene.add_entity(entity)
}
pub fn bump_geometry(&mut self) {
self.app.tabs[self.tab].scene.bump_geometry();
}
pub fn push_undo(&mut self, label: &str) {
self.app.push_undo_snapshot(self.tab, label);
}
pub fn set_dirty(&mut self) {
self.app.tabs[self.tab].dirty = true;
}
pub fn push_info(&mut self, msg: &str) {
self.app.command_line.push_info(msg);
}
pub fn push_output(&mut self, msg: &str) {
self.app.command_line.push_output(msg);
}
pub fn push_error(&mut self, msg: &str) {
self.app.command_line.push_error(msg);
}
pub fn set_active_command(&mut self, cmd: Box<dyn CadCommand>) {
self.app.tabs[self.tab].active_cmd = Some(cmd);
}
pub fn plugin_state<T: Any + Send + Sync + 'static>(
&self,
plugin_id: &'static str,
) -> Option<&T> {
self.app.tabs[self.tab].plugin_state(plugin_id, TypeId::of::<T>())
}
pub fn plugin_state_mut<T: Any + Send + Sync + 'static>(
&mut self,
plugin_id: &'static str,
) -> Option<&mut T> {
self.app.tabs[self.tab].plugin_state_mut(plugin_id, TypeId::of::<T>())
}
pub fn ensure_plugin_state<T: Any + Send + Sync + 'static>(
&mut self,
plugin_id: &'static str,
init: impl FnOnce() -> T,
) -> &mut T {
self.app.tabs[self.tab].ensure_plugin_state(plugin_id, init)
}
}

View file

@ -2386,6 +2386,11 @@ impl OpenCADStudio {
.as_ref()
.map(|c| c.needs_entity_pick())
.unwrap_or(false);
let needs_structure = self.tabs[i]
.active_cmd
.as_ref()
.map(|c| c.needs_structure_point_pick())
.unwrap_or(false);
let is_gathering = self.tabs[i]
.active_cmd
.as_ref()
@ -2396,7 +2401,7 @@ impl OpenCADStudio {
.as_ref()
.map(|c| c.needs_tangent_pick())
.unwrap_or(false);
self.tabs[i].snap_result = if needs_entity || is_gathering {
self.tabs[i].snap_result = if needs_entity || is_gathering || needs_structure {
None
} else if needs_tan {
self.snapper.snap_tangent_only(
@ -2468,6 +2473,39 @@ impl OpenCADStudio {
};
self.tabs[i].last_cursor_world = effective;
self.tabs[i].last_cursor_screen = p_full;
// C3D-style orange object snap (plugin commands implement resolve_object_pick).
if needs_structure {
use crate::snap::{SnapResult, SnapType};
let pick = self.tabs[i].active_cmd.as_ref().and_then(|c| {
c.resolve_object_pick(
&self.tabs[i].scene,
effective.x as f64,
effective.y as f64,
)
});
if let Some(pick) = pick {
let world =
glam::Vec3::new(pick.x as f32, pick.y as f32, effective.z);
let ndc = view_proj.project_point3(world);
let screen = iced::Point::new(
(ndc.x + 1.0) * 0.5 * bounds.width,
(1.0 - ndc.y) * 0.5 * bounds.height,
);
self.tabs[i].snap_result = Some(SnapResult {
world,
screen,
snap_type: SnapType::ObjectPick,
tangent_obj: None,
});
if let Some(cmd) = self.tabs[i].active_cmd.as_mut() {
cmd.set_acquisition_hint(Some(pick.label));
}
} else if let Some(cmd) = self.tabs[i].active_cmd.as_mut() {
cmd.set_acquisition_hint(None);
}
}
// Snap glyph is positioned in canvas space; shift the
// tile-local snap screen point back to the full canvas.
if let Some(s) = self.tabs[i].snap_result.as_mut() {
@ -2475,16 +2513,42 @@ impl OpenCADStudio {
s.screen.y += tile_b.y;
}
let mut previews = if needs_entity {
let mut previews = if needs_structure {
let mut p = self.tabs[i]
.active_cmd
.as_ref()
.map(|c| c.object_pick_hover_previews(&self.tabs[i].scene, effective))
.unwrap_or_default();
if let Some(cmd) = self.tabs[i].active_cmd.as_mut() {
p.extend(cmd.on_preview_wires(effective));
}
p
} else if needs_entity {
let hover_handle =
scene::hit_test::click_hit(p, &all_wires[..], view_proj, bounds)
.and_then(|s| Scene::handle_from_wire_name(s))
.unwrap_or(acadrust::Handle::NULL);
self.tabs[i]
let mut p = self.tabs[i]
.active_cmd
.as_mut()
.map(|c| c.on_hover_entity(hover_handle, effective))
.unwrap_or_default()
.unwrap_or_default();
if !hover_handle.is_null() {
if let Some(cmd) = self.tabs[i].active_cmd.as_ref() {
p.extend(cmd.entity_pick_acquire_previews(
&self.tabs[i].scene,
hover_handle,
));
}
if let Some(cmd) = self.tabs[i].active_cmd.as_mut() {
if let Some(hint) =
cmd.entity_pick_acquire_hint(hover_handle)
{
cmd.set_acquisition_hint(Some(hint));
}
}
}
p
} else {
self.tabs[i]
.active_cmd
@ -2946,6 +3010,36 @@ impl OpenCADStudio {
};
let result = if self.tabs[i]
.active_cmd
.as_ref()
.map(|c| c.needs_structure_point_pick())
.unwrap_or(false)
{
let pick = self.tabs[i].active_cmd.as_ref().and_then(|c| {
c.resolve_object_pick(
&self.tabs[i].scene,
world_pt.x as f64,
world_pt.y as f64,
)
});
if let Some(pick) = pick {
let center = glam::Vec3::new(pick.x as f32, pick.y as f32, world_pt.z);
let result = self.tabs[i].active_cmd.as_mut().map(|c| {
c.on_structure_pick(pick.handle, center)
});
self.command_line
.push_info(&format!("{} acquired.", pick.label));
result
} else {
let msg = self.tabs[i]
.active_cmd
.as_ref()
.map(|c| c.object_pick_miss_message())
.unwrap_or("No object near click.");
self.command_line.push_error(msg);
None
}
} else if self.tabs[i]
.active_cmd
.as_ref()
.map(|c| c.needs_entity_pick())
@ -2956,6 +3050,23 @@ impl OpenCADStudio {
let hit = scene::hit_test::click_hit(p, &all_wires2[..], vp_mat2, bounds)
.and_then(|s| Scene::handle_from_wire_name(s));
if let Some(handle) = hit {
// Some commands (e.g. SS_CATCHMENT) need the entity
// body before `on_entity_pick` can advance.
let inject_first = self.tabs[i]
.active_cmd
.as_ref()
.map(|c| c.inject_before_entity_pick())
.unwrap_or(false);
if inject_first {
if let Some(entity) =
self.tabs[i].scene.document.get_entity(handle).cloned()
{
if let Some(cmd) = self.tabs[i].active_cmd.as_mut() {
cmd.inject_picked_entity(entity);
}
}
}
let result = self.tabs[i]
.active_cmd
.as_mut()