From 078eecad2fb8d6fad07a7b125c3d2f12ba9198c8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Loi=CC=88c=20Correnson?= <loic.correnson@cea.fr>
Date: Fri, 4 Sep 2020 16:10:40 +0200
Subject: [PATCH] [dome] introduce useFlipSettings helper

---
 ivette/src/dome/src/renderer/dome.tsx         | 27 ++++++++++++-------
 .../src/dome/src/renderer/frame/sidebars.js   |  6 ++---
 ivette/src/dome/src/renderer/layout/boxes.tsx |  5 ++--
 ivette/src/frama-c/dive/Dive.tsx              |  2 +-
 ivette/src/renderer/ASTview.tsx               |  2 +-
 ivette/src/renderer/Application.tsx           |  4 +--
 ivette/src/renderer/Properties.tsx            |  2 +-
 7 files changed, 28 insertions(+), 20 deletions(-)

diff --git a/ivette/src/dome/src/renderer/dome.tsx b/ivette/src/dome/src/renderer/dome.tsx
index 2020fef1295..4ab30ec636e 100644
--- a/ivette/src/dome/src/renderer/dome.tsx
+++ b/ivette/src/dome/src/renderer/dome.tsx
@@ -589,31 +589,40 @@ export function useClock(period: number, initStart: boolean): Timer {
 // --- Settings Hookds
 // --------------------------------------------------------------------------
 
-export type FlipState = [boolean, (newState?: boolean) => void];
-
 /**
    Bool window settings helper. Default is `false` unless specified.
-   See also [[dome/data/settings]].
-   @returns `[state,flipState]` where flipState can be invoked with an
-   optional argument. By default, `flipState()` invert the state and
-    `flipState(s)` set the state to `s`.
 */
 export function useBoolSettings(
   key: string | undefined,
   defaultValue = false,
-): FlipState {
+) {
+  return Settings.useWindowSettings(
+    key, Json.jBoolean, defaultValue,
+  );
+}
+
+/**
+   Bool window settings helper with a flip callback.
+ */
+export function useFlipSettings(
+  key: string | undefined,
+  defaultValue = false,
+): [boolean, () => void] {
   const [state, setState] = Settings.useWindowSettings(
     key, Json.jBoolean, defaultValue,
   );
   const flipState = React.useCallback(
-    (v) => setState(v === undefined ? !state : v),
+    () => setState(!state),
     [state, setState],
   );
   return [state, flipState];
 }
 
 /** Number window settings helper. Default is `0` unless specified. */
-export function useNumberSettings(key: string | undefined, defaultValue = 0) {
+export function useNumberSettings(
+  key: string | undefined,
+  defaultValue = 0,
+) {
   return Settings.useWindowSettings(
     key, Json.jNumber, defaultValue,
   );
diff --git a/ivette/src/dome/src/renderer/frame/sidebars.js b/ivette/src/dome/src/renderer/frame/sidebars.js
index 3ee74333bc2..a87d5dbca06 100644
--- a/ivette/src/dome/src/renderer/frame/sidebars.js
+++ b/ivette/src/dome/src/renderer/frame/sidebars.js
@@ -8,7 +8,7 @@
 */
 
 import React from 'react' ;
-import { useBoolSettings } from 'dome';
+import { useFlipSettings } from 'dome';
 import { Badge } from 'dome/controls/icons' ;
 import { Label } from 'dome/controls/labels' ;
 
@@ -107,7 +107,7 @@ const disableAll = (children) =>
 export function Section(props) {
 
   const context = React.useContext( SideBarContext );
-  const [ state=true, setState ] = useBoolSettings(
+  const [ state, flipState ] = useFlipSettings(
     makeSettings(context.settings,props), props.defaultUnfold
   );
   const { enabled=true, disabled=false, unfold, children } = props ;
@@ -117,7 +117,7 @@ export function Section(props) {
   const dimmed = context.disabled || disabled || !enabled ;
   const foldable = unfold === undefined ;
   const visible = foldable ? state : unfold ;
-  const onClick = foldable ? (() => setState(!state)) : undefined ;
+  const onClick = foldable ? flipState : undefined ;
   const maxHeight = visible ? 'max-content' : 0 ;
   const subContext = Object.assign( {}, context, { disabled: dimmed } );
 
diff --git a/ivette/src/dome/src/renderer/layout/boxes.tsx b/ivette/src/dome/src/renderer/layout/boxes.tsx
index 3e87958bbbd..3ddb05d1a24 100644
--- a/ivette/src/dome/src/renderer/layout/boxes.tsx
+++ b/ivette/src/dome/src/renderer/layout/boxes.tsx
@@ -180,13 +180,12 @@ export const Folder = (props: FolderProps) => {
     indent = 18,
     label, title, children,
   } = props;
-  const [unfold, onClick]: [boolean, () => void] =
-    Dome.useBoolSettings(settings, defaultUnfold);
+  const [unfold, flip] = Dome.useFlipSettings(settings, defaultUnfold);
   const icon = unfold ? 'TRIANGLE.DOWN' : 'TRIANGLE.RIGHT';
   const display = unfold ? 'none' : 'block';
   return (
     <Vpack>
-      <Hpack onClick={onClick}>
+      <Hpack onClick={flip}>
         <Title icon={icon} label={label} title={title} />
       </Hpack>
       <Vpack style={{ display, marginLeft: indent }}>
diff --git a/ivette/src/frama-c/dive/Dive.tsx b/ivette/src/frama-c/dive/Dive.tsx
index 547aaff243a..ced3fce226f 100644
--- a/ivette/src/frama-c/dive/Dive.tsx
+++ b/ivette/src/frama-c/dive/Dive.tsx
@@ -475,7 +475,7 @@ const GraphView = () => {
   const [dive, setDive] = useState(() => new Dive());
   const [selection, updateSelection] = States.useSelection();
   const [lock, flipLock] =
-    Dome.useBoolSettings('dive.lock');
+    Dome.useFlipSettings('dive.lock');
   const [selectionMode, setSelectionMode] =
     Dome.useStringSettings('dive.selectionMode', 'follow');
 
diff --git a/ivette/src/renderer/ASTview.tsx b/ivette/src/renderer/ASTview.tsx
index b97767d733a..a6480aa679b 100644
--- a/ivette/src/renderer/ASTview.tsx
+++ b/ivette/src/renderer/ASTview.tsx
@@ -92,7 +92,7 @@ const ASTview = () => {
   const multipleSelections = selection?.multiple.allSelections;
   const [theme, setTheme] = Settings.useGlobalSettings(Theme);
   const [fontSize, setFontSize] = Settings.useGlobalSettings(FontSize);
-  const [wrapText, flipWrapText] = Dome.useBoolSettings('ASTview.wrapText');
+  const [wrapText, flipWrapText] = Dome.useFlipSettings('ASTview.wrapText');
   const markersInfo = States.useSyncArray(markerInfo);
 
   const theFunction = selection?.current?.function;
diff --git a/ivette/src/renderer/Application.tsx b/ivette/src/renderer/Application.tsx
index 29683ee7fed..b42cf0c033c 100644
--- a/ivette/src/renderer/Application.tsx
+++ b/ivette/src/renderer/Application.tsx
@@ -58,9 +58,9 @@ const HistorySelectionControls = () => {
 
 export default (() => {
   const [sidebar, flipSidebar] =
-    Dome.useBoolSettings('frama-c.sidebar.unfold', true);
+    Dome.useFlipSettings('frama-c.sidebar.unfold', true);
   const [viewbar, flipViewbar] =
-    Dome.useBoolSettings('frama-c.viewbar.unfold', true);
+    Dome.useFlipSettings('frama-c.viewbar.unfold', true);
 
   return (
     <Vfill>
diff --git a/ivette/src/renderer/Properties.tsx b/ivette/src/renderer/Properties.tsx
index 4da79082754..e0ab3df3411 100644
--- a/ivette/src/renderer/Properties.tsx
+++ b/ivette/src/renderer/Properties.tsx
@@ -442,7 +442,7 @@ const RenderTable = () => {
     States.useSelection();
 
   const [showFilter, flipFilter] =
-    Dome.useBoolSettings('ivette.properties.showFilter');
+    Dome.useFlipSettings('ivette.properties.showFilter');
 
   // Updating the filter
   const selectedFunction = selection?.current?.function;
-- 
GitLab