Skip to content

Commit

Permalink
string-valued enums, etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
eanders-ms committed Nov 3, 2024
1 parent 80e4c2a commit 9ccbfca
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 27 deletions.
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@
"localRoot": "${workspaceRoot}/../pxt-bedrock/codegen",
"console": "integratedTerminal"
},
{
"name": "codegen2 (pxt-bedrock)",
"type": "node",
"request": "launch",
"runtimeArgs": [
"run-script",
"vscode:debug"
],
"runtimeExecutable": "npm",
"stopOnEntry": false,
"sourceMaps": true,
"outFiles": [],
"localRoot": "${workspaceRoot}/../pxt-bedrock/codegen2",
"console": "integratedTerminal"
},
{
"name": "pxt ci (pxt-core)",
"type": "node",
Expand Down
1 change: 1 addition & 0 deletions docs/static/icons/ts-logo-512.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions pxtcompiler/emitter/backjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ function ${id}(s) {
else if (e.data === null) return "null"
else if (e.data === undefined) return "undefined"
else if (typeof e.data == "number") return e.data + ""
else if (typeof e.data == "string") return e.data
else throw oops("invalid data: " + typeof e.data);
case EK.PointerLiteral:
if (e.ptrlabel()) {
Expand Down
8 changes: 3 additions & 5 deletions pxtcompiler/emitter/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3507,8 +3507,8 @@ ${lbl}: .short 0xffff
return ev;
if (/^0x[A-Fa-f\d]{2,8}$/.test(ev))
return ev;
U.userError("enumval only support number literals")
return "0"
//U.userError("enumval only support number literals")
return ev;
}

function emitFolded(f: Folded) {
Expand All @@ -3535,9 +3535,7 @@ ${lbl}: .short 0xffff
if (ev == null) {
info.constantFolded = constantFold(en.initializer)
} else {
const v = parseInt(ev)
if (!isNaN(v))
info.constantFolded = { val: v }
info.constantFolded = { val: ev }
}
} else if (decl.kind == SK.PropertyDeclaration && isStatic(decl) && isReadOnly(decl)) {
const pd = decl as PropertyDeclaration
Expand Down
2 changes: 2 additions & 0 deletions pxtcompiler/emitter/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1258,6 +1258,7 @@ namespace ts.pxtc.service {
host.opts.fileSystem = prevFS
for (let k of Object.keys(newFS))
host.setFile(k, newFS[k]) // update version numbers
res.fileSystem = U.flatClone(newFS)
if (res.diagnostics.length == 0) {
host.opts.skipPxtModulesEmit = false
host.opts.skipPxtModulesTSC = false
Expand All @@ -1275,6 +1276,7 @@ namespace ts.pxtc.service {
let ts2asm = compile(host.opts, service)
res = {
sourceMap: res.sourceMap,
fileSystem: res.fileSystem,
...ts2asm,
}
if (res.needsFullRecompile || ((!res.success || res.diagnostics.length) && host.opts.clearIncrBuildAndRetryOnError)) {
Expand Down
1 change: 1 addition & 0 deletions pxtlib/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ namespace ts.pxtc {
sourceMap?: SourceInterval[];
globalNames?: pxt.Map<SymbolInfo>;
builtVariants?: string[];
fileSystem?: pxt.Map<string>;
}

export interface Breakpoint extends LocationInfo {
Expand Down
2 changes: 2 additions & 0 deletions theme/themes/pxt/globals/site.variables
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@
@pyIcon: data-uri(@pyIconUrl);
@jsIconUrl: "../docs/static/icons/js.svg";
@jsIcon: data-uri(@jsIconUrl);
@tsIconUrl: "../docs/static/icons/ts-logo-512.svg";
@tsIcon: data-uri(@tsIconUrl);
@immersiveReaderUrl: "../docs/static/icons/immersive-reader.svg";
@immersiveReaderIcon: data-uri(@immersiveReaderUrl);
@immersiveReaderLightUrl: "../docs/static/icons/immersive-reader-light.svg";
Expand Down
15 changes: 11 additions & 4 deletions webapp/src/container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export interface SettingsMenuState {
greenScreen?: boolean;
accessibleBlocks?: boolean;
showShare?: boolean;
extDownloadMenuItems?: sui.MenuItem[];
}

export class SettingsMenu extends data.Component<SettingsMenuProps, SettingsMenuState> {
Expand Down Expand Up @@ -263,6 +264,12 @@ export class SettingsMenu extends data.Component<SettingsMenuProps, SettingsMenu
this.props.parent.signOutGithub();
}

onDropdownShow = () => {
// Ensure dropdown always has the editor's most up-to-date menu items
const extDownloadMenuItems = pxt.commands.getDownloadMenuItems?.() || [];
this.setState({ extDownloadMenuItems });
}

UNSAFE_componentWillReceiveProps(nextProps: SettingsMenuProps) {
const newState: SettingsMenuState = {};
if (nextProps.greenScreen !== undefined) {
Expand All @@ -282,6 +289,7 @@ export class SettingsMenu extends data.Component<SettingsMenuProps, SettingsMenu
|| this.state.accessibleBlocks != nextState.accessibleBlocks
|| this.state.showShare != nextState.showShare
|| nextProps.inBlocks !== this.props.inBlocks
|| !!nextState.extDownloadMenuItems?.length
}

renderCore() {
Expand Down Expand Up @@ -315,10 +323,9 @@ export class SettingsMenu extends data.Component<SettingsMenuProps, SettingsMenu
const showCenterDivider = targetTheme.selectLanguage || targetTheme.highContrast || showGreenScreen || githubUser;

const simCollapseText = headless ? lf("Toggle the File Explorer") : lf("Toggle the simulator");
const extMenuItems = pxt.commands.getDownloadMenuItems?.() || [];
const showDownloadMenuItems = headless && !!extMenuItems.length;
const showDownloadMenuItems = headless && !!this.state.extDownloadMenuItems;

return <sui.DropdownMenu role="menuitem" icon={'setting large'} title={lf("Settings")} className="item icon more-dropdown-menuitem" ref={ref => this.dropdown = ref}>
return <sui.DropdownMenu role="menuitem" icon={'setting large'} title={lf("Settings")} className="item icon more-dropdown-menuitem" ref={ref => this.dropdown = ref} onShow={this.onDropdownShow}>
{showHome && <sui.Item className="mobile only inherit" role="menuitem" icon="home" title={lf("Home")} text={lf("Home")} ariaLabel={lf("Home screen")} onClick={this.showExitAndSaveDialog} />}
{showShare && <sui.Item className="mobile only inherit" role="menuitem" icon="share alternate" title={lf("Publish your game to create a shareable link")} text={lf("Share")} ariaLabel={lf("Share Project")} onClick={this.showShareDialog} />}
{(showHome || showShare) && <div className="ui divider mobile only inherit" />}
Expand All @@ -333,7 +340,7 @@ export class SettingsMenu extends data.Component<SettingsMenuProps, SettingsMenu
{showSimCollapse ? <sui.Item role="menuitem" icon='toggle right' text={simCollapseText} onClick={this.toggleCollapse} /> : undefined}
{showDownloadMenuItems && <>
<div className="ui divider" />
{extMenuItems.map((props, index) => <sui.Item key={"ext" + index} role="menuitem" tabIndex={-1} {...props} />)}
{this.state.extDownloadMenuItems.map((props, index) => <sui.Item key={"ext" + index} role="menuitem" tabIndex={-1} {...props} />)}
</>}
<div className="ui divider"></div>
{targetTheme.selectLanguage ? <sui.Item icon='xicon globe' role="menuitem" text={lf("Language")} onClick={this.showLanguagePicker} /> : undefined}
Expand Down
71 changes: 53 additions & 18 deletions webapp/src/sui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ export interface DropdownProps extends UiProps {
id?: string;
onChange?: (v: string) => void;
onClick?: () => boolean; // Return 'true' to toggle open/close
onShow?: () => void;
onHide?: () => void;

titleContent?: React.ReactNode;
displayAbove?: boolean;
Expand All @@ -84,10 +86,12 @@ export interface DropdownState {
export class DropdownMenu extends UIElement<DropdownProps, DropdownState> {

show() {
this.props.onShow?.();
this.setState({ open: true, focus: true });
}

hide() {
this.props.onHide?.();
this.setState({ open: false });
}

Expand Down Expand Up @@ -167,32 +171,52 @@ export class DropdownMenu extends UIElement<DropdownProps, DropdownState> {
}
}

componentDidMount() {
childFocus = (child: HTMLElement) => {
this.setActive(child);
}
childBlur = (child: HTMLElement) => {
this.blur(child);
}
childClick = (child: HTMLElement) => {
this.hide();
}
childKeyDown = (child: HTMLElement, e: KeyboardEvent, prev: HTMLElement, next: HTMLElement) => {
this.navigateToNextElement(e, prev, next);
}
lastChildKeyDown = (child: HTMLElement, e: KeyboardEvent) => {
const charCode = core.keyCodeFromEvent(e);
if (!e.shiftKey && charCode === core.TAB_KEY) {
this.hide();
}
}

UNSAFE_componentWillUpdate(nextProps: Readonly<DropdownProps>, nextState: Readonly<DropdownState>, nextContext: any): void {
const children = this.getChildren();
for (let i = 0; i < children.length; i++) {
const prev = i > 0 ? children[i - 1] as HTMLElement : undefined;
const child = children[i] as HTMLElement;
const next = i < children.length ? children[i + 1] as HTMLElement : undefined;
child.removeEventListener('keydown', (e) => this.childKeyDown(child, e, prev, next));
child.removeEventListener('focus', () => this.childFocus(child));
child.removeEventListener('blur', () => this.childBlur(child));
child.removeEventListener('click', () => this.childClick(child));
child.removeEventListener('keydown', (e) => this.lastChildKeyDown(child, e));
}

Check failure on line 204 in webapp/src/sui.tsx

View workflow job for this annotation

GitHub Actions / buildpush

Trailing spaces not allowed
}

child.addEventListener('keydown', (e) => {
this.navigateToNextElement(e, prev, next);
})

child.addEventListener('focus', (e: FocusEvent) => {
this.setActive(child);
})
child.addEventListener('blur', (e: FocusEvent) => {
this.blur(child);
})

componentDidMount() {
const children = this.getChildren();
for (let i = 0; i < children.length; i++) {
const prev = i > 0 ? children[i - 1] as HTMLElement : undefined;
const child = children[i] as HTMLElement;
const next = i < children.length ? children[i + 1] as HTMLElement : undefined;
child.addEventListener('keydown', (e) => this.childKeyDown(child, e, prev, next));
child.addEventListener('focus', (e: FocusEvent) => this.childFocus(child));
child.addEventListener('blur', (e: FocusEvent) => this.childBlur(child));
child.addEventListener('click', (e) => this.childClick(child));
if (i == children.length - 1) {
// set tab on last child to clear focus
child.addEventListener('keydown', (e) => {
const charCode = core.keyCodeFromEvent(e);
if (!e.shiftKey && charCode === core.TAB_KEY) {
this.hide();
}
})
child.addEventListener('keydown', (e) => this.lastChildKeyDown(child, e));
}
}
}
Expand All @@ -204,6 +228,17 @@ export class DropdownMenu extends UIElement<DropdownProps, DropdownState> {
const child = children[i] as HTMLElement;
// On allow tabbing to valid child nodes (ie: no separators or mobile only items)
child.tabIndex = this.state.open ? 0 : -1;
// Set event handlers
const prev = i > 0 ? children[i - 1] as HTMLElement : undefined;
const next = i < children.length ? children[i + 1] as HTMLElement : undefined;
child.addEventListener('keydown', (e) => this.childKeyDown(child, e, prev, next));
child.addEventListener('focus', (e: FocusEvent) => this.childFocus(child));
child.addEventListener('blur', (e: FocusEvent) => this.childBlur(child));
child.addEventListener('click', (e) => this.childClick(child));
if (i == children.length - 1) {
// set tab on last child to clear focus
child.addEventListener('keydown', (e) => this.lastChildKeyDown(child, e));
}
}

// Check if dropdown width exceeds the bounds, add the left class to the menu
Expand Down

0 comments on commit 9ccbfca

Please sign in to comment.