Skip to content

Commit

Permalink
fix: Crash because of default icon path
Browse files Browse the repository at this point in the history
  • Loading branch information
fazil47 committed Jun 23, 2024
1 parent 8b19809 commit 90cf94b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
2 changes: 2 additions & 0 deletions examples/hello_triangle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ fn fs_main() -> @location(0) vec4<f32> {
`;

const windowTitle = "Hello Triangle";
const windowIconPath = Deno.realPathSync("assets/icon.png");
const presentationFormat = "bgra8unorm";
let renderPipeline: GPURenderPipeline | null = null;

Expand Down Expand Up @@ -75,6 +76,7 @@ const resizeFunction = (width: number, height: number) => {

const window = new WinitWindow({
windowTitle,
windowIconPath,
presentationFormat,
setupFunction,
drawFunction,
Expand Down
6 changes: 3 additions & 3 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class WinitWindow {
private dylibPromise: Promise<winitDylib>;
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";
Expand All @@ -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.
Expand Down Expand Up @@ -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,
Expand Down
29 changes: 16 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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,
Expand Down

0 comments on commit 90cf94b

Please sign in to comment.