@@ -10,6 +10,7 @@ import nimx/private/js_vk_map
1010type WebWindow *  =  ref  object  of  Window 
1111  renderingContext: GraphicsContext 
1212  canvasId: string 
13+   textInputCompositionInProgress: bool 
1314
1415proc  fullScreenAvailableAux (): int  {.importwasmraw : """ 
1516  var d = document; 
@@ -304,19 +305,33 @@ method onResize*(w: WebWindow, newSize: Size) =
304305  let  vp =  w.frame.size *  p
305306  sharedGL ().viewport (0 , 0 , GLSizei GLsizei 
306307
307- proc  startTextInputAux (cb: proc (a: JSRef ) {.cdecl .}) {.importwasmraw : """ 
308-   if (window.__nimx_textinput === undefined) { 
309-     var i = window.__nimx_textinput = document.createElement('input'); 
310-     i.type = 'text'; 
311-     i.style.position = 'absolute'; 
312-     i.style.top = '-99999px'; 
313-     document.body.appendChild(i) 
314-   } 
315-   window.__nimx_textinput.oninput = () => { 
316-     _nime._dvi($0, _nimok(window.__nimx_textinput.value)); 
317-     window.__nimx_textinput.value = "" 
318-   }; 
319-   setTimeout(() => window.__nimx_textinput.focus(), 1) 
308+ proc  startTextInputAux (cb: proc (e: int32 , a: JSRef ) {.cdecl .}, x, y, w, h: float32 , canvasId: cstring ) {.importwasmraw : """ 
309+   if (window.__nimx_textinput) window.__nimx_textinput.remove(); 
310+   var d = document, i = window.__nimx_textinput = d.createElement('input'), 
311+   c = d.getElementById(_nimsj($5)), 
312+   l = (n, k) => i.addEventListener(n, e => _nime._dvii($0, k, _nimok(e.data))); 
313+ 
314+   i.type = 'text'; 
315+ 
316+   Object.assign(i.style, { 
317+     position: 'absolute', 
318+     left: (c.offsetLeft + $1) + 'px', 
319+     top: (c.offsetTop + $2) + 'px', 
320+     width: $3 + 'px', 
321+     height: $4 + 'px', 
322+     opacity: '0',            // Fully transparent 
323+     pointerEvents: 'none',   // Not interactive 
324+     zIndex: 10               // Above canvas 
325+   }); 
326+ 
327+   d.body.appendChild(i); 
328+ 
329+   l("input", 0); 
330+   l("compositionupdate", 1); 
331+   l("compositionstart", 2); 
332+   l("compositionend", 3); 
333+ 
334+   setTimeout(() => i.focus(), 1) 
320335""" .}
321336
322337proc  length (j: JSObj ): int  {.importwasmp .}
@@ -330,29 +345,45 @@ proc jsStringToStr(v: JSObj): string =
330345      let  actualSz =  strWriteOut (v, addr  result [0 ], sz)
331346      result .setLen (actualSz)
332347
333- proc  onInput (a: JSRef ) {.cdecl .} = 
334-   var  e =  newEvent (etTextInput)
335-   e.text =  block :
348+ proc  onInput (eventKind: int32 , text: JSRef ) {.cdecl .} = 
349+   let  text =  block :
336350    #  Force JSRef destruction early
337-     jsStringToStr (JSObj 
338- 
351+     jsStringToStr (JSObj 
339352  let  a =  mainApplication ()
340-   e.window =  a.keyWindow
341-   if  not  e.window.isNil:
342-     discard  a.handleEvent (e)
353+   let  w =  WebWindow 
354+   var  e =  newEvent (etTextInput)
355+   e.text =  text
356+   e.window =  w
357+ 
358+   if  not  w.isNil:
359+     case  eventKind
360+     of  0 , 1 : #  0 = input, 1 = compositionupdate
361+       if  eventKind ==  0 :
362+         if  not  w.textInputCompositionInProgress:
363+           discard  a.handleEvent (e)
364+       else :
365+         e.kind =  etTextEditing
366+         discard  a.handleEvent (e)
367+ 
368+     of  2 : #  compositionstart
369+       w.textInputCompositionInProgress =  true 
370+     else : #  compositionend
371+       w.textInputCompositionInProgress =  false 
372+       discard  a.handleEvent (e)
343373
344374method  startTextInput * (w: WebWindow , r: Rect ) = 
345-   defineDyncall (" vi " 
346-   startTextInputAux (onInput)
375+   defineDyncall (" vii " 
376+   startTextInputAux (onInput, r.x, r.y, r.width, r.height, w.canvasId )
347377
348378proc  stopTextInputAux () {.importwasmraw : """ 
349-   if (window.__nimx_textinput !== undefined ) { 
350-     window.__nimx_textinput.oninput = null ; 
351-     window.__nimx_textinput.blur()  
379+   if (window.__nimx_textinput) { 
380+     window.__nimx_textinput.remove() ; 
381+     window.__nimx_textinput = null  
352382  } 
353383""" .}
354384
355385method  stopTextInput * (w: WebWindow ) = 
386+   w.textInputCompositionInProgress =  false 
356387  stopTextInputAux ()
357388
358389#  window.onload = () => _nime._dv(p)
0 commit comments