Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 1 addition & 16 deletions src/components/bar/utils/monitors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
48 changes: 48 additions & 0 deletions src/services/display/monitor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ export class GdkMonitorService {
* @returns The corresponding Hyprland monitor id.
*/
public mapGdkToHyprland(monitor: number): number {
const monitorMappings = this.getMonitorMappings();

for (const monitorMapping of monitorMappings) {
if (monitorMapping.gdkIndex === monitor) {
return monitorMapping.hyprlandId;
}
}

const gdkMonitors = this._getGdkMonitors();

if (Object.keys(gdkMonitors).length === 0) {
Expand Down Expand Up @@ -79,6 +87,14 @@ export class GdkMonitorService {
* @returns The corresponding GDK monitor id.
*/
public mapHyprlandToGdk(monitor: number): number {
const monitorMappings = this.getMonitorMappings();

for (var monitorMapping of monitorMappings) {
if (monitorMapping.hyprlandId === monitor) {
return monitorMapping.gdkIndex;
}
}

const gdkMonitors = this._getGdkMonitors();
const gdkCandidates = Object.entries(gdkMonitors).map(([monitorId, monitorMetadata]) => ({
id: Number(monitorId),
Expand All @@ -105,6 +121,38 @@ export class GdkMonitorService {
);
}

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();
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;
}

/**
* Generic helper that finds the best matching candidate monitor based on:
* 1. A direct match (candidate matches the source and has the same id as the target, and hasn't been used).
Expand Down