diff options
| author | ruki <[email protected]> | 2017-12-14 00:33:08 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2017-12-13 21:18:51 +0800 |
| commit | 2694f86469cdd776ba2caba9bcd7ec261457aa2c (patch) | |
| tree | cda848781d502ca90804432575d88871be1a8b55 | |
| parent | fccce25811e0a02bca4ac81a9ecadea85b2f855f (diff) | |
add core.base.dlist
| -rw-r--r-- | xmake/core/base/dlist.lua | 213 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/base/dlist.lua | 26 | ||||
| -rw-r--r-- | xmake/core/ui/canvas.lua | 32 | ||||
| -rw-r--r-- | xmake/core/ui/program.lua | 7 | ||||
| -rw-r--r-- | xmake/core/ui/view.lua | 90 |
5 files changed, 328 insertions, 40 deletions
diff --git a/xmake/core/base/dlist.lua b/xmake/core/base/dlist.lua new file mode 100644 index 000000000..e16518238 --- /dev/null +++ b/xmake/core/base/dlist.lua @@ -0,0 +1,213 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you under the Apache License, Version 2.0 (the +-- "License"); you may not use this file except in compliance +-- with the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015 - 2017, TBOOX Open Source Group. +-- +-- @author ruki +-- @file dlist.lua +-- + +-- load modules +local object = require("ui/object") + +-- define module +local dlist = dlist or object { _init = {"_length"} } {0} + +-- clear list +function dlist:clear() + self._length = 0 + self._first = nil + self._last = nil +end + +-- push item to tail +function dlist:push(t) + assert(t) + if self._last then + self._last._next = t + t._prev = self._last + self._last = t + else + self._first = t + self._last = t + end + self._length = self._length + 1 +end + +-- insert item after the given item +function dlist:insert(t, after) + assert(t) + if not after then + return self:push(t) + end + assert(t ~= after) + if after._next then + after._next._prev = t + t._next = after._next + else + self._last = t + end + t._prev = after + after._next = t + self._length = self._length + 1 +end + +-- pop item from tail +function dlist:pop() + if not self._last then return end + local t = self._last + if t._prev then + t._prev._next = nil + self._last = t._prev + t._prev = nil + else + self._first = nil + self._last = nil + end + self._length = self._length - 1 + return t +end + +-- shift item: 1 2 3 <- 2 3 +function dlist:shift() + if not self._first then return end + local t = self._first + if t._next then + t._next._prev = nil + self._first = t._next + t._next = nil + else + self._first = nil + self._last = nil + end + self._length = self._length - 1 + return t +end + +-- unshift item: 1 2 -> t 1 2 +function dlist:unshift(t) + assert(t) + if self._first then + self._first._prev = t + t._next = self._first + self._first = t + else + self._first = t + self._last = t + end + self._length = self._length + 1 +end + +-- remove item +function dlist:remove(t) + assert(t) + if t._next then + if t._prev then + t._next._prev = t._prev + t._prev._next = t._next + else + assert(t == self._first) + t._next._prev = nil + self._first = t._next + end + elseif t._prev then + assert(t == self._last) + t._prev._next = nil + self._last = t._prev + else + assert(t == self._first and t == self._last) + self._first = nil + self._last = nil + end + t._next = nil + t._prev = nil + self._length = self._length - 1 + return t +end + +-- get first item +function dlist:first() + return self._first +end + +-- get last item +function dlist:last() + return self._last +end + +-- get next item +function dlist:next(last) + if last then + return last._next + else + return self._first + end +end + +-- get the previous item +function dlist:prev(last) + if last then + return last._prev + else + return self._last + end +end + +-- get list size +function dlist:size() + return self._length +end + +-- is empty? +function dlist:empty() + return self:size() == 0 +end + +-- get items +-- +-- .e.g +-- +-- for item in dlist:items() do +-- print(item) +-- end +-- +function dlist:items() + + -- init iterator + local iter = function (list, item) + return list:next(item) + end + + -- return iterator and initialized state + return iter, self, nil +end + +-- get reverse items +function dlist:ritems() + + -- init iterator + local iter = function (list, item) + return list:prev(item) + end + + -- return iterator and initialized state + return iter, self, nil +end + +-- return module: dlist +return dlist diff --git a/xmake/core/sandbox/modules/import/core/base/dlist.lua b/xmake/core/sandbox/modules/import/core/base/dlist.lua new file mode 100644 index 000000000..4c87365f2 --- /dev/null +++ b/xmake/core/sandbox/modules/import/core/base/dlist.lua @@ -0,0 +1,26 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you under the Apache License, Version 2.0 (the +-- "License"); you may not use this file except in compliance +-- with the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015 - 2017, TBOOX Open Source Group. +-- +-- @author ruki +-- @file dlist.lua +-- + +-- return module +return require("base/dlist") diff --git a/xmake/core/ui/canvas.lua b/xmake/core/ui/canvas.lua index 6511fe127..0662ccca4 100644 --- a/xmake/core/ui/canvas.lua +++ b/xmake/core/ui/canvas.lua @@ -111,37 +111,5 @@ function canvas:attr(attrs, modify) return self end ---[[ -function canvas:line(length) - return Line:create(length) -end - - -function Line:create(length) - self = self() - self._length = length - self._line = curses.new_chstr(length) - return self -end - -function Line:__len() - return self._length -end - -function Line:ch(offset, char, attrs, length) - self._line:set_ch(offset, char, attrs and calc_attr(attrs), length) - return self -end - -function Line:acs(offset, char, attrs, length) - return self:ch(offset, acs(char), attrs, length) -end - -function Line:str(offset, str, attrs, rep) - self._line:set_str(offset, str, calc_attr(attrs), rep) - return self -end -]] - -- return module return canvas diff --git a/xmake/core/ui/program.lua b/xmake/core/ui/program.lua index a1ec01edf..da4edabc7 100644 --- a/xmake/core/ui/program.lua +++ b/xmake/core/ui/program.lua @@ -63,8 +63,6 @@ function program:init() curses.start_color() end --- init_keymap() - -- disable main window cursor main_window:leaveok(false) @@ -79,11 +77,6 @@ function program:init() -- init group group.init(self, rect {0, 0, curses.columns(), curses.lines()}) - - --[[ - self:set_state('selected', true) - self:set_state('focused', true) - ]] end -- exit program diff --git a/xmake/core/ui/view.lua b/xmake/core/ui/view.lua index e8e38a9f3..4008ce547 100644 --- a/xmake/core/ui/view.lua +++ b/xmake/core/ui/view.lua @@ -29,6 +29,7 @@ $Id: view.lua 18 2007-06-21 20:43:52Z tngd $ -- load modules local object = require("ui/object") +local canvas = require("ui/canvas") local curses = require("ui/curses") -- define module @@ -53,7 +54,18 @@ function view:init(bounds) -- check assert(type(bounds) == 'table') - -- init bounds + -- init state + local state = object() + state.visible = false -- view visibility + state.cursor_visible = false -- cursor visibility + state.block_cursor = false -- block cursor + state.selected = false -- current selected window inside group + state.focused = false -- true if parent is also focused + state.disabled = false -- view disabled + state.modal = false -- is modal view? + self._STATE = state + + -- init bounds and window self:bounds_set(bounds) end @@ -92,6 +104,82 @@ function view:bounds() return self._bounds() end +-- get the parent view +function view:parent() + return self._PARENT +end + +-- get the view canvas +function view:canvas() + if not self._CANVAS then + self._CANVAS = canvas:new(self, self._window) + end + return self._CANVAS +end + +-- on draw window +function view:on_draw() + self:canvas():clear() +end + +-- refresh view in parent window +function view:refresh() + self:on_draw() + self:redraw(true) +end + +-- redraw view +function view:redraw(on_parent) + + -- redraw this child view on parent view (group) + if on_parent and self:parent() then + self:lock() + local v = self + while v:parent() do + v:parent():draw_child(v) + v = v:parent() + end + self:unlock() + end +end + +-- lock view +function view:lock() +end + +-- unlock view +function view:unlock() +end + +-- show view? +function view:show(visible) + if self:state("visible") ~= visible then + self:state_set("visible", visible) + end +end + +-- get state +function view:state(name) + return self._STATE[name] +end + +-- set state +function view:state_set(name, enable) + + -- state is not changed? + enable = enable or false + if self:state(name) == enable then + return + end + + -- change state + self._STATE[name] = enable + + -- update visible? + if name == "visible" then + self:redraw(true) + end +end -- return module return view |
