mirror of
https://github.com/kepler155c/opus
synced 2025-07-01 01:23:15 +00:00
embedded windows
This commit is contained in:
parent
8451c94abe
commit
1eea0d7cd8
@ -3,6 +3,7 @@ local class = require('class')
|
|||||||
local Event = require('event')
|
local Event = require('event')
|
||||||
local Input = require('input')
|
local Input = require('input')
|
||||||
local Peripheral = require('peripheral')
|
local Peripheral = require('peripheral')
|
||||||
|
local Terminal = require('terminal')
|
||||||
local Transition = require('ui.transition')
|
local Transition = require('ui.transition')
|
||||||
local Util = require('util')
|
local Util = require('util')
|
||||||
|
|
||||||
@ -13,6 +14,7 @@ local device = _G.device
|
|||||||
local fs = _G.fs
|
local fs = _G.fs
|
||||||
local os = _G.os
|
local os = _G.os
|
||||||
local term = _G.term
|
local term = _G.term
|
||||||
|
local window = _G.window
|
||||||
|
|
||||||
--[[
|
--[[
|
||||||
Using the shorthand window definition, elements are created from
|
Using the shorthand window definition, elements are created from
|
||||||
@ -2405,6 +2407,63 @@ function UI.SlideOut:eventHandler(event)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--[[-- Embedded --]]--
|
||||||
|
UI.Embedded = class(UI.Window)
|
||||||
|
UI.Embedded.defaults = {
|
||||||
|
UIElement = 'Embedded',
|
||||||
|
backgroundColor = colors.black,
|
||||||
|
textColor = colors.white,
|
||||||
|
accelerators = {
|
||||||
|
up = 'scroll_up',
|
||||||
|
down = 'scroll_down',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function UI.Embedded:setParent()
|
||||||
|
UI.Window.setParent(self)
|
||||||
|
self.win = window.create(UI.term.device, 1, 1, self.width, self.height, false)
|
||||||
|
Canvas.convertWindow(self.win, UI.term.device, self.x, self.y)
|
||||||
|
Terminal.scrollable(self.win, 100)
|
||||||
|
|
||||||
|
local canvas = self:getCanvas()
|
||||||
|
self.win.canvas.parent = canvas
|
||||||
|
table.insert(canvas.layers, self.win.canvas)
|
||||||
|
self.canvas = self.win.canvas
|
||||||
|
|
||||||
|
self.win.setCursorPos(1, 1)
|
||||||
|
self.win.setBackgroundColor(self.backgroundColor)
|
||||||
|
self.win.setTextColor(self.textColor)
|
||||||
|
self.win.clear()
|
||||||
|
end
|
||||||
|
|
||||||
|
function UI.Embedded:draw()
|
||||||
|
self.canvas:dirty()
|
||||||
|
end
|
||||||
|
|
||||||
|
function UI.Embedded:enable()
|
||||||
|
self.canvas:setVisible(true)
|
||||||
|
UI.Window.enable(self)
|
||||||
|
end
|
||||||
|
|
||||||
|
function UI.Embedded:disable()
|
||||||
|
self.canvas:setVisible(false)
|
||||||
|
UI.Window.disable(self)
|
||||||
|
end
|
||||||
|
|
||||||
|
function UI.Embedded:eventHandler(event)
|
||||||
|
if event.type == 'scroll_up' then
|
||||||
|
self.win.scrollUp()
|
||||||
|
return true
|
||||||
|
elseif event.type == 'scroll_down' then
|
||||||
|
self.win.scrollDown()
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function UI.Embedded:focus()
|
||||||
|
-- allow scrolling
|
||||||
|
end
|
||||||
|
|
||||||
--[[-- Notification --]]--
|
--[[-- Notification --]]--
|
||||||
UI.Notification = class(UI.Window)
|
UI.Notification = class(UI.Window)
|
||||||
UI.Notification.defaults = {
|
UI.Notification.defaults = {
|
||||||
|
@ -129,7 +129,6 @@ end
|
|||||||
|
|
||||||
function Canvas:writeBlit(x, y, text, bg, fg)
|
function Canvas:writeBlit(x, y, text, bg, fg)
|
||||||
if y > 0 and y <= self.height and x <= self.width then
|
if y > 0 and y <= self.height and x <= self.width then
|
||||||
|
|
||||||
local width = #text
|
local width = #text
|
||||||
|
|
||||||
-- fix ffs
|
-- fix ffs
|
||||||
@ -342,6 +341,7 @@ function Canvas.convertWindow(win, parent, wx, wy)
|
|||||||
str,
|
str,
|
||||||
win.getBackgroundColor(),
|
win.getBackgroundColor(),
|
||||||
win.getTextColor())
|
win.getTextColor())
|
||||||
|
win.setCursorPos(x + #str, y)
|
||||||
end
|
end
|
||||||
|
|
||||||
function win.blit(text, fg, bg)
|
function win.blit(text, fg, bg)
|
||||||
@ -353,8 +353,10 @@ function Canvas.convertWindow(win, parent, wx, wy)
|
|||||||
win.canvas:redraw(parent)
|
win.canvas:redraw(parent)
|
||||||
end
|
end
|
||||||
|
|
||||||
function win.scroll()
|
function win.scroll(n)
|
||||||
error('scroll: not implemented')
|
table.insert(win.canvas.lines, table.remove(win.canvas.lines, 1))
|
||||||
|
win.canvas.lines[#win.canvas.lines].text = _rep(' ', win.canvas.width)
|
||||||
|
win.canvas:dirty()
|
||||||
end
|
end
|
||||||
|
|
||||||
function win.reposition(x, y, width, height)
|
function win.reposition(x, y, width, height)
|
||||||
|
@ -55,11 +55,11 @@ function Util.tostring(pattern, ...)
|
|||||||
end
|
end
|
||||||
str = str .. string.format(' %s: %s\n', k, value)
|
str = str .. string.format(' %s: %s\n', k, value)
|
||||||
end
|
end
|
||||||
if #str < width then
|
--if #str < width then
|
||||||
str = str:gsub('\n', '') .. ' }'
|
--str = str:gsub('\n', '') .. ' }'
|
||||||
else
|
--else
|
||||||
str = str .. '}'
|
str = str .. '}'
|
||||||
end
|
--end
|
||||||
return str
|
return str
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -8,6 +8,7 @@ local Util = require('util')
|
|||||||
local colors = _G.colors
|
local colors = _G.colors
|
||||||
local os = _G.os
|
local os = _G.os
|
||||||
local textutils = _G.textutils
|
local textutils = _G.textutils
|
||||||
|
local term = _G.term
|
||||||
|
|
||||||
local sandboxEnv = setmetatable(Util.shallowCopy(_ENV), { __index = _G })
|
local sandboxEnv = setmetatable(Util.shallowCopy(_ENV), { __index = _G })
|
||||||
sandboxEnv.exit = function() Event.exitPullEvents() end
|
sandboxEnv.exit = function() Event.exitPullEvents() end
|
||||||
@ -45,7 +46,7 @@ local page = UI.Page {
|
|||||||
y = 2, x = -1, width = 1,
|
y = 2, x = -1, width = 1,
|
||||||
},
|
},
|
||||||
grid = UI.ScrollingGrid {
|
grid = UI.ScrollingGrid {
|
||||||
y = 3,
|
y = 3, ey = -2,
|
||||||
columns = {
|
columns = {
|
||||||
{ heading = 'Key', key = 'name' },
|
{ heading = 'Key', key = 'name' },
|
||||||
{ heading = 'Value', key = 'value' },
|
{ heading = 'Value', key = 'value' },
|
||||||
@ -53,7 +54,16 @@ local page = UI.Page {
|
|||||||
sortColumn = 'name',
|
sortColumn = 'name',
|
||||||
autospace = true,
|
autospace = true,
|
||||||
},
|
},
|
||||||
notification = UI.Notification(),
|
titleBar = UI.TitleBar {
|
||||||
|
title = 'Output',
|
||||||
|
y = -1,
|
||||||
|
event = 'show_output',
|
||||||
|
closeInd = '^'
|
||||||
|
},
|
||||||
|
output = UI.Embedded {
|
||||||
|
y = -6,
|
||||||
|
},
|
||||||
|
--notification = UI.Notification(),
|
||||||
}
|
}
|
||||||
|
|
||||||
function page.indicator:showResult(s)
|
function page.indicator:showResult(s)
|
||||||
@ -86,6 +96,7 @@ end
|
|||||||
function page:enable()
|
function page:enable()
|
||||||
self:setFocus(self.prompt)
|
self:setFocus(self.prompt)
|
||||||
UI.Page.enable(self)
|
UI.Page.enable(self)
|
||||||
|
self.output:disable()
|
||||||
end
|
end
|
||||||
|
|
||||||
local function autocomplete(env, oLine, x)
|
local function autocomplete(env, oLine, x)
|
||||||
@ -131,6 +142,32 @@ function page:eventHandler(event)
|
|||||||
self:executeStatement('_ENV')
|
self:executeStatement('_ENV')
|
||||||
command = nil
|
command = nil
|
||||||
|
|
||||||
|
elseif event.type == 'hide_output' then
|
||||||
|
self.output:disable()
|
||||||
|
|
||||||
|
self.titleBar.oy = -1
|
||||||
|
self.titleBar.event = 'show_output'
|
||||||
|
self.titleBar.closeInd = '^'
|
||||||
|
self.titleBar:resize()
|
||||||
|
|
||||||
|
self.grid.ey = -2
|
||||||
|
self.grid:resize()
|
||||||
|
|
||||||
|
self:draw()
|
||||||
|
|
||||||
|
elseif event.type == 'show_output' then
|
||||||
|
self.output:enable()
|
||||||
|
|
||||||
|
self.titleBar.oy = -7
|
||||||
|
self.titleBar.event = 'hide_output'
|
||||||
|
self.titleBar.closeInd = 'v'
|
||||||
|
self.titleBar:resize()
|
||||||
|
|
||||||
|
self.grid.ey = -8
|
||||||
|
self.grid:resize()
|
||||||
|
|
||||||
|
self:draw()
|
||||||
|
|
||||||
elseif event.type == 'autocomplete' then
|
elseif event.type == 'autocomplete' then
|
||||||
local sz = #self.prompt.value
|
local sz = #self.prompt.value
|
||||||
local pos = self.prompt.pos
|
local pos = self.prompt.pos
|
||||||
@ -190,6 +227,10 @@ end
|
|||||||
function page:setResult(result)
|
function page:setResult(result)
|
||||||
local t = { }
|
local t = { }
|
||||||
|
|
||||||
|
local oterm = term.redirect(self.output.win)
|
||||||
|
Util.print(result)
|
||||||
|
term.redirect(oterm)
|
||||||
|
|
||||||
local function safeValue(v)
|
local function safeValue(v)
|
||||||
if type(v) == 'string' or type(v) == 'number' then
|
if type(v) == 'string' or type(v) == 'number' then
|
||||||
return v
|
return v
|
||||||
@ -300,7 +341,12 @@ end
|
|||||||
function page:executeStatement(statement)
|
function page:executeStatement(statement)
|
||||||
command = statement
|
command = statement
|
||||||
|
|
||||||
|
local oterm = term.redirect(self.output.win)
|
||||||
local s, m = self:rawExecute(command)
|
local s, m = self:rawExecute(command)
|
||||||
|
if not s then
|
||||||
|
_G.printError(m)
|
||||||
|
end
|
||||||
|
term.redirect(oterm)
|
||||||
|
|
||||||
if s and m then
|
if s and m then
|
||||||
self:setResult(m)
|
self:setResult(m)
|
||||||
@ -308,7 +354,10 @@ function page:executeStatement(statement)
|
|||||||
self.grid:setValues({ })
|
self.grid:setValues({ })
|
||||||
self.grid:draw()
|
self.grid:draw()
|
||||||
if m then
|
if m then
|
||||||
self.notification:error(m, 5)
|
if not self.output.enabled then
|
||||||
|
self:emit({ type = 'show_output' })
|
||||||
|
end
|
||||||
|
--self.notification:error(m, 5)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
self.indicator:showResult(not not s)
|
self.indicator:showResult(not not s)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user