diff options
| author | ruki <[email protected]> | 2018-01-11 00:44:06 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2018-01-10 22:24:36 +0800 |
| commit | 7973898f7c886f981ef3b27ad62fab129e5c5b03 (patch) | |
| tree | ca2648f40a56553e9876d0e5a732250420bd7185 | |
| parent | 1677f339859edde6e1603dd437a15cc9b2d2e8b0 (diff) | |
redraw ui window
| -rw-r--r-- | tests/ui/desktop.lua | 6 | ||||
| -rw-r--r-- | tests/ui/window.lua | 2 | ||||
| -rw-r--r-- | xmake/core/ui/dialog.lua | 7 | ||||
| -rw-r--r-- | xmake/core/ui/label.lua | 5 | ||||
| -rw-r--r-- | xmake/core/ui/view.lua | 9 | ||||
| -rw-r--r-- | xmake/core/ui/window.lua | 130 |
6 files changed, 67 insertions, 92 deletions
diff --git a/tests/ui/desktop.lua b/tests/ui/desktop.lua index 004cee53f..8323e3d95 100644 --- a/tests/ui/desktop.lua +++ b/tests/ui/desktop.lua @@ -49,13 +49,13 @@ function demo:init() self:menubar():title():text_set("Menu Bar (Hello)") -- add title label - self:desktop():insert(label:new("title", rect {0, 0, 12, 1}, "hello xmake!"), {centerx = true}) + self:desktop():insert(label:new("title", rect {0, 0, 12, 1}, "hello xmake!"):textattr_set("white"), {centerx = true}) -- add yes button - self:desktop():insert(button:new("yes", rect {0, 1, 7, 2}, "< Yes >"), {centerx = true}) + self:desktop():insert(button:new("yes", rect {0, 1, 7, 2}, "< Yes >"):textattr_set("white"), {centerx = true}) -- add no button - self:desktop():insert(button:new("no", rect {0, 2, 6, 3}, "< No >"), {centerx = true}) + self:desktop():insert(button:new("no", rect {0, 2, 6, 3}, "< No >"):textattr_set("white"), {centerx = true}) end -- on event diff --git a/tests/ui/window.lua b/tests/ui/window.lua index 9eabd5cd6..00d04d296 100644 --- a/tests/ui/window.lua +++ b/tests/ui/window.lua @@ -44,7 +44,7 @@ function demo:init() self:background_set("blue") -- init main window - self:insert(window:new("window.main", rect {1, 1, self:width() - 1, self:height() - 1}, "main window")) + self:insert(window:new("window.main", rect {1, 1, self:width() - 1, self:height() - 1}, "main window", true)) end -- main entry diff --git a/xmake/core/ui/dialog.lua b/xmake/core/ui/dialog.lua index 28292c300..dd9b06d1c 100644 --- a/xmake/core/ui/dialog.lua +++ b/xmake/core/ui/dialog.lua @@ -27,6 +27,7 @@ local log = require("ui/log") local rect = require("ui/rect") local label = require("ui/label") local panel = require("ui/panel") +local button = require("ui/button") local window = require("ui/window") local curses = require("ui/curses") @@ -37,7 +38,11 @@ local dialog = dialog or window() function dialog:init(name, bounds, title) -- init window - window.init(self, name, bounds, title) + window.init(self, name, bounds, title, true) + + -- init buttons + local button_yes = button:new("dialog.button.yes", rect {0, self:panel():height() - 1, 7, 1}, "< Yes >") + self:panel():insert(button_yes, {centerx = true}) end -- return module diff --git a/xmake/core/ui/label.lua b/xmake/core/ui/label.lua index aea099e0e..f42260ee1 100644 --- a/xmake/core/ui/label.lua +++ b/xmake/core/ui/label.lua @@ -40,7 +40,7 @@ function label:init(name, bounds, text) self:text_set(text) -- init text attribute - self:textattr_set("white") + self:textattr_set("black") end -- draw view @@ -69,6 +69,7 @@ end function label:text_set(text) self._TEXT = text or "" self:invalidate() + return self end -- get text attribute @@ -78,7 +79,7 @@ end -- set text attribute, .e.g textattr_set("yellow onblue bold") function label:textattr_set(attr) - self:attr_set("textattr", attr) + return self:attr_set("textattr", attr) end -- get the current text attribute value diff --git a/xmake/core/ui/view.lua b/xmake/core/ui/view.lua index e9f0ccb82..18b5898f7 100644 --- a/xmake/core/ui/view.lua +++ b/xmake/core/ui/view.lua @@ -253,6 +253,7 @@ end -- set type function view:type_set(t) self._TYPE = t or "view" + return self end -- get state @@ -266,11 +267,12 @@ function view:state_set(name, enable) -- state is not changed? enable = enable or false if self:state(name) == enable then - return + return self end -- change state self._STATE[name] = enable + return self end -- get option @@ -300,6 +302,7 @@ end function view:attr_set(name, value) self._ATTRS[name] = value self:invalidate() + return self end -- get cursor position @@ -310,6 +313,7 @@ end -- move cursor to the given position function view:cursor_move(x, y) self._CURSOR = point{ self:_limit(x, 0, self:width() - 1), self:_limit(y, 0, self:height() - 1) } + return self end -- show cursor? @@ -317,6 +321,7 @@ function view:cursor_show(visible) if self:state("cursor_visible") ~= visible then self:state_set("cursor_visible", visible) end + return self end -- get background @@ -330,7 +335,7 @@ end -- set background, .e.g background_set("blue") function view:background_set(color) - self:attr_set("background", color) + return self:attr_set("background", color) end -- limit value range diff --git a/xmake/core/ui/window.lua b/xmake/core/ui/window.lua index 69d3d342d..52b946300 100644 --- a/xmake/core/ui/window.lua +++ b/xmake/core/ui/window.lua @@ -25,6 +25,7 @@ -- load modules local log = require("ui/log") local rect = require("ui/rect") +local view = require("ui/view") local label = require("ui/label") local panel = require("ui/panel") local curses = require("ui/curses") @@ -33,7 +34,7 @@ local curses = require("ui/curses") local window = window or panel() -- init window -function window:init(name, bounds, title) +function window:init(name, bounds, title, shadow) -- init panel panel.init(self, name, bounds) @@ -41,79 +42,46 @@ function window:init(name, bounds, title) -- check bounds assert(self:width() > 4 and self:height() > 3, string.format("%s: too small!", self)) - -- init background - self:background_set("white") + -- insert shadow + if shadow then + self:insert(self:shadow()) + self:frame():bounds():movee(-2, -1) + end + + -- insert border + self:frame():insert(self:border()) - -- init title + -- insert panel + self:frame():insert(self:panel()) + + -- insert title if title then self._TITLE = label:new("window.title", rect{0, 0, #title, 1}, title) self:title():textattr_set("blue bold") - self:insert(self:title(), {centerx = true}) + self:frame():insert(self:title(), {centerx = true}) end -- insert frame self:insert(self:frame()) - - -- init shadow - self:shadow_set("black") - - -- init border - self:border_set({"white", "black"}) -end - --- draw window -function window:draw() - - -- draw background - panel.draw(self) - - -- draw shadow - local shadow = self:shadow() - if shadow then - local parent = assert(self:parent()) - self:canvas():attr(curses.color_pair(parent:background(), parent:background())) - self:canvas():move(0, self:height() - 1):putchar(' ', 2) - self:canvas():move(self:width() - 2, 0):putchar(' ', 2) - self:canvas():attr(curses.color_pair(shadow, shadow)) - self:canvas():move(2, self:height() - 1):putchar(' ', self:width() - 2) - self:canvas():move(self:width() - 2, 1):putchar(' ', self:height() - 1, true) - self:canvas():move(self:width() - 1, 1):putchar(' ', self:height() - 1, true) - end - - -- draw border - local border = self:border() - if border then - local fbounds = self:frame():bounds() - - -- draw left and top border - self:canvas():attr({curses.color_pair(border[1], self:frame():background()), "standout"}) - self:canvas():move(0, 0):putchar("hline", fbounds.ex) - self:canvas():move(0, 0):putchar("ulcorner") - self:canvas():move(0, 1):putchar("vline", fbounds.ey - 1, true) - self:canvas():move(0, fbounds.ey):putchar("llcorner") - - -- draw bottom and right border - self:canvas():attr(curses.color_pair(border[2], self:frame():background())) - self:canvas():move(1, fbounds.ey):putchar("hline", fbounds.ex) - self:canvas():move(fbounds.ex, 0):putchar("urcorner") - self:canvas():move(fbounds.ex, 1):putchar("vline", fbounds.ey - 1, true) - self:canvas():move(fbounds.ex, fbounds.ey):putchar("lrcorner") - end - - -- draw title - if self:title() then - label.draw(self:title()) - end end -- get frame function window:frame() if not self._FRAME then - self._FRAME = panel:new("window.panel", rect{0, 0, self:width(), self:height()}) + self._FRAME = panel:new("window.frame", rect{0, 0, self:width(), self:height()}):background_set("white") end return self._FRAME end +-- get panel +function window:panel() + if not self._PANEL then + self._PANEL = panel:new("window.panel", self:frame():bounds()) + self._PANEL:bounds():grow(-1, -1) + end + return self._PANEL +end + -- get title function window:title() return self._TITLE @@ -121,39 +89,35 @@ end -- get shadow function window:shadow() - return self._SHADOW -end - --- set shadow -function window:shadow_set(shadow) - if not self._SHADOW and shadow then - self:frame():bounds():movee(-2, -1) - elseif self._SHADOW and not shadow then - self:frame():bounds():movee(2, 1) + if not self._SHADOW then + self._SHADOW = view:new("window.shadow", rect{2, 1, self:width(), self:height()}):background_set("black") end - self._SHADOW = shadow - self:invalidate() + return self._SHADOW end -- get border function window:border() - return self._BORDER -end + if not self._BORDER then + local border = view:new("window.border", self:frame():bounds()) + function border:draw() --- set border -function window:border_set(border) - -- FIXME cannot draw 'hline' and 'vline' correctly on pdcurses - -- so we disable border now - if os.host() == "windows" then - return - end - if not self._BORDER and border then - self:frame():bounds():grow(-1, -1) - elseif self._BORDER and not border then - self:frame():bounds():grow(1, 1) + -- draw left and top border + self:canvas():attr(curses.color_pair("white", self:background())) + self:canvas():move(0, 0):putchar("hline", self:width()) + self:canvas():move(0, 0):putchar("ulcorner") + self:canvas():move(0, 1):putchar("vline", self:height() - 1, true) + self:canvas():move(0, self:height() - 1):putchar("llcorner") + + -- draw bottom and right border + self:canvas():attr(curses.color_pair("black", self:background())) + self:canvas():move(1, self:height() - 1):putchar("hline", self:width() - 1) + self:canvas():move(self:width() - 1, 0):putchar("urcorner") + self:canvas():move(self:width() - 1, 1):putchar("vline", self:height() - 1, true) + self:canvas():move(self:width() - 1, self:height() - 1):putchar("lrcorner") + end + self._BORDER = border end - self._BORDER = border - self:invalidate() + return self._BORDER end -- return module |
