-
Notifications
You must be signed in to change notification settings - Fork 120
/
layoutService.ts
177 lines (163 loc) · 5.38 KB
/
layoutService.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
172
173
174
175
176
177
import { container, singleton } from 'tsyringe';
import { Component } from 'mo/react';
import { ID_APP } from 'mo/common/id';
import {
ILayout,
Position,
LayoutModel,
MenuBarMode,
} from 'mo/model/workbench/layout';
export interface ILayoutService extends Component<ILayout> {
/**
* Get the container of the molecule
*/
readonly container: HTMLElement | null;
/**
* Toggle the visibility of menu bar, returns the status of menu bar's `hidden`
*/
toggleMenuBarVisibility(): boolean;
/**
* Toggle the visibility of side bar, returns the status of side bar's `hidden`
*/
toggleSidebarVisibility(): boolean;
/**
* Toggle the visibility of the panel, returns the status of panel's `hidden`
*/
togglePanelVisibility(): boolean;
/**
* Toggle the visibility of the activity bar, returns the status of activity bar's `hidden`
*/
toggleActivityBarVisibility(): boolean;
/**
* Toggle the visibility of the status bar, returns the status of status bar's `hidden`
*/
toggleStatusBarVisibility(): boolean;
/**
* Toggle the maximized status of the panel, returns the status of maximized panel
*/
togglePanelMaximized(): boolean;
/**
* Set the sizes between the side bar and main content area
* @param splitPanePos
*/
setPaneSize(splitPanePos: string[]): void;
/**
* Set the sizes between the editor and the panel
* @param horizontalSplitPanePos
*/
setHorizontalPaneSize(horizontalSplitPanePos: string[]): void;
/**
* Set the position of the side bar, default is in `left`
* @param position
* @unachieved
*/
setSideBarPosition(position: keyof typeof Position): void;
/**
* Set the mode of the MenuBar, default is `vertical`
* @param mode
* @unachieved
*/
setMenuBarMode(mode: keyof typeof MenuBarMode): void;
/**
* Get the mode of the MenuBar
*/
getMenuBarMode(): keyof typeof MenuBarMode;
/**
* Reset all layout data as default value
*/
reset(): void;
}
@singleton()
export class LayoutService
extends Component<ILayout>
implements ILayoutService {
protected state: ILayout;
private _container!: HTMLElement | null;
constructor() {
super();
this.state = container.resolve(LayoutModel);
}
public get container() {
if (!this._container) {
this._container = document.getElementById(ID_APP) || document.body;
}
return this._container;
}
public toggleMenuBarVisibility(): boolean {
const { menuBar } = this.state;
const wasHidden = menuBar.hidden;
this.setState({ menuBar: { ...menuBar, hidden: !wasHidden } });
return !wasHidden;
}
public togglePanelVisibility(): boolean {
const { panel } = this.state;
const wasHidden = panel.hidden;
this.setState({ panel: { ...panel, hidden: !wasHidden } });
return !wasHidden;
}
public toggleSidebarVisibility(): boolean {
const { sidebar } = this.state;
const wasHidden = sidebar.hidden;
this.setState({ sidebar: { ...sidebar, hidden: !wasHidden } });
return !wasHidden;
}
public toggleActivityBarVisibility(): boolean {
const { activityBar } = this.state;
const wasHidden = activityBar.hidden;
this.setState({ activityBar: { ...activityBar, hidden: !wasHidden } });
return !wasHidden;
}
public toggleStatusBarVisibility(): boolean {
const { statusBar } = this.state;
const wasHidden = statusBar.hidden;
this.setState({ statusBar: { ...statusBar, hidden: !wasHidden } });
return !wasHidden;
}
public setSideBarPosition(position: keyof typeof Position): void {
const { sidebar } = this.state;
const { position: prePos } = sidebar;
if (prePos !== position) {
this.setState({
sidebar: { position: position, hidden: false },
});
}
}
public togglePanelMaximized(): boolean {
const panelViewState = this.state.panel;
this.setState({
panel: {
...panelViewState,
panelMaximized: !panelViewState.panelMaximized,
},
});
return !panelViewState.panelMaximized;
}
public setPaneSize(splitPanePos: string[]): void {
this.setState({ splitPanePos });
}
public setHorizontalPaneSize(horizontalSplitPanePos: string[]): void {
this.setState({ horizontalSplitPanePos });
}
public setMenuBarMode(mode: keyof typeof MenuBarMode): void {
const { menuBar } = this.state;
const { mode: preMode } = menuBar;
if (preMode !== mode) {
this.setState({ menuBar: { ...menuBar, mode, hidden: false } });
}
}
public getMenuBarMode(): keyof typeof MenuBarMode {
const { menuBar } = this.state;
return menuBar.mode;
}
public reset() {
this.setState({
splitPanePos: ['300px', 'auto'],
horizontalSplitPanePos: ['70%', 'auto'],
activityBar: { hidden: false },
panel: { hidden: false, panelMaximized: false },
statusBar: { hidden: false },
sidebar: { hidden: false, position: Position.left },
menuBar: { hidden: false, mode: MenuBarMode.vertical },
});
}
}