summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2018-01-19 22:44:08 +0800
committerruki <[email protected]>2018-01-19 10:24:53 +0800
commit41ad9ed26eec93d72782a6db633124b754fd2258 (patch)
tree0ac6415d22229eaf4bd7e987df8a8ab1e53c9f29
parent7d42cff795e9600a837fc0e595baa4d76ac0838e (diff)
improve input dialog
-rw-r--r--xmake/core/ui/boxdialog.lua1
-rw-r--r--xmake/core/ui/canvas.lua2
-rw-r--r--xmake/core/ui/inputdialog.lua2
-rw-r--r--xmake/core/ui/panel.lua2
-rw-r--r--xmake/core/ui/textarea.lua45
-rw-r--r--xmake/core/ui/textdialog.lua6
-rw-r--r--xmake/core/ui/textedit.lua31
-rw-r--r--xmake/core/ui/window.lua12
8 files changed, 80 insertions, 21 deletions
diff --git a/xmake/core/ui/boxdialog.lua b/xmake/core/ui/boxdialog.lua
index be9282ca3..92ec704e5 100644
--- a/xmake/core/ui/boxdialog.lua
+++ b/xmake/core/ui/boxdialog.lua
@@ -44,6 +44,7 @@ function boxdialog:init(name, bounds, title)
-- resize text
self:text():bounds().ey = 3
self:text():invalidate(true)
+ self:text():option_set("selectable", false)
-- select buttons by default
self:panel():select(self:buttons())
diff --git a/xmake/core/ui/canvas.lua b/xmake/core/ui/canvas.lua
index f705b091a..0de77db18 100644
--- a/xmake/core/ui/canvas.lua
+++ b/xmake/core/ui/canvas.lua
@@ -114,7 +114,7 @@ function canvas:puts(str, startline)
for idx = startline or 1, #str do
self._window:addstr(str[idx])
local _, y = self:pos()
- if y + 1 < ey then
+ if y + 1 < ey and idx < #str then
self:move(sx, y + 1)
else
break
diff --git a/xmake/core/ui/inputdialog.lua b/xmake/core/ui/inputdialog.lua
index 349dc131c..b91070cb1 100644
--- a/xmake/core/ui/inputdialog.lua
+++ b/xmake/core/ui/inputdialog.lua
@@ -26,6 +26,7 @@
local log = require("ui/log")
local rect = require("ui/rect")
local view = require("ui/view")
+local event = require("ui/event")
local curses = require("ui/curses")
local window = require("ui/window")
local textedit = require("ui/textedit")
@@ -46,6 +47,7 @@ function inputdialog:init(name, bounds, title)
-- resize text
self:text():bounds().ey = 1
self:text():invalidate(true)
+ self:text():option_set("selectable", false)
end
-- get textedit
diff --git a/xmake/core/ui/panel.lua b/xmake/core/ui/panel.lua
index a43bf864b..8cacefc90 100644
--- a/xmake/core/ui/panel.lua
+++ b/xmake/core/ui/panel.lua
@@ -258,7 +258,7 @@ function panel:event_on(e)
-- select view?
if e.type == event.ev_keyboard then
- if e.key_name == "Right" or e.key_name == "Tab" then
+ if e.key_name == "Right" then
return self:select_next()
elseif e.key_name == "Left" then
return self:select_prev()
diff --git a/xmake/core/ui/textarea.lua b/xmake/core/ui/textarea.lua
index 4ef3bd878..3554bc02f 100644
--- a/xmake/core/ui/textarea.lua
+++ b/xmake/core/ui/textarea.lua
@@ -41,6 +41,9 @@ function textarea:init(name, bounds, text)
-- mark as selectable
self:option_set("selectable", true)
+ -- enable progress
+ self:option_set("progress", true)
+
-- init start line
self._STARTLINE = 0
self._LINECOUNT = 0
@@ -62,9 +65,11 @@ function textarea:draw(transparent)
end
-- draw progress
- local progress = (self._STARTLINE + math.min(self:height(), self._LINECOUNT)) * 100 / self._LINECOUNT
- if (self._STARTLINE > 0 or progress < 100) and self:width() > 20 then
- self:canvas():move(self:width() - 10, self:height() - 1):puts(string.format("(%%%d)", progress))
+ if self:option("progress") then
+ local progress = (self._STARTLINE + math.min(self:height(), self._LINECOUNT)) * 100 / self._LINECOUNT
+ if (self._STARTLINE > 0 or progress < 100) and self:width() > 20 then
+ self:canvas():move(self:width() - 10, self:height() - 1):puts(string.format("(%%%d)", progress))
+ end
end
end
@@ -75,22 +80,36 @@ function textarea:text_set(text)
return label.text_set(self, text)
end
+-- scroll
+function textarea:scroll(lines)
+ if self._LINECOUNT > self:height() then
+ self._STARTLINE = self._STARTLINE + lines
+ if self._STARTLINE < 0 then
+ self._STARTLINE = 0
+ end
+ if self._STARTLINE > self._LINECOUNT - self:height() then
+ self._STARTLINE = self._LINECOUNT - self:height()
+ end
+ self:invalidate()
+ end
+end
+
+-- scroll to end
+function textarea:scroll_to_end()
+ if self._LINECOUNT > self:height() then
+ self._STARTLINE = self._LINECOUNT - self:height()
+ self:invalidate()
+ end
+end
+
-- on event
function textarea:event_on(e)
if e.type == event.ev_keyboard then
if e.key_name == "Up" then
- self._STARTLINE = self._STARTLINE - 5
- if self._STARTLINE < 0 then
- self._STARTLINE = 0
- end
- self:invalidate()
+ self:scroll(-5)
return true
elseif e.key_name == "Down" then
- self._STARTLINE = self._STARTLINE + 5
- if self._LINECOUNT - self._STARTLINE < self:height() then
- self._STARTLINE = self._LINECOUNT - self:height()
- end
- self:invalidate()
+ self:scroll(5)
return true
end
end
diff --git a/xmake/core/ui/textdialog.lua b/xmake/core/ui/textdialog.lua
index 42dc3f45b..517473c4b 100644
--- a/xmake/core/ui/textdialog.lua
+++ b/xmake/core/ui/textdialog.lua
@@ -56,6 +56,12 @@ end
-- on event
function textdialog:event_on(e)
+
+ -- pass event to dialog
+ if dialog.event_on(self, e) then
+ return true
+ end
+
-- pass keyboard event to text area to scroll
if e.type == event.ev_keyboard then
return self:text():event_on(e)
diff --git a/xmake/core/ui/textedit.lua b/xmake/core/ui/textedit.lua
index f00c8e888..4bc941855 100644
--- a/xmake/core/ui/textedit.lua
+++ b/xmake/core/ui/textedit.lua
@@ -29,33 +29,46 @@ local label = require("ui/label")
local event = require("ui/event")
local border = require("ui/border")
local curses = require("ui/curses")
+local textarea = require("ui/textarea")
-- define module
-local textedit = textedit or label()
+local textedit = textedit or textarea()
-- init textedit
function textedit:init(name, bounds, text)
-- init label
- label.init(self, name, bounds, text)
+ textarea.init(self, name, bounds, text)
-- show cursor
self:cursor_show(true)
-- mark as selectable
self:option_set("selectable", true)
+
+ -- disable progress
+ self:option_set("progress", false)
end
-- draw textedit
function textedit:draw(transparent)
-- draw label
- label.draw(self, transparent)
+ textarea.draw(self, transparent)
- -- TODO
-- move cursor
- local x, y = self:canvas():pos()
- self:cursor_move(x, y)
+ if not self:text() or #self:text() == 0 then
+ self:cursor_move(0, 0)
+ else
+ self:cursor_move(self:canvas():pos())
+ end
+end
+
+-- set text
+function textedit:text_set(text)
+ textarea.text_set(self, text)
+ self:scroll_to_end()
+ return self
end
-- on event
@@ -65,15 +78,21 @@ function textedit:event_on(e)
if e.type == event.ev_keyboard then
if e.key_code > 0x1f and e.key_code < 0x7f then
self:text_set(self:text() .. e.key_name)
+ return true
elseif e.key_name == "Enter" then
self:text_set(self:text() .. '\n')
+ return true
elseif e.key_name == "Backspace" then
local text = self:text()
if #text > 0 then
self:text_set(text:sub(1, #text - 1))
end
+ return true
end
end
+
+ -- do textarea event
+ return textarea.event_on(self, e)
end
-- return module
diff --git a/xmake/core/ui/window.lua b/xmake/core/ui/window.lua
index 7040c6e26..158f3d3f3 100644
--- a/xmake/core/ui/window.lua
+++ b/xmake/core/ui/window.lua
@@ -28,6 +28,7 @@ local rect = require("ui/rect")
local view = require("ui/view")
local label = require("ui/label")
local panel = require("ui/panel")
+local event = require("ui/event")
local border = require("ui/border")
local curses = require("ui/curses")
@@ -106,5 +107,16 @@ function window:border()
return self._BORDER
end
+-- on event
+function window:event_on(e)
+
+ -- select panel?
+ if e.type == event.ev_keyboard then
+ if e.key_name == "Tab" then
+ return self:panel():select_next()
+ end
+ end
+end
+
-- return module
return window