Rust-Rust sciter: rust-sciter — Sciter bindings

Rust bindings for Sciter

Build status Build Status Minimum supported Rust version Current Version Documentation License Join the forums at https://sciter.com/forums

Check this page for other language bindings (Delphi / D / Go / .NET / Python / Rust).


Introduction

Sciter is an embeddable multiplatform HTML/CSS/script engine with GPU accelerated rendering designed to render modern desktop application UI. It's a compact, single dll/dylib/so file (4-8 mb) engine without any additional dependencies.

Screenshots

Check the screenshot gallery of desktop UI examples and DirectX UI integration via Rust GFX.

Description

Physically Sciter is a mono library which contains:

  • HTML and CSS rendering engine based on the H-SMILE core used in HTMLayout,
  • JavaScript alike Scripting engine – core of TIScript which by itself is based on c-smile engine,
  • Persistent Database (a.k.a. JSON DB) based on excellent DB products of Konstantin Knizhnik.
  • Graphics module that uses native graphics primitives provided by supported platforms: Direct2D on Windows 7 and above, GDI+ on Windows XP, CoreGraphics on MacOS, Cairo on Linux/GTK. Yet there is an option to use built-in Skia/OpenGL backend on each platform.
  • Network communication module, it relies on platform HTTP client primitives and/or Libcurl.

Internally it contains the following modules:

  • CSS – CSS parser and the collection of parsed CSS rules, etc.
  • HTML DOM – HTML parser and DOM tree implementation.
  • layout managers – collection of various layout managers – text layout, default block layout, flex layouts. Support of positioned floating elements is also here. This module does the layout calculations heavy lifting. This module is also responsible for the rendering of layouts.
  • input behaviors – a collection of built-in behaviors – code behind "active" DOM elements: <input>, <select>, <textarea>, etc.
  • script module – source-to-bytecode compiler and virtual machine (VM) with compacting garbage collector (GC). This module also contains runtime implementation of standard classes and objects: Array, Object, Function and others.
  • script DOM – runtime classes that expose DOM and DOM view (a.k.a. window) to the script.
  • graphics abstraction layer – abstract graphics implementation that isolates the modules mentioned above from the particular platform details:
    • Direct2D/DirectWrite graphics backend (Windows);
    • GDI+ graphics backend (Windows);
    • CoreGraphics backend (Mac OS X);
    • Cairo backend (GTK on all Linux platforms);
    • Skia/OpenGL backend (all platforms)
  • core primitives – set of common primitives: string, arrays, hash maps and so on.

Sciter supports all standard elements defined in HTML5 specification with some additions. CSS is extended to better support the Desktop UI development, e.g. flow and flex units, vertical and horizontal alignment, OS theming.

Sciter SDK comes with a demo "browser" with a builtin DOM inspector, script debugger and documentation viewer:

Sciter tools

Check https://sciter.com website and its documentation resources for engine principles, architecture and more.

Getting started:

  1. Download the Sciter SDK and extract it somewhere.
  2. Add the corresponding target platform binaries to PATH (bin.win, bin.osx or bin.lnx).
  3. If you do not already have it installed, you need GTK 3 development tools installed to continue: sudo apt-get install libgtk-3-dev
  4. Build the crate and run a minimal sciter sample: cargo run --example minimal.
  5. For your apps add the following dependency to the Cargo.toml: sciter-rs = "*".

Brief look:

Here is a minimal sciter app:

extern crate sciter;

fn main() {
    let mut frame = sciter::Window::new();
    frame.load_file("minimal.htm");
    frame.run_app();
}

It looks similar to this:

Minimal sciter sample

Interoperability

In respect of tiscript functions calling:

use sciter::{Element, Value};

let root = Element::from_window(hwnd);
let result: Value = root.call_function("namespace.name", &make_args!(1,"2",3));

Calling Rust from script can be implemented as following:

struct Handler;

impl Handler {
  fn calc_sum(&self, a: i32, b: i32) -> i32 {
    a + b
  }
}

impl sciter::EventHandler for Handler {
  dispatch_script_call! {
    fn calc_sum(i32, i32);
  }
}

And we can access this function from script:

// `view` represents window where script is runnung.
// `stdout` stream is a standard output stream (shell or debugger console, for example)

stdout.printf("2 + 3 = %d\n", view.calc_sum(2, 3));

Check rust-sciter/examples folder for more complex usage.

Library documentation.

What is supported right now:

Platforms:

  • Windows
  • OSX
  • Linux
  • Raspberry Pi

License

Bindings library licensed under MIT license. Sciter Engine has the own license terms and end used license agreement for SDK usage.

Comments

  • Segfault involving on_data_load and anchor in htm document
    Segfault involving on_data_load and anchor in htm document

    Apr 22, 2019

    This segfault can be easily reproduced by unzipping the minimal project I'm attaching, and typing cargo run.

    This minimal application will display

    <html>
      <head>
        <title>Segfault when loading main.css and an anchor is present</title>
        <style src="main.css" />
      </head>
    <body>
      <h1>Minimal Sciter Application</h1>
      <p><a href="any_url">Sciter SDK</a></p>
    </body>
    </html>
    

    and the main.css file is send to Sciter using on_data_load in the main.rs file.

    Oddly enough:

    • when both
      <style src="main.css" />
      ...
      <a href="any_url">Sciter SDK</a>
      

      lines are present, the segfault occurs ; but

    • when either one is present, the program runs fine.

    segfault.zip

    bug 
    Reply
  • sciter::Window::attach - how to call?
    sciter::Window::attach - how to call?

    Aug 29, 2019

    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!

    Reply
  • what is the possible causes for memory leak
    what is the possible causes for memory leak

    Nov 1, 2019

    Hi there, I get some data from a specific api, pass them as string to sciter, and update these data in sciter tables. In EventHandler, I write a function for doing this in a loop mode. But memory leak occurred, memory usage would increase by about 0.1mb/sec. Is this normal? If not, any idea how to fix it? Thanks a lot.

    Reply
  • windows blurbehind effect isn't working on windows 10
    windows blurbehind effect isn't working on windows 10

    Nov 25, 2019

    windows blurbehind effect isn't working on windows 10. I'm trying add blur effect to my application windows, but itn't display it. image

    Reply
  • Dom example doesn't work : cannot load html-content from http://httpbin.org/html
    Dom example doesn't work : cannot load html-content from http://httpbin.org/html

    Nov 28, 2019

    Hello !

    I've just build DOM example (https://github.com/sciter-sdk/rust-sciter/blob/master/examples/dom.rs) and run it, but all i see is blank white window.

    I also tried to change from load_file to load_html to load site from memory( i've coppied html-code of site to minimal.htm) :

    let html = include_bytes!("minimal.htm");
    frame.load_html(html, Some("example://minimal.htm")); 
    

    And it worked fine. So it seems like rust-sciter cannot load internet web page. Site is available, PC has internet connection. OS : ArchLinux

    I also tried to run as root but no success.

    domexample

    Reply
  • SOM support
    SOM support

    May 27, 2020

    Supported:

    • [x] C interface
    • [x] Rust wrapper
    • [x] Global assets
    • [x] Event handler assets - blocked by https://sciter.com/forums/topic/wsciter-4-4-3-19-crash-in-behavior_tabs-cpp/
    • [ ] auto-generation based on object declaration

    Unresolved questions:

    1. auto-generation: based on syntax macros (quick and dirty) and derive-macros.

    Syntax macros = just specify a relation between the data+methods and the passport.

    Derive macros - properties are easy, but how to collect methods?

    enhancement help wanted 
    Reply
  • Enable debug mode for example
    Enable debug mode for example "minimal"

    Feb 12, 2019

    Debug mode is required to connect to the inspector.

    os:osx 
    Reply
  • Can't set `UxTheming(true)`
    Can't set `UxTheming(true)`

    May 6, 2019

    This line in my code panics: sciter::set_options(sciter::RuntimeOptions::UxTheming(true)).unwrap(); It panics with the following message: thread 'main' panicked at 'calledResult::unwrap()on anErrvalue: ()', src/libcore/result.rs:999:5

    I see the following code is related to this error:

    /// Set various sciter engine global options, see the [`RuntimeOptions`](enum.RuntimeOptions.html).
    pub fn set_options(options: RuntimeOptions) -> std::result::Result<(), ()> {
    	use RuntimeOptions::*;
    	use capi::scdef::SCITER_RT_OPTIONS::*;
    	let (option, value) = match options {
    		ConnectionTimeout(ms) => (SCITER_CONNECTION_TIMEOUT, ms as usize),
    		OnHttpsError(behavior) => (SCITER_HTTPS_ERROR, behavior as usize),
    		// GpuBlacklist(json) => (SCITER_SET_GPU_BLACKLIST, json.as_bytes().as_ptr() as usize),
    		InitScript(script) => (SCITER_SET_INIT_SCRIPT, script.as_bytes().as_ptr() as usize),
    		ScriptFeatures(mask) => (SCITER_SET_SCRIPT_RUNTIME_FEATURES, mask as usize),
    		GfxLayer(backend) => (SCITER_SET_GFX_LAYER, backend as usize),
    		DebugMode(enable) => (SCITER_SET_DEBUG_MODE, enable as usize),
    		UxTheming(enable) => (SCITER_SET_UX_THEMING, enable as usize),
        LibraryPath(path) => {
          return set_library(path).map_err(|_|());
        }
    	};
    	let ok = (_API.SciterSetOption)(std::ptr::null_mut(), option, value);
    	if ok != 0 {
    		Ok(())
    	} else {
    		Err(())
    	}
    }
    

    The value of ok ends up being 0, and so the Err(()) is returned. I'm not sure where to look for documentation on the Sciter C++ API, but the set option call is returning 0, what does that mean?

    The error occurs on Linux (GTK / Wayland), but not on Windows.

    bug os:linux 
    Reply
  • I can't connect to the inspector anymore
    I can't connect to the inspector anymore

    Apr 25, 2019

    I recently updated my sciter-sdk and I am unable to connect to the inspector. My code is unchanged. I was previously able to connect to the inspector and now I am not able to. Unfortunately, I didn't take note of what version of the SDK I had previously, but it was a few months old I think. EDIT: I should add that I am using Sciter version 4.2.7.5 on Linux-5.0.5-200.fc29.x86_64

    Reply
  • [osx] Unable to start examples
    [osx] Unable to start examples

    Apr 8, 2018

    When I tried to launch examples(multiple), I got the following error messages.

    $ cargo run --example threads
        Finished dev [unoptimized + debuginfo] target(s) in 0.3 secs
         Running `target/debug/examples/threads`
    window::create(rect (0, 0, 1200, 900), flags 96, parent 0x0
    window::create with flags 96
    view 0x7f9722432bb0
    view 0x7f9722432bb0 -> window 0x7f9722455140
    fatal runtime error: allocator memory exhausted
    [1]    981 illegal hardware instruction  cargo run --example threads
    

    I have no idea why. I just wanted to learn this library using rust. I'm using macOS High Sierra 10.13.4, rustc 1.26.0-nightly (e5277c145 2018-03-28) and fresh sciter-sdk.

    bug os:osx 
    Reply
  • Implementation of fire_event
    Implementation of fire_event

    Oct 13, 2016

    Hi, @pravic. When are you gonna implement fire_event function for Element?

    enhancement help wanted 
    Reply
  • compile error on macosx
    compile error on macosx

    Mar 10, 2017

    cargo run --example minimal just output error message:

    warning: unused manifest key: badges.appveyor.repository
    warning: unused manifest key: package.categories
    warning: unused manifest key: package.categories
    warning: unused manifest key: package.categories
    warning: unused manifest key: package.categories
       Compiling sciter-rs v0.4.16 (file:///Volumes/src/git/rust/rust-sciter)
    error: linking with `cc` failed: exit code: 1
      |
      = note: "cc" "-m64" "-L" "/Users/tony/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "/Volumes/src/git/rust/rust-sciter/target/debug/examples/minimal-049750bad57ec635.0.o" "-o" "/Volumes/src/git/rust/rust-sciter/target/debug/examples/minimal-049750bad57ec635" "-Wl,-dead_strip" "-nodefaultlibs" "-L" "/Volumes/src/git/rust/rust-sciter/target/debug/deps" "-L" "/Users/tony/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "/Volumes/src/git/rust/rust-sciter/target/debug/deps/libsciter-e229de1bcb7d347a.rlib" "/Volumes/src/git/rust/rust-sciter/target/debug/deps/liblazy_static-4345d0db7d7e9b05.rlib" "/Volumes/src/git/rust/rust-sciter/target/debug/deps/libobjc_foundation-cd7bec3c0f31e91a.rlib" "/Volumes/src/git/rust/rust-sciter/target/debug/deps/libblock-81bb5816adb186e4.rlib" "/Volumes/src/git/rust/rust-sciter/target/debug/deps/libobjc_id-f01a1943dfb75bb9.rlib" "/Volumes/src/git/rust/rust-sciter/target/debug/deps/libobjc-2834ac60b01b7699.rlib" "/Volumes/src/git/rust/rust-sciter/target/debug/deps/libmalloc_buf-826d6968858cdff6.rlib" "/Volumes/src/git/rust/rust-sciter/target/debug/deps/liblibc-6451aa7d8103c93e.rlib" "/Users/tony/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libstd-f1544d51c14ee547.rlib" "/Users/tony/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libpanic_unwind-0973ad751bdffbae.rlib" "/Users/tony/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libunwind-30637a1739b412eb.rlib" "/Users/tony/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librand-6ce8560490ee791c.rlib" "/Users/tony/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcollections-77c40ab2fac1172e.rlib" "/Users/tony/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/liballoc-40208fb59386bff5.rlib" "/Users/tony/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/liballoc_jemalloc-4b56f5c0b7251555.rlib" "/Users/tony/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/liblibc-cba64299ce12485f.rlib" "/Users/tony/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libstd_unicode-a98ebaa82aaee358.rlib" "/Users/tony/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcore-cfc94a4f91ad8df0.rlib" "/Users/tony/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcompiler_builtins-51cf9867f46a760f.rlib" "-l" "sciter-osx-64" "-framework" "Foundation" "-l" "System" "-l" "objc" "-l" "objc" "-l" "objc" "-l" "System" "-l" "pthread" "-l" "c" "-l" "m"
      = note: ld: library not found for -lsciter-osx-64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    
    
    error: aborting due to previous error
    
    error: Could not compile `sciter-rs`.
    
    To learn more, run the command again with --verbose.
    
    bug help wanted os:osx 
    Reply