Skip to content

Commit b7f4f29

Browse files
fix: Ensure visual indicating for focus loss. (#226)
* fix: Ensure visual indicating for focus loss. If the workspace loses focus, any existing block selection or cursor visuals should be hidden to represent that the focus has been lost. * feat: use CSS for passive focus on blocks and next connections * Attempt to use passive focus indicator via CSS. * Add a workaround to fix passive indicator hiding. This was a workable solution found by Rachel with the explanation captured in a new line comment. * Use a better color for passive indicators. This color came at Rachel's suggestion. --------- Co-authored-by: Rachel Fenichel <fenichel@google.com>
1 parent 8d78159 commit b7f4f29

File tree

5 files changed

+43
-8
lines changed

5 files changed

+43
-8
lines changed

src/line_cursor.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,22 @@ export class LineCursor extends Marker {
652652
}
653653
}
654654

655+
override hide(): void {
656+
super.hide();
657+
658+
// If there's a block currently selected, remove the selection since the
659+
// cursor should now be hidden.
660+
const curNode = this.getCurNode();
661+
if (curNode.getType() === ASTNode.types.BLOCK) {
662+
const block = curNode.getLocation() as Blockly.BlockSvg;
663+
if (!block.isShadow()) {
664+
Blockly.common.setSelected(null);
665+
} else {
666+
block.removeSelect();
667+
}
668+
}
669+
}
670+
655671
/**
656672
* Redraw the current marker.
657673
*

src/navigation.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,15 @@ export class Navigation {
501501
}
502502

503503
if (this.markedNode) {
504+
// Note that this hide happens twice, one before setCurNode() and once in
505+
// removeMark. The latter is actually a logical no-op because setCurNode()
506+
// will trigger a selection update of the currently marked node (if it's a
507+
// block) and that, in turn, clones the underlying block's
508+
// pathObject.svgPath. Since svgPath is updated to remove any passive
509+
// focus indicator after selection clones it, the effect of removing the
510+
// indicator doesn't do anything (hence it needs to be done *before*
511+
// selection is added in order to immediately take effect).
512+
this.passiveFocusIndicator.hide();
504513
cursor.setCurNode(this.markedNode);
505514
this.removeMark(workspace);
506515
return;

src/navigation_controller.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@ export class NavigationController {
167167
this.hasNavigationFocus = isFocused;
168168
if (isFocused) {
169169
this.navigation.focusWorkspace(workspace, true);
170+
} else {
171+
// Hide cursor to indicate lost focus. Also, mark the current node so that
172+
// it can be properly restored upon returning to the workspace.
173+
this.navigation.markAtCursor(workspace);
174+
workspace.getCursor()?.hide();
170175
}
171176
}
172177

src/passive_focus.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,7 @@ export class PassiveFocus {
9494
*/
9595
showAtBlock(node: ASTNode) {
9696
const block = node.getLocation() as BlockSvg;
97-
// Note that this changes rendering but does not change Blockly's
98-
// internal selected state.
99-
block.addSelect();
97+
utils.dom.addClass(block.pathObject.svgPath, 'passiveBlockFocus');
10098
}
10199

102100
/**
@@ -106,9 +104,7 @@ export class PassiveFocus {
106104
*/
107105
hideAtBlock(node: ASTNode) {
108106
const block = node.getLocation() as BlockSvg;
109-
// Note that this changes rendering but does not change Blockly's
110-
// internal selected state.
111-
block.removeSelect();
107+
utils.dom.removeClass(block.pathObject.svgPath, 'passiveBlockFocus');
112108
}
113109

114110
/**
@@ -122,8 +118,6 @@ export class PassiveFocus {
122118
'width': 100,
123119
'height': 5,
124120
'class': 'passiveNextIndicator',
125-
'stroke': '#4286f4',
126-
'fill': '#4286f4',
127121
});
128122
return indicator;
129123
}

test/index.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,17 @@
9696
#announcer {
9797
height: 25%;
9898
}
99+
100+
.passiveBlockFocus.blocklyPath {
101+
stroke-dasharray: 5 3;
102+
stroke-width: 3;
103+
stroke: #ffa200;
104+
}
105+
106+
.passiveNextIndicator {
107+
stroke: #ffa200;
108+
fill: #ffa200;
109+
}
99110
</style>
100111
</head>
101112

0 commit comments

Comments
 (0)