From a9ddeb9f96401b00b1489e6ba1fd1bcea319ffe7 Mon Sep 17 00:00:00 2001 From: Eric Mastro Date: Wed, 7 Jul 2021 14:16:28 +1000 Subject: [PATCH] WIP: experiment with creating a modal window Creation of the modal window works, however there needs to be some keyboard capture that does not put the keyboard input in to the main input box. --- examples/chat/tui/commands.nim | 3 ++- examples/chat/tui/common.nim | 3 +++ examples/chat/tui/screen.nim | 47 ++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/examples/chat/tui/commands.nim b/examples/chat/tui/commands.nim index 8cb4ea7..685cb3e 100644 --- a/examples/chat/tui/commands.nim +++ b/examples/chat/tui/commands.nim @@ -141,7 +141,8 @@ proc command*(self: ChatTUI, command: Login) {.async, gcsafe, nimcall.} = account = parseInt(command.account) password = command.password - asyncSpawn self.client.login(account, password) + self.showModal("Enter password:") + # asyncSpawn self.client.login(account, password) except: self.wprintFormatError(epochTime().int64, "invalid login arguments.") diff --git a/examples/chat/tui/common.nim b/examples/chat/tui/common.nim index db3251d..b611c1b 100644 --- a/examples/chat/tui/common.nim +++ b/examples/chat/tui/common.nim @@ -19,8 +19,11 @@ type inputReady*: bool inputWin*: PWindow inputWinBox*: PWindow + isModalShown*: bool locale*: string mainWin*: PWindow + modalWin*: PWindow + modalWinBox*: PWindow mouse*: bool outputReady*: bool running*: bool diff --git a/examples/chat/tui/screen.nim b/examples/chat/tui/screen.nim index 39b3c20..4c1c523 100644 --- a/examples/chat/tui/screen.nim +++ b/examples/chat/tui/screen.nim @@ -93,6 +93,34 @@ proc drawInputWin*(self: ChatTUI) = self.inputWin = subwin(self.inputWinBox, ((LINES.float64 * 0.2).int - 3).cint, COLS - 2, ((LINES.float64 * 0.8).int + 2).cint, 1) +proc drawModalWin*(self: ChatTUI) = + # create subwindow for the input box + self.modalWinBox = subwin(self.mainWin, + ((LINES.float64 * 0.333).int - 1).cint, + (COLS.float64 * 0.333).cint, + ((LINES.float64 * 0.333).int + 1).cint, + (COLS.float64 * 0.333).cint) + box(self.modalWinBox, 0, 0) + + # draw subwindow + # Must call `touchwin` before calling `subwin` + # See https://invisible-island.net/ncurses/man/curs_window.3x.html#h3-subwin + # for more info. + touchwin(self.mainWin) + wrefresh(self.modalWinBox) + + # create sub subwindow to hold input text + self.modalWin = subwin(self.modalWinBox, + ((LINES.float64 * 0.333).int - 3).cint, + ((COLS.float64 * 0.333).int - 2).cint, + ((LINES.float64 * 0.333).int + 2).cint, + ((COLS.float64 * 0.333).int + 1).cint) + # Must call `touchwin` before calling `subwin` + # See https://invisible-island.net/ncurses/man/curs_window.3x.html#h3-subwin + # for more info. + touchwin(self.modalWinBox) + wrefresh(self.modalWin) + proc drawTermTooSmall*(self: ChatTUI) = wbkgd(self.mainWin, COLOR_PAIR(8).chtype) wattron(self.mainWin, A_BOLD.cint) @@ -296,6 +324,25 @@ proc printResult*(self: ChatTUI, message: string, timestamp: int64) = trace "TUI printed in message window", message +proc showModal*(self: ChatTUI, message: string) = + self.drawModalWin() + + # must move cursor to the window before writing to it + wcursyncup(self.modalWin) + + # print message + wattron(self.modalWin, COLOR_PAIR(1).cint) + wprintw(self.modalWin, fmt("{message}\n")) + wattroff(self.modalWin, COLOR_PAIR(1).cint) + + wrefresh(self.modalWin) + + self.isModalShown = true + +proc hideModal*(self: ChatTUI) = + self.resizeScreen() + self.isModalShown = false + proc wprintFormatNotice(self: ChatTUI, win: PWindow, timestamp: int64, notice: string) = discard