Rewild Your Web
web
browser
dweb
1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3import { MenuBase, css, html } from "./menu_base.js";
4
5export class WebViewMenu extends MenuBase {
6 static properties = {
7 ...MenuBase.properties,
8 x: { type: Number },
9 y: { type: Number },
10 };
11
12 constructor() {
13 super();
14 this.x = 0;
15 this.y = 0;
16 this.handleKeyDown = this.handleKeyDown.bind(this);
17 }
18
19 disconnectedCallback() {
20 super.disconnectedCallback();
21 this.removeEventListeners();
22 }
23
24 updated(changedProperties) {
25 if (changedProperties.has("open")) {
26 if (this.open) {
27 document.addEventListener("keydown", this.handleKeyDown);
28 } else {
29 this.removeEventListeners();
30 }
31 }
32 }
33
34 removeEventListeners() {
35 document.removeEventListener("keydown", this.handleKeyDown);
36 }
37
38 handleKeyDown(e) {
39 if (e.key === "Escape") {
40 this.close();
41 }
42 }
43
44 handleBackdropClick(e) {
45 // Only close if the click was directly on the backdrop
46 if (e.target.classList.contains("backdrop")) {
47 this.close();
48 }
49 }
50
51 static styles = css`
52 @import url(//system.localhost:8888/webview_menu.css);
53 `;
54
55 render() {
56 // Position the menu so its right edge aligns with x, top at y
57 const menuStyle = `right: calc(100% - ${this.x}px); top: ${this.y}px;`;
58
59 return html`
60 <div class="backdrop" @click=${this.handleBackdropClick}></div>
61 <div class="menu" style="${menuStyle}">
62 <div
63 class="menu-item"
64 @click=${() => this.handleItemClick("split-horizontal")}
65 >
66 <lucide-icon name="square-split-horizontal"></lucide-icon>
67 <span>Horizontal Split</span>
68 </div>
69 <div
70 class="menu-item"
71 @click=${() => this.handleItemClick("split-vertical")}
72 >
73 <lucide-icon name="square-split-vertical"></lucide-icon>
74 <span>Vertical Split</span>
75 </div>
76 <div
77 class="menu-item"
78 @click=${() => this.handleItemClick("reduce-size")}
79 >
80 <lucide-icon name="chevrons-right-left"></lucide-icon>
81 <span>Reduce Width</span>
82 </div>
83 <div
84 class="menu-item"
85 @click=${() => this.handleItemClick("increase-size")}
86 >
87 <lucide-icon name="chevrons-left-right"></lucide-icon>
88 <span>Increase Width</span>
89 </div>
90 <div class="menu-separator"></div>
91 <div class="menu-item" @click=${() => this.handleItemClick("zoom-in")}>
92 <lucide-icon name="zoom-in"></lucide-icon>
93 <span>Zoom In</span>
94 </div>
95 <div class="menu-item" @click=${() => this.handleItemClick("zoom-out")}>
96 <lucide-icon name="zoom-out"></lucide-icon>
97 <span>Zoom Out</span>
98 </div>
99 <div
100 class="menu-item"
101 @click=${() => this.handleItemClick("zoom-reset")}
102 >
103 <lucide-icon name="refresh-cw"></lucide-icon>
104 <span>Reset Zoom</span>
105 </div>
106 </div>
107 `;
108 }
109}
110
111customElements.define("webview-menu", WebViewMenu);