Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable MapProvider variant when not available #8006

Merged
merged 4 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,11 @@ enum MapProvider: ubyte (
OpenStreetMap (default),

/// Mapbox Streets is a minimalistic map designed by Mapbox.
///
/// **Note**: Requires a Mapbox access token in the settings or using the `RERUN_MAPBOX_ACCESS_TOKEN` environment variable.
MapboxStreets,

/// Mapbox Dark is a dark-themed map designed by Mapbox.
///
/// **Note**: Requires a Mapbox access token in the settings or using the `RERUN_MAPBOX_ACCESS_TOKEN` environment variable.
MapboxDark,

/// Mapbox Satellite is a satellite map designed by Mapbox.
///
/// **Note**: Requires a Mapbox access token in the settings or using the `RERUN_MAPBOX_ACCESS_TOKEN` environment variable.
MapboxSatellite,
}
18 changes: 3 additions & 15 deletions crates/store/re_types/src/blueprint/components/map_provider.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

97 changes: 88 additions & 9 deletions crates/viewer/re_component_ui/src/datatype_uis/enum_combobox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,70 @@

use crate::response_utils::response_with_changes_of_inner;

/// Is a particular variant of an enum available for selection? If not, why?
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum VariantAvailable {
/// The variant is available
Yes,

/// The variant is not available.
No {
/// The reason why the variant is not available, markdown formated.

Check warning on line 14 in crates/viewer/re_component_ui/src/datatype_uis/enum_combobox.rs

View workflow job for this annotation

GitHub Actions / Checks / Spell Check

"formated" should be "formatted".
reason_markdown: String,
},
}

/// Trait for a type that can provide information about whether a particular variant of an enum is
/// available for selection.
pub trait VariantAvailableProvider<EnumT: re_types_core::reflection::Enum> {
fn is_variant_enabled(ctx: &ViewerContext<'_>, variant: EnumT) -> VariantAvailable;
}

/// A variant available provider that makes all variants available.
struct AllEnumVariantAvailable<EnumT: re_types_core::reflection::Enum> {
_phantom: std::marker::PhantomData<EnumT>,
}

impl<EnumT: re_types_core::reflection::Enum> VariantAvailableProvider<EnumT>
for AllEnumVariantAvailable<EnumT>
{
fn is_variant_enabled(_ctx: &ViewerContext<'_>, _variant: EnumT) -> VariantAvailable {
VariantAvailable::Yes
}
}

/// Edit or view an enum value using a combobox. All variants are available.
pub fn edit_view_enum<EnumT: re_types_core::reflection::Enum + re_types_core::Component>(
_ctx: &ViewerContext<'_>,
ctx: &ViewerContext<'_>,
ui: &mut egui::Ui,
current_value: &mut MaybeMutRef<'_, EnumT>,
) -> egui::Response {
edit_view_enum_with_variant_available::<EnumT, AllEnumVariantAvailable<EnumT>>(
ctx,
ui,
current_value,
)
}

/// Edit or view an enum value using a combobox. The availability of each variant is determined by
/// the provided `VariantAvailableProvider` type.
pub fn edit_view_enum_with_variant_available<
EnumT: re_types_core::reflection::Enum + re_types_core::Component,
VariantAvailT: VariantAvailableProvider<EnumT>,
>(
ctx: &ViewerContext<'_>,
ui: &mut egui::Ui,
current_value: &mut MaybeMutRef<'_, EnumT>,
) -> egui::Response {
let id_salt = EnumT::name().full_name();
edit_view_enum_impl(ui, id_salt, current_value)
edit_view_enum_impl::<_, VariantAvailT>(ctx, ui, id_salt, current_value)
}

fn edit_view_enum_impl<EnumT: re_types_core::reflection::Enum>(
fn edit_view_enum_impl<
EnumT: re_types_core::reflection::Enum,
VariantAvailT: VariantAvailableProvider<EnumT>,
>(
ctx: &ViewerContext<'_>,
ui: &mut egui::Ui,
id_salt: &str,
current_value: &mut MaybeMutRef<'_, EnumT>,
Expand All @@ -32,9 +86,19 @@
return ui.label("<no variants>");
};

let mut response = variant_ui(ui, current_value, first);
let mut response = crate::datatype_uis::enum_combobox::variant_ui(
ui,
current_value,
first,
VariantAvailT::is_variant_enabled(ctx, first),
);
for variant in iter {
response |= variant_ui(ui, current_value, variant);
response |= variant_ui(
ui,
current_value,
variant,
VariantAvailT::is_variant_enabled(ctx, variant),
);
}
response
});
Expand All @@ -53,9 +117,24 @@
ui: &mut egui::Ui,
current_value: &mut EnumT,
variant: EnumT,
variant_available: VariantAvailable,
) -> egui::Response {
ui.selectable_value(current_value, variant, variant.to_string())
.on_hover_ui(|ui| {
ui.markdown_ui(variant.docstring_md());
})
match variant_available {
VariantAvailable::Yes => ui
.selectable_value(current_value, variant, variant.to_string())
.on_hover_ui(|ui| {
ui.markdown_ui(variant.docstring_md());
}),
VariantAvailable::No {
reason_markdown: reason,
} => {
ui.add_enabled_ui(false, |ui| {
ui.selectable_value(current_value, variant, variant.to_string())
.on_disabled_hover_ui(|ui| {
ui.markdown_ui(&format!("{}\n\n{}", variant.docstring_md(), reason));
})
})
.inner
}
}
}
5 changes: 4 additions & 1 deletion crates/viewer/re_component_ui/src/datatype_uis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ mod vec;
mod view_id;

pub use bool_toggle::edit_bool;
pub use enum_combobox::edit_view_enum;
pub use enum_combobox::{
edit_view_enum, edit_view_enum_with_variant_available, VariantAvailable,
VariantAvailableProvider,
};
pub use float_drag::{
edit_f32_min_to_max_float, edit_f32_zero_to_max, edit_f32_zero_to_one,
edit_f64_float_raw_with_speed_impl,
Expand Down
9 changes: 8 additions & 1 deletion crates/viewer/re_component_ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod entity_path;
mod fallback_ui;
mod image_format;
mod line_strip;
mod map_provider;
mod marker_shape;
mod pinhole;
mod radius;
Expand All @@ -27,6 +28,7 @@ use datatype_uis::{
edit_view_enum, edit_view_range1d, view_view_id,
};

use crate::datatype_uis::edit_view_enum_with_variant_available;
abey79 marked this conversation as resolved.
Show resolved Hide resolved
use re_types::{
blueprint::components::{
BackgroundKind, Corner2D, LockRangeDuringZoom, MapProvider, ViewFit, Visible, ZoomLevel,
Expand Down Expand Up @@ -94,7 +96,12 @@ pub fn create_component_ui_registry() -> re_viewer_context::ComponentUiRegistry
registry.add_singleline_edit_or_view::<Corner2D>(edit_view_enum);
registry.add_singleline_edit_or_view::<FillMode>(edit_view_enum);
registry.add_singleline_edit_or_view::<MagnificationFilter>(edit_view_enum);
registry.add_singleline_edit_or_view::<MapProvider>(edit_view_enum);
registry.add_singleline_edit_or_view::<MapProvider>(
edit_view_enum_with_variant_available::<
MapProvider,
crate::map_provider::MapProviderVariantAvailable,
>,
);
registry.add_singleline_edit_or_view::<TransformRelation>(edit_view_enum);
registry.add_singleline_edit_or_view::<ViewFit>(edit_view_enum);

Expand Down
32 changes: 32 additions & 0 deletions crates/viewer/re_component_ui/src/map_provider.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use re_types::blueprint::components::MapProvider;
use re_viewer_context::ViewerContext;

use crate::datatype_uis::{VariantAvailable, VariantAvailableProvider};

pub struct MapProviderVariantAvailable;

impl VariantAvailableProvider<MapProvider> for MapProviderVariantAvailable {
fn is_variant_enabled(ctx: &ViewerContext<'_>, variant: MapProvider) -> VariantAvailable {
let map_box_available = if ctx
.app_options
.mapbox_access_token()
.is_some_and(|token| !token.is_empty())
{
VariantAvailable::Yes
} else {
VariantAvailable::No {
reason_markdown: "A Mapbox access token is not available. You can set it in the \
settings or using the `RERUN_MAPBOX_ACCESS_TOKEN` environment variable."
.to_owned(),
}
};

match variant {
MapProvider::OpenStreetMap => VariantAvailable::Yes,

MapProvider::MapboxStreets | MapProvider::MapboxDark | MapProvider::MapboxSatellite => {
map_box_available
}
}
}
}
6 changes: 0 additions & 6 deletions rerun_cpp/src/rerun/blueprint/components/map_provider.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 3 additions & 15 deletions rerun_py/rerun_sdk/rerun/blueprint/components/map_provider.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading