CSS styles and related helper code for Pulse rendering.

(ns metabase.channel.render.style
  (:require
   [clojure.java.io :as io]
   [clojure.string :as str]
   [metabase.public-settings :as public-settings]
   [metabase.util.i18n :refer [trs]]
   [metabase.util.log :as log])
  (:import
   (java.awt Font GraphicsEnvironment)))
(set! *warn-on-reflection* true)

TODO - we should move other CSS definitions from metabase.channel.render namespaces into this one, so they're all in one place.

Compile one or more CSS style maps into a string.

(style {:font-weight 400, :color "white"}) -> "font-weight: 400; color: white;"

(defn style
  [& style-maps]
  (str/join " " (for [[k v] (into {} style-maps)
                      :let  [v (if (keyword? v) (name v) (str v))]
                      :when (seq v)]
                  (str (name k) ": " v ";"))))

Used as color for 'We were unable to display this Pulse' messages.

(def ^:const color-gold
  "#F9D45C")

Color for error messages.

(def ^:const color-error
  "#EF8C8C")

~75% gray.

(def ^:const color-gray-2
  "#BDC1BF")

~50% gray.

(def ^:const color-gray-3
  "#7C8381")

~25% gray.

(def ^:const color-gray-4
  "#394340")

Color for light text.

(def ^:const color-text-light
  "#B8BBC3")

Color for medium text.

(def ^:const color-text-medium
  "#949AAB")

Color for dark text.

(def ^:const color-text-dark
  "#4C5773")

Used as color for the border of table, table header, and table body rows for charts with :table vizualization.

(def ^:const color-border
  "#F0F0F0")

Primary color to use in Pulses; normally 'classic' MB blue, but customizable when whitelabeling is enabled.

don't try to improve the code and make this a plain variable, in EE it's customizable which is why it's a function. Too much of a hassle to have it be a fn in one version of the code an a constant in another

(defn primary-color
  []
  (public-settings/application-color))

Secondary color to use in Pulse charts; normally red, but customizable when whitelabeling is enabled.

(defn secondary-color
  []
  (public-settings/secondary-chart-color))

Font family to use in rendered Pulses.

(defn font-style
  []
  {:font-family "Lato, \"Helvetica Neue\", Helvetica, Arial, sans-serif"})

CSS style for a Pulse section.

(defn section-style
  []
  (font-style))

Style for a header of a pulse section.

(defn header-style
  []
  (merge
   (font-style)
   {:font-size       :18px
    :font-weight     700
    :color           (primary-color)
    :text-decoration :none}))

Style for a scalar display-type 'chart' in a Pulse.

(defn scalar-style
  []
  (merge
   (font-style)
   {:font-size   :24px
    :font-weight 700
    :color       color-text-dark}))
(defn- register-font! [filename]
  (with-open [is (io/input-stream (io/resource filename))]
    (.registerFont (GraphicsEnvironment/getLocalGraphicsEnvironment)
                   (Font/createFont java.awt.Font/TRUETYPE_FONT is))))
(defn- register-fonts! []
  (try
    (register-font! "frontend_client/app/fonts/Lato/Lato-Regular.ttf")
    (doseq [weight ["700" "900"]]
      (register-font! (format "frontend_client/app/fonts/Lato/lato-v16-latin-%s.ttf" weight)))
    (catch Throwable e
      (let [message (str (trs "Error registering fonts: Metabase will not be able to send Pulses.")
                         " "
                         (trs "This is a known issue with certain JVMs. See {0} and for more details."
                              "https://github.com/metabase/metabase/issues/7986"))]
        (log/error e message)
        (throw (ex-info message {} e))))))

Makes custom fonts available to Java so that CSSBox can render them.

(defonce ^{:doc      
           :arglists '([])} register-fonts-if-needed!
  (let [register!* (delay (register-fonts!))]
    (fn []
      @register!*)))