current-screen-detector: more reliable no longer assumes screens[0] has a bar.

This commit is contained in:
Lemmy
2026-01-30 19:42:10 -05:00
parent 817b81e020
commit 75243adb0d
+110 -81
View File
@@ -37,95 +37,124 @@ Item {
}
/**
* Execute callback with the screen where the cursor currently is.
* On single-monitor setups, executes immediately.
* On multi-monitor setups, briefly opens an invisible window to detect the screen.
* Find a fallback screen that has a bar configured.
* Prioritizes the screen at position 0x0 (likely the primary screen).
*/
function withCurrentScreen(callback: var): void {
if (root.pendingCallback) {
Logger.w("CurrentScreenDetector", "Another detection is pending, ignoring new call");
return;
}
function findScreenWithBar(): var {
const monitors = Settings.data.bar.monitors || [];
let primaryCandidate = null;
let firstWithBar = null;
// Single monitor setup can execute immediately
if (Quickshell.screens.length === 1) {
callback(Quickshell.screens[0]);
return;
}
for (let i = 0; i < Quickshell.screens.length; i++) {
const s = Quickshell.screens[i];
const hasBar = monitors.length === 0 || monitors.includes(s.name);
// Try compositor-specific focused monitor detection first
let screen = CompositorService.getFocusedScreen();
if (hasBar) {
// Check if this is at 0x0 (primary position)
if (s.x === 0 && s.y === 0) {
primaryCandidate = s;
}
// Track first screen with bar as fallback
if (!firstWithBar) {
firstWithBar = s;
}
}
}
if (screen) {
// Apply the bar check if configured
if (!Settings.data.general.allowPanelsOnScreenWithoutBar) {
const monitors = Settings.data.bar.monitors || [];
const hasBar = monitors.length === 0 || monitors.includes(screen.name);
if (!hasBar) {
screen = Quickshell.screens[0];
}
}
Logger.d("CurrentScreenDetector", "Using compositor-detected screen:", screen.name);
callback(screen);
// Prefer primary (0x0), then first with bar, then just first screen
return primaryCandidate || firstWithBar || Quickshell.screens[0];
}
/**
* Execute callback with the screen where the cursor currently is.
* On single-monitor setups, executes immediately.
* On multi-monitor setups, briefly opens an invisible window to detect the screen.
*/
function withCurrentScreen(callback: var) {
if (root.pendingCallback) {
Logger.w("CurrentScreenDetector", "Another detection is pending, ignoring new call");
return;
}
// Fallback: Multi-monitor setup needs async detection via invisible PanelWindow
root.detectedScreen = null;
root.pendingCallback = callback;
screenDetectorLoader.active = true;
}
// Single monitor setup can execute immediately
if (Quickshell.screens.length === 1) {
callback(Quickshell.screens[0]);
return;
}
Timer {
id: screenDetectorDebounce
running: false
interval: 40
onTriggered: {
Logger.d("CurrentScreenDetector", "Screen debounced to:", root.detectedScreen?.name || "null");
// Try compositor-specific focused monitor detection first
let screen = CompositorService.getFocusedScreen();
// Execute pending callback if any
if (root.pendingCallback) {
if (!Settings.data.general.allowPanelsOnScreenWithoutBar) {
// If we explicitly disabled panels on screen without bar, check if bar is configured
// for this screen, and fallback to primary screen if necessary
var monitors = Settings.data.bar.monitors || [];
const hasBar = monitors.length === 0 || monitors.includes(root.detectedScreen?.name);
if (!hasBar) {
root.detectedScreen = Quickshell.screens[0];
if (screen) {
// Apply the bar check if configured
if (!Settings.data.general.allowPanelsOnScreenWithoutBar) {
const monitors = Settings.data.bar.monitors || [];
const hasBar = monitors.length === 0 || monitors.includes(screen.name);
if (!hasBar) {
screen = findScreenWithBar();
}
}
Logger.d("CurrentScreenDetector", "Using compositor-detected screen:", screen.name);
callback(screen);
return;
}
// Fallback: Multi-monitor setup needs async detection via invisible PanelWindow
root.detectedScreen = null;
root.pendingCallback = callback;
screenDetectorLoader.active = true;
}
Timer {
id: screenDetectorDebounce
running: false
interval: 40
onTriggered: {
Logger.d("CurrentScreenDetector", "Screen debounced to:", root.detectedScreen?.name || "null");
// Execute pending callback if any
if (root.pendingCallback) {
if (!Settings.data.general.allowPanelsOnScreenWithoutBar) {
// If we explicitly disabled panels on screen without bar, check if bar is configured
// for this screen, and fallback to primary screen if necessary
var monitors = Settings.data.bar.monitors || [];
const hasBar = monitors.length === 0 || monitors.includes(root.detectedScreen?.name);
if (!hasBar) {
root.detectedScreen = findScreenWithBar();
}
}
Logger.d("CurrentScreenDetector", "Executing callback on screen:", root.detectedScreen.name);
// Store callback locally and clear pendingCallback first to prevent deadlock
// if the callback throws an error
var callback = root.pendingCallback;
root.pendingCallback = null;
try {
callback(root.detectedScreen);
} catch (e) {
Logger.e("CurrentScreenDetector", "Callback failed:", e);
}
}
// Clean up
screenDetectorLoader.active = false;
}
}
// Invisible dummy PanelWindow to detect which screen should receive the action
Loader {
id: screenDetectorLoader
active: false
sourceComponent: PanelWindow {
implicitWidth: 0
implicitHeight: 0
color: "transparent"
WlrLayershell.exclusionMode: ExclusionMode.Ignore
WlrLayershell.namespace: "noctalia-screen-detector"
mask: Region {}
onScreenChanged: root.screenDetected(screen)
}
}
}
Logger.d("CurrentScreenDetector", "Executing callback on screen:", root.detectedScreen.name);
// Store callback locally and clear pendingCallback first to prevent deadlock
// if the callback throws an error
var callback = root.pendingCallback;
root.pendingCallback = null;
try {
callback(root.detectedScreen);
} catch (e) {
Logger.e("CurrentScreenDetector", "Callback failed:", e);
}
}
// Clean up
screenDetectorLoader.active = false;
}
}
// Invisible dummy PanelWindow to detect which screen should receive the action
Loader {
id: screenDetectorLoader
active: false
sourceComponent: PanelWindow {
implicitWidth: 0
implicitHeight: 0
color: "transparent"
WlrLayershell.exclusionMode: ExclusionMode.Ignore
WlrLayershell.namespace: "noctalia-screen-detector"
mask: Region {}
onScreenChanged: root.screenDetected(screen)
}
}
}