|
| 1 | +import { registerComponent } from "../config"; |
| 2 | +import { QPixmap } from "@nodegui/nodegui"; |
| 3 | +import { ViewProps, setProps as setViewProps } from "../View"; |
| 4 | +import { ImageLabel } from "./ImageLabel"; |
| 5 | + |
| 6 | +interface ImageProps extends ViewProps { |
| 7 | + src?: string; |
| 8 | +} |
| 9 | + |
| 10 | +const setProps = ( |
| 11 | + widget: ImageLabel, |
| 12 | + newProps: ImageProps, |
| 13 | + oldProps: ImageProps |
| 14 | +) => { |
| 15 | + const setter: ImageProps = { |
| 16 | + set src(imageUrl: string) { |
| 17 | + const pixMap = new QPixmap(imageUrl); |
| 18 | + widget.setPixmap(pixMap); |
| 19 | + widget.scalePixmap(320, 120); |
| 20 | + } |
| 21 | + }; |
| 22 | + Object.assign(setter, newProps); |
| 23 | + setViewProps(widget, newProps, oldProps); |
| 24 | +}; |
| 25 | + |
| 26 | +export const Image = registerComponent<ImageProps>({ |
| 27 | + id: "image", |
| 28 | + getContext() { |
| 29 | + return {}; |
| 30 | + }, |
| 31 | + shouldSetTextContent: () => { |
| 32 | + return false; |
| 33 | + }, |
| 34 | + createInstance: newProps => { |
| 35 | + const widget = new ImageLabel(); |
| 36 | + setProps(widget, newProps, {}); |
| 37 | + return widget; |
| 38 | + }, |
| 39 | + finalizeInitialChildren: () => { |
| 40 | + return false; |
| 41 | + }, |
| 42 | + commitMount: (instance, newProps, internalInstanceHandle) => { |
| 43 | + return; |
| 44 | + }, |
| 45 | + prepareUpdate: ( |
| 46 | + instance, |
| 47 | + oldProps, |
| 48 | + newProps, |
| 49 | + rootContainerInstance, |
| 50 | + hostContext |
| 51 | + ) => { |
| 52 | + return true; |
| 53 | + }, |
| 54 | + commitUpdate: (instance, updatePayload, oldProps, newProps, finishedWork) => { |
| 55 | + setProps(instance as ImageLabel, newProps, oldProps); |
| 56 | + } |
| 57 | +}); |
0 commit comments