Skip to content
This repository has been archived by the owner on Jan 25, 2024. It is now read-only.

Symbols and overrides #129

Merged
merged 3 commits into from
Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class PBColorGenHelper extends PBAttributesHelper {
? 'backgroundColor: ${findDefaultColor(scaffold.backgroundColor)},'
: 'backgroundColor: Color(${scaffold.backgroundColor}),\n';
}
}
if (source.color == null) {

} else if (source.color == null) {
statement = '';
} else {
if (source is! InheritedContainer) {
Expand Down
55 changes: 25 additions & 30 deletions lib/generation/generators/attribute-helper/pb_size_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ class PBSizeHelper extends PBAttributesHelper {
@override
String generate(PBIntermediateNode source) {
final buffer = StringBuffer();

bool isSymbolMaster = (source.builder_type == BUILDER_TYPE.SYMBOL_MASTER);
bool isScaffoldBody = (source.builder_type == BUILDER_TYPE.SCAFFOLD_BODY);
Map body = source.size ?? {};
double height = body['height'];
double width = body['width'];
var wString = 'width: ';
var hString = 'height: ';

//Add relative sizing if the widget has context
if (source.builder_type != null &&
source.builder_type == BUILDER_TYPE.SCAFFOLD_BODY) {
if ((source.builder_type != null) && (isSymbolMaster || isScaffoldBody)) {
var screenWidth;
var screenHeight;
if (source.currentContext?.screenTopLeftCorner?.y != null &&
Expand All @@ -24,13 +27,23 @@ class PBSizeHelper extends PBAttributesHelper {
((source.currentContext.screenTopLeftCorner.y as double) -
(source.currentContext.screenBottomRightCorner.y as double))
.abs();
if (isSymbolMaster) {
hString = 'height: constraints.maxHeight * ';
} else {
hString = 'height: MediaQuery.of(context).size.height * ';
}
}
if (source.currentContext?.screenTopLeftCorner?.x != null &&
source.currentContext?.screenBottomRightCorner?.x != null) {
screenWidth = ((source.currentContext?.screenTopLeftCorner?.x
as double) -
(source.currentContext?.screenBottomRightCorner?.x as double))
.abs();
if (isSymbolMaster) {
wString = 'width: constraints.maxWidth * ';
} else {
wString = 'width: MediaQuery.of(context).size.width * ';
}
}

height = (height != null && screenHeight != null && screenHeight > 0.0)
Expand All @@ -39,35 +52,17 @@ class PBSizeHelper extends PBAttributesHelper {
width = (width != null && screenWidth != null && screenWidth > 0.0)
? width / screenWidth
: width;
}

if (width != null) {
if (source.topLeftCorner.x != null &&
source.bottomRightCorner.x != null &&
screenWidth != null) {
buffer.write(
'width : MediaQuery.of(context).size.width * ${width.toStringAsFixed(3)},');
} else {
buffer.write('width :${width.toStringAsFixed(2)},');
}
}
if (height != null) {
if (source.topLeftCorner.y != null &&
source.bottomRightCorner.y != null &&
screenHeight != null) {
buffer.write(
' height : MediaQuery.of(context).size.height * ${height.toStringAsFixed(3)},');
} else {
buffer.write('height : ${height.toStringAsFixed(2)},');
}
}
} else {
if (width != null) {
buffer.write('width :${width.toStringAsFixed(2)},');
}
if (height != null) {
buffer.write('height : ${height.toStringAsFixed(2)},');
}
if (width != null) {
buffer.write(
' ${wString}${width.toStringAsFixed(3)},');
}
if (height != null) {
buffer.write(
'${hString}${height.toStringAsFixed(3)},');
}

return buffer.toString();
}
}
42 changes: 30 additions & 12 deletions lib/generation/generators/symbols/pb_instancesym_gen.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:parabeac_core/generation/generators/pb_generator.dart';
import 'package:parabeac_core/generation/generators/util/pb_input_formatter.dart';
import 'package:parabeac_core/input/sketch/helper/symbol_node_mixin.dart';
import 'package:parabeac_core/interpret_and_optimize/entities/inherited_bitmap.dart';
import 'package:parabeac_core/interpret_and_optimize/entities/pb_shared_instance.dart';
import 'package:parabeac_core/interpret_and_optimize/entities/subclasses/pb_intermediate_node.dart';
import 'package:quick_log/quick_log.dart';
Expand All @@ -14,26 +16,42 @@ class PBSymbolInstanceGenerator extends PBGenerator {
if (source is PBSharedInstanceIntermediateNode) {
String method_signature = source.functionCallName;
if (method_signature == null) {
log.error(' Could not find master name on: $source');
return 'Container(/** This Symbol was not found **/)';
log.error(' Could not find master name on: $source');
return 'Container(/** This Symbol was not found **/)';
}
method_signature = PBInputFormatter.formatLabel(method_signature,
destroy_digits: false, space_to_underscore: false, isTitle: false);
var buffer = StringBuffer();


method_signature = method_signature[0].toLowerCase() +
method_signature.substring(1, method_signature.length);

buffer.write('LayoutBuilder( \n');
buffer.write(' builder: (context, constraints) {\n');
buffer.write(' return ');
buffer.write(method_signature);
buffer.write('(context, ');
// TODO: Add this once PCQ-03 is fixed
// for (var param in source.sharedParamValues ?? []) {
// var text = param.value != null && param.value is String
// ? '\"${param.value}\"'
// : '${param.value}';
// if (text.isNotEmpty) {
// buffer.write((text + ','));
// }
// }
buffer.write('(context, constraints,');
for (var param in source.sharedParamValues ?? []) {
switch (param.type) {
case PBSharedParameterValue:
// TODO, maybe this points to static instances? Like Styles.g.dart that will be eventually generated,
// but what about prototype links?
break;
case InheritedBitmap:
buffer.write('${param.name}: \"assets/${param.value["_ref"]}\",');
break;
default:
buffer.write('${param.name}: \"${param.value}\",');
break;

}
}
// end of return function();
buffer.write(');\n');
// end of builder: (context, constraints) {
buffer.write('}\n');
// end of LayoutBuilder()
buffer.write(')');
return buffer.toString();
}
Expand Down
15 changes: 7 additions & 8 deletions lib/generation/generators/symbols/pb_mastersym_gen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ class PBMasterSymbolGenerator extends PBGenerator {
PBMasterSymbolGenerator() : super();

///Generating the parameters of the symbol master, keeping [definitions] so
///we can replace the generic names of the paramters by that of the real overridable node's name.
///we can replace the generic names of the parameters by that of the real overridable node's name.
///However, they need to be established in all the nodes.
String _generateParameters(List<PBSharedParameterProp> signatures,
List<PBSymbolMasterParameter> definitions) {
if (signatures == null || definitions.isEmpty) {
if ((signatures == null) || (signatures.isEmpty)) {
return '';
}
var buffer = StringBuffer();
// optional parameters
buffer.write('{ ');
for (var i = 0; i < signatures.length; i++) {
var signature = signatures[i];
var overridableType =
Expand All @@ -36,18 +38,15 @@ class PBMasterSymbolGenerator extends PBGenerator {
: 'var';

if (signature.canOverride) {
var name = definitions[i] != null ? definitions[i].propertyName : null;
var name = signature.propertyName;
if (name == null) {
continue;
}
var objectId =
definitions[i] != null ? definitions[i].parameterID : name;
var defenition =
definitions[i] != null ? definitions[i].parameterDefinition : null;

buffer.write(('$type $name,'));
}
}
buffer.write(' }');
return buffer.toString();
}

Expand Down Expand Up @@ -86,7 +85,7 @@ class PBMasterSymbolGenerator extends PBGenerator {
);
log.error(e.toString());
}
buffer.write('Widget ${name}(BuildContext context,');
buffer.write('Widget ${name}(BuildContext context, BoxConstraints constraints, ');
var parameters = _generateParameters((source.overridableProperties ?? []),
(source.parametersDefinition ?? []));
buffer.write(parameters);
Expand Down
13 changes: 11 additions & 2 deletions lib/generation/generators/visual-widgets/pb_bitmap_gen.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:parabeac_core/generation/generators/attribute-helper/pb_size_helper.dart';
import 'package:parabeac_core/generation/generators/pb_flutter_generator.dart';
import 'package:parabeac_core/generation/generators/pb_generator.dart';
import 'package:parabeac_core/input/sketch/helper/symbol_node_mixin.dart';
import 'package:parabeac_core/interpret_and_optimize/entities/subclasses/pb_intermediate_node.dart';
import 'package:parabeac_core/interpret_and_optimize/entities/inherited_bitmap.dart';

Expand All @@ -13,8 +15,15 @@ class PBBitmapGenerator extends PBGenerator {
@override
String generate(PBIntermediateNode source) {
var buffer = StringBuffer();
buffer.write(
'Image.asset(\'assets/${source is InheritedBitmap ? source.referenceImage : ('images/' + source.UUID + '.png')}\', ${_sizehelper.generate(source)})');
buffer.write('Image.asset(');
if (source.builder_type == BUILDER_TYPE.SYMBOL_MASTER) {
// see if source is overridden
String ovrName = SN_UUIDtoVarName[source.UUID];
if (ovrName != null) {
buffer.write("${ovrName} ?? ");
}
}
buffer.write('\'assets/${source is InheritedBitmap ? source.referenceImage : ('images/' + source.UUID + '.png')}\', ${_sizehelper.generate(source)})');
return buffer.toString();
}
}
9 changes: 5 additions & 4 deletions lib/generation/generators/visual-widgets/pb_padding_gen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ class PBPaddingGen extends PBGenerator {

String relativePadding(BUILDER_TYPE type, bool isVertical, double value) {
var fixedValue = value.toStringAsFixed(2);
if ((type != null) && (type != BUILDER_TYPE.SYMBOL_MASTER)) {
var property = isVertical ? 'height' : 'width';
return 'MediaQuery.of(context).size.$property * $fixedValue';
if (type != null) {
if (type == BUILDER_TYPE.SYMBOL_MASTER) {
return 'constraints.max' + (isVertical ? 'Height' : 'Width') + ' * $fixedValue';
}
return 'MediaQuery.of(context).size.' + (isVertical ? 'height' : 'width') + ' * $fixedValue';
}

return '$fixedValue';
}

Expand Down
32 changes: 30 additions & 2 deletions lib/generation/generators/visual-widgets/pb_positioned_gen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,41 @@ class PBPositionedGenerator extends PBGenerator {
var buffer = StringBuffer();
buffer.write('Positioned(');

double hAlignValue = source.horizontalAlignValue;
double vAlignValue = source.verticalAlignValue;
var multStringH = '';
var multStringV = '';

// TODO: this should be for all widgets once LayoutBuilder and constraints are used
if (source.builder_type == BUILDER_TYPE.SYMBOL_MASTER) {
if (source.currentContext?.screenTopLeftCorner?.x != null &&
source.currentContext?.screenBottomRightCorner?.x != null) {
double screenWidth = ((source.currentContext?.screenTopLeftCorner?.x
as double) -
(source.currentContext?.screenBottomRightCorner?.x as double))
.abs();
multStringH = 'constraints.maxWidth * ';
hAlignValue = hAlignValue / screenWidth;
}

if (source.currentContext?.screenTopLeftCorner?.y != null &&
source.currentContext?.screenBottomRightCorner?.y != null) {
double screenHeight =
((source.currentContext.screenTopLeftCorner.y as double) -
(source.currentContext.screenBottomRightCorner.y as double))
.abs();
multStringV = 'constraints.maxHeight * ';
vAlignValue = vAlignValue / screenHeight;
}
}

if (source.horizontalAlignValue != null) {
buffer.write(
'${source.horizontalAlignType} :${source.horizontalAlignValue},');
'${source.horizontalAlignType}: ${multStringH}${hAlignValue},');
}
if (source.verticalAlignValue != null) {
buffer.write(
'${source.verticalAlignType} :${source.verticalAlignValue},');
'${source.verticalAlignType} :${multStringV}${vAlignValue},');
}

try {
Expand Down
8 changes: 8 additions & 0 deletions lib/generation/generators/visual-widgets/pb_text_gen.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:parabeac_core/generation/generators/pb_flutter_generator.dart';
import 'package:parabeac_core/generation/generators/pb_generator.dart';
import 'package:parabeac_core/input/sketch/helper/symbol_node_mixin.dart';
import 'package:parabeac_core/interpret_and_optimize/entities/inherited_text.dart';
import 'package:parabeac_core/interpret_and_optimize/entities/subclasses/pb_intermediate_node.dart';

Expand All @@ -15,6 +17,12 @@ class PBTextGen extends PBGenerator {
var text = source.text;
buffer.write('$text, \n');
} else {
if (source.builder_type == BUILDER_TYPE.SYMBOL_MASTER) {
var ovrName = SN_UUIDtoVarName[source.UUID];
if (ovrName != null) {
buffer.write('${ovrName} ?? ');
}
}
buffer
.write(('\'${source.text?.replaceAll('\n', ' ') ?? ''}\'') + ',\n');
}
Expand Down
2 changes: 1 addition & 1 deletion lib/input/figma/entities/layers/component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class Component extends FigmaFrame
overrideProperties?.map((prop) {
var properties = extractParameter(prop.overrideName);
return PBSharedParameterProp(
properties[0], null, prop.canOverride, name, properties[1]);
properties['type'], null, prop.canOverride, name, properties['uuid'], properties['default_value']);
})?.toList();

@override
Expand Down
2 changes: 1 addition & 1 deletion lib/input/helper/page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Page {
}

List<PageItem> getPageItems() {
log.info('We encountered a page that has 0 page items.');
log.info('We encountered a page that has ${_pageItems.length} page items.');
return _pageItems;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/input/sketch/entities/layers/symbol_instance.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class SymbolInstance extends SketchNode
///Converting the [OverridableValue] into [PBSharedParameterValue] to be processed in intermediate phase.
List<PBSharedParameterValue> _extractParameters() => overrideValues?.map((e) {
var properties = extractParameter(e.overrideName);
return PBSharedParameterValue(properties[0], e.value, properties[1]);
return PBSharedParameterValue(properties['type'], e.value, properties['uuid']);
})?.toList();

@override
Expand Down
5 changes: 3 additions & 2 deletions lib/input/sketch/entities/layers/symbol_master.dart
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,10 @@ class SymbolMaster extends AbstractGroupLayer
///Converting the [OverridableProperty] into [PBSharedParameterProp] to be processed in intermediate phase.
List<PBSharedParameterProp> _extractParameters() =>
overrideProperties?.map((prop) {
var properties = extractParameter(prop.overrideName);
// add ourselves to the list so symbol instances can find our parameter names
var properties = AddMasterSymbolName(prop.overrideName, children);
return PBSharedParameterProp(
properties[0], null, prop.canOverride, name, properties[1]);
properties['type'], null, prop.canOverride, properties['name'], properties['uuid'], properties['default_value']);
})?.toList();

@override
Expand Down
Loading