This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Doric/src/Counter.ts

120 lines
3.2 KiB
TypeScript
Raw Normal View History

2019-12-04 14:07:14 +08:00
import { text, vlayout, Image, ViewHolder, VMPanel, ViewModel, Gravity, NativeCall, Text, Color, log, logw, loge, Group, LayoutSpec, } from "doric"
interface CountModel {
count: number
}
2019-12-06 11:41:51 +08:00
class CounterView extends ViewHolder {
2019-12-04 14:07:14 +08:00
number!: Text
counter!: Text
build(root: Group) {
root.addChild(vlayout([
text({
textSize: 40,
layoutConfig: {
alignment: Gravity.Center,
widthSpec: LayoutSpec.WRAP_CONTENT,
heightSpec: LayoutSpec.WRAP_CONTENT,
},
}).also(it => { this.number = it }),
text({
text: "点击计数",
textSize: 20,
border: {
width: 1,
color: Color.parse('#000000'),
},
corners: 5,
layoutConfig: {
alignment: Gravity.Center,
widthSpec: LayoutSpec.WRAP_CONTENT,
heightSpec: LayoutSpec.WRAP_CONTENT,
},
2019-12-04 18:15:41 +08:00
padding: {
left: 10,
right: 10,
top: 10,
bottom: 10,
},
2019-12-04 14:07:14 +08:00
shadow: {
color: Color.parse("#00ff00"),
opacity: 0.5,
radius: 20,
offsetX: 10,
offsetY: 10,
}
}).also(it => { this.counter = it }),
]).also(it => {
it.width = 200
it.height = 200
it.space = 20
it.gravity = Gravity.Center
it.layoutConfig = {
alignment: Gravity.Center
}
it.border = {
width: 1,
color: Color.parse("#000000"),
}
it.shadow = {
color: Color.parse("#ffff00"),
opacity: 0.5,
radius: 20,
offsetX: 10,
offsetY: 10,
}
it.corners = 20
it.backgroundColor = Color.parse('#ff00ff')
}))
root.addChild((new Image).also(iv => {
iv.imageUrl = "https://misc.aotu.io/ONE-SUNDAY/SteamEngine.png"
iv.layoutConfig = {
widthSpec: LayoutSpec.WRAP_CONTENT,
heightSpec: LayoutSpec.WRAP_CONTENT,
}
}))
}
}
class CounterVM extends ViewModel<CountModel, CounterView> {
2019-12-06 11:41:51 +08:00
onAttached(s: CountModel, vh: CounterView) {
2019-12-04 14:07:14 +08:00
vh.counter.onClick = () => {
this.updateState(state => {
state.count++
})
}
}
2019-12-06 11:41:51 +08:00
onBind(s: CountModel, vh: CounterView) {
vh.number.text = `${s.count}`
}
2019-12-04 14:07:14 +08:00
}
@Entry
class MyPage extends VMPanel<CountModel, CounterView>{
getViewHolderClass() {
return CounterView
}
getViewModelClass() {
return CounterVM
}
getState(): CountModel {
return {
count: 0
}
}
@NativeCall
log() {
log("Hello.HEGO")
logw("Hello.HEGO")
loge("Hello.HEGO")
context.modal.toast('This is a toast.').then((r) => {
loge(r)
})
}
}