pinnacle/api/lua/window.lua

330 lines
7.9 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
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.
2023-07-19 04:10:43 +02:00
---
---### Examples
---```lua
---window.get_focused():set_size({ w = 500, h = 500 }) -- make the window square and 500 pixels wide/tall
---window.get_focused():set_size({ h = 300 }) -- keep the window's width but make it 300 pixels tall
---window.get_focused():set_size({}) -- do absolutely nothing useful
---```
---@param size { w: integer?, h: integer? }
function win:set_size(size)
SendMsg({
SetWindowSize = {
window_id = self.id,
2023-07-19 04:10:43 +02:00
width = size.w,
height = size.h,
},
})
end
---Move a window to a tag, removing all other ones.
2023-07-19 04:10:43 +02:00
---
---### Example
---```lua
----- With the focused window on tags 1, 2, 3, and 4...
---window.get_focused():move_to_tag("5")
----- ...will make the window only appear on tag 5.
---```
---@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.
2023-07-19 04:10:43 +02:00
---
---Note: toggling off all tags currently makes a window not response to layouting.
---
---### Example
---```lua
----- With the focused window only on tag 1...
---window.get_focused():toggle_tag("2")
----- ...will also make the window appear on tag 2.
---```
---@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.
2023-07-19 04:10:43 +02:00
---
---This only sends a close *event* to the window and is the same as just clicking the X button in the titlebar.
---This will trigger save prompts in applications like GIMP.
---
---### Example
---```lua
---window.get_focused():close() -- close the currently focused window
---```
2023-07-18 22:12:23 +02:00
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.
2023-07-19 04:10:43 +02:00
---
---### Example
---```lua
---window.get_focused():toggle_floating() -- toggles the focused window between tiled and floating
---```
2023-07-18 22:12:23 +02:00
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.
2023-07-19 04:10:43 +02:00
---
---### Example
---```lua
----- With a 4K monitor, given a focused fullscreen window...
---local size = window.get_focused():size()
----- ...should have size equal to `{ w = 3840, h = 2160 }`.
---```
---@return { w: integer, h: integer }|nil size The size of the window, or nil if it doesn't exist.
function win:size()
SendRequest({
GetWindowSize = {
window_id = self.id,
},
})
local response = ReadMsg()
local size = response.RequestResponse.response.WindowSize.size
if size == nil then
return nil
else
return {
w = size[1],
h = size[2],
}
end
end
---Get this window's location in the global space.
---
---Think of your monitors as being laid out on a big sheet.
---The top left of the sheet if you trim it down is (0, 0).
---The location of this window is relative to that point.
---
---### Example
---```lua
----- With two 1080p monitors side by side and set up as such,
----- if a window is fullscreen on the right one...
---local loc = that_window:loc()
----- ...should have loc equal to `{ x = 1920, y = 0 }`.
---```
---@return { x: integer, y: integer }|nil loc The location of the window, or nil if it's not on-screen or alive.
function win:loc()
SendRequest({
GetWindowLocation = {
window_id = self.id,
},
})
local response = ReadMsg()
local loc = response.RequestResponse.response.WindowLocation.loc
if loc == nil then
return nil
else
return {
x = loc[1],
y = loc[2],
}
end
end
---Get this window's class. This is usually the name of the application.
---
---### Example
---```lua
----- With Alacritty focused...
---print(window.get_focused():class())
----- ...should print "Alacritty".
---```
---@return string|nil class This window's class, or nil if it doesn't exist.
function win:class()
SendRequest({
GetWindowClass = {
window_id = self.id,
},
})
local response = ReadMsg()
local class = response.RequestResponse.response.WindowClass.class
return class
end
---Get this window's title.
---
---### Example
---```lua
----- With Alacritty focused...
---print(window.get_focused():title())
----- ...should print the directory Alacritty is in or what it's running (what's in its title bar).
---```
---@return string|nil title This window's title, or nil if it doesn't exist.
function win:title()
SendRequest({
GetWindowTitle = {
window_id = self.id,
},
})
local response = ReadMsg()
local title = response.RequestResponse.response.WindowTitle.title
return title
end
---Get this window's floating status.
---
---### Example
---```lua
----- With Alacritty focused and floating...
---print(tostring(window.get_focused():floating()))
----- ...should print "true".
---```
---@return boolean|nil floating `true` if it's floating, `false` if it's tiled, or nil if it doesn't exist.
function win:floating()
SendRequest({
GetWindowFloating = {
window_id = self.id,
},
})
local response = ReadMsg()
local floating = response.RequestResponse.response.WindowFloating.floating
return floating
2023-07-18 22:12:23 +02:00
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