Centralize menu button coordinate logic and replace popupwindows with dropdownmenu.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
const battery = await Service.import("battery");
|
||||
import { closeAllMenus } from "../index.js";
|
||||
import { openMenu } from "../utils.js";
|
||||
|
||||
const BatteryLabel = () => {
|
||||
const isVis = Variable(battery.available);
|
||||
@@ -54,14 +54,8 @@ const BatteryLabel = () => {
|
||||
}),
|
||||
isVis,
|
||||
props: {
|
||||
on_primary_click: (_, event) => {
|
||||
const clickPos = event.get_root_coords();
|
||||
const coords = [clickPos[1], clickPos[2]];
|
||||
|
||||
globalMousePos.value = coords;
|
||||
|
||||
closeAllMenus();
|
||||
App.toggleWindow("energymenu");
|
||||
on_primary_click: (clicked, event) => {
|
||||
openMenu(clicked, event, "energymenu");
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
const bluetooth = await Service.import('bluetooth')
|
||||
import { closeAllMenus } from "../index.js";
|
||||
import { openMenu } from "../utils.js";
|
||||
|
||||
const Bluetooth = () => {
|
||||
const btIcon = Widget.Label({
|
||||
@@ -19,14 +19,8 @@ const Bluetooth = () => {
|
||||
}),
|
||||
isVisible: true,
|
||||
props: {
|
||||
on_primary_click: (_, event) => {
|
||||
const clickPos = event.get_root_coords();
|
||||
const coords = [clickPos[1], clickPos[2]];
|
||||
|
||||
globalMousePos.value = coords;
|
||||
|
||||
closeAllMenus();
|
||||
App.toggleWindow("bluetoothmenu");
|
||||
on_primary_click: (clicked, event) => {
|
||||
openMenu(clicked, event, "bluetoothmenu");
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { closeAllMenus } from "../index.js";
|
||||
import { openMenu } from "../utils.js";
|
||||
|
||||
const date = Variable("", {
|
||||
poll: [1000, 'date "+ %a %b %d %I:%M:%S %p"'],
|
||||
@@ -12,9 +12,8 @@ const Clock = () => {
|
||||
}),
|
||||
isVisible: true,
|
||||
props: {
|
||||
on_primary_click: () => {
|
||||
closeAllMenus();
|
||||
App.toggleWindow("calendarmenu");
|
||||
on_primary_click: (clicked, event) => {
|
||||
openMenu(clicked, event, "calendarmenu");
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
@@ -12,14 +12,6 @@ import { SysTray } from "./systray/index.js";
|
||||
|
||||
import { BarItemBox } from "../shared/barItemBox.js";
|
||||
|
||||
const closeAllMenus = () => {
|
||||
App.closeWindow("bluetoothmenu");
|
||||
App.closeWindow("audiomenu");
|
||||
App.closeWindow("networkmenu");
|
||||
App.closeWindow("mediamenu");
|
||||
App.closeWindow("calendarmenu");
|
||||
}
|
||||
|
||||
// layout of the bar
|
||||
const Left = (monitor, wsMap) => {
|
||||
return Widget.Box({
|
||||
@@ -118,4 +110,4 @@ const BarAlt = (monitor = 0, wsMap) => {
|
||||
});
|
||||
};
|
||||
|
||||
export { Bar, BarAlt, closeAllMenus };
|
||||
export { Bar, BarAlt };
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
const mpris = await Service.import("mpris");
|
||||
import { closeAllMenus } from "../index.js";
|
||||
import { openMenu } from "../utils.js";
|
||||
|
||||
const Media = () => {
|
||||
const activePlayer = Variable(mpris.players[0]);
|
||||
@@ -90,14 +90,8 @@ const Media = () => {
|
||||
props: {
|
||||
on_scroll_up: () => mpris.getPlayer("")?.next(),
|
||||
on_scroll_down: () => mpris.getPlayer("")?.previous(),
|
||||
on_primary_click: (_, event) => {
|
||||
const clickPos = event.get_root_coords();
|
||||
const coords = [clickPos[1], clickPos[2]];
|
||||
|
||||
globalMousePos.value = coords;
|
||||
|
||||
closeAllMenus();
|
||||
App.toggleWindow("mediamenu");
|
||||
on_primary_click: (clicked, event) => {
|
||||
openMenu(clicked, event, "mediamenu");
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { closeAllMenus } from "../index.js";
|
||||
import { openMenu } from "../utils.js";
|
||||
|
||||
const Menu = () => {
|
||||
return {
|
||||
@@ -10,14 +10,8 @@ const Menu = () => {
|
||||
}),
|
||||
isVisible: true,
|
||||
props: {
|
||||
on_primary_click: (_, event) => {
|
||||
const clickPos = event.get_root_coords();
|
||||
const coords = [clickPos[1], clickPos[2]];
|
||||
|
||||
globalMousePos.value = coords;
|
||||
|
||||
closeAllMenus();
|
||||
App.toggleWindow("dashboardmenu");
|
||||
on_primary_click: (clicked, event) => {
|
||||
openMenu(clicked, event, "dashboardmenu");
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
const network = await Service.import("network");
|
||||
import { closeAllMenus } from "../index.js";
|
||||
import { openMenu } from "../utils.js";
|
||||
|
||||
import { globalMousePos } from "../../../globals.js";
|
||||
|
||||
@@ -31,14 +31,8 @@ const Network = () => {
|
||||
}),
|
||||
isVisible: true,
|
||||
props: {
|
||||
on_primary_click: (_, event) => {
|
||||
const clickPos = event.get_root_coords();
|
||||
const coords = [clickPos[1], clickPos[2]];
|
||||
|
||||
globalMousePos.value = coords;
|
||||
|
||||
closeAllMenus();
|
||||
App.toggleWindow("networkmenu");
|
||||
on_primary_click: (clicked, event) => {
|
||||
openMenu(clicked, event, "networkmenu");
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { openMenu } from "../utils.js";
|
||||
|
||||
const notifs = await Service.import("notifications");
|
||||
|
||||
export const Notifications = () => {
|
||||
@@ -29,7 +31,9 @@ export const Notifications = () => {
|
||||
}),
|
||||
isVisible: true,
|
||||
props: {
|
||||
on_primary_click: () => App.toggleWindow("notificationsmenu"),
|
||||
on_primary_click: (clicked, event) => {
|
||||
openMenu(clicked, event, "notificationsmenu");
|
||||
},
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
37
modules/bar/utils.js
Normal file
37
modules/bar/utils.js
Normal file
@@ -0,0 +1,37 @@
|
||||
export const closeAllMenus = () => {
|
||||
App.closeWindow("bluetoothmenu");
|
||||
App.closeWindow("audiomenu");
|
||||
App.closeWindow("networkmenu");
|
||||
App.closeWindow("mediamenu");
|
||||
App.closeWindow("calendarmenu");
|
||||
};
|
||||
|
||||
export const openMenu = (clicked, event, window) => {
|
||||
/*
|
||||
* NOTE: We have to make some adjustments so the menu pops up relatively
|
||||
* to the center of the button clicked. We don't want the menu to spawn
|
||||
* offcenter dependending on which edge of the button you click on.
|
||||
* -------------
|
||||
* To fix this, we take the x coordinate of the click within the icon's bounds.
|
||||
* So if you click the left edge of a 100width button then the x axis will be 0
|
||||
* and if you click the right edge then the x axis will be 100.
|
||||
* -------------
|
||||
* Then we divide the width of the button by 2 to get the center of the button and then get
|
||||
* the offset by subtracting the clicked x coordinate. Then we can apply that offset
|
||||
* to the x coordinate of the click relative to the screen to get the center of the
|
||||
* icon click.
|
||||
*/
|
||||
|
||||
const middleOfButton = Math.floor(clicked.get_allocated_width() / 2);
|
||||
const xAxisOfButtonClick = clicked.get_pointer()[0];
|
||||
const middleOffset = middleOfButton - xAxisOfButtonClick;
|
||||
|
||||
const clickPos = event.get_root_coords();
|
||||
const adjustedXCoord = clickPos[1] + middleOffset;
|
||||
const coords = [adjustedXCoord, clickPos[2]];
|
||||
|
||||
globalMousePos.value = coords;
|
||||
|
||||
closeAllMenus();
|
||||
App.toggleWindow(window);
|
||||
};
|
||||
@@ -1,5 +1,5 @@
|
||||
const audio = await Service.import("audio");
|
||||
import { closeAllMenus } from "../index.js";
|
||||
import { openMenu } from "../utils.js";
|
||||
|
||||
import { globalMousePos } from "../../../globals.js";
|
||||
|
||||
@@ -45,14 +45,8 @@ const Volume = () => {
|
||||
}),
|
||||
isVisible: true,
|
||||
props: {
|
||||
on_primary_click: (_, event) => {
|
||||
const clickPos = event.get_root_coords();
|
||||
const coords = [clickPos[1], clickPos[2]];
|
||||
|
||||
globalMousePos.value = coords;
|
||||
|
||||
closeAllMenus();
|
||||
App.toggleWindow("audiomenu");
|
||||
on_primary_click: (clicked, event) => {
|
||||
openMenu(clicked, event, "audiomenu");
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user