@@ -33,7 +33,7 @@ interface DragState {
3333
3434type MouseHandler = ( event : Event , mouse : Electron . MouseInputEvent ) => void ;
3535
36- export type DraggableWindow = BaseWindow & { __wdrag__ ?: Draggable } ;
36+ type DraggableWindow = BaseWindow & { __wdrag__ ?: Draggable } ;
3737
3838export class Draggable {
3939 private static readonly TRUE_PROMISE = Promise . resolve ( true ) ;
@@ -108,20 +108,22 @@ export class Draggable {
108108 }
109109
110110 /** Register a WebContents as a drag source for this window. */
111- public attach ( webContents : WebContents , overrideOptions ?: DragOptions ) : void {
111+ public attach ( webContents : WebContents , overrideOptions ?: DragOptions ) : this {
112112 if ( webContents . isDestroyed ( ) ) {
113113 throw new Error ( 'Cannot attach to destroyed WebContents' ) ;
114114 }
115115
116116 if ( this . optionsByWebContents . has ( webContents ) ) {
117117 overrideOptions && this . updateOptions ( webContents , overrideOptions ) ;
118- return ;
118+ return this ;
119119 }
120120
121121 let options : InternalDragOptions ;
122122 if ( overrideOptions ) {
123- Draggable . normalizeOptions ( overrideOptions ) ;
124- options = { ...this . options , ...overrideOptions } ;
123+ // Fix: Avoid changing the user-provided options object
124+ const newOptions = { ...overrideOptions } ;
125+ Draggable . normalizeOptions ( newOptions ) ;
126+ options = { ...this . options , ...newOptions } ;
125127 } else {
126128 options = { ...this . options } ;
127129 }
@@ -132,34 +134,40 @@ export class Draggable {
132134 this . optionsByWebContents . set ( webContents , options ) ;
133135 webContents . on ( 'before-mouse-event' , options . eventHandler ) ;
134136 webContents . once ( 'destroyed' , options . destroyListener ) ;
137+ return this ;
135138 }
136139
137140 /** Unregister a WebContents from dragging. */
138- public detach ( webContents : WebContents ) : void {
141+ public detach ( webContents : WebContents ) : this {
139142 const options = this . optionsByWebContents . get ( webContents ) ;
140143 if ( options ) {
141144 webContents . removeListener ( 'before-mouse-event' , options . eventHandler ! ) ;
142145 webContents . removeListener ( 'destroyed' , options . destroyListener ! ) ;
143146 this . optionsByWebContents . delete ( webContents ) ;
144147 }
148+ return this ;
145149 }
146150
147151 /** Unregister all WebContents from dragging. */
148- public detachAll ( ) : void {
152+ public detachAll ( ) : this {
149153 for ( const wc of this . optionsByWebContents . keys ( ) ) {
150154 this . detach ( wc ) ;
151155 }
156+ return this ;
152157 }
153158
154159 /**
155160 * Disable drag for all registered WebContents and release the window reference.
156161 * @remarks After calling this method, the instance is dead and must not be reused.
157162 */
158- public disable ( ) : void {
159- if ( ! this . window ) { return ; }
163+ public disable ( ) : this {
164+ if ( ! this . window ) { return this ; }
160165 this . detachAll ( ) ;
161- this . window . __wdrag__ = undefined ;
166+ if ( this . window . __wdrag__ === this ) {
167+ this . window . __wdrag__ = undefined ;
168+ }
162169 this . window = undefined ;
170+ return this ;
163171 }
164172
165173 /**
@@ -169,7 +177,7 @@ export class Draggable {
169177 * @example
170178 * drag.updateOptions({ fps: 120 });
171179 */
172- public updateOptions ( update : Partial < DragOptions > ) : void ;
180+ public updateOptions ( update : Partial < DragOptions > ) : this ;
173181
174182 /**
175183 * Update drag options for specific WebContents.
@@ -179,9 +187,9 @@ export class Draggable {
179187 * @example
180188 * drag.updateOptions(webContents, { fps: 120 });
181189 */
182- public updateOptions ( wc : WebContents , update : Partial < DragOptions > ) : void ;
190+ public updateOptions ( wc : WebContents , update : Partial < DragOptions > ) : this ;
183191
184- public updateOptions ( wcOrUpdate : WebContents | Partial < DragOptions > , update ?: Partial < DragOptions > ) : void {
192+ public updateOptions ( wcOrUpdate : WebContents | Partial < DragOptions > , update ?: Partial < DragOptions > ) : this {
185193 const newOptions = update ?? wcOrUpdate as Partial < DragOptions > ;
186194 Draggable . normalizeOptions ( newOptions ) ;
187195
@@ -193,14 +201,18 @@ export class Draggable {
193201 Object . assign ( options , newOptions ) ;
194202 }
195203 }
204+ return this ;
196205 }
197206
198207 /** Retarget the instance to a new window. */
199- public setWindow ( newWin : DraggableWindow ) : void {
200- this . window ! . __wdrag__ = undefined ; // Clear old reference
201-
202- this . window = newWin ;
203- this . window . __wdrag__ = this ; // Set new reference
208+ public setWindow ( newWindow : DraggableWindow ) : this {
209+ const oldWin = this . window ! ;
210+ this . window = newWindow ;
211+ if ( oldWin . __wdrag__ === this ) {
212+ oldWin . __wdrag__ = undefined ; // Clear old reference
213+ this . window . __wdrag__ = this ; // Set new reference
214+ }
215+ return this ;
204216 }
205217
206218 private createEventHandler ( wc : WebContents ) : MouseHandler {
0 commit comments