rust-sciter icon indicating copy to clipboard operation
rust-sciter copied to clipboard

sciter::Window::attach - how to call?

Open Alovchin91 opened this issue 5 years ago • 5 comments

Hi,

I'm trying out Sciter as a UI engine for my Rust app that runs on macOS.

I need a system menu, so I use cocoa crate to create a NSApplication and a NSWindow.

Unfortunately, I can't make sciter::Window::attach to work: it crashes every time I call it.

The code is pretty much as follows (menu creation is omitted):

use sciter;

use cocoa::base::nil;
use cocoa::foundation::NSAutoreleasePool;
use cocoa::appkit::{NSApplication, NSWindow};

fn main() {
    unsafe {
        let _pool = NSAutoreleasePool::new(nil);
    }

    let app = create_app();
    let window = add_window();

    let mut frame = sciter::Window::attach(window);
    frame.load_html(br#"<html><body>Hello world!</body></html>"#, Some("x:main"));

    unsafe {
        app.run();
    }
}

fn create_app() -> cocoa::base::id {
    unsafe {
        let app = cocoa::appkit::NSApp();
        app.setActivationPolicy_(cocoa::appkit::NSApplicationActivationPolicyRegular);
        app
    }
}

fn add_window() -> sciter::types::HWINDOW {
    unsafe {
        let window = NSWindow::alloc(nil).initWithContentRect_styleMask_backing_defer_(
            cocoa::foundation::NSRect::new(cocoa::foundation::NSPoint::new(0., 0.), cocoa::foundation::NSSize::new(300., 200.)),
            cocoa::appkit::NSWindowStyleMask::NSTitledWindowMask,
            cocoa::appkit::NSBackingStoreBuffered,
            cocoa::base::NO
        ).autorelease();
        window.center();
        window.makeKeyAndOrderFront_(nil);
        window.contentView() as _
    }
}

It doesn't matter if I try to pass window itself or window.contentView() - it still crashes on sciter::Window::attach line with the following message:

2019-08-29 16:03:10.938 sciter-app[28447:3486458] -[NSView engine]: unrecognized selector sent to instance 0x7ff96f830190
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', src/libcore/option.rs:347:21

Could you please help me to make it work? Thanks!

Alovchin91 avatar Aug 29 '19 14:08 Alovchin91

The crash happens on the following line:

https://github.com/sciter-sdk/rust-sciter/blob/ef93434c1ba684b2f9a64c9da5d70c3595971353/src/host.rs#L203

Alovchin91 avatar Aug 30 '19 09:08 Alovchin91

Interesting. So, that line works for a created window, but doesn't for a attached one?

In general, I need to test the attaching because I've checked on Windows only.

pravic avatar Sep 01 '19 06:09 pravic

Relatedly, on x11 I get a segmentation fault when trying to render to another window.

Programs as simple as:

      let ptr = 0x1e6ad8c as HWINDOW; // existing Window ID obtained via running xwininfo
      let frame = sciter::Window::attach(ptr);
      frame.run_app();

will segfault when run.

DanielHeath avatar Nov 29 '20 06:11 DanielHeath

@DanielHeath HWINDOW is a GtkWidget* on Linux. I suppose, xwininfo returns something different.

pravic avatar Nov 29 '20 08:11 pravic

xwininfo returns an X window handle.

GtkWidget is a confusing type for that value given that

A) GtkWindow exists, and B) it's named as a handle

DanielHeath avatar Nov 30 '20 01:11 DanielHeath