Dependency objects and dependency properties for MoonBit, with the behavior of WPF
| Package | Purpose |
|---|---|
| moonrockz/dependency-object | Types, properties, objects and their values, and version() |
| moonrockz/dependency-object/events | Routed events of these types |
| moonrockz/dependency-object/path | Property paths, as WPF's PropertyPath |
///|
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)
}///|
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")
}| Function | What it registers |
|---|---|
| register_owner_type | a type of objects, with the type that it derives from |
| register | a property of a type |
| register_attached | a property that a type defines for objects of every type, such as Grid.Row |
| register_read_only, register_attached_read_only | a property that only code with the returned DependencyPropertyKey can change |
| DependencyProperty::add_owner | an existing property under its name on another type |
| Argument | What it is | When an override does not have it |
|---|---|---|
| default_value | the value of objects on which nothing is set | that of the base type |
| coerce | computes the effective value | that of the base type |
| changed | runs after a change | that of the base type, which also runs, and first, when there is one |
| inherits | whether elements use the value of their ancestors, see Inherited values | that of the base type |
| logical_content | the element in a value, see The logical tree | that of the base type |
///|
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)
}| Function | What it does |
|---|---|
| get_value | gives the effective value |
| set_value | sets the local value |
| set_current_value | changes the effective value and not the local value |
| clear_value | removes the local value and the current value |
| coerce_value | coerces the value again |
| read_local_value | gives the current value or the local value, or None |
| local_values | gives all properties that have such a value |
| set_with_key, clear_with_key | change a read-only property |
///|
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)
}///|
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",
#|]
),
)
}///|
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)
}| Function | What it does |
|---|---|
| logical_parent | gives the parent, or None |
| logical_children | gives a copy of the list of the children |
| route_parent | gives the next element on the route of an event, which is the logical parent |
| add_logical_child, insert_logical_child | link a child |
| remove_logical_child | unlinks a child |
| Case | Message |
|---|---|
| the element has another parent | Specified element is already the logical child of another element. Disconnect it first. |
| the element is the parent itself | Element cannot be its own parent. |
| a callback changes the children over which a walk goes | Cannot modify the logical children for this node at this time because a tree walk is in progress. |
| an element becomes a descendant of itself | Logical tree depth exceeded while traversing the tree. This could indicate a cycle in the tree. |
| the element is in another slot of the collection | names the element and the index |
| the parent or an item of a collection is not an element | names the type |
| the elements have two registries | names both types |
///|
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)
}///|
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)
}pub(all) suberror DependencyError {
RegistryMismatch(String)
InvalidRegistration(String)
HostAlreadySet(String)
InvalidValue(String)
ReadOnly(String)
InvalidEvent(String)
InvalidTreeEdit(String)
TreeLoop(String)
NestingLimit(String)
} derive(Debug)impl Show for DependencyErrorpub struct AnyValue {
// private fields
}pub struct DependencyObject {
// private fields
}impl Eq for DependencyObjectimpl Hash for DependencyObjectimpl Debug for DependencyObjectfn DependencyObject::add_change_handler(self : DependencyObject, handler : (DependencyObject, PropertyInfo, AnyValue, AnyValue) -> Unit raise) -> HandlerTokenfn DependencyObject::add_logical_child(self : DependencyObject, child : DependencyObject) -> Unit raisefn[T] DependencyObject::clear_value(self : DependencyObject, property : DependencyProperty[T]) -> Unit raisefn[T] DependencyObject::clear_with_key(self : DependencyObject, key : DependencyPropertyKey[T]) -> Unit raisefn[T] DependencyObject::coerce_value(self : DependencyObject, property : DependencyProperty[T]) -> Unit raisefn[T] DependencyObject::get_value(self : DependencyObject, property : DependencyProperty[T]) -> T raise DependencyErrorfn DependencyObject::insert_logical_child(self : DependencyObject, index : Int, child : DependencyObject) -> Unit raisefn DependencyObject::new(owner : OwnerType, registry? : PropertyRegistry) -> DependencyObject raise DependencyErrorfn[T] DependencyObject::read_local_value(self : DependencyObject, property : DependencyProperty[T]) -> T? raise DependencyErrorfn DependencyObject::remove_logical_child(self : DependencyObject, child : DependencyObject) -> Unit raisefn[T] DependencyObject::set_current_value(self : DependencyObject, property : DependencyProperty[T], value : T) -> Unit raisefn[T] DependencyObject::set_extension(self : DependencyObject, key : ValueKey[T], value : T) -> Unitfn DependencyObject::set_host(self : DependencyObject, host : AnyValue) -> Unit raise DependencyErrorfn[T] DependencyObject::set_value(self : DependencyObject, property : DependencyProperty[T], value : T) -> Unit raisefn[T] DependencyObject::set_with_key(self : DependencyObject, key : DependencyPropertyKey[T], value : T) -> Unit raisepub struct DependencyProperty[T] {
// private fields
}fn[T] DependencyProperty::add_owner(self : DependencyProperty[T], owner : OwnerType, metadata? : PropertyMetadata[T], registry? : PropertyRegistry) -> DependencyProperty[T] raise DependencyErrorfn[T] DependencyProperty::get_metadata(self : DependencyProperty[T], owner : OwnerType) -> MergedMetadata[T] raise DependencyErrorfn[T] DependencyProperty::override_metadata(self : DependencyProperty[T], owner : OwnerType, metadata : PropertyMetadata[T], key? : DependencyPropertyKey[T]) -> Unit raise DependencyErrorpub struct DependencyPropertyKey[T] {
// private fields
}pub struct ElementCollection {
// private fields
}fn ElementCollection::insert(self : ElementCollection, index : Int, element : DependencyObject) -> Unit raisefn ElementCollection::replace(self : ElementCollection, index : Int, element : DependencyObject) -> Unit raisepub struct MergedMetadata[T] {
// private fields
}pub struct OwnerType {
// private fields
}pub struct PropertyInfo {
// private fields
}impl Eq for PropertyInfoimpl Hash for PropertyInfoimpl Show for PropertyInfoimpl Debug for PropertyInfofn PropertyInfo::clear_with_key(self : PropertyInfo, object : DependencyObject, key : PropertyKeyInfo) -> Unit raisefn PropertyInfo::get_value(self : PropertyInfo, object : DependencyObject) -> AnyValue raise DependencyErrorfn PropertyInfo::read_local_value(self : PropertyInfo, object : DependencyObject) -> AnyValue? raise DependencyErrorfn PropertyInfo::set_current_value(self : PropertyInfo, object : DependencyObject, value : AnyValue) -> Unit raisefn PropertyInfo::set_value(self : PropertyInfo, object : DependencyObject, value : AnyValue) -> Unit raisefn PropertyInfo::set_with_key(self : PropertyInfo, object : DependencyObject, key : PropertyKeyInfo, value : AnyValue) -> Unit raisepub struct PropertyMetadata[T] {
// private fields
}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]pub struct PropertyRegistry {
// private fields
}fn PropertyRegistry::check_owner(self : PropertyRegistry, owner : OwnerType) -> Unit raise DependencyErrorfn[T] PropertyRegistry::set_extension(self : PropertyRegistry, key : ValueKey[T], value : T) -> Unitimpl Show for RegistryIdimpl Debug for RegistryIdpub struct ValueKey[T] {
// private fields
}pub struct ValueKeyId {
// private fields
}impl Eq for ValueKeyIdimpl Hash for ValueKeyIdimpl Show for ValueKeyIdimpl Debug for ValueKeyIdfn[T] guard_nesting(label : String, f : () -> T raise) -> T raisefn lookup_property(owner : OwnerType, name : String, registry? : PropertyRegistry) -> PropertyInfo? raise DependencyErrorlet nesting_limit : Intfn[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 DependencyErrorfn[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 DependencyErrorfn[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 DependencyErrorfn register_owner_type(name : String, base? : OwnerType, element? : Bool, registry? : PropertyRegistry) -> OwnerType raise DependencyErrorfn[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 DependencyErrorfn version() -> StringInstall
Download zipDependency objects and dependency properties for MoonBit, with the behavior of WPF