Skip to content

Commit

Permalink
add setIcon function for Windows (HumbleUI#95 HumbleUI#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
i10416 authored Aug 24, 2021
1 parent 2e145d7 commit 87a9967
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/java/org/jetbrains/jwm/examples/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.util.concurrent.*;
import java.util.function.*;
import java.util.stream.*;
import java.io.File;


public class Example implements Consumer<Event> {
public static int PADDING = 10;
Expand Down Expand Up @@ -66,6 +68,7 @@ public Example() {
case 4 -> window.setWindowPosition(bounds.getLeft() + bounds.getWidth() / 2, bounds.getTop() + bounds.getHeight() / 2);
}
window.setTitle("JWM Window #" + count);
window.setIcon(new File("sample.ico"));
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 @@ -82,6 +82,12 @@ public Window setMouseCursor(MouseCursor cursor) {
return this;
}

@Override
public Window setIcon(File icon) {
// TODO #95
return this;
}

@Override
public Screen getScreen() {
assert _onUIThread();
Expand Down
5 changes: 5 additions & 0 deletions macos/java/org/jetbrains/jwm/WindowMac.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ public Window setTitle(String title) {
}

@Override
public Window setIcon(File icon) {
// TODO #95
return this;
}

public Window setMouseCursor(MouseCursor cursor) {
assert _onUIThread();
// TODO: impl me!
Expand Down
Binary file added sample.ico
Binary file not shown.
2 changes: 2 additions & 0 deletions shared/java/org/jetbrains/jwm/Window.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.function.*;
import org.jetbrains.annotations.*;
import org.jetbrains.jwm.impl.*;
import java.io.File;

public abstract class Window extends RefCounted {
@ApiStatus.Internal
Expand Down Expand Up @@ -54,6 +55,7 @@ public UIRect getContentRectAbsolute() {
public abstract Window setTitle(String title);

@NotNull @Contract("-> this")
public abstract Window setIcon(File icon);
public abstract Window setMouseCursor(MouseCursor cursor);

public abstract Screen getScreen();
Expand Down
14 changes: 14 additions & 0 deletions windows/cc/WindowWin32.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ void jwm::WindowWin32::setTitle(const std::wstring& title) {
SetWindowTextW(_hWnd, title.c_str());
}

void jwm::WindowWin32::setIcon(const std::wstring& iconPath) {
JWM_VERBOSE("Set window icon '" << iconPath << "'");
// width / height of 0 along with LR_DEFAULTSIZE tells windows to load the default icon size.
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::setMouseCursor(MouseCursor cursor) {
JWM_VERBOSE("Set window cursor '" << mouseCursorToStr(cursor) << "'");

Expand Down Expand Up @@ -842,6 +848,14 @@ extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_jwm_WindowWin32__1nSetTitle
env->ReleaseStringChars(title, titleStr);
}

extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_jwm_WindowWin32__1nSetIcon
(JNIEnv* env, jobject obj, jstring iconPath) {
jwm::WindowWin32* instance = reinterpret_cast<jwm::WindowWin32*>(jwm::classes::Native::fromJava(env, obj));
const jchar* iconPathStr = env->GetStringChars(iconPath, nullptr);
jsize length = env->GetStringLength(iconPath);
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__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 @@ -51,6 +51,7 @@ namespace jwm {
void unmarkText();
void setImeEnabled(bool enabled);
void setTitle(const std::wstring& title);
void setIcon(const std::wstring& iconPath);
void setMouseCursor(MouseCursor cursor);
void show();
void requestSwap();
Expand Down
10 changes: 10 additions & 0 deletions windows/java/org/jetbrains/jwm/WindowWin32.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.jetbrains.annotations.*;
import org.jetbrains.jwm.*;
import org.jetbrains.jwm.impl.*;
import java.io.File;

public class WindowWin32 extends Window {
@ApiStatus.Internal
Expand Down Expand Up @@ -72,6 +73,14 @@ public Window setTitle(String title) {
return this;
}


@Override
public Window setIcon(File icon){
assert _onUIThread();
_nSetIcon(icon.getAbsolutePath().toString());
return this;
}

@Override
public Window setMouseCursor(MouseCursor cursor) {
assert _onUIThread();
Expand Down Expand Up @@ -123,6 +132,7 @@ public void close() {
@ApiStatus.Internal public native void _nSetWindowSize(int width, int height);
@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 _nSetMouseCursor(int cursorId);
@ApiStatus.Internal public native Screen _nGetScreen();
@ApiStatus.Internal public native void _nRequestFrame();
Expand Down

0 comments on commit 87a9967

Please sign in to comment.