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
|
|
|
|
2023-06-27 01:48:29 +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)
|
2023-07-02 17:26:07 +02:00
|
|
|
local win = {}
|
2023-06-27 01:48:29 +02:00
|
|
|
|
2023-07-11 18:59:38 +02:00
|
|
|
---@param props Window
|
2023-06-27 01:48:29 +02:00
|
|
|
---@return Window
|
|
|
|
local function new_window(props)
|
2023-06-27 04:05:29 +02:00
|
|
|
-- Copy functions over
|
2023-07-02 17:26:07 +02:00
|
|
|
for k, v in pairs(win) do
|
2023-06-27 04:05:29 +02:00
|
|
|
props[k] = v
|
2023-06-27 01:48:29 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return props
|
|
|
|
end
|
|
|
|
|
|
|
|
---Set a window's size.
|
|
|
|
---@param size { w: integer?, h: integer? }
|
2023-07-02 17:26:07 +02:00
|
|
|
function win:set_size(size)
|
2023-06-27 01:48:29 +02:00
|
|
|
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
|
|
|
|
|
2023-07-02 17:26:07 +02:00
|
|
|
---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.
|
|
|
|
---
|
2023-07-02 17:26:07 +02:00
|
|
|
---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
|
2023-07-02 17:26:07 +02:00
|
|
|
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
|
2023-07-02 17:26:07 +02:00
|
|
|
|
|
|
|
---@type Window
|
|
|
|
local wind = {
|
2023-07-18 22:12:23 +02:00
|
|
|
id = window_id,
|
2023-07-02 17:26:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return new_window(wind)
|
|
|
|
end
|
|
|
|
|
2023-07-11 18:59:38 +02:00
|
|
|
---TODO: This function is not implemented yet.
|
|
|
|
---
|
2023-07-02 17:26:07 +02:00
|
|
|
---Get a window by its title.
|
|
|
|
---@param title string The window's title.
|
2023-07-18 22:12:23 +02:00
|
|
|
---@return Window|nil
|
2023-07-02 17:26:07 +02:00
|
|
|
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
|
2023-07-02 17:26:07 +02:00
|
|
|
|
|
|
|
---@type Window
|
|
|
|
local wind = {
|
2023-07-18 22:12:23 +02:00
|
|
|
id = window_id,
|
2023-07-02 17:26:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return new_window(wind)
|
|
|
|
end
|
|
|
|
|
|
|
|
---Get the currently focused window.
|
2023-07-18 22:12:23 +02:00
|
|
|
---@return Window|nil
|
2023-07-02 17:26:07 +02:00
|
|
|
function window.get_focused()
|
2023-07-11 18:59:38 +02:00
|
|
|
SendRequest("GetWindowByFocus")
|
2023-06-27 01:48:29 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2023-06-27 01:48:29 +02:00
|
|
|
---@type Window
|
2023-07-02 17:26:07 +02:00
|
|
|
local wind = {
|
2023-07-18 22:12:23 +02:00
|
|
|
id = window_id,
|
2023-06-27 01:48:29 +02:00
|
|
|
}
|
|
|
|
|
2023-07-02 17:26:07 +02:00
|
|
|
return new_window(wind)
|
2023-06-27 01:48:29 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
---Get all windows.
|
|
|
|
---@return Window[]
|
2023-07-11 18:59:38 +02:00
|
|
|
function window.get_all()
|
|
|
|
SendRequest("GetAllWindows")
|
2023-06-27 01:48:29 +02:00
|
|
|
|
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 })
|
2023-06-27 01:48:29 +02:00
|
|
|
end
|
|
|
|
return windows
|
|
|
|
end
|
|
|
|
|
2023-07-02 17:26:07 +02:00
|
|
|
return window
|