pinnacle/api/lua/window.lua
2023-07-21 15:04:39 -05:00

378 lines
9.6 KiB
Lua

-- 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/.
--
-- SPDX-License-Identifier: MPL-2.0
---@class WindowGlobal
local window_global = {}
---@class Window
---@field private _id integer The internal id of this window
local window = {}
---@param window_id WindowId
---@return Window
local function new_window(window_id)
---@type Window
local w = { _id = window_id }
-- Copy functions over
for k, v in pairs(window) do
w[k] = v
end
return w
end
---Set a window's size.
---
---### 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 window:set_size(size)
SendMsg({
SetWindowSize = {
window_id = self._id,
width = size.w,
height = size.h,
},
})
end
---Move a window to a tag, removing all other ones.
---
---### 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
---@param output Output?
---@overload fun(self: self, t: Tag)
function window:move_to_tag(name, output)
window_global.move_to_tag(self, name, output)
end
---Toggle the specified tag for this window.
---
---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
---@param output Output?
---@overload fun(self: self, t: Tag)
function window:toggle_tag(name, output)
window_global.toggle_tag(self, name, output)
end
---Close this window.
---
---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
---```
function window:close()
SendMsg({
CloseWindow = {
window_id = self._id,
},
})
end
---Toggle this window's floating status.
---
---### Example
---```lua
---window.get_focused():toggle_floating() -- toggles the focused window between tiled and floating
---```
function window:toggle_floating()
SendMsg({
ToggleFloating = {
window_id = self._id,
},
})
end
---Get a window's size.
---
---### 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 window:size()
local response = Request({
GetWindowProps = {
window_id = self._id,
},
})
local size = response.RequestResponse.response.WindowProps.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 window:loc()
local response = Request({
GetWindowProps = {
window_id = self._id,
},
})
local loc = response.RequestResponse.response.WindowProps.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 window:class()
local response = Request({
GetWindowProps = {
window_id = self._id,
},
})
local class = response.RequestResponse.response.WindowProps.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 window:title()
local response = Request({
GetWindowProps = {
window_id = self._id,
},
})
local title = response.RequestResponse.response.WindowProps.title
return title
end
---Get this window's floating status.
---
---### Example
---```lua
----- With the focused window floating...
---print(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 window:floating()
local response = Request({
GetWindowProps = {
window_id = self._id,
},
})
local floating = response.RequestResponse.response.WindowProps.floating
return floating
end
---Get whether or not this window is focused.
---
---### Example
---```lua
---print(window.get_focused():focused()) -- 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 window:focused()
local response = Request({
GetWindowProps = {
window_id = self._id,
},
})
local focused = response.RequestResponse.response.WindowProps.focused
return focused
end
---@return WindowId
function window:id()
return self._id
end
-------------------------------------------------------------------
---Get all windows with the specified class (usually the name of the application).
---@param class string The class. For example, Alacritty's class is "Alacritty".
---@return Window[]
function window_global.get_by_class(class)
local windows = window_global.get_all()
---@type Window[]
local windows_ret = {}
for _, w in pairs(windows) do
if w:class() == class then
table.insert(windows_ret, w)
end
end
return windows_ret
end
---Get all windows with the specified title.
---@param title string The title.
---@return Window[]
function window_global.get_by_title(title)
local windows = window_global.get_all()
---@type Window[]
local windows_ret = {}
for _, w in pairs(windows) do
if w:title() == title then
table.insert(windows_ret, w)
end
end
return windows_ret
end
---Get the currently focused window.
---@return Window|nil
function window_global.get_focused()
local windows = window_global.get_all()
for _, w in pairs(windows) do
if w:focused() then
return w
end
end
return nil
end
---Get all windows.
---@return Window[]
function window_global.get_all()
local window_ids = Request("GetWindows").RequestResponse.response.Windows.window_ids
---@type Window[]
local windows = {}
for _, window_id in pairs(window_ids) do
table.insert(windows, new_window(window_id))
end
return windows
end
---comment
---@param w Window
---@param name string
---@param output Output?
function window_global.toggle_tag(w, name, output)
if type(name) == "table" then
SendMsg({
ToggleTagOnWindow = {
window_id = w:id(),
tag_id = name--[[@as Tag]]:id(),
},
})
return
end
local output = output or require("output").get_focused()
if output == nil then
return
end
local tags = require("tag").get_by_name(name)
for _, t in pairs(tags) do
if t:output() and t:output():name() == output:name() then
SendMsg({
ToggleTagOnWindow = {
window_id = w:id(),
tag_id = t:id(),
},
})
return
end
end
end
---comment
---@param w Window
---@param name string
---@param output Output?
---@overload fun(w: Window, t: Tag)
function window_global.move_to_tag(w, name, output)
if type(name) == "table" then
SendMsg({
MoveWindowToTag = {
window_id = w:id(),
tag_id = name--[[@as Tag]]:id(),
},
})
return
end
local output = output or require("output").get_focused()
if output == nil then
return
end
local tags = require("tag").get_by_name(name)
for _, t in pairs(tags) do
if t:output() and t:output():name() == output:name() then
SendMsg({
MoveWindowToTag = {
window_id = w:id(),
tag_id = t:id(),
},
})
return
end
end
end
return window_global