2024-01-30 11:32:21 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <qcontainerfwd.h>
|
|
|
|
#include <qlist.h>
|
2024-02-01 09:29:45 +00:00
|
|
|
#include <qlogging.h>
|
2024-01-30 11:32:21 +00:00
|
|
|
#include <qmap.h>
|
|
|
|
#include <qobject.h>
|
|
|
|
#include <qqmlcomponent.h>
|
|
|
|
#include <qqmlparserstatus.h>
|
2024-02-16 14:38:20 +00:00
|
|
|
#include <qtmetamacros.h>
|
2024-01-30 11:32:21 +00:00
|
|
|
|
2024-02-16 14:38:20 +00:00
|
|
|
#include "reload.hpp"
|
2024-02-01 09:29:45 +00:00
|
|
|
|
2024-01-30 11:32:21 +00:00
|
|
|
// extremely inefficient map
|
|
|
|
template <typename K, typename V>
|
|
|
|
class AwfulMap {
|
|
|
|
public:
|
|
|
|
[[nodiscard]] bool contains(const K& key) const;
|
2024-02-01 09:29:45 +00:00
|
|
|
[[nodiscard]] V* get(const K& key);
|
2024-01-30 11:32:21 +00:00
|
|
|
void insert(K key, V value); // assumes no duplicates
|
|
|
|
bool remove(const K& key); // returns true if anything was removed
|
|
|
|
QList<QPair<K, V>> values;
|
|
|
|
};
|
|
|
|
|
2024-02-12 12:21:24 +00:00
|
|
|
///! Creates instances of a component based on a given set of variants.
|
|
|
|
/// Creates and destroys instances of the given component when the given property changes.
|
|
|
|
///
|
|
|
|
/// See [QuickShell.screens] for an example of using `Variants` to create copies of a window per
|
|
|
|
/// screen.
|
|
|
|
///
|
|
|
|
/// [QuickShell.screens]: ../quickshell#prop.screens
|
2024-02-16 14:38:20 +00:00
|
|
|
class Variants: public Reloadable {
|
2024-01-30 11:32:21 +00:00
|
|
|
Q_OBJECT;
|
2024-02-12 12:21:24 +00:00
|
|
|
/// The component to create instances of
|
2024-01-30 11:32:21 +00:00
|
|
|
Q_PROPERTY(QQmlComponent* component MEMBER mComponent);
|
2024-02-12 12:21:24 +00:00
|
|
|
/// The list of sets of properties to create instances with.
|
|
|
|
/// Each set creates an instance of the component, which are updated when the input sets update.
|
|
|
|
Q_PROPERTY(QList<QVariant> variants MEMBER mVariants WRITE setVariants);
|
2024-01-30 11:32:21 +00:00
|
|
|
Q_CLASSINFO("DefaultProperty", "component");
|
|
|
|
QML_ELEMENT;
|
|
|
|
|
|
|
|
public:
|
2024-02-16 14:38:20 +00:00
|
|
|
explicit Variants(QObject* parent = nullptr): Reloadable(parent) {}
|
2024-02-01 09:29:45 +00:00
|
|
|
|
2024-02-16 14:38:20 +00:00
|
|
|
void onReload(QObject* oldInstance) override;
|
2024-01-30 11:32:21 +00:00
|
|
|
|
|
|
|
void componentComplete() override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
void setVariants(QVariantList variants);
|
|
|
|
void updateVariants();
|
|
|
|
|
|
|
|
QQmlComponent* mComponent = nullptr;
|
|
|
|
QVariantList mVariants;
|
|
|
|
AwfulMap<QVariantMap, QObject*> instances;
|
|
|
|
};
|