Skip to main content

Crate ferroui_static_view_locator

Crate ferroui_static_view_locator 

Source
Expand description

ferroui-static-view-locator

View locators generated at build time: a locator maps the type of a view model to the view that shows it, through a table written by the build script of the crate, with no lookup of a type by its name and no scan of the types of the program at run time. A port of StaticViewLocator (https://github.com/wieslawsoltes/StaticViewLocator), a C# source generator; the plan and the deviations are in docs/porting/static-view-locator.md.

Three crates make the port:

  • this crate: the attributes, the tables a generated locator holds (ViewTable, MissingViewTable), the run-time type of a view model (RuntimeType) and what the generated adapters call;
  • ferroui-static-view-locator-macros: static_view_locator and static_view_mapping, re-exported here;
  • ferroui-static-view-locator-build: the generator, which the build script of the crate runs (the counterpart of the incremental generator of the reference).

§Usage

[dependencies]
ferroui-static-view-locator = "0.4"

[build-dependencies]
ferroui-static-view-locator-build = "0.4"
ⓘ
// build.rs
fn main() {
    ferroui_static_view_locator_build::StaticViewLocatorBuild::from_env().run();
}
ⓘ
use ferroui_static_view_locator::{static_view_locator, static_view_mapping};

#[static_view_locator(generate_data_template = true, data_template_match_types = [ViewModelBase])]
#[static_view_mapping(LoginViewModel, LogInView)]
#[derive(Default)]
pub struct ViewLocator;

The attribute writes the struct and includes the file the build script generated for it: the tables and the members the options ask for, in an impl block of the struct (the partial class of the reference). By convention crate::view_models::SettingsViewModel is shown by crate::views::SettingsView, and crate::settings_view_model::SettingsViewModel by crate::settings_view::SettingsView; a view is a class of the object model that derives from UserControl or Window and has a constructor without parameters (new()).

Structs§

NameTable
A table of values by the path of a generic type definition (type.GetGenericTypeDefinition() as the key of the legacy fallback): the path of the declaration of the type, as std::any::type_name writes it without its generic arguments (my_app::view_models::WidgetViewModel).
RuntimeType
The run-time type of a view model: the TypeId the generated tables are keyed by and the path of the type (Type of the generated source, with FullName).
TypeEntry
An entry of a TypeTable: the type, as the function that gives its TypeId (TypeId::of::<T>), and the value of the type.
TypeTable
A table of values by type (Dictionary<Type, V> of the generated source), declared as a static of constant entries and indexed on its first lookup. Of two entries of one type the first is the value.

Enums§

ViewResolutionMode
The fallback precedence the generated adapters are compiled with (generated_adapter_resolution_mode of static_view_locator).

Functions§

set_data_context
Sets data as the data context of control (control.DataContext = data).
text_view
A text block that shows text (new TextBlock { Text = text }): the view of a missing view and of an invalid view model.
unable_to_create_view
The failure of the legacy build for a view model of a type without a view or a missing-view entry (throw new Exception($"Unable to create view for type: {type}")).
upcast
A view as a control (the conversion of new View() to Control).
view_not_found_text
The text of a view model type whose view is not found and that has no entry in the table of missing views ($"View for {viewModelType.FullName} is not found.").

Type Aliases§

MissingViewTable
The messages of the view model types whose view is not found (Dictionary<Type, string> s_missingViews): "Not Found: crate::views::MainWindowView".
TypeSet
A set of types (the resolved view model types a generated match_ accepts).
ViewFactory
Creates the view of a mapping (Func<Control> of s_views).
ViewTable
The views of the view model types (Dictionary<Type, Func<Control>> s_views).

Attribute Macros§

static_view_locator
Makes a struct a view locator generated at build time ([StaticViewLocator]).
static_view_mapping
Maps a view model type to a view type on a locator ([StaticViewMapping(typeof(VM), typeof(V))]): the final override of the mapping of the view model, for types that do not follow the naming rules. Repeatable; the types are named as in the module of the locator.