Files
endorphin-tessera-gui/examples/native-signatures.endor
T

24 lines
1.8 KiB
Plaintext

// 原生回调也发布 Function 签名:state.set 接收一个任意值,increment 接收零参数;此处事件分发将 Event 作为 set 的那个值。
// 同一事件注册机制按实际签名调用;label 绑定再把事件 kind 转成可显示的字符串。
// 保留最后一次收到的 Event 对象,初值 kind=waiting;不是只存一份格式化事件文本。
received = ui.state(Event("waiting"))
// 单独计数的状态与 received 无关,第二个按钮不会改变 eventKind 读取的事件对象。
count = ui.state(0)
// 只读计算提取当前事件种类,作为 label.text 绑定的依赖。
// 由 label.bind 的绑定求值调用,无参数;读取链经过状态 value 再到 Event.kind,返回可显示字符串。
function eventKind(): String { return received.value.kind }
label = TextBlock("waiting")
subscription = label.bind("text", eventKind)
// subscription 管理字段绑定,下面两个 Connection 管理输入监听,两种注册的用途不同。
withEvent = Button("传递 Event 给原生 set")
// received.set 的参数并非专门限定为 Event;本次订阅按一参数签名调用,传入实际 click Event 并覆盖 received.value。
firstConnection = withEvent.on("click", received.set)
// 保存的是绑定 count 的原生方法,事件分发按它的零参签名调用,无需人为包装一个丢弃 Event 的函数。
withoutEvent = Button("调用零参数原生 increment")
// count.increment 的签名为零参数,分发器调用时不传多余 Event。
secondConnection = withoutEvent.on("click", count.increment)
// 把已绑定状态与两种回调放到同一页面,便于对比输入效果。
page = Page()
page.title = "原生方法签名"
page.render(Column(24, 16, TextBlock("源码和原生回调共用参数契约"), label, ui.textOf(count, "value"), withEvent, withoutEvent))