Skip to content

Commit 82232a3

Browse files
committed
tweak variable icon to also work with fields with border rects
1 parent 765a6a3 commit 82232a3

2 files changed

Lines changed: 49 additions & 19 deletions

File tree

pxtblocks/builtins/variables.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ export function initVariables() {
2222

2323
let xmlList: HTMLElement[] = [];
2424
if (variableModelList.length > 0) {
25+
if (!mostRecentVariable) {
26+
mostRecentVariable = variableModelList[0];
27+
}
2528
let currentFile: string;
2629

2730
// variables getters first

pxtblocks/plugins/newVariableField/fieldVariable.ts

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export class FieldVariable extends Blockly.FieldVariable {
6666
private fieldRootBinding: Blockly.browserEvents.Data | null = null;
6767
private clickTargetRect: SVGRectElement;
6868
private globeIcon: svg.Text;
69+
private globeIconVisible: boolean = false;
6970

7071
/**
7172
* Check if the current variable is a global variable (exported or imported)
@@ -80,6 +81,22 @@ export class FieldVariable extends Blockly.FieldVariable {
8081
override initView() {
8182
super.initView();
8283

84+
this.globeIcon = new svg.Text("\uf0ac")
85+
.setClass("semanticIcon")
86+
.setAttribute("alignment-baseline", "middle")
87+
.setAttribute("dy", "3.5")
88+
.anchor("middle");
89+
this.fieldGroup_.appendChild(this.globeIcon.el);
90+
91+
// Add globe icon only for global variables
92+
if (this.isGlobalVariable()) {
93+
this.globeIconVisible = true;
94+
}
95+
else {
96+
this.globeIconVisible = false;
97+
this.globeIcon.el.style.display = "none";
98+
}
99+
83100
if (this.shouldAddBorderRect_()) {
84101
return;
85102
}
@@ -93,15 +110,6 @@ export class FieldVariable extends Blockly.FieldVariable {
93110
// Make sure to unset the border rect so that it isn't included in size
94111
// calculations
95112
this.borderRect_ = undefined;
96-
97-
// Add globe icon only for global variables
98-
if (this.isGlobalVariable()) {
99-
this.globeIcon = new svg.Text("\uf0ac")
100-
.setClass("semanticIcon")
101-
.setAttribute("alignment-baseline", "middle")
102-
.anchor("middle");
103-
this.fieldGroup_.appendChild(this.globeIcon.el);
104-
}
105113
}
106114

107115
override shouldAddBorderRect_() {
@@ -182,7 +190,7 @@ export class FieldVariable extends Blockly.FieldVariable {
182190
super.updateSize_(margin);
183191

184192
// Then add extra width for the icon if we're rendering it
185-
if (this.globeIcon && !this.shouldAddBorderRect_()) {
193+
if (this.globeIconVisible) {
186194
// Add space for: icon + padding between icon and text + extra padding after text for arrow
187195
this.size_.width += ICON_WIDTH + ICON_PADDING + TEXT_ARROW_PADDING;
188196
}
@@ -191,6 +199,17 @@ export class FieldVariable extends Blockly.FieldVariable {
191199
protected override positionBorderRect_() {
192200
super.positionBorderRect_();
193201

202+
// Position globe icon
203+
if (this.globeIcon) {
204+
this.globeIcon.at(ICON_WIDTH / 2, this.size_.height / 2);
205+
206+
if (this.globeIconVisible && this.borderRect_) {
207+
this.globeIcon.at(ICON_PADDING + ICON_WIDTH / 2, this.size_.height / 2);
208+
this.borderRect_.setAttribute("x", String(Number(this.borderRect_.getAttribute("x") || 0) - ICON_WIDTH - ICON_PADDING));
209+
this.borderRect_.setAttribute("width", String(Number(this.borderRect_.getAttribute("width") || 0) + ICON_WIDTH + ICON_PADDING));
210+
}
211+
}
212+
194213
// The logic below is duplicated from the blockly implementation
195214
if (!this.clickTargetRect) {
196215
return;
@@ -205,18 +224,26 @@ export class FieldVariable extends Blockly.FieldVariable {
205224
'ry',
206225
String(this.getConstants()!.FIELD_BORDER_RECT_RADIUS),
207226
);
227+
}
208228

209-
// Position globe icon
229+
protected override render_() {
210230
if (this.globeIcon) {
211-
this.globeIcon.at(ICON_WIDTH / 2, this.size_.height / 2);
231+
if (this.isGlobalVariable()) {
232+
if (!this.globeIconVisible) {
233+
this.globeIconVisible = true;
234+
this.globeIcon.el.style.display = "";
235+
}
236+
}
237+
else if (this.globeIconVisible) {
238+
this.globeIconVisible = false;
239+
this.globeIcon.el.style.display = "none";
240+
}
212241
}
213-
}
214242

215-
protected override render_() {
216243
super.render_();
217244

218245
// After parent renders, shift all children (except the icon) to make room for icon
219-
if (this.globeIcon && !this.shouldAddBorderRect_() && this.fieldGroup_) {
246+
if (this.globeIcon && this.globeIconVisible && this.fieldGroup_) {
220247
const children = this.fieldGroup_.children;
221248
for (let i = 0; i < children.length; i++) {
222249
const child = children[i] as SVGElement;
@@ -246,11 +273,11 @@ export class FieldVariable extends Blockly.FieldVariable {
246273

247274
// Update the width to account for the icon and shifted elements
248275
this.size_.width += ICON_WIDTH + ICON_PADDING;
276+
}
249277

250-
// Update the click target rect to cover the new width
251-
if (this.clickTargetRect) {
252-
this.clickTargetRect.setAttribute('width', String(this.size_.width));
253-
}
278+
// Update the click target rect to cover the new width
279+
if (this.clickTargetRect) {
280+
this.clickTargetRect.setAttribute('width', String(this.size_.width));
254281
}
255282
}
256283

0 commit comments

Comments
 (0)