Skip to content

Commit

Permalink
FIX: [TUI] nested view is not blocking
Browse files Browse the repository at this point in the history
```
view [button "Alert" [alert "Hi" print "text"]]
```
  • Loading branch information
qtxie committed Jun 14, 2024
1 parent fe8f031 commit fb266f9
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 45 deletions.
4 changes: 4 additions & 0 deletions modules/view/backends/terminal/ansi-parser.reds
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ ansi-parser: context [

clear-buffer: does [
array/clear buffer
incomplete?: no
pos: 1
]

next-byte: func [return: [logic!]][
Expand Down Expand Up @@ -336,6 +338,8 @@ ansi-parser: context [
]
true [parse-utf8]
]
if end-of-buffer? [exit]

if incomplete? [
if cur > 1 [
move-memory pbuffer pbuffer + cur - 1 end - cur + 1
Expand Down
64 changes: 38 additions & 26 deletions modules/view/backends/terminal/events.reds
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ flags-blk/header: TYPE_BLOCK
last-mouse-evt: 0
mouse-click-delta: 0
mouse-event?: no
mouse-x: as float32! 0
mouse-y: as float32! 0
mouse-x: as float32! 0
mouse-y: as float32! 0
event-loop-cnt: 0

map-pt-from-win: func [
g [widget!]
Expand Down Expand Up @@ -536,10 +537,8 @@ do-mouse-wheel: func [
res
]

exit-loop?: no

post-quit-msg: does [
exit-loop?: yes
event-loop-cnt: event-loop-cnt - 1
]

#define DELTA_TIME 33
Expand All @@ -559,20 +558,28 @@ do-events: func [
tm [time-meter! value]
t delta [integer!]
mouse? [red-logic!]
n-loop [integer!]
reenter? [logic!]
][
if all [
not no-wait?
reenter?: any [
no-wait?
1 < screen/windows-cnt
][return no]
]

n-loop: event-loop-cnt
unless no-wait? [event-loop-cnt: event-loop-cnt + 1]

tty/init
screen/enter-alter-screen
exit-loop?: no
either reenter? [
unless no-wait? [ansi-parser/clear-buffer]
][
tty/init
screen/enter-alter-screen

mouse?: as red-logic! #get system/view/platform/mouse-event?
if mouse?/value <> mouse-event? [
mouse-event?: mouse?/value
either mouse-event? [tty/enable-mouse][tty/disable-mouse]
mouse?: as red-logic! #get system/view/platform/mouse-event?
if mouse?/value <> mouse-event? [
mouse-event?: mouse?/value
either mouse-event? [tty/enable-mouse][tty/disable-mouse]
]
]

t: 0
Expand Down Expand Up @@ -605,22 +612,27 @@ do-events: func [
]
if system/thrown <> 0 [
system/thrown: 0
exit-loop?: yes
post-quit-msg
]

any [no-wait? exit-loop?]
any [no-wait? n-loop >= event-loop-cnt]
]

screen/set-cursor-bottom
if mouse-event? [
mouse-event?: no
tty/disable-mouse
either reenter? [
tty/read-input no ;-- clear stdin queue
ansi-parser/clear-buffer
][
screen/set-cursor-bottom
if mouse-event? [
mouse-event?: no
tty/disable-mouse
]
screen/exit-alter-screen
tty/restore
tty/read-input no ;-- clear stdin queue
screen/reset
tty/show-cursor
]
screen/exit-alter-screen
tty/restore
tty/read-input no ;-- clear stdin queue
screen/reset
tty/show-cursor

msg?
]
5 changes: 2 additions & 3 deletions modules/view/backends/terminal/gui.reds
Original file line number Diff line number Diff line change
Expand Up @@ -779,9 +779,8 @@ OS-destroy-view: func [
empty? [logic!]
][
free-faces face
either zero? screen/windows-cnt [
post-quit-msg
][
post-quit-msg
if 0 < screen/windows-cnt [
screen/redraw null
]
]
Expand Down
6 changes: 4 additions & 2 deletions modules/view/backends/terminal/make-ui.red
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ TUI-helpers: context [
path: text 60 with [text: to-local-file root] return
frame " Folders " [
dirs: text-list 20x20 data (get-files/dir root none) on-key [
if event/key = enter [
if all [event/key = enter dirs/selected][
new: pick dirs/data dirs/selected
root: either new = %../ [first split-path root][append root new]
path/text: to-local-file root
Expand All @@ -189,7 +189,9 @@ TUI-helpers: context [
]
frame " Files " [
files: text-list 35x20 data (get-files root filter) on-key [
if event/key = enter [sfile/text: to-string pick files/data files/selected]
if all [event/key = enter files/selected][
sfile/text: to-string pick files/data files/selected
]
]
] return pad 0x-1

Expand Down
3 changes: 2 additions & 1 deletion modules/view/backends/terminal/screen.reds
Original file line number Diff line number Diff line change
Expand Up @@ -426,9 +426,10 @@ screen: context [
#define ADD_STR(s) [array/append-bytes esc-sequences as byte-ptr! s length? s]

reset-cursor: func [/local s [c-string!] _buf [tiny-str! value]][
tty/write as byte-ptr! "^M" 1 ;-- move to start of the line
if relative-y > 0 [
s: as c-string! :_buf
sprintf [s "^M^[[%dA^[[0J" relative-y] ;-- move left and move up
sprintf [s "^[[%dA^[[0J" relative-y] ;-- move up and erase line
tty/write as byte-ptr! s length? s
relative-y: 0
]
Expand Down
1 change: 1 addition & 0 deletions modules/view/backends/terminal/widgets/button.reds
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ on-button-event: func [
cp: 0
if type = EVT_KEY [
cp: evt/data
if cp = 32 [send-mouse-event EVT_CLICK widget F32_0 F32_0 0] ;-- space key
]

if zero? cp [return 0]
Expand Down
13 changes: 2 additions & 11 deletions modules/view/view.red
Original file line number Diff line number Diff line change
Expand Up @@ -1435,17 +1435,8 @@ insert-event-func 'tab function [face event][
event/type = 'key
event/key = #"^["
][
system/view/platform/exit-event-loop
unview/all
'stop
]
]
insert-event-func 'space [
all [
event/type = 'key
event/key = #" "
switch event/face/type [
button [event/type: 'click]
]
]
event
]
]
4 changes: 2 additions & 2 deletions system/runtime/utils.reds
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ _print: func [
s [c-string!]
c [byte!]
][
#if all [debug? = no GUI-engine = 'terminal][exec/gui/back-to-console]
#if GUI-engine = 'terminal [exec/gui/back-to-console]

until [
switch list/type [
Expand Down Expand Up @@ -112,7 +112,7 @@ _print: func [
zero? count
]
fflush 0
#if all [debug? = no GUI-engine = 'terminal][exec/gui/enter-tui]
#if GUI-engine = 'terminal [exec/gui/enter-tui]
]

;-------------------------------------------
Expand Down

0 comments on commit fb266f9

Please sign in to comment.