aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/AntimodeMenu.tsx
blob: 16e76694da197e850c9723374de81ac45058c7a1 (plain)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import React = require('react');
import { observable, action, runInAction } from 'mobx';
import './AntimodeMenu.scss';
import { StrCast } from '../../fields/Types';
import { Doc } from '../../fields/Doc';
import { SettingsManager } from '../util/SettingsManager';
export interface AntimodeMenuProps {}

/**
 * This is an abstract class that serves as the base for a PDF-style or Marquee-style
 * menu. To use this class, look at PDFMenu.tsx or MarqueeOptionsMenu.tsx for an example.
 */
export abstract class AntimodeMenu<T extends AntimodeMenuProps> extends React.Component<T, {}> {
    protected _offsetY: number = 0;
    protected _offsetX: number = 0;
    protected _mainCont: React.RefObject<HTMLDivElement> = React.createRef();
    protected _dragging: boolean = false;

    @observable protected _top: number = -300;
    @observable protected _left: number = -300;
    @observable protected _opacity: number = 0;
    @observable protected _transitionProperty: string = 'opacity';
    @observable protected _transitionDuration: string = '0.5s';
    @observable protected _transitionDelay: string = '';
    @observable protected _canFade: boolean = false;

    @observable public Pinned: boolean = false;

    get width() {
        return this._mainCont.current ? this._mainCont.current.getBoundingClientRect().width : 0;
    }
    get height() {
        return this._mainCont.current ? this._mainCont.current.getBoundingClientRect().height : 0;
    }

    @action
    /**
     * @param x
     * @param y
     * @param forceJump: If the menu is pinned down, do you want to force it to jump to the new location?
     * Called when you want the menu to show up at a location
     */
    public jumpTo = (x: number, y: number, forceJump: boolean = false) => {
        if (!this.Pinned || forceJump) {
            this._transitionProperty = this._transitionDuration = this._transitionDelay = '';
            this._opacity = 1;
            this._left = x;
            this._top = y;
        }
    };

    @action
    /**
     * @param forceOut: Do you want the menu to disappear immediately or to slowly fadeout?
     * Called when you want the menu to disappear
     */
    public fadeOut = (forceOut: boolean) => {
        if (!this.Pinned) {
            if (this._opacity === 0.2) {
                this._transitionProperty = 'opacity';
                this._transitionDuration = '0.1s';
            }

            if (forceOut) {
                this._transitionProperty = '';
                this._transitionDuration = '';
            }
            this._transitionDelay = '';
            this._opacity = 0;
            this._left = this._top = -300;
        }
    };

    @action
    protected pointerLeave = (e: React.PointerEvent) => {
        if (!this.Pinned && this._canFade) {
            this._transitionProperty = 'opacity';
            this._transitionDuration = '0.5s';
            this._transitionDelay = '1s';
            this._opacity = 0.2;
            setTimeout(() => this.fadeOut(false), 3000);
        }
    };

    @action
    protected pointerEntered = (e: React.PointerEvent) => {
        this._transitionProperty = 'opacity';
        this._transitionDuration = '0.1s';
        this._transitionDelay = '';
        this._opacity = 1;
    };

    @action
    protected togglePin = (e: React.MouseEvent) => {
        runInAction(() => (this.Pinned = !this.Pinned));
    };

    protected dragStart = (e: React.PointerEvent) => {
        document.removeEventListener('pointermove', this.dragging);
        document.addEventListener('pointermove', this.dragging);
        document.removeEventListener('pointerup', this.dragEnd);
        document.addEventListener('pointerup', this.dragEnd);

        this._offsetX = e.pageX - this._mainCont.current!.getBoundingClientRect().left;
        this._offsetY = e.pageY - this._mainCont.current!.getBoundingClientRect().top;

        e.stopPropagation();
        e.preventDefault();
    };

    @action
    protected dragging = (e: PointerEvent) => {
        const width = this._mainCont.current!.getBoundingClientRect().width;
        const height = this._mainCont.current!.getBoundingClientRect().height;

        const left = e.pageX - this._offsetX;
        const top = e.pageY - this._offsetY;

        this._left = Math.min(Math.max(left, 0), window.innerWidth - width);
        this._top = Math.min(Math.max(top, 0), window.innerHeight - height);

        e.stopPropagation();
        e.preventDefault();
    };

    protected dragEnd = (e: PointerEvent) => {
        document.removeEventListener('pointermove', this.dragging);
        document.removeEventListener('pointerup', this.dragEnd);
        e.stopPropagation();
        e.preventDefault();
    };

    protected handleContextMenu = (e: React.MouseEvent) => {
        e.stopPropagation();
        e.preventDefault();
    };

    protected getDragger = () => {
        return <div className="antimodeMenu-dragger" key="dragger" onPointerDown={this.dragStart} style={{ width: '20px' }} />;
    };

    protected getElement(buttons: JSX.Element) {
        return (
            <div
                className="antimodeMenu-cont"
                onPointerLeave={this.pointerLeave}
                onPointerEnter={this.pointerEntered}
                ref={this._mainCont}
                onContextMenu={this.handleContextMenu}
                style={{
                    left: this._left,
                    top: this._top,
                    opacity: this._opacity,
                    background: SettingsManager.userBackgroundColor,
                    transitionProperty: this._transitionProperty,
                    transitionDuration: this._transitionDuration,
                    transitionDelay: this._transitionDelay,
                    position: this.Pinned ? 'unset' : undefined,
                }}>
                {buttons}
            </div>
        );
    }

    protected getElementVert(buttons: JSX.Element[]) {
        return (
            <div
                className="antimodeMenu-cont"
                onPointerLeave={this.pointerLeave}
                onPointerEnter={this.pointerEntered}
                ref={this._mainCont}
                onContextMenu={this.handleContextMenu}
                style={{
                    left: this.Pinned ? undefined : this._left,
                    top: this.Pinned ? 0 : this._top,
                    right: this.Pinned ? 0 : undefined,
                    height: 'inherit',
                    width: 200,
                    opacity: this._opacity,
                    background: SettingsManager.userBackgroundColor,
                    transitionProperty: this._transitionProperty,
                    transitionDuration: this._transitionDuration,
                    transitionDelay: this._transitionDelay,
                    position: this.Pinned ? 'absolute' : undefined,
                }}>
                {buttons}
            </div>
        );
    }

    protected getElementWithRows(rows: JSX.Element[], numRows: number, hasDragger: boolean = true) {
        return (
            <div
                className="antimodeMenu-cont with-rows"
                onPointerLeave={this.pointerLeave}
                onPointerEnter={this.pointerEntered}
                ref={this._mainCont}
                onContextMenu={this.handleContextMenu}
                style={{
                    left: this._left,
                    top: this._top,
                    opacity: this._opacity,
                    background: SettingsManager.userBackgroundColor,
                    transitionProperty: this._transitionProperty,
                    transitionDuration: this._transitionDuration,
                    transitionDelay: this._transitionDelay,
                    height: 'auto',
                    flexDirection: this.Pinned ? 'row' : undefined,
                    position: this.Pinned ? 'unset' : undefined,
                }}>
                {hasDragger ? <div className="antimodeMenu-dragger" onPointerDown={this.dragStart} style={{ width: '20px' }} /> : null}
                {rows}
            </div>
        );
    }
}