From 95aa9b83621a1ebecc57f87dec5760ad17bcaa46 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Fri, 3 Dec 2021 10:35:20 +0800 Subject: [PATCH] Add demo --- doric-demo/src/ColorPaletteDemo.tsx | 235 ++++++++++++++++++++++++++ doric-demo/src/components/SeekBar.tsx | 69 ++++++++ 2 files changed, 304 insertions(+) create mode 100644 doric-demo/src/ColorPaletteDemo.tsx create mode 100644 doric-demo/src/components/SeekBar.tsx diff --git a/doric-demo/src/ColorPaletteDemo.tsx b/doric-demo/src/ColorPaletteDemo.tsx new file mode 100644 index 00000000..f0d93ad3 --- /dev/null +++ b/doric-demo/src/ColorPaletteDemo.tsx @@ -0,0 +1,235 @@ +import { + Panel, + Group, + jsx, + VLayout, + layoutConfig, + Scroller, + Color, + HLayout, + Text, + Gravity, + Stack, + createRef, + ViewHolder, + ViewModel, + VMPanel, + modal, +} from "doric"; +import { SeekBar } from "./components/SeekBar"; +import { colors } from "./utils"; + +interface ColorModel { + color: number; +} + +class ColorVH extends ViewHolder { + colorRef = createRef(); + colorLabelRef = createRef(); + rSeekBar = createRef(); + gSeekBar = createRef(); + bSeekBar = createRef(); + aSeekBar = createRef(); + + build(root: Group) { + + + + + + + R + + + + + + G + + + + + + B + + + + + + A + + + + + ; + } +} + +class ColorVM extends ViewModel { + onAttached(state: ColorModel, vh: ColorVH) { + vh.rSeekBar.current.apply({ + context: this.context, + onProgressChanged: (progress: number) => { + this.updateState((state) => { + state.color = + (state.color & 0xff00ffff) + + ((Math.round(progress * 0xff) & 0xff) << 16); + }); + }, + }); + vh.gSeekBar.current.apply({ + context: this.context, + onProgressChanged: (progress: number) => { + this.updateState((state) => { + state.color = + (state.color & 0xffff00ff) + + ((Math.round(progress * 0xff) & 0xff) << 8); + }); + }, + }); + vh.bSeekBar.current.apply({ + context: this.context, + onProgressChanged: (progress: number) => { + this.updateState((state) => { + state.color = + (state.color & 0xffffff00) + (Math.round(progress * 0xff) & 0xff); + }); + }, + }); + vh.aSeekBar.current.apply({ + context: this.context, + onProgressChanged: (progress: number) => { + this.updateState((state) => { + state.color = + (state.color & 0x00ffffff) + + ((Math.round(progress * 0xff) & 0xff) << 24); + }); + }, + }); + vh.colorLabelRef.current.apply({ + onClick: async () => { + const color = await modal(this.context).prompt({ + title: "Please input color", + }); + this.updateState((state) => { + state.color = parseInt(color, 16); + }); + }, + }); + } + onBind(state: ColorModel, vh: ColorVH) { + const color = state.color; + const a = (color >> 24) & 0xff; + const r = (color >> 16) & 0xff; + + const g = (color >> 8) & 0xff; + + const b = color & 0xff; + vh.colorRef.current.backgroundColor = new Color(color); + vh.colorLabelRef.current.text = `${a.toString(16)}${r.toString( + 16 + )}${g.toString(16)}${b.toString(16)}`.toLocaleUpperCase(); + vh.rSeekBar.current.progress = r / 0xff; + vh.gSeekBar.current.progress = g / 0xff; + vh.bSeekBar.current.progress = b / 0xff; + vh.aSeekBar.current.progress = a / 0xff; + } +} + +@Entry +export class ColorPalettePanel extends VMPanel { + getViewModelClass() { + return ColorVM; + } + getState() { + const val = Math.floor(0xff * 0.3); + let color = val | ((val << 8) | (val << 16) | 0xff000000); + return { + color, + }; + } + getViewHolderClass() { + return ColorVH; + } +} diff --git a/doric-demo/src/components/SeekBar.tsx b/doric-demo/src/components/SeekBar.tsx new file mode 100644 index 00000000..8a126a97 --- /dev/null +++ b/doric-demo/src/components/SeekBar.tsx @@ -0,0 +1,69 @@ +import { + GestureContainer, + ViewComponent, + Stack, + Color, + LayoutSpec, + BridgeContext, + loge, + Panel, +} from "doric"; + +@ViewComponent +export class SeekBar extends GestureContainer { + private content: Stack; + private maxWidth = 0; + context?: BridgeContext; + onProgressChanged?: (progress: number) => void; + + set progress(progress: number) { + if (this.maxWidth === 0) { + Promise.resolve().then(() => { + if (this.context) { + (this.context.entity as Panel).addOnRenderFinishedCallback( + async () => { + this.maxWidth = await this.getWidth(this.context!!); + this.content.width = this.maxWidth * progress; + } + ); + } + }); + } else { + this.content.width = this.maxWidth * progress; + } + } + constructor() { + super(); + this.content = new Stack(); + this.content.layoutConfig = { + widthSpec: LayoutSpec.JUST, + heightSpec: LayoutSpec.MOST, + }; + this.content.backgroundColor = Color.RED; + this.onTouchDown = async (event) => { + if (!this.context) { + return; + } + this.maxWidth = await this.getWidth(this.context); + event.x = Math.max(0, event.x); + this.content.width = event.x; + this.onProgressChanged?.( + Math.max(0, Math.min(1, event.x / this.maxWidth)) + ); + }; + this.onTouchMove = async (event) => { + if (!this.context) { + return; + } + event.x = Math.max(0, event.x); + this.content.width = event.x; + this.onProgressChanged?.( + Math.max(0, Math.min(1, event.x / this.maxWidth)) + ); + }; + this.addChild(this.content); + } + set contentColor(color: Color) { + this.content.backgroundColor = color; + } +}