pinnacle/api/lua/window.lua

587 lines
16 KiB
Lua
Raw Normal View History

2023-08-01 18:06:35 +02:00
-- SPDX-License-Identifier: GPL-3.0-or-later
2023-06-26 00:18:50 +02:00
2023-08-29 02:47:29 +02:00
---Window management.
---
---This module helps you deal with setting windows to fullscreen and maximized, setting their size,
---moving them between tags, and various other actions.
---@class Window
local window = {
2023-09-06 05:13:43 +02:00
---Window rules.
rules = require("window_rules"),
}
---A window handle.
---
---This is a handle to an application window that allows manipulation of the window.
2023-08-29 02:47:29 +02:00
---
---If the window is destroyed, the handle will become invalid and may not do
---what you want it to.
2023-08-29 02:47:29 +02:00
---
---You can retrieve window handles through the various `get` functions in the `Window` module.
2023-08-27 05:26:32 +02:00
---@classmod
---@class WindowHandle
---@field private _id WindowId The internal id of this window
local window_handle = {}
---@param window_id WindowId
---@return WindowHandle
2023-07-22 04:02:02 +02:00
local function create_window(window_id)
---@type WindowHandle
local w = { _id = window_id }
2023-06-27 04:05:29 +02:00
-- Copy functions over
for k, v in pairs(window_handle) do
w[k] = v
end
return w
end
2023-07-22 04:02:02 +02:00
---Get this window's unique id.
---
---***You will probably not need to use this.***
---@return WindowId
function window_handle:id()
2023-07-22 04:02:02 +02:00
return self._id
end
---Set this window's size.
2023-07-19 04:10:43 +02:00
---
---See `Window.set_size` for examples.
2023-08-22 03:37:15 +02:00
---
---@param size { w: integer?, h: integer? }
---@see Window.set_size — The corresponding module function
function window_handle:set_size(size)
window.set_size(self, size)
end
2023-07-22 04:02:02 +02:00
---Move this window to a tag, removing all other ones.
2023-07-19 04:10:43 +02:00
---
---See `Window.move_to_tag` for examples.
2023-08-22 03:37:15 +02:00
---
2023-09-08 03:36:49 +02:00
---@param t TagConstructor
---@see Window.move_to_tag — The corresponding module function
function window_handle:move_to_tag(t)
window.move_to_tag(self, t)
end
---Toggle the specified tag for this window.
2023-07-19 04:10:43 +02:00
---
2023-08-22 03:37:15 +02:00
---Note: toggling off all tags currently makes a window not respond to layouting.
2023-07-19 04:10:43 +02:00
---
---See `Window.toggle_tag` for examples.
2023-09-08 03:36:49 +02:00
---@param t TagConstructor
---@see Window.toggle_tag — The corresponding module function
function window_handle:toggle_tag(t)
window.toggle_tag(self, t)
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.
---
---See `Window.close` for examples.
---@see Window.close — The corresponding module function
function window_handle:close()
window.close(self)
2023-06-15 19:42:34 +02:00
end
2023-07-22 04:02:02 +02:00
---Get this window's size.
2023-07-19 04:10:43 +02:00
---
---See `Window.size` for examples.
2023-07-19 04:10:43 +02:00
---@return { w: integer, h: integer }|nil size The size of the window, or nil if it doesn't exist.
---@see Window.size — The corresponding module function
function window_handle:size()
return window.size(self)
2023-07-19 04:10:43 +02:00
end
---Get this window's location in the global space.
---
---Think of your monitors as being laid out on a big sheet.
2023-08-22 03:37:15 +02:00
---The location of this window is relative inside the sheet.
2023-07-19 04:10:43 +02:00
---
2023-08-22 03:37:15 +02:00
---If you don't set the location of your monitors, they will start at (0, 0)
---and extend rightward with their tops aligned.
---
---See `Window.loc` for examples.
2023-07-19 04:10:43 +02:00
---@return { x: integer, y: integer }|nil loc The location of the window, or nil if it's not on-screen or alive.
---@see Window.loc — The corresponding module function
function window_handle:loc()
return window.loc(self)
2023-07-19 04:10:43 +02:00
end
---Get this window's class. This is usually the name of the application.
---
---See `Window.class` for examples.
2023-07-19 04:10:43 +02:00
---@return string|nil class This window's class, or nil if it doesn't exist.
---@see Window.class — The corresponding module function
function window_handle:class()
return window.class(self)
2023-07-19 04:10:43 +02:00
end
---Get this window's title.
---
---See `Window.title` for examples.
2023-07-19 04:10:43 +02:00
---@return string|nil title This window's title, or nil if it doesn't exist.
---@see Window.title — The corresponding module function
function window_handle:title()
return window.title(self)
2023-07-19 04:10:43 +02:00
end
2023-08-14 21:35:06 +02:00
---Get this window's floating status.
---@return boolean|nil
---@see Window.floating — The corresponding module function
function window_handle:floating()
return window.floating(self)
end
2023-08-14 21:35:06 +02:00
---Get this window's fullscreen status.
---@return boolean|nil
---@see Window.fullscreen — The corresponding module function
function window_handle:fullscreen()
return window.fullscreen(self)
end
2023-08-14 21:35:06 +02:00
---Get this window's maximized status.
---@return boolean|nil
---@see Window.maximized — The corresponding module function
function window_handle:maximized()
return window.maximized(self)
end
2023-08-14 21:35:06 +02:00
---Toggle this window's floating status.
---
---When used on a floating window, this will change it to tiled, and vice versa.
---
---When used on a fullscreen or maximized window, this will still change its
---underlying floating/tiled status.
function window_handle:toggle_floating()
window.toggle_floating(self)
end
2023-08-14 21:35:06 +02:00
---Toggle this window's fullscreen status.
---
---When used on a fullscreen window, this will change the window back to
---floating or tiled.
---
---When used on a non-fullscreen window, it becomes fullscreen.
function window_handle:toggle_fullscreen()
window.toggle_fullscreen(self)
end
2023-08-14 21:35:06 +02:00
---Toggle this window's maximized status.
---
---When used on a maximized window, this will change the window back to
---floating or tiled.
---
---When used on a non-maximized window, it becomes maximized.
function window_handle:toggle_maximized()
window.toggle_maximized(self)
2023-07-18 22:12:23 +02:00
end
2023-07-20 03:11:15 +02:00
---Get whether or not this window is focused.
2023-07-11 18:59:38 +02:00
---
---See `Window.focused` for examples.
2023-08-14 21:35:06 +02:00
---@return boolean|nil
---@see Window.focused — The corresponding module function
function window_handle:focused()
return window.focused(self)
end
2023-07-18 22:12:23 +02:00
-------------------------------------------------------------------
2023-07-20 03:11:15 +02:00
---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 WindowHandle[]
function window.get_by_class(class)
local windows = window.get_all()
---@type WindowHandle[]
2023-07-20 18:56:45 +02:00
local windows_ret = {}
for _, w in pairs(windows) do
2023-07-20 03:11:15 +02:00
if w:class() == class then
2023-07-20 18:56:45 +02:00
table.insert(windows_ret, w)
2023-07-20 03:11:15 +02:00
end
end
2023-07-20 18:56:45 +02:00
return windows_ret
end
2023-07-20 03:11:15 +02:00
---Get all windows with the specified title.
---@param title string The title.
---@return WindowHandle[]
function window.get_by_title(title)
local windows = window.get_all()
2023-07-18 22:12:23 +02:00
---@type WindowHandle[]
2023-07-20 18:56:45 +02:00
local windows_ret = {}
for _, w in pairs(windows) do
2023-07-20 03:11:15 +02:00
if w:title() == title then
2023-07-20 18:56:45 +02:00
table.insert(windows_ret, w)
2023-07-20 03:11:15 +02:00
end
2023-07-18 22:12:23 +02:00
end
2023-07-20 18:56:45 +02:00
return windows_ret
end
---Get the currently focused window.
2023-10-19 03:35:36 +02:00
---
---@return WindowHandle handle A handle to the currently focused window. If there are none, this returns a dummy handle that can still be used but will be ignored by the compositor.
function window.get_focused()
2023-09-10 05:47:59 +02:00
-- TODO: get focused on output
local windows = window.get_all()
2023-07-20 18:56:45 +02:00
for _, w in pairs(windows) do
2023-07-20 03:11:15 +02:00
if w:focused() then
return w
end
2023-07-18 22:12:23 +02:00
end
2023-06-28 23:42:07 +02:00
return create_window("None")
end
---Get all windows.
---@return WindowHandle[]
function window.get_all()
2023-09-10 05:47:59 +02:00
local window_ids = Request("GetWindows").RequestResponse.response.Windows.window_ids
2023-08-22 03:37:15 +02:00
---@type WindowHandle[]
2023-06-27 04:05:29 +02:00
local windows = {}
2023-08-22 03:37:15 +02:00
2023-07-20 03:11:15 +02:00
for _, window_id in pairs(window_ids) do
2023-07-22 04:02:02 +02:00
table.insert(windows, create_window(window_id))
end
2023-08-22 03:37:15 +02:00
return windows
end
2023-07-22 04:02:02 +02:00
---Toggle the tag with the given name and (optional) output for the specified window.
2023-08-22 03:37:15 +02:00
---
---@param w WindowHandle
2023-09-08 03:36:49 +02:00
---@param t TagConstructor
---@see WindowHandle.toggle_tag — The corresponding object method
function window.toggle_tag(w, t)
2023-09-08 03:36:49 +02:00
local t = require("tag").get(t)
2023-08-22 03:47:51 +02:00
if t then
SendMsg({
ToggleTagOnWindow = {
window_id = w:id(),
2023-08-22 03:47:51 +02:00
tag_id = t:id(),
},
})
end
end
2023-07-22 04:02:02 +02:00
---Move the specified window to the tag with the given name and (optional) output.
2023-08-22 03:47:51 +02:00
---
---@param w WindowHandle
2023-09-08 03:36:49 +02:00
---@param t TagConstructor
---@see WindowHandle.move_to_tag — The corresponding object method
function window.move_to_tag(w, t)
2023-09-08 03:36:49 +02:00
local t = require("tag").get(t)
2023-08-22 03:47:51 +02:00
if t then
SendMsg({
MoveWindowToTag = {
window_id = w:id(),
2023-08-22 03:47:51 +02:00
tag_id = t:id(),
},
})
end
end
2023-08-14 21:35:06 +02:00
---Toggle `win`'s floating status.
---
---When used on a floating window, this will change it to tiled, and vice versa.
---
---When used on a fullscreen or maximized window, this will still change its
---underlying floating/tiled status.
---@param win WindowHandle
function window.toggle_floating(win)
2023-08-14 21:35:06 +02:00
SendMsg({
ToggleFloating = {
window_id = win:id(),
},
})
end
---Toggle `win`'s fullscreen status.
---
---When used on a fullscreen window, this will change the window back to
---floating or tiled.
---
---When used on a non-fullscreen window, it becomes fullscreen.
---@param win WindowHandle
function window.toggle_fullscreen(win)
2023-08-14 21:35:06 +02:00
SendMsg({
ToggleFullscreen = {
2023-08-14 21:35:06 +02:00
window_id = win:id(),
},
})
end
---Toggle `win`'s maximized status.
---
---When used on a maximized window, this will change the window back to
---floating or tiled.
---
---When used on a non-maximized window, it becomes maximized.
---@param win WindowHandle
function window.toggle_maximized(win)
2023-08-14 21:35:06 +02:00
SendMsg({
ToggleMaximized = {
2023-08-14 21:35:06 +02:00
window_id = win:id(),
},
})
end
2023-07-22 04:02:02 +02:00
---Set the specified window's size.
---
---### Examples
---```lua
---local win = window.get_focused()
---if win ~= nil then
--- window.set_size(win, { w = 500, h = 500 }) -- make the window square and 500 pixels wide/tall
--- window.set_size(win, { h = 300 }) -- keep the window's width but make it 300 pixels tall
--- window.set_size(win, {}) -- do absolutely nothing useful
---end
---```
---@param win WindowHandle
2023-07-22 04:02:02 +02:00
---@param size { w: integer?, h: integer? }
---@see WindowHandle.set_size — The corresponding object method
function window.set_size(win, size)
2023-07-22 04:02:02 +02:00
SendMsg({
SetWindowSize = {
window_id = win:id(),
width = size.w,
height = size.h,
},
})
end
---Close the specified 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
---local win = window.get_focused()
---if win ~= nil then
--- window.close(win) -- close the currently focused window
---end
---```
---@param win WindowHandle
---@see WindowHandle.close — The corresponding object method
function window.close(win)
2023-07-22 04:02:02 +02:00
SendMsg({
CloseWindow = {
window_id = win:id(),
},
})
end
---Get the specified window's size.
---
---### Example
---```lua
2023-09-08 03:48:41 +02:00
--- -- With a 4K monitor, given a focused fullscreen window `win`...
2023-07-22 04:02:02 +02:00
---local size = window.size(win)
2023-09-08 03:48:41 +02:00
--- -- ...should have size equal to `{ w = 3840, h = 2160 }`.
2023-07-22 04:02:02 +02:00
---```
---@param win WindowHandle
2023-07-22 04:02:02 +02:00
---@return { w: integer, h: integer }|nil size The size of the window, or nil if it doesn't exist.
---@see WindowHandle.size — The corresponding object method
function window.size(win)
2023-07-22 04:02:02 +02:00
local response = Request({
GetWindowProps = {
window_id = win: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 the specified window's location in the global space.
---
---Think of your monitors as being laid out on a big sheet.
2023-08-22 03:37:15 +02:00
---The location of this window is relative inside the sheet.
---
---If you don't set the location of your monitors, they will start at (0, 0)
---and extend rightward with their tops aligned.
2023-07-22 04:02:02 +02:00
---
---### Example
---```lua
2023-09-08 03:48:41 +02:00
--- -- With two 1080p monitors side by side and set up as such,
--- -- if a window `win` is fullscreen on the right one...
2023-07-22 04:02:02 +02:00
---local loc = window.loc(win)
2023-09-08 03:48:41 +02:00
--- -- ...should have loc equal to `{ x = 1920, y = 0 }`.
2023-07-22 04:02:02 +02:00
---```
---@param win WindowHandle
2023-07-22 04:02:02 +02:00
---@return { x: integer, y: integer }|nil loc The location of the window, or nil if it's not on-screen or alive.
---@see WindowHandle.loc — The corresponding object method
function window.loc(win)
2023-07-22 04:02:02 +02:00
local response = Request({
GetWindowProps = {
window_id = win: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 the specified window's class. This is usually the name of the application.
---
---### Example
---```lua
2023-09-08 03:48:41 +02:00
--- -- With Alacritty focused...
2023-07-22 04:02:02 +02:00
---local win = window.get_focused()
---if win ~= nil then
--- print(window.class(win))
---end
2023-09-08 03:48:41 +02:00
--- -- ...should print "Alacritty".
2023-07-22 04:02:02 +02:00
---```
---@param win WindowHandle
2023-07-22 04:02:02 +02:00
---@return string|nil class This window's class, or nil if it doesn't exist.
---@see WindowHandle.class — The corresponding object method
function window.class(win)
2023-07-22 04:02:02 +02:00
local response = Request({
GetWindowProps = {
window_id = win:id(),
},
})
local class = response.RequestResponse.response.WindowProps.class
return class
end
---Get the specified window's title.
---
---### Example
---```lua
2023-09-08 03:48:41 +02:00
--- -- With Alacritty focused...
2023-07-22 04:02:02 +02:00
---local win = window.get_focused()
---if win ~= nil then
--- print(window.title(win))
---end
2023-09-08 03:48:41 +02:00
--- -- ...should print the directory Alacritty is in or what it's running (what's in its title bar).
2023-07-22 04:02:02 +02:00
---```
---@param win WindowHandle
2023-07-22 04:02:02 +02:00
---@return string|nil title This window's title, or nil if it doesn't exist.
---@see WindowHandle.title — The corresponding object method
function window.title(win)
2023-07-22 04:02:02 +02:00
local response = Request({
GetWindowProps = {
window_id = win:id(),
},
})
local title = response.RequestResponse.response.WindowProps.title
return title
end
---Get this window's floating status.
---@param win WindowHandle
---@return boolean|nil
---@see WindowHandle.floating — The corresponding object method
function window.floating(win)
local response = Request({
GetWindowProps = {
window_id = win:id(),
},
})
local floating = response.RequestResponse.response.WindowProps.floating
return floating
end
---Get this window's fullscreen status.
---@param win WindowHandle
---@return boolean|nil
---@see WindowHandle.fullscreen — The corresponding object method
function window.fullscreen(win)
local response = Request({
GetWindowProps = {
window_id = win:id(),
},
})
2023-09-10 05:47:59 +02:00
local fom = response.RequestResponse.response.WindowProps.fullscreen_or_maximized
return fom == "Fullscreen"
end
---Get this window's maximized status.
---@param win WindowHandle
---@return boolean|nil
---@see WindowHandle.maximized — The corresponding object method
function window.maximized(win)
2023-07-22 04:02:02 +02:00
local response = Request({
GetWindowProps = {
window_id = win:id(),
},
})
2023-09-10 05:47:59 +02:00
local fom = response.RequestResponse.response.WindowProps.fullscreen_or_maximized
return fom == "Maximized"
2023-07-22 04:02:02 +02:00
end
---Get whether or not this window is focused.
---
---### Example
---```lua
---local win = window.get_focused()
---if win ~= nil then
--- print(window.focused(win)) -- Should print `true`
---end
---```
---@param win WindowHandle
2023-08-14 21:35:06 +02:00
---@return boolean|nil
---@see WindowHandle.focused — The corresponding object method
function window.focused(win)
2023-07-22 04:02:02 +02:00
local response = Request({
GetWindowProps = {
window_id = win:id(),
},
})
local focused = response.RequestResponse.response.WindowProps.focused
return focused
end
2023-09-11 04:54:58 +02:00
---Begin a window move.
---
---This will start a window move grab with the provided button on the window the pointer
---is currently hovering over. Once `button` is let go, the move will end.
---@param button MouseButton The button you want to trigger the move.
function window.begin_move(button)
2023-09-11 04:54:58 +02:00
SendMsg({
WindowMoveGrab = {
button = button,
},
})
end
---Begin a window resize.
---
---This will start a window resize grab with the provided button on the window the
---pointer is currently hovering over. Once `button` is let go, the resize will end.
---@param button MouseButton
function window.begin_resize(button)
2023-09-11 04:54:58 +02:00
SendMsg({
WindowResizeGrab = {
button = button,
},
})
end
return window