diff options
| author | ruki <[email protected]> | 2018-01-20 18:32:59 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2018-01-20 18:32:59 +0800 |
| commit | 623cb048114b26cf10b250aad43475f8971a9649 (patch) | |
| tree | 0581609036f22a96a960be0cdb308213b971b722 | |
| parent | ba7696ff7174d8d07e6aec4138b9cce5f0952ca4 (diff) | |
fix canvas putstrss
| -rw-r--r-- | xmake/core/ui/boxdialog.lua | 2 | ||||
| -rw-r--r-- | xmake/core/ui/button.lua | 2 | ||||
| -rw-r--r-- | xmake/core/ui/canvas.lua | 40 | ||||
| -rw-r--r-- | xmake/core/ui/inputdialog.lua | 2 | ||||
| -rw-r--r-- | xmake/core/ui/label.lua | 22 | ||||
| -rw-r--r-- | xmake/core/ui/textarea.lua | 11 | ||||
| -rw-r--r-- | xmake/core/ui/view.lua | 15 |
7 files changed, 72 insertions, 22 deletions
diff --git a/xmake/core/ui/boxdialog.lua b/xmake/core/ui/boxdialog.lua index 65112e65d..ed5a48b9d 100644 --- a/xmake/core/ui/boxdialog.lua +++ b/xmake/core/ui/boxdialog.lua @@ -50,7 +50,7 @@ function boxdialog:init(name, bounds, title) -- text changed self:text():action_set("text.changed", function (v, e) if e.text then - local lines = #e.text:split('\n', true) + local lines = #self:text():splitext(e.text) if lines > 0 and lines < self:height() then self:box():bounds().sy = lines self:text():bounds().ey = lines diff --git a/xmake/core/ui/button.lua b/xmake/core/ui/button.lua index f906e12d8..6d6d19996 100644 --- a/xmake/core/ui/button.lua +++ b/xmake/core/ui/button.lua @@ -72,7 +72,7 @@ function button:draw(transparent) end -- draw text - self:canvas():attr(textattr):move(0, 0):puts(str) + self:canvas():attr(textattr):move(0, 0):putstr(str) end -- on event diff --git a/xmake/core/ui/canvas.lua b/xmake/core/ui/canvas.lua index 0de77db18..bc4bcf27c 100644 --- a/xmake/core/ui/canvas.lua +++ b/xmake/core/ui/canvas.lua @@ -24,6 +24,7 @@ -- load modules local log = require("ui/log") +local point = require("ui/point") local object = require("ui/object") local curses = require("ui/curses") @@ -67,10 +68,22 @@ function canvas:pos() return x, y end --- get the max position -function canvas:maxpos() +-- get the canvas size +function canvas:size() local y, x = self._window:getmaxyx() - return x, y + return point {x + 1, y + 1} +end + +-- get the canvas width +function canvas:width() + local _, x = self._window:getmaxyx() + return x + 1 +end + +-- get the canvas height +function canvas:height() + local y, _ = self._window:getmaxyx() + return y + 1 end -- put character to canvas @@ -101,20 +114,21 @@ function canvas:putchar(ch, n, vertical) end -- put a string to canvas -function canvas:puts(str, startline) - - -- split string first - if type(str) == "string" then - str = str:split('\n', true) - end +function canvas:putstr(str) + self._window:addstr(str) + return self +end - -- draw string +-- put strings to canvas +function canvas:putstrs(strs, startline) + + -- draw strings local sy, sx = self._window:getyx() local ey, _ = self._window:getmaxyx() - for idx = startline or 1, #str do - self._window:addstr(str[idx]) + for idx = startline or 1, #strs do local _, y = self:pos() - if y + 1 < ey and idx < #str then + self._window:addstr(strs[idx]) + if y + 1 < ey and idx < #strs then self:move(sx, y + 1) else break diff --git a/xmake/core/ui/inputdialog.lua b/xmake/core/ui/inputdialog.lua index dc908bc29..d58095292 100644 --- a/xmake/core/ui/inputdialog.lua +++ b/xmake/core/ui/inputdialog.lua @@ -53,7 +53,7 @@ function inputdialog:init(name, bounds, title) -- text changed self:text():action_set("text.changed", function (v, e) if e.text then - local lines = #e.text:split('\n', true) + local lines = #self:text():splitext(e.text) if lines > 0 and lines < self:height() then self:text():bounds().ey = lines self:textedit():bounds().sy = lines diff --git a/xmake/core/ui/label.lua b/xmake/core/ui/label.lua index d2f571c6c..87df818fc 100644 --- a/xmake/core/ui/label.lua +++ b/xmake/core/ui/label.lua @@ -56,7 +56,7 @@ function label:draw(transparent) -- draw text string local str = self:text() if str and #str > 0 and textattr then - self:canvas():attr(textattr):move(0, 0):puts(str) + self:canvas():attr(textattr):move(0, 0):putstrs(self:splitext(str)) end end @@ -116,5 +116,25 @@ function label:textattr_val() return value end +-- split text by width +function label:splitext(text, width) + + -- get width + width = width or self:width() + + -- split text first + local result = {} + local lines = text:split('\n', true) + for idx = 1, #lines do + local line = lines[idx] + while #line > width do + table.insert(result, line:sub(1, width)) + line = line:sub(width + 1) + end + table.insert(result, line) + end + return result +end + -- return module return label diff --git a/xmake/core/ui/textarea.lua b/xmake/core/ui/textarea.lua index 3554bc02f..b11132ae5 100644 --- a/xmake/core/ui/textarea.lua +++ b/xmake/core/ui/textarea.lua @@ -59,16 +59,16 @@ function textarea:draw(transparent) local textattr = self:textattr_val() -- draw text string - local str = self:text() - if str and #str > 0 and textattr then - self:canvas():attr(textattr):move(0, 0):puts(str, self._STARTLINE + 1) + local strs = self._SPLITTEXT + if strs and #strs > 0 and textattr then + self:canvas():attr(textattr):move(0, 0):putstrs(strs, self._STARTLINE + 1) end -- draw 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)) + self:canvas():move(self:width() - 10, self:height() - 1):putstr(string.format("(%%%d)", progress)) end end end @@ -76,7 +76,8 @@ end -- set text function textarea:text_set(text) self._STARTLINE = 0 - self._LINECOUNT = text and #text:split('\n', true) or 0 + self._SPLITTEXT = text and self:splitext(text) or {} + self._LINECOUNT = #self._SPLITTEXT return label.text_set(self, text) end diff --git a/xmake/core/ui/view.lua b/xmake/core/ui/view.lua index 8c3cabaee..d918ff07b 100644 --- a/xmake/core/ui/view.lua +++ b/xmake/core/ui/view.lua @@ -388,6 +388,11 @@ end -- need resize view function view:_mark_resize() + -- have been marked? + if self:state("resize") then + return + end + -- trace log:print("%s: mark as resize", self) @@ -398,6 +403,11 @@ end -- need redraw view function view:_mark_redraw() + -- have been marked? + if self:state("redraw") then + return + end + -- trace log:print("%s: mark as redraw", self) @@ -413,6 +423,11 @@ end -- need refresh view function view:_mark_refresh() + -- have been marked? + if self:state("refresh") then + return + end + -- need refresh it if self:state("visible") then self:state_set("refresh", true) |
