fix resize

This commit is contained in:
ruki
2020-05-06 23:14:16 +08:00
parent 2d95bff3ea
commit 4a18bd37eb
6 changed files with 90 additions and 335 deletions

View File

@@ -1,136 +0,0 @@
--!A cross-platform terminal ui library based on Lua
--
-- Licensed 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-2020, TBOOX Open Source Group.
--
--
-- @author OpportunityLiu
-- @file hashset.lua
--
-- define module
local hashset = hashset or {}
local hashset_impl = hashset.__index or {}
-- load modules
local table = require("base/table")
-- representaion for nil key
hashset._NIL = setmetatable({}, {__tostring = function() return "symbol(nil)" end })
function hashset._to_key(key)
if key == nil then
key = hashset._NIL
end
return key
end
-- make a new hashset
function hashset.new()
return setmetatable({ _DATA = {}, _SIZE = 0 }, hashset)
end
-- construct from list of items
function hashset.of(...)
local result = hashset.new()
local data = table.pack(...)
for i = 1, data.n do
result:insert(data[i])
end
return result
end
-- construct from an array
function hashset.from(array)
assert(array)
return hashset.of(table.unpack(array))
end
-- check value is in hashset
function hashset_impl:has(value)
value = hashset._to_key(value)
return self._DATA[value] or false
end
-- insert value to hashset, returns false if value has already in the hashset
function hashset_impl:insert(value)
value = hashset._to_key(value)
local result = not (self._DATA[value] or false)
if result then
self._DATA[value] = true
self._SIZE = self._SIZE + 1
end
return result
end
-- remove value from hashset, returns false if value is not in the hashset
function hashset_impl:remove(value)
value = hashset._to_key(value)
local result = self._DATA[value] or false
if result then
self._DATA[value] = nil
self._SIZE = self._SIZE - 1
end
return result
end
-- convert hashset to an array, nil in the set will be ignored
function hashset_impl:to_array()
local result = {}
for k, _ in pairs(self._DATA) do
if k ~= hashset._NIL then
table.insert(result, k)
end
end
return result
end
-- iterate keys of hashtable
-- for _, key in instance:keys() do ... end
function hashset_impl:keys()
return function (table, key)
local k, _ = next(table._DATA, key)
if k == hashset._NIL then
return k, nil
else
return k, k
end
end, self, nil
end
-- get size of hashset
function hashset_impl:size()
return self._SIZE
end
-- is empty?
function hashset_impl:empty()
return self:size() == 0
end
-- get data of hashset
function hashset_impl:data()
return self._DATA
end
-- clear hashset
function hashset_impl:clear()
self._DATA = {}
self._SIZE = 0
end
-- return module
hashset.__index = hashset_impl
return hashset

View File

