Skip to content

Commit

Permalink
fix(pushbutton): sticky buttons are released when the mouse cursor mo…
Browse files Browse the repository at this point in the history
…ves over them

wokwi/wokwi-features#686
  • Loading branch information
urish committed Dec 1, 2023
1 parent c48bd97 commit 9558368
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/pushbutton-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export class PushbuttonElement extends LitElement {

private static pushbuttonCounter = 0;
private uniqueId;
private sticky = false;

readonly pinInfo: ElementPin[] = [
{ name: '1.l', x: 0, y: 13, signals: [] },
Expand Down Expand Up @@ -77,7 +78,7 @@ export class PushbuttonElement extends LitElement {
@mouseup=${this.up}
@touchstart=${this.down}
@touchend=${this.up}
@pointerleave=${this.up}
@pointerleave=${this.leave}
@keydown=${(e: KeyboardEvent) => SPACE_KEYS.includes(e.key) && this.down()}
@keyup=${(e: KeyboardEvent) => SPACE_KEYS.includes(e.key) && this.up(e)}
>
Expand Down Expand Up @@ -158,9 +159,21 @@ export class PushbuttonElement extends LitElement {
}

private up(e: KeyboardEvent | MouseEvent) {
if (this.pressed && !ctrlCmdPressed(e)) {
if (!this.pressed) {
return;
}
if (ctrlCmdPressed(e)) {
this.sticky = true;
} else {
this.sticky = false;
this.pressed = false;
this.dispatchEvent(new Event('button-release'));
}
}

private leave(e: MouseEvent) {
if (!this.sticky) {
this.up(e);
}
}
}

0 comments on commit 9558368

Please sign in to comment.