From d2e3c10330babcc01aee253b34d47973506dfefc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Loi=CC=88c=20Correnson?= <loic.correnson@cea.fr>
Date: Fri, 17 Jul 2020 11:06:02 +0200
Subject: [PATCH] [dome] more settings hooks

---
 ivette/src/dome/src/renderer/data/settings.ts |  8 ++++++++
 ivette/src/dome/src/renderer/dome.ts          | 19 +++++++++++++++++++
 2 files changed, 27 insertions(+)

diff --git a/ivette/src/dome/src/renderer/data/settings.ts b/ivette/src/dome/src/renderer/data/settings.ts
index 19e6730a157..e6e3a8a246e 100644
--- a/ivette/src/dome/src/renderer/data/settings.ts
+++ b/ivette/src/dome/src/renderer/data/settings.ts
@@ -29,6 +29,14 @@ interface Settings<A> {
 
 /**
    Global settings.
+   This utility class allows you to share accross several
+   components and windows the parameters associated to global settings.
+
+   However, it is important to note that global settings are uniquely identified
+   by their `name`. If you have multiple definitions of global settings class
+   with the same name, they will actually share the same value. Hence, if they
+   have different default values or decoders, this might leads to strange
+   results.
  */
 export class GlobalSettings<A> {
   name: string;
diff --git a/ivette/src/dome/src/renderer/dome.ts b/ivette/src/dome/src/renderer/dome.ts
index 873367b4b83..ab451cbafcb 100644
--- a/ivette/src/dome/src/renderer/dome.ts
+++ b/ivette/src/dome/src/renderer/dome.ts
@@ -636,6 +636,25 @@ export function useStringOptSettings(key: string | undefined) {
   );
 }
 
+/** Direct shortcut to [[dome/data/settings.useWindowSettings]]. */
+export const useWindowSettings = Settings.useWindowSettings;
+
+/**
+   Utility shortcut to [[dome/data/settings.useGlobalSettings]]
+   with global settings class created on-the-fly.
+ */
+export function useGlobalSettings<A extends Json.json>(
+  globalKey: string,
+  decoder: Json.Loose<A>,
+  defaultValue: A
+) {
+  // Object creation is cheaper than useMemo...
+  const G = new Settings.GlobalSettings(
+    globalKey, decoder, Json.identity, defaultValue
+  );
+  return Settings.useGlobalSettings(G);
+}
+
 // --------------------------------------------------------------------------
 // --- Pretty Printing (Browser Console)
 // --------------------------------------------------------------------------
-- 
GitLab