Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a Shell to be always on top in SWT?

I'd like to implement "Always on top" configuration option in my application that takes effect immediately.

I know that I can call Shell constructor with ON_TOP style. Is there a way to do that at runtime, that is after a Shell instance has already been created?

like image 296
parxier Avatar asked Jan 24 '26 02:01

parxier


2 Answers

In Cocoa you need to use reflection to get at the Shell instance variable window and then call window.setLevel(OS.NSStatusWindowLevel).

In Carbon you need to get at the shellHandle instance variable and then call OS.SetWindowGroup(shellHandle, OS.kFloatingWindowClass). You might be able to get away with doing just that much, depending on your needs.

In either case, you should also forcibly add the SWT.ON_TOP bit to the style field. Particularly in Carbon, lots of things rely on that bit being set.

like image 71
Scott K. Avatar answered Jan 25 '26 16:01

Scott K.


On windows, you can do it like this:

private static final void toggleAlwaysOnTop(Shell shell, boolean isOnTop){
    long handle = shell.handle;
    Point location = shell.getLocation();
    Point dimension = shell.getSize();
    OS.SetWindowPos(handle, isOnTop ? OS.HWND_TOPMOST : OS.HWND_NOTOPMOST,location.x, location.y, dimension.x, dimension.y, 0);
}

All those api's are public so there is no need for reflection.

The last argument for SetWindowPos is not the same with Shell.getStyle(). Leaving it as 0 currently causing no problem for me.

like image 25
Eddie Deng Avatar answered Jan 25 '26 15:01

Eddie Deng



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!