dioxus/packages/desktop
2021-12-26 14:22:30 -05:00
..
examples wip: rip out unsafe task engine 2021-12-26 14:22:30 -05:00
src examples: upgrade to new version of dioxus core. 2021-12-25 17:18:05 -05:00
ARCHITECTURE.md chore: cleanup workspace 2021-07-15 18:40:12 -04:00
Cargo.toml wip: prepare to change our fragment pattern. Add some more docs 2021-12-18 15:17:32 -05:00
CHANGELOG.md Release dioxus-core v0.1.3, dioxus-core-macro v0.1.2, dioxus-html v0.1.0, dioxus-desktop v0.0.0, dioxus-hooks v0.1.3, dioxus-liveview v0.1.0, dioxus-mobile v0.0.0, dioxus-router v0.1.0, dioxus-ssr v0.1.0, dioxus-web v0.0.0, dioxus v0.1.1 2021-12-15 16:07:09 -05:00
README.md docs: update local examples and docs to support new syntaxes 2021-12-15 15:56:53 -05:00

Dioxus-Desktop

This crate provides an ergonomic API for Dioxus to build desktop apps.

fn main() {
    dioxus::desktop::launch(App)
}

static App: Component<()> = |cx| {
    let (count, set_count) = use_state(&cx, || 0);

    cx.render(rsx!(
        WebviewWindow {
            onclose: move |e| log::info!("save our counter state to disk"),
            div {
                h1 { "Dioxus Desktop Demo" }
                p { "Count is {count}"}
                button { onclick: move |_| count += 1}
            }
        }
    ))
}

Window management, system trays, notifications, and other desktop-related functionality is managed using the declarative Dioxus API, making it easy to add new features without having to jump through hoops.

Features

  • Your rust code runs natively and under a Tokio runtime
  • Declarative application management (dedicated components for windows, models, handlers, task tray, etc)
  • Cross platform (runs on Mac, Linux, Windows, etc and mobile through the dioxus-mobile sub crate)

Managing Windows

Managing windows is done by simply rendering content into a WebviewWindow component.

static App: Component<()> = |cx| {
    rsx!(cx, WebviewWindow { "hello world" } )
}

This will create a new window with only the content "hello world". As this crate matures, we'll have new types of windows for different functionality.

Managing Notifications

Notifications also use a declarative approach. Sending a notification has never been easier!

The api has been somewhat modeled after https://github.com/mikaelbr/node-notifier

static Notifications: Component<()> = |cx| {
    cx.render(rsx!(
        Notification {
            title: "title"
            subtitle: "subtitle"
            message: "message"
            sound: "Basso"
            icon: "Terminal"
            contentImage: "image.png"
            open: "https://github.com"
            wait: true,
            timeout: 5,
            closeLabel: "Cancel"
            actions: ["send", "receive"]
            dropdownLabel: "messaging"
            reply: true

            onclose: move |e| {}
            onreply: move |e| {}
            ondropdownselected: move |e| {}
            ontimeout: move |e| {}
            onerror: move |e| {}
        }
    ))
}

App Tray

Dioxus Desktop supports app trays, which can be built with native menu groups or with a custom window.

static Tray: Component<()> = |cx| {
    cx.render(rsx!(
        GlobalTray {
            MenuGroup {
                MenuGroupItem { title: "New File", shortcut: "cmd+N", onclick: move |e| {} }
                MenuGroupItem { title: "New Window", shortcut: "shift+cmd+N", onclick: move |e| {} }
            }
        }
    ))
};

// using a builder
static Tray: Component<()> = |cx| {
    let menu = MenuGroup::builder(cx)
        .with_items([
            MenuGroupItem::builder()
                .title()
                .shortcut()
                .onclick(move |e| {}),
            MenuGroupItem::builder()
                .title()
                .shortcut()
                .onclick(move |e| {})
        ]).build();

    rsx!(cx, GlobalTray { rawmenu: menu })
}

// or with a custom window
static Tray: Component<()> = |cx| {
    rsx!(cx, GlobalTray { div { "custom buttons here" } })
};

Menu Bar

Declaring menus is convenient and cross-platform.

static Menu: Component<()> = |cx| {
    cx.render(rsx!(
        MenuBarMajorItem { title: "File"
            MenuGroup {
                MenuGroupItem { title: "New File", shortcut: "cmd+N", onclick: move |e| {} }
                MenuGroupItem { title: "New Window", shortcut: "shift+cmd+N", onclick: move |e| {} }
            }            
            MenuGroup {
                MenuGroupList { 
                    title: "Open Recent", shortcut: "cmd+N" 
                    MenuGroup {
                        (recent_items.iter().map(|file| rsx!(
                            MenuGroupItem {
                                onclick: move |_| open_file(file),
                                title: "{file}"
                            }
                        )))
                    }
                }
            }
        }        
    ))
};

Building, bundling, etc

and then to create a native .app:

dioxus bundle --platform macOS

Goals

Because the host VirtualDOM is running in its own native process, native applications can unlock their full potential. Dioxus-Desktop is designed to be a 100% rust alternative to ElectronJS without the memory overhead or bloat of ElectronJS apps.

By bridging the native process, desktop apps can access full multithreading power, peripheral support, hardware access, and native filesystem controls without the hassle of web technologies. Our goal with this desktop crate is to make it easy to ship both a web and native application, and quickly see large performance boosts without having to re-write the whole stack. As the dioxus ecosystem grows, we hope to see 3rd parties providing wrappers for storage, offline mode, etc that supports both web and native technologies.