forked from microsoft/copilot-for-eclipse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFeaturePage.java
More file actions
544 lines (463 loc) · 19.4 KB
/
Copy pathFeaturePage.java
File metadata and controls
544 lines (463 loc) · 19.4 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package com.microsoft.copilot.eclipse.ui.quickstart;
import org.eclipse.e4.ui.css.swt.CSSSWTConstants;
import org.eclipse.e4.ui.services.IStylingEngine;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
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.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.PlatformUI;
import com.microsoft.copilot.eclipse.core.utils.PlatformUtils;
import com.microsoft.copilot.eclipse.ui.i18n.Messages;
import com.microsoft.copilot.eclipse.ui.utils.UiUtils;
/**
* GitHub Copilot feature page for the Quick Start dialog. Contains the main UI components and their update logic.
*/
public class FeaturePage extends Composite {
/**
* Enum for different feature types in the quick start dialog.
*/
public enum Feature {
AGENT, ASK, COMPLETION
}
private Label rightPanelContent;
private Composite selectedCard;
private Color normalBackgroundColor;
private Color selectedBackgroundColor;
private Image currentContentImage;
private Display display;
private boolean isDarkTheme;
private static final int ALIGNED_MARGIN = 24;
/**
* Creates a FeaturePage with colors appropriate for the current theme.
*/
public FeaturePage(Composite parent) {
super(parent, SWT.NONE);
// Cache commonly used objects
this.display = Display.getCurrent();
this.isDarkTheme = UiUtils.isDarkTheme();
initializeColors();
this.addDisposeListener(e -> disposeColors());
createContent(parent);
}
/**
* Initializes theme-appropriate colors.
*/
private void initializeColors() {
if (isDarkTheme) {
// Dark theme colors
normalBackgroundColor = new Color(display, 47, 48, 48); // #2F3030
selectedBackgroundColor = new Color(display, 72, 72, 76); // #48484C
} else {
// Light theme colors
normalBackgroundColor = new Color(display, 255, 255, 255); // #FFFFFF
selectedBackgroundColor = new Color(display, 241, 241, 242); // #F1F1F2
}
}
/**
* Creates the main content area with all UI components.
*/
private void createContent(Composite parent) {
GridLayout containerLayout = new GridLayout(1, false);
containerLayout.marginWidth = 0;
containerLayout.verticalSpacing = 0;
this.setLayout(containerLayout);
this.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
setData(CSSSWTConstants.CSS_ID_KEY, "quick-start-container");
// Title section
createTitleSection(this);
// Main content area
createMainContent(this);
}
private void createTitleSection(Composite parent) {
GridLayout titleLayout = new GridLayout(1, false);
titleLayout.marginWidth = 0;
titleLayout.marginHeight = 0;
titleLayout.marginBottom = ALIGNED_MARGIN;
Composite titleComposite = new Composite(parent, SWT.NONE);
titleComposite.setLayout(titleLayout);
titleComposite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
titleComposite.setData(CSSSWTConstants.CSS_ID_KEY, "quick-start-container");
Composite titleAndCloseComposite = new Composite(titleComposite, SWT.NONE);
GridLayout titleAndCloseLayout = new GridLayout(2, false);
titleAndCloseLayout.marginLeft = 20;
titleAndCloseComposite.setLayout(titleAndCloseLayout);
titleAndCloseComposite.setData(CSSSWTConstants.CSS_ID_KEY, "quick-start-container");
titleAndCloseComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
Label titleLabel = new Label(titleAndCloseComposite, SWT.NONE);
titleLabel.setText(Messages.quickStart_title);
titleLabel.setData(CSSSWTConstants.CSS_ID_KEY, "quick-start-container");
titleLabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false));
FontData fontData = new FontData();
fontData.setHeight(14);
fontData.setStyle(SWT.BOLD);
Font titleFont = new Font(Display.getCurrent(), fontData);
titleLabel.setFont(titleFont);
titleLabel.addDisposeListener(e -> {
if (titleFont != null && !titleFont.isDisposed()) {
titleFont.dispose();
}
});
createCloseButton(titleAndCloseComposite);
Label subtitleLabel = new Label(titleComposite, SWT.WRAP | SWT.CENTER);
subtitleLabel.setText(Messages.quickStart_description);
subtitleLabel.setData(CSSSWTConstants.CSS_ID_KEY, "quick-start-container");
subtitleLabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false));
}
private void createMainContent(Composite parent) {
GridLayout mainLayout = new GridLayout(2, false);
mainLayout.horizontalSpacing = 12;
mainLayout.marginHeight = 0;
mainLayout.marginWidth = 0;
Composite mainComposite = new Composite(parent, SWT.NONE);
mainComposite.setLayout(mainLayout);
GridData mainLayoutData = new GridData(SWT.FILL, SWT.FILL, true, true);
mainLayoutData.minimumHeight = 327;
mainComposite.setLayoutData(mainLayoutData);
mainComposite.setData(CSSSWTConstants.CSS_ID_KEY, "quick-start-container");
// Left panel with feature cards
createLeftPanel(mainComposite);
// Right panel
createRightPanel(mainComposite);
// Set initial selection to Agent feature after panel creation
selectCard(selectedCard, Feature.AGENT);
}
private void createLeftPanel(Composite parent) {
GridLayout leftPanelLayout = new GridLayout(1, false);
leftPanelLayout.marginWidth = 0;
leftPanelLayout.marginHeight = 0;
leftPanelLayout.verticalSpacing = 8;
Composite leftPanel = new Composite(parent, SWT.NONE);
leftPanel.setLayout(leftPanelLayout);
GridData leftPanelData = new GridData(SWT.FILL, SWT.FILL, false, true);
leftPanelData.widthHint = 245;
leftPanel.setLayoutData(leftPanelData);
leftPanel.setData(CSSSWTConstants.CSS_ID_KEY, "quick-start-container");
// Create clickable feature cards (Agent selected by default)
selectedCard = createClickableFeatureCard(leftPanel, "/icons/github_copilot.png", Messages.quickStart_agent_title,
Messages.quickStart_agent_description, Feature.AGENT);
createClickableFeatureCard(leftPanel, "/icons/chat/chatview_icon_chat.png", Messages.quickStart_ask_title,
Messages.quickStart_ask_description, Feature.ASK);
createClickableFeatureCard(leftPanel, "/icons/chat/chatview_icon_code.png", Messages.quickStart_completion_title,
Messages.quickStart_completion_description, Feature.COMPLETION);
}
private void createRightPanel(Composite parent) {
Canvas rightPanel = createRoundedPanel(parent);
GridData rightPanelData = new GridData(SWT.FILL, SWT.FILL, true, true);
rightPanelData.widthHint = 520; // Set width to 520 pixels
rightPanelData.heightHint = 327; // Set height to 327 pixels
rightPanel.setLayoutData(rightPanelData);
rightPanel.setData(CSSSWTConstants.CSS_ID_KEY, "quick-start-feature-card");
// Content area that will change based on selection
rightPanelContent = new Label(rightPanel, SWT.WRAP | SWT.CENTER);
rightPanelContent.setData(CSSSWTConstants.CSS_ID_KEY, "quick-start-feature-card");
rightPanelContent.addDisposeListener(e -> {
if (currentContentImage != null && !currentContentImage.isDisposed()) {
currentContentImage.dispose();
}
});
// Add resize listener to properly size the content
rightPanel.addListener(SWT.Resize, e -> {
Rectangle bounds = rightPanel.getBounds();
rightPanelContent.setBounds(25, 25, bounds.width - 30, bounds.height - 50);
});
updateRightPanelContent(Feature.AGENT);
}
private Composite createClickableFeatureCard(Composite parent, String imagePath, String title, String description,
Feature feature) {
// Create a canvas for custom rounded painting
Canvas cardCanvas = new Canvas(parent, SWT.NONE);
cardCanvas.setData(CSSSWTConstants.CSS_ID_KEY, "quick-start-container");
GridData canvasData = new GridData(SWT.FILL, SWT.FILL, true, true);
canvasData.widthHint = 245;
canvasData.heightHint = 102;
cardCanvas.setLayoutData(canvasData);
cardCanvas.setSize(245, 102);
// Add paint listener for rounded border
cardCanvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {
GC gc = e.gc;
// Set anti-aliasing for smoother curves
gc.setAntialias(SWT.ON);
// Get background color from stored data, fallback to canvas background
Color bgColor = (Color) cardCanvas.getData("bgColor");
if (bgColor == null) {
bgColor = cardCanvas.getBackground();
}
// Fill rounded rectangle background with proper color
gc.setBackground(bgColor);
Rectangle bounds = cardCanvas.getBounds();
gc.fillRoundRectangle(2, 2, bounds.width - 5, bounds.height - 5, 15, 15);
}
});
// Create the content composite inside the canvas
GridLayout cardLayout = new GridLayout(1, false);
cardLayout.marginWidth = 0;
cardLayout.marginHeight = 0;
cardLayout.verticalSpacing = 0;
Composite card = new Composite(cardCanvas, SWT.NONE);
card.setLayout(cardLayout);
card.setData(CSSSWTConstants.CSS_ID_KEY, "quick-start-feature-card");
// Add resize listener to properly size the card content
cardCanvas.addListener(SWT.Resize, e -> {
Rectangle bounds = cardCanvas.getBounds();
Point cardSize = card.computeSize(bounds.width - 16, SWT.DEFAULT);
int x = 8;
int y = (bounds.height - cardSize.y) / 2; // Center vertically
card.setBounds(x, y, bounds.width - 16, cardSize.y);
});
// Make components clickable
MouseAdapter clickHandler = new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
selectCard(card, feature);
}
};
addClickableToControls(clickHandler, cardCanvas, card);
// Icon and title composite
GridLayout iconTitleLayout = new GridLayout(2, false);
iconTitleLayout.marginHeight = 0;
iconTitleLayout.marginWidth = 0;
iconTitleLayout.horizontalSpacing = 8;
Composite iconTitleComposite = new Composite(card, SWT.NONE);
iconTitleComposite.setLayout(iconTitleLayout);
iconTitleComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
iconTitleComposite.setData(CSSSWTConstants.CSS_ID_KEY, "quick-start-feature-card");
// Icon - Load image from path using UiUtils
Image iconImage = UiUtils.buildImageFromPngPath(imagePath);
Label iconLabel = new Label(iconTitleComposite, SWT.NONE);
iconLabel.setImage(iconImage);
iconLabel.setData(CSSSWTConstants.CSS_ID_KEY, "quick-start-feature-card");
GridData iconData = new GridData(SWT.LEFT, SWT.CENTER, false, false);
iconData.widthHint = 20;
iconData.heightHint = 20;
iconLabel.setLayoutData(iconData);
// Dispose image when label is disposed
iconLabel.addDisposeListener(e -> {
if (iconImage != null && !iconImage.isDisposed()) {
iconImage.dispose();
}
});
// Make icon clickable too
addClickableToControls(clickHandler, iconLabel);
Label titleLabel = new Label(iconTitleComposite, SWT.NONE);
titleLabel.setText(title);
titleLabel.setData(CSSSWTConstants.CSS_ID_KEY, "quick-start-feature-card");
titleLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
// Make title clickable too
addClickableToControls(clickHandler, titleLabel);
// Make title bold
FontData[] fontDataArray = titleLabel.getFont().getFontData();
for (FontData fontData : fontDataArray) {
fontData.setStyle(SWT.BOLD);
fontData.setHeight(PlatformUtils.isMac() ? 15 : 10);
}
Font cardTitleFont = new Font(display, fontDataArray);
titleLabel.setFont(cardTitleFont);
// Add dispose listener for title font
titleLabel.addDisposeListener(e -> {
if (cardTitleFont != null && !cardTitleFont.isDisposed()) {
cardTitleFont.dispose();
}
});
// Description composite
GridLayout descLayout = new GridLayout(1, false);
descLayout.marginHeight = 0;
descLayout.marginWidth = 0;
descLayout.marginLeft = 28; // Align with title text (icon width + spacing)
Composite descComposite = new Composite(card, SWT.NONE);
descComposite.setLayout(descLayout);
descComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
descComposite.setData(CSSSWTConstants.CSS_ID_KEY, "quick-start-feature-card");
// Make description composite clickable
addClickableToControls(clickHandler, descComposite);
Label descLabel = new Label(descComposite, SWT.WRAP);
descLabel.setText(description);
descLabel.setData(CSSSWTConstants.CSS_ID_KEY, "quick-start-feature-card");
descLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
if (PlatformUtils.isMac()) {
// Set font height for description label on macOS
FontData[] descFontDataArray = descLabel.getFont().getFontData();
for (FontData fontData : descFontDataArray) {
fontData.setHeight(13);
}
Font descFont = new Font(Display.getCurrent(), descFontDataArray);
descLabel.setFont(descFont);
// Add dispose listener for description font
descLabel.addDisposeListener(e -> {
if (descFont != null && !descFont.isDisposed()) {
descFont.dispose();
}
});
}
// Make description clickable too
addClickableToControls(clickHandler, descLabel);
return card;
}
private Canvas createRoundedPanel(Composite parent) {
Canvas canvas = new Canvas(parent, SWT.NONE);
canvas.setData(CSSSWTConstants.CSS_ID_KEY, "quick-start-feature-card");
canvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {
GC gc = e.gc;
// Set anti-aliasing for smoother curves
gc.setAntialias(SWT.ON);
gc.setBackground(selectedBackgroundColor);
Rectangle bounds = canvas.getBounds();
gc.fillRoundRectangle(2, 2, bounds.width - 5, bounds.height - 5, 15, 15);
}
});
return canvas;
}
private void selectCard(Composite card, Feature feature) {
// Update the previously selected card back to normal
if (selectedCard != null) {
updateControlBackground(selectedCard, false);
}
// Update the newly selected card
selectedCard = card;
updateControlBackground(card, true);
// Update right panel content
updateRightPanelContent(feature);
}
private void updateControlBackground(Control card, boolean selected) {
Color backgroundColor = selected ? selectedBackgroundColor : normalBackgroundColor;
// Find the parent canvas and set the bgColor data there
Control parent = card.getParent();
if (parent instanceof Canvas) {
parent.setData("bgColor", backgroundColor);
parent.redraw(); // Redraw the canvas to trigger the paint listener
}
// Update CSS ID for styling consistency
updateChildrenControlBackground(card, selected);
IStylingEngine engine = PlatformUI.getWorkbench().getService(IStylingEngine.class);
if (engine != null) {
engine.setId(card, selected ? "quick-start-feature-card-selected" : "quick-start-feature-card");
}
}
private void updateChildrenControlBackground(Control control, boolean selected) {
control.setData(CSSSWTConstants.CSS_ID_KEY,
selected ? "quick-start-feature-card-selected" : "quick-start-feature-card");
if (control instanceof Composite composite) {
for (Control child : composite.getChildren()) {
updateChildrenControlBackground(child, selected);
}
}
}
private void updateRightPanelContent(Feature feature) {
// Dispose previous image if it exists
if (currentContentImage != null && !currentContentImage.isDisposed()) {
currentContentImage.dispose();
currentContentImage = null;
}
String imagePath;
switch (feature) {
case ASK:
imagePath = "/intro/quickstart/quick_start_ask.png";
break;
case COMPLETION:
imagePath = "/intro/quickstart/quick_start_completion.png";
break;
case AGENT:
default:
imagePath = "/intro/quickstart/quick_start_agent.png";
}
// Load and set the new image
currentContentImage = UiUtils.buildImageFromPngPath(imagePath);
rightPanelContent.setImage(currentContentImage);
rightPanelContent.requestLayout();
}
/**
* Closes the dialog containing this feature page.
*/
private void closeDialog() {
// Find the parent shell and close it
getShell().close();
}
/**
* Disposes of all color resources.
*/
private void disposeColors() {
if (normalBackgroundColor != null && !normalBackgroundColor.isDisposed()) {
normalBackgroundColor.dispose();
}
if (selectedBackgroundColor != null && !selectedBackgroundColor.isDisposed()) {
selectedBackgroundColor.dispose();
}
}
/**
* Creates a close button with hover effects.
*/
private Label createCloseButton(Composite parent) {
Image normalImage = UiUtils
.buildImageFromPngPath(isDarkTheme ? "/intro/quickstart/close_dark.png" : "/intro/quickstart/close_light.png");
Label closeButton = new Label(parent, SWT.NONE);
closeButton.setImage(normalImage);
closeButton.setData(CSSSWTConstants.CSS_ID_KEY, "quick-start-container");
GridData closeButtonData = new GridData(SWT.RIGHT, SWT.TOP, false, false);
closeButtonData.verticalIndent = 0;
closeButton.setLayoutData(closeButtonData);
closeButton.setToolTipText(Messages.quickStart_closeButton_tooltip);
// Add click listener to close the dialog
closeButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
closeDialog();
}
});
Image hoverImage = UiUtils.buildImageFromPngPath(
isDarkTheme ? "/intro/quickstart/close_hover_dark.png" : "/intro/quickstart/close_hover_light.png");
// Add dispose listener for close button images
closeButton.addDisposeListener(e -> {
if (normalImage != null && !normalImage.isDisposed()) {
normalImage.dispose();
}
if (hoverImage != null && !hoverImage.isDisposed()) {
hoverImage.dispose();
}
});
// Add hover effect for close button
closeButton.addMouseTrackListener(new MouseTrackAdapter() {
@Override
public void mouseEnter(MouseEvent e) {
closeButton.setCursor(display.getSystemCursor(SWT.CURSOR_HAND));
closeButton.setImage(hoverImage);
closeButton.redraw();
}
@Override
public void mouseExit(MouseEvent e) {
closeButton.setCursor(null);
closeButton.setImage(normalImage);
closeButton.redraw();
}
});
return closeButton;
}
/**
* Adds a click handler to multiple controls.
*/
private void addClickableToControls(MouseAdapter clickHandler, Control... controls) {
for (Control control : controls) {
control.addMouseListener(clickHandler);
}
}
}