0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-03-12 15:51:37 -05:00

Reorganize viewport positioning related code.

This commit is contained in:
Andrey Antukh 2017-01-11 18:19:22 +01:00
parent c906a4e46f
commit 137af0e14d
No known key found for this signature in database
GPG key ID: 4DFEBCB8316A8B95
6 changed files with 108 additions and 65 deletions

View file

@ -25,3 +25,10 @@
:grid-color "#cccccc"
:grid-alignment true
:background "#ffffff"})
(def zoom-levels
[0.20 0.21 0.22 0.23 0.24 0.25 0.27 0.28 0.30 0.32 0.34
0.36 0.38 0.40 0.42 0.44 0.46 0.48 0.51 0.54 0.57 0.60
0.63 0.66 0.69 0.73 0.77 0.81 0.85 0.90 0.95 1.00 1.05
1.10 1.15 1.21 1.27 1.33 1.40 1.47 1.54 1.62 1.70 1.78
1.87 1.96 2.06 2.16 2.27 2.38 2.50 2.62 2.75 2.88 3.00])

View file

@ -20,6 +20,8 @@
[uxbox.main.data.shapes-impl :as shimpl]
[uxbox.main.data.lightbox :as udl]
[uxbox.main.data.history :as udh]
[uxbox.main.data.workspace.scroll :as wscroll]
[uxbox.util.uuid :as uuid]
[uxbox.util.spec :as us]
[uxbox.util.forms :as sc]
@ -29,14 +31,10 @@
[uxbox.util.math :as mth]
[uxbox.util.data :refer (index-of)]))
;; --- Constants
;; --- Expose inner functions
(def zoom-levels
[0.20 0.21 0.22 0.23 0.24 0.25 0.27 0.28 0.30 0.32 0.34
0.36 0.38 0.40 0.42 0.44 0.46 0.48 0.51 0.54 0.57 0.60
0.63 0.66 0.69 0.73 0.77 0.81 0.85 0.90 0.95 1.00 1.05
1.10 1.15 1.21 1.27 1.33 1.40 1.47 1.54 1.62 1.70 1.78
1.87 1.96 2.06 2.16 2.27 2.38 2.50 2.62 2.75 2.88 3.00])
(def start-viewport-positioning wscroll/start-viewport-positioning)
(def stop-viewport-positioning wscroll/stop-viewport-positioning)
;; --- Initialize Workspace
@ -192,7 +190,7 @@
[]
(InitializeIconsToolbox.))
;; --- Copy to Clipboard
;; --- Clipboard Management
(defrecord CopyToClipboard []
ptk/UpdateEvent
@ -213,8 +211,6 @@
[]
(CopyToClipboard.))
;; --- Paste from Clipboard
(defrecord PasteFromClipboard [id]
ptk/UpdateEvent
(update [_ state]
@ -231,37 +227,33 @@
([] (PasteFromClipboard. nil))
([id] (PasteFromClipboard. id)))
;; --- Increase Zoom
;; --- Zoom Management
(defrecord IncreaseZoom []
(deftype IncreaseZoom []
ptk/UpdateEvent
(update [_ state]
(let [increase #(nth zoom-levels
(+ (index-of zoom-levels %) 1)
(last zoom-levels))]
(let [increase #(nth c/zoom-levels
(+ (index-of c/zoom-levels %) 1)
(last c/zoom-levels))]
(update-in state [:workspace :zoom] (fnil increase 1)))))
(defn increase-zoom
[]
(IncreaseZoom.))
;; --- Decrease Zoom
(defrecord DecreaseZoom []
(deftype DecreaseZoom []
ptk/UpdateEvent
(update [_ state]
(let [decrease #(nth zoom-levels
(- (index-of zoom-levels %) 1)
(first zoom-levels))]
(let [decrease #(nth c/zoom-levels
(- (index-of c/zoom-levels %) 1)
(first c/zoom-levels))]
(update-in state [:workspace :zoom] (fnil decrease 1)))))
(defn decrease-zoom
[]
(DecreaseZoom.))
;; --- Reset Zoom
(defrecord ResetZoom []
(deftype ResetZoom []
ptk/UpdateEvent
(update [_ state]
(assoc-in state [:workspace :zoom] 1)))
@ -270,7 +262,7 @@
[]
(ResetZoom.))
;; --- Set tooltip
;; --- Tooltips
(defrecord SetTooltip [text]
ptk/UpdateEvent
@ -281,7 +273,7 @@
[text]
(SetTooltip. text))
;; --- Initialize Alignment Index
;; --- Grid Alignment
(declare initialize-alignment?)

View file

@ -0,0 +1,69 @@
;; 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) 2015-2017 Andrey Antukh <niwi@niwi.nz>
(ns uxbox.main.data.workspace.scroll
"Workspace scroll related events. Mostly or all events
are related to UI logic."
(:require [beicon.core :as rx]
[potok.core :as ptk]
[uxbox.main.ui.workspace.base :as wb]
[uxbox.util.mixins :as mx :include-macros true]
[uxbox.util.rlocks :as rlocks]
[uxbox.util.dom :as dom]
[uxbox.util.geom.point :as gpt]))
;; --- Start Viewport Positioning
(declare stop-viewport-positioning?)
(defn run-viewport-positioning
[stream]
(let [stoper (->> (rx/filter stop-viewport-positioning? stream)
(rx/take 1))
reference @wb/mouse-viewport-a
dom (dom/get-element "workspace-canvas")]
(->> wb/mouse-viewport-s
(rx/take-until stoper)
(rx/map (fn [point]
(let [{:keys [x y]} (gpt/subtract point reference)
cx (.-scrollLeft dom)
cy (.-scrollTop dom)]
(set! (.-scrollLeft dom) (- cx x))
(set! (.-scrollTop dom) (- cy y)))))
(rx/ignore))))
(deftype StartViewportPositioning [id]
ptk/UpdateEvent
(update [_ state]
(update-in state [:workspace :viewport-positionig] #(if (nil? %) id %)))
ptk/WatchEvent
(watch [_ state stream]
(let [cid (get-in state [:workspace :viewport-positionig])]
(if (= cid id)
(run-viewport-positioning stream)
(rx/empty)))))
(defn start-viewport-positioning
[]
(StartViewportPositioning. (gensym "viewport-positioning")))
;; --- Stop Viewport positioning
(deftype StopViewportPositioning []
ptk/UpdateEvent
(update [_ state]
(update state :workspace dissoc :viewport-positionig)))
(defn stop-viewport-positioning
[]
(StopViewportPositioning.))
(defn stop-viewport-positioning?
[v]
(instance? StopViewportPositioning v))

View file

@ -46,7 +46,7 @@
(let [[projectid pageid] (:rum/args own)
dom (mx/ref-node own "workspace-canvas")
scroll-to-page-center #(scroll/scroll-to-page-center dom @wb/page-ref)
sub1 (scroll/watch-scroll-interactions own)
;; sub1 (scroll/watch-scroll-interactions own)
sub2 (rx/subscribe wb/page-id-ref-s scroll-to-page-center)]
(scroll-to-page-center)
@ -55,12 +55,14 @@
(udu/watch-page-changes pageid)
(udh/watch-page-changes pageid))
(assoc own ::sub1 sub1 ::sub2 sub2)))
(assoc own
;; ::sub1 sub1
::sub2 sub2)))
(defn- workspace-will-unmount
[own]
(st/emit! ::udp/stop-page-watcher)
(.close (::sub1 own))
;; (.close (::sub1 own))
(.close (::sub2 own))
(dissoc own ::sub1 ::sub2))

