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_locatorandstatic_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§
- Name
Table - 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, asstd::any::type_namewrites it without its generic arguments (my_app::view_models::WidgetViewModel). - Runtime
Type - The run-time type of a view model: the
TypeIdthe generated tables are keyed by and the path of the type (Typeof the generated source, withFullName). - Type
Entry - An entry of a
TypeTable: the type, as the function that gives itsTypeId(TypeId::of::<T>), and the value of the type. - Type
Table - 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§
- View
Resolution Mode - The fallback precedence the generated adapters are compiled with
(
generated_adapter_resolution_modeofstatic_view_locator).
Functions§
- set_
data_ context - Sets
dataas the data context ofcontrol(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
buildfor 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()toControl). - 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§
- Missing
View Table - 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). - View
Factory - Creates the view of a mapping (
Func<Control>ofs_views). - View
Table - 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.