Skip to content

Commit

Permalink
fix(app): fix menu position not align with button
Browse files Browse the repository at this point in the history
  • Loading branch information
aidenlx committed Feb 4, 2024
1 parent 6d2b2c3 commit 9241eb2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
5 changes: 3 additions & 2 deletions apps/app/src/components/player/menus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from "@vidstack/react";
import { Menu } from "obsidian";
import { MoreIcon, SubtitlesIcon } from "@/components/icon";
import { showAtButton } from "@/lib/menu";
import {
useApp,
useIsEmbed,
Expand Down Expand Up @@ -38,7 +39,7 @@ export function Captions() {
item.setTitle(label).setChecked(selected).onClick(select);
});
});
menu.showAtMouseEvent(evt.nativeEvent);
showAtButton(evt.nativeEvent, menu);
evt.nativeEvent.stopImmediatePropagation();
}}
aria-label="Select Caption"
Expand Down Expand Up @@ -79,7 +80,7 @@ export function MoreOptions() {
},
isEmbed ? "player-menu-embed" : "player-menu-view",
);
menu.showAtMouseEvent(evt.nativeEvent);
showAtButton(evt.nativeEvent, menu);
evt.nativeEvent.stopImmediatePropagation();
}}
aria-label="More options"
Expand Down
24 changes: 24 additions & 0 deletions apps/app/src/lib/menu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { Menu } from "obsidian";

declare module "obsidian" {
interface MenuItem {
setSubmenu(): Menu;
}
interface Menu {
addSections(sections: string[]): void;
setParentElement(parent: HTMLElement): this;
}
}

export function showAtButton(evt: Event, menu: Menu) {
const target = evt.target;
if (!(target instanceof HTMLElement)) return;
const rect = target.getBoundingClientRect();
return menu.setParentElement(target).showAtPosition({
x: rect.x,
y: rect.bottom,
width: rect.width,
overlap: true,
left: true,
});
}
4 changes: 2 additions & 2 deletions apps/app/src/login/modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
DropdownComponent,
} from "obsidian";
import "./modal.global.less";
import { showAtButton } from "@/lib/menu";
import { getPartition } from "@/lib/remote-player/const";
import { webHostDisplayNameNoGeneric, webHostUrl } from "@/web/match-webpage";
import type { SupportedWebHostNoGeneric } from "@/web/match-webpage";
Expand Down Expand Up @@ -93,8 +94,7 @@ export class LoginModal extends Modal {
.onClick((e) => {
const menu = new Menu();
this.onMoreOptions(menu);
const bounding = (e.target as HTMLElement).getBoundingClientRect();
menu.showAtPosition({ x: bounding.right, y: bounding.bottom });
showAtButton(e, menu);
});

buildWelcome() {
Expand Down
3 changes: 0 additions & 3 deletions apps/app/src/media-view/menu/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ declare module "obsidian" {
leaf?: WorkspaceLeaf,
): void;
}
interface MenuItem {
setSubmenu(): Menu;
}
}

export default function registerMediaMenu(this: MxPlugin) {
Expand Down

0 comments on commit 9241eb2

Please sign in to comment.