thornWM/lib/tree/window.lua
2025-05-23 15:41:23 +00:00

122 lines
2.8 KiB
Lua

local Tree
local X = require("x")
local Util = require("util")
local Async = require("async")
local Tag = require("tree.tag")
local function round(n)
return math.floor(n+0.5)
end
local Window = {
x = 0, y = 0,
w = 0, h = 0,
bx = 0, by = 0,
bw = 0, bh = 0,
margin = {top = 0, right = 0, bottom = 0, left = 0},
borderWidth = 0
}
Window.__index = Window
function Window.new(id)
local window = {
id = id,
monitor = Tree.focusedMonitor,
-- tags = {
-- main = Tag.new()
-- }
}
setmetatable(window, Window)
return window
end
function Window:getClass()
if self.class == nil then
local c1, c2 = X.getProperty(self.id, X.getAtoms("WM_CLASS"), 0, X.AnyPropertyType)
if c1 then
self.class = {c1, c2}
else
self.class = false
end
end
return self.class
end
function Window:getName()
self.name = X.getProperty(self.id, X.getAtoms("_NET_WM_NAME"), 0, X.getAtoms("UTF8_STRING"))
if not self.name then
self.name = X.getProperty(self.id, X.getAtoms("WM_NAME"), 0, X.AnyPropertyType)
end
return self.name
end
function Window:float()
end
function Window:setGeometry(x, y, w, h)
if self.x ~= x or self.y ~= y or self.width ~= w or self.h ~= h then
self.x, self.y, self.width, self.height = x, y, w, h
self:applyGeometry()
end
-- self.monitor.dirtyGeometry = true
-- self.dirtyGeometry = true
-- Tree.dirtyGeometry = true
-- self.dirtyGeometry = true
-- print("set")
-- Async.idle(function()
-- if self.dirtyGeometry then
-- self:applyGeometry()
-- end
-- print("ran")
-- self.dirtyGeometry = false
-- end)
end
function Window:applyGeometry()
X.setWindowGeometry(self.id, self.x, self.y, self.width, self.height)
end
function Window:show()
self.visible = true
X.mapWindow(self.id)
Tree.focusedMonitor:updateTiling()
end
function Window:hide()
self.visible = false
X.unmapWindow(self.id)
Tree.focusedMonitor:updateTiling()
end
--* Window:focus() Moves keyboard focus to window, appends it to the focus history (**Tree**.**focusHistory**), and adds the "focused" tag
function Window:unmanage()
local i = self:getIndex()
table.remove(self.monitor.stack, i)
if self.focused then
Tree.focusedWindow = nil
self.focused = nil
end
Tree.windows[self.id] = nil
Tree.focusedMonitor:updateTiling()
end
function Window:getIndex()
return Util.indexOf(self.monitor.stack, self)
end
function Window:getNext(d)
d = d or 1
local stack = Tree.focusedMonitor.stack
local w = self:getIndex()
local i = (((w-1) + d) % #stack) + 1
return stack[i]
end
function Window:focus()
self.focused = true
if Tree.focusedWindow then
Tree.focusedWindow.focused = false
end
Tree.focusedWindow = self
Tree.focusedContainer = self.parent
Tree.focusedMonitor.focusedContainer = self.parent
X.setInputFocus(self.id)
end
function Window:getStackIndex()
return Util.indexOf(self, self.monitor.stack)
end
function Window.init()
Tree = require("tree")
end
return Window