77
88import { ButtonDefaults } from '../constants' ;
99
10-
11-
1210//TODO adjust Opacity when focus, Blur
1311type PressEvent = { || } ;
1412type ButtonProps = { |
1513 title : string ,
1614 onPress : ( event ? : PressEvent ) => mixed ,
17- onClick : ( event ?: SyntheticMouseEvent < T > ) => mixed ,
15+ onClick : ( event ?: SyntheticMouseEvent < HTMLButtonElement > ) => mixed ,
1816 touchSoundDisabled ?: ?boolean ,
1917 color ?: ?string ,
2018 /**
@@ -69,10 +67,10 @@ type ButtonProps = {|
6967
7068function renderButton ( props : ButtonProps , apeContext , parentLayout ) {
7169 //
72- console . log ( '[RENDER]' )
70+ console . log ( '[RENDER]' ) ;
7371 const { spatialGeometry = { x : 0 , y : 0 } } = parentLayout ;
7472 const { ctx} = apeContext ;
75-
73+
7674 // If is relative and x and y haven't be processed, don't render
7775 if ( ! spatialGeometry ) return null ;
7876 // start drawing the canvas
@@ -85,28 +83,27 @@ function renderButton(props: ButtonProps, apeContext, parentLayout) {
8583 let height = ButtonDefaults . containerStyle . height ;
8684 let globalStyle = {
8785 width : width ,
88- height :height ,
86+ height : height ,
8987 color : color ,
90- borderRadius :borderRadius ,
88+ borderRadius : borderRadius ,
9189 backgroundColor,
92- lineWidth :0 ,
93- borderColor : 'transparent'
94- }
95- const resetStyle = ( newStyle ) => {
96- globalStyle = { ...globalStyle , newStyle}
97- console . log ( 'style)))))' , globalStyle )
98- }
99- const redrawButton = ( ctx ) => {
90+ lineWidth : 0 ,
91+ borderColor : 'transparent' ,
92+ } ;
93+ const resetStyle = newStyle => {
94+ globalStyle = { ...globalStyle , newStyle} ;
95+ console . log ( 'style)))))' , globalStyle ) ;
96+ } ;
97+ const redrawButton = ctx => {
10098 // TODO reset Style on focus
101- let newStyle = {
102- lineWidth :2 ,
103- borderColor :'#ccc'
104- }
99+ let newStyle = {
100+ lineWidth : 2 ,
101+ borderColor : '#ccc' ,
102+ } ;
105103 //let prevStyle = globalStyle
106- resetStyle ( newStyle )
104+ resetStyle ( newStyle ) ;
105+ } ;
107106
108- }
109-
110107 ctx . beginPath ( ) ;
111108 ctx . fillStyle = backgroundColor ;
112109 ctx . moveTo ( x , y ) ;
@@ -137,8 +134,8 @@ const resetStyle = (newStyle)=>{
137134 ctx . quadraticCurveTo ( x , y , x + borderRadius , y ) ;
138135
139136 ctx . fill ( ) ;
140- ctx . lineWidth = globalStyle . lineWidth
141- ctx . strokeStyle = globalStyle . borderColor
137+ ctx . lineWidth = globalStyle . lineWidth ;
138+ ctx . strokeStyle = globalStyle . borderColor ;
142139 ctx . stroke ( ) ;
143140 ctx . fillStyle = color || ButtonDefaults . textStyle . color ;
144141 ctx . font = `${ ButtonDefaults . textStyle . fontSize } Helvetica` ;
@@ -147,41 +144,41 @@ const resetStyle = (newStyle)=>{
147144 ctx . fillText ( title , x + width / 2 , y + height / 2 ) ;
148145 ctx . closePath ( ) ;
149146
150- const onClick = ( event :SyntheticMouseEvent < HTMLButtonElement > ) => {
151- const rect = {
147+ const onClick = ( event : SyntheticMouseEvent < HTMLButtonElement > ) => {
148+ const rect = {
152149 x,
153150 y,
154151 height,
155- width
152+ width,
153+ } ;
154+ const mousePosition = trackMousePosition ( ctx . canvas , event ) ;
155+ if ( isInside ( mousePosition , rect ) ) {
156+ redrawButton ( ctx ) ;
157+ if ( props . onClick && typeof props . onClick === 'function' ) {
158+ props . onClick ( event ) ;
159+ }
156160 }
157- const mousePosition = trackMousePosition ( ctx . canvas , event )
158- if ( isInside ( mousePosition , rect ) ) {
159- redrawButton ( ctx )
160- if ( props . onClick && typeof props . onClick === "function" ) {
161- props . onClick ( event )
162- }
163-
164- }
165-
166- }
161+ } ;
167162 function trackMousePosition ( canvas , event ) {
168163 return {
169164 x : event . clientX - canvas . offsetLeft ,
170- y : event . clientY - canvas . offsetTop
165+ y : event . clientY - canvas . offsetTop ,
171166 } ;
172167 }
173- const isInside = ( pos , rect ) => {
174- return pos . x > rect . x && pos . x < rect . x + rect . width && pos . y < rect . y + rect . height && pos . y > rect . y
175- }
176-
177- ctx . canvas . addEventListener ( 'click' , onClick , false )
178- ctx . canvas . addEventListener ( 'focus' , redrawButton )
179- ctx . canvas . addEventListener ( 'blur' , redrawButton )
180-
181- // events
168+ const isInside = ( pos , rect ) => {
169+ return (
170+ pos . x > rect . x &&
171+ pos . x < rect . x + rect . width &&
172+ pos . y < rect . y + rect . height &&
173+ pos . y > rect . y
174+ ) ;
175+ } ;
182176
183-
177+ ctx . canvas . addEventListener ( 'click' , onClick , false ) ;
178+ ctx . canvas . addEventListener ( 'focus' , redrawButton ) ;
179+ ctx . canvas . addEventListener ( 'blur' , redrawButton ) ;
184180
181+ // events
185182}
186183
187184export default function createButtonInstance ( props : ButtonProps ) : mixed {
0 commit comments