@@ -61,110 +61,6 @@ function table.join2(self, ...)
return self return self
end end
-- append all objects to array
function table.append(array, ...)
for _, value in ipairs({...}) do
table.insert(array, value)
end
return array
end
-- copy the table to self
function table.copy(copied)
-- init it
copied = copied or {}
-- copy it
local result = {}
for k, v in pairs(table.wrap(copied)) do
result[k] = v
end
-- ok
return result
end
-- copy the table to self
function table.copy2(self, copied)
-- check
assert(self)
-- init it
copied = copied or {}
-- clear self first
table.clear(self)
-- copy it
for k, v in pairs(table.wrap(copied)) do
self[k] = v
end
end
-- inherit interfaces and create a new instance
function table.inherit(...)
-- init instance
local classes = {...}
local instance = {}
local metainfo = {}
for _, clasz in ipairs(classes) do
for k, v in pairs(clasz) do
if type(v) == "function" then
if k:startswith("__") then
if metainfo[k] == nil then
metainfo[k] = v
end
else
if instance[k] == nil then
instance[k] = v
else
instance["_super_" .. k] = v
end
end
end
end
end
setmetatable(instance, metainfo)
-- ok?
return instance
end
-- inherit interfaces from the given class
function table.inherit2(self, ...)
-- check
assert(self)
-- init instance
local classes = {...}
local metainfo = getmetatable(self) or {}
for _, clasz in ipairs(classes) do
for k, v in pairs(clasz) do
if type(v) == "function" then
if k:startswith("__") then
if metainfo[k] == nil then
metainfo[k] = v
end
else
if self[k] == nil then
self[k] = v
else
self["_super_" .. k] = v
end
end
end
end
end
-- ok?
return self
end
-- slice table array -- slice table array
function table.slice(self, first, last, step) function table.slice(self, first, last, step)
@@ -186,86 +82,6 @@ function table.is_dictionary(dict)
return type(dict) == "table" and dict[1] == nil return type(dict) == "table" and dict[1] == nil
end end
-- dump it with the level
function table._dump(self, exclude, level)
-- dump basic type
if type(self) == "string" or type(self) == "boolean" or type(self) == "number" then
io.write(tostring(self))
elseif type(self) == "table" and (getmetatable(self) or {}).__tostring then
io.write(tostring(self))
-- dump table
elseif type(self) == "table" then
-- dump head
io.write("\n")
for l = 1, level do
io.write(" ")
end
io.write("{\n")
-- dump body
local i = 0
for k, v in pairs(self) do
-- exclude some keys
if not exclude or type(k) ~= "string" or not k:find(exclude) then
-- dump spaces and separator
for l = 1, level do
io.write(" ")
end
if i == 0 then
io.write(" ")
else
io.write(", ")
end
-- dump key
if type(k) == "string" then
io.write(k, " = ")
end
-- dump value
table._dump(v, exclude, level + 1)
-- dump newline
io.write("\n")
i = i + 1
end
end
-- dump tail
for l = 1, level do
io.write(" ")
end
io.write("}\n")
elseif self ~= nil then
io.write("<" .. tostring(self) .. ">")
else
io.write("nil")
end
end
-- dump it
function table.dump(self, exclude, prefix)
-- dump prefix
if prefix then
io.write(prefix)
end
-- dump it
table._dump(self, exclude, 0)
-- end
print("")
-- return it
return self
end
-- unwrap object if be only one -- unwrap object if be only one
function table.unwrap(object) function table.unwrap(object)
if type(object) == "table" then if type(object) == "table" then
@@ -336,5 +152,82 @@ function table.unique(array, barrier)
return array return array
end end
-- pack arguments into a table
-- polyfill of lua 5.2, @see https://www.lua.org/manual/5.2/manual.html#pdf-table.pack
function table.pack(...)
return { n = select("#", ...), ... }
end
-- unpack table values
-- polyfill of lua 5.2, @see https://www.lua.org/manual/5.2/manual.html#pdf-table.unpack
table.unpack = unpack
-- get keys of a table
function table.keys(tab)
assert(tab)
local keyset = {}
local n = 0
for k, _ in pairs(tab) do
n = n + 1
keyset[n] = k
end
return keyset, n
end
-- get values of a table
function table.values(tab)
assert(tab)
local valueset = {}
local n = 0
for _, v in pairs(tab) do
n = n + 1
valueset[n] = v
end
return valueset, n
end
-- map values to a new table
function table.map(tab, mapper)
assert(tab)
assert(mapper)
local newtab = {}
for k, v in pairs(tab) do
newtab[k] = mapper(k, v)
end
return newtab
end
-- map values to a new array
function table.imap(arr, mapper)
assert(arr)
assert(mapper)
local newarr = {}
for k, v in ipairs(arr) do
table.insert(newarr, mapper(k, v))
end
return newarr
end
-- reverse table values
function table.reverse(arr)
assert(arr)
local revarr = {}
local l = #arr
for i = 1, l do
revarr[i] = arr[l - i + 1]
end
return revarr
end
-- return module: table -- return module: table
return table return table

View File

@@ -51,7 +51,7 @@ function boxdialog:init(name, bounds, title)
local lines = #self:text():splitext(v:text()) local lines = #self:text():splitext(v:text())
if lines > 0 and lines < self:height() then if lines > 0 and lines < self:height() then
self._TEXT_EY = lines self._TEXT_EY = lines
self:invalidate(true) self:panel():invalidate(true)
--[[ --[[
self:box():bounds().sy = lines self:box():bounds().sy = lines
self:text():bounds().ey = lines self:text():bounds().ey = lines

View File

@@ -139,15 +139,8 @@ function panel:insert(v, opt)
self:select(v) self:select(v)
end end
-- @note we need force to trigger on_resize for this panel for
-- passing on_resize event to this subview if this subview has been resized?
local resize = false
if v:state("resize") and not self:state("resize") then
resize = true
end
-- invalidate it -- invalidate it
self:invalidate(resize) self:invalidate()
end end
-- remove view -- remove view

View File

@@ -19,12 +19,12 @@
-- --
-- load modules -- load modules
local log = require("ltui/base/log") local log = require("ltui/base/log")
local rect = require("ltui/rect") local rect = require("ltui/rect")
local point = require("ltui/point") local point = require("ltui/point")
local panel = require("ltui/panel") local panel = require("ltui/panel")
local event = require("ltui/event") local event = require("ltui/event")
local curses = require("ltui/curses") local curses = require("ltui/curses")
-- define module -- define module
local program = program or panel() local program = program or panel()

View File

@@ -442,6 +442,11 @@ function view:_mark_resize()
-- need resize it -- need resize it
self:state_set("resize", true) self:state_set("resize", true)
-- @note we need trigger on_resize() of the root view and pass it to this subview
if self:parent() then
self:parent():invalidate(true)
end
end end
-- need redraw view -- need redraw view