From dc3cec536896fa224e0ce340557aa02b8d98738f Mon Sep 17 00:00:00 2001 From: Aylur Date: Wed, 11 Sep 2024 14:48:53 +0000 Subject: add lua builder --- flake.nix | 5 +++++ nix/lua.nix | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 nix/lua.nix diff --git a/flake.nix b/flake.nix index 000ded1..a975e78 100644 --- a/flake.nix +++ b/flake.nix @@ -29,6 +29,11 @@ outputs = ["out" "dev"]; }; in { + mkLuaPackage = import ./nix/lua.nix { + inherit pkgs; + astal = self; + }; + packages.${system} = with pkgs; { docs = import ./docs {inherit self pkgs;}; default = self.packages.${system}.astal; diff --git a/nix/lua.nix b/nix/lua.nix new file mode 100644 index 0000000..36ab4e5 --- /dev/null +++ b/nix/lua.nix @@ -0,0 +1,58 @@ +defaults: { + pkgs ? defaults.pkgs, + astal ? defaults.astal, + name ? "astal-lua", + src, + extraLuaPackages ? (ps: []), + extraPacakges ? [], +}: let + lua = pkgs.lua.withPackages (ps: + (extraLuaPackages ps) + ++ [ + ps.lgi + (ps.luaPackages.toLuaModule (pkgs.stdenv.mkDerivation { + name = "astal"; + version = "0.1.0"; + src = "${astal}/core/lua"; + dontBuild = true; + installPhase = '' + mkdir -p $out/share/lua/${ps.lua.luaversion}/astal + cp -r astal/* $out/share/lua/${ps.lua.luaversion}/astal + ''; + })) + ]); + + nativeBuildInputs = with pkgs; [ + wrapGAppsHook + gobject-introspection + ]; + + buildInputs = + extraPacakges + ++ [ + lua + astal.packages.${pkgs.system}.default + ]; + + script = pkgs.writeScript "astal-lua" '' + #!${lua}/bin/lua + package.path = package.path .. ";${src}/?.lua" + require "app" + ''; +in + pkgs.stdenv.mkDerivation { + inherit nativeBuildInputs buildInputs src name; + + installPhase = '' + mkdir -p $out/bin + cp -r * $out/bin + cp ${script} $out/bin/${name} + chmod +x $out/bin/${name} + ''; + + preFixup = '' + gappsWrapperArgs+=( + --prefix PATH : ${pkgs.lib.makeBinPath extraPacakges} + ) + ''; + } -- cgit v1.2.3 From 374d76f4152ea46987203f49642d7d46aff221ab Mon Sep 17 00:00:00 2001 From: Aylur Date: Wed, 11 Sep 2024 16:28:04 +0000 Subject: nix: move devshell into its own file --- flake.nix | 36 ++++-------------------------------- nix/devshell.nix | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ nix/lua.nix | 2 +- 3 files changed, 57 insertions(+), 33 deletions(-) create mode 100644 nix/devshell.nix diff --git a/flake.nix b/flake.nix index a975e78..8ff60e7 100644 --- a/flake.nix +++ b/flake.nix @@ -29,6 +29,10 @@ outputs = ["out" "dev"]; }; in { + devShells.${system} = import ./nix/devshell.nix { + inherit self pkgs; + }; + mkLuaPackage = import ./nix/lua.nix { inherit pkgs; astal = self; @@ -52,37 +56,5 @@ tray = mkPkg "astal-tray" ./lib/tray [gtk3 gdk-pixbuf libdbusmenu-gtk3 json-glib]; wireplumber = mkPkg "astal-wireplumber" ./lib/wireplumber [wireplumber]; }; - - devShells.${system} = let - buildInputs = with pkgs; [ - wrapGAppsHook - gobject-introspection - meson - pkg-config - ninja - vala - gtk3 - gtk-layer-shell - json-glib - pam - gvfs - networkmanager - gdk-pixbuf - wireplumber - libdbusmenu-gtk3 - wayland - - (lua.withPackages (ps: [ps.lgi])) - (python3.withPackages (ps: [ps.pygobject3 ps.pygobject-stubs])) - gjs - ]; - in { - default = pkgs.mkShell { - inherit buildInputs; - }; - astal = pkgs.mkShell { - buildInputs = buildInputs ++ (builtins.attrValues self.packages.${system}); - }; - }; }; } diff --git a/nix/devshell.nix b/nix/devshell.nix new file mode 100644 index 0000000..9f664f6 --- /dev/null +++ b/nix/devshell.nix @@ -0,0 +1,52 @@ +{ + self, + pkgs, +}: let + lua = pkgs.lua.withPackages (ps: [ + ps.lgi + (ps.luaPackages.toLuaModule (pkgs.stdenv.mkDerivation { + name = "astal"; + src = "${self}/core/lua"; + dontBuild = true; + installPhase = '' + mkdir -p $out/share/lua/${ps.lua.luaversion}/astal + cp -r astal/* $out/share/lua/${ps.lua.luaversion}/astal + ''; + })) + ]); + + python = pkgs.python3.withPackages (ps: [ + ps.pygobject3 + ps.pygobject-stubs + ]); + + buildInputs = with pkgs; [ + wrapGAppsHook + gobject-introspection + meson + pkg-config + ninja + vala + gtk3 + gtk-layer-shell + json-glib + pam + gvfs + networkmanager + gdk-pixbuf + wireplumber + libdbusmenu-gtk3 + wayland + + lua + python + gjs + ]; +in { + default = pkgs.mkShell { + inherit buildInputs; + }; + astal = pkgs.mkShell { + buildInputs = buildInputs ++ (builtins.attrValues self.packages.${pkgs.system}); + }; +} diff --git a/nix/lua.nix b/nix/lua.nix index 36ab4e5..ed8ec0a 100644 --- a/nix/lua.nix +++ b/nix/lua.nix @@ -52,7 +52,7 @@ in preFixup = '' gappsWrapperArgs+=( - --prefix PATH : ${pkgs.lib.makeBinPath extraPacakges} + --prefix PATH : "${pkgs.lib.makeBinPath extraPacakges}" ) ''; } -- cgit v1.2.3 From 0e259e49357d5389db897414fc52b50b1ce1d14c Mon Sep 17 00:00:00 2001 From: Aylur Date: Wed, 11 Sep 2024 20:52:44 +0000 Subject: example: add lua simple-bar --- core/gjs/src/variable.ts | 4 +- core/lua/astal/binding.lua | 20 ++- core/lua/astal/process.lua | 4 +- core/lua/astal/variable.lua | 4 +- core/lua/test.lua | 8 -- docs/getting-started/supported-languages.md | 3 +- examples/js/simple-bar/README.md | 2 +- examples/js/simple-bar/widget/Bar.tsx | 34 +++-- examples/lua/simple-bar/README.md | 12 ++ examples/lua/simple-bar/app.lua | 20 +++ examples/lua/simple-bar/lib.lua | 25 ++++ examples/lua/simple-bar/style.scss | 88 +++++++++++++ examples/lua/simple-bar/widget/Bar.lua | 192 ++++++++++++++++++++++++++++ nix/devshell.nix | 7 +- 14 files changed, 386 insertions(+), 37 deletions(-) delete mode 100644 core/lua/test.lua create mode 100644 examples/lua/simple-bar/README.md create mode 100644 examples/lua/simple-bar/app.lua create mode 100644 examples/lua/simple-bar/lib.lua create mode 100644 examples/lua/simple-bar/style.scss create mode 100644 examples/lua/simple-bar/widget/Bar.lua diff --git a/core/gjs/src/variable.ts b/core/gjs/src/variable.ts index 9528ffe..84f8cc5 100644 --- a/core/gjs/src/variable.ts +++ b/core/gjs/src/variable.ts @@ -1,6 +1,6 @@ import Binding, { type Connectable } from "./binding.js" import { Astal } from "./imports.js" -import { interval } from "./time.js" +import { interval, idle } from "./time.js" import { execAsync, subprocess } from "./process.js" class VariableWrapper extends Function { @@ -101,7 +101,7 @@ class VariableWrapper extends Function { drop() { this.variable.emit("dropped") - this.variable.run_dispose() + idle(() => this.variable.run_dispose()) } onDropped(callback: () => void) { diff --git a/core/lua/astal/binding.lua b/core/lua/astal/binding.lua index 50509d1..ba1e6e4 100644 --- a/core/lua/astal/binding.lua +++ b/core/lua/astal/binding.lua @@ -29,10 +29,13 @@ function Binding:__tostring() end function Binding:get() + if self.property ~= nil and GObject.Object:is_type_of(self.emitter) then + return self.transformFn(self.emitter[self.property]) + end if type(self.emitter.get) == "function" then return self.transformFn(self.emitter:get()) end - return self.transformFn(self.emitter[self.property]) + error("can not get: Not a GObject or a Variable " + self) end ---@param transform fun(value: any): any @@ -48,17 +51,20 @@ end ---@param callback fun(value: any) ---@return function function Binding:subscribe(callback) + if self.property ~= nil and GObject.Object:is_type_of(self.emitter) then + local id = self.emitter.on_notify:connect(function() + callback(self:get()) + end, self.property, false) + return function() + GObject.signal_handler_disconnect(self.emitter, id) + end + end if type(self.emitter.subscribe) == "function" then return self.emitter:subscribe(function() callback(self:get()) end) end - local id = self.emitter.on_notify:connect(function() - callback(self:get()) - end, self.property, false) - return function() - GObject.signal_handler_disconnect(self.emitter, id) - end + error("can not subscribe: Not a GObject or a Variable " + self) end Binding.__index = Binding diff --git a/core/lua/astal/process.lua b/core/lua/astal/process.lua index 3d10f8b..6f73613 100644 --- a/core/lua/astal/process.lua +++ b/core/lua/astal/process.lua @@ -72,7 +72,7 @@ function M.exec_async(commandline, on_stdout, on_stderr) local out, err = defualt_proc_args(on_stdout, on_stderr) if type(commandline) == "table" then Astal.Process.exec_asyncv(commandline, function(_, res) - local stdout, fail = Astal.exec_asyncv_finish(res) + local stdout, fail = Astal.Process.exec_asyncv_finish(res) if fail ~= nil then err(fail) else @@ -81,7 +81,7 @@ function M.exec_async(commandline, on_stdout, on_stderr) end) else Astal.Process.exec_async(commandline, function(_, res) - local stdout, fail = Astal.exec_finish(res) + local stdout, fail = Astal.Process.exec_finish(res) if fail ~= nil then err(fail) else diff --git a/core/lua/astal/variable.lua b/core/lua/astal/variable.lua index 1e894b5..02d6b45 100644 --- a/core/lua/astal/variable.lua +++ b/core/lua/astal/variable.lua @@ -123,7 +123,9 @@ end function Variable:drop() self.variable.emit_dropped() - self.variable.run_dispose() + Astal.Time.idle(GObject.Closure(function() + self.variable.run_dispose() + end)) end ---@param callback function diff --git a/core/lua/test.lua b/core/lua/test.lua deleted file mode 100644 index f5123a3..0000000 --- a/core/lua/test.lua +++ /dev/null @@ -1,8 +0,0 @@ -local App = require("astal.application") - -App:start({ - instance_name = "test", - main = function() - App:quit(1) - end, -}) diff --git a/docs/getting-started/supported-languages.md b/docs/getting-started/supported-languages.md index f69dd19..7d8fc5f 100644 --- a/docs/getting-started/supported-languages.md +++ b/docs/getting-started/supported-languages.md @@ -25,7 +25,8 @@ components that don't render child nodes dynamically, bars and panels for exampl Examples: -- TODO +- [Simple Bar](https://github.com/Aylur/astal/tree/main/examples/lua/simple-bar) +![simple-bar](https://github.com/user-attachments/assets/a306c864-56b7-44c4-8820-81f424f32b9b) ## Python diff --git a/examples/js/simple-bar/README.md b/examples/js/simple-bar/README.md index 3a4316e..8f733da 100644 --- a/examples/js/simple-bar/README.md +++ b/examples/js/simple-bar/README.md @@ -1,6 +1,6 @@ # Simple Bar Example -![sime-bar](https://github.com/user-attachments/assets/a306c864-56b7-44c4-8820-81f424f32b9b) +![simple-bar](https://github.com/user-attachments/assets/a306c864-56b7-44c4-8820-81f424f32b9b) A simple bar for Hyprland using diff --git a/examples/js/simple-bar/widget/Bar.tsx b/examples/js/simple-bar/widget/Bar.tsx index d669fd5..492ab1d 100644 --- a/examples/js/simple-bar/widget/Bar.tsx +++ b/examples/js/simple-bar/widget/Bar.tsx @@ -22,7 +22,7 @@ function SysTray() { onClickRelease={self => { menu?.popup_at_widget(self, Gdk.Gravity.SOUTH, Gdk.Gravity.NORTH, null) }}> - + }))} @@ -64,21 +64,27 @@ function BatteryLevel() { } function Media() { - const player = Mpris.Player.new("spotify") + const mpris = Mpris.get_default() return - - `background-image: url('${cover}');` - )} - /> -