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.
|
2023-07-22 04:02:02 +02:00
|
|
|
---@class WindowModule
|
2023-09-06 05:13:43 +02:00
|
|
|
local window_module = {
|
|
|
|
---Window rules.
|
|
|
|
rules = require("window_rules"),
|
|
|
|
}
|
2023-07-21 21:36:32 +02:00
|
|
|
|
2023-08-29 02:47:29 +02:00
|
|
|
---A window object.
|
|
|
|
---
|
|
|
|
---This is a representation of an application window to the config process.
|
|
|
|
---
|
|
|
|
---You can retrieve windows through the various `get` function in the `WindowModule`.
|
2023-08-27 05:26:32 +02:00
|
|
|
---@classmod
|
2023-06-27 01:48:29 +02:00
|
|
|
---@class Window
|
2023-07-21 21:36:32 +02:00
|
|
|
---@field private _id integer The internal id of this window
|
|
|
|
local window = {}
|
2023-06-27 01:48:29 +02:00
|
|
|
|
2023-07-21 21:36:32 +02:00
|
|
|
---@param window_id WindowId
|
2023-06-27 01:48:29 +02:00
|
|
|
---@return Window
|
2023-07-22 04:02:02 +02:00
|
|
|
local function create_window(window_id)
|
2023-07-21 21:36:32 +02:00
|
|
|
---@type Window
|
|
|
|
local w = { _id = window_id }
|
2023-06-27 04:05:29 +02:00
|
|
|
-- Copy functions over
|
2023-07-21 21:36:32 +02:00
|
|
|
for k, v in pairs(window) do
|
|
|
|
w[k] = v
|
2023-06-27 01:48:29 +02:00
|
|
|
end
|
|
|
|
|
2023-07-21 21:36:32 +02:00
|
|
|
return w
|
2023-06-27 01:48:29 +02:00
|
|
|
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:id()
|
|
|
|
return self._id
|
|
|
|
end
|
|
|
|
|
|
|
|
---Set this window's size.
|
2023-07-19 04:10:43 +02:00
|
|
|
---
|
2023-08-22 03:37:15 +02:00
|
|
|
---See `WindowModule.set_size` for examples.
|
|
|
|
---
|
2023-06-27 01:48:29 +02:00
|
|
|
---@param size { w: integer?, h: integer? }
|
2023-07-22 04:44:56 +02:00
|
|
|
---@see WindowModule.set_size — The corresponding module function
|
2023-07-21 21:36:32 +02:00
|
|
|
function window:set_size(size)
|
2023-07-22 04:02:02 +02:00
|
|
|
window_module.set_size(self, size)
|
2023-06-27 01:48:29 +02:00
|
|
|
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
|
|
|
---
|
2023-08-22 03:37:15 +02:00
|
|
|
---See `WindowModule.move_to_tag` for examples.
|
|
|
|
---
|
2023-09-08 03:36:49 +02:00
|
|
|
---@param t TagConstructor
|
2023-07-22 04:44:56 +02:00
|
|
|
---@see WindowModule.move_to_tag — The corresponding module function
|
2023-08-22 04:04:57 +02:00
|
|
|
function window:move_to_tag(t)
|
|
|
|
window_module.move_to_tag(self, t)
|
2023-07-02 17:26:07 +02:00
|
|
|
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
|
|
|
---
|
2023-08-22 03:37:15 +02:00
|
|
|
---See `WindowModule.toggle_tag` for examples.
|
2023-09-08 03:36:49 +02:00
|
|
|
---@param t TagConstructor
|
2023-07-22 04:44:56 +02:00
|
|
|
---@see WindowModule.toggle_tag — The corresponding module function
|
2023-08-22 04:04:57 +02:00
|
|
|
function window:toggle_tag(t)
|
|
|
|
window_module.toggle_tag(self, t)
|
2023-07-02 17:26:07 +02:00
|
|
|
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.
|
|
|
|
---
|
2023-08-22 03:37:15 +02:00
|
|
|
---See `WindowModule.close` for examples.
|
2023-07-22 04:44:56 +02:00
|
|
|
---@see WindowModule.close — The corresponding module function
|
2023-07-21 21:36:32 +02:00
|
|
|
function window:close()
|
2023-07-22 04:02:02 +02:00
|
|
|
window_module.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
|
|
|
---
|
2023-08-22 03:37:15 +02:00
|
|
|
---See `WindowModule.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.
|
2023-07-22 04:44:56 +02:00
|
|
|
---@see WindowModule.size — The corresponding module function
|
2023-07-21 21:36:32 +02:00
|
|
|
function window:size()
|
2023-07-22 04:02:02 +02:00
|
|
|
return window_module.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 `WindowModule.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.
|
2023-07-22 04:44:56 +02:00
|
|
|
---@see WindowModule.loc — The corresponding module function
|
2023-07-21 21:36:32 +02:00
|
|
|
function window:loc()
|
2023-07-22 04:02:02 +02:00
|
|
|
return window_module.loc(self)
|
2023-07-19 04:10:43 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
---Get this window's class. This is usually the name of the application.
|
|
|
|
---
|
2023-08-22 03:37:15 +02:00
|
|
|
---See `WindowModule.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.
|
2023-07-22 04:44:56 +02:00
|
|
|
---@see WindowModule.class — The corresponding module function
|
2023-07-21 21:36:32 +02:00
|
|
|
function window:class()
|
2023-07-22 04:02:02 +02:00
|
|
|
return window_module.class(self)
|
2023-07-19 04:10:43 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
---Get this window's title.
|
|
|
|
---
|
2023-08-22 03:37:15 +02:00
|
|
|
---See `WindowModule.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.
|
2023-07-22 04:44:56 +02:00
|
|
|
---@see WindowModule.title — The corresponding module function
|
2023-07-21 21:36:32 +02:00
|
|
|
function window:title()
|
2023-07-22 04:02:02 +02:00
|
|
|
return window_module.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.
|
2023-08-14 20:54:50 +02:00
|
|
|
---@return boolean|nil
|
2023-08-14 21:35:06 +02:00
|
|
|
---@see WindowModule.floating — The corresponding module function
|
2023-08-14 20:54:50 +02:00
|
|
|
function window:floating()
|
|
|
|
return window_module.floating(self)
|
|
|
|
end
|
|
|
|
|
2023-08-14 21:35:06 +02:00
|
|
|
---Get this window's fullscreen status.
|
2023-08-14 20:54:50 +02:00
|
|
|
---@return boolean|nil
|
2023-08-14 21:35:06 +02:00
|
|
|
---@see WindowModule.fullscreen — The corresponding module function
|
2023-08-14 20:54:50 +02:00
|
|
|
function window:fullscreen()
|
|
|
|
return window_module.fullscreen(self)
|
|
|
|
end
|
|
|
|
|
2023-08-14 21:35:06 +02:00
|
|
|
---Get this window's maximized status.
|
2023-08-14 20:54:50 +02:00
|
|
|
---@return boolean|nil
|
2023-08-14 21:35:06 +02:00
|
|
|
---@see WindowModule.maximized — The corresponding module function
|
2023-08-14 20:54:50 +02:00
|
|
|
function window:maximized()
|
|
|
|
return window_module.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.
|
2023-08-14 20:54:50 +02:00
|
|
|
function window:toggle_floating()
|
2023-08-14 21:35:06 +02:00
|
|
|
window_module.toggle_floating(self)
|
2023-08-14 20:54:50 +02:00
|
|
|
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.
|
2023-08-14 20:54:50 +02:00
|
|
|
function window:toggle_fullscreen()
|
2023-08-14 21:35:06 +02:00
|
|
|
window_module.toggle_fullscreen(self)
|
2023-08-14 20:54:50 +02:00
|
|
|
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.
|
2023-08-14 20:54:50 +02:00
|
|
|
function window:toggle_maximized()
|
2023-08-14 21:35:06 +02:00
|
|
|
window_module.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
|
|
|
---
|
2023-08-22 03:37:15 +02:00
|
|
|
---See `WindowModule.focused` for examples.
|
2023-08-14 21:35:06 +02:00
|
|
|
---@return boolean|nil
|
2023-07-22 04:44:56 +02:00
|
|
|
---@see WindowModule.focused — The corresponding module function
|
2023-07-21 21:36:32 +02:00
|
|
|
function window:focused()
|
2023-07-22 04:02:02 +02:00
|
|
|
return window_module.focused(self)
|
2023-07-21 21:36:32 +02:00
|
|
|
end
|
2023-07-18 22:12:23 +02:00
|
|
|
|
2023-07-21 21:36:32 +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 Window[]
|
2023-07-22 04:02:02 +02:00
|
|
|
function window_module.get_by_class(class)
|
|
|
|
local windows = window_module.get_all()
|
2023-07-02 17:26:07 +02:00
|
|
|
|
2023-07-20 03:11:15 +02:00
|
|
|
---@type Window[]
|
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
|
2023-07-02 17:26:07 +02:00
|
|
|
end
|
|
|
|
|
2023-07-20 03:11:15 +02:00
|
|
|
---Get all windows with the specified title.
|
|
|
|
---@param title string The title.
|
|
|
|
---@return Window[]
|
2023-07-22 04:02:02 +02:00
|
|
|
function window_module.get_by_title(title)
|
|
|
|
local windows = window_module.get_all()
|
2023-07-18 22:12:23 +02:00
|
|
|
|
2023-07-20 03:11:15 +02:00
|
|
|
---@type Window[]
|
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-02 17:26:07 +02:00
|
|
|
|
2023-07-20 18:56:45 +02:00
|
|
|
return windows_ret
|
2023-07-02 17:26:07 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
---Get the currently focused window.
|
2023-07-18 22:12:23 +02:00
|
|
|
---@return Window|nil
|
2023-07-22 04:02:02 +02:00
|
|
|
function window_module.get_focused()
|
2023-09-10 05:47:59 +02:00
|
|
|
-- TODO: get focused on output
|
2023-07-22 04:02:02 +02:00
|
|
|
local windows = window_module.get_all()
|
2023-06-27 01:48:29 +02:00
|
|
|
|
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
|
|
|
|
2023-07-20 03:11:15 +02:00
|
|
|
return nil
|
2023-06-27 01:48:29 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
---Get all windows.
|
|
|
|
---@return Window[]
|
2023-07-22 04:02:02 +02:00
|
|
|
function window_module.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
|
|
|
|
2023-06-27 04:05:29 +02:00
|
|
|
---@type Window[]
|
|
|
|
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))
|
2023-06-27 01:48:29 +02:00
|
|
|
end
|
2023-08-22 03:37:15 +02:00
|
|
|
|
2023-06-27 01:48:29 +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
|
|
|
---
|
2023-07-21 21:36:32 +02:00
|
|
|
---@param w Window
|
2023-09-08 03:36:49 +02:00
|
|
|
---@param t TagConstructor
|
2023-07-22 04:44:56 +02:00
|
|
|
---@see Window.toggle_tag — The corresponding object method
|
2023-08-22 03:47:51 +02:00
|
|
|
function window_module.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
|
2023-07-21 21:36:32 +02:00
|
|
|
SendMsg({
|
|
|
|
ToggleTagOnWindow = {
|
|
|
|
window_id = w:id(),
|
2023-08-22 03:47:51 +02:00
|
|
|
tag_id = t:id(),
|
2023-07-21 21:36:32 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
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
|
|
|
---
|
2023-07-21 21:36:32 +02:00
|
|
|
---@param w Window
|
2023-09-08 03:36:49 +02:00
|
|
|
---@param t TagConstructor
|
2023-07-22 04:44:56 +02:00
|
|
|
---@see Window.move_to_tag — The corresponding object method
|
2023-08-22 03:47:51 +02:00
|
|
|
function window_module.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
|
2023-07-21 21:36:32 +02:00
|
|
|
SendMsg({
|
|
|
|
MoveWindowToTag = {
|
|
|
|
window_id = w:id(),
|
2023-08-22 03:47:51 +02:00
|
|
|
tag_id = t:id(),
|
2023-07-21 21:36:32 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
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 Window
|
|
|
|
function window_module.toggle_floating(win)
|
|
|
|
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 Window
|
|
|
|
function window_module.toggle_fullscreen(win)
|
|
|
|
SendMsg({
|
2023-08-15 03:23:23 +02:00
|
|
|
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 Window
|
|
|
|
function window_module.toggle_maximized(win)
|
|
|
|
SendMsg({
|
2023-08-15 03:23:23 +02:00
|
|
|
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 Window
|
|
|
|
---@param size { w: integer?, h: integer? }
|
2023-07-22 04:44:56 +02:00
|
|
|
---@see Window.set_size — The corresponding object method
|
2023-07-22 04:02:02 +02:00
|
|
|
function window_module.set_size(win, size)
|
|
|
|
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 Window
|
2023-07-22 04:44:56 +02:00
|
|
|
---@see Window.close — The corresponding object method
|
2023-07-22 04:02:02 +02:00
|
|
|
function window_module.close(win)
|
|
|
|
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 Window
|
|
|
|
---@return { w: integer, h: integer }|nil size The size of the window, or nil if it doesn't exist.
|
2023-07-22 04:44:56 +02:00
|
|
|
---@see Window.size — The corresponding object method
|
2023-07-22 04:02:02 +02:00
|
|
|
function window_module.size(win)
|
|
|
|
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 Window
|
|
|
|
---@return { x: integer, y: integer }|nil loc The location of the window, or nil if it's not on-screen or alive.
|
2023-07-22 04:44:56 +02:00
|
|
|
---@see Window.loc — The corresponding object method
|
2023-07-22 04:02:02 +02:00
|
|
|
function window_module.loc(win)
|
|
|
|
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 Window
|
|
|
|
---@return string|nil class This window's class, or nil if it doesn't exist.
|
2023-07-22 04:44:56 +02:00
|
|
|
---@see Window.class — The corresponding object method
|
2023-07-22 04:02:02 +02:00
|
|
|
function window_module.class(win)
|
|
|
|
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 Window
|
|
|
|
---@return string|nil title This window's title, or nil if it doesn't exist.
|
2023-07-22 04:44:56 +02:00
|
|
|
---@see Window.title — The corresponding object method
|
2023-07-22 04:02:02 +02:00
|
|
|
function window_module.title(win)
|
|
|
|
local response = Request({
|
|
|
|
GetWindowProps = {
|
|
|
|
window_id = win:id(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
local title = response.RequestResponse.response.WindowProps.title
|
|
|
|
return title
|
|
|
|
end
|
|
|
|
|
2023-08-14 20:54:50 +02:00
|
|
|
---Get this window's floating status.
|
|
|
|
---@param win Window
|
|
|
|
---@return boolean|nil
|
2023-08-14 21:35:06 +02:00
|
|
|
---@see Window.floating — The corresponding object method
|
2023-08-14 20:54:50 +02:00
|
|
|
function window_module.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 Window
|
|
|
|
---@return boolean|nil
|
2023-08-14 21:35:06 +02:00
|
|
|
---@see Window.fullscreen — The corresponding object method
|
2023-08-14 20:54:50 +02:00
|
|
|
function window_module.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
|
2023-08-14 20:54:50 +02:00
|
|
|
return fom == "Fullscreen"
|
|
|
|
end
|
|
|
|
|
|
|
|
---Get this window's maximized status.
|
2023-07-22 04:02:02 +02:00
|
|
|
---@param win Window
|
2023-08-14 20:54:50 +02:00
|
|
|
---@return boolean|nil
|
2023-08-14 21:35:06 +02:00
|
|
|
---@see Window.maximized — The corresponding object method
|
2023-08-14 20:54:50 +02:00
|
|
|
function window_module.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
|
2023-08-14 20:54:50 +02:00
|
|
|
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 Window
|
2023-08-14 21:35:06 +02:00
|
|
|
---@return boolean|nil
|
2023-07-22 04:44:56 +02:00
|
|
|
---@see Window.focused — The corresponding object method
|
2023-07-22 04:02:02 +02:00
|
|
|
function window_module.focused(win)
|
|
|
|
local response = Request({
|
|
|
|
GetWindowProps = {
|
|
|
|
window_id = win:id(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
local focused = response.RequestResponse.response.WindowProps.focused
|
|
|
|
return focused
|
|
|
|
end
|
|
|
|
return window_module
|