Skip to content

Commit

Permalink
[Feat] Column and Row Alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
thize committed Jun 11, 2020
1 parent 1c7141d commit 4e5bafb
Show file tree
Hide file tree
Showing 11 changed files with 364 additions and 49 deletions.
22 changes: 6 additions & 16 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const { Artboard, SymbolInstance } = require("scenegraph");
const { itemsToDart } = require("./src/items_to_dart");
const { StatelessWidget } = require("./src/widgets/stateless");
const { formatDart } = require("./src/widgets/util/format_dart");
const { findMasterForSymbolId } = require("./src/util");
const { ComponentWidget } = require("./src/widgets/component");
const { listToString } = require("./src/util");

function onTapGenerate() {
const items = scenegraph.selection.items;
Expand Down Expand Up @@ -34,17 +35,10 @@ module.exports = {
function generateComponents(components) {
const componentsWidget = [];
components.forEach(component => {
const master = findMasterForSymbolId(component.symbolId);
const componentName = master.name;
const dartCode = new StatelessWidget(componentName, itemsToDart([component])).toDart();
const dartCode = new ComponentWidget(component).toDartClass();
componentsWidget.push(dartCode);
});
let stringComponents = '';
componentsWidget.forEach(componentWidget => {
stringComponents += '\n' + componentWidget;
});
stringComponents = formatDart(stringComponents);
clipboard.copyText(stringComponents);
clipboard.copyText(formatDart(listToString(componentsWidget)));
}

function generateArtboards(artboards) {
Expand All @@ -53,15 +47,11 @@ function generateArtboards(artboards) {
const dartCode = new StatelessWidget(artboard.name, itemsToDart([artboard])).toDart();
artboardsWidget.push(dartCode);
});
let stringArtboards = '';
artboardsWidget.forEach(artboardWidget => {
stringArtboards += '\n' + artboardWidget;
});
stringArtboards = formatDart(stringArtboards);
clipboard.copyText(stringArtboards);
clipboard.copyText(formatDart(listToString(artboardsWidget)));
}

function generateSelection(items) {
const dartCode = formatDart(itemsToDart(items) + ";");
clipboard.copyText(dartCode);
}

23 changes: 21 additions & 2 deletions src/icon/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,27 @@ class AppIcon {
}

async exportAndroidAdaptiveIcons() {
console.log('exportAndroidAdaptiveIcons');

const mainFolder = await this.flutterProjectFolder.getEntry('android/app/src/main');
const resFolder = mainFolder.getEntry('res');
const entrys = [await resFolder.getEntry('mipmap-hdpi'), await resFolder.getEntry('mipmap-mdpi'), await resFolder.getEntry('mipmap-xhdpi'), await resFolder.getEntry('mipmap-xxhdpi'), await resFolder.getEntry('mipmap-xxxhdpi')];
const scales = [72, 48, 96, 144, 192];
for (let i = 0; i < entrys.length; i++) {
const file = await entrys[i].createFile(`ic_launcher.png`, { overwrite: true });
const obj = this.generateRenditionObject(scales[i], file);
this.renditions.push(obj);
}
// getEntry('mipmap-anydpi-v26'); dont exist, it have to be created
const anyDpiFolder = await resFolder.getEntry('mipmap-anydpi-v26');
const icLauncherXml = await anyDpiFolder.createFile(`ic_launcher.xml`, { overwrite: true });
const icLauncherRoundXml = await anyDpiFolder.createFile(`ic_launcher.xml`, { overwrite: true });
const xml = `<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@mipmap/ic_launcher_background"/>
<foreground android:drawable="@mipmap/ic_launcher_foreground"/>
</adaptive-icon>`;
icLauncherXml.write(xml);
icLauncherRoundXml.write(xml);
this.createRenditions('Android Adaptive');
}

generateRenditionObject(scale, file, withoutAlpha) {
Expand Down
8 changes: 7 additions & 1 deletion src/items_to_dart.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const { xdItemToWidget, widgetCanHaveChild, removeItemsFromGroup } = require("./
const { Bounds } = require("./bounds");
const { Children } = require("./widgets/children");
const { ArtboardWidget } = require("./widgets/artboard");
const { wrapWithInkWell, wrapWithRotation } = require("./widgets/util/widgets_util");
const { ComponentWidget } = require("./widgets/component");

function itemsToDart(items) {
const ungroupedItems = removeItemsFromGroup(items);
Expand Down Expand Up @@ -281,7 +283,11 @@ class Node {
this.widget = new Children(this.type, this);
}
const child = this.children != null ? this.children[0] : null
return this.widget.toDart(child);
const dartWidget = this.widget.toDart(child);
if (this.widget instanceof ComponentWidget) {
return dartWidget;
}
return wrapWithRotation(this, wrapWithInkWell(this.widget.XdNode, dartWidget));
}

debug(depth) {
Expand Down
20 changes: 19 additions & 1 deletion src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ const { InkWellWidget } = require("./widgets/inkwell");
const { SvgWidget } = require("./widgets/svg");
const { TextWidget } = require("./widgets/text");

function fix(num, digits = 1) {
let p = Math.pow(10, digits);
num = Math.round(num * p) / p;
return num + (num === (num | 0) ? '.0' : '');
}

exports.fix = fix;

function _isSvgLine(item) {
return item instanceof Line && item.globalBounds.width != 0 && item.globalBounds.height != 0;
}
Expand Down Expand Up @@ -117,4 +125,14 @@ function findMasterForSymbolId(symbolId, xdNode) {
return result;
}

exports.findMasterForSymbolId = findMasterForSymbolId;
exports.findMasterForSymbolId = findMasterForSymbolId;

function listToString(list) {
var string = '';
list.forEach(item => {
string += '\n' + item;
});
return string;
}

exports.listToString = listToString;
24 changes: 22 additions & 2 deletions src/widgets/artboard.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { randomColor } = require("./util/widgets_util");
const { Bounds } = require("../bounds");
const { xdAlignmentToDartAlignment } = require("./util/xd_alignment_to_dart_alignment");

class ArtboardWidget {
constructor(XdNode) {
Expand All @@ -8,7 +9,7 @@ class ArtboardWidget {
}

toDart(child) {
let childWidget = child != null ? `child:${child.toDart()},` : ``;
let childWidget = child != null ? `body:${getAlignmentByFather(child, this)},` : ``;
return `Scaffold(
backgroundColor: ${randomColor()},
${childWidget}
Expand All @@ -17,4 +18,23 @@ class ArtboardWidget {

}

exports.ArtboardWidget = ArtboardWidget;
exports.ArtboardWidget = ArtboardWidget;


function getAlignmentByFather(node, fatherNode) {
const top = node.bounds.y1 - fatherNode.bounds.y1;
const right = fatherNode.bounds.x2 - node.bounds.x2;
const bot = fatherNode.bounds.y2 - node.bounds.y2;
const left = node.bounds.x1 - fatherNode.bounds.x1;
let auxBot = bot == 0 && top == 0 ? 1 : bot;
const alignY = (top / (top + auxBot));
let auxRight = right == 0 && left == 0 ? 1 : right;
const alignX = (left / (left + auxRight));
const resAlignment = xdAlignmentToDartAlignment(alignX, alignY);
if (resAlignment == 'Alignment.center')
return `Center(child: ${node.toDart()},)`;
if (resAlignment != 'Alignment.topLeft') {
return `Align(alignment: ${resAlignment}, child: ${node.toDart()},)`;
}
return node.toDart();
}
Loading

0 comments on commit 4e5bafb

Please sign in to comment.