dependency-object

    Dependency objects and dependency properties for MoonBit, with the behavior of WPF

    dependency-property
    dependency-object
    ui
    xaml
    Download zip
    Author
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    17 hours ago
    Downloads
    15

    No dependencies

    #moonrockz/dependency-object

    Dependency objects for MoonBit: objects whose property values the library stores and resolves, with default values, coercion, change notification and value inheritance, as DependencyObject in WPF, BindableObject in .NET MAUI and AvaloniaObject in Avalonia. Elements also have routed events, and property paths such as Child.Width read, set and observe values, both as in WPF.

    Add it with moon add moonrockz/dependency-object. While the version is 0.x, a minor release can have breaking changes.

    You can register types and properties, set, coerce and observe values, build trees of elements, inherit values in them, raise events that go through them and follow property paths. The specifications are in openspec/specs/ of the repository.

    PackagePurpose
    moonrockz/dependency-objectTypes, properties, objects and their values, and version()
    moonrockz/dependency-object/eventsRouted events of these types
    moonrockz/dependency-object/pathProperty paths, as WPF's PropertyPath

    The module does not depend on moonrockz/xaml. You can use it without XAML. It targets wasm, wasm-gc, js and native.

    #Dependency objects

    The package moonrockz/dependency-object.

    Dependency objects and dependency properties with the behavior of WPF. @dependency in the examples is this package: write "moonrockz/dependency-object" @dependency in your moon.pkg, because the name has a hyphen.

    #A control with two properties

    Value is never above Maximum. The coercion of Value sees to that when Value is set, and a function of the metadata when Maximum changes. The two properties use each other, so Maximum gets its metadata after both are registered.

    ///|
    let slider_type : @dependency.OwnerType = try! @dependency.register_owner_type(
    "Slider",
    )

    ///|
    let slider_maximum : @dependency.DependencyProperty[Double] = try! @dependency.register(
    "Maximum",
    slider_type,
    @dependency.double_key,
    default_value=100.0,
    )

    ///|
    let slider_value : @dependency.DependencyProperty[Double] = try! @dependency.register(
    "Value",
    slider_type,
    @dependency.double_key,
    default_value=0.0,
    validate=value => !value.is_nan(),
    metadata=@dependency.PropertyMetadata::new(coerce=(object, value) => {
    value.min(object.get_value(slider_maximum))
    }),
    )

    ///|
    fn init {
    try! slider_maximum.override_metadata(
    slider_type,
    @dependency.PropertyMetadata::new(changed=(object, _, _, _) => {
    object.coerce_value(slider_value)
    }),
    )
    }

    ///|
    test "a slider" {
    let slider = @dependency.DependencyObject::new(slider_type)
    let changes = Ref(0)
    ignore(slider.add_change_handler((_, _, _, _) => changes.val += 1))
    slider.set_value(slider_value, 80.0)
    slider.set_value(slider_maximum, 50.0)
    assert_eq(slider.get_value(slider_value), 50.0)
    // The value that was set is still there.
    slider.set_value(slider_maximum, 100.0)
    assert_eq(slider.get_value(slider_value), 80.0)
    slider.clear_value(slider_value)
    assert_eq(slider.get_value(slider_value), 0.0)
    // The handler also ran for the two changes of `Value` by its coercion.
    assert_eq(changes.val, 6)
    }

    #Register types and properties

    MoonBit has no reflection, so a program registers its types of objects, and each property with the key of its value type and its default value.

    ///|
    test "register and look up" {
    let control = @dependency.register_owner_type("Control", element=true)
    let button = @dependency.register_owner_type("Button", base=control)
    let padding = @dependency.register(
    "Padding",
    control,
    @dependency.double_key,
    default_value=0.0,
    validate=value => value >= 0.0,
    )
    guard @dependency.lookup_property(button, "Padding") is Some(found) else {
    fail("no such property")
    }
    assert_eq(found, padding.info())
    inspect(found, content="Control.Padding")
    }

    FunctionWhat it registers
    register_owner_typea type of objects, with the type that it derives from
    registera property of a type
    register_attacheda property that a type defines for objects of every type, such as Grid.Row
    register_read_only, register_attached_read_onlya property that only code with the returned DependencyPropertyKey can change
    DependencyProperty::add_owneran existing property under its name on another type

    default_value is required, because MoonBit has no default value for every type. validate accepts or rejects values, and the default value too. Two types can use one name for two properties, also when one derives from the other. lookup_property finds the property of the nearest type. A failure is a DependencyError, whose text names the property as Owner.Name.

    Every property of a registry has a value on every object of the registry. The owner type decides only where the property is found by its name, and which objects use its metadata.

    #Metadata

    PropertyMetadata says how a property behaves on the objects of a type:

    ArgumentWhat it isWhen an override does not have it
    default_valuethe value of objects on which nothing is setthat of the base type
    coercecomputes the effective valuethat of the base type
    changedruns after a changethat of the base type, which also runs, and first, when there is one
    inheritswhether elements use the value of their ancestors, see Inherited valuesthat of the base type
    logical_contentthe element in a value, see The logical treethat of the base type

    The metadata of register is for objects of the owner type and of the types that derive from it. The metadata of an attached property is for objects of every type. override_metadata gives a property metadata for one more type, and so does add_owner. An object uses the metadata of its type, or else of the nearest base type that has some.

    ///|
    let dial_type : @dependency.OwnerType = try! @dependency.register_owner_type(
    "Dial",
    base=slider_type,
    )

    ///|
    test "a type with other metadata" {
    slider_maximum.override_metadata(
    dial_type,
    @dependency.PropertyMetadata::new(default_value=360.0),
    )
    let dial = @dependency.DependencyObject::new(dial_type)
    dial.set_value(slider_value, 270.0)
    assert_eq(dial.get_value(slider_value), 270.0)
    // `Maximum` still coerces `Value`, because the override has the `changed` of the slider.
    dial.set_value(slider_maximum, 180.0)
    assert_eq(dial.get_value(slider_value), 180.0)
    assert_eq(slider_maximum.get_metadata(slider_type).default_value(), 100.0)
    }

    • A type can have metadata for a property once. The owner type has it when register got some, and always when a property is read-only and not attached. Both are so in WPF.
    • An override takes what it does not have from the metadata that the base type has at that moment. So override the base type first. A later override of the base type does not change the type, as in WPF.
    • Override before there are objects of the type. A later override is for values that objects have not stored, and nothing is notified. WPF says that the result is undefined, so do not depend on it.
    • The default value of an override is validated.
    • A read-only property needs its key: override_metadata(owner, metadata, key~). add_owner cannot give it metadata.
    • get_metadata gives what is in force for a type.

    #Values

    FunctionWhat it does
    get_valuegives the effective value
    set_valuesets the local value
    set_current_valuechanges the effective value and not the local value
    clear_valueremoves the local value and the current value
    coerce_valuecoerces the value again
    read_local_valuegives the current value or the local value, or None
    local_valuesgives all properties that have such a value
    set_with_key, clear_with_keychange a read-only property

    These are methods of DependencyObject with a DependencyProperty[T]. PropertyInfo has all but local_values too, with AnyValues, for code that does not know the type. Both fail in the same cases.

    The effective value is the result of three steps:

    1. The base value is the local value, or else the inherited value, or else the default value for the type of the object.
    2. The current value replaces it when there is one. A control uses a current value for its own changes, such as a check box that is clicked, because the next set_value or clear_value discards it. A current value that equals the base value is none.
    3. The coercion changes it. get_value gives what the coercion gave when the value was last set, cleared or coerced.

    ///|
    test "a current value" {
    let slider = @dependency.DependencyObject::new(slider_type)
    slider.set_value(slider_value, 10.0)
    slider.set_current_value(slider_value, 20.0)
    assert_eq(slider.get_value(slider_value), 20.0)
    assert_eq(slider.read_local_value(slider_value), Some(20.0))
    // The coercion gives another value than 200, so 200 is not kept, and the next coercion starts with the local value.
    slider.set_current_value(slider_value, 200.0)
    assert_eq(slider.get_value(slider_value), 100.0)
    assert_eq(slider.read_local_value(slider_value), Some(10.0))
    slider.coerce_value(slider_value)
    assert_eq(slider.get_value(slider_value), 10.0)
    }

    • validate checks each value that is set, also a current value that coerce_value sets again. It does not check the result of a coercion. A value that it rejects changes nothing.
    • A default value is not coerced when it is read or when clear_value brings it back. coerce_value coerces it, and the result stays until the next operation. An inherited value is always coerced.

    #Changes

    A change is notified when the old and the new effective value are not equal by the equal of the property, or else by its key. An operation does this:

    1. It validates the value.
    2. It takes the old value.
    3. It coerces the new value.
    4. It stores the new value.
    5. It compares the two. When they are equal, it is done.
    6. It runs the changed functions of the metadata, that of the base type first.
    7. It runs the handlers of the object, in the order in which they were added.

    ///|
    test "handlers" {
    let slider = @dependency.DependencyObject::new(slider_type)
    let changes = []
    let token = slider.add_change_handler((_, property, old, new) => {
    guard @dependency.double_key.unwrap(old) is Some(old) &&
    @dependency.double_key.unwrap(new) is Some(new) else {
    return
    }
    changes.push("\{property}: \{old} -> \{new}")
    })
    slider.set_value(slider_value, 80.0)
    slider.set_value(slider_maximum, 50.0)
    assert_true(slider.remove_change_handler(token))
    slider.set_value(slider_maximum, 20.0)
    debug_inspect(
    changes,
    content=(
    #|[
    #| "Slider.Value: 0 -> 80",
    #| "Slider.Value: 80 -> 50",
    #| "Slider.Maximum: 100 -> 50",
    #|]
    ),
    )
    }

    A change that a function or a handler makes runs at once and completely. So the handler above hears of Value before Maximum, because the changed function of Maximum coerces Value.

    A handler that a handler adds or removes is added or removed from the next change on. One that changed adds or removes already is for this change.

    An error of the coercion stops the operation before it stores the value. An error of changed or of a handler stops the rest of the notification, and the new value stays. The caller of the operation gets the error as it is.

    Changes can cause each other. At nesting_limit levels the next change fails with a DependencyError, before the stack is full. Raises of events count too, and other code that calls handlers takes part with guard_nesting.

    #The logical tree

    An element is an object of a type that was registered with element=true, or that derives from such a type. An element can have a logical parent and logical children. Only a control changes them, for the elements that it contains: a panel for its children, a button for its content. There are two ways to do this without code for each change:

    • An ElementCollection is the list of the children of a panel. It links the elements that it gets and unlinks those that it loses.
    • logical_content in the metadata of a property gives the element in a value. This element is a logical child of the object as long as the property has the value.

    ///|
    let element_type : @dependency.OwnerType = try! @dependency.register_owner_type(
    "FrameworkElement",
    element=true,
    )

    ///|
    let stack_type : @dependency.OwnerType = try! @dependency.register_owner_type(
    "StackPanel",
    base=element_type,
    )

    ///|
    let card_type : @dependency.OwnerType = try! @dependency.register_owner_type(
    "Card",
    base=element_type,
    )

    ///|
    let element_key : @dependency.ValueKey[@dependency.DependencyObject?] = @dependency.ValueKey::new(
    "Element",
    )

    ///|
    let card_content : @dependency.DependencyProperty[@dependency.DependencyObject?] = try! @dependency.register(
    "Content",
    card_type,
    element_key,
    default_value=None,
    metadata=@dependency.PropertyMetadata::new(logical_content=value => value),
    )

    ///|
    priv struct StackPanel {
    object : @dependency.DependencyObject
    children : @dependency.ElementCollection
    }

    ///|
    fn StackPanel::new() -> StackPanel raise {
    let object = @dependency.DependencyObject::new(stack_type)
    { object, children: @dependency.ElementCollection::new(object), }
    }

    ///|
    test "a panel and a card" {
    let panel = StackPanel::new()
    let card = @dependency.DependencyObject::new(card_type)
    let image = @dependency.DependencyObject::new(element_type)
    panel.children.add(card)
    card.set_value(card_content, Some(image))
    assert_eq(panel.object.logical_children(), [card])
    assert_eq(image.logical_parent(), Some(card))
    panel.children.clear()
    card.clear_value(card_content)
    assert_eq(card.logical_parent(), None)
    assert_eq(image.logical_parent(), None)
    }

    FunctionWhat it does
    logical_parentgives the parent, or None
    logical_childrengives a copy of the list of the children
    route_parentgives the next element on the route of an event, which is the logical parent
    add_logical_child, insert_logical_childlink a child
    remove_logical_childunlinks a child

    A control that has its elements in another form, such as two named parts, calls the last three itself. The tree has only elements. Other objects that a control contains, such as text, are not in it: add_logical_child ignores them.

    #What fails

    An element has one parent at most, and both have the same registry. The messages of the first four cases are those of WPF.

    CaseMessage
    the element has another parentSpecified element is already the logical child of another element. Disconnect it first.
    the element is the parent itselfElement cannot be its own parent.
    a callback changes the children over which a walk goesCannot modify the logical children for this node at this time because a tree walk is in progress.
    an element becomes a descendant of itselfLogical tree depth exceeded while traversing the tree. This could indicate a cycle in the tree.
    the element is in another slot of the collectionnames the element and the index
    the parent or an item of a collection is not an elementnames the type
    the elements have two registriesnames both types

    To add an element to the parent that it has already is no error, and changes nothing. To remove one that is no child is none either.

    The three functions above that link and unlink, and add, insert and replace of a collection, find all but the loop before something changes. The other edits of a collection and a change of logical content consist of steps, which are described below. The steps before the one that fails stay done.

    As in WPF, a loop is linked first, and then the walk over the new child fails because it gets more than 4,096 levels deep. Remove the child again to repair the tree.

    #Collections

    • add and insert store the element and then link it. MAUI and Avalonia do the same, and WPF links first.
    • remove and remove_at remove the element from the collection and then unlink it.
    • replace unlinks the old element while it still has the slot, then puts the new one in the slot, and then links it.
    • clear empties the collection and then unlinks one element after the other.

    The logical children have the order of the collection, as long as nothing else changes them.

    #Logical content

    When the effective value of a property with logical_content changes, three steps follow after the new value is stored and before callbacks and handlers run. They are those of ContentControl in WPF:

    1. The old element is unlinked, when it still is a child of the object.
    2. The new element is unlinked from the parent that it has, so content moves from one parent to another without an error.
    3. The new element is linked.

    The former parent does not hear of step 2. An ElementCollection keeps the element in its slot, and the property of another control keeps its value. Both leave the element alone when they lose it, because they are not its parent any more.

    #Errors while the tree changes

    A callback or a handler that hears of a new inherited value can fail. The edit then stops with this error. What it has done stays, and it does not go on with the next step or the next element. So a replace can leave the old element in its slot without a parent, and a clear can leave later elements linked that the collection does not have any more. A collection can add those again or remove them.

    #Inherited values

    A property with inherits=true in its metadata goes down the logical tree. An element on which nothing is set uses the value of its ancestors, as with a font size.

    ///|
    let text_type : @dependency.OwnerType = try! @dependency.register_owner_type(
    "TextElement",
    )

    ///|
    let font_size : @dependency.DependencyProperty[Double] = try! @dependency.register_attached(
    "FontSize",
    text_type,
    @dependency.double_key,
    default_value=12.0,
    metadata=@dependency.PropertyMetadata::new(inherits=true),
    )

    ///|
    test "a font size for a panel and all that is in it" {
    let panel = StackPanel::new()
    let card = @dependency.DependencyObject::new(card_type)
    let caption = @dependency.DependencyObject::new(element_type)
    card.set_value(card_content, Some(caption))
    panel.object.set_value(font_size, 20.0)
    panel.children.add(card)
    assert_eq(caption.get_value(font_size), 20.0)
    assert_eq(caption.read_local_value(font_size), None)
    card.set_value(font_size, 16.0)
    assert_eq(caption.get_value(font_size), 16.0)
    card.clear_value(font_size)
    assert_eq(caption.get_value(font_size), 20.0)
    panel.children.remove(card)
    assert_eq(caption.get_value(font_size), 12.0)
    }

    • An element inherits from the nearest ancestor on which the property inherits. What it gets is the effective value there, with the current value and the coercion.
    • An ancestor gives no value when it has only its default value. The element then uses the default value of its own type. So a button type with another default font size keeps it, until a font size is set above the button.
    • A current value over an inherited value stays until the element inherits another value, or until the next set_value or clear_value.
    • The metadata for the type of each element decides whether the property inherits there. So register a property as attached when it is for elements of unrelated types. One from register inherits on its owner type and the types that derive from it, and on others with an override. Do not depend on what it does below an element of another type. WPF does not define that either.
    • An element on whose type an override has inherits=false has only its own value and gives none. What its parent gives passes it and reaches the elements below it.
    • What an element inherits depends on the tree, and not on the order in which it was built and the values were set.

    #The order of the changes

    When the value that an element gives changes, the elements below it are evaluated one after the other: a child, then all that is below this child, then the next child. This does not go below an element with a local value. When an element gets another place, it is linked or unlinked first. Then the element and all that is below it are evaluated in the same order, also below local values, with all properties of one element before the next element.

    Each element stores its new value and notifies the change before the next one is evaluated. So a handler on the parent still sees the old values of the children. A handler can change values, and these changes are complete when it goes on. It can change the children of other elements, but not of one over whose children the evaluation goes at that moment.

    An error of a callback or a handler stops the evaluation. The elements that it did not reach keep the values that they had, until a later change of the value or of their place reaches them.

    An evaluation fails when it gets more than 4,096 levels deep, and the search for an ancestor ends after as many. So a tree with a loop gives an error and can be repaired.

    #Events

    The package events has routed events for elements: a button raises Click, and handlers on the button, its panel and all other ancestors can run.

    #Property paths

    The package path has property paths: Child.Width reads, sets and observes the Width of whatever object is the Child.

    #Value keys

    A ValueKey[T] stands for the type T at run time. It wraps a value as AnyValue, and only the same key gives the value back. Keys that were made separately for one type do not match, so there is one shared key for each primitive type (bool_key, byte_key, int16_key, int_key, int64_key, float_key, double_key, char_key, string_key and unit_key) and object_key for dependency objects. Make one key for each type of your own:

    • ValueKey::new compares values with ==.
    • ValueKey::by_identity compares objects by identity, as WPF does with the values of reference types.

    DependencyProperty::info gives the handle of a property without its value type, a PropertyInfo, for code that has properties of many types. Its equal_values compares two AnyValues with the equality of the property. Two NaNs are equal for double_key and float_key.

    #Registries

    Types and properties are in the registry of the process, as in WPF, unless you pass another one with registry~. A test can make its own with PropertyRegistry::new(). Nothing is shared between registries. A function that gets a type of another registry, or values of two registries, fails.

    Other packages can keep their own tables in a registry, with extension and set_extension and a key of their own.

    #Identities

    These are equal and have a hash by identity, so that they can be keys of tables: RegistryId, OwnerType, PropertyInfo, PropertyKeyInfo (the key of a read-only property without its value type), ValueKeyId and DependencyObject. All but the last have no reference to the registry, so a table in a registry can hold them.

    #The host of an object

    A MoonBit struct cannot derive from DependencyObject. It has one in a field instead, and gives itself to the object as its host value, so that code with only the object finds the control.

    ///|
    priv struct Label {
    object : @dependency.DependencyObject
    caption : String
    }

    ///|
    let label_key : @dependency.ValueKey[Label] = @dependency.ValueKey::by_identity(
    "Label",
    )

    ///|
    test "from the object to the control" {
    let label_type = @dependency.register_owner_type("Label")
    let object = @dependency.DependencyObject::new(label_type)
    object.set_host(label_key.wrap({ object, caption: "Name", }))
    guard object.host() is Some(host) && label_key.unwrap(host) is Some(label) else {
    fail("no label")
    }
    assert_eq(label.caption, "Name")
    label.object.release_host()
    assert_true(object.host() is None)
    }

    An object has at most one host value in its life. Other packages can keep data on an object, with extension, set_extension and remove_extension and a key of their own.

    #Memory

    js and wasm-gc free everything that a program no longer uses. native and wasm count references, and never free objects that refer to each other in a circle. A control and its object are such a circle, so call release_host before you drop the control on these targets. A handler that refers to the object on which it is needs remove_change_handler, so use the object that the handler gets as its argument. A value that refers to the object needs clear_value.

    A parent and its child are such a circle too. On native and wasm, nothing frees a tree that a program just drops. Unlink every element of it, not only the root: clear each collection and clear_value each property with logical content. An element that is unlinked is freed, also when the program goes on to use its former parent.

    A registry of your own needs no such call, as long as your default values, your functions and your tables in it do not refer to it. A dependency object refers to its registry.

    #Differences from WPF

    MoonBit forces these:

    1. A registry is a value. WPF has one hidden table for the process. Here that table is the default of an argument, and values of two registries cannot be used together.
    2. Changes nest at most nesting_limit levels deep. WPF has no limit, and its process ends when the stack is full. Programs on wasm cannot recover from that either.
    3. Values have types, and keys stand for them. WPF stores object. Here a property is a DependencyProperty[T], and a value of unknown type is an AnyValue. MoonBit has no overloads, so SetValue and ClearValue with a key are set_with_key and clear_with_key.
    4. Handlers are removed with a token. WPF compares the handlers, and functions of MoonBit have no equality.
    5. A control has a dependency object. In WPF it is one, and MoonBit has no inheritance between structs.
    6. release_host. In WPF the control is the dependency object, and nothing has to be released.
    7. The default value is required. WPF uses the default value of the .NET type when there is none.
    8. An element is an object of an element type. In WPF it is an object of a class that derives from FrameworkElement or FrameworkContentElement.
    9. The logical children are a stored list. In WPF a panel computes them from its collection, and so lists an element that was unlinked and still has its slot.

    One difference is a decision. Each element stores the values that it inherits, and notifies whenever one changes. In WPF an element without other values can read them from an ancestor. This shows in four cases:

    • When a local value equal to the default is set above an element, the elements below that element get it too, and notify when their own default is another one. WPF stops at the first element whose value stays the same.
    • The elements below one with inherits=false always have the value from above it, and notify it. In WPF they have it or not, depending on which other values were set on them and on the order of the steps, and a new parent is not notified.
    • The elements that an evaluation did not reach because of an error keep their values. In WPF they can read the new value.
    • A current value over an inherited value stays. WPF loses it at once on an element that reads its values from an ancestor.

    These follow MAUI and Avalonia, which agree with each other and not with WPF:

    • A coerced value is not validated. WPF validates it.
    • A coercion returns a value. In WPF it can also return UnsetValue, which keeps the old value. Here it returns the old value for that.
    • A collection stores an element before it links it, and replace works for a slot that has an element. In WPF such a replacement fails, after the old element has lost its parent.

    DependencyError

    pub(all) suberror DependencyError {
    RegistryMismatch(String)
    InvalidRegistration(String)
    HostAlreadySet(String)
    InvalidValue(String)
    ReadOnly(String)
    InvalidEvent(String)
    InvalidTreeEdit(String)
    TreeLoop(String)
    NestingLimit(String)
    } derive(
    Debug
    )

    Why a function of this package failed. The text names the property as Owner.Name, the type and, when there is one, the value. An error of a callback or a handler is not one of these: it reaches the caller as it is. The functions of the package events fail with these errors too.

    DependencyError::to_string

    fn DependencyError::to_string(self : DependencyError) -> String

    Default implementation for Show::to_string, uses a StringBuilder

    AnyValue

    pub struct AnyValue {
    // private fields
    }

    A value of any type, which only the key that wrapped it gives back.

    MoonBit has no Any type and no downcasts. The payload is a closure that puts the value in the slot of its key. An AnyValue has no equality, because equality belongs to the property: see PropertyInfo::equal_values.

    AnyValue::key_id

    fn AnyValue::key_id(self : AnyValue) -> ValueKeyId

    The identity of the key that wrapped the value.

    DependencyObject

    pub struct DependencyObject {
    // private fields
    }

    An object whose property values this package stores and resolves.

    MoonBit has no struct inheritance. A type such as Button has a dependency object in a field, made for the owner type Button, where a WPF control derives from DependencyObject.

    DependencyObject::add_change_handler

    fn DependencyObject::add_change_handler(self : DependencyObject, handler : (DependencyObject, PropertyInfo, AnyValue, AnyValue) -> Unit raise) -> HandlerToken

    Run handler after each change of an effective value of the object, with the object, the property, the old value and the new value. Handlers run in the order in which they were added, after the callbacks of the metadata. An error of a handler reaches the caller that made the change, and the new value stays.

    DependencyObject::add_logical_child

    fn DependencyObject::add_logical_child(self : DependencyObject, child : DependencyObject) -> Unit raise

    Make child the last logical child. A control does this for an element that becomes a part of it. Nothing happens when child is a logical child of this element already, or when its type is not an element type.

    child and the elements below it then inherit values from their new ancestors and notify the changes. An error of a callback or a handler reaches the caller, and child stays a logical child.

    DependencyObject::clear_value

    fn[T] DependencyObject::clear_value(self : DependencyObject, property : DependencyProperty[T]) -> Unit raise

    Remove the local value and the current value, so that the object inherits again. A default value that becomes effective is not coerced, and an inherited value is.

    DependencyObject::clear_with_key

    fn[T] DependencyObject::clear_with_key(self : DependencyObject, key : DependencyPropertyKey[T]) -> Unit raise

    Remove the local value of a read-only property.

    DependencyObject::coerce_value

    fn[T] DependencyObject::coerce_value(self : DependencyObject, property : DependencyProperty[T]) -> Unit raise

    Coerce the value again, after a change of something that the coercion reads. This coerces a default value too.

    DependencyObject::extension

    fn[T] DependencyObject::extension(self : DependencyObject, key : ValueKey[T]) -> T?

    The data that a package keeps on the object under its own key.

    DependencyObject::get_value

    fn[T] DependencyObject::get_value(self : DependencyObject, property : DependencyProperty[T]) -> T raise DependencyError

    The effective value: the local value, or else the inherited value, or else the default value for the type of the object, then the current value instead when there is one, and then the result of the coercion. Every property of the registry has a value on every object.

    DependencyObject::host

    The value that embeds the object.

    DependencyObject::insert_logical_child

    fn DependencyObject::insert_logical_child(self : DependencyObject, index : Int, child : DependencyObject) -> Unit raise

    Make child a logical child, as add_logical_child does, at the position index, which can be the number of logical children.

    DependencyObject::local_values

    The properties for which read_local_value has a value, with that value, in the order of their registration. The result is a copy.

    DependencyObject::logical_children

    fn DependencyObject::logical_children(self : DependencyObject) -> Array[DependencyObject]

    The logical children in their order. The result is a copy.

    DependencyObject::logical_parent

    fn DependencyObject::logical_parent(self : DependencyObject) -> DependencyObject?

    The element that has this one as a logical child, or None when there is none.

    DependencyObject::new

    A new object of the type owner, which belongs to registry.

    DependencyObject::owner_type

    fn DependencyObject::owner_type(self : DependencyObject) -> OwnerType

    DependencyObject::read_local_value

    fn[T] DependencyObject::read_local_value(self : DependencyObject, property : DependencyProperty[T]) -> T? raise DependencyError

    The current value when there is one, or else the local value. Both are without the coercion.

    DependencyObject::registry

    The registry that has the type of the object and the properties that can be used with it.

    DependencyObject::registry_id

    fn DependencyObject::registry_id(self : DependencyObject) -> RegistryId

    DependencyObject::release_host

    fn DependencyObject::release_host(self : DependencyObject) -> Unit

    Let go of the host value. The host and the object hold each other, and native and wasm do not free such a circle.

    DependencyObject::remove_change_handler

    fn DependencyObject::remove_change_handler(self : DependencyObject, token : HandlerToken) -> Bool

    Remove the handler for which add_change_handler of this object returned token. Returns false when it has no such handler.

    DependencyObject::remove_extension

    fn[T] DependencyObject::remove_extension(self : DependencyObject, key : ValueKey[T]) -> Unit

    Let go of the value under key, when there is one.

    DependencyObject::remove_logical_child

    fn DependencyObject::remove_logical_child(self : DependencyObject, child : DependencyObject) -> Unit raise

    Make child an element without a logical parent. Nothing happens when it is not a logical child of this element. An ElementCollection that has it keeps it.

    child and the elements below it then lose the values of their former ancestors and notify the changes.

    DependencyObject::route_parent

    The next element on the route of an event to the root. This is the logical parent.

    DependencyObject::set_current_value

    fn[T] DependencyObject::set_current_value(self : DependencyObject, property : DependencyProperty[T], value : T) -> Unit raise

    Change the effective value until the next set_value or clear_value, or until the object inherits another value and has no local value. A control does this for its own changes, such as those by a click. An invalid value is an error and changes nothing.

    DependencyObject::set_extension

    fn[T] DependencyObject::set_extension(self : DependencyObject, key : ValueKey[T], value : T) -> Unit

    Keep value on the object under key, in the place of the value that was there. This is not a property: it has no metadata and notifies nothing.

    DependencyObject::set_host

    fn DependencyObject::set_host(self : DependencyObject, host : AnyValue) -> Unit raise DependencyError

    Connect the object with the value that embeds it, such as a Button, so that code that has only the object gets the Button with host. This works once, also after release_host.

    DependencyObject::set_value

    fn[T] DependencyObject::set_value(self : DependencyObject, property : DependencyProperty[T], value : T) -> Unit raise

    Set the local value, which discards a current value. An invalid value is an error and changes nothing.

    DependencyObject::set_with_key

    fn[T] DependencyObject::set_with_key(self : DependencyObject, key : DependencyPropertyKey[T], value : T) -> Unit raise

    Set the local value of a read-only property.

    DependencyProperty

    pub struct DependencyProperty[T] {
    // private fields
    }

    A dependency property with values of type T.

    DependencyProperty::add_owner

    fn[T] DependencyProperty::add_owner(self : DependencyProperty[T], owner : OwnerType, metadata? : PropertyMetadata[T], registry? : PropertyRegistry) -> DependencyProperty[T] raise DependencyError

    Make the property one of owner too, so that lookup_property finds it there by its name. Returns the same property.

    metadata is for objects of the type owner, as with override_metadata. A read-only property cannot have it here, but override_metadata can give it with the key.

    DependencyProperty::get_metadata

    fn[T] DependencyProperty::get_metadata(self : DependencyProperty[T], owner : OwnerType) -> MergedMetadata[T] raise DependencyError

    The metadata for objects of the type owner.

    DependencyProperty::info

    The property without its value type.

    DependencyProperty::override_metadata

    fn[T] DependencyProperty::override_metadata(self : DependencyProperty[T], owner : OwnerType, metadata : PropertyMetadata[T], key? : DependencyPropertyKey[T]) -> Unit raise DependencyError

    Give the property metadata for objects of the type owner and of the types that derive from it. key is needed for a read-only property.

    This works once for each type, and the registration does it already for the type of a property with metadata and of a read-only one that is not attached. The metadata of the base type is merged at this moment. Do this before there are objects of the type: an object keeps the values that it has stored, and nothing is notified.

    DependencyPropertyKey

    pub struct DependencyPropertyKey[T] {
    // private fields
    }

    The right to change a read-only property. The code that registers the property gets it, and keeps it private.

    DependencyPropertyKey::info

    The key without the value type of its property.

    DependencyPropertyKey::property

    The property, which everyone can read.

    ElementCollection

    pub struct ElementCollection {
    // private fields
    }

    The elements of a control, such as the children of a panel. Each element that the collection gets becomes a logical child of the owner, and each that it loses does not stay one.

    An element that is not a child of the owner any more keeps its slot: this is so after remove_logical_child of the owner, and when another control takes the element as its content.

    ElementCollection::add

    fn ElementCollection::add(self : ElementCollection, element : DependencyObject) -> Unit raise

    Put element at the end. It has no parent, or the owner is its parent already.

    ElementCollection::clear

    fn ElementCollection::clear(self : ElementCollection) -> Unit raise

    Remove all elements, and then unlink one after the other. When that fails for one, the later ones stay logical children of the owner.

    ElementCollection::contains

    fn ElementCollection::contains(self : ElementCollection, element : DependencyObject) -> Bool

    ElementCollection::get

    fn ElementCollection::get(self : ElementCollection, index : Int) -> DependencyObject?

    The element at index, or None when there is no such index.

    ElementCollection::insert

    fn ElementCollection::insert(self : ElementCollection, index : Int, element : DependencyObject) -> Unit raise

    Put element at index, which can be the length. It has no parent, or the owner is its parent already.

    An error of a callback or a handler that hears of a new inherited value reaches the caller, and the element stays in the collection.

    ElementCollection::iter

    The elements that the collection has now.

    ElementCollection::length

    fn ElementCollection::length(self : ElementCollection) -> Int

    ElementCollection::new

    An empty collection of logical children of owner.

    ElementCollection::remove

    fn ElementCollection::remove(self : ElementCollection, element : DependencyObject) -> Unit raise

    Remove element, and unlink it when the owner is its parent, also when the collection does not have it.

    ElementCollection::remove_at

    fn ElementCollection::remove_at(self : ElementCollection, index : Int) -> Unit raise

    Remove the element at index, and unlink it when the owner is its parent.

    ElementCollection::replace

    fn ElementCollection::replace(self : ElementCollection, index : Int, element : DependencyObject) -> Unit raise

    Put element in the slot at index. The old element is unlinked first, while it has the slot. When that fails, it keeps the slot. Nothing happens when element has this slot.

    HandlerToken

    pub struct HandlerToken {
    // private fields
    } derive(Eq, Hash,
    Debug
    )

    The identity of a handler, with which it is removed. MoonBit functions have no equality, with which WPF does this.

    HandlerToken::new

    A token that is unequal to all others, for a list of handlers in another package.

    MergedMetadata

    pub struct MergedMetadata[T] {
    // private fields
    }

    The metadata of a property that is in force for a type.

    MergedMetadata::default_value

    fn[T] MergedMetadata::default_value(self : MergedMetadata[T]) -> T

    MergedMetadata::inherits

    fn[T] MergedMetadata::inherits(self : MergedMetadata[T]) -> Bool

    MergedMetadata::logical_content

    fn[T] MergedMetadata::logical_content(self : MergedMetadata[T], value : T) -> DependencyObject?

    The element in value that becomes a logical child of the object.

    OwnerType

    pub struct OwnerType {
    // private fields
    }

    A type of dependency objects. It owns the names of properties, and properties can have metadata for it.
    impl Eq for OwnerType
    impl Hash for OwnerType
    impl Show for OwnerType

    OwnerType::base_type

    fn OwnerType::base_type(self : OwnerType) -> OwnerType?

    OwnerType::derives_from

    fn OwnerType::derives_from(self : OwnerType, other : OwnerType) -> Bool

    Whether other is this type or a type that it derives from.

    OwnerType::is_element

    fn OwnerType::is_element(self : OwnerType) -> Bool

    Whether the type, or a type that it derives from, was registered as an element type.

    OwnerType::name

    fn OwnerType::name(self : OwnerType) -> String

    OwnerType::registry_id

    fn OwnerType::registry_id(self : OwnerType) -> RegistryId

    OwnerType::to_string

    fn OwnerType::to_string(self : OwnerType) -> String

    Default implementation for Show::to_string, uses a StringBuilder

    PropertyInfo

    pub struct PropertyInfo {
    // private fields
    }

    A dependency property without its value type: its identity, and access to its values as AnyValue.

    All handles of one property are equal, also one that lookup_property gives.
    impl Eq for PropertyInfo

    PropertyInfo::clear_value

    fn PropertyInfo::clear_value(self : PropertyInfo, object : DependencyObject) -> Unit raise

    Remove the local value from object, as DependencyObject::clear_value does.

    PropertyInfo::clear_with_key

    fn PropertyInfo::clear_with_key(self : PropertyInfo, object : DependencyObject, key : PropertyKeyInfo) -> Unit raise

    Remove the local value of a read-only property from object. key has to be the key of this property.

    PropertyInfo::coerce_value

    fn PropertyInfo::coerce_value(self : PropertyInfo, object : DependencyObject) -> Unit raise

    Coerce the value on object again, as DependencyObject::coerce_value does.

    PropertyInfo::equal_values

    fn PropertyInfo::equal_values(self : PropertyInfo, a : AnyValue, b : AnyValue) -> Bool

    Whether a and b are equal values of the property, by the equality that also decides whether a value has changed. A value of another key is equal to nothing.

    PropertyInfo::get_value

    fn PropertyInfo::get_value(self : PropertyInfo, object : DependencyObject) -> AnyValue raise DependencyError

    The effective value on object, as DependencyObject::get_value gives it.

    PropertyInfo::is_attached

    fn PropertyInfo::is_attached(self : PropertyInfo) -> Bool

    PropertyInfo::is_read_only

    fn PropertyInfo::is_read_only(self : PropertyInfo) -> Bool

    PropertyInfo::name

    fn PropertyInfo::name(self : PropertyInfo) -> String

    PropertyInfo::owner_type

    fn PropertyInfo::owner_type(self : PropertyInfo) -> OwnerType

    The type on which the property was registered, not one that add_owner added.

    PropertyInfo::read_local_value

    fn PropertyInfo::read_local_value(self : PropertyInfo, object : DependencyObject) -> AnyValue? raise DependencyError

    The current value or the local value on object, as DependencyObject::read_local_value gives it.

    PropertyInfo::registry_id

    fn PropertyInfo::registry_id(self : PropertyInfo) -> RegistryId

    PropertyInfo::set_current_value

    fn PropertyInfo::set_current_value(self : PropertyInfo, object : DependencyObject, value : AnyValue) -> Unit raise

    Set the current value on object, as DependencyObject::set_current_value does.

    PropertyInfo::set_value

    fn PropertyInfo::set_value(self : PropertyInfo, object : DependencyObject, value : AnyValue) -> Unit raise

    Set the local value on object, as DependencyObject::set_value does. The value key of the property has to have wrapped value.

    PropertyInfo::set_with_key

    fn PropertyInfo::set_with_key(self : PropertyInfo, object : DependencyObject, key : PropertyKeyInfo, value : AnyValue) -> Unit raise

    Set the local value of a read-only property on object. key has to be the key of this property.

    PropertyInfo::to_string

    fn PropertyInfo::to_string(self : PropertyInfo) -> String

    Default implementation for Show::to_string, uses a StringBuilder

    PropertyInfo::value_key_id

    fn PropertyInfo::value_key_id(self : PropertyInfo) -> ValueKeyId

    The identity of the key that wraps the values of the property.

    PropertyKeyInfo

    pub struct PropertyKeyInfo {
    // private fields
    } derive(Eq, Hash,
    Debug
    )

    The right to change a read-only property, without its value type. Two keys are equal when their properties are.

    PropertyKeyInfo::property

    PropertyMetadata

    pub struct PropertyMetadata[T] {
    // private fields
    }

    How a property behaves on the objects of a type. What is missing comes from the metadata of the base type.

    PropertyMetadata::new

    fn[T] PropertyMetadata::new(default_value? : T, changed? : (DependencyObject, DependencyProperty[T], T, T) -> Unit raise, coerce? : (DependencyObject, T) -> T raise, inherits? : Bool, logical_content? : (T) -> DependencyObject?) -> PropertyMetadata[T]

    Metadata to register a property with, or to give it for another type.

    • default_value is the value of objects on which nothing is set. A registration has it as its own argument, and fails when it is here.
    • changed runs after each change of the effective value, with the object, the property, the old value and the new one. It does not replace the one of the base type, which runs first.
    • coerce computes the effective value from the value that is set. It can read and change other properties of the object. Its result is not validated.
    • inherits says that elements on which nothing is set use the value of their logical ancestors.
    • logical_content gives the element in a value. It becomes a logical child of the object, before changed runs.

    PropertyRegistry

    pub struct PropertyRegistry {
    // private fields
    }

    Owner types, dependency properties and the tables of other packages.

    Every function that registers or looks up something uses the default registry of the process, unless it gets another one with registry~. A type, a property or an object of one registry cannot be used with those of another.

    Nothing in a registry holds the registry: types and properties have only its id. Records in an extension table have to do the same, so that every target frees a registry that a program drops.

    PropertyRegistry::check_owner

    fn PropertyRegistry::check_owner(self : PropertyRegistry, owner : OwnerType) -> Unit raise DependencyError

    Fail when owner is a type of another registry. Functions of other packages that get a registry and a type call it.

    PropertyRegistry::default

    The registry of the process.

    PropertyRegistry::extension

    fn[T] PropertyRegistry::extension(self : PropertyRegistry, key : ValueKey[T]) -> T?

    The table that a package keeps in the registry under its own key.

    PropertyRegistry::id

    PropertyRegistry::new

    A new registry that shares nothing with other registries.

    PropertyRegistry::set_extension

    fn[T] PropertyRegistry::set_extension(self : PropertyRegistry, key : ValueKey[T], value : T) -> Unit

    Keep value in the registry under key, in the place of the value that was there.

    RegistryId

    pub struct RegistryId {
    // private fields
    } derive(Eq, Hash)

    The identity of a registry. It does not hold the registry, so that a record in an extension table can keep it.
    impl Show for RegistryId

    RegistryId::to_string

    fn RegistryId::to_string(self : RegistryId) -> String

    Default implementation for Show::to_string, uses a StringBuilder

    ValueKey

    pub struct ValueKey[T] {
    // private fields
    }

    A key that wraps values of type T as AnyValue and reads them back.

    Keys that were made separately for one type do not read the values of each other. Use the shared keys of this package, such as double_key, for the primitive types, and make one key for each type of your own.

    ValueKey::by_identity

    fn[T] ValueKey::by_identity(name : String, show? : (T) -> String) -> ValueKey[T]

    A new key whose values are equal only when they are the same object, as WPF compares the values of reference types.

    ValueKey::id

    fn[T] ValueKey::id(self : ValueKey[T]) -> ValueKeyId

    The identity of the key, which AnyValue::key_id gives for its values.

    ValueKey::new

    fn[T : Eq] ValueKey::new(name : String, show? : (T) -> String) -> ValueKey[T]

    A new key whose values are equal by ==. Error messages show a value with show, or with name when it is omitted.

    ValueKey::unwrap

    fn[T] ValueKey::unwrap(self : ValueKey[T], any : AnyValue) -> T?

    The value in any when this key wrapped it.

    ValueKey::wrap

    fn[T] ValueKey::wrap(self : ValueKey[T], value : T) -> AnyValue

    Wrap value as a value of any type.

    ValueKeyId

    pub struct ValueKeyId {
    // private fields
    }

    The identity of a ValueKey. Its text is the name of the key.
    impl Eq for ValueKeyId
    impl Hash for ValueKeyId
    impl Show for ValueKeyId

    ValueKeyId::to_string

    fn ValueKeyId::to_string(self : ValueKeyId) -> String

    Default implementation for Show::to_string, uses a StringBuilder

    bool_key

    let bool_key : ValueKey[Bool]

    The key of Bool values.

    byte_key

    let byte_key : ValueKey[Byte]

    The key of Byte values.

    char_key

    let char_key : ValueKey[Char]

    The key of Char values.

    double_key

    let double_key : ValueKey[Double]

    The key of Double values. Two NaNs are equal, as in .NET.

    float_key

    let float_key : ValueKey[Float]

    The key of Float values. Two NaNs are equal, as in .NET.

    guard_nesting

    fn[T] guard_nesting(label : String, f : () -> T raise) -> T raise

    Run f one level deeper in the nesting that the changes of properties share, and fail past nesting_limit with an error that names label. Code that calls handlers which can change properties, such as a raise of a routed event, runs them with this function.

    int16_key

    let int16_key : ValueKey[Int16]

    The key of Int16 values.

    int64_key

    let int64_key : ValueKey[Int64]

    The key of Int64 values.

    int_key

    let int_key : ValueKey[Int]

    The key of Int values.

    lookup_property

    fn lookup_property(owner : OwnerType, name : String, registry? : PropertyRegistry) -> PropertyInfo? raise DependencyError

    The property with the name name of owner, or else of the nearest type that it derives from.

    nesting_limit

    let nesting_limit : Int

    How deep changes of properties and raises of routed events can cause each other. One more level fails with an error. WPF has no limit, and its process ends when the stack is full.

    object_key

    let object_key : ValueKey[DependencyObject]

    The key of dependency objects. Two of them are equal when they are the same object.

    register

    fn[T] register(name : String, owner : OwnerType, key : ValueKey[T], default_value~ : T, validate? : (T) -> Bool, equal? : (T, T) -> Bool, metadata? : PropertyMetadata[T], registry? : PropertyRegistry) -> DependencyProperty[T] raise DependencyError

    Register a dependency property on owner.

    • key wraps the values. Its equality decides whether a value has changed, unless equal is given.
    • default_value is the value of an object on which nothing is set.
    • validate accepts or rejects a value that is set, and the default value.
    • metadata is for objects of the type owner and of the types that derive from it.

    An owner type can have one property with a name. A type that derives from it can use the name for another property.

    register_attached

    fn[T] register_attached(name : String, owner : OwnerType, key : ValueKey[T], default_value~ : T, validate? : (T) -> Bool, equal? : (T, T) -> Bool, metadata? : PropertyMetadata[T], registry? : PropertyRegistry) -> DependencyProperty[T] raise DependencyError

    Register a property that its owner type defines for objects of every type, such as Grid.Row. The arguments are those of register, but metadata is for objects of all types.

    register_attached_read_only

    fn[T] register_attached_read_only(name : String, owner : OwnerType, key : ValueKey[T], default_value~ : T, validate? : (T) -> Bool, equal? : (T, T) -> Bool, metadata? : PropertyMetadata[T], registry? : PropertyRegistry) -> DependencyPropertyKey[T] raise DependencyError

    Register an attached property that only code with the returned key can change. The arguments are those of register.

    register_owner_type

    fn register_owner_type(name : String, base? : OwnerType, element? : Bool, registry? : PropertyRegistry) -> OwnerType raise DependencyError

    Register a type of dependency objects under a name that is unique in the registry.

    base is a type of the same registry. The new type finds its properties by name and uses their metadata. element says that objects of the type can have a place in the logical tree, which is also so for a type that derives from an element type.

    register_read_only

    fn[T] register_read_only(name : String, owner : OwnerType, key : ValueKey[T], default_value~ : T, validate? : (T) -> Bool, equal? : (T, T) -> Bool, metadata? : PropertyMetadata[T], registry? : PropertyRegistry) -> DependencyPropertyKey[T] raise DependencyError

    Register a property that only code with the returned key can change. The arguments are those of register.

    string_key

    let string_key : ValueKey[String]

    The key of String values.

    unit_key

    let unit_key : ValueKey[Unit]

    The key of Unit, for things without data.

    version

    fn version() -> String

    The version of the moonrockz/dependency-object module. mise run validate:versions keeps it equal to the version of the root moon.mod.