0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-10 09:08:31 -05:00

WIP: proper initialization

This commit is contained in:
Alejandro Alonso 2024-10-02 11:17:36 +02:00
parent bc0fde68c7
commit 5f8d56b366
6 changed files with 9471 additions and 10 deletions

View file

@ -105,6 +105,15 @@ pub extern "C" fn init(width: i32, height: i32) -> Box<State> {
Box::new(state)
}
/// This is called from JS when the window is resized.
/// # Safety
#[no_mangle]
pub unsafe extern "C" fn resize_surface(state: *mut State, width: i32, height: i32) {
let state = unsafe { state.as_mut() }.expect("got an invalid state pointer");
let surface = create_surface(&mut state.gpu_state, width, height);
state.set_surface(surface);
}
/// Draw a black rect at the specified coordinates.
/// # Safety
#[no_mangle]

View file

@ -275,7 +275,7 @@
[canvas-ref]
(let [canvas (mf/ref-val canvas-ref)]
(p/then (renderer/init)
#(renderer/set-canvas canvas)))))
#(renderer/set-canvas canvas vbox base-objects)))))
(hooks/setup-dom-events zoom disable-paste in-viewport? workspace-read-only? drawing-tool drawing-path?)
(hooks/setup-viewport-size vport viewport-ref)

View file

@ -21,7 +21,7 @@
(renderer-rs/init)))
(defn set-canvas
[canvas]
[canvas vbox base-objects]
(cond
;; CPP
(contains? cf/flags :renderer-v2-cpp)
@ -29,4 +29,4 @@
;; Rust
(contains? cf/flags :renderer-v2-rs)
(renderer-rs/set-canvas canvas)))
(renderer-rs/set-canvas canvas vbox base-objects)))

View file

@ -8,7 +8,8 @@
(:require
["./cpp.js" :as renderer]
[beicon.v2.core :as rx]
[potok.v2.core :as ptk]))
[potok.v2.core :as ptk]
[promesa.core :as p]))
(defonce ^:dynamic internal-module nil)
@ -25,9 +26,4 @@
(defn init
[]
(ptk/reify ::init
ptk/WatchEvent
(watch [_ _ _]
(let [p (renderer)]
(->> (rx/from p)
(rx/map #(on-init %)))))))
(p/then (renderer) #(on-init %)))

View file

@ -0,0 +1,55 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) KALEIDOS INC
(ns app.renderer.rs
(:require
["./rs.js" :as renderer]
[app.config :as cf]
[beicon.v2.core :as rx]
[goog.object :as gobj]
[potok.v2.core :as ptk]
[promesa.core :as p]))
(defonce ^:dynamic internal-module #js {})
(defn set-canvas
[canvas vbox objects]
(let [gl (gobj/get ^js internal-module "GL")
context (.getContext canvas "webgl2" {"antialias" true
"depth" true
"stencil" true
"alpha" true})
_ (js/console.log "context" context)
;; Register the context with emscripten
handle (.registerContext gl context {"majorVersion" 2})
_ (.makeContextCurrent gl handle)
;; Initialize Skia
state (._init ^js internal-module (.-width canvas) (.-height canvas))
draw_rect (gobj/get ^js internal-module "_draw_rect")
resize_surface (gobj/get ^js internal-module "_resize_surface")]
(set! (.-width canvas) (.-clientWidth canvas))
(set! (.-height canvas) (.-clientHeight canvas))
;; (resize_surface state (.-clientWidth canvas) (.-clientHeight canvas))
(doseq [shape (vals objects)]
(let [sr (:selrect shape)]
(println "-----" (:x1 sr) (:y1 sr) (:x2 sr) (:y2 sr))
;; TODO: scale y translate con el vbox
(draw_rect state (:x1 sr) (:y1 sr) (:x2 sr) (:y2 sr))))
#_(draw_rect state 100 100 500 500)
(println "set-canvas ok" (.-width canvas) (.-height canvas))))
(defn on-init
[module']
(set! internal-module module')
(println "on-init ok"))
(defn init
[]
(p/then (renderer) #(on-init %)))

File diff suppressed because it is too large Load diff