This commit is contained in:
pengfei.zhou
2019-12-21 21:37:51 +08:00
parent b5cb29ca03
commit 9b5fbedfca
47 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,98 @@
/*
* Copyright [2019] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Modeling } from "./types";
/**
* Store color as format AARRGGBB or RRGGBB
*/
export class Color implements Modeling {
static BLACK = new Color(0xFF000000)
static DKGRAY = new Color(0xFF444444)
static GRAY = new Color(0xFF888888)
static LTGRAY = new Color(0xFFCCCCCC)
static WHITE = new Color(0xFFFFFFFF)
static RED = new Color(0xFFFF0000)
static GREEN = new Color(0xFF00FF00)
static BLUE = new Color(0xFF0000FF)
static YELLOW = new Color(0xFFFFFF00)
static CYAN = new Color(0xFF00FFFF)
static MAGENTA = new Color(0xFFFF00FF)
static TRANSPARENT = new Color(0)
_value: number = 0
constructor(v: number) {
this._value = v | 0x0
}
static parse(str: string) {
if (!str.startsWith("#")) {
throw new Error(`Parse color error with ${str}`)
}
const val = parseInt(str.substr(1), 16)
if (str.length === 7) {
return new Color(val | 0xff000000)
} else if (str.length === 9) {
return new Color(val)
} else {
throw new Error(`Parse color error with ${str}`)
}
}
static safeParse(str: string, defVal: Color = Color.TRANSPARENT) {
let color = defVal
try {
color = Color.parse(str)
} catch (e) {
} finally {
return color
}
}
alpha(v: number) {
v = v * 255
return new Color((this._value & 0xffffff) | ((v & 0xff) << 24))
}
toModel() {
return this._value
}
}
export enum GradientOrientation {
/** draw the gradient from the top to the bottom */
TOP_BOTTOM = 0,
/** draw the gradient from the top-right to the bottom-left */
TR_BL,
/** draw the gradient from the right to the left */
RIGHT_LEFT,
/** draw the gradient from the bottom-right to the top-left */
BR_TL,
/** draw the gradient from the bottom to the top */
BOTTOM_TOP,
/** draw the gradient from the bottom-left to the top-right */
BL_TR,
/** draw the gradient from the left to the right */
LEFT_RIGHT,
/** draw the gradient from the top-left to the bottom-right */
TL_BR,
}
export interface GradientColor {
start: Color
end: Color
orientation: GradientOrientation
}

View File

@@ -0,0 +1,102 @@
/*
* Copyright [2019] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Modeling } from "./types";
const SPECIFIED = 1
const START = 1 << 1
const END = 1 << 2
const SHIFT_X = 0
const SHIFT_Y = 4
export const LEFT = (START | SPECIFIED) << SHIFT_X
export const RIGHT = (END | SPECIFIED) << SHIFT_X
export const TOP = (START | SPECIFIED) << SHIFT_Y
export const BOTTOM = (END | SPECIFIED) << SHIFT_Y
export const CENTER_X = SPECIFIED << SHIFT_X
export const CENTER_Y = SPECIFIED << SHIFT_Y
export const CENTER = CENTER_X | CENTER_Y
export class Gravity implements Modeling {
val = 0
left() {
const val = this.val | LEFT
const ret = new Gravity
ret.val = val
return ret
}
right() {
const val = this.val | RIGHT
const ret = new Gravity
ret.val = val
return ret
}
top() {
const val = this.val | TOP
const ret = new Gravity
ret.val = val
return ret
}
bottom() {
const val = this.val | BOTTOM
const ret = new Gravity
ret.val = val
return ret
}
center() {
const val = this.val | CENTER
const ret = new Gravity
ret.val = val
return ret
}
centerX() {
const val = this.val | CENTER_X
const ret = new Gravity
ret.val = val
return ret
}
centerY() {
const val = this.val | CENTER_Y
const ret = new Gravity
ret.val = val
return ret
}
toModel() {
return this.val
}
private static origin = new Gravity
static Center = Gravity.origin.center()
static Left = Gravity.origin.left()
static Right = Gravity.origin.right()
static Top = Gravity.origin.top()
static Bottom = Gravity.origin.bottom()
}
export function gravity() {
return new Gravity
}

View File

@@ -0,0 +1,21 @@
/*
* Copyright [2019] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export * from './color'
export * from './gravity'
export * from './layoutconfig'
export * from './log'
export * from './types'
export * from './uniqueId'

View File

@@ -0,0 +1,122 @@
/*
* Copyright [2019] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Gravity } from "./gravity";
import { Modeling } from "./types";
export enum LayoutSpec {
/**
* Depends on what's been set on width or height.
*/
JUST = 0,
/**
* Depends on it's content.
*/
FIT = 1,
/**
* Extend as much as parent let it take.
*/
MOST = 2,
}
export interface LayoutConfig {
widthSpec?: LayoutSpec
heightSpec?: LayoutSpec
margin?: {
left?: number,
right?: number,
top?: number,
bottom?: number,
}
alignment?: Gravity
//Only affective in VLayout or HLayout
weight?: number
}
export class LayoutConfigImpl implements LayoutConfig, Modeling {
widthSpec?: LayoutSpec
heightSpec?: LayoutSpec
margin?: {
left?: number,
right?: number,
top?: number,
bottom?: number,
}
alignment?: Gravity
//Only affective in VLayout or HLayout
weight?: number
fit() {
this.widthSpec = LayoutSpec.FIT
this.heightSpec = LayoutSpec.FIT
return this
}
most() {
this.widthSpec = LayoutSpec.MOST
this.heightSpec = LayoutSpec.MOST
return this
}
just() {
this.widthSpec = LayoutSpec.JUST
this.heightSpec = LayoutSpec.JUST
return this
}
configWidth(w: LayoutSpec) {
this.widthSpec = w
return this
}
configHeight(h: LayoutSpec) {
this.heightSpec = h
return this
}
configMargin(m: {
left?: number,
right?: number,
top?: number,
bottom?: number,
}) {
this.margin = m
return this
}
configAligmnet(a: Gravity) {
this.alignment = a
return this
}
configWeight(w: number) {
this.weight = w
return this
}
toModel() {
return {
widthSpec: this.widthSpec,
heightSpec: this.heightSpec,
margin: this.margin,
alignment: this.alignment ? this.alignment.toModel() : undefined,
weight: this.weight,
}
}
}
export function layoutConfig() {
return new LayoutConfigImpl
}

65
doric-js/src/util/log.ts Normal file
View File

@@ -0,0 +1,65 @@
/*
* Copyright [2019] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
declare function nativeLog(type: 'd' | 'w' | 'e', message: string): void
function toString(message: any) {
if (message instanceof Function) {
return message.toString()
} else if (message instanceof Object) {
try {
return JSON.stringify(message)
} catch (e) {
return message.toString()
}
} else if (message === undefined) {
return "undefined"
} else {
return message.toString()
}
}
export function log(...args: any) {
let out = ""
for (let i = 0; i < arguments.length; i++) {
if (i > 0) {
out += ','
}
out += toString(arguments[i])
}
nativeLog('d', out)
}
export function loge(...message: any) {
let out = ""
for (let i = 0; i < arguments.length; i++) {
if (i > 0) {
out += ','
}
out += toString(arguments[i])
}
nativeLog('e', out)
}
export function logw(...message: any) {
let out = ""
for (let i = 0; i < arguments.length; i++) {
if (i > 0) {
out += ','
}
out += toString(arguments[i])
}
nativeLog('w', out)
}

View File

@@ -0,0 +1,71 @@
/*
* Copyright [2019] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export interface Modeling {
toModel(): Model
}
export function obj2Model(obj: Model): Model {
if (obj instanceof Array) {
return obj.map(e => obj2Model(e)) as Model
} else if (obj instanceof Object) {
if (Reflect.has(obj, 'toModel') && Reflect.get(obj, 'toModel') instanceof Function) {
obj = Reflect.apply(Reflect.get(obj, 'toModel'), obj, [])
return obj
} else {
for (let key in obj) {
const val = Reflect.get(obj, key)
Reflect.set(obj, key, obj2Model(val))
}
return obj
}
} else {
return obj
}
}
type _M = string | number | boolean | Modeling | { [index: string]: Model } | undefined
export type Model = _M | Array<_M>
export type Binder<T> = (v: T) => void
export class Mutable<T>{
private val: T
private binders: Set<Binder<T>> = new Set
get = () => {
return this.val
}
set = (v: T) => {
this.val = v
this.binders.forEach(e => {
Reflect.apply(e, undefined, [this.val])
})
}
private constructor(v: T) {
this.val = v
}
bind(binder: Binder<T>) {
this.binders.add(binder)
Reflect.apply(binder, undefined, [this.val])
}
static of<E>(v: E) {
return new Mutable(v)
}
}

View File

@@ -0,0 +1,19 @@
/*
* Copyright [2019] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
let __uniqueId__ = 0
export function uniqueId(prefix: string) {
return `__${prefix}_${__uniqueId__++}__`;
}