From d805c0f211f7305bf020a98e027f2d9dedfddc65 Mon Sep 17 00:00:00 2001 From: randomthought Date: Fri, 6 Jun 2025 06:17:28 -0500 Subject: [PATCH 1/2] Using monitor x,y values from hyprland to get gdk monitor --- src/components/bar/utils/monitors/index.ts | 17 +---- src/services/display/monitor/index.ts | 85 ++++++++++------------ 2 files changed, 41 insertions(+), 61 deletions(-) diff --git a/src/components/bar/utils/monitors/index.ts b/src/components/bar/utils/monitors/index.ts index 3c2092dd..6b6d5b4a 100644 --- a/src/components/bar/utils/monitors/index.ts +++ b/src/components/bar/utils/monitors/index.ts @@ -59,24 +59,9 @@ export async function forMonitors( return []; } - const monitorCount = display.get_n_monitors(); const gdkMonitorService = GdkMonitorService.getInstance(); - const monitorMappings: MonitorMapping[] = []; + const monitorMappings: MonitorMapping[] = gdkMonitorService.getMonitorMappings(); - for (let gdkMonitorIndex = 0; gdkMonitorIndex < monitorCount; gdkMonitorIndex++) { - const monitor = display.get_monitor(gdkMonitorIndex); - if (monitor === null) { - console.warn(`[forMonitors] Skipping invalid monitor at index ${gdkMonitorIndex}`); - continue; - } - - const hyprlandId = gdkMonitorService.mapGdkToHyprland(gdkMonitorIndex); - - monitorMappings.push({ - gdkIndex: gdkMonitorIndex, - hyprlandId, - }); - } const monitorPromises = monitorMappings.map(async ({ gdkIndex, hyprlandId }) => { try { diff --git a/src/services/display/monitor/index.ts b/src/services/display/monitor/index.ts index 156d13cc..226be27c 100644 --- a/src/services/display/monitor/index.ts +++ b/src/services/display/monitor/index.ts @@ -44,32 +44,13 @@ export class GdkMonitorService { * @returns The corresponding Hyprland monitor id. */ public mapGdkToHyprland(monitor: number): number { - const gdkMonitors = this._getGdkMonitors(); + const monitorMappings = this.getMonitorMappings(); - if (Object.keys(gdkMonitors).length === 0) { - return monitor; - } - - const gdkMonitor = gdkMonitors[monitor]; - if (!gdkMonitor) { - return monitor; + for (const monitorMapping of monitorMappings) { + if (monitorMapping.gdkIndex === monitor) { + return monitorMapping.hyprlandId; + } } - - const hyprlandMonitors = hyprlandService.get_monitors(); - const validMonitors = hyprlandMonitors.filter((m) => m.model && m.model !== 'null'); - const tempUsedIds = new Set(); - const monitorsToUse = validMonitors.length > 0 ? validMonitors : hyprlandMonitors; - - const result = this._matchMonitor( - monitorsToUse, - gdkMonitor, - monitor, - (mon) => mon.id, - (mon, gdkMon) => this._matchMonitorKey(mon, gdkMon), - tempUsedIds, - ); - - return result; } /** @@ -79,30 +60,44 @@ export class GdkMonitorService { * @returns The corresponding GDK monitor id. */ public mapHyprlandToGdk(monitor: number): number { - const gdkMonitors = this._getGdkMonitors(); - const gdkCandidates = Object.entries(gdkMonitors).map(([monitorId, monitorMetadata]) => ({ - id: Number(monitorId), - monitor: monitorMetadata, - })); - - if (gdkCandidates.length === 0) { - return monitor; + const monitorMappings = this.getMonitorMappings(); + + for (var monitorMapping of monitorMappings) { + if (monitorMapping.hyprlandId === monitor) { + return monitorMapping.gdkIndex; + } } + } + + public getMonitorMappings(): MonitorMapping[] { + const display = Gdk.Display.get_default(); + const monitorCount = display.get_n_monitors(); + + const x : IHash = {}; + + for (let gdkMonitorIndex = 0; gdkMonitorIndex < monitorCount; gdkMonitorIndex++) { + const monitor = display.get_monitor(gdkMonitorIndex); + if (monitor === null) { + console.warn(`[forMonitors] Skipping invalid monitor at index ${gdkMonitorIndex}`); + continue; + } + x[monitor] = gdkMonitorIndex; + } + + const monitorMappings: MonitorMapping[] = []; const hyprlandMonitors = hyprlandService.get_monitors(); - const foundHyprlandMonitor = - hyprlandMonitors.find((mon) => mon.id === monitor) || hyprlandMonitors[0]; - - const tempUsedIds = new Set(); - - return this._matchMonitor( - gdkCandidates, - foundHyprlandMonitor, - monitor, - (candidate) => candidate.id, - (candidate, hyprlandMonitor) => this._matchMonitorKey(hyprlandMonitor, candidate.monitor), - tempUsedIds, - ); + for (let i = 0; i < monitorCount; i++) { + const gdkMonitor = display.get_monitor_at_point(hyprlandMonitors[i].x, hyprlandMonitors[i].y); + monitorMappings.push({ + gdkIndex: x[gdkMonitor], + hyprlandId: hyprlandMonitors[i].id, + }); + } + + // console.log("monitorMappings ", monitorMappings); + + return monitorMappings; } /** From ac3287ce5321880a476c5c35f025bdf802572a14 Mon Sep 17 00:00:00 2001 From: randomthought Date: Fri, 6 Jun 2025 06:29:27 -0500 Subject: [PATCH 2/2] use fall back instead of removing code --- src/services/display/monitor/index.ts | 53 +++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/services/display/monitor/index.ts b/src/services/display/monitor/index.ts index 226be27c..4bb7fac5 100644 --- a/src/services/display/monitor/index.ts +++ b/src/services/display/monitor/index.ts @@ -51,6 +51,33 @@ export class GdkMonitorService { return monitorMapping.hyprlandId; } } + + const gdkMonitors = this._getGdkMonitors(); + + if (Object.keys(gdkMonitors).length === 0) { + return monitor; + } + + const gdkMonitor = gdkMonitors[monitor]; + if (!gdkMonitor) { + return monitor; + } + + const hyprlandMonitors = hyprlandService.get_monitors(); + const validMonitors = hyprlandMonitors.filter((m) => m.model && m.model !== 'null'); + const tempUsedIds = new Set(); + const monitorsToUse = validMonitors.length > 0 ? validMonitors : hyprlandMonitors; + + const result = this._matchMonitor( + monitorsToUse, + gdkMonitor, + monitor, + (mon) => mon.id, + (mon, gdkMon) => this._matchMonitorKey(mon, gdkMon), + tempUsedIds, + ); + + return result; } /** @@ -67,12 +94,38 @@ export class GdkMonitorService { return monitorMapping.gdkIndex; } } + + const gdkMonitors = this._getGdkMonitors(); + const gdkCandidates = Object.entries(gdkMonitors).map(([monitorId, monitorMetadata]) => ({ + id: Number(monitorId), + monitor: monitorMetadata, + })); + + if (gdkCandidates.length === 0) { + return monitor; + } + + const hyprlandMonitors = hyprlandService.get_monitors(); + const foundHyprlandMonitor = + hyprlandMonitors.find((mon) => mon.id === monitor) || hyprlandMonitors[0]; + + const tempUsedIds = new Set(); + + return this._matchMonitor( + gdkCandidates, + foundHyprlandMonitor, + monitor, + (candidate) => candidate.id, + (candidate, hyprlandMonitor) => this._matchMonitorKey(hyprlandMonitor, candidate.monitor), + tempUsedIds, + ); } public getMonitorMappings(): MonitorMapping[] { const display = Gdk.Display.get_default(); const monitorCount = display.get_n_monitors(); + const x : IHash = {}; for (let gdkMonitorIndex = 0; gdkMonitorIndex < monitorCount; gdkMonitorIndex++) {