Skip to content

Commit

Permalink
feat: update to current gridui
Browse files Browse the repository at this point in the history
  • Loading branch information
Tasssadar committed Jul 5, 2024
1 parent 1afba32 commit 192b4a6
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 44 deletions.
24 changes: 12 additions & 12 deletions components/CodeDisplay.vue
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,14 @@ export default {
hidden: {
type: Boolean,
required: false,
default: true,
default: true
}
},
data() {
return {
showCode: !this.hideable
}
},
watch: {
hidden: {
immediate: true,
handler() {
if(this.hideable && this.showCode !== !this.hidden) {
this.showCode = !this.hidden
}
}
},
},
computed: {
highlighted() {
return Prism.highlight(
Expand All @@ -90,6 +80,16 @@ export default {
)
}
},
watch: {
hidden: {
immediate: true,
handler() {
if (this.hideable && this.showCode !== !this.hidden) {
this.showCode = !this.hidden
}
}
}
},
methods: {
onCopyClick() {
copy(this.value)
Expand All @@ -101,7 +101,7 @@ export default {
},
onShowClick() {
this.showCode = !this.showCode
this.$emit("update:hidden", !this.showCode)
this.$emit('update:hidden', !this.showCode)
}
}
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@nuxtjs/axios": "^5.3.6",
"copy-to-clipboard": "^3.3.1",
"deep-equal": "^2.0.2",
"jszip": "^3.10.1",
"nipplejs": "^0.8.5",
"nuxt": "^2.0.0",
"prismjs": "^1.23.0",
Expand Down
105 changes: 89 additions & 16 deletions pages/codegen_jaculus.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@
gridui.h methods
</v-btn>

<v-btn
block
color="secondary"
class="justify-start"
large
text
@click="onDownloadClick"
>
Download zip
</v-btn>

<v-card-title>Widgets</v-card-title>
<v-btn
v-for="t in widgetTypes"
Expand Down Expand Up @@ -42,6 +53,7 @@
</template>

<script>
import JSZip from 'jszip'
import CodeDisplay from '~/components/CodeDisplay'
import * as Utils from '~/src/layoutgen/Common'
Expand Down Expand Up @@ -130,19 +142,19 @@ export default {
const typ = Utils.getCppType(prop, false)
builderMethods += `
static JSValue ${name}(JSContext* ctx_, JSValueConst thisVal, int argc, JSValueConst* argv) {
auto& builder = *reinterpret_cast<gridui::builder::${wname}*>(JS_GetOpaque(thisVal, 1));
auto& builder = builderOpaque<gridui::builder::${wname}>(thisVal);
builder.${name}(jac::ValueWeak(ctx_, argv[0]).to<${typ}>());
return JS_DupValue(ctx_, thisVal);
}
`
builderProps += `
proto.set("${name}", jac::Value(ctx, JS_NewCFunction(ctx, ${name}, "${name}", 1)));`
if(name == "${name}") return ${name};`
}
if (Object.entries(widget.prototype.EVENTS).length !== 0) {
builderProps += `\n`
for (const [, methodName] of Object.entries(widget.prototype.EVENTS)) {
builderProps += `\n defineBuilderCallback<builder::${wname}, ${wname}, &builder::${wname}::${methodName}>(ctx, proto, "${methodName}");`
builderProps += `\n if(name == "${methodName}") return &builderCallbackImpl<builder::${wname}, ${wname}, &builder::${wname}::${methodName}>;`
}
}
Expand All @@ -151,17 +163,20 @@ export default {
#include <jac/machine/functionFactory.h>
#include <gridui.h>
#include "../widgets/_common.h"
namespace gridui_jac {
class ${wname}Builder {${builderMethods}
public:
static jac::Object proto(jac::ContextRef ctx) {
static JSCFunction *getPropFunc(const AtomString& name) {
using namespace gridui;
auto proto = jac::Object::create(ctx);
if(name == "css") return builderCss<builder::${wname}>;
if(name == "finish") return builderFinish<WidgetTypeId::${wname}, builder::${wname}, ${wname}>;
${builderProps}
return proto;
return nullptr;
}
};
Expand All @@ -182,21 +197,30 @@ ${builderProps}
const setName =
'set' + name.substring(0, 1).toUpperCase() + name.substring(1)
widgetProps += `\n defineWidgetProperty(ctx, proto, "${name}", "${setName}", ${name}, ${setName});`
widgetProps += `
if(name == "${name}") {
*getter = ${name};
*setter = ${setName};
return;
}`
widgetMethods += `
static JSValue ${setName}(JSContext* ctx_, JSValueConst thisVal, JSValueConst val) {
auto& widget = *reinterpret_cast<gridui::${wname}*>(JS_GetOpaque(thisVal, 1));
auto& widget = widgetOpaque<gridui::${wname}>(thisVal);
widget.${setName}(jac::ValueWeak(ctx_, val).to<${typIn}>());
return JS_UNDEFINED;
}`
} else {
widgetProps += `\n defineWidgetPropertyReadOnly(ctx, proto, "${name}", ${name});`
widgetProps += `
if(name == "${name}") {
*getter = ${name};
return;
}`
}
widgetMethods += `
static JSValue ${name}(JSContext* ctx_, JSValueConst thisVal) {
auto& widget = *reinterpret_cast<gridui::${wname}*>(JS_GetOpaque(thisVal, 1));
auto& widget = widgetOpaque<gridui::${wname}>(thisVal);
return jac::Value::from(ctx_, widget.${name}()).loot().second;
}
`
Expand Down Expand Up @@ -240,9 +264,7 @@ namespace gridui_jac {
class ${wname}Widget {${widgetMethods}
public:
static jac::Object proto(jac::ContextRef ctx) {
auto proto = jac::Object::create(ctx);${widgetProps}
return proto;
static void getProperty(const AtomString& name, qjsGetter* getter, qjsSetter *setter) {${widgetProps}
}
};
Expand All @@ -267,10 +289,30 @@ public:
res += '\n\n'
for (const t of this.widgetTypes) {
if (t.name === 'Widget') continue
const nameLower =
t.name.substring(0, 1).toLowerCase() + t.name.substring(1)
res += `proto.defineProperty("${nameLower}", ff.newFunctionThisVariadic(std::function(&builder<WidgetTypeId::${t.name}, gridui::builder::${t.name}, gridui::${t.name}, ${t.name}Builder::proto, ${t.name}Widget::proto>)), jac::PropFlags::Enumerable);\n`
res += `proto.defineProperty("${nameLower}", ff.newFunctionThisVariadic(std::function(&builder<gridui::builder::${t.name}, gridui::${t.name}>)), jac::PropFlags::Enumerable);\n`
}
res += '\n\n'
let elseStr = ''
for (const t of this.widgetTypes) {
if (t.name === 'Widget') continue
res += `${elseStr}if(literalEqual(wName, wNameLen, "${t.name}")) getFunc = ${t.name}Builder::getPropFunc(propName);\n`
elseStr = 'else '
}
res += '\n\n'
for (const t of this.widgetTypes) {
if (t.name === 'Widget') continue
res += `case WidgetTypeId::${t.name}: ${t.name}Widget::getProperty(propName, &getter, &setter); break;\n`
}
res += '\n\n'
Expand All @@ -296,7 +338,7 @@ public:
let widgetInterfaces = ''
for (const t of this.widgetTypes) {
if(t.name === "Widget") {
if (t.name === 'Widget') {
continue
}
Expand Down Expand Up @@ -339,6 +381,8 @@ public:
}
}
builderInterfaces += `\n\n finish(): widget.${t.name}`
builderInterfaces += '\n }\n'
widgetInterfaces += '\n }\n'
Expand All @@ -356,7 +400,6 @@ public:
namespace builder {
interface Base {
css(key: string, value: string): this
finish(): this
}
${builderInterfaces} }
Expand Down Expand Up @@ -392,6 +435,36 @@ ${builderInterfaces} }
default:
return name
}
},
async onDownloadClick() {
const zip = new JSZip()
for (const t of this.widgetTypes) {
let filename = t.name.toLowerCase() + '.h'
if (t.name === 'Widget') {
filename = 'base.h'
}
const builderFile = this.generateCodeBuilder(t)
if (builderFile) {
zip.file(`builder/${filename}`, builderFile)
}
const widgetFile = this.generateCodeRuntime(t)
if (widgetFile) {
zip.file(`widgets/${filename}`, widgetFile)
}
}
const blob = await zip.generateAsync({ type: 'blob' })
const element = document.createElement('a')
element.setAttribute('href', window.URL.createObjectURL(blob))
element.setAttribute('download', 'gridui_generated.zip')
element.style.display = 'none'
document.body.appendChild(element)
element.click()
document.body.removeChild(element)
}
}
}
Expand Down
26 changes: 19 additions & 7 deletions pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,22 @@
/>
</v-card>
<v-card class="mt-1 code-card" style="flex-grow: 0">
<code-display :value="cppCode" language="cpp" title="Generated C++" :hideable="true" :hidden.sync="hideCpp"/>
<code-display
:value="cppCode"
language="cpp"
title="Generated C++"
:hideable="true"
:hidden.sync="hideCpp"
/>
</v-card>
<v-card class="mt-1 code-card" style="flex-grow: 0">
<code-display :value="tsCode" language="typescript" title="Generated TypeScript" :hideable="true" :hidden.sync="hideTs"/>
<code-display
:value="tsCode"
language="typescript"
title="Generated TypeScript"
:hideable="true"
:hidden.sync="hideTs"
/>
</v-card>
</div>
</div>
Expand Down Expand Up @@ -217,8 +229,8 @@ export default {
copyPaster: null,
tabsCount: 1,
activeTab: 0,
hideCpp: window.localStorage.getItem("hideCpp") === "true",
hideTs: window.localStorage.getItem("hideTs") === "true",
hideCpp: window.localStorage.getItem('hideCpp') === 'true',
hideTs: window.localStorage.getItem('hideTs') === 'true'
}
},
computed: {
Expand Down Expand Up @@ -268,11 +280,11 @@ export default {
gGrid.setCurrentTab(val)
},
hideCpp() {
window.localStorage.setItem("hideCpp", this.hideCpp ? "true" : "false")
window.localStorage.setItem('hideCpp', this.hideCpp ? 'true' : 'false')
},
hideTs() {
window.localStorage.setItem("hideTs", this.hideTs ? "true" : "false")
},
window.localStorage.setItem('hideTs', this.hideTs ? 'true' : 'false')
}
},
mounted() {
if (gGrid !== null) return
Expand Down
16 changes: 10 additions & 6 deletions src/layoutgen/Typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ function genMember(widget, namespace) {
function genBuilder(widget, isUsingTabs) {
const type = widget.constructor.name
const proto = Object.getPrototypeOf(widget)
const nameLower =
type.substring(0, 1).toLowerCase() + type.substring(1)
const nameLower = type.substring(0, 1).toLowerCase() + type.substring(1)

const id = Common.getPropertyValue(widget, 'id')
let res = `${id}: builder.${nameLower}(`
Expand All @@ -28,8 +27,8 @@ function genBuilder(widget, isUsingTabs) {
if (Common.isDefaultPropValue(widget, name, prop)) continue

const val = Common.getPropertyValue(widget, name, prop)
res += "\n" + Common.formatProperty(name, val, ' ')
res = res.substring(0, res.length-1)
res += '\n' + Common.formatProperty(name, val, ' ')
res = res.substring(0, res.length - 1)
}
return res
}
Expand All @@ -55,8 +54,12 @@ export function generate(widgets, layout) {
const builder = widgets
.map((w) => genBuilder(w, isUsingTabs))
.join(',\n ')
const builderMembers = widgets.map((w) => genMember(w, 'gridui.builder')).join('\n ')
const layoutMembers = widgets.map((w) => genMember(w, 'gridui.widget')).join('\n ')
const builderMembers = widgets
.map((w) => genMember(w, 'gridui.builder'))
.join('\n ')
const layoutMembers = widgets
.map((w) => genMember(w, 'gridui.widget'))
.join('\n ')

const baseMinVer = isUsingTabs ? 0x040900 : 0x000000
const minVersion = widgets
Expand Down Expand Up @@ -122,6 +125,7 @@ const layout = {
for(const key in layoutBuilder) {
layout[key] = layoutBuilder[key].finish()
layoutBuilder[key] = undefined
}
})
}
Expand Down
Loading

0 comments on commit 192b4a6

Please sign in to comment.