Phosphor
Qt6 / Wayland library suite for window-management tools
 
Loading...
Searching...
No Matches
LuauTileAlgorithm.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 fuddlesworth
2// SPDX-License-Identifier: LGPL-2.1-or-later
3
4#pragma once
5
6#include "AutotileConstants.h"
8#include "TilingAlgorithm.h"
9
10#include <phosphortiles_export.h>
11
12#include <QString>
13#include <QVariantMap>
14
15#include <memory>
16
18class LuauEngine;
19class LuauWatchdog;
20}
21
22namespace PhosphorTiles {
23
24class SplitNode;
25
57class PHOSPHORTILES_EXPORT LuauTileAlgorithm : public TilingAlgorithm
58{
59 Q_OBJECT
60
61public:
64 explicit LuauTileAlgorithm(const QString& filePath,
65 std::shared_ptr<PhosphorScripting::LuauWatchdog> watchdog = nullptr,
66 QObject* parent = nullptr);
71 LuauTileAlgorithm(const QString& filePath, std::shared_ptr<PhosphorScripting::LuauEngine> sharedEngine,
72 std::shared_ptr<PhosphorScripting::LuauWatchdog> watchdog, QObject* parent = nullptr);
74
79 static std::shared_ptr<PhosphorScripting::LuauEngine>
80 createSandboxedEngine(std::shared_ptr<PhosphorScripting::LuauWatchdog> watchdog, QString* error = nullptr);
81
83 bool isValid() const;
84
85 QString filePath() const;
86
88 QString id() const;
89
90 void setUserScript(bool isUser);
91
92 // TilingAlgorithm interface
93 QString name() const override;
94 QString description() const override;
95 QVector<QRect> calculateZones(const TilingParams& params) const override;
96 int masterZoneIndex() const override;
97 bool supportsMasterCount() const override;
98 bool supportsSplitRatio() const override;
99 qreal defaultSplitRatio() const override;
100 int minimumWindows() const override;
101 int defaultMaxWindows() const override;
102 bool producesOverlappingZones() const override;
103 bool supportsMinSizes() const noexcept override;
104 QString overlapStacking() const noexcept override;
105 bool supportsMemory() const noexcept override;
106 QString zoneNumberDisplay() const noexcept override;
107 bool centerLayout() const override;
108 bool supportsSingleWindow() const noexcept override;
109 bool retilesOnFocusChange() const noexcept override;
110 bool isScripted() const noexcept override;
111 bool isUserScript() const noexcept override;
112 void prepareTilingState(TilingState* state) const override;
113
114 // Lifecycle hooks (v2)
115 bool supportsLifecycleHooks() const noexcept override;
116 void onWindowAdded(TilingState* state, int windowIndex) override;
117 void onWindowRemoved(TilingState* state, int windowIndex) override;
118
119 // Interactive-resize hook (v2) — for non-tree algorithms that persist their
120 // own ctx.state (e.g. an aligned grid remembering column widths).
121 bool supportsResizeHook() const noexcept override;
122 void onWindowResized(TilingState* state, const ResizeEvent& resize) override;
123 bool supportsScriptState() const noexcept override;
124
125 // Custom parameters (v2)
126 bool supportsCustomParams() const noexcept override;
127 QVariantList customParamDefList() const override;
128 bool hasCustomParam(const QString& name) const override;
129
130private:
131 bool loadScript(const QString& filePath);
132 void cacheMetadataAndOverrides();
133
135 QVariantMap buildContext(const TilingParams& params, const QRect& area) const;
137 QVariantMap buildStateMap(const TilingState* state, bool includeCountAfterRemoval) const;
138
139 // Engine ownership: exactly one of m_ownedEngine / m_sharedEngine is set,
140 // and m_engine is a non-owning view of whichever is active. Both are
141 // shared_ptr so the active engine survives this object's deferred-delete
142 // teardown (see class docs); m_sharedEngine additionally keeps the
143 // loader's shared VM alive until this algorithm releases its module.
144 std::shared_ptr<PhosphorScripting::LuauEngine> m_ownedEngine;
145 std::shared_ptr<PhosphorScripting::LuauEngine> m_sharedEngine;
146 PhosphorScripting::LuauEngine* m_engine = nullptr;
147 std::shared_ptr<PhosphorScripting::LuauWatchdog> m_watchdog;
148 int m_module = -1;
149
150 QString m_filePath;
151 QString m_scriptId;
152 bool m_valid = false;
153 bool m_isUserScript = false;
154 bool m_hasOnWindowAdded = false;
155 bool m_hasOnWindowRemoved = false;
156 bool m_hasOnWindowResized = false;
157
158 ScriptedHelpers::ScriptMetadata m_metadata;
159
160 // Accessor values resolved once at load (override fn → metadata → default).
161 int m_cachedMasterZoneIndex = -1;
162 int m_cachedMinimumWindows = 1;
163 int m_cachedDefaultMaxWindows = AutotileDefaults::ScriptedDefaultMaxWindows;
164 qreal m_cachedDefaultSplitRatio = AutotileDefaults::DefaultSplitRatio;
165 bool m_cachedSupportsMasterCount = false;
166 bool m_cachedSupportsSplitRatio = false;
167 bool m_cachedProducesOverlappingZones = false;
168 bool m_cachedOverlapFirstOnTop = false;
169 bool m_cachedCenterLayout = false;
170};
171
172} // namespace PhosphorTiles
Algorithm-layer constants for the autotile/tile primitives.
A tiling algorithm backed by a user-provided Luau script.
Definition LuauTileAlgorithm.h:58
bool supportsMinSizes() const noexcept override
Whether this algorithm supports per-window minimum size constraints.
LuauTileAlgorithm(const QString &filePath, std::shared_ptr< PhosphorScripting::LuauEngine > sharedEngine, std::shared_ptr< PhosphorScripting::LuauWatchdog > watchdog, QObject *parent=nullptr)
Shared-engine ctor: loads this script's module into sharedEngine (an already init+prelude+sandbox'd V...
LuauTileAlgorithm(const QString &filePath, std::shared_ptr< PhosphorScripting::LuauWatchdog > watchdog=nullptr, QObject *parent=nullptr)
Owned-engine ctor: creates an isolated, per-engine-capped VM for this script.
int minimumWindows() const override
Get minimum number of windows for meaningful tiling.
static std::shared_ptr< PhosphorScripting::LuauEngine > createSandboxedEngine(std::shared_ptr< PhosphorScripting::LuauWatchdog > watchdog, QString *error=nullptr)
Build a sandboxed VM with the pluau standard library installed and frozen, ready for loadModule().
bool supportsSplitRatio() const override
Check if algorithm supports split ratio adjustment.
bool isValid() const
Whether the script loaded and exposes a callable tile() function.
int defaultMaxWindows() const override
Get default maximum number of windows for this algorithm.
int masterZoneIndex() const override
Get the index of the "master" zone (if applicable)
QString name() const override
Human-readable name of the algorithm.
qreal defaultSplitRatio() const override
Get default split ratio for this algorithm.
bool supportsMasterCount() const override
Check if algorithm supports variable master count.
QVector< QRect > calculateZones(const TilingParams &params) const override
Calculate zone geometries for N windows.
QString id() const
Optional algorithm id declared in metadata (empty if unset).
bool producesOverlappingZones() const override
Whether this algorithm intentionally produces overlapping zones.
QString description() const override
Description of the algorithm behavior.
Abstract base class for tiling algorithms.
Definition TilingAlgorithm.h:54
Tracks tiling state for a single screen.
Definition TilingState.h:40
Definition LuauTileAlgorithm.h:17
Definition AutotileEngine.h:59
Describes an interactive resize that triggered a retile.
Definition TilingParams.h:51
Parameters for zone calculation.
Definition TilingParams.h:84