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
7#include "TilingAlgorithm.h"
8
9#include <phosphortiles_export.h>
10
11#include <QString>
12#include <QVariantMap>
13
14#include <memory>
15
17class LuauEngine;
18class LuauWatchdog;
19}
20
21namespace PhosphorTiles {
22
23class SplitNode;
24
56class PHOSPHORTILES_EXPORT LuauTileAlgorithm : public TilingAlgorithm
57{
58 Q_OBJECT
59
60public:
63 explicit LuauTileAlgorithm(const QString& filePath,
64 std::shared_ptr<PhosphorScripting::LuauWatchdog> watchdog = nullptr,
65 QObject* parent = nullptr);
70 LuauTileAlgorithm(const QString& filePath, std::shared_ptr<PhosphorScripting::LuauEngine> sharedEngine,
71 std::shared_ptr<PhosphorScripting::LuauWatchdog> watchdog, QObject* parent = nullptr);
73
78 static std::shared_ptr<PhosphorScripting::LuauEngine>
79 createSandboxedEngine(std::shared_ptr<PhosphorScripting::LuauWatchdog> watchdog, QString* error = nullptr);
80
82 bool isValid() const;
83
84 QString filePath() const;
85
87 QString id() const;
88
89 void setUserScript(bool isUser);
90
91 // TilingAlgorithm interface
92 QString name() const override;
93 QString description() const override;
94 QVector<QRect> calculateZones(const TilingParams& params) const override;
95 int masterZoneIndex() const override;
96 bool supportsMasterCount() const override;
97 bool supportsSplitRatio() const override;
98 qreal defaultSplitRatio() const override;
99 int minimumWindows() const override;
100 int defaultMaxWindows() const override;
101 bool producesOverlappingZones() const override;
102 bool supportsMinSizes() const noexcept override;
103 bool supportsMemory() const noexcept override;
104 QString zoneNumberDisplay() const noexcept override;
105 bool centerLayout() const override;
106 bool isScripted() const noexcept override;
107 bool isUserScript() const noexcept override;
108 void prepareTilingState(TilingState* state) const override;
109
110 // Lifecycle hooks (v2)
111 bool supportsLifecycleHooks() const noexcept override;
112 void onWindowAdded(TilingState* state, int windowIndex) override;
113 void onWindowRemoved(TilingState* state, int windowIndex) override;
114
115 // Interactive-resize hook (v2) — for non-tree algorithms that persist their
116 // own ctx.state (e.g. an aligned grid remembering column widths).
117 bool supportsResizeHook() const noexcept override;
118 void onWindowResized(TilingState* state, const ResizeEvent& resize) override;
119 bool supportsScriptState() const noexcept override;
120
121 // Custom parameters (v2)
122 bool supportsCustomParams() const noexcept override;
123 QVariantList customParamDefList() const override;
124 bool hasCustomParam(const QString& name) const override;
125
126private:
127 bool loadScript(const QString& filePath);
128 void cacheMetadataAndOverrides();
129
131 QVariantMap buildContext(const TilingParams& params, const QRect& area) const;
133 QVariantMap buildStateMap(const TilingState* state, bool includeCountAfterRemoval) const;
134
135 // Engine ownership: exactly one of m_ownedEngine / m_sharedEngine is set,
136 // and m_engine is a non-owning view of whichever is active. Both are
137 // shared_ptr so the active engine survives this object's deferred-delete
138 // teardown (see class docs); m_sharedEngine additionally keeps the
139 // loader's shared VM alive until this algorithm releases its module.
140 std::shared_ptr<PhosphorScripting::LuauEngine> m_ownedEngine;
141 std::shared_ptr<PhosphorScripting::LuauEngine> m_sharedEngine;
142 PhosphorScripting::LuauEngine* m_engine = nullptr;
143 std::shared_ptr<PhosphorScripting::LuauWatchdog> m_watchdog;
144 int m_module = -1;
145
146 QString m_filePath;
147 QString m_scriptId;
148 bool m_valid = false;
149 bool m_isUserScript = false;
150 bool m_hasOnWindowAdded = false;
151 bool m_hasOnWindowRemoved = false;
152 bool m_hasOnWindowResized = false;
153
154 ScriptedHelpers::ScriptMetadata m_metadata;
155
156 // Accessor values resolved once at load (override fn → metadata → default).
157 int m_cachedMasterZoneIndex = -1;
158 int m_cachedMinimumWindows = 1;
159 int m_cachedDefaultMaxWindows = 6;
160 qreal m_cachedDefaultSplitRatio = AutotileDefaults::DefaultSplitRatio;
161 bool m_cachedSupportsMasterCount = false;
162 bool m_cachedSupportsSplitRatio = false;
163 bool m_cachedProducesOverlappingZones = false;
164 bool m_cachedCenterLayout = false;
165};
166
167} // namespace PhosphorTiles
A tiling algorithm backed by a user-provided Luau script.
Definition LuauTileAlgorithm.h:57
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:51
Tracks tiling state for a single screen.
Definition TilingState.h:40
Definition LuauTileAlgorithm.h:16
Definition AutotileEngine.h:46
Describes an interactive resize that triggered a retile.
Definition TilingParams.h:51
Parameters for zone calculation.
Definition TilingParams.h:84