From 18df91b128f6ca8850e39cb53b8536a8207e1741 Mon Sep 17 00:00:00 2001 From: Kevin Date: Sun, 13 Oct 2024 12:57:37 -0300 Subject: core: lua compat 5.1/5.4/luajit --- core/lua/astal/application.lua | 13 +++++++++++-- core/lua/astal/variable.lua | 9 +++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/core/lua/astal/application.lua b/core/lua/astal/application.lua index 663a457..88650a8 100644 --- a/core/lua/astal/application.lua +++ b/core/lua/astal/application.lua @@ -4,6 +4,15 @@ local Astal = lgi.require("Astal", "0.1") local AstalLua = Astal.Application:derive("AstalLua") local request_handler +local function unpack(t, i) + i = i or 1 + if t[i] == nil then + return nil + else + return t[i], unpack(t, i + 1) + end +end + function AstalLua:do_request(msg, conn) if type(request_handler) == "function" then request_handler(msg, function(response) @@ -75,7 +84,7 @@ function Astal.Application:start(config) app.on_activate = function() if type(config.main) == "function" then - config.main(table.unpack(arg)) + config.main(unpack(arg)) end if config.hold then self:hold() @@ -85,7 +94,7 @@ function Astal.Application:start(config) if not app:acquire_socket() then return config.client(function(msg) return Astal.Application.send_message(self.instance_name, msg) - end, table.unpack(arg)) + end, unpack(arg)) end self:run(nil) diff --git a/core/lua/astal/variable.lua b/core/lua/astal/variable.lua index 662eee7..bd95edb 100644 --- a/core/lua/astal/variable.lua +++ b/core/lua/astal/variable.lua @@ -5,6 +5,15 @@ local Binding = require("astal.binding") local Time = require("astal.time") local Process = require("astal.process") +local function unpack(t, i) + i = i or 1 + if t[i] == nil then + return nil + else + return t[i], unpack(t, i + 1) + end +end + ---@class Variable ---@field private variable table ---@field private err_handler? function -- cgit v1.2.3 From 1426fc6df0fa09e1b4ecd54da7e79ba06474e1d5 Mon Sep 17 00:00:00 2001 From: Kevin Date: Mon, 14 Oct 2024 10:27:57 -0300 Subject: core: table.unpack compat in lua --- core/lua/astal/application.lua | 13 ++----------- core/lua/astal/init.lua | 4 ++++ core/lua/astal/variable.lua | 11 +---------- 3 files changed, 7 insertions(+), 21 deletions(-) diff --git a/core/lua/astal/application.lua b/core/lua/astal/application.lua index 88650a8..663a457 100644 --- a/core/lua/astal/application.lua +++ b/core/lua/astal/application.lua @@ -4,15 +4,6 @@ local Astal = lgi.require("Astal", "0.1") local AstalLua = Astal.Application:derive("AstalLua") local request_handler -local function unpack(t, i) - i = i or 1 - if t[i] == nil then - return nil - else - return t[i], unpack(t, i + 1) - end -end - function AstalLua:do_request(msg, conn) if type(request_handler) == "function" then request_handler(msg, function(response) @@ -84,7 +75,7 @@ function Astal.Application:start(config) app.on_activate = function() if type(config.main) == "function" then - config.main(unpack(arg)) + config.main(table.unpack(arg)) end if config.hold then self:hold() @@ -94,7 +85,7 @@ function Astal.Application:start(config) if not app:acquire_socket() then return config.client(function(msg) return Astal.Application.send_message(self.instance_name, msg) - end, unpack(arg)) + end, table.unpack(arg)) end self:run(nil) diff --git a/core/lua/astal/init.lua b/core/lua/astal/init.lua index f56c3f5..7befc65 100644 --- a/core/lua/astal/init.lua +++ b/core/lua/astal/init.lua @@ -1,3 +1,7 @@ +if not table.unpack then + table.unpack = unpack +end + local lgi = require("lgi") local Astal = lgi.require("Astal", "0.1") local Gtk = lgi.require("Gtk", "3.0") diff --git a/core/lua/astal/variable.lua b/core/lua/astal/variable.lua index bd95edb..04cdeb2 100644 --- a/core/lua/astal/variable.lua +++ b/core/lua/astal/variable.lua @@ -5,15 +5,6 @@ local Binding = require("astal.binding") local Time = require("astal.time") local Process = require("astal.process") -local function unpack(t, i) - i = i or 1 - if t[i] == nil then - return nil - else - return t[i], unpack(t, i + 1) - end -end - ---@class Variable ---@field private variable table ---@field private err_handler? function @@ -282,4 +273,4 @@ return setmetatable(Variable, { __call = function(_, v) return Variable.new(v) end, -}) +}) \ No newline at end of file -- cgit v1.2.3 From ea6b3e6b24ced559c33b5f7df3fdd138409491d3 Mon Sep 17 00:00:00 2001 From: Kevin Date: Mon, 14 Oct 2024 11:56:20 -0300 Subject: notifications lua example --- examples/lua/notification-popups/README.md | 14 +++ examples/lua/notification-popups/init.lua | 21 ++++ examples/lua/notification-popups/lib.lua | 52 +++++++++ examples/lua/notification-popups/style.scss | 69 ++++++++++++ .../notification-popups/widget/notifications.lua | 125 +++++++++++++++++++++ 5 files changed, 281 insertions(+) create mode 100644 examples/lua/notification-popups/README.md create mode 100644 examples/lua/notification-popups/init.lua create mode 100644 examples/lua/notification-popups/lib.lua create mode 100644 examples/lua/notification-popups/style.scss create mode 100644 examples/lua/notification-popups/widget/notifications.lua diff --git a/examples/lua/notification-popups/README.md b/examples/lua/notification-popups/README.md new file mode 100644 index 0000000..bcc6bba --- /dev/null +++ b/examples/lua/notification-popups/README.md @@ -0,0 +1,14 @@ +# Simple Bar Example + +![simple-bar](https://github.com/user-attachments/assets/a306c864-56b7-44c4-8820-81f424f32b9b) + +A simple bar for Hyprland using + +- [Audio library](https://aylur.github.io/astal/guide/libraries/audio). +- [Battery library](https://aylur.github.io/astal/guide/libraries/battery). +- [Hyprland library](https://aylur.github.io/astal/guide/libraries/hyprland). +- [Mpris library](https://aylur.github.io/astal/guide/libraries/mpris). +- [Network library](https://aylur.github.io/astal/guide/libraries/network). +- [Tray library](https://aylur.github.io/astal/guide/libraries/tray). +- [WirePlumber library](https://aylur.github.io/astal/guide/libraries/wireplumber). +- [dart-sass](https://sass-lang.com/dart-sass/) as the css precompiler diff --git a/examples/lua/notification-popups/init.lua b/examples/lua/notification-popups/init.lua new file mode 100644 index 0000000..ffa910c --- /dev/null +++ b/examples/lua/notification-popups/init.lua @@ -0,0 +1,21 @@ +local astal = require("astal") +local Notify = astal.require("Notify") +local timeout = astal.timeout +local App = astal.App + +local Notifications = require("widget.notifications") +local src = require("lib").src + +local scss = src("style.scss") +local css = "/tmp/style.css" + +astal.exec("sass " .. scss .. " " .. css) + +App:start({ + css = css, + main = function() + for _, mon in pairs(App.monitors) do + Notifications(mon) + end + end, +}) diff --git a/examples/lua/notification-popups/lib.lua b/examples/lua/notification-popups/lib.lua new file mode 100644 index 0000000..2ae71a9 --- /dev/null +++ b/examples/lua/notification-popups/lib.lua @@ -0,0 +1,52 @@ +local Variable = require("astal").Variable +local astal = require("astal") +local Gtk = astal.Gtk + +local M = {} + +function M.src(path) + local str = debug.getinfo(2, "S").source:sub(2) + local src = str:match("(.*/)") or str:match("(.*\\)") or "./" + return src .. path +end + +---@generic T, R +---@param arr T[] +---@param func fun(T, integer): R +---@return R[] +function M.map(arr, func) + local new_arr = {} + for i, v in ipairs(arr) do + new_arr[i] = func(v, i) + end + return new_arr +end + +---@param name string +---@param size? 16 | 32 | 64 | 128 | 256 | 512 | number +function M.lookup_icon(name, size) + if not name or #name == 0 then + return + end + size = size or 256 + + local theme = Gtk.IconTheme.get_default() + local icon_info, path + + for _, n in ipairs({ + name, + string.lower(name), + string.upper(name), + }) do + icon_info = theme:lookup_icon(n, size, "USE_BUILTIN") + + if icon_info then + return icon_info + end + end + return false +end + +M.date = Variable(""):poll(1000, "date") + +return M diff --git a/examples/lua/notification-popups/style.scss b/examples/lua/notification-popups/style.scss new file mode 100644 index 0000000..8eb764a --- /dev/null +++ b/examples/lua/notification-popups/style.scss @@ -0,0 +1,69 @@ +$bg: #212223; +$fg: #f1f1f1; +$accent: #378df7; +$radius: 7px; + +window.notification-popups { + box.notifications { + padding: 0.5em; + } +} + +.icon { + min-width: 68px; + min-height: 68px; + margin-right: 1em; +} + +.icon { + icon { + font-size: 58px; + margin: 5px; + color: $fg; + } + box { + min-width: 68px; + min-height: 68px; + border-radius: $radius; + background-size: contain; + background-repeat: no-repeat; + background-position: center; + } +} + +.notification { + min-width: 350px; + border-radius: 11px; + padding: 1em; + margin: 0.5em; + border: 1px solid transparentize($fg, 0.9); + background-color: $bg; + + &.critical { + border: 1px solid $accent; + } + + .title { + color: $fg; + font-size: 1.4em; + } + + .body { + color: transparentize($fg, 0.4); + } + + .actions { + .action-button { + margin: 0 0.4em; + margin-top: 0.8em; + + &:first-child { + margin-left: 0; + } + + &:last-child { + margin-right: 0; + } + } + } +} diff --git a/examples/lua/notification-popups/widget/notifications.lua b/examples/lua/notification-popups/widget/notifications.lua new file mode 100644 index 0000000..4704c22 --- /dev/null +++ b/examples/lua/notification-popups/widget/notifications.lua @@ -0,0 +1,125 @@ +local astal = require("astal") +local Widget = astal.Widget +local GLib = astal.GLib +local bind = astal.bind +local timeout = astal.timeout + +local map = require("lib").map +local lookup_icon = require("lib").lookup_icon + +local Notifd = astal.require("AstalNotifd") +local notifd = Notifd.get_default() + +local NOTIFICATION_TIMEOUTMS = 5000 + +local function NotificationIcon(n) + local icon = "dialog-information-symbolic" + + if n.app_icon and GLib.file_test(n.app_icon, "EXISTS") then + return Widget.Box({ css = string.format('background-image: url("%s");', n.app_icon) }) + elseif n.app_icon and lookup_icon(n.app_icon) then + icon = n.app_icon + elseif n.app_name and lookup_icon(n.app_name) then + icon = n.app_name + end + + return Widget.Icon({ + icon = icon, + }) +end + +local function Notification(n) + local icon = Widget.Box({ + valign = "START", + class_name = "icon", + NotificationIcon(n), + }) + + local title = Widget.Label({ + class_name = "title", + xalign = 0, + justify = "LEFT", + hexpand = true, + max_width_chars = 24, + wrap = true, + ellipsize = "END", + use_markup = true, + label = n.summary, + }) + + local body = Widget.Label({ + class_name = "body", + hexpand = true, + use_markup = true, + xalign = 0, + justify = "LEFT", + wrap = true, + label = n.body, + }) + + local actions = Widget.Box({ + class_name = "actions", + map(n.actions, function(action) + return Widget.Button({ + on_click_release = function() + return n:invoke(action.id) + end, + class_name = "action-button", + hexpand = true, + label = action.label, + }) + end), + }) + return Widget.EventBox({ + Widget.Box({ + class_name = "notification", + orientation = "VERTICAL", + Widget.Box({ + icon, + Widget.Box({ + orientation = "VERTICAL", + title, + body, + }), + }), + actions, + }), + }) +end + +return function(gdkmonitor) + local n_list = {} + + local list = Widget.Box({ + orientation = "VERTICAL", + setup = function(self) + self:hook(notifd, "notified", function(_, id) + local n = notifd:get_notification(id) + n_list[id] = Notification(n) + self:add(n_list[id]) + local timeout_ms = n.expire_timeout > 0 and n.expire_timeout or NOTIFICATION_TIMEOUTMS + timeout(timeout_ms, function() + return n:dismiss() + end) + end) + self:hook(notifd, "resolved", function(_, id) + if n_list[id] then + n_list[id]:destroy() + n_list[id] = nil + end + end) + end, + }) + + return Widget.Window({ + namespace = "notifications", + gdkmonitor = gdkmonitor, + anchor = astal.Astal.WindowAnchor.TOP, + layer = "OVERLAY", + class_name = "notification-popups", + visible = bind(list, "children"):as(function(v) + return v and #v > 0 + end), + list, + }) +end -- cgit v1.2.3 From 03f2c4706faba7dac5aee71b10255eac218cbeec Mon Sep 17 00:00:00 2001 From: Kevin Date: Tue, 15 Oct 2024 02:57:27 -0300 Subject: revert example-lua commit --- examples/lua/notification-popups/README.md | 14 --- examples/lua/notification-popups/init.lua | 21 ---- examples/lua/notification-popups/lib.lua | 52 --------- examples/lua/notification-popups/style.scss | 69 ------------ .../notification-popups/widget/notifications.lua | 125 --------------------- 5 files changed, 281 deletions(-) delete mode 100644 examples/lua/notification-popups/README.md delete mode 100644 examples/lua/notification-popups/init.lua delete mode 100644 examples/lua/notification-popups/lib.lua delete mode 100644 examples/lua/notification-popups/style.scss delete mode 100644 examples/lua/notification-popups/widget/notifications.lua diff --git a/examples/lua/notification-popups/README.md b/examples/lua/notification-popups/README.md deleted file mode 100644 index bcc6bba..0000000 --- a/examples/lua/notification-popups/README.md +++ /dev/null @@ -1,14 +0,0 @@ -# Simple Bar Example - -![simple-bar](https://github.com/user-attachments/assets/a306c864-56b7-44c4-8820-81f424f32b9b) - -A simple bar for Hyprland using - -- [Audio library](https://aylur.github.io/astal/guide/libraries/audio). -- [Battery library](https://aylur.github.io/astal/guide/libraries/battery). -- [Hyprland library](https://aylur.github.io/astal/guide/libraries/hyprland). -- [Mpris library](https://aylur.github.io/astal/guide/libraries/mpris). -- [Network library](https://aylur.github.io/astal/guide/libraries/network). -- [Tray library](https://aylur.github.io/astal/guide/libraries/tray). -- [WirePlumber library](https://aylur.github.io/astal/guide/libraries/wireplumber). -- [dart-sass](https://sass-lang.com/dart-sass/) as the css precompiler diff --git a/examples/lua/notification-popups/init.lua b/examples/lua/notification-popups/init.lua deleted file mode 100644 index ffa910c..0000000 --- a/examples/lua/notification-popups/init.lua +++ /dev/null @@ -1,21 +0,0 @@ -local astal = require("astal") -local Notify = astal.require("Notify") -local timeout = astal.timeout -local App = astal.App - -local Notifications = require("widget.notifications") -local src = require("lib").src - -local scss = src("style.scss") -local css = "/tmp/style.css" - -astal.exec("sass " .. scss .. " " .. css) - -App:start({ - css = css, - main = function() - for _, mon in pairs(App.monitors) do - Notifications(mon) - end - end, -}) diff --git a/examples/lua/notification-popups/lib.lua b/examples/lua/notification-popups/lib.lua deleted file mode 100644 index 2ae71a9..0000000 --- a/examples/lua/notification-popups/lib.lua +++ /dev/null @@ -1,52 +0,0 @@ -local Variable = require("astal").Variable -local astal = require("astal") -local Gtk = astal.Gtk - -local M = {} - -function M.src(path) - local str = debug.getinfo(2, "S").source:sub(2) - local src = str:match("(.*/)") or str:match("(.*\\)") or "./" - return src .. path -end - ----@generic T, R ----@param arr T[] ----@param func fun(T, integer): R ----@return R[] -function M.map(arr, func) - local new_arr = {} - for i, v in ipairs(arr) do - new_arr[i] = func(v, i) - end - return new_arr -end - ----@param name string ----@param size? 16 | 32 | 64 | 128 | 256 | 512 | number -function M.lookup_icon(name, size) - if not name or #name == 0 then - return - end - size = size or 256 - - local theme = Gtk.IconTheme.get_default() - local icon_info, path - - for _, n in ipairs({ - name, - string.lower(name), - string.upper(name), - }) do - icon_info = theme:lookup_icon(n, size, "USE_BUILTIN") - - if icon_info then - return icon_info - end - end - return false -end - -M.date = Variable(""):poll(1000, "date") - -return M diff --git a/examples/lua/notification-popups/style.scss b/examples/lua/notification-popups/style.scss deleted file mode 100644 index 8eb764a..0000000 --- a/examples/lua/notification-popups/style.scss +++ /dev/null @@ -1,69 +0,0 @@ -$bg: #212223; -$fg: #f1f1f1; -$accent: #378df7; -$radius: 7px; - -window.notification-popups { - box.notifications { - padding: 0.5em; - } -} - -.icon { - min-width: 68px; - min-height: 68px; - margin-right: 1em; -} - -.icon { - icon { - font-size: 58px; - margin: 5px; - color: $fg; - } - box { - min-width: 68px; - min-height: 68px; - border-radius: $radius; - background-size: contain; - background-repeat: no-repeat; - background-position: center; - } -} - -.notification { - min-width: 350px; - border-radius: 11px; - padding: 1em; - margin: 0.5em; - border: 1px solid transparentize($fg, 0.9); - background-color: $bg; - - &.critical { - border: 1px solid $accent; - } - - .title { - color: $fg; - font-size: 1.4em; - } - - .body { - color: transparentize($fg, 0.4); - } - - .actions { - .action-button { - margin: 0 0.4em; - margin-top: 0.8em; - - &:first-child { - margin-left: 0; - } - - &:last-child { - margin-right: 0; - } - } - } -} diff --git a/examples/lua/notification-popups/widget/notifications.lua b/examples/lua/notification-popups/widget/notifications.lua deleted file mode 100644 index 4704c22..0000000 --- a/examples/lua/notification-popups/widget/notifications.lua +++ /dev/null @@ -1,125 +0,0 @@ -local astal = require("astal") -local Widget = astal.Widget -local GLib = astal.GLib -local bind = astal.bind -local timeout = astal.timeout - -local map = require("lib").map -local lookup_icon = require("lib").lookup_icon - -local Notifd = astal.require("AstalNotifd") -local notifd = Notifd.get_default() - -local NOTIFICATION_TIMEOUTMS = 5000 - -local function NotificationIcon(n) - local icon = "dialog-information-symbolic" - - if n.app_icon and GLib.file_test(n.app_icon, "EXISTS") then - return Widget.Box({ css = string.format('background-image: url("%s");', n.app_icon) }) - elseif n.app_icon and lookup_icon(n.app_icon) then - icon = n.app_icon - elseif n.app_name and lookup_icon(n.app_name) then - icon = n.app_name - end - - return Widget.Icon({ - icon = icon, - }) -end - -local function Notification(n) - local icon = Widget.Box({ - valign = "START", - class_name = "icon", - NotificationIcon(n), - }) - - local title = Widget.Label({ - class_name = "title", - xalign = 0, - justify = "LEFT", - hexpand = true, - max_width_chars = 24, - wrap = true, - ellipsize = "END", - use_markup = true, - label = n.summary, - }) - - local body = Widget.Label({ - class_name = "body", - hexpand = true, - use_markup = true, - xalign = 0, - justify = "LEFT", - wrap = true, - label = n.body, - }) - - local actions = Widget.Box({ - class_name = "actions", - map(n.actions, function(action) - return Widget.Button({ - on_click_release = function() - return n:invoke(action.id) - end, - class_name = "action-button", - hexpand = true, - label = action.label, - }) - end), - }) - return Widget.EventBox({ - Widget.Box({ - class_name = "notification", - orientation = "VERTICAL", - Widget.Box({ - icon, - Widget.Box({ - orientation = "VERTICAL", - title, - body, - }), - }), - actions, - }), - }) -end - -return function(gdkmonitor) - local n_list = {} - - local list = Widget.Box({ - orientation = "VERTICAL", - setup = function(self) - self:hook(notifd, "notified", function(_, id) - local n = notifd:get_notification(id) - n_list[id] = Notification(n) - self:add(n_list[id]) - local timeout_ms = n.expire_timeout > 0 and n.expire_timeout or NOTIFICATION_TIMEOUTMS - timeout(timeout_ms, function() - return n:dismiss() - end) - end) - self:hook(notifd, "resolved", function(_, id) - if n_list[id] then - n_list[id]:destroy() - n_list[id] = nil - end - end) - end, - }) - - return Widget.Window({ - namespace = "notifications", - gdkmonitor = gdkmonitor, - anchor = astal.Astal.WindowAnchor.TOP, - layer = "OVERLAY", - class_name = "notification-popups", - visible = bind(list, "children"):as(function(v) - return v and #v > 0 - end), - list, - }) -end -- cgit v1.2.3 From 4dd0b1840d343dc65f1c781c3d6b8731b6e79eda Mon Sep 17 00:00:00 2001 From: Kevin Date: Sun, 13 Oct 2024 12:57:37 -0300 Subject: core: lua compat 5.1/5.4/luajit --- lang/lua/astal/gtk3/app.lua | 16 +++++++++++++++- lang/lua/astal/gtk3/astalify.lua | 3 +-- lang/lua/astal/init.lua | 7 ++++++- lang/lua/astal/variable.lua | 3 ++- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/lang/lua/astal/gtk3/app.lua b/lang/lua/astal/gtk3/app.lua index 7895f69..13347c3 100644 --- a/lang/lua/astal/gtk3/app.lua +++ b/lang/lua/astal/gtk3/app.lua @@ -5,6 +5,15 @@ local AstalIO = lgi.require("AstalIO", "0.1") local AstalLua = Astal.Application:derive("AstalLua") local request_handler +local function unpack(t, i) + i = i or 1 + if t[i] == nil then + return nil + else + return t[i], unpack(t, i + 1) + end +end + function AstalLua:do_request(msg, conn) if type(request_handler) == "function" then request_handler(msg, function(response) @@ -76,7 +85,7 @@ function Astal.Application:start(config) app.on_activate = function() if type(config.main) == "function" then - config.main(table.unpack(arg)) + config.main(unpack(arg)) end if config.hold then self:hold() @@ -86,8 +95,13 @@ function Astal.Application:start(config) local _, err = app:acquire_socket() if err ~= nil then return config.client(function(msg) +<<<<<<< HEAD:lang/lua/astal/gtk3/app.lua return AstalIO.send_message(self.instance_name, msg) end, table.unpack(arg)) +======= + return Astal.Application.send_message(self.instance_name, msg) + end, unpack(arg)) +>>>>>>> 18df91b (core: lua compat 5.1/5.4/luajit):core/lua/astal/application.lua end self:run(nil) diff --git a/lang/lua/astal/gtk3/astalify.lua b/lang/lua/astal/gtk3/astalify.lua index 065de40..c344c07 100644 --- a/lang/lua/astal/gtk3/astalify.lua +++ b/lang/lua/astal/gtk3/astalify.lua @@ -28,8 +28,7 @@ local function map(tbl, fn) return copy end -local flatten -flatten = function(tbl) +local function flatten(tbl) local copy = {} for _, value in pairs(tbl) do if type(value) == "table" and getmetatable(value) == nil then diff --git a/lang/lua/astal/init.lua b/lang/lua/astal/init.lua index f442db0..783c78a 100644 --- a/lang/lua/astal/init.lua +++ b/lang/lua/astal/init.lua @@ -1,7 +1,12 @@ +if not table.unpack then + table.unpack = unpack +end + + local lgi = require("lgi") local Binding = require("astal.binding") local File = require("astal.file") -local Process = require("astal.proc") +local Process = require("astal.process") local Time = require("astal.time") local Variable = require("astal.variable") diff --git a/lang/lua/astal/variable.lua b/lang/lua/astal/variable.lua index 5a5e169..c2ed337 100644 --- a/lang/lua/astal/variable.lua +++ b/lang/lua/astal/variable.lua @@ -5,6 +5,7 @@ local Binding = require("astal.binding") local Time = require("astal.time") local Process = require("astal.process") + ---@class Variable ---@field private variable table ---@field private err_handler? function @@ -273,4 +274,4 @@ return setmetatable(Variable, { __call = function(_, v) return Variable.new(v) end, -}) +}) \ No newline at end of file -- cgit v1.2.3 From 9e255738f835c0e47cc6ae4d0cfbb96a261b4a2f Mon Sep 17 00:00:00 2001 From: Kevin Date: Mon, 14 Oct 2024 10:27:57 -0300 Subject: core: table.unpack compat in lua, fix typo in astal/init.lua --- lang/lua/astal/gtk3/app.lua | 16 +--------------- lang/lua/astal/init.lua | 1 - lang/lua/astal/variable.lua | 1 - 3 files changed, 1 insertion(+), 17 deletions(-) diff --git a/lang/lua/astal/gtk3/app.lua b/lang/lua/astal/gtk3/app.lua index 13347c3..7895f69 100644 --- a/lang/lua/astal/gtk3/app.lua +++ b/lang/lua/astal/gtk3/app.lua @@ -5,15 +5,6 @@ local AstalIO = lgi.require("AstalIO", "0.1") local AstalLua = Astal.Application:derive("AstalLua") local request_handler -local function unpack(t, i) - i = i or 1 - if t[i] == nil then - return nil - else - return t[i], unpack(t, i + 1) - end -end - function AstalLua:do_request(msg, conn) if type(request_handler) == "function" then request_handler(msg, function(response) @@ -85,7 +76,7 @@ function Astal.Application:start(config) app.on_activate = function() if type(config.main) == "function" then - config.main(unpack(arg)) + config.main(table.unpack(arg)) end if config.hold then self:hold() @@ -95,13 +86,8 @@ function Astal.Application:start(config) local _, err = app:acquire_socket() if err ~= nil then return config.client(function(msg) -<<<<<<< HEAD:lang/lua/astal/gtk3/app.lua return AstalIO.send_message(self.instance_name, msg) end, table.unpack(arg)) -======= - return Astal.Application.send_message(self.instance_name, msg) - end, unpack(arg)) ->>>>>>> 18df91b (core: lua compat 5.1/5.4/luajit):core/lua/astal/application.lua end self:run(nil) diff --git a/lang/lua/astal/init.lua b/lang/lua/astal/init.lua index 783c78a..5630ba4 100644 --- a/lang/lua/astal/init.lua +++ b/lang/lua/astal/init.lua @@ -2,7 +2,6 @@ if not table.unpack then table.unpack = unpack end - local lgi = require("lgi") local Binding = require("astal.binding") local File = require("astal.file") diff --git a/lang/lua/astal/variable.lua b/lang/lua/astal/variable.lua index c2ed337..f9be161 100644 --- a/lang/lua/astal/variable.lua +++ b/lang/lua/astal/variable.lua @@ -5,7 +5,6 @@ local Binding = require("astal.binding") local Time = require("astal.time") local Process = require("astal.process") - ---@class Variable ---@field private variable table ---@field private err_handler? function -- cgit v1.2.3 From 1a1c8a605c8011056a07d69b15153bcdec4c0ceb Mon Sep 17 00:00:00 2001 From: Kevin Date: Mon, 14 Oct 2024 11:56:20 -0300 Subject: notifications lua example --- examples/lua/notification-popups/README.md | 14 +++ examples/lua/notification-popups/init.lua | 21 ++++ examples/lua/notification-popups/lib.lua | 52 +++++++++ examples/lua/notification-popups/style.scss | 69 ++++++++++++ .../notification-popups/widget/notifications.lua | 125 +++++++++++++++++++++ 5 files changed, 281 insertions(+) create mode 100644 examples/lua/notification-popups/README.md create mode 100644 examples/lua/notification-popups/init.lua create mode 100644 examples/lua/notification-popups/lib.lua create mode 100644 examples/lua/notification-popups/style.scss create mode 100644 examples/lua/notification-popups/widget/notifications.lua diff --git a/examples/lua/notification-popups/README.md b/examples/lua/notification-popups/README.md new file mode 100644 index 0000000..bcc6bba --- /dev/null +++ b/examples/lua/notification-popups/README.md @@ -0,0 +1,14 @@ +# Simple Bar Example + +![simple-bar](https://github.com/user-attachments/assets/a306c864-56b7-44c4-8820-81f424f32b9b) + +A simple bar for Hyprland using + +- [Audio library](https://aylur.github.io/astal/guide/libraries/audio). +- [Battery library](https://aylur.github.io/astal/guide/libraries/battery). +- [Hyprland library](https://aylur.github.io/astal/guide/libraries/hyprland). +- [Mpris library](https://aylur.github.io/astal/guide/libraries/mpris). +- [Network library](https://aylur.github.io/astal/guide/libraries/network). +- [Tray library](https://aylur.github.io/astal/guide/libraries/tray). +- [WirePlumber library](https://aylur.github.io/astal/guide/libraries/wireplumber). +- [dart-sass](https://sass-lang.com/dart-sass/) as the css precompiler diff --git a/examples/lua/notification-popups/init.lua b/examples/lua/notification-popups/init.lua new file mode 100644 index 0000000..ffa910c --- /dev/null +++ b/examples/lua/notification-popups/init.lua @@ -0,0 +1,21 @@ +local astal = require("astal") +local Notify = astal.require("Notify") +local timeout = astal.timeout +local App = astal.App + +local Notifications = require("widget.notifications") +local src = require("lib").src + +local scss = src("style.scss") +local css = "/tmp/style.css" + +astal.exec("sass " .. scss .. " " .. css) + +App:start({ + css = css, + main = function() + for _, mon in pairs(App.monitors) do + Notifications(mon) + end + end, +}) diff --git a/examples/lua/notification-popups/lib.lua b/examples/lua/notification-popups/lib.lua new file mode 100644 index 0000000..2ae71a9 --- /dev/null +++ b/examples/lua/notification-popups/lib.lua @@ -0,0 +1,52 @@ +local Variable = require("astal").Variable +local astal = require("astal") +local Gtk = astal.Gtk + +local M = {} + +function M.src(path) + local str = debug.getinfo(2, "S").source:sub(2) + local src = str:match("(.*/)") or str:match("(.*\\)") or "./" + return src .. path +end + +---@generic T, R +---@param arr T[] +---@param func fun(T, integer): R +---@return R[] +function M.map(arr, func) + local new_arr = {} + for i, v in ipairs(arr) do + new_arr[i] = func(v, i) + end + return new_arr +end + +---@param name string +---@param size? 16 | 32 | 64 | 128 | 256 | 512 | number +function M.lookup_icon(name, size) + if not name or #name == 0 then + return + end + size = size or 256 + + local theme = Gtk.IconTheme.get_default() + local icon_info, path + + for _, n in ipairs({ + name, + string.lower(name), + string.upper(name), + }) do + icon_info = theme:lookup_icon(n, size, "USE_BUILTIN") + + if icon_info then + return icon_info + end + end + return false +end + +M.date = Variable(""):poll(1000, "date") + +return M diff --git a/examples/lua/notification-popups/style.scss b/examples/lua/notification-popups/style.scss new file mode 100644 index 0000000..8eb764a --- /dev/null +++ b/examples/lua/notification-popups/style.scss @@ -0,0 +1,69 @@ +$bg: #212223; +$fg: #f1f1f1; +$accent: #378df7; +$radius: 7px; + +window.notification-popups { + box.notifications { + padding: 0.5em; + } +} + +.icon { + min-width: 68px; + min-height: 68px; + margin-right: 1em; +} + +.icon { + icon { + font-size: 58px; + margin: 5px; + color: $fg; + } + box { + min-width: 68px; + min-height: 68px; + border-radius: $radius; + background-size: contain; + background-repeat: no-repeat; + background-position: center; + } +} + +.notification { + min-width: 350px; + border-radius: 11px; + padding: 1em; + margin: 0.5em; + border: 1px solid transparentize($fg, 0.9); + background-color: $bg; + + &.critical { + border: 1px solid $accent; + } + + .title { + color: $fg; + font-size: 1.4em; + } + + .body { + color: transparentize($fg, 0.4); + } + + .actions { + .action-button { + margin: 0 0.4em; + margin-top: 0.8em; + + &:first-child { + margin-left: 0; + } + + &:last-child { + margin-right: 0; + } + } + } +} diff --git a/examples/lua/notification-popups/widget/notifications.lua b/examples/lua/notification-popups/widget/notifications.lua new file mode 100644 index 0000000..4704c22 --- /dev/null +++ b/examples/lua/notification-popups/widget/notifications.lua @@ -0,0 +1,125 @@ +local astal = require("astal") +local Widget = astal.Widget +local GLib = astal.GLib +local bind = astal.bind +local timeout = astal.timeout + +local map = require("lib").map +local lookup_icon = require("lib").lookup_icon + +local Notifd = astal.require("AstalNotifd") +local notifd = Notifd.get_default() + +local NOTIFICATION_TIMEOUTMS = 5000 + +local function NotificationIcon(n) + local icon = "dialog-information-symbolic" + + if n.app_icon and GLib.file_test(n.app_icon, "EXISTS") then + return Widget.Box({ css = string.format('background-image: url("%s");', n.app_icon) }) + elseif n.app_icon and lookup_icon(n.app_icon) then + icon = n.app_icon + elseif n.app_name and lookup_icon(n.app_name) then + icon = n.app_name + end + + return Widget.Icon({ + icon = icon, + }) +end + +local function Notification(n) + local icon = Widget.Box({ + valign = "START", + class_name = "icon", + NotificationIcon(n), + }) + + local title = Widget.Label({ + class_name = "title", + xalign = 0, + justify = "LEFT", + hexpand = true, + max_width_chars = 24, + wrap = true, + ellipsize = "END", + use_markup = true, + label = n.summary, + }) + + local body = Widget.Label({ + class_name = "body", + hexpand = true, + use_markup = true, + xalign = 0, + justify = "LEFT", + wrap = true, + label = n.body, + }) + + local actions = Widget.Box({ + class_name = "actions", + map(n.actions, function(action) + return Widget.Button({ + on_click_release = function() + return n:invoke(action.id) + end, + class_name = "action-button", + hexpand = true, + label = action.label, + }) + end), + }) + return Widget.EventBox({ + Widget.Box({ + class_name = "notification", + orientation = "VERTICAL", + Widget.Box({ + icon, + Widget.Box({ + orientation = "VERTICAL", + title, + body, + }), + }), + actions, + }), + }) +end + +return function(gdkmonitor) + local n_list = {} + + local list = Widget.Box({ + orientation = "VERTICAL", + setup = function(self) + self:hook(notifd, "notified", function(_, id) + local n = notifd:get_notification(id) + n_list[id] = Notification(n) + self:add(n_list[id]) + local timeout_ms = n.expire_timeout > 0 and n.expire_timeout or NOTIFICATION_TIMEOUTMS + timeout(timeout_ms, function() + return n:dismiss() + end) + end) + self:hook(notifd, "resolved", function(_, id) + if n_list[id] then + n_list[id]:destroy() + n_list[id] = nil + end + end) + end, + }) + + return Widget.Window({ + namespace = "notifications", + gdkmonitor = gdkmonitor, + anchor = astal.Astal.WindowAnchor.TOP, + layer = "OVERLAY", + class_name = "notification-popups", + visible = bind(list, "children"):as(function(v) + return v and #v > 0 + end), + list, + }) +end -- cgit v1.2.3 From 236487001ab2a6c9c8e87e5db0ced9e5ab3ed791 Mon Sep 17 00:00:00 2001 From: Kevin Date: Tue, 15 Oct 2024 02:57:27 -0300 Subject: revert example-lua commit --- examples/lua/notification-popups/README.md | 14 --- examples/lua/notification-popups/init.lua | 21 ---- examples/lua/notification-popups/lib.lua | 52 --------- examples/lua/notification-popups/style.scss | 69 ------------ .../notification-popups/widget/notifications.lua | 125 --------------------- 5 files changed, 281 deletions(-) delete mode 100644 examples/lua/notification-popups/README.md delete mode 100644 examples/lua/notification-popups/init.lua delete mode 100644 examples/lua/notification-popups/lib.lua delete mode 100644 examples/lua/notification-popups/style.scss delete mode 100644 examples/lua/notification-popups/widget/notifications.lua diff --git a/examples/lua/notification-popups/README.md b/examples/lua/notification-popups/README.md deleted file mode 100644 index bcc6bba..0000000 --- a/examples/lua/notification-popups/README.md +++ /dev/null @@ -1,14 +0,0 @@ -# Simple Bar Example - -![simple-bar](https://github.com/user-attachments/assets/a306c864-56b7-44c4-8820-81f424f32b9b) - -A simple bar for Hyprland using - -- [Audio library](https://aylur.github.io/astal/guide/libraries/audio). -- [Battery library](https://aylur.github.io/astal/guide/libraries/battery). -- [Hyprland library](https://aylur.github.io/astal/guide/libraries/hyprland). -- [Mpris library](https://aylur.github.io/astal/guide/libraries/mpris). -- [Network library](https://aylur.github.io/astal/guide/libraries/network). -- [Tray library](https://aylur.github.io/astal/guide/libraries/tray). -- [WirePlumber library](https://aylur.github.io/astal/guide/libraries/wireplumber). -- [dart-sass](https://sass-lang.com/dart-sass/) as the css precompiler diff --git a/examples/lua/notification-popups/init.lua b/examples/lua/notification-popups/init.lua deleted file mode 100644 index ffa910c..0000000 --- a/examples/lua/notification-popups/init.lua +++ /dev/null @@ -1,21 +0,0 @@ -local astal = require("astal") -local Notify = astal.require("Notify") -local timeout = astal.timeout -local App = astal.App - -local Notifications = require("widget.notifications") -local src = require("lib").src - -local scss = src("style.scss") -local css = "/tmp/style.css" - -astal.exec("sass " .. scss .. " " .. css) - -App:start({ - css = css, - main = function() - for _, mon in pairs(App.monitors) do - Notifications(mon) - end - end, -}) diff --git a/examples/lua/notification-popups/lib.lua b/examples/lua/notification-popups/lib.lua deleted file mode 100644 index 2ae71a9..0000000 --- a/examples/lua/notification-popups/lib.lua +++ /dev/null @@ -1,52 +0,0 @@ -local Variable = require("astal").Variable -local astal = require("astal") -local Gtk = astal.Gtk - -local M = {} - -function M.src(path) - local str = debug.getinfo(2, "S").source:sub(2) - local src = str:match("(.*/)") or str:match("(.*\\)") or "./" - return src .. path -end - ----@generic T, R ----@param arr T[] ----@param func fun(T, integer): R ----@return R[] -function M.map(arr, func) - local new_arr = {} - for i, v in ipairs(arr) do - new_arr[i] = func(v, i) - end - return new_arr -end - ----@param name string ----@param size? 16 | 32 | 64 | 128 | 256 | 512 | number -function M.lookup_icon(name, size) - if not name or #name == 0 then - return - end - size = size or 256 - - local theme = Gtk.IconTheme.get_default() - local icon_info, path - - for _, n in ipairs({ - name, - string.lower(name), - string.upper(name), - }) do - icon_info = theme:lookup_icon(n, size, "USE_BUILTIN") - - if icon_info then - return icon_info - end - end - return false -end - -M.date = Variable(""):poll(1000, "date") - -return M diff --git a/examples/lua/notification-popups/style.scss b/examples/lua/notification-popups/style.scss deleted file mode 100644 index 8eb764a..0000000 --- a/examples/lua/notification-popups/style.scss +++ /dev/null @@ -1,69 +0,0 @@ -$bg: #212223; -$fg: #f1f1f1; -$accent: #378df7; -$radius: 7px; - -window.notification-popups { - box.notifications { - padding: 0.5em; - } -} - -.icon { - min-width: 68px; - min-height: 68px; - margin-right: 1em; -} - -.icon { - icon { - font-size: 58px; - margin: 5px; - color: $fg; - } - box { - min-width: 68px; - min-height: 68px; - border-radius: $radius; - background-size: contain; - background-repeat: no-repeat; - background-position: center; - } -} - -.notification { - min-width: 350px; - border-radius: 11px; - padding: 1em; - margin: 0.5em; - border: 1px solid transparentize($fg, 0.9); - background-color: $bg; - - &.critical { - border: 1px solid $accent; - } - - .title { - color: $fg; - font-size: 1.4em; - } - - .body { - color: transparentize($fg, 0.4); - } - - .actions { - .action-button { - margin: 0 0.4em; - margin-top: 0.8em; - - &:first-child { - margin-left: 0; - } - - &:last-child { - margin-right: 0; - } - } - } -} diff --git a/examples/lua/notification-popups/widget/notifications.lua b/examples/lua/notification-popups/widget/notifications.lua deleted file mode 100644 index 4704c22..0000000 --- a/examples/lua/notification-popups/widget/notifications.lua +++ /dev/null @@ -1,125 +0,0 @@ -local astal = require("astal") -local Widget = astal.Widget -local GLib = astal.GLib -local bind = astal.bind -local timeout = astal.timeout - -local map = require("lib").map -local lookup_icon = require("lib").lookup_icon - -local Notifd = astal.require("AstalNotifd") -local notifd = Notifd.get_default() - -local NOTIFICATION_TIMEOUTMS = 5000 - -local function NotificationIcon(n) - local icon = "dialog-information-symbolic" - - if n.app_icon and GLib.file_test(n.app_icon, "EXISTS") then - return Widget.Box({ css = string.format('background-image: url("%s");', n.app_icon) }) - elseif n.app_icon and lookup_icon(n.app_icon) then - icon = n.app_icon - elseif n.app_name and lookup_icon(n.app_name) then - icon = n.app_name - end - - return Widget.Icon({ - icon = icon, - }) -end - -local function Notification(n) - local icon = Widget.Box({ - valign = "START", - class_name = "icon", - NotificationIcon(n), - }) - - local title = Widget.Label({ - class_name = "title", - xalign = 0, - justify = "LEFT", - hexpand = true, - max_width_chars = 24, - wrap = true, - ellipsize = "END", - use_markup = true, - label = n.summary, - }) - - local body = Widget.Label({ - class_name = "body", - hexpand = true, - use_markup = true, - xalign = 0, - justify = "LEFT", - wrap = true, - label = n.body, - }) - - local actions = Widget.Box({ - class_name = "actions", - map(n.actions, function(action) - return Widget.Button({ - on_click_release = function() - return n:invoke(action.id) - end, - class_name = "action-button", - hexpand = true, - label = action.label, - }) - end), - }) - return Widget.EventBox({ - Widget.Box({ - class_name = "notification", - orientation = "VERTICAL", - Widget.Box({ - icon, - Widget.Box({ - orientation = "VERTICAL", - title, - body, - }), - }), - actions, - }), - }) -end - -return function(gdkmonitor) - local n_list = {} - - local list = Widget.Box({ - orientation = "VERTICAL", - setup = function(self) - self:hook(notifd, "notified", function(_, id) - local n = notifd:get_notification(id) - n_list[id] = Notification(n) - self:add(n_list[id]) - local timeout_ms = n.expire_timeout > 0 and n.expire_timeout or NOTIFICATION_TIMEOUTMS - timeout(timeout_ms, function() - return n:dismiss() - end) - end) - self:hook(notifd, "resolved", function(_, id) - if n_list[id] then - n_list[id]:destroy() - n_list[id] = nil - end - end) - end, - }) - - return Widget.Window({ - namespace = "notifications", - gdkmonitor = gdkmonitor, - anchor = astal.Astal.WindowAnchor.TOP, - layer = "OVERLAY", - class_name = "notification-popups", - visible = bind(list, "children"):as(function(v) - return v and #v > 0 - end), - list, - }) -end -- cgit v1.2.3 From dcb7cb325d5525a864098dfcba3100a6151d4d13 Mon Sep 17 00:00:00 2001 From: Kevin Date: Wed, 16 Oct 2024 02:32:56 -0300 Subject: core: lua fix require --- lang/lua/astal/gtk3/astalify.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lang/lua/astal/gtk3/astalify.lua b/lang/lua/astal/gtk3/astalify.lua index c344c07..211a1d4 100644 --- a/lang/lua/astal/gtk3/astalify.lua +++ b/lang/lua/astal/gtk3/astalify.lua @@ -2,9 +2,9 @@ local lgi = require("lgi") local Astal = lgi.require("Astal", "3.0") local Gtk = lgi.require("Gtk", "3.0") local GObject = lgi.require("GObject", "2.0") -local Binding = require("astal.lib.binding") -local Variable = require("astal.lib.variable") -local exec_async = require("astal.lib.process").exec_async +local Binding = require("astal.binding") +local Variable = require("astal.variable") +local exec_async = require("astal.process").exec_async local function filter(tbl, fn) local copy = {} -- cgit v1.2.3