Gin
Loading...
Searching...
No Matches
Classes | Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
LayoutSupport Class Reference

JSON-based layout system for JUCE components. More...

#include <gin_layoutsupport.h>

Inherits gin::FileSystemWatcher::Listener.

Classes

struct  Bounds
 
struct  JsonFile
 

Public Member Functions

 LayoutSupport (juce::Component &parent_, std::function< std::pair< juce::Component *, bool >(const juce::String &)> factory_=nullptr)
 Construct a LayoutSupport instance for the given parent component.
 
 ~LayoutSupport () override
 
bool isLayoutSet ()
 Returns true if a layout has been set.
 
void setLayout (const juce::Array< juce::File > &files)
 Load layout from JSON files.
 
void setLayout (const juce::StringArray &resourceNames)
 Load layout from binary resources (BinaryData).
 
void setLayout (const juce::String &rawJson)
 Load layout from raw JSON string.
 
void setConstant (const juce::String &name, int value)
 Set or update a constant value.
 

Static Public Member Functions

static void dumpComponents (juce::Component &c)
 Generate JSON layout from existing component hierarchy.
 

Public Attributes

std::function< void()> layoutChanged
 Callback invoked when layout is loaded or reloaded.
 

Protected Member Functions

juce::String findContent (const juce::String &name)
 
juce::Component * getComp (const juce::String &cid)
 
void setLayoutInternal (const juce::Array< JsonFile > &files)
 
int parse (const juce::var &equation, int equationIndex)
 
void setComponentsLayout (const juce::String &currentPath, const juce::var &components)
 
void loadConstants (juce::var &j)
 
void loadMacros (juce::var &j)
 
Bounds getBounds (int idIdx, const juce::var &component)
 
juce::Component * setPosition (const juce::String &currentPath, const juce::String &id, int idIdx, const juce::var &component)
 
juce::var expandMacro (const juce::var &component)
 
void setGridPositions (const juce::String &currentPath, const juce::var &component)
 
void fileChanged (const juce::File &, gin::FileSystemWatcher::FileSystemEvent) override
 

Protected Attributes

juce::Component & parent
 
ConstantsStack constants
 
std::map< juce::String, juce::var > macros
 
juce::Array< juce::File > layoutFiles
 
bool layoutSet = false
 
juce::Component * curComponent = nullptr
 
juce::OwnedArray< juce::Component > createdComponents
 
ComponentMap compMap
 
std::function< std::pair< juce::Component *, bool >(const juce::String &)> componentFactory
 

Static Protected Attributes

static int refCount = 0
 
static std::unique_ptr< FileSystemWatcherwatcher
 

Detailed Description

JSON-based layout system for JUCE components.

Allows declarative component positioning with expressions, constants, macros, and live reloading.

Usage Example

class MyComponent : public juce::Component
{
public:
{
button.setComponentID ("myButton");
layoutSupport.setLayout ({ juce::File ("layout.json") });
}
private:
juce::TextButton button;
};
JSON-based layout system for JUCE components.
Definition gin_layoutsupport.h:231
A lightweight 2D point class for projects that don't use juce_graphics.
Definition gin_point.h:25
void addAndMakeVisible(juce::Component &parent, juce::Array< juce::Component * > children)
Adds an Array of components to a parent Component.
Definition gin_componentutils.h:16

JSON Format

Top-level properties:

Component Properties

Identification:**

Constants

Built-in constants:

Custom constants can be defined at top-level or per-component:

{
"constants": [
{ "margin": 10 },
{ "buttonSize": { "w": 100, "h", 25 } }
],
"components": [
{ "id": "button", "x": "margin", "w": 100, "h": 30 }
]
}

Expressions

All numeric properties support mathematical expressions:

Example: ‘"x": "parW / 2 - getW('button&rsquo;) / 2"`

Macros

Reusable component templates with parameters:

{
"macros": [
{ "id": "standardButton", "w": 100, "h": 30, "tip": "P1" }
],
"components": [
{ "id": "saveBtn", "macro": "standardButton('Save')", "x": 10, "y": 10 }
]
}

Parameters are referenced as P1, P2, P3, etc.

Examples

Simple layout:

{
"components": [
{ "id": "header", "bounds": "parent" },
{ "id": "button", "x": 10, "y": 10, "w": 100, "h": 30 }
]
}

Relative positioning:

{
"components": [
{ "id": "button1", "x": 10, "y": 10, "w": 100, "h": 30 },
{ "id": "button2", "xd": 10, "y": 10 },
{ "id": "button3", "xd": 10, "y": 10 }
]
}

Grid layout:

{
"grid": "cell[0..8]",
"bounds": "10,10,300,300",
"cols": 3,
"rows": 3,
"colGap": 5,
"rowGap": 5
}

Range syntax and expressions:

{
"id": "knob[0..7]",
"x": "10 + i * 50",
"y": 10,
"w": 40,
"h": 40
}

Constructor & Destructor Documentation

◆ LayoutSupport()

LayoutSupport::LayoutSupport ( juce::Component &  parent_,
std::function< std::pair< juce::Component *, bool >(const juce::String &)>  factory_ = nullptr 
)

Construct a LayoutSupport instance for the given parent component.

Parameters
parent_The parent component whose children will be positioned
factory_Optional factory function to create components on demand. Function signature: (factoryName) -> (Component*, sendToBack) Return nullptr if factory name not recognized.

◆ ~LayoutSupport()

LayoutSupport::~LayoutSupport ( )
override

Member Function Documentation

◆ dumpComponents()

static void LayoutSupport::dumpComponents ( juce::Component &  c)
static

Generate JSON layout from existing component hierarchy.

Outputs JSON to debug console and copies to clipboard. Useful for migrating existing layouts to JSON format.

Parameters
cRoot component to dump

◆ isLayoutSet()

bool LayoutSupport::isLayoutSet ( )

Returns true if a layout has been set.

◆ setLayout() [1/3]

void LayoutSupport::setLayout ( const juce::Array< juce::File > &  files)

Load layout from JSON files.

Watches files for changes and auto-reloads.

Parameters
filesArray of JSON files to load

◆ setLayout() [2/3]

void LayoutSupport::setLayout ( const juce::StringArray &  resourceNames)

Load layout from binary resources (BinaryData).

Parameters
resourceNamesArray of resource names to load

◆ setLayout() [3/3]

void LayoutSupport::setLayout ( const juce::String &  rawJson)

Load layout from raw JSON string.

Parameters
rawJsonJSON string containing layout definition

◆ setConstant()

void LayoutSupport::setConstant ( const juce::String &  name,
int  value 
)

Set or update a constant value.

Parameters
nameConstant name
valueConstant value

◆ findContent()

juce::String LayoutSupport::findContent ( const juce::String &  name)
protected

◆ getComp()

juce::Component * LayoutSupport::getComp ( const juce::String &  cid)
protected

◆ setLayoutInternal()

void LayoutSupport::setLayoutInternal ( const juce::Array< JsonFile > &  files)
protected

◆ parse()

int LayoutSupport::parse ( const juce::var &  equation,
int  equationIndex 
)
protected

◆ setComponentsLayout()

void LayoutSupport::setComponentsLayout ( const juce::String &  currentPath,
const juce::var &  components 
)
protected

◆ loadConstants()

void LayoutSupport::loadConstants ( juce::var &  j)
protected

◆ loadMacros()

void LayoutSupport::loadMacros ( juce::var &  j)
protected

◆ getBounds()

Bounds LayoutSupport::getBounds ( int  idIdx,
const juce::var &  component 
)
protected

◆ setPosition()

juce::Component * LayoutSupport::setPosition ( const juce::String &  currentPath,
const juce::String &  id,
int  idIdx,
const juce::var &  component 
)
protected

◆ expandMacro()

juce::var LayoutSupport::expandMacro ( const juce::var &  component)
protected

◆ setGridPositions()

void LayoutSupport::setGridPositions ( const juce::String &  currentPath,
const juce::var &  component 
)
protected

◆ fileChanged()

void LayoutSupport::fileChanged ( const juce::File &  ,
gin::FileSystemWatcher::FileSystemEvent   
)
overrideprotected

Member Data Documentation

◆ layoutChanged

std::function<void()> LayoutSupport::layoutChanged

Callback invoked when layout is loaded or reloaded.

◆ parent

juce::Component& LayoutSupport::parent
protected

◆ refCount

int LayoutSupport::refCount = 0
staticprotected

◆ watcher

std::unique_ptr<FileSystemWatcher> LayoutSupport::watcher
staticprotected

◆ constants

ConstantsStack LayoutSupport::constants
protected

◆ macros

std::map<juce::String, juce::var> LayoutSupport::macros
protected

◆ layoutFiles

juce::Array<juce::File> LayoutSupport::layoutFiles
protected

◆ layoutSet

bool LayoutSupport::layoutSet = false
protected

◆ curComponent

juce::Component* LayoutSupport::curComponent = nullptr
protected

◆ createdComponents

juce::OwnedArray<juce::Component> LayoutSupport::createdComponents
protected

◆ compMap

ComponentMap LayoutSupport::compMap
protected

◆ componentFactory

std::function<std::pair<juce::Component*, bool>(const juce::String&)> LayoutSupport::componentFactory
protected

The documentation for this class was generated from the following file: