1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
use re_types_core::{
    reflection::ArchetypeFieldReflection, Archetype, ArchetypeReflectionMarker, ComponentName,
};
use re_ui::{list_item, UiExt as _};
use re_viewer_context::{
    ComponentFallbackProvider, ComponentUiTypes, QueryContext, ViewId, ViewState, ViewerContext,
};
use re_viewport_blueprint::ViewProperty;

/// Display the UI for editing all components of a blueprint archetype.
///
/// Note that this will show default values for components that are null.
pub fn view_property_ui<A: Archetype + ArchetypeReflectionMarker>(
    ctx: &ViewerContext<'_>,
    ui: &mut egui::Ui,
    view_id: ViewId,
    fallback_provider: &dyn ComponentFallbackProvider,
    view_state: &dyn ViewState,
) {
    let view_property =
        ViewProperty::from_archetype::<A>(ctx.blueprint_db(), ctx.blueprint_query, view_id);
    view_property_ui_impl(ctx, ui, &view_property, fallback_provider, view_state);
}

fn view_property_ui_impl(
    ctx: &ViewerContext<'_>,
    ui: &mut egui::Ui,
    property: &ViewProperty,
    fallback_provider: &dyn ComponentFallbackProvider,
    view_state: &dyn ViewState,
) {
    let Some(reflection) = ctx.reflection.archetypes.get(&property.archetype_name) else {
        // The `ArchetypeReflectionMarker` bound should make this impossible.
        re_log::warn_once!(
            "Missing reflection data for archetype {:?}.",
            property.archetype_name
        );
        return;
    };

    let query_ctx = property.query_context(ctx, view_state);
    // If the property archetype only has a single component, don't show an additional hierarchy level!
    if reflection.fields.len() == 1 {
        let field = &reflection.fields[0];

        view_property_component_ui(
            &query_ctx,
            ui,
            property,
            reflection.display_name,
            field,
            fallback_provider,
        );
    } else {
        let sub_prop_ui = |ui: &mut egui::Ui| {
            for field in &reflection.fields {
                view_property_component_ui(
                    &query_ctx,
                    ui,
                    property,
                    field.display_name,
                    field,
                    fallback_provider,
                );
            }
        };

        ui.list_item()
            .interactive(false)
            .show_hierarchical_with_children(
                ui,
                ui.make_persistent_id(property.archetype_name.full_name()),
                true,
                list_item::LabelContent::new(reflection.display_name),
                sub_prop_ui,
            );
    }
}

/// Draw view property ui for a single component of a view property archetype.
///
/// Use [`view_property_ui`] whenever possible to show the ui for all components of a view property archetype.
/// This function is only useful if you want to show custom ui for some of the components.
pub fn view_property_component_ui(
    ctx: &QueryContext<'_>,
    ui: &mut egui::Ui,
    property: &ViewProperty,
    display_name: &str,
    field: &ArchetypeFieldReflection,
    fallback_provider: &dyn ComponentFallbackProvider,
) {
    let component_array = property.component_raw(field.component_name);
    let row_id = property.component_row_id(field.component_name);

    let ui_types = ctx
        .viewer_ctx
        .component_ui_registry
        .registered_ui_types(field.component_name);

    let singleline_ui: &dyn Fn(&mut egui::Ui) = &|ui| {
        ctx.viewer_ctx.component_ui_registry.singleline_edit_ui(
            ctx,
            ui,
            ctx.viewer_ctx.blueprint_db(),
            ctx.target_entity_path,
            field.component_name,
            row_id,
            component_array.as_deref(),
            fallback_provider,
        );
    };

    let multiline_ui: &dyn Fn(&mut egui::Ui) = &|ui| {
        ctx.viewer_ctx.component_ui_registry.multiline_edit_ui(
            ctx,
            ui,
            ctx.viewer_ctx.blueprint_db(),
            ctx.target_entity_path,
            field.component_name,
            row_id,
            component_array.as_deref(),
            fallback_provider,
        );
    };
    // Do this as a separate step to avoid borrowing issues.
    let multiline_ui_ref: Option<&dyn Fn(&mut egui::Ui)> =
        if ui_types.contains(ComponentUiTypes::MultiLineEditor) {
            Some(multiline_ui)
        } else {
            None
        };

    view_property_component_ui_custom(
        ctx,
        ui,
        property,
        display_name,
        field,
        singleline_ui,
        multiline_ui_ref,
    );
}

/// Draw view property ui for a single component of a view property archetype with custom ui for singleline & multiline.
///
/// Use [`view_property_ui`] whenever possible to show the ui for all components of a view property archetype.
/// This function is only useful if you want to show custom ui for some of the components.
pub fn view_property_component_ui_custom(
    ctx: &QueryContext<'_>,
    ui: &mut egui::Ui,
    property: &ViewProperty,
    display_name: &str,
    field: &ArchetypeFieldReflection,
    singleline_ui: &dyn Fn(&mut egui::Ui),
    multiline_ui: Option<&dyn Fn(&mut egui::Ui)>,
) {
    let singleline_list_item_content = list_item::PropertyContent::new(display_name)
        .menu_button(&re_ui::icons::MORE, |ui| {
            menu_more(ctx.viewer_ctx, ui, property, field.component_name);
        })
        .value_fn(move |ui, _| singleline_ui(ui));

    let list_item_response = if let Some(multiline_ui) = multiline_ui {
        let default_open = false;
        let id = egui::Id::new((ctx.target_entity_path.hash(), field.component_name));
        ui.list_item()
            .interactive(false)
            .show_hierarchical_with_children(
                ui,
                id,
                default_open,
                singleline_list_item_content,
                |ui| {
                    multiline_ui(ui);
                },
            )
            .item_response
    } else {
        ui.list_item()
            .interactive(false)
            // It might have siblings that have a hierarchy.
            .show_hierarchical(ui, singleline_list_item_content)
    };

    list_item_response.on_hover_ui(|ui| {
        ui.markdown_ui(field.docstring_md);
    });
}

fn menu_more(
    ctx: &ViewerContext<'_>,
    ui: &mut egui::Ui,
    property: &ViewProperty,
    component_name: ComponentName,
) {
    let component_array = property.component_raw(component_name);

    let property_differs_from_default = component_array
        != ctx.raw_latest_at_in_default_blueprint(&property.blueprint_store_path, component_name);

    let response = ui
        .add_enabled(
            property_differs_from_default,
            egui::Button::new("Reset to default blueprint"),
        )
        .on_hover_text(
"Resets this property to the value in the default blueprint.
If no default blueprint was set or it didn't set any value for this field, this is the same as resetting to empty."
        )
        .on_disabled_hover_text(
            "The property is already set to the same value it has in the default blueprint",
        );
    if response.clicked() {
        ctx.reset_blueprint_component_by_name(&property.blueprint_store_path, component_name);
        ui.close_menu();
    }

    let response = ui
        .add_enabled(
            component_array.is_some(),
            egui::Button::new("Unset"),
        )
        .on_hover_text(
"Resets this property to an unset value, meaning that a heuristically determined value will be used instead.
This has the same effect as not setting the value in the blueprint at all."
        )
        .on_disabled_hover_text("The property is already unset.");
    if response.clicked() {
        ctx.clear_blueprint_component_by_name(&property.blueprint_store_path, component_name);
        ui.close_menu();
    }

    // TODO(andreas): The next logical thing here is now to save it to the default blueprint!
    // This should be fairly straight forward except that we need to make sure that a default blueprint exists in the first place.
}