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/js-framework/ui/view.ts

192 lines
3.5 KiB
TypeScript
Raw Normal View History

2019-07-16 14:02:55 +08:00
import { Color, GradientColor } from "../util/color"
2019-07-16 21:05:48 +08:00
import { Modeling, Model } from "../util/types";
import "reflect-metadata"
2019-07-16 14:02:55 +08:00
2019-07-16 21:05:48 +08:00
export function Property(target: Object, propKey: string) {
Reflect.defineMetadata(propKey, true, target)
}
2019-07-16 14:02:55 +08:00
2019-07-16 21:05:48 +08:00
export abstract class View implements Modeling {
2019-07-16 14:02:55 +08:00
@Property
width: number = 0
@Property
height: number = 0
@Property
x: number = 0
@Property
y: number = 0
@Property
bgColor?: Color | GradientColor
@Property
corners?: number | { leftTop?: number; rightTop?: number; leftBottom?: number; rightBottom?: number }
@Property
border?: { width: number; color: Color; }
@Property
shadow?: { color: Color; opacity: number; radius: number; offset: { width: number; height: number; }; }
@Property
alpha?: number
2019-07-16 21:05:48 +08:00
constructor() {
return new Proxy(this, {
get: (target, p) => {
return Reflect.get(target, p)
},
set: (target, p, v) => {
const oldV = Reflect.get(target, p)
const ret = Reflect.set(target, p, v)
if (Reflect.getMetadata(p, target)) {
this.onPropertyChanged(p.toString(), oldV, v)
}
return ret
}
})
}
2019-07-16 14:02:55 +08:00
/** Anchor start*/
get left() {
return this.x
}
set left(v: number) {
this.x = v
}
get right() {
return this.x + this.width
}
set right(v: number) {
this.x = v - this.width
}
get top() {
return this.y
}
set top(v: number) {
this.y = v
}
get bottom() {
return this.y + this.height
}
set bottom(v: number) {
this.y = v - this.height
}
/** Anchor end*/
2019-07-16 21:05:48 +08:00
__dirty_props__: { [index: string]: Model | undefined } = {}
2019-07-16 15:30:46 +08:00
onPropertyChanged(propKey: string, oldV: Model, newV: Model): void {
if (newV instanceof Object
&& Reflect.has(newV, 'toModel')
&& Reflect.get(newV, 'toModel') instanceof Function) {
newV = Reflect.apply(Reflect.get(newV, 'toModel'), newV, [])
}
this.__dirty_props__[propKey] = newV
}
toModel() {
return this.__dirty_props__ || {}
2019-07-16 14:02:55 +08:00
}
2019-07-16 16:51:52 +08:00
@Property
2019-07-16 19:50:12 +08:00
padding?: {
left?: number,
right?: number,
top?: number,
bottom?: number,
}
@Property
config?: Config
}
2019-07-16 14:02:55 +08:00
2019-07-16 19:50:12 +08:00
export enum Alignment {
center = 0,
start,
end,
}
export enum Gravity {
center = 0,
left,
right,
top,
bottom,
}
export interface Config {
margin?: {
left?: number,
right?: number,
top?: number,
bottom?: number,
2019-07-16 14:02:55 +08:00
}
2019-07-16 19:50:12 +08:00
alignment?: Alignment
2019-07-16 14:02:55 +08:00
}
2019-07-16 19:50:12 +08:00
export interface StackConfig extends Config {
}
export interface LayoutConfig extends Config {
weight?: number
}
export abstract class Group extends View {
@Property
children: View[] = []
}
2019-07-16 16:51:52 +08:00
2019-07-16 19:50:12 +08:00
export class Stack extends Group {
@Property
gravity?: number
2019-07-16 16:51:52 +08:00
}
2019-07-16 19:50:12 +08:00
class LinearLayout extends Group {
@Property
space?: number
2019-07-16 16:51:52 +08:00
2019-07-16 19:50:12 +08:00
@Property
gravity?: number
2019-07-16 16:51:52 +08:00
}
2019-07-16 19:50:12 +08:00
export class VLayout extends LinearLayout {
}
2019-07-16 16:51:52 +08:00
2019-07-16 19:50:12 +08:00
export class HLayout extends LinearLayout {
2019-07-16 16:51:52 +08:00
}
2019-07-16 14:02:55 +08:00
export class Text extends View {
2019-07-16 16:51:52 +08:00
@Property
text?: string
2019-07-16 14:02:55 +08:00
2019-07-16 16:51:52 +08:00
@Property
textColor?: Color
@Property
textSize?: number
@Property
maxLines?: number
2019-07-16 14:02:55 +08:00
}
export class Image extends View {
2019-07-16 16:51:52 +08:00
@Property
imageUrl?: string
2019-07-16 14:02:55 +08:00
}
export class List extends View {
}
export class Slide extends View {
}