跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

cursor.js

250 行 · 8.2 KB历史下载
1/*
2 * noVNC: HTML5 VNC client
3 * Copyright (C) 2019 The noVNC authors
4 * Licensed under MPL 2.0 or any later version (see LICENSE.txt)
5 */
6
7import { supportsCursorURIs, isTouchDevice } from './browser.js';
8
9const useFallback = !supportsCursorURIs || isTouchDevice;
10
11export default class Cursor {
12 constructor(fallback = 'none') {
13 this._target = null;
14 this._fallback = fallback === 'default' ? 'default' : 'none';
15
16 this._canvas = document.createElement('canvas');
17
18 if (useFallback) {
19 this._canvas.style.position = 'fixed';
20 this._canvas.style.zIndex = '65535';
21 this._canvas.style.pointerEvents = 'none';
22 // Safari on iOS can select the cursor image
23 // https://bugs.webkit.org/show_bug.cgi?id=249223
24 this._canvas.style.userSelect = 'none';
25 this._canvas.style.WebkitUserSelect = 'none';
26 // Can't use "display" because of Firefox bug #1445997
27 this._canvas.style.visibility = 'hidden';
28 }
29
30 this._position = { x: 0, y: 0 };
31 this._hotSpot = { x: 0, y: 0 };
32
33 this._eventHandlers = {
34 'mouseover': this._handleMouseOver.bind(this),
35 'mouseleave': this._handleMouseLeave.bind(this),
36 'mousemove': this._handleMouseMove.bind(this),
37 'mouseup': this._handleMouseUp.bind(this),
38 };
39 }
40
41 attach(target) {
42 if (this._target) {
43 this.detach();
44 }
45
46 this._target = target;
47
48 if (useFallback) {
49 document.body.appendChild(this._canvas);
50
51 const options = { capture: true, passive: true };
52 this._target.addEventListener('mouseover', this._eventHandlers.mouseover, options);
53 this._target.addEventListener('mouseleave', this._eventHandlers.mouseleave, options);
54 this._target.addEventListener('mousemove', this._eventHandlers.mousemove, options);
55 this._target.addEventListener('mouseup', this._eventHandlers.mouseup, options);
56 }
57
58 this.clear();
59 }
60
61 detach() {
62 if (!this._target) {
63 return;
64 }
65
66 if (useFallback) {
67 const options = { capture: true, passive: true };
68 this._target.removeEventListener('mouseover', this._eventHandlers.mouseover, options);
69 this._target.removeEventListener('mouseleave', this._eventHandlers.mouseleave, options);
70 this._target.removeEventListener('mousemove', this._eventHandlers.mousemove, options);
71 this._target.removeEventListener('mouseup', this._eventHandlers.mouseup, options);
72
73 if (document.contains(this._canvas)) {
74 document.body.removeChild(this._canvas);
75 }
76 }
77
78 this._target = null;
79 }
80
81 change(rgba, hotx, hoty, w, h) {
82 if ((w === 0) || (h === 0)) {
83 this.clear();
84 return;
85 }
86
87 this._position.x = this._position.x + this._hotSpot.x - hotx;
88 this._position.y = this._position.y + this._hotSpot.y - hoty;
89 this._hotSpot.x = hotx;
90 this._hotSpot.y = hoty;
91
92 let ctx = this._canvas.getContext('2d');
93
94 this._canvas.width = w;
95 this._canvas.height = h;
96
97 let img = new ImageData(new Uint8ClampedArray(rgba), w, h);
98 ctx.clearRect(0, 0, w, h);
99 ctx.putImageData(img, 0, 0);
100
101 if (useFallback) {
102 this._updatePosition();
103 } else {
104 let url = this._canvas.toDataURL();
105 this._target.style.cursor = 'url(' + url + ')' + hotx + ' ' + hoty + ', default';
106 }
107 }
108
109 clear() {
110 this._target.style.cursor = this._fallback;
111 this._canvas.width = 0;
112 this._canvas.height = 0;
113 this._position.x = this._position.x + this._hotSpot.x;
114 this._position.y = this._position.y + this._hotSpot.y;
115 this._hotSpot.x = 0;
116 this._hotSpot.y = 0;
117 }
118
119 // Mouse events might be emulated, this allows
120 // moving the cursor in such cases
121 move(clientX, clientY) {
122 if (!useFallback) {
123 return;
124 }
125 // clientX/clientY are relative the _visual viewport_,
126 // but our position is relative the _layout viewport_,
127 // so try to compensate when we can
128 if (window.visualViewport) {
129 this._position.x = clientX + window.visualViewport.offsetLeft;
130 this._position.y = clientY + window.visualViewport.offsetTop;
131 } else {
132 this._position.x = clientX;
133 this._position.y = clientY;
134 }
135 this._updatePosition();
136 let target = document.elementFromPoint(clientX, clientY);
137 this._updateVisibility(target);
138 }
139
140 _handleMouseOver(event) {
141 // This event could be because we're entering the target, or
142 // moving around amongst its sub elements. Let the move handler
143 // sort things out.
144 this._handleMouseMove(event);
145 }
146
147 _handleMouseLeave(event) {
148 // Check if we should show the cursor on the element we are leaving to
149 this._updateVisibility(event.relatedTarget);
150 }
151
152 _handleMouseMove(event) {
153 this._updateVisibility(event.target);
154
155 this._position.x = event.clientX - this._hotSpot.x;
156 this._position.y = event.clientY - this._hotSpot.y;
157
158 this._updatePosition();
159 }
160
161 _handleMouseUp(event) {
162 // We might get this event because of a drag operation that
163 // moved outside of the target. Check what's under the cursor
164 // now and adjust visibility based on that.
165 let target = document.elementFromPoint(event.clientX, event.clientY);
166 this._updateVisibility(target);
167
168 // Captures end with a mouseup but we can't know the event order of
169 // mouseup vs releaseCapture.
170 //
171 // In the cases when releaseCapture comes first, the code above is
172 // enough.
173 //
174 // In the cases when the mouseup comes first, we need wait for the
175 // browser to flush all events and then check again if the cursor
176 // should be visible.
177 if (this._captureIsActive()) {
178 window.setTimeout(() => {
179 // We might have detached at this point
180 if (!this._target) {
181 return;
182 }
183 // Refresh the target from elementFromPoint since queued events
184 // might have altered the DOM
185 target = document.elementFromPoint(event.clientX,
186 event.clientY);
187 this._updateVisibility(target);
188 }, 0);
189 }
190 }
191
192 _showCursor() {
193 if (this._canvas.style.visibility === 'hidden') {
194 this._canvas.style.visibility = '';
195 }
196 }
197
198 _hideCursor() {
199 if (this._canvas.style.visibility !== 'hidden') {
200 this._canvas.style.visibility = 'hidden';
201 }
202 }
203
204 // Should we currently display the cursor?
205 // (i.e. are we over the target, or a child of the target without a
206 // different cursor set)
207 _shouldShowCursor(target) {
208 if (!target) {
209 return false;
210 }
211 // Easy case
212 if (target === this._target) {
213 return true;
214 }
215 // Other part of the DOM?
216 if (!this._target.contains(target)) {
217 return false;
218 }
219 // Has the child its own cursor?
220 // FIXME: How can we tell that a sub element has an
221 // explicit "cursor: none;"?
222 if (window.getComputedStyle(target).cursor !== 'none') {
223 return false;
224 }
225 return true;
226 }
227
228 _updateVisibility(target) {
229 // When the cursor target has capture we want to show the cursor.
230 // So, if a capture is active - look at the captured element instead.
231 if (this._captureIsActive()) {
232 target = document.captureElement;
233 }
234 if (this._shouldShowCursor(target)) {
235 this._showCursor();
236 } else {
237 this._hideCursor();
238 }
239 }
240
241 _updatePosition() {
242 this._canvas.style.left = this._position.x + "px";
243 this._canvas.style.top = this._position.y + "px";
244 }
245
246 _captureIsActive() {
247 return document.captureElement &&
248 document.documentElement.contains(document.captureElement);
249 }
250}