From 886105867725df8c49e47cf337a332b5395625eb Mon Sep 17 00:00:00 2001 From: Jamling Date: Tue, 10 Apr 2018 16:05:44 +0800 Subject: [PATCH] opt linux platform notification --- .../smartim/common/Notifications.java | 66 +++++++++++++++---- 1 file changed, 54 insertions(+), 12 deletions(-) diff --git a/cn.ieclipse.smartqq/src/cn/ieclipse/smartim/common/Notifications.java b/cn.ieclipse.smartqq/src/cn/ieclipse/smartim/common/Notifications.java index a46cf01..f85de2c 100644 --- a/cn.ieclipse.smartqq/src/cn/ieclipse/smartim/common/Notifications.java +++ b/cn.ieclipse.smartqq/src/cn/ieclipse/smartim/common/Notifications.java @@ -1,10 +1,17 @@ package cn.ieclipse.smartim.common; +import java.awt.Insets; +import java.awt.Toolkit; import java.lang.reflect.Method; import java.util.Timer; import java.util.TimerTask; +import javax.swing.JFrame; + +import org.eclipse.core.runtime.Platform; import org.eclipse.swt.SWT; +import org.eclipse.swt.events.MouseAdapter; +import org.eclipse.swt.events.MouseEvent; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.graphics.Rectangle; @@ -33,34 +40,45 @@ public class Notifications extends Shell { public static void main(String args[]) { try { final Display display = Display.getDefault(); - Notifications.getInstance(new Shell(display)).setMock(true) - .setMessage(null, "test"); - IMPlugin.runOnUI(new Runnable() { - - @Override - public void run() { - System.out.println("UI Thread"); +// Notifications.getInstance(new Shell(display)).setMock(true) +// .setMessage(null, "test"); +// Notifications.notify("title", "message"); + Notifications instance = new Notifications(display); + instance.open(); + instance.layout(); + instance.setLocation(100, 100); + instance.setVisible(true); + instance.setMessage("title", "message"); + while (!instance.isDisposed()) { + if (!display.readAndDispatch()) { + display.sleep(); } - }); - if (!display.readAndDispatch()) { - display.sleep(); } } catch (Exception e) { e.printStackTrace(); } } + private Label fIcon; + private Label fTitle; private Label fText; private TranslateAnimation ta; private Timer timer; + /** + * @wbp.parser.constructor + */ + public Notifications(Display display) { + this(display, SWT.ON_TOP | SWT.CLOSE | SWT.TITLE); + } + /** * Create the shell. * * @param display */ - public Notifications(Display display) { - super(display, SWT.ON_TOP | SWT.CLOSE | SWT.TITLE); + public Notifications(Display display, int style) { + super(display, style); try { Class clazz = Class.forName("org.eclipse.swt.internal.win32.OS"); if (clazz != null) { @@ -89,6 +107,24 @@ protected void createContents() { setSize(300, 150); setLayout(new GridLayout(2, false)); + if (Platform.OS_LINUX.equals(Platform.getOS())) + { + fIcon = new Label(this, SWT.NONE); + fIcon.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1)); + fIcon.setImage(SWTResourceManager.getImage(org.eclipse.ui.IWorkbench.class, + "/icons/full/etool16/delete_edit.png")); + fIcon.addMouseListener(new MouseAdapter() { + @Override + public void mouseDown(MouseEvent e) { + setVisible(false); + } + }); + + fTitle = new Label(this, SWT.NONE); + fTitle.setText("通知"); + fTitle.setLayoutData( + new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1)); + } fText = new Label(this, SWT.WRAP); // fText.setEditable(false); @@ -121,7 +157,10 @@ public void widgetSelected(SelectionEvent e) { } }); + Insets sd = Toolkit.getDefaultToolkit().getScreenInsets( + new JFrame().getGraphicsConfiguration()); Rectangle screen = Display.getDefault().getClientArea(); + screen.height = screen.height - sd.bottom; ta = new TranslateAnimation(screen.width, screen.width - getSize().x, screen.height, screen.height - getSize().y).setTarget(this) .setDuration(300); @@ -179,6 +218,9 @@ private void doSetMessage(String title, CharSequence text) { } if (title != null) { this.setText(title); + if (fTitle != null) { + fTitle.setText(title); + } } this.fText.setText(text == null ? "" : text.toString()); }