Skip to content

Commit b29ec70

Browse files
committed
Updates context behavior - avoid multiple same context menus calling
1 parent 32515a0 commit b29ec70

File tree

3 files changed

+55
-50
lines changed

3 files changed

+55
-50
lines changed

src/webviews/apps/plus/home/components/active-work.ts

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { ifDefined } from 'lit/directives/if-defined.js';
66
import { when } from 'lit/directives/when.js';
77
import type { GitTrackingState } from '../../../../../git/models/branch';
88
import { createWebviewCommandLink } from '../../../../../system/webview';
9-
import type { GetOverviewBranch, OpenInGraphParams, State } from '../../../../home/protocol';
9+
import type { BranchRef, GetOverviewBranch, OpenInGraphParams, State } from '../../../../home/protocol';
1010
import { stateContext } from '../../../home/context';
1111
import type { ActionList } from '../../../shared/components/actions/action-list';
1212
import { ipcContext } from '../../../shared/context';
@@ -209,7 +209,30 @@ export class GlActiveWork extends SignalWatcher(LitElement) {
209209
`;
210210
}
211211

212-
private prevAttr = JSON.parse(document.body.getAttribute('data-vscode-context') ?? '{}');
212+
private applyContext(context: object) {
213+
const prevContext = JSON.parse(document.body.getAttribute('data-vscode-context') ?? '{}');
214+
document.body.setAttribute(
215+
'data-vscode-context',
216+
JSON.stringify({
217+
...prevContext,
218+
...context,
219+
}),
220+
);
221+
setTimeout(() => {
222+
document.body.setAttribute('data-vscode-context', JSON.stringify(prevContext));
223+
});
224+
}
225+
226+
private handleBranchContext(branchRefs: BranchRef, e: typeof ActionList.OpenContextMenuEvent) {
227+
let context = 'gitlens:home';
228+
e.detail.items.forEach(x => {
229+
if (x.href) {
230+
context += `+${x.href}`;
231+
}
232+
});
233+
// clear context immediatelly after the contextmenu is opened to avoid randomly clicked contextmenu being filled
234+
this.applyContext({ webviewItem: context, ...branchRefs, type: 'branch' });
235+
}
213236

214237
private renderActions(branch: GetOverviewBranch, repo: string) {
215238
const branchRefs = {
@@ -242,27 +265,7 @@ export class GlActiveWork extends SignalWatcher(LitElement) {
242265
return nothing;
243266
}
244267
return html`<action-list
245-
@open-actions-menu=${(e: typeof ActionList.OpenContextMenuEvent) => {
246-
this.prevAttr = JSON.parse(document.body.getAttribute('data-vscode-context') ?? '{}');
247-
let context = 'gitlens:home';
248-
e.detail.items.forEach(x => {
249-
if (x.href) {
250-
context += `+${x.href}`;
251-
}
252-
});
253-
document.body.setAttribute(
254-
'data-vscode-context',
255-
JSON.stringify({
256-
...this.prevAttr,
257-
webviewItem: context,
258-
...branchRefs,
259-
type: 'branch',
260-
}),
261-
);
262-
}}
263-
@close-actions-menu=${() => {
264-
document.body.setAttribute('data-vscode-context', JSON.stringify(this.prevAttr));
265-
}}
268+
@open-actions-menu=${this.handleBranchContext.bind(this, branchRefs)}
266269
limit=${3}
267270
.items=${actions}
268271
class="branch-item__actions"

src/webviews/apps/plus/home/components/overview.ts

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,31 @@ export class GlOverview extends SignalWatcher(LitElement) {
9292
});
9393
}
9494

95-
private prevAttr = JSON.parse(document.body.getAttribute('data-vscode-context') ?? '{}');
95+
// TODO: can be moved to a separate function (maybe for home scope only)
96+
private applyContext(context: object) {
97+
const prevContext = JSON.parse(document.body.getAttribute('data-vscode-context') ?? '{}');
98+
document.body.setAttribute(
99+
'data-vscode-context',
100+
JSON.stringify({
101+
...prevContext,
102+
...context,
103+
}),
104+
);
105+
// clear context immediatelly after the contextmenu is opened to avoid randomly clicked contextmenu being filled
106+
setTimeout(() => {
107+
document.body.setAttribute('data-vscode-context', JSON.stringify(prevContext));
108+
});
109+
}
110+
111+
private handleBranchContext(e: typeof GlBranchSection.OpenContextMenuEvent) {
112+
let context = 'gitlens:home';
113+
e.detail.items.forEach(x => {
114+
if (x.href) {
115+
context += `+${x.href}`;
116+
}
117+
});
118+
this.applyContext({ webviewItem: context, ...e.detail.branchRefs, type: 'branch' });
119+
}
96120

97121
private renderComplete(overview: Overview, isFetching = false) {
98122
if (overview == null) return nothing;
@@ -103,27 +127,7 @@ export class GlOverview extends SignalWatcher(LitElement) {
103127
.isFetching=${isFetching}
104128
.repo=${repository.path}
105129
.branches=${repository.branches.recent}
106-
@branch-context-opened=${(e: typeof GlBranchSection.OpenContextMenuEvent) => {
107-
this.prevAttr = JSON.parse(document.body.getAttribute('data-vscode-context') ?? '{}');
108-
let context = 'gitlens:home';
109-
e.detail.items.forEach(x => {
110-
if (x.href) {
111-
context += `+${x.href}`;
112-
}
113-
});
114-
document.body.setAttribute(
115-
'data-vscode-context',
116-
JSON.stringify({
117-
...this.prevAttr,
118-
webviewItem: context,
119-
...e.detail.branchRefs,
120-
type: 'branch',
121-
}),
122-
);
123-
}}
124-
@branch-context-closed=${() => {
125-
document.body.setAttribute('data-vscode-context', JSON.stringify(this.prevAttr));
126-
}}
130+
@branch-context-opened=${this.handleBranchContext}
127131
>
128132
<gl-branch-threshold-filter
129133
slot="heading-actions"

src/webviews/apps/shared/components/actions/action-list.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export class ActionList extends LitElement {
4848
super.disconnectedCallback();
4949
}
5050

51+
/** is used to remove tooltip under the context menu */
5152
@state()
5253
private open = false;
5354

@@ -57,8 +58,7 @@ export class ActionList extends LitElement {
5758
}
5859
e.preventDefault();
5960

60-
this.open = true;
61-
61+
this.open = true
6262
const event = new CustomEvent('open-actions-menu', {
6363
detail: {
6464
items: this.items
@@ -81,9 +81,8 @@ export class ActionList extends LitElement {
8181
});
8282
this.dispatchEvent(contextMenuEvent);
8383
this.modifier = undefined;
84+
8485
const handleClick = () => {
85-
const ev = new CustomEvent('close-actions-menu');
86-
this.dispatchEvent(ev);
8786
this.open = false;
8887
window.removeEventListener('keyup', handleClick);
8988
window.removeEventListener('mousedown', handleClick);
@@ -99,7 +98,6 @@ export class ActionList extends LitElement {
9998
}
10099

101100
private renderMoreOptions(from: number) {
102-
console.log('render action', this.open);
103101
return html`
104102
<action-item
105103
icon="more"

0 commit comments

Comments
 (0)