Skip to content

Conversation

@sonnyp
Copy link
Contributor

@sonnyp sonnyp commented Oct 16, 2023

Noop code files were introduced in #579
So that "Run" on demos with node code in Vala or Rust would work.

This is an alternative solution that

  • Remove the noop files
  • Does not attempt to compile if there is no code
  • Remove all tags on the Library demos without code
  • Remove the placeholders - they would show in user projects too
  • Opens the demo in JavaScript if it's not available in the user preferred (last used) language

image

The downside is that if the user opens the "Code" panel on these, there isn't much to extend from. But it's also an issue with the noop files and since we have multi-windows, they can check an example with "JavaScript", "Rust" or "Vala".

Comment on lines -287 to -346
if (language === "JavaScript") {
await previewer.update(true);

// We have to create a new file each time
// because gjs doesn't appear to use etag for module caching
// ?foo=Date.now() also does not work as expected
// TODO: File a bug
const [file_javascript] = Gio.File.new_tmp("workbench-XXXXXX.js");
await file_javascript.replace_contents_async(
new GLib.Bytes(document_javascript.code_view.buffer.text || " "),
null,
false,
Gio.FileCreateFlags.NONE,
null,
);
let exports;
try {
exports = await import(`file://${file_javascript.get_path()}`);
} catch (err) {
await previewer.update(true);
throw err;
} finally {
file_javascript
.delete_async(GLib.PRIORITY_DEFAULT, null)
.catch(console.error);
}
previewer.setSymbols(exports);
} else if (language === "Vala") {
if (!isValaEnabled()) {
action_extensions.activate(null);
return;
}

compiler_vala = compiler_vala || ValaCompiler({ session });
const success = await compiler_vala.compile();
if (success) {
await previewer.useExternal();
if (await compiler_vala.run()) {
await previewer.open();
} else {
await previewer.useInternal();
}
}
} else if (language === "Rust") {
if (!isRustEnabled()) {
action_extensions.activate(null);
return;
}

compiler_rust = compiler_rust || RustCompiler({ session });
const success = await compiler_rust.compile();
if (success) {
await previewer.useExternal();
if (await compiler_rust.run()) {
await previewer.open();
} else {
await previewer.useInternal();
}
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was extracted into the compile function

src/window.js Outdated
Comment on lines 306 to 310
const buffer = langs[language.toLowerCase()].document.code_view.buffer;
// Do nothing if there is no code to avoid compile errors
if (buffer.text.trim() === "") {
return;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only addition / change to the compile logic

@Hofer-Julian
Copy link
Contributor

I am fine with the changed behavior. I didn't test the branch locally yet, though.
Let me know if you want me to do that.

@sonnyp
Copy link
Contributor Author

sonnyp commented Oct 17, 2023

@Hofer-Julian I'm confident it works well.

I'd like to hear your thoughts on

The downside is that if the user opens the "Code" panel on these, there isn't much to extend from. But it's also an issue with the noop files and since we have multi-windows, they can check an example with "JavaScript", "Rust" or "Vala".

Comment on lines -34 to -36
const language = getLanguage("javascript");
const language_tag = this.#createLanguageTag(language);
this._languages_box.append(language_tag);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was necessary because demos without Code didn't have a .js file (but they had Rust and Vala files that did nothing)

With this we don't show language for demos without code anymore

@Hofer-Julian
Copy link
Contributor

@Hofer-Julian I'm confident it works well.

I'd like to hear your thoughts on

The downside is that if the user opens the "Code" panel on these, there isn't much to extend from. But it's also an issue with the noop files and since we have multi-windows, they can check an example with "JavaScript", "Rust" or "Vala".

I would mind if the code panel can be used, just for users to notice that it doesn't do anything.
If you make it somehow hidden or not-targetable, I am fine with it.

@sonnyp
Copy link
Contributor Author

sonnyp commented Oct 17, 2023

It is hidden by default (via main.json) when the demo doesn't have/need Code.
It is still possible for the user to enable and use it.

@Hofer-Julian
Copy link
Contributor

The branch doesn't seem to work as expected for me

Bildschirmaufzeichnung.vom.2023-10-17.17-37-10.webm

@sonnyp
Copy link
Contributor Author

sonnyp commented Oct 19, 2023

@Hofer-Julian thanks for catching, I forgot to remove the placeholder which was inadequate because it would also show in user saved projects when switching lang. Done.

You might still get older files in the build dir, try again after clean build.

I also added an override to open the demo with JavaScript if it's not available in Rust or Vala.

Updated the description.

@Hofer-Julian
Copy link
Contributor

@Hofer-Julian thanks for catching, I forgot to remove the placeholder which was inadequate because it would also show in user saved projects when switching lang. Done.

Thanks, can try it again in the evening.

Copy link
Contributor

@Hofer-Julian Hofer-Julian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works as expected

@sonnyp sonnyp merged commit 3c4fe7d into main Oct 19, 2023
@sonnyp sonnyp deleted the sonnyp/demos-no-code branch October 19, 2023 18:15
sonnyp added a commit that referenced this pull request Oct 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants