1414 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
1515 ********************************************************************************/
1616
17- import { CommandPalette } from '@eclipse-glsp/sprotty' ;
17+ import { Bounds , CommandPalette , getWindowScroll , GModelElement , GModelRoot } from '@eclipse-glsp/sprotty' ;
1818import { injectable } from 'inversify' ;
1919import { CSS_HIDDEN_EXTENSION_CLASS , CSS_UI_EXTENSION_CLASS } from '../../base/ui-extension/ui-extension' ;
20+ import { getElements , isSelectableAndBoundsAware } from '../../utils/gmodel-util' ;
2021
2122@injectable ( )
2223export class GlspCommandPalette extends CommandPalette {
2324 protected override initializeContents ( containerElement : HTMLElement ) : void {
2425 super . initializeContents ( containerElement ) ;
26+ containerElement . style . position = '' ;
2527 containerElement . classList . add ( CSS_UI_EXTENSION_CLASS ) ;
2628 }
2729
@@ -32,4 +34,42 @@ export class GlspCommandPalette extends CommandPalette {
3234 this . containerElement ?. classList . add ( CSS_HIDDEN_EXTENSION_CLASS ) ;
3335 }
3436 }
37+
38+ protected override onBeforeShow ( containerElement : HTMLElement , root : Readonly < GModelRoot > , ...selectedElementIds : string [ ] ) : void {
39+ let x = this . xOffset ;
40+ let y = this . yOffset ;
41+
42+ const selectedElements = getElements ( root . index , selectedElementIds , isSelectableAndBoundsAware ) ;
43+ if ( selectedElements . length === 1 ) {
44+ const bounds = this . getAbsoluteBounds ( selectedElements [ 0 ] ) ;
45+ x += bounds . x + bounds . width ;
46+ y += bounds . y ;
47+ } else {
48+ const bounds = this . getAbsoluteBounds ( root ) ;
49+ x += bounds . x ;
50+ y += bounds . y ;
51+ }
52+ containerElement . style . left = `${ x } px` ;
53+ containerElement . style . top = `${ y } px` ;
54+ containerElement . style . width = `${ this . defaultWidth } px` ;
55+ }
56+
57+ protected getAbsoluteBounds ( element : Readonly < GModelElement > ) : Bounds {
58+ let x = 0 ;
59+ let y = 0 ;
60+ let width = 0 ;
61+ let height = 0 ;
62+
63+ const svgElementId = this . domHelper . createUniqueDOMElementId ( element ) ;
64+ const svgElement = document . getElementById ( svgElementId ) ;
65+ if ( svgElement ) {
66+ const rect = svgElement . getBoundingClientRect ( ) ;
67+ const scroll = getWindowScroll ( ) ;
68+ x = rect . left + scroll . x ;
69+ y = rect . top + scroll . y ;
70+ width = rect . width ;
71+ height = rect . height ;
72+ }
73+ return { x, y, width, height } ;
74+ }
3575}
0 commit comments