diff --git a/examples/hello_triangle.ts b/examples/hello_triangle.ts index f2004c8..2b2d6e7 100644 --- a/examples/hello_triangle.ts +++ b/examples/hello_triangle.ts @@ -15,6 +15,7 @@ fn fs_main() -> @location(0) vec4 { `; const windowTitle = "Hello Triangle"; +const windowIconPath = Deno.realPathSync("assets/icon.png"); const presentationFormat = "bgra8unorm"; let renderPipeline: GPURenderPipeline | null = null; @@ -75,6 +76,7 @@ const resizeFunction = (width: number, height: number) => { const window = new WinitWindow({ windowTitle, + windowIconPath, presentationFormat, setupFunction, drawFunction, diff --git a/mod.ts b/mod.ts index 5cde5be..24d1052 100644 --- a/mod.ts +++ b/mod.ts @@ -36,7 +36,7 @@ export class WinitWindow { private dylibPromise: Promise; private system: "win32" | "cocoa" | "wayland" | "x11" | null = null; private windowTitle: string = "Deno + winit"; - private windowIconPath: string = Deno.realPathSync("assets/icon.png"); + private windowIconPath: string | null = null; private width: number = 512; private height: number = 512; private presentationFormat: GPUTextureFormat = "bgra8unorm"; @@ -53,7 +53,7 @@ export class WinitWindow { * The winit window constructor. * @param forceX11 Whether to force X11 on Linux. * @param windowTitle The window title. - * @param windowIconPath The window icon path. + * @param windowIconPath The window icon absolute path. Only supported on Windows and X11. * @param width The window width in pixels. * @param height The window height in pixels. * @param presentationFormat The presentation format. @@ -262,7 +262,7 @@ export class WinitWindow { const dylib = await this.dylibPromise; dylib.symbols.spawn_window( asCString(this.windowTitle), - asCString(this.windowIconPath), + this.windowIconPath ? asCString(this.windowIconPath) : null, this.width, this.height, setupFunctionFfiCallback.pointer, diff --git a/src/lib.rs b/src/lib.rs index de98596..2e5527e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,18 +28,6 @@ pub extern "C" fn spawn_window( draw_func: extern "C" fn(), resize_func: extern "C" fn(width: u32, height: u32), ) { - // Load window icon from path - let window_icon_path: String = unsafe { - String::from_utf8( - CStr::from_ptr(window_icon_path as *const c_char) - .to_bytes() - .to_vec(), - ) - .expect("Failed to convert window icon path to string") - }; - let window_icon_path: &Path = Path::new(window_icon_path.as_str()); - let window_icon = load_icon(window_icon_path); - let event_loop = EventLoop::new().unwrap(); let window = WindowBuilder::new() @@ -52,10 +40,25 @@ pub extern "C" fn spawn_window( .expect("Failed to convert window title to string") }) .with_inner_size(Size::Physical(PhysicalSize::new(width, height))) - .with_window_icon(Some(window_icon)) .build(&event_loop) .unwrap(); + // Load window icon if provided + if !window_icon_path.is_null() { + let window_icon_path: String = unsafe { + String::from_utf8( + CStr::from_ptr(window_icon_path as *const c_char) + .to_bytes() + .to_vec(), + ) + .expect("Failed to convert window icon path to string") + }; + let window_icon_path: &Path = Path::new(window_icon_path.as_str()); + let window_icon = load_icon(window_icon_path); + + window.set_window_icon(Some(window_icon)); + } + match window.raw_window_handle() { raw_window_handle::RawWindowHandle::Win32(handle) => setup_func( handle.hwnd,