forked from microsoft/copilot-for-eclipse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBottomBar.java
More file actions
161 lines (135 loc) · 4.63 KB
/
Copy pathBottomBar.java
File metadata and controls
161 lines (135 loc) · 4.63 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package com.microsoft.copilot.eclipse.ui.nes;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import com.microsoft.copilot.eclipse.ui.swt.CssConstants;
/**
* Encapsulates the transient bottom notification pill that prompts the user to jump to an out-of-viewport Next Edit
* Suggestion. Extracted from the controller to reduce its size / responsibilities.
*/
public class BottomBar {
private final StyledText text;
private final Runnable jumpAction;
private Composite bar;
private Label notificationLabel;
private Font keyFont;
/**
* Constructor.
*/
public BottomBar(StyledText text, Runnable jumpAction) {
this.text = text;
this.jumpAction = jumpAction;
}
/** Show (create if necessary) the bar. */
public void show() {
if (text == null || text.isDisposed()) {
return;
}
if (bar == null || bar.isDisposed()) {
create();
}
positionAtBottom();
if (!bar.isVisible()) {
bar.setVisible(true);
bar.requestLayout();
}
}
/** Hide the bar. */
public void hide() {
if (bar != null && !bar.isDisposed()) {
bar.setVisible(false);
}
}
/** Dispose resources. */
public void dispose() {
if (bar != null && !bar.isDisposed()) {
bar.dispose();
}
}
private void create() {
Composite parent = text.getParent();
if (parent == null || parent.isDisposed()) {
return;
}
bar = new Composite(parent, SWT.DOUBLE_BUFFERED | SWT.NO_FOCUS);
bar.setData(CssConstants.CSS_ID_KEY, "nes-bottom-bar");
bar.addPaintListener(e -> {
e.gc.setForeground(CssConstants.getNesBottomBarBorderColor(bar.getDisplay()));
e.gc.setLineWidth(1);
e.gc.drawRectangle(0, 0, e.width - 1, e.height - 1);
});
GridLayout outer = new GridLayout(1, false);
outer.marginWidth = 2;
outer.marginHeight = 2;
outer.horizontalSpacing = 0;
outer.verticalSpacing = 0;
bar.setLayout(outer);
GridLayout contentLayout = new GridLayout(3, false);
contentLayout.marginWidth = 5;
contentLayout.marginHeight = 3;
contentLayout.horizontalSpacing = 4;
contentLayout.verticalSpacing = 0;
Composite content = new Composite(bar, SWT.NO_FOCUS);
content.setLayout(contentLayout);
content.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
Label pressLabel = new Label(content, SWT.NONE);
pressLabel.setText(Messages.bottomBar_press);
pressLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
Label keyLabel = new Label(content, SWT.NONE);
keyLabel.setText("Tab");
FontData baseFd = keyLabel.getFont().getFontData()[0];
baseFd.setStyle(SWT.BOLD);
Display display = parent.getDisplay();
keyFont = new Font(display, baseFd);
keyLabel.setFont(keyFont);
bar.addDisposeListener(e -> {
if (keyFont != null && !keyFont.isDisposed()) {
keyFont.dispose();
keyFont = null;
}
});
keyLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
notificationLabel = new Label(content, SWT.NONE);
notificationLabel.setText(Messages.bottomBar_jumpMessage);
notificationLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
MouseAdapter clickListener = new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
if (jumpAction != null) {
jumpAction.run();
}
}
};
bar.addMouseListener(clickListener);
content.addMouseListener(clickListener);
pressLabel.addMouseListener(clickListener);
keyLabel.addMouseListener(clickListener);
notificationLabel.addMouseListener(clickListener);
positionAtBottom();
}
private void positionAtBottom() {
if (bar == null || bar.isDisposed()) {
return;
}
Composite parent = bar.getParent();
Rectangle parentBounds = parent.getClientArea();
Point preferred = bar.computeSize(SWT.DEFAULT, SWT.DEFAULT);
int x = (parentBounds.width - preferred.x) / 2;
int y = parentBounds.height - preferred.y - 20;
bar.setBounds(x, y, preferred.x, preferred.y);
bar.moveAbove(null);
}
}