<commit_before>(ns clj-simple-chart.jfx
  (:gen-class
    :extends javafx.application.Application)
  (:require [hiccup.core :as hiccup])
  (:import (javafx.application Application Platform)
           (java.util.concurrent CountDownLatch)
           (javafx.scene.web WebView)
           (javafx.scene.layout VBox)
           (javafx.scene Scene SnapshotParameters)
           (javafx.geometry Rectangle2D)
           (javafx.embed.swing SwingFXUtils)
           (javax.imageio ImageIO)
           (java.io File)
           (javafx.concurrent Worker$State)
           (javafx.event EventHandler)
           (javafx.scene.input KeyCode)))

(defonce latch (CountDownLatch. 1))
(defonce zoom (atom 1.0))
(defonce webview (atom nil))
(defonce stage (atom nil))
(defonce engine (atom nil))
(defonce scene (atom nil))

(defonce input-height (atom nil))
(defonce input-width (atom nil))

(defn update-zoom! [zoomvalue]
  (swap! zoom (fn [o] zoomvalue))
  (Platform/runLater (fn [] (.setZoom @webview @zoom)))
  (Platform/runLater (fn [] (.setPrefHeight @webview (* @zoom @input-height))))
  (Platform/runLater (fn [] (.setPrefWidth @webview (* @zoom @input-width))))
  (Platform/runLater (fn [] (.sizeToScene @stage))))

(def zoom-handler
  (reify EventHandler
    (handle [_ v]
      (update-zoom! (* @zoom (.getZoomFactor v))))))

(def zoom-delta 0.025)

(def key-released-handler
  (reify EventHandler
    (handle [_ v]
      (cond (and (.isControlDown v)
                 (= (.getText v) "0"))
            (update-zoom! 1.0)

            (and (.isControlDown v)
                 (= (.getCode v) KeyCode/MINUS))
            (update-zoom! (- @zoom zoom-delta))

            (and (.isControlDown v)
                 (= (.getCode v) KeyCode/EQUALS))
            (update-zoom! (+ @zoom zoom-delta))

            :else (do (println "unhandled" v))))))

(defn -start [this internal-stage]
  (.setTitle internal-stage "SVG Output")
  (let [internal-webview (WebView.)
        internal-engine (.getEngine internal-webview)
        layout (VBox. 0.0)
        internal-scene (Scene. layout)
        children (.getChildren layout)]
    (.loadContent internal-engine "about:blank")
    (.setAll children [internal-webview])
    (.setScene internal-stage internal-scene)
    (.setOnZoom internal-scene zoom-handler)
    (.setOnKeyReleased internal-scene key-released-handler)
    (.show internal-stage)
    (swap! engine (fn [x] internal-engine))<commit_msg>More jfx tweaks<commit_after>(ns clj-simple-chart.jfx
  (:gen-class
    :extends javafx.application.Application)
  (:require [hiccup.core :as hiccup])
  (:import (javafx.application Application Platform)
           (java.util.concurrent CountDownLatch)
           (javafx.scene.web WebView)
           (javafx.scene.layout VBox)
           (javafx.scene Scene SnapshotParameters)
           (javafx.geometry Rectangle2D)
           (javafx.embed.swing SwingFXUtils)
           (javax.imageio ImageIO)
           (java.io File)
           (javafx.concurrent Worker$State)
           (javafx.event EventHandler)
           (javafx.scene.input KeyCode)
           (javafx.beans.value ChangeListener)))

(defonce latch (CountDownLatch. 1))
(defonce zoom (atom 1.0))
(defonce webview (atom nil))
(defonce stage (atom nil))
(defonce engine (atom nil))
(defonce scene (atom nil))

(defonce input-height (atom nil))
(defonce input-width (atom nil))

(defonce window-height (atom nil))
(defonce window-width (atom nil))

(defn update-zoom! [zoomvalue]
  (swap! zoom (fn [o] zoomvalue))
  (Platform/runLater (fn [] (.setZoom @webview @zoom)))
  (Platform/runLater (fn [] (.setPrefHeight @webview (* @zoom @input-height))))
  (Platform/runLater (fn [] (.setPrefWidth @webview (* @zoom @input-width))))
  (Platform/runLater (fn [] (.sizeToScene @stage))))

(def zoom-handler
  (reify EventHandler
    (handle [_ v]
      (update-zoom! (* @zoom (.getZoomFactor v))))))

(def zoom-delta 0.025)

; input-width * zoom = new
; zoom = new / input-width

(defn key-released-handler
  [v]
  (cond (and (.isControlDown v)
             (= (.getText v) "0"))
        (update-zoom! 1.0)

        (and (.isControlDown v)
             (= (.getCode v) KeyCode/W))
        (update-zoom! (/ @window-width @input-width))

        (and (.isControlDown v)
             (= (.getCode v) KeyCode/MINUS))
        (update-zoom! (- @zoom zoom-delta))

        (and (.isControlDown v)
             (= (.getCode v) KeyCode/EQUALS))
        (update-zoom! (+ @zoom zoom-delta))

        :else (do #_(println "unhandled" v))))

(defn window-width-change-handler [new]
  (when (number? new)
    (swap! window-width (fn [o] new))))

(defn window-height-change-handler [new]
  (when (number? new)
    (swap! window-height (fn [o] new))))

(defn -start [this internal-stage]
  (.setTitle internal-stage "SVG Output")
  (let [internal-webview (WebView.)
        internal-engine (.getEngine internal-webview)
        layout (VBox. 0.0)
        internal-scene (Scene. layout)
        children (.getChildren layout)]
    (.loadContent internal-engine "about:blank")
    (.setAll children [internal-webview])
    (.setScene internal-stage internal-scene)
    (.setOnZoom internal-scene zoom-handler)
    (.addListener (.widthProperty internal-scene) (reify ChangeListener
                                                    (changed [_ val old new]
                                                      (window-width-change-handler new))))
    (.addListener (.heightProperty internal-scene) (reify ChangeListener
                                                     (changed [_ val old new]
                                                       (window-height-change-handler new))))
    (.setOnKeyReleased internal-scene (reify EventHandler
                                        (handle [_ v]
                                          (key-released-handler v))))
    (.show internal-stage)
    (swap! engine (fn [x] internal-engine))
{'fraction_non_alphanumeric': 0.13974683544303798, 'fraction_numerical': 0.004388185654008439, 'mean_word_length': 2.376638176638177, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '898497', 'n_tokens_mistral': 1872, 'n_tokens_neox': 1779, 'n_words': 458}