View file

@ -133,12 +133,15 @@
:ctrl? (kbd/ctrl? event)}]
(rx/push! wb/events-b [:key/down opts])
(when (kbd/space? event)
(rlocks/acquire! :workspace/scroll))))
(st/emit! (dw/start-viewport-positioning)))))
#_(rlocks/acquire! :workspace/scroll)
(on-key-up [event]
(let [opts {:key (.-keyCode event)
:shift? (kbd/shift? event)
:ctrl? (kbd/ctrl? event)}]
(when (kbd/space? event)
(st/emit! (dw/stop-viewport-positioning)))
(rx/push! wb/events-b [:key/up opts])))
(on-mousemove [event]

View file

@ -2,49 +2,19 @@
;; 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) 2015-2016 Andrey Antukh <niwi@niwi.nz>
;; Copyright (c) 2015-2016 Juan de la Cruz <delacruzgarciajuan@gmail.com>
;; Copyright (c) 2015-2017 Andrey Antukh <niwi@niwi.nz>
;; Copyright (c) 2015-2017 Juan de la Cruz <delacruzgarciajuan@gmail.com>
(ns uxbox.main.ui.workspace.scroll
"Workspace scroll events handling."
(:require [beicon.core :as rx]
[uxbox.util.mixins :as mx :include-macros true]
[potok.core :as ptk]
[uxbox.main.ui.workspace.base :as wb]
[uxbox.util.mixins :as mx :include-macros true]
[uxbox.util.rlocks :as rlocks]
[uxbox.util.dom :as dom]
[uxbox.util.geom.point :as gpt]))
(defn watch-scroll-interactions
[own]
(letfn [(is-space-up? [[type {:keys [key]}]]
(and (= 32 key) (= :key/up type)))
(on-start []
(let [stoper (->> wb/events-s
(rx/filter is-space-up?)
(rx/take 1))
local (:rum/local own)
initial @wb/mouse-viewport-a
stream (rx/take-until stoper wb/mouse-viewport-s)]
(swap! local assoc :scrolling true)
(rx/subscribe stream #(on-scroll % initial) nil on-scroll-end)))
(on-scroll-end []
(rlocks/release! :workspace/scroll)
(let [local (:rum/local own)]
(swap! local assoc :scrolling false)))
(on-scroll [pt initial]
(let [{:keys [x y]} (gpt/subtract pt initial)
el (mx/ref-node own "workspace-canvas")
cx (.-scrollLeft el)
cy (.-scrollTop el)]
(set! (.-scrollLeft el) (- cx x))
(set! (.-scrollTop el) (- cy y))))]
(let [stream (->> (rx/map first rlocks/stream)
(rx/filter #(= % :workspace/scroll)))]
(rx/subscribe stream on-start))))
(defn set-scroll-position
[dom position]
(set! (.-scrollLeft dom) (:x position))