about summary refs log tree commit diff stats
path: root/src/buffer
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2022-12-01 08:59:44 +0100
committerbptato <nincsnevem662@gmail.com>2022-12-01 08:59:44 +0100
commitb1794c0961a5efb30a0dafa34845bedf49261819 (patch)
tree9f2a0a4770cc3644d435c2bfc2168fce06204838 /src/buffer
parent8bb35b0ea47eb91cc2703e27ca94c07b6a5cb9a3 (diff)
downloadchawan-b1794c0961a5efb30a0dafa34845bedf49261819.tar.gz
Fix some screen update issues, remove redraw timer
For now it's unnecessary; may be re-added later to support some dynamic
document features.
Diffstat (limited to 'src/buffer')
-rw-r--r--src/buffer/buffer.nim37
-rw-r--r--src/buffer/container.nim21
2 files changed, 34 insertions, 24 deletions
diff --git a/src/buffer/buffer.nim b/src/buffer/buffer.nim
index 9049bc24..ef2ce5fd 100644
--- a/src/buffer/buffer.nim
+++ b/src/buffer/buffer.nim
@@ -794,15 +794,17 @@ proc submitForm(form: HTMLFormElement, submitter: Element): Option[Request] =
       assert formmethod == FORM_METHOD_POST
       getActionUrl
 
-template set_focus(buffer: Buffer, e: Element) =
+proc setFocus(buffer: Buffer, e: Element): bool =
   if buffer.document.focus != e:
     buffer.document.focus = e
     buffer.do_reshape()
+    return true
 
-template restore_focus(buffer: Buffer) =
+proc restoreFocus(buffer: Buffer): bool =
   if buffer.document.focus != nil:
     buffer.document.focus = nil
     buffer.do_reshape()
+    return true
 
 type ReadSuccessResult* = object
   open*: Option[Request]
@@ -841,7 +843,9 @@ proc readSuccess*(buffer: Buffer, s: string): ReadSuccessResult {.proxy.} =
       buffer.do_reshape()
       result.repaint = true
     else: discard
-    buffer.restore_focus
+    let r = buffer.restoreFocus()
+    if not result.repaint:
+      result.repaint = r
 
 type ReadLineResult* = object
   prompt*: string
@@ -859,10 +863,9 @@ proc click*(buffer: Buffer, cursorx, cursory: int): ClickResult {.proxy.} =
   if clickable != nil:
     case clickable.tagType
     of TAG_SELECT:
-      buffer.set_focus clickable
-      result.repaint = true
+      result.repaint = buffer.setFocus(clickable)
     of TAG_A:
-      buffer.restore_focus
+      result.repaint = buffer.restoreFocus()
       let url = parseUrl(HTMLAnchorElement(clickable).href, clickable.document.baseUrl.some)
       if url.issome:
         result.open = some(newRequest(url.get, HTTP_GET))
@@ -876,10 +879,10 @@ proc click*(buffer: Buffer, cursorx, cursory: int): ClickResult {.proxy.} =
             for option in select.options:
               option.selected = false
           option.selected = true
-          buffer.restore_focus
+          result.repaint = buffer.restoreFocus()
         else:
           # focus on select
-          buffer.set_focus select
+          result.repaint = buffer.setFocus(select)
     of TAG_BUTTON:
       let button = HTMLButtonElement(clickable)
       if button.form != nil:
@@ -891,31 +894,31 @@ proc click*(buffer: Buffer, cursorx, cursory: int): ClickResult {.proxy.} =
           buffer.do_reshape()
         of BUTTON_BUTTON: discard
     of TAG_TEXTAREA:
-      buffer.set_focus clickable
+      result.repaint = buffer.setFocus(clickable)
       let textarea = HTMLTextAreaElement(clickable)
       result.readline = some(ReadLineResult(
         value: textarea.value,
         area: true
       ))
     of TAG_INPUT:
-      buffer.restore_focus
+      result.repaint = buffer.restoreFocus()
       let input = HTMLInputElement(clickable)
       case input.inputType
       of INPUT_SEARCH:
-        buffer.set_focus input
+        result.repaint = buffer.setFocus(input)
         result.readline = some(ReadLineResult(
           prompt: "SEARCH: ",
           value: input.value
         ))
       of INPUT_TEXT, INPUT_PASSWORD:
-        buffer.set_focus input
+        result.repaint = buffer.setFocus(input)
         result.readline = some(ReadLineResult(
           prompt: "TEXT: ",
           value: input.value,
           hide: input.inputType == INPUT_PASSWORD
         ))
       of INPUT_FILE:
-        buffer.set_focus input
+        result.repaint = buffer.setFocus(input)
         var path = if input.file.issome:
           input.file.get.path.serialize_unicode()
         else:
@@ -946,12 +949,12 @@ proc click*(buffer: Buffer, cursorx, cursory: int): ClickResult {.proxy.} =
         if input.form != nil:
           result.open = submitForm(input.form, input)
       else:
-        buffer.restore_focus
+        result.repaint = buffer.restoreFocus()
     else:
-      buffer.restore_focus
+      result.repaint = buffer.restoreFocus()
 
-proc readCanceled*(buffer: Buffer) {.proxy.} =
-  buffer.restore_focus
+proc readCanceled*(buffer: Buffer): bool {.proxy.} =
+  return buffer.restoreFocus()
 
 proc findAnchor*(buffer: Buffer, anchor: string): bool {.proxy.} =
   return buffer.document != nil and buffer.document.getElementById(anchor) != nil
diff --git a/src/buffer/container.nim b/src/buffer/container.nim
index 63f867ad..dd76f449 100644
--- a/src/buffer/container.nim
+++ b/src/buffer/container.nim
@@ -270,13 +270,12 @@ proc requestLines*(container: Container, w = container.lineWindow) =
       container.lines[y] = res.lines[y]
     if res.numLines != container.numLines:
       container.setNumLines(res.numLines, true)
-    container.redraw = true
     let cw = container.fromy ..< container.fromy + container.height
     if w.a in cw or w.b in cw or cw.a in w or cw.b in w:
       container.triggerEvent(UPDATE))
 
 proc redraw*(container: Container) {.jsfunc.} =
-  container.redraw = true
+  container.triggerEvent(UPDATE)
 
 proc sendCursorPosition*(container: Container) =
   container.iface.updateHover(container.cursorx, container.cursory).then(proc(res: UpdateHoverResult) =
@@ -290,7 +289,7 @@ proc setFromY*(container: Container, y: int) {.jsfunc.} =
   if container.pos.fromy != y:
     container.pos.fromy = max(min(y, container.maxfromy), 0)
     container.needslines = true
-    container.redraw = true
+    container.triggerEvent(UPDATE)
 
 proc setFromX*(container: Container, x: int) {.jsfunc.} =
   if container.pos.fromx != x:
@@ -298,7 +297,7 @@ proc setFromX*(container: Container, x: int) {.jsfunc.} =
     if container.pos.fromx > container.cursorx:
       container.pos.cursorx = min(container.pos.fromx, container.currentLineWidth())
       container.sendCursorPosition()
-    container.redraw = true
+    container.triggerEvent(UPDATE)
 
 proc setFromXY*(container: Container, x, y: int) {.jsfunc.} =
   container.setFromY(y)
@@ -530,8 +529,8 @@ proc pushCursorPos*(container: Container) =
 
 proc popCursorPos*(container: Container, nojump = false) =
   container.pos = container.bpos.pop()
-  container.updateCursor()
   if not nojump:
+    container.updateCursor()
     container.sendCursorPosition()
     container.needslines = true
 
@@ -569,7 +568,13 @@ proc cursorNextMatch*(container: Container, regex: Regex, wrap: bool) {.jsfunc.}
           let ex = res.x + res.str.width() - 1
           let hl = Highlight(x: res.x, y: res.y, endx: ex, endy: res.y, clear: true)
           container.highlights.add(hl)
-          container.hlon = false)
+          container.triggerEvent(UPDATE)
+          container.hlon = false
+      elif container.hlon:
+        container.clearSearchHighlights()
+        container.triggerEvent(UPDATE)
+        container.needslines = true
+        container.hlon = false)
 
 proc cursorPrevMatch*(container: Container, regex: Regex, wrap: bool) {.jsfunc.} =
   container.iface
@@ -654,7 +659,9 @@ proc findAnchor*(container: Container, anchor: string) =
       container.triggerEvent(NO_ANCHOR))
 
 proc readCanceled*(container: Container) =
-  container.iface.readCanceled()
+  container.iface.readCanceled().then(proc(repaint: bool) =
+    if repaint:
+      container.needslines = true)
 
 proc readSuccess*(container: Container, s: string) =
   container.iface.readSuccess(s).then(proc(res: ReadSuccessResult) =