pinnacle/api/lua/window.lua

184 lines
4 KiB
Lua
Raw Normal View History

2023-06-26 00:18:50 +02:00
-- This Source Code Form is subject to the terms of the Mozilla Public
-- License, v. 2.0. If a copy of the MPL was not distributed with this
-- file, You can obtain one at https://mozilla.org/MPL/2.0/.
2023-06-26 00:49:06 +02:00
--
-- SPDX-License-Identifier: MPL-2.0
2023-06-26 00:18:50 +02:00
---@class Window
---@field private id integer The internal id of this window
---@field private app_id string? The equivalent of an X11 window's class
---@field private title string? The window's title
---@field private size { w: integer, h: integer } The size of the window
---@field private location { x: integer, y: integer } The location of the window
---@field private floating boolean Whether the window is floating or not (tiled)
local win = {}
2023-07-11 18:59:38 +02:00
---@param props Window
---@return Window
local function new_window(props)
2023-06-27 04:05:29 +02:00
-- Copy functions over
for k, v in pairs(win) do
2023-06-27 04:05:29 +02:00
props[k] = v
end
return props
end
---Set a window's size.
---@param size { w: integer?, h: integer? }
function win:set_size(size)
self.size = {
w = size.w or self.size.w,
h = size.h or self.size.h,
}
SendMsg({
SetWindowSize = {
window_id = self.id,
size = { self.size.w, self.size.h },
},
})
end
---Move a window to a tag, removing all other ones.
---@param name string The name of the tag.
function win:move_to_tag(name)
SendMsg({
MoveWindowToTag = {
window_id = self.id,
tag_id = name,
},
})
end
---Toggle the specified tag for this window.
---@param name string The name of the tag.
function win:toggle_tag(name)
SendMsg({
ToggleTagOnWindow = {
window_id = self.id,
tag_id = name,
},
})
end
2023-07-18 22:12:23 +02:00
---Close this window.
function win:close()
2023-06-15 19:42:34 +02:00
SendMsg({
2023-06-18 04:02:58 +02:00
CloseWindow = {
2023-07-18 22:12:23 +02:00
window_id = self.id,
2023-06-15 19:42:34 +02:00
},
})
end
2023-07-18 22:12:23 +02:00
---Toggle this window's floating status.
function win:toggle_floating()
2023-06-19 02:30:52 +02:00
SendMsg({
ToggleFloating = {
2023-07-18 22:12:23 +02:00
window_id = self.id,
2023-06-19 02:30:52 +02:00
},
})
end
2023-07-18 22:12:23 +02:00
---Get a window's size.
---@return { w: integer, h: integer }
function win:get_size()
return self.size
end
-------------------------------------------------------------------
local window = {}
2023-07-11 18:59:38 +02:00
---TODO: This function is not implemented yet.
---
---Get a window by its app id (aka its X11 class).
---@param app_id string The window's app id. For example, Alacritty's app id is "Alacritty".
2023-07-18 22:12:23 +02:00
---@return Window|nil
function window.get_by_app_id(app_id)
SendRequest({
GetWindowByAppId = {
app_id = app_id,
},
})
local response = ReadMsg()
2023-07-18 22:12:23 +02:00
local window_id = response.RequestResponse.response.Window.window_id
if window_id == nil then
return nil
end
---@type Window
local wind = {
2023-07-18 22:12:23 +02:00
id = window_id,
}
return new_window(wind)
end
2023-07-11 18:59:38 +02:00
---TODO: This function is not implemented yet.
---
---Get a window by its title.
---@param title string The window's title.
2023-07-18 22:12:23 +02:00
---@return Window|nil
function window.get_by_title(title)
SendRequest({
GetWindowByTitle = {
title = title,
},
})
local response = ReadMsg()
2023-07-18 22:12:23 +02:00
local window_id = response.RequestResponse.response.Window.window_id
if window_id == nil then
return nil
end
---@type Window
local wind = {
2023-07-18 22:12:23 +02:00
id = window_id,
}
return new_window(wind)
end
---Get the currently focused window.
2023-07-18 22:12:23 +02:00
---@return Window|nil
function window.get_focused()
2023-07-11 18:59:38 +02:00
SendRequest("GetWindowByFocus")
local response = ReadMsg()
2023-07-18 22:12:23 +02:00
local window_id = response.RequestResponse.response.Window.window_id
if window_id == nil then
return nil
end
2023-06-28 23:42:07 +02:00
---@type Window
local wind = {
2023-07-18 22:12:23 +02:00
id = window_id,
}
return new_window(wind)
end
---Get all windows.
---@return Window[]
2023-07-11 18:59:38 +02:00
function window.get_all()
SendRequest("GetAllWindows")
2023-07-18 22:12:23 +02:00
local window_ids = ReadMsg().RequestResponse.response.Windows.window_ids
2023-06-27 04:05:29 +02:00
---@type Window[]
local windows = {}
2023-07-18 22:12:23 +02:00
for i, window_id in ipairs(window_ids) do
windows[i] = new_window({ id = window_id })
end
return windows
end
return window