-
Notifications
You must be signed in to change notification settings - Fork 2
/
state.ts
171 lines (162 loc) · 6.12 KB
/
state.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module State
*/
import { SnapMode } from "@itwin/core-frontend";
import type { ActionsUnion } from "../redux/redux-ts";
import { createAction } from "../redux/redux-ts";
import type { ThemeId } from "../theme/ThemeManager";
import {
SYSTEM_PREFERRED_COLOR_THEME,
TOOLBAR_OPACITY_DEFAULT,
WIDGET_OPACITY_DEFAULT,
} from "../theme/ThemeManager";
// cSpell:ignore configurableui snapmode toolprompt
/** Action Ids used by Redux and to send sync UI components. Typically used to refresh visibility or enable state of control.
* Since these are also used as sync ids they should be in lowercase.
* @public
*/
export enum ConfigurableUiActionId {
SetSnapMode = "configurableui:set_snapmode",
SetTheme = "configurableui:set_theme",
SetToolPrompt = "configurableui:set_toolprompt",
SetWidgetOpacity = "configurableui:set_widget_opacity",
SetDragInteraction = "configurableui:set-drag-interaction",
SetShowWidgetIcon = "configurableui:set-show-widget-icon",
AutoCollapseUnpinnedPanels = "configurableui:set-auto-collapse-unpinned-panels",
SetViewOverlayDisplay = "configurableui:set-view-overlay-display",
AnimateToolSettings = "configurableui:set-animate-tool-settings",
UseToolAsToolSettingsLabel = "configurableui:set-use-tool-as-tool-settings-label",
SetToolbarOpacity = "configurableui:set-toolbar-opacity",
}
/** The portion of state managed by the ConfigurableUiReducer.
* @public
*/
export interface ConfigurableUiState {
snapMode: number;
toolPrompt: string;
theme: string;
widgetOpacity: number;
useDragInteraction: boolean;
showWidgetIcon: boolean;
autoCollapseUnpinnedPanels: boolean;
viewOverlayDisplay: boolean;
animateToolSettings: boolean;
useToolAsToolSettingsLabel: boolean;
toolbarOpacity: number;
}
/** used on first call of ConfigurableUiReducer */
const initialState: ConfigurableUiState = {
snapMode: SnapMode.NearestKeypoint as number,
toolPrompt: "",
theme: SYSTEM_PREFERRED_COLOR_THEME,
widgetOpacity: WIDGET_OPACITY_DEFAULT,
useDragInteraction: false,
showWidgetIcon: true,
autoCollapseUnpinnedPanels: false,
viewOverlayDisplay: true,
animateToolSettings: false,
useToolAsToolSettingsLabel: false,
toolbarOpacity: TOOLBAR_OPACITY_DEFAULT,
};
/** An object with a function that creates each ConfigurableUiReducer that can be handled by our reducer.
* @public
*/
export const ConfigurableUiActions = {
setSnapMode: (snapMode: number) =>
createAction(ConfigurableUiActionId.SetSnapMode, snapMode),
setTheme:
/**
* Use `UiFramework.setColorTheme` instead which is conveniently typed with available theme union.
* @param theme ThemeId
*/
(theme: string) => createAction(ConfigurableUiActionId.SetTheme, theme),
setToolPrompt:
// istanbul ignore next
(toolPrompt: string) =>
createAction(ConfigurableUiActionId.SetToolPrompt, toolPrompt),
setWidgetOpacity:
// istanbul ignore next
(opacity: number) =>
createAction(ConfigurableUiActionId.SetWidgetOpacity, opacity),
setDragInteraction: (dragInteraction: boolean) =>
createAction(ConfigurableUiActionId.SetDragInteraction, dragInteraction),
setShowWidgetIcon: (showWidgetIcon: boolean) =>
createAction(ConfigurableUiActionId.SetShowWidgetIcon, showWidgetIcon),
setAutoCollapseUnpinnedPanels: (autoCollapse: boolean) =>
createAction(
ConfigurableUiActionId.AutoCollapseUnpinnedPanels,
autoCollapse
),
setViewOverlayDisplay: (displayViewOverlay: boolean) =>
createAction(
ConfigurableUiActionId.SetViewOverlayDisplay,
displayViewOverlay
),
setAnimateToolSettings: (animateToolSettings: boolean) =>
createAction(
ConfigurableUiActionId.AnimateToolSettings,
animateToolSettings
),
setUseToolAsToolSettingsLabel: (useToolAsToolSettingsLabel: boolean) =>
createAction(
ConfigurableUiActionId.UseToolAsToolSettingsLabel,
useToolAsToolSettingsLabel
),
setToolbarOpacity: (opacity: number) =>
createAction(ConfigurableUiActionId.SetToolbarOpacity, opacity),
};
/** Union of ConfigurableUi Redux actions
* @public
*/
export type ConfigurableUiActionsUnion = ActionsUnion<
typeof ConfigurableUiActions
>;
/** Handles actions to update ConfigurableUiState.
* @public
*/
export function ConfigurableUiReducer(
state: ConfigurableUiState = initialState,
action: ConfigurableUiActionsUnion
): ConfigurableUiState {
const outState = state;
switch (action.type) {
case ConfigurableUiActionId.SetSnapMode: {
return { ...state, snapMode: action.payload };
}
case ConfigurableUiActionId.SetToolPrompt: {
return { ...state, toolPrompt: action.payload };
}
case ConfigurableUiActionId.SetTheme: {
return { ...state, theme: action.payload as ThemeId }; // Need to cast because of the (string & {}) trick.
}
case ConfigurableUiActionId.SetWidgetOpacity: {
return { ...state, widgetOpacity: action.payload };
}
case ConfigurableUiActionId.SetDragInteraction: {
return { ...state, useDragInteraction: action.payload };
}
case ConfigurableUiActionId.SetShowWidgetIcon: {
return { ...state, showWidgetIcon: action.payload };
}
case ConfigurableUiActionId.AutoCollapseUnpinnedPanels: {
return { ...state, autoCollapseUnpinnedPanels: action.payload };
}
case ConfigurableUiActionId.SetViewOverlayDisplay: {
return { ...state, viewOverlayDisplay: action.payload };
}
case ConfigurableUiActionId.AnimateToolSettings: {
return { ...state, animateToolSettings: action.payload };
}
case ConfigurableUiActionId.UseToolAsToolSettingsLabel: {
return { ...state, useToolAsToolSettingsLabel: action.payload };
}
case ConfigurableUiActionId.SetToolbarOpacity: {
return { ...state, toolbarOpacity: action.payload };
}
}
return outState;
}