Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/test/simulation/fixtures/edit-add-toString2/index.ts
13399 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { IBoundarySashes, ISashEvent, Orientation, Sash, SashState } from 'vs/base/browser/ui/sash/sash';
7
import { Disposable } from 'vs/base/common/lifecycle';
8
import { IObservable, IReader, autorun, derived, observableValue } from 'vs/base/common/observable';
9
import { DiffEditorOptions } from './diffEditorOptions';
10
11
export class DiffEditorSash extends Disposable {
12
private readonly _sashRatio = observableValue<number | undefined>(this, undefined);
13
14
public readonly sashLeft = derived(this, reader => {
15
const ratio = this._sashRatio.read(reader) ?? this._options.splitViewDefaultRatio.read(reader);
16
return this._computeSashLeft(ratio, reader);
17
});
18
19
private readonly _sash = this._register(new Sash(this._domNode, {
20
getVerticalSashTop: (_sash: Sash): number => 0,
21
getVerticalSashLeft: (_sash: Sash): number => this.sashLeft.get(),
22
getVerticalSashHeight: (_sash: Sash): number => this._dimensions.height.get(),
23
}, { orientation: Orientation.VERTICAL }));
24
25
private _startSashPosition: number | undefined = undefined;
26
27
constructor(
28
private readonly _options: DiffEditorOptions,
29
private readonly _domNode: HTMLElement,
30
private readonly _dimensions: { height: IObservable<number>; width: IObservable<number> },
31
) {
32
super();
33
34
this._register(this._sash.onDidStart(() => {
35
this._startSashPosition = this.sashLeft.get();
36
}));
37
this._register(this._sash.onDidChange((e: ISashEvent) => {
38
const contentWidth = this._dimensions.width.get();
39
const sashPosition = this._computeSashLeft((this._startSashPosition! + (e.currentX - e.startX)) / contentWidth, undefined);
40
this._sashRatio.set(sashPosition / contentWidth, undefined);
41
}));
42
this._register(this._sash.onDidEnd(() => this._sash.layout()));
43
this._register(this._sash.onDidReset(() => this._sashRatio.set(undefined, undefined)));
44
45
this._register(autorun(reader => {
46
/** @description update sash layout */
47
const enabled = this._options.enableSplitViewResizing.read(reader);
48
this._sash.state = enabled ? SashState.Enabled : SashState.Disabled;
49
this.sashLeft.read(reader);
50
this._dimensions.height.read(reader);
51
this._sash.layout();
52
}));
53
}
54
55
56
57
setBoundarySashes(sashes: IBoundarySashes): void {
58
this._sash.orthogonalEndSash = sashes.bottom;
59
}
60
61
/** @pure */
62
private _computeSashLeft(desiredRatio: number, reader: IReader | undefined): number {
63
const contentWidth = this._dimensions.width.read(reader);
64
const midPoint = Math.floor(this._options.splitViewDefaultRatio.read(reader) * contentWidth);
65
const sashLeft = this._options.enableSplitViewResizing.read(reader) ? Math.floor(desiredRatio * contentWidth) : midPoint;
66
67
const MINIMUM_EDITOR_WIDTH = 100;
68
if (contentWidth <= MINIMUM_EDITOR_WIDTH * 2) {
69
return midPoint;
70
}
71
if (sashLeft < MINIMUM_EDITOR_WIDTH) {
72
return MINIMUM_EDITOR_WIDTH;
73
}
74
if (sashLeft > contentWidth - MINIMUM_EDITOR_WIDTH) {
75
return contentWidth - MINIMUM_EDITOR_WIDTH;
76
}
77
return sashLeft;
78
}
79
}
80
81