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)
local window = { }
---@param props { id: integer, app_id: string?, title: string?, size: { w: integer, h: integer }, location: { x: integer, y: integer }, floating: boolean }
---@return Window
local function new_window ( props )
2023-06-27 04:05:29 +02:00
-- Copy functions over
for k , v in pairs ( window ) do
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? }
function window : 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
---Get a window's size.
---@return { w: integer, h: integer }
function window : get_size ( )
return self.size
end
-------------------------------------------------------------------
local client = { }
2023-06-15 19:42:34 +02:00
---Close a window.
---@param client_id integer? The id of the window you want closed, or nil to close the currently focused window, if any.
2023-06-27 01:48:29 +02:00
function client . close_window ( client_id )
2023-06-15 19:42:34 +02:00
SendMsg ( {
2023-06-18 04:02:58 +02:00
CloseWindow = {
2023-06-21 21:48:38 +02:00
client_id = client_id ,
2023-06-15 19:42:34 +02:00
} ,
} )
end
2023-06-19 02:30:52 +02:00
---Toggle a window's floating status.
---@param client_id integer? The id of the window you want to toggle, or nil to toggle the currently focused window, if any.
2023-06-27 01:48:29 +02:00
function client . toggle_floating ( client_id )
2023-06-19 02:30:52 +02:00
SendMsg ( {
ToggleFloating = {
2023-06-21 21:48:38 +02:00
client_id = client_id ,
2023-06-19 02:30:52 +02:00
} ,
} )
end
2023-06-27 01:48:29 +02:00
---Get a window.
---@param identifier { app_id: string } | { title: string } | "focus" A table with either the key app_id or title, depending if you want to get the window via its app_id or title, OR the string "focus" to get the currently focused window.
---@return Window
function client . get_window ( identifier )
local req_id = Requests : next ( )
if type ( identifier ) == " string " then
SendRequest ( {
GetWindowByFocus = {
id = req_id ,
} ,
} )
elseif identifier.app_id then
SendRequest ( {
GetWindowByAppId = {
id = req_id ,
app_id = identifier.app_id ,
} ,
} )
else
SendRequest ( {
GetWindowByTitle = {
id = req_id ,
title = identifier.title ,
} ,
} )
end
local response = ReadMsg ( )
local props = response.RequestResponse . response.Window . window
2023-06-28 23:42:07 +02:00
2023-06-27 01:48:29 +02:00
---@type Window
local win = {
id = props.id ,
app_id = props.app_id or " " ,
title = props.title or " " ,
size = {
w = props.size [ 1 ] ,
h = props.size [ 2 ] ,
} ,
location = {
x = props.location [ 1 ] ,
y = props.location [ 2 ] ,
} ,
floating = props.floating ,
}
return new_window ( win )
end
---Get all windows.
---@return Window[]
function client . get_windows ( )
SendRequest ( {
GetAllWindows = {
id = Requests : next ( ) ,
} ,
} )
2023-06-27 04:19:02 +02:00
-- INFO: these read synchronously so this should always work IF the server works correctly
2023-06-27 04:05:29 +02:00
local window_props = ReadMsg ( ) . RequestResponse.response . GetAllWindows.windows
---@type Window[]
local windows = { }
for i , v in ipairs ( window_props ) do
2023-06-27 01:48:29 +02:00
windows [ i ] = {
id = v.id ,
app_id = v.app_id or " " ,
title = v.title or " " ,
size = {
w = v.size [ 1 ] ,
h = v.size [ 2 ] ,
} ,
location = {
x = v.location [ 1 ] ,
y = v.location [ 2 ] ,
} ,
floating = v.floating ,
}
end
return windows
end
-- local win = client.get_window("focus")
return client