Skip to content

armgit / BUI

Public Imported from GitHub
0

Blender's GPU Accelerated UI, Decoupled from it's original codebase. Supports Rust, Python, JS, React, C & C++. Twice as good as GPUI

share host

Preetham Pemmasani0954b16ce6e1
diff --git a/bindings/node/host.d.ts b/bindings/node/host.d.ts
new file mode 100644
index 0000000..a36040e
--- /dev/null
+++ b/bindings/node/host.d.ts
@@ -0,0 +1,23 @@
+// Types for @blender-ui/native/host: what framework bindings share.
+import type { App, AppOptions, Element, Window, WindowOptions } from './index'
+
+export type RenderOptions = WindowOptions & AppOptions & { app?: App }
+
+/** The window `render(..., options)` draws into: `options.app`'s, or a new App's. */
+export function open (options?: RenderOptions): { app: App, window: Window }
+
+/** `{ Button: wrap('Button'), ... }`: each Blender widget as a component. */
+export function blenderWidgets<C> (wrap: (name: string) => C): Record<
+ 'Button' | 'Checkbox' | 'Radio' | 'NumberField' | 'NumberSlider' | 'TextField' | 'TextArea' | 'ColorSwatch' |
+ 'Dropdown' | 'ProgressBar', C>
+
+/** The props of an element that had none (`applyProps(el, NO_PROPS, props)`). */
+export const NO_PROPS: Record<string, unknown>
+/** JSX-style prop -> BUI event (`onClick` -> `click`). */
+export const EVENTS: Record<string, string>
+/** Apply the difference between two JSX-style prop objects to an element:
+ * className, style (string, object or signal), events, signals as values,
+ * text children. */
+export function applyProps (el: Element, prev: Record<string, unknown>, next: Record<string, unknown>): void
+/** Text children (a string or number) rather than elements. */
+export function isText (children: unknown): children is string | number | bigint
diff --git a/bindings/node/host.js b/bindings/node/host.js
new file mode 100644
index 0000000..894bc77
--- /dev/null
+++ b/bindings/node/host.js
@@ -0,0 +1,126 @@
+// What every framework binding shares, so each one is a thin adapter over
+// the DOM-like tree (dom.js):
+//
+// open(options) the App and window a `render(..., options)` opens
+// blenderWidgets(wrap) Blender's widgets as the framework's components
+// applyProps(el, prev, next)
+// JSX-style props (className, style, onClick ...)
+// onto an element: for prop-based renderers (React;
+// a Vue or Solid universal renderer the same way)
+//
+// DOM-based frameworks (Svelte) need none of the props: their compiled code
+// calls the DOM, which `@blender-ui/native/globals` provides.
+//
+// SPDX-License-Identifier: GPL-2.0-or-later
+'use strict'
+
+const { App } = require('./index')
+const { BLENDER_COMPONENTS } = require('./dom')
+const { isSignal } = require('./signals')
+
+/**
+ * The window `render(..., options)` draws into: `options.app`'s, or a new App's.
+ * @param {{ app?: App, gpu?: boolean, slug?: boolean, headless?: boolean, theme?: 'dark' | 'light',
+ * title?: string, width?: number, height?: number, resizable?: boolean }} options
+ */
+function open (options = {}) {
+ const app = options.app ?? new App({ gpu: options.gpu, slug: options.slug, headless: options.headless, theme: options.theme })
+ return { app, window: app.window(options) }
+}
+
+/** `{ Button: wrap('Button'), ... }`: each Blender widget as a component. */
+function blenderWidgets (wrap) {
+ const out = {}
+ for (const name of Object.keys(BLENDER_COMPONENTS)) {
+ const C = wrap(name)
+ Object.defineProperty(C, 'name', { value: name })
+ out[name] = C
+ }
+ return out
+}
+
+// ---- JSX-style props ----------------------------------------------------------
+
+// prop -> BUI event; onChange is per keystroke on text fields, as in React DOM.
+const EVENTS = {
+ onClick: 'click', onMouseDown: 'mousedown', onMouseUp: 'mouseup', onMouseMove: 'mousemove',
+ onMouseEnter: 'mouseenter', onMouseLeave: 'mouseleave', onWheel: 'wheel',
+ onKeyDown: 'keydown', onKeyUp: 'keyup', onInput: 'input', onChange: 'change',
+}
+const TEXT_INPUTS = new Set(['textarea'])
+const ATTR_ALIASES = { className: 'class', htmlFor: 'for', tabIndex: 'tabindex' }
+const SKIP = new Set(['children', 'key', 'ref', 'dangerouslySetInnerHTML', 'suppressHydrationWarning',
+ 'suppressContentEditableWarning'])
+/** The props of an element that had none. */
+const NO_PROPS = {}
+
+function eventName (el, prop) {
+ if (prop === 'onChange') {
+ // text fields report every edit as `input`; checkboxes, selects: `change`
+ const type = el.getAttribute('type')
+ if (TEXT_INPUTS.has(el.localName) || (el.localName === 'input' &&
+ !['checkbox', 'radio', 'range', 'color', 'file'].includes(type))) return 'input'
+ }
+ return EVENTS[prop]
+}
+
+/** Listen with `fn` (or stop, when it isn't a function) for prop `k`.
+ * The DOM event is remembered with it: onChange's depends on `type`. */
+function listen (el, k, fn) {
+ const props = (el._props ??= {})
+ const cur = props[k]
+ const type = typeof fn === 'function' ? eventName(el, k) : null
+ if (cur !== undefined && cur.fn === fn && cur.type === type) return
+ if (cur !== undefined) el.removeEventListener(cur.type, cur.fn)
+ if (type === null) delete props[k]
+ else {
+ el.addEventListener(type, fn)
+ props[k] = { type, fn }
+ }
+}
+
+/** Text children (`<b>{n}</b>`) rather than elements. */
+function isText (children) {
+ return typeof children === 'string' || typeof children === 'number' || typeof children === 'bigint'
+}
+
+/** Apply the difference between two prop objects to an element. Hot: React
+ * calls it for every element of a re-rendered list, mostly with equal props. */
+function applyProps (el, prev, next) {
+ if (prev !== NO_PROPS) {
+ for (const k in prev) {
+ if (k in next || SKIP.has(k)) continue
+ if (k in EVENTS) {
+ listen(el, k, null)
+ } else {
+ el.removeAttribute(ATTR_ALIASES[k] ?? k)
+ }
+ }
+ }
+ let events = false
+ for (const k in next) {
+ const v = next[k]
+ if (prev[k] === v || SKIP.has(k)) continue
+ if (k in EVENTS) {
+ events = true // after the attributes: onChange's event depends on `type`
+ } else if (k === 'style') {
+ if (v == null) el.removeAttribute('style')
+ else el.style = v // a string, an object or a signal of either
+ } else if (v == null || v === false) {
+ el.removeAttribute(ATTR_ALIASES[k] ?? k)
+ } else if (isSignal(v)) {
+ el.setAttribute(ATTR_ALIASES[k] ?? k, v) // bound: updates without a re-render
+ } else {
+ el.setAttribute(ATTR_ALIASES[k] ?? k, v === true ? '' : v)
+ }
+ }
+ if (events || (next.onChange !== undefined && prev.type !== next.type)) {
+ for (const k in next) if (k in EVENTS) listen(el, k, next[k])
+ }
+ // text children; text -> elements is the renderer's to clear (React's
+ // resetTextContent, before it inserts them)
+ const c = next.children
+ if (c !== prev.children && isText(c)) el.textContent = String(c)
+}
+
+module.exports = { open, blenderWidgets, applyProps, isText, NO_PROPS, EVENTS }
diff --git a/bindings/node/package.json b/bindings/node/package.json
index 4f8f5ba..ad4aacc 100644
--- a/bindings/node/package.json
+++ b/bindings/node/package.json
@@ -13,13 +13,19 @@
"default": "./index.js"
},
"./signals": "./signals.js",
+ "./host": {
+ "types": "./host.d.ts",
+ "default": "./host.js"
+ },
"./package.json": "./package.json"
},
"files": [
"index.js",
"dom.js",
+ "host.js",
"signals.js",
"index.d.ts",
+ "host.d.ts",
"cli.js",
"blenderui.*.node",
"bui-runtime.*",
diff --git a/bindings/react/index.js b/bindings/react/index.js
index 957dac3..5a98b6d 100644
--- a/bindings/react/index.js
+++ b/bindings/react/index.js
@@ -16,7 +16,8 @@ const Reconciler = require('react-reconciler')
const { ConcurrentRoot, DefaultEventPriority, DiscreteEventPriority, NoEventPriority } =
require('react-reconciler/constants')
const native = require('@blender-ui/native')
-const { App, TextNode, Signal, Memo, isSignal } = native
+const { TextNode, Signal, Memo, isSignal } = native
+const { open, blenderWidgets, applyProps, isText, NO_PROPS } = require('@blender-ui/native/host')
const hooks = require('./hooks')
// A signal rendered as a child (`<span>{count}</span>`) becomes this host
@@ -35,85 +36,6 @@ for (const C of [Signal, Memo]) {
})
}
-// React prop -> BUI event; onChange is per keystroke on text fields, as in React DOM.
-const EVENTS = {
- onClick: 'click', onMouseDown: 'mousedown', onMouseUp: 'mouseup', onMouseMove: 'mousemove',
- onMouseEnter: 'mouseenter', onMouseLeave: 'mouseleave', onWheel: 'wheel',
- onKeyDown: 'keydown', onKeyUp: 'keyup', onInput: 'input', onChange: 'change',
-}
-const TEXT_INPUTS = new Set(['textarea'])
-const ATTR_ALIASES = { className: 'class', htmlFor: 'for', tabIndex: 'tabindex' }
-const SKIP = new Set(['children', 'key', 'ref', 'dangerouslySetInnerHTML', 'suppressHydrationWarning',
- 'suppressContentEditableWarning'])
-const NO_PROPS = {}
-
-function eventName (el, prop) {
- if (prop === 'onChange') {
- // text fields report every edit as `input`; checkboxes, selects: `change`
- const type = el.getAttribute('type')
- if (TEXT_INPUTS.has(el.localName) || (el.localName === 'input' &&
- !['checkbox', 'radio', 'range', 'color', 'file'].includes(type))) return 'input'
- }
- return EVENTS[prop]
-}
-
-/** Listen with `fn` (or stop, when it isn't a function) for React prop `k`.
- * The DOM event is remembered with it: onChange's depends on `type`. */
-function listen (el, k, fn) {
- const cur = el._react[k]
- const type = typeof fn === 'function' ? eventName(el, k) : null
- if (cur !== undefined && cur.fn === fn && cur.type === type) return
- if (cur !== undefined) el.removeEventListener(cur.type, cur.fn)
- if (type === null) delete el._react[k]
- else {
- el.addEventListener(type, fn)
- el._react[k] = { type, fn }
- }
-}
-
-function isText (children) {
- return typeof children === 'string' || typeof children === 'number' || typeof children === 'bigint'
-}
-
-/** Apply the difference between two prop objects to an element. Hot: React
- * calls it for every element of a re-rendered list, mostly with equal props. */
-function applyProps (el, prev, next) {
- if (prev !== NO_PROPS) {
- for (const k in prev) {
- if (k in next || SKIP.has(k)) continue
- if (k in EVENTS) {
- listen(el, k, null)
- } else {
- el.removeAttribute(ATTR_ALIASES[k] ?? k)
- }
- }
- }
- let events = false
- for (const k in next) {
- const v = next[k]
- if (prev[k] === v || SKIP.has(k)) continue
- if (k in EVENTS) {
- events = true // after the attributes: onChange's event depends on `type`
- } else if (k === 'style') {
- if (v == null) el.removeAttribute('style')
- else el.style = v // a string, an object or a signal of either
- } else if (v == null || v === false) {
- el.removeAttribute(ATTR_ALIASES[k] ?? k)
- } else if (isSignal(v)) {
- el.setAttribute(ATTR_ALIASES[k] ?? k, v) // bound: updates without a re-render
- } else {
- el.setAttribute(ATTR_ALIASES[k] ?? k, v === true ? '' : v)
- }
- }
- if (events || (next.onChange !== undefined && prev.type !== next.type)) {
- for (const k in next) if (k in EVENTS) listen(el, k, next[k])
- }
- // text children; text -> elements is React's resetTextContent (before it
- // inserts them), so nothing to clear here
- const c = next.children
- if (c !== prev.children && isText(c)) el.textContent = String(c)
-}
-
// Nodes a render created. React may throw a render away (an interrupted or
// suspended concurrent render); its nodes are then never attached, and are
// freed after the next commit.
@@ -162,7 +84,6 @@ const host = {
createInstance (type, props, container) {
if (type === SIGNAL_TEXT) return created(container, container.window.createTextNode(props.signal))
const el = container.window.createElement(type)
- el._react = {}
applyProps(el, NO_PROPS, props)
return created(container, el)
},
@@ -317,12 +238,11 @@ function createRoot (window) {
* Open a window and render `element` into it.
* @param {React.ReactNode} element
* @param {{ title?: string, width?: number, height?: number, resizable?: boolean,
- * gpu?: boolean, slug?: boolean, headless?: boolean, app?: App }} [options]
- * @returns {{ app: App, window: import('@blender-ui/native').Window, root: ReturnType<typeof createRoot> }}
+ * gpu?: boolean, slug?: boolean, headless?: boolean, app?: import('@blender-ui/native').App }} [options]
+ * @returns {{ app: import('@blender-ui/native').App, window: import('@blender-ui/native').Window, root: ReturnType<typeof createRoot> }}
*/
function render (element, options = {}) {
- const app = options.app ?? new App({ gpu: options.gpu, slug: options.slug, headless: options.headless, theme: options.theme })
- const window = app.window(options)
+ const { app, window } = open(options)
const root = createRoot(window)
root.render(element)
return { app, window, root }
@@ -342,12 +262,7 @@ for (const k of Object.keys(React)) if (/^use([A-Z]|$)/.test(k)) reactHooks[k] =
// Blender's widgets as components: `<NumberSlider min={0} max={10} />`,
// `<Button onClick={..}>Apply</Button>`. Always Blender widgets; their
// lowercase HTML counterparts look like a browser's (Blender's inside `styled`).
-const blenderComponents = {}
-for (const name of Object.keys(native.BLENDER_COMPONENTS)) {
- const C = (props) => React.createElement(name, props)
- Object.defineProperty(C, 'name', { value: name })
- blenderComponents[name] = C
-}
+const blenderComponents = blenderWidgets((name) => (props) => React.createElement(name, props))
module.exports = {
...blenderComponents,