Skip to main content

sim_lib_view/
universal.rs

1//! Registration of the universal default view and editor.
2//!
3//! Both are registered as the lowest-quality, always-matching lens of their
4//! kind, so dispatch ends here when nothing specialized claims a value.
5
6use std::sync::Arc;
7
8use sim_kernel::Symbol;
9use sim_shape::{AnyShape, shape_value};
10
11use crate::codec::{PairCodec, universal_surface_codec_symbol};
12use crate::contract::{Lens, LensKind, LensMeta};
13use crate::dispatch::LensRegistry;
14use crate::universal_editor::UniversalEditor;
15use crate::universal_view::UniversalView;
16
17/// The universal default view lens id.
18pub const UNIVERSAL_VIEW_ID: &str = "view:default";
19
20/// The universal default editor lens id.
21pub const UNIVERSAL_EDITOR_ID: &str = "edit:default";
22
23/// The lowest quality, so the universal default loses every ranked tie.
24const LOWEST_QUALITY: i32 = -1_000_000;
25
26fn any_shape() -> sim_kernel::Value {
27    shape_value(Symbol::qualified("core", "Any"), Arc::new(AnyShape))
28}
29
30/// Register the universal default view and editor into `registry`. When
31/// `readonly` is set, the universal editor renders but never commits.
32pub fn register_universal_default(registry: &mut LensRegistry, readonly: bool) {
33    let view = Arc::new(UniversalView);
34    registry.register(Lens::view(
35        LensMeta::new(Symbol::new(UNIVERSAL_VIEW_ID), LensKind::View)
36            .claiming_shape(any_shape())
37            .with_quality_cost(LOWEST_QUALITY, 0)
38            .as_universal_default(),
39        view.clone(),
40    ));
41    let editor = if readonly {
42        UniversalEditor::readonly()
43    } else {
44        UniversalEditor::writable()
45    };
46    let editor = Arc::new(editor);
47    registry.register(Lens::editor(
48        LensMeta::new(Symbol::new(UNIVERSAL_EDITOR_ID), LensKind::Editor)
49            .claiming_shape(any_shape())
50            .with_quality_cost(LOWEST_QUALITY, 0)
51            .as_universal_default(),
52        editor.clone(),
53    ));
54    registry.register_surface_codec(
55        universal_surface_codec_symbol(),
56        Arc::new(PairCodec::new(view, editor)),
57    );
58}