|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package software.aws.toolkits.eclipse.amazonq.util; |
| 5 | + |
| 6 | +import org.eclipse.swt.events.MouseAdapter; |
| 7 | +import org.eclipse.swt.events.MouseEvent; |
| 8 | +import org.eclipse.swt.events.SelectionAdapter; |
| 9 | +import org.eclipse.swt.events.SelectionEvent; |
| 10 | +import org.eclipse.swt.graphics.Color; |
| 11 | +import org.eclipse.swt.graphics.Font; |
| 12 | +import org.eclipse.swt.graphics.FontData; |
| 13 | +import org.eclipse.swt.layout.GridData; |
| 14 | +import org.eclipse.swt.widgets.Button; |
| 15 | +import org.eclipse.swt.widgets.Composite; |
| 16 | +import org.eclipse.swt.widgets.Display; |
| 17 | +import org.eclipse.swt.widgets.Label; |
| 18 | + |
| 19 | +import java.util.function.Consumer; |
| 20 | +import org.eclipse.swt.SWT; |
| 21 | + |
| 22 | +public final class PersistentToolkitNotification extends ToolkitNotification { |
| 23 | + private final Consumer<Boolean> checkboxCallback; |
| 24 | + |
| 25 | + public PersistentToolkitNotification(Display display, String title, String description, Consumer<Boolean> checkboxCallback) { |
| 26 | + super(display, title, description); |
| 27 | + this.checkboxCallback = checkboxCallback; |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + protected void createContentArea(final Composite parent) { |
| 32 | + super.createContentArea(parent); |
| 33 | + |
| 34 | + Composite container = (Composite) parent.getChildren()[0]; |
| 35 | + |
| 36 | + // create checkbox |
| 37 | + Button doNotShowCheckbox = new Button(container, SWT.CHECK); |
| 38 | + GridData checkboxGridData = new GridData(SWT.BEGINNING, SWT.CENTER, false, false); |
| 39 | + doNotShowCheckbox.setLayoutData(checkboxGridData); |
| 40 | + |
| 41 | + // create checkbox button text |
| 42 | + Label checkboxLabel = new Label(container, SWT.NONE); |
| 43 | + checkboxLabel.setText("Don't show this message again"); |
| 44 | + checkboxLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); |
| 45 | + |
| 46 | + // style button text |
| 47 | + Color grayColor = new Color(parent.getDisplay(), 128, 128, 128); |
| 48 | + checkboxLabel.setForeground(grayColor); |
| 49 | + |
| 50 | + Font originalFont = checkboxLabel.getFont(); |
| 51 | + FontData[] fontData = originalFont.getFontData(); |
| 52 | + for (FontData fd : fontData) { |
| 53 | + fd.setHeight(fd.getHeight() - 1); |
| 54 | + } |
| 55 | + Font smallerFont = new Font(parent.getDisplay(), fontData); |
| 56 | + checkboxLabel.setFont(smallerFont); |
| 57 | + |
| 58 | + checkboxLabel.addDisposeListener(e -> { |
| 59 | + smallerFont.dispose(); |
| 60 | + grayColor.dispose(); |
| 61 | + }); |
| 62 | + |
| 63 | + // make button text clickable |
| 64 | + checkboxLabel.addMouseListener(new MouseAdapter() { |
| 65 | + @Override |
| 66 | + public void mouseUp(MouseEvent e) { |
| 67 | + doNotShowCheckbox.setSelection(!doNotShowCheckbox.getSelection()); |
| 68 | + if (checkboxCallback != null) { |
| 69 | + checkboxCallback.accept(doNotShowCheckbox.getSelection()); |
| 70 | + } |
| 71 | + } |
| 72 | + }); |
| 73 | + |
| 74 | + doNotShowCheckbox.addSelectionListener(new SelectionAdapter() { |
| 75 | + @Override |
| 76 | + public void widgetSelected(SelectionEvent e) { |
| 77 | + if (checkboxCallback != null) { |
| 78 | + checkboxCallback.accept(doNotShowCheckbox.getSelection()); |
| 79 | + } |
| 80 | + } |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | +} |
0 commit comments