forked from microsoft/copilot-for-eclipse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActionMenu.java
More file actions
106 lines (91 loc) · 3.18 KB
/
Copy pathActionMenu.java
File metadata and controls
106 lines (91 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package com.microsoft.copilot.eclipse.ui.nes;
import org.eclipse.e4.core.services.events.IEventBroker;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.ui.PlatformUI;
import com.microsoft.copilot.eclipse.core.events.CopilotEventConstants;
import com.microsoft.copilot.eclipse.core.utils.PlatformUtils;
import com.microsoft.copilot.eclipse.ui.utils.UiUtils;
/**
* Action menu for accepting/rejecting suggestions.
*/
public class ActionMenu {
private final StyledText text;
private final IEventBroker eventBroker;
private Menu activeMenu; // JFace created context menu
private int maxWidth; // Cached max width for alignment
private int spaceWidth; // Cached space width for alignment
/**
* Constructor.
*
* @param text the StyledText to attach the menu to
*/
public ActionMenu(StyledText text) {
this.text = text;
this.eventBroker = PlatformUI.getWorkbench().getService(IEventBroker.class);
}
/**
* Checks if the action menu is currently open.
*/
public boolean isOpen() {
return activeMenu != null && !activeMenu.isDisposed() && activeMenu.isVisible();
}
/**
* Disposes the active menu if it exists.
*/
public void dispose() {
if (activeMenu != null && !activeMenu.isDisposed()) {
activeMenu.dispose();
activeMenu = null;
}
}
/**
* Shows the action menu at the specified coordinates.
*/
public void show(int x, int y) {
if (text == null || text.isDisposed()) {
return;
}
dispose();
MenuManager menuManager = new MenuManager();
String acceptLabel = buildAlignedMenuLabel(Messages.actionMenu_accept, "Tab");
String rejectLabel = buildAlignedMenuLabel(Messages.actionMenu_reject, "Esc");
ImageDescriptor acceptImage = UiUtils.buildImageDescriptorFromPngPath("/icons/chat/keyboard-tab.png");
ImageDescriptor cancelImage = UiUtils.buildImageDescriptorFromPngPath("/icons/close.png");
menuManager.add(new Action(acceptLabel, acceptImage) {
@Override
public void run() {
if (eventBroker != null) {
eventBroker.post(CopilotEventConstants.TOPIC_NES_ACCEPT_SUGGESTION, text);
}
}
});
menuManager.add(new Action(rejectLabel, cancelImage) {
@Override
public void run() {
if (eventBroker != null) {
eventBroker.post(CopilotEventConstants.TOPIC_NES_REJECT_SUGGESTION, text);
}
}
});
Point location = text.toDisplay(x, y);
activeMenu = menuManager.createContextMenu(text.getShell());
activeMenu.setLocation(location.x, location.y);
activeMenu.setVisible(true);
}
/**
* Builds an aligned menu label with consistent spacing. Pattern matches QuotaTextCalculator's getAlignedQuotaText
* method.
*/
private String buildAlignedMenuLabel(String base, String shortcut) {
// Windows supports align the text via \t
return base + "\t" + shortcut;
}
}