Skip to content

Commit

Permalink
lvgl page post create refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mvladic committed Oct 13, 2024
1 parent e9a7451 commit debb7fc
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 84 deletions.
24 changes: 12 additions & 12 deletions packages/project-editor/features/page/PagesNavigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export const PageStructure = observer(
);
}

onLockAll = () => {
onLockAll = action(() => {
if (!this.treeAdapter) {
return;
}
Expand All @@ -202,9 +202,9 @@ export const PageStructure = observer(
});

this.context.undoManager.setCombineCommands(false);
};
});

onUnlockAll = () => {
onUnlockAll = action(() => {
if (!this.treeAdapter) {
return;
}
Expand All @@ -221,7 +221,7 @@ export const PageStructure = observer(
});

this.context.undoManager.setCombineCommands(false);
};
});

get isAnyHidden() {
if (!this.treeAdapter) {
Expand All @@ -236,7 +236,7 @@ export const PageStructure = observer(
);
}

onHideAll = () => {
onHideAll = action(() => {
if (!this.treeAdapter) {
return;
}
Expand All @@ -253,9 +253,9 @@ export const PageStructure = observer(
});

this.context.undoManager.setCombineCommands(false);
};
});

onShowAll = () => {
onShowAll = action(() => {
if (!this.treeAdapter) {
return;
}
Expand All @@ -272,7 +272,7 @@ export const PageStructure = observer(
});

this.context.undoManager.setCombineCommands(false);
};
});

renderItem = (itemId: string) => {
if (!this.treeAdapter) {
Expand Down Expand Up @@ -302,11 +302,11 @@ export const PageStructure = observer(
: "Lock this widget"
}
iconSize={14}
onClick={() =>
onClick={action(() =>
this.context.updateObject(widget, {
locked: !widget.locked
})
}
)}
style={{
visibility: widget.locked ? "visible" : "hidden"
}}
Expand All @@ -319,7 +319,7 @@ export const PageStructure = observer(
}
title={widget.hiddenInEditor ? "Show" : "Hide"}
iconSize={14}
onClick={() => {
onClick={action(() => {
const hiddenInEditor = !widget.hiddenInEditor;

this.context.undoManager.setCombineCommands(
Expand Down Expand Up @@ -349,7 +349,7 @@ export const PageStructure = observer(
this.context.undoManager.setCombineCommands(
false
);
}}
})}
style={{
visibility: widget.hiddenInEditor
? "visible"
Expand Down
8 changes: 0 additions & 8 deletions packages/project-editor/features/page/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -996,10 +996,6 @@ export class Page extends Flow {
customWidget
);

this._lvglWidgetsIncludingUserWidgets.forEach(lvglWidget =>
lvglWidget.lvglPostCreate(runtime)
);

return lvglObj;
} else {
const obj = customWidget
Expand All @@ -1024,10 +1020,6 @@ export class Page extends Flow {
.filter(component => component instanceof Widget)
.map((widget: Widget) => widget.lvglCreate(runtime, obj));

this._lvglWidgetsIncludingUserWidgets.forEach(lvglWidget =>
lvglWidget.lvglPostCreate(runtime)
);

return obj;
}
}
Expand Down
2 changes: 0 additions & 2 deletions packages/project-editor/flow/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3897,8 +3897,6 @@ export class Widget extends Component {
) {
return 0;
}

lvglPostCreate(runtime: LVGLPageRuntime) {}
}

////////////////////////////////////////////////////////////////////////////////
Expand Down
23 changes: 18 additions & 5 deletions packages/project-editor/lvgl/page-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,19 @@ export abstract class LVGLPageRuntime {
lvglStyle.lvglRemoveStyleFromObject(this, targetObj);
}
}

postCreateCallbacks: (() => void)[] = [];

addPostCreateCallback(callback: () => void) {
this.postCreateCallbacks.push(callback);
}

lvglCreatePage() {
const pageObj = this.page.lvglCreate(this, 0);
this.postCreateCallbacks.forEach(callback => callback());
this.postCreateCallbacks = [];
return pageObj;
}
}

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -451,7 +464,7 @@ export class LVGLPageEditorRuntime extends LVGLPageRuntime {

this.createStyles();

const pageObj = this.page.lvglCreate(this, 0);
const pageObj = this.lvglCreatePage();
if (!pageObj) {
console.error("pageObj is undefined");
}
Expand Down Expand Up @@ -633,7 +646,7 @@ export class LVGLNonActivePageViewerRuntime extends LVGLPageRuntime {

this.createStyles();

const pageObj = this.page.lvglCreate(this, 0);
const pageObj = this.lvglCreatePage();
this.wasm._lvglScreenLoad(-1, pageObj);
runInAction(() => {
this.page._lvglRuntime = this;
Expand Down Expand Up @@ -859,7 +872,7 @@ export class LVGLPageViewerRuntime extends LVGLPageRuntime {

this.createStyles();

const pageObj = this.page.lvglCreate(this, 0);
const pageObj = this.lvglCreatePage();

this.wasm._lvglAddScreenLoadedEventHandler(pageObj);

Expand Down Expand Up @@ -1116,7 +1129,7 @@ export class LVGLStylesEditorRuntime extends LVGLPageRuntime {
}
});

const pageObj = this.page.lvglCreate(this, 0);
const pageObj = this.lvglCreatePage();
if (!pageObj) {
console.error("pageObj is undefined");
return;
Expand Down Expand Up @@ -1334,7 +1347,7 @@ export class LVGLReflectEditorRuntime extends LVGLPageRuntime {
-(new Date().getTimezoneOffset() / 60) * 100
);

const pageObj = this.page.lvglCreate(this, 0);
const pageObj = this.lvglCreatePage();
if (!pageObj) {
console.error("pageObj is undefined");
return;
Expand Down
62 changes: 31 additions & 31 deletions packages/project-editor/lvgl/widgets/Keyboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -201,41 +201,41 @@ export class LVGLKeyboardWidget extends LVGLWidget {
KEYBOARD_MODES[this.mode]
);

return obj;
}

override lvglPostCreate(runtime: LVGLPageRuntime) {
if (this.textarea) {
const lvglIdentifier = ProjectEditor.getProjectStore(
this
).lvglIdentifiers.getIdentifierByName(
ProjectEditor.getFlow(this),
this.textarea
);
runtime.addPostCreateCallback(() => {
if (this.textarea) {
const lvglIdentifier = ProjectEditor.getProjectStore(
this
).lvglIdentifiers.getIdentifierByName(
ProjectEditor.getFlow(this),
this.textarea
);

if (lvglIdentifier) {
const textareaWidget = lvglIdentifier.object;
if (lvglIdentifier) {
const textareaWidget = lvglIdentifier.object;

if (
textareaWidget instanceof LVGLTextareaWidget &&
(runtime instanceof LVGLPageViewerRuntime ||
runtime instanceof LVGLNonActivePageViewerRuntime)
) {
setTimeout(() => {
if (
this._lvglObj &&
textareaWidget._lvglObj &&
runtime.isMounted
) {
runtime.wasm._lvglSetKeyboardTextarea(
this._lvglObj,
textareaWidget._lvglObj
);
}
});
if (
textareaWidget instanceof LVGLTextareaWidget &&
(runtime instanceof LVGLPageViewerRuntime ||
runtime instanceof LVGLNonActivePageViewerRuntime)
) {
setTimeout(() => {
if (
this._lvglObj &&
textareaWidget._lvglObj &&
runtime.isMounted
) {
runtime.wasm._lvglSetKeyboardTextarea(
this._lvglObj,
textareaWidget._lvglObj
);
}
});
}
}
}
}
});

return obj;
}

override lvglBuildObj(build: LVGLBuild) {
Expand Down
36 changes: 19 additions & 17 deletions packages/project-editor/lvgl/widgets/UserWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -459,23 +459,25 @@ export class LVGLUserWidgetWidget extends LVGLWidget {
if (runtime.wasm.assetsMap) {
const flow =
runtime.wasm.assetsMap.flows[savedUserWidgetContext.pageIndex];
const componentPath = getObjectPathAsString(this);
const componentIndex = flow.componentIndexes[componentPath];

runtime.lvglCreateContext = {
widgetIndex:
savedUserWidgetContext.widgetIndex + widgetIndex + 1,
pageIndex:
runtime.wasm.assetsMap.flowIndexes[
getObjectPathAsString(this.userWidgetPage!)
],
flowState: savedUserWidgetContext.flowState
? runtime.wasm._lvglGetFlowState(
savedUserWidgetContext.flowState,
componentIndex
)
: 0
};
if (flow) {
const componentPath = getObjectPathAsString(this);
const componentIndex = flow.componentIndexes[componentPath];

runtime.lvglCreateContext = {
widgetIndex:
savedUserWidgetContext.widgetIndex + widgetIndex + 1,
pageIndex:
runtime.wasm.assetsMap.flowIndexes[
getObjectPathAsString(this.userWidgetPage!)
],
flowState: savedUserWidgetContext.flowState
? runtime.wasm._lvglGetFlowState(
savedUserWidgetContext.flowState,
componentIndex
)
: 0
};
}
}

const rect = this.getLvglCreateRect();
Expand Down
24 changes: 15 additions & 9 deletions packages/project-editor/store/layout-models.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,8 @@ export class LayoutModels extends AbstractLayoutModels {
children: [
{
type: "row",
weight: 20,
weight: 0,
width: 350,
children: [
{
type: "tabset",
Expand Down Expand Up @@ -422,15 +423,16 @@ export class LayoutModels extends AbstractLayoutModels {
},
{
type: "tabset",
weight: 60,
weight: 100,
enableDeleteWhenEmpty: false,
enableClose: false,
id: LayoutModels.EDITOR_MODE_EDITORS_TABSET_ID,
children: []
},
{
type: "row",
weight: 25,
weight: 0,
width: 420,
children: [
{
type: "tabset",
Expand Down Expand Up @@ -479,7 +481,8 @@ export class LayoutModels extends AbstractLayoutModels {
children: [
{
type: "row",
weight: 20,
weight: 0,
width: 350,
children: [
{
type: "tabset",
Expand Down Expand Up @@ -507,15 +510,16 @@ export class LayoutModels extends AbstractLayoutModels {
},
{
type: "tabset",
weight: 60,
weight: 1,
enableClose: false,
enableDeleteWhenEmpty: false,
id: LayoutModels.EDITOR_MODE_EDITORS_TABSET_ID,
children: []
},
{
type: "row",
weight: 25,
weight: 0,
width: 420,
children: [
{
type: "tabset",
Expand Down Expand Up @@ -549,7 +553,8 @@ export class LayoutModels extends AbstractLayoutModels {
children: [
{
type: "row",
weight: 25,
weight: 0,
width: 320,
children: [
{
type: "tabset",
Expand Down Expand Up @@ -590,15 +595,16 @@ export class LayoutModels extends AbstractLayoutModels {
},
{
type: "tabset",
weight: 60,
weight: 1,
enableClose: false,
enableDeleteWhenEmpty: false,
id: LayoutModels.RUNTIME_MODE_EDITORS_TABSET_ID,
children: []
},
{
type: "row",
weight: 25,
weight: 0,
width: 320,
children: [
{
type: "tabset",
Expand Down

0 comments on commit debb7fc

Please sign in to comment.