summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2017-12-27 21:03:29 +0800
committerruki <[email protected]>2017-12-27 00:20:57 +0800
commit663308b956fb9cedbd002e980d716818f0b5e7b2 (patch)
treebc0e3aee5a547db50ff61d3a7cb793d2f7f36714
parent603f29d146a735671c90d7430e7e5a200b749f74 (diff)
improve menubar and statusbar
-rw-r--r--tests/ui/hello.lua4
-rw-r--r--xmake/core/ui/curses.lua29
-rw-r--r--xmake/core/ui/desktop.lua14
-rw-r--r--xmake/core/ui/event.lua12
-rw-r--r--xmake/core/ui/group.lua34
-rw-r--r--xmake/core/ui/label.lua53
-rw-r--r--xmake/core/ui/menubar.lua43
-rw-r--r--xmake/core/ui/program.lua1
-rw-r--r--xmake/core/ui/statusbar.lua45
-rw-r--r--xmake/core/ui/view.lua21
10 files changed, 139 insertions, 117 deletions
diff --git a/tests/ui/hello.lua b/tests/ui/hello.lua
index f52d71230..776cee75a 100644
--- a/tests/ui/hello.lua
+++ b/tests/ui/hello.lua
@@ -40,7 +40,7 @@ function hello:init()
application.init(self, "hello")
-- init title
- self:menubar():title_set("Menu Bar (Hello)")
+ 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})
@@ -50,7 +50,7 @@ end
function hello:event_on(e)
view.event_on(self, e)
if e.type == event.ev_keyboard then
- self:statusbar():info_set(e.key_name)
+ self:statusbar():info():text_set(e.key_name)
if e.key_name == "s" then
self:statusbar():show(not self:statusbar():state("visible"))
elseif e.key_name == "m" then
diff --git a/xmake/core/ui/curses.lua b/xmake/core/ui/curses.lua
index b856bdc40..77cb004f6 100644
--- a/xmake/core/ui/curses.lua
+++ b/xmake/core/ui/curses.lua
@@ -48,6 +48,12 @@ function curses.color(name)
end
end
+-- is color?
+local colors = {black = true, red = true, green = true, yellow = true, blue = true, magenta = true, cyan = true, white = true}
+function curses.iscolor(name)
+ return colors[name] or colors[name:sub(3) or ""]
+end
+
-- get attr from the given name
function curses.attr(name)
if name == 'normal' then return curses.A_NORMAL
@@ -108,6 +114,9 @@ end
-- calculate attr from the attributes list
--
-- local attr = curses.calc_attr("bold")
+-- local attr = curses.calc_attr("yellow")
+-- local attr = curses.calc_attr{ "yellow", "ongreen" }
+-- local attr = curses.calc_attr{ "yellow", "ongreen", "bold" }
-- local attr = curses.calc_attr{ curses.color_pair("yellow", "green"), "bold" }
--
function curses.calc_attr(attrs)
@@ -118,22 +127,42 @@ function curses.calc_attr(attrs)
if atype == "number" then
return attrs
-- curses.calc_attr("bold")
+ -- curses.calc_attr("yellow")
elseif atype == "string" then
+ if curses.iscolor(attrs) then
+ local color = attrs
+ if color:startswith("on") then
+ color = color:sub(3)
+ end
+ return curses.color_pair(color, color)
+ end
return curses.attr(attrs)
+ -- curses.calc_attr{ "yellow", "ongreen", "bold" }
-- curses.calc_attr{ curses.color_pair("yellow", "green"), "bold" }
elseif atype == "table" then
local v = 0
local set = {}
+ local fg = nil
+ local bg = nil
for _, a in ipairs(attrs) do
if not set[a] and a then
set[a] = true
if type(a) == "number" then
v = v + a
+ elseif curses.iscolor(a) then
+ if a:startswith("on") then
+ bg = a:sub(3)
+ else
+ fg = a
+ end
else
v = v + curses.attr(a)
end
end
end
+ if fg or bg then
+ v = v + curses.color_pair(fg or bg, bg or fg)
+ end
return v
else
return 0
diff --git a/xmake/core/ui/desktop.lua b/xmake/core/ui/desktop.lua
index 85b0053eb..3682a230f 100644
--- a/xmake/core/ui/desktop.lua
+++ b/xmake/core/ui/desktop.lua
@@ -39,19 +39,7 @@ function desktop:init(name, bounds)
group.init(self, name, bounds)
-- init background
- self:background_set(curses.color_pair("white", "blue"))
-end
-
--- exit desktop
-function desktop:exit()
- group.exit(self)
-end
-
--- draw desktop
-function desktop:draw()
-
- -- draw background
- group.draw(self)
+ self:background_set("blue")
end
-- return module
diff --git a/xmake/core/ui/event.lua b/xmake/core/ui/event.lua
index 6fab1c4f4..2e43a4ca7 100644
--- a/xmake/core/ui/event.lua
+++ b/xmake/core/ui/event.lua
@@ -28,6 +28,7 @@ $Id: event.lua 18 2007-06-21 20:43:52Z tngd $
--------------------------------------------------------------------------]]
-- load modules
+local log = require("ui/log")
local object = require("ui/object")
-- define module
@@ -54,6 +55,17 @@ function event:is_command(command)
return self.type == event.ev_command and self.command == command
end
+-- dump event
+function event:dump()
+ if self.type == event.ev_keyboard then
+ log:print("event(key): %s %s ..", self.key_name, self.key_code)
+ elseif self.type == event.ev_command then
+ log:print("event(cmd): %s ..", self.command)
+ else
+ log:print("event(%s): ..", self.type)
+ end
+end
+
-- register event types, event.ev_keyboard = 1, event.ev_mouse = 2, ... , event.ev_idle = 4, event.ev_max = 4
event:register("ev_max", "ev_keyboard", "ev_mouse", "ev_command", "ev_idle")
diff --git a/xmake/core/ui/group.lua b/xmake/core/ui/group.lua
index fada01444..571064260 100644
--- a/xmake/core/ui/group.lua
+++ b/xmake/core/ui/group.lua
@@ -246,33 +246,21 @@ end
-- draw group
function group:draw()
- -- draw it
- if self:state("redraw") then
+ -- redraw group?
+ local redraw = self:state("redraw")
- -- draw group background first
+ -- draw group background first
+ if redraw then
view.draw(self)
+ end
- -- draw all child views
- for v in self:views() do
- if v:state("visible") then
- v:draw()
- v:state_set("redraw", false)
- v:_mark_refresh()
- end
+ -- draw all child views
+ for v in self:views() do
+ if redraw then
+ v:state_set("redraw", true)
end
-
- -- clear mark
- self:state_set("redraw", false)
- self:_mark_refresh()
- else
-
- -- only draw child views
- for v in self:views() do
- if v:state("visible") and v:state("redraw") then
- v:draw()
- v:state_set("redraw", false)
- v:_mark_refresh()
- end
+ if v:state("visible") and v:state("redraw") then
+ v:draw()
end
end
end
diff --git a/xmake/core/ui/label.lua b/xmake/core/ui/label.lua
index 3c443ed91..7cf6741d6 100644
--- a/xmake/core/ui/label.lua
+++ b/xmake/core/ui/label.lua
@@ -39,13 +39,8 @@ function label:init(name, bounds, text)
-- init text
self:text_set(text)
- -- init background
- self:background_set(curses.color_pair("white", "blue"))
-end
-
--- exit label
-function label:exit()
- view.exit(self)
+ -- init text attribute
+ self:textattr_set("white")
end
-- draw view
@@ -54,11 +49,14 @@ function label:draw()
-- draw background
view.draw(self)
+ -- get the text attribute value
+ local textattr = self:textattr_val()
+
-- strip text string
local str = self:text()
- if str and #str > 0 then
+ if str and #str > 0 and textattr then
str = string.sub(str, 1, self:width())
- self:canvas():move(0, 0):write(str)
+ self:canvas():attr(textattr):move(0, 0):write(str)
end
end
@@ -73,5 +71,42 @@ function label:text_set(text)
self:invalidate()
end
+-- get text attribute
+function label:textattr()
+ return self:attr("textattr")
+end
+
+-- set text attribute, .e.g textattr_set("yellow onblue bold")
+function label:textattr_set(attr)
+ self:attr_set("textattr", attr)
+end
+
+-- get the current text attribute value
+function label:textattr_val()
+
+ -- get text attribute
+ local textattr = self:textattr()
+ if not textattr then
+ return
+ end
+
+ -- no text background? use view's background
+ if self:background() and not textattr:find("on") then
+ textattr = textattr .. " on" .. self:background()
+ end
+
+ -- attempt to get the attribute value from the cache first
+ self._TEXTATTR = self._TEXTATTR or {}
+ local value = self._TEXTATTR[textattr]
+ if value then
+ return value
+ end
+
+ -- update the cache
+ value = curses.calc_attr(textattr:split("%s+"))
+ self._TEXTATTR[textattr] = value
+ return value
+end
+
-- return module
return label
diff --git a/xmake/core/ui/menubar.lua b/xmake/core/ui/menubar.lua
index 356a70f9e..6fdcad24f 100644
--- a/xmake/core/ui/menubar.lua
+++ b/xmake/core/ui/menubar.lua
@@ -24,43 +24,28 @@
-- load modules
local log = require("ui/log")
-local view = require("ui/view")
+local rect = require("ui/rect")
+local label = require("ui/label")
+local group = require("ui/group")
local curses = require("ui/curses")
-- define module
-local menubar = menubar or view()
+local menubar = menubar or group()
-- init menubar
function menubar:init(name, bounds)
- -- init view
- view.init(self, name, bounds)
+ -- init group
+ group.init(self, name, bounds)
-- init title
- self:title_set("Menu Bar")
+ self._TITLE = label:new("menubar.title", rect{0, 0, self:width(), self:height()})
+ self:insert(self:title())
+ self:title():text_set("Menu Bar")
+ self:title():textattr_set("red")
-- init background
- self:background_set(curses.color_pair("red", "white"))
-end
-
--- exit menubar
-function menubar:exit()
- view.exit(self)
-end
-
--- draw view
-function menubar:draw()
-
- -- draw background
- view.draw(self)
-
- -- draw it
- self:canvas():move(1, 0):write(self:title())
-end
-
--- en event
-function menubar:event_on(e)
- view.event_on(self, e)
+ self:background_set("white")
end
-- get title
@@ -68,11 +53,5 @@ function menubar:title()
return self._TITLE
end
--- set title
-function menubar:title_set(title)
- self._TITLE = title or ""
- self:invalidate()
-end
-
-- return module
return menubar
diff --git a/xmake/core/ui/program.lua b/xmake/core/ui/program.lua
index a79d2af51..16518d357 100644
--- a/xmake/core/ui/program.lua
+++ b/xmake/core/ui/program.lua
@@ -176,6 +176,7 @@ function program:loop(argv)
-- do event
if e then
+ event.dump(e)
self:event_on(e)
sleep = false
else
diff --git a/xmake/core/ui/statusbar.lua b/xmake/core/ui/statusbar.lua
index 18688e86f..c0e0814dc 100644
--- a/xmake/core/ui/statusbar.lua
+++ b/xmake/core/ui/statusbar.lua
@@ -24,44 +24,29 @@
-- load modules
local log = require("ui/log")
-local view = require("ui/view")
+local rect = require("ui/rect")
+local group = require("ui/group")
+local label = require("ui/label")
local event = require("ui/event")
local curses = require("ui/curses")
-- define module
-local statusbar = statusbar or view()
+local statusbar = statusbar or group()
-- init statusbar
-function statusbar:init(name, bounds, commands)
+function statusbar:init(name, bounds)
- -- init view
- view.init(self, name, bounds)
+ -- init group
+ group.init(self, name, bounds)
-- init info
- self:info_set("")
+ self._INFO = label:new("statusbar.info", rect{0, 0, self:width(), self:height()})
+ self:insert(self:info())
+ self:info():text_set("Status Bar")
+ self:info():textattr_set("blue")
-- init background
- self:background_set(curses.color_pair("blue", "white"))
-end
-
--- exit statusbar
-function statusbar:exit()
- view.exit(self)
-end
-
--- draw view
-function statusbar:draw()
-
- -- draw background
- view.draw(self)
-
- -- draw status info
- self:canvas():move(1, 0):write(self:info())
-end
-
--- on event
-function statusbar:event_on(e)
- view.event_on(self, e)
+ self:background_set("white")
end
-- get status info
@@ -69,11 +54,5 @@ function statusbar:info()
return self._INFO
end
--- set status info
-function statusbar:info_set(info)
- self._INFO = info or ""
- self:invalidate()
-end
-
-- return module
return statusbar
diff --git a/xmake/core/ui/view.lua b/xmake/core/ui/view.lua
index e119d0c24..6af29906e 100644
--- a/xmake/core/ui/view.lua
+++ b/xmake/core/ui/view.lua
@@ -175,14 +175,18 @@ function view:draw()
-- trace
log:print("%s: draw ..", self)
- -- clear view
- self:canvas():clear()
-
-- draw background
local background = self:background()
if background then
+ background = curses.color_pair(background, background)
self:canvas():attr(background):move(0, 0):write(string.rep(' ', self:width() * self:height()))
+ else
+ self:canvas():clear()
end
+
+ -- clear mark
+ self:state_set("redraw", false)
+ self:_mark_refresh()
end
-- refresh view
@@ -292,10 +296,14 @@ end
-- get background
function view:background()
- return self:attr("background")
+ local background = self:attr("background")
+ if not background and self:parent() then
+ background = self:parent():background()
+ end
+ return background
end
--- set background
+-- set background, .e.g background_set("blue")
function view:background_set(color)
self:attr_set("background", color)
end
@@ -303,6 +311,9 @@ end
-- need redraw view
function view:_mark_redraw()
+ -- trace
+ log:print("%s: mark as redraw", self)
+
-- need redraw it
self:state_set("redraw", true)