diff options
author | Aylur <[email protected]> | 2024-10-15 13:25:45 +0000 |
---|---|---|
committer | Aylur <[email protected]> | 2024-10-15 13:29:19 +0000 |
commit | bafd48d3df9b43a1d49ec015eff30619d595468b (patch) | |
tree | d5c3788835ca7e50d68cd023026e7738f39f6f71 /lang/lua/astal/gtk3/app.lua | |
parent | fe11c037bad45697451b7264ff93fa37f1fac78f (diff) |
update lua and gjs layout
installing the gjs package through meson or npm now results in the same
exposed structure
lua: fix rockspec
docs: aur package
Diffstat (limited to 'lang/lua/astal/gtk3/app.lua')
-rw-r--r-- | lang/lua/astal/gtk3/app.lua | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/lang/lua/astal/gtk3/app.lua b/lang/lua/astal/gtk3/app.lua new file mode 100644 index 0000000..7895f69 --- /dev/null +++ b/lang/lua/astal/gtk3/app.lua @@ -0,0 +1,96 @@ +local lgi = require("lgi") +local Astal = lgi.require("Astal", "3.0") +local AstalIO = lgi.require("AstalIO", "0.1") + +local AstalLua = Astal.Application:derive("AstalLua") +local request_handler + +function AstalLua:do_request(msg, conn) + if type(request_handler) == "function" then + request_handler(msg, function(response) + AstalIO.write_sock(conn, tostring(response), function(_, res) + AstalIO.write_sock_finish(res) + end) + end) + else + Astal.Application.do_request(self, msg, conn) + end +end + +function AstalLua:quit(code) + Astal.Application.quit(self) + os.exit(code) +end + +local app = AstalLua() + +---@class StartConfig +---@field icons? string +---@field instance_name? string +---@field gtk_theme? string +---@field icon_theme? string +---@field cursor_theme? string +---@field css? string +---@field hold? boolean +---@field request_handler? fun(msg: string, response: fun(res: any)) +---@field main? fun(...): unknown +---@field client? fun(message: fun(msg: string): string, ...): unknown + +---@param config StartConfig | nil +function Astal.Application:start(config) + if config == nil then + config = {} + end + + if config.client == nil then + config.client = function() + print('Astal instance "' .. app.instance_name .. '" is already running') + os.exit(1) + end + end + + if config.hold == nil then + config.hold = true + end + + request_handler = config.request_handler + + if config.css then + self:apply_css(config.css) + end + if config.icons then + self:add_icons(config.icons) + end + if config.instance_name then + self.instance_name = config.instance_name + end + if config.gtk_theme then + self.gtk_theme = config.gtk_theme + end + if config.icon_theme then + self.icon_theme = config.icon_theme + end + if config.cursor_theme then + self.cursor_theme = config.cursor_theme + end + + app.on_activate = function() + if type(config.main) == "function" then + config.main(table.unpack(arg)) + end + if config.hold then + self:hold() + end + end + + local _, err = app:acquire_socket() + if err ~= nil then + return config.client(function(msg) + return AstalIO.send_message(self.instance_name, msg) + end, table.unpack(arg)) + end + + self:run(nil) +end + +return app |