Skip to content

Commit

Permalink
Feature: window opacity (HumbleUI#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
i10416 authored Aug 25, 2021
1 parent d1c0704 commit a76550c
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 1 deletion.
2 changes: 2 additions & 0 deletions examples/java/org/jetbrains/jwm/examples/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,12 @@ public Example() {
case 4 -> window.setWindowPosition(bounds.getLeft() + bounds.getWidth() / 2, bounds.getTop() + bounds.getHeight() / 2);
}
window.setTitle("JWM Window #" + count);

switch (Platform.CURRENT) {
case WINDOWS -> window.setIcon(new File("examples/resources/windows.ico"));
case MACOS -> window.setIcon(new File("examples/resources/macos.icns"));
}
window.setOpacity(0.8f);
window.setMouseCursor(MouseCursor.ARROW);
window.show();
window.requestFrame();
Expand Down
6 changes: 6 additions & 0 deletions linux/java/org/jetbrains/jwm/WindowX11.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ public Window setIcon(File icon) {
return this;
}

@Override
public Window setOpacity(float opacity) {
// TODO: impl me!
return this;
}

@Override
public Screen getScreen() {
assert _onUIThread();
Expand Down
6 changes: 6 additions & 0 deletions macos/java/org/jetbrains/jwm/WindowMac.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ public Window setIcon(File icon) {
_nSetIcon(icon.getAbsolutePath().toString());
return this;
}

@Override
public Window setOpacity(float opacity) {
// TODO: impl me!
return this;
}

public Window setMouseCursor(MouseCursor cursor) {
assert _onUIThread();
Expand Down
7 changes: 7 additions & 0 deletions shared/java/org/jetbrains/jwm/Window.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ public UIRect getContentRectAbsolute() {
@NotNull @Contract("-> this")
public abstract Window setIcon(File icon);
public abstract Window setMouseCursor(MouseCursor cursor);

/**
* Sets window opacity (0.0 - 1.0)
* If the opacity is outside the range, it will be rounded to 0.0 to 1.0.
*/
@NotNull @Contract("-> this")
public abstract Window setOpacity(float opacity);

public abstract Screen getScreen();

Expand Down
4 changes: 3 additions & 1 deletion windows/cc/PlatformWin32.hh
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#pragma once

#include <cwctype>
#include <windows.h>
#include <winuser.h>
Expand All @@ -11,6 +10,9 @@
#ifdef max
#undef max
#endif
#ifdef min
#undef min
#endif

#ifndef JWM_WIN32_WINDOW_CLASS_NAME
#define JWM_WIN32_WINDOW_CLASS_NAME L"JWM_WINDOW"
Expand Down
25 changes: 25 additions & 0 deletions windows/cc/WindowWin32.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <algorithm>
#include <PlatformWin32.hh>
#include <AppWin32.hh>
#include <WindowWin32.hh>
Expand Down Expand Up @@ -74,6 +75,25 @@ void jwm::WindowWin32::setIcon(const std::wstring& iconPath) {
HICON hicon = (HICON)LoadImage(NULL, iconPath.c_str(), IMAGE_ICON, 0, 0, LR_LOADFROMFILE | LR_DEFAULTSIZE);
SendMessage(_hWnd, WM_SETICON, ICON_SMALL, (LPARAM)hicon);
}

void jwm::WindowWin32::setOpacity(float opacity) {
JWM_VERBOSE("Set window opacity'" << opacity << "'");
LONG_PTR previousStyle = GetWindowLongPtr(_hWnd, GWL_EXSTYLE);
float lower = 0.0;
float upper = 1.0;
float clamped = std::max(lower, std::min(opacity, upper));
if (clamped == 1.0)
{
LONG reset = previousStyle & ~WS_EX_LAYERED;
SetWindowLongPtr(_hWnd, GWL_EXSTYLE, reset);
return;
}
LONG_PTR opacityEnabled = previousStyle |= WS_EX_LAYERED;
SetWindowLongPtr(_hWnd, GWL_EXSTYLE, opacityEnabled);
int adjusted = static_cast<int>(round(clamped * 255));
SetLayeredWindowAttributes(_hWnd, RGB(0, 0, 0), adjusted, LWA_ALPHA);
}

void jwm::WindowWin32::setMouseCursor(MouseCursor cursor) {
JWM_VERBOSE("Set window cursor '" << mouseCursorToStr(cursor) << "'");

Expand Down Expand Up @@ -872,6 +892,11 @@ extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_jwm_WindowWin32__1nSetIcon
instance->setIcon(std::wstring(reinterpret_cast<const wchar_t*>(iconPathStr), length));
env->ReleaseStringChars(iconPath, iconPathStr);
}
extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_jwm_WindowWin32__1nSetOpacity
(JNIEnv* env, jobject obj,float opacity) {
jwm::WindowWin32* instance = reinterpret_cast<jwm::WindowWin32*>(jwm::classes::Native::fromJava(env, obj));
instance->setOpacity(opacity);
}
extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_jwm_WindowWin32__1nSetMouseCursor
(JNIEnv* env, jobject obj, jint cursorId) {
jwm::WindowWin32* instance = reinterpret_cast<jwm::WindowWin32*>(jwm::classes::Native::fromJava(env, obj));
Expand Down
1 change: 1 addition & 0 deletions windows/cc/WindowWin32.hh
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ namespace jwm {
void setImeEnabled(bool enabled);
void setTitle(const std::wstring& title);
void setIcon(const std::wstring& iconPath);
void setOpacity(float opacity);
void setMouseCursor(MouseCursor cursor);
void show();
void requestSwap();
Expand Down
8 changes: 8 additions & 0 deletions windows/java/org/jetbrains/jwm/WindowWin32.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ public Window setIcon(File icon){
_nSetIcon(icon.getAbsolutePath().toString());
return this;
}

@Override
public Window setOpacity(float opacity) {
assert _onUIThread();
_nSetOpacity(opacity);
return this;
}

@Override
public Window setMouseCursor(MouseCursor cursor) {
Expand Down Expand Up @@ -133,6 +140,7 @@ public void close() {
@ApiStatus.Internal public native void _nSetContentSize(int width, int height);
@ApiStatus.Internal public native void _nSetTitle(String title);
@ApiStatus.Internal public native void _nSetIcon(String iconPath);
@ApiStatus.Internal public native void _nSetOpacity(float opacity);
@ApiStatus.Internal public native void _nSetMouseCursor(int cursorId);
@ApiStatus.Internal public native Screen _nGetScreen();
@ApiStatus.Internal public native void _nRequestFrame();
Expand Down

0 comments on commit a76550c

Please sign in to comment.