formatter
This commit is contained in:
+180
-176
@@ -8,219 +8,223 @@ import qs.Config
|
||||
import qs.Components
|
||||
|
||||
Singleton {
|
||||
id: root
|
||||
id: root
|
||||
|
||||
property list<var> ddcMonitors: []
|
||||
readonly property list<Monitor> monitors: variants.instances
|
||||
property bool appleDisplayPresent: false
|
||||
property bool appleDisplayPresent: false
|
||||
property list<var> ddcMonitors: []
|
||||
readonly property list<Monitor> monitors: variants.instances
|
||||
|
||||
function getMonitorForScreen(screen: ShellScreen): var {
|
||||
return monitors.find(m => m.modelData === screen);
|
||||
}
|
||||
function decreaseBrightness(): void {
|
||||
const monitor = getMonitor("active");
|
||||
if (monitor)
|
||||
monitor.setBrightness(monitor.brightness - Config.services.brightnessIncrement);
|
||||
}
|
||||
|
||||
function getMonitor(query: string): var {
|
||||
if (query === "active") {
|
||||
return monitors.find(m => Hypr.monitorFor(m.modelData)?.focused);
|
||||
}
|
||||
function getMonitor(query: string): var {
|
||||
if (query === "active") {
|
||||
return monitors.find(m => Hypr.monitorFor(m.modelData)?.focused);
|
||||
}
|
||||
|
||||
if (query.startsWith("model:")) {
|
||||
const model = query.slice(6);
|
||||
return monitors.find(m => m.modelData.model === model);
|
||||
}
|
||||
if (query.startsWith("model:")) {
|
||||
const model = query.slice(6);
|
||||
return monitors.find(m => m.modelData.model === model);
|
||||
}
|
||||
|
||||
if (query.startsWith("serial:")) {
|
||||
const serial = query.slice(7);
|
||||
return monitors.find(m => m.modelData.serialNumber === serial);
|
||||
}
|
||||
if (query.startsWith("serial:")) {
|
||||
const serial = query.slice(7);
|
||||
return monitors.find(m => m.modelData.serialNumber === serial);
|
||||
}
|
||||
|
||||
if (query.startsWith("id:")) {
|
||||
const id = parseInt(query.slice(3), 10);
|
||||
return monitors.find(m => Hypr.monitorFor(m.modelData)?.id === id);
|
||||
}
|
||||
if (query.startsWith("id:")) {
|
||||
const id = parseInt(query.slice(3), 10);
|
||||
return monitors.find(m => Hypr.monitorFor(m.modelData)?.id === id);
|
||||
}
|
||||
|
||||
return monitors.find(m => m.modelData.name === query);
|
||||
}
|
||||
return monitors.find(m => m.modelData.name === query);
|
||||
}
|
||||
|
||||
function increaseBrightness(): void {
|
||||
const monitor = getMonitor("active");
|
||||
if (monitor)
|
||||
monitor.setBrightness(monitor.brightness + Config.services.brightnessIncrement);
|
||||
}
|
||||
function getMonitorForScreen(screen: ShellScreen): var {
|
||||
return monitors.find(m => m.modelData === screen);
|
||||
}
|
||||
|
||||
function decreaseBrightness(): void {
|
||||
const monitor = getMonitor("active");
|
||||
if (monitor)
|
||||
monitor.setBrightness(monitor.brightness - Config.services.brightnessIncrement);
|
||||
}
|
||||
function increaseBrightness(): void {
|
||||
const monitor = getMonitor("active");
|
||||
if (monitor)
|
||||
monitor.setBrightness(monitor.brightness + Config.services.brightnessIncrement);
|
||||
}
|
||||
|
||||
onMonitorsChanged: {
|
||||
ddcMonitors = [];
|
||||
ddcProc.running = true;
|
||||
}
|
||||
onMonitorsChanged: {
|
||||
ddcMonitors = [];
|
||||
ddcProc.running = true;
|
||||
}
|
||||
|
||||
Variants {
|
||||
id: variants
|
||||
Variants {
|
||||
id: variants
|
||||
|
||||
model: Quickshell.screens
|
||||
model: Quickshell.screens
|
||||
|
||||
Monitor {}
|
||||
}
|
||||
Monitor {
|
||||
}
|
||||
}
|
||||
|
||||
Process {
|
||||
running: true
|
||||
command: ["sh", "-c", "asdbctl get"] // To avoid warnings if asdbctl is not installed
|
||||
stdout: StdioCollector {
|
||||
onStreamFinished: root.appleDisplayPresent = text.trim().length > 0
|
||||
}
|
||||
}
|
||||
Process {
|
||||
command: ["sh", "-c", "asdbctl get"] // To avoid warnings if asdbctl is not installed
|
||||
running: true
|
||||
|
||||
Process {
|
||||
id: ddcProc
|
||||
stdout: StdioCollector {
|
||||
onStreamFinished: root.appleDisplayPresent = text.trim().length > 0
|
||||
}
|
||||
}
|
||||
|
||||
command: ["ddcutil", "detect", "--brief"]
|
||||
stdout: StdioCollector {
|
||||
onStreamFinished: root.ddcMonitors = text.trim().split("\n\n").filter(d => d.startsWith("Display ")).map(d => ({
|
||||
busNum: d.match(/I2C bus:[ ]*\/dev\/i2c-([0-9]+)/)[1],
|
||||
connector: d.match(/DRM connector:\s+(.*)/)[1].replace(/^card\d+-/, "") // strip "card1-"
|
||||
}))
|
||||
}
|
||||
}
|
||||
Process {
|
||||
id: ddcProc
|
||||
|
||||
CustomShortcut {
|
||||
name: "brightnessUp"
|
||||
description: "Increase brightness"
|
||||
onPressed: root.increaseBrightness()
|
||||
}
|
||||
command: ["ddcutil", "detect", "--brief"]
|
||||
|
||||
CustomShortcut {
|
||||
name: "brightnessDown"
|
||||
description: "Decrease brightness"
|
||||
onPressed: root.decreaseBrightness()
|
||||
}
|
||||
stdout: StdioCollector {
|
||||
onStreamFinished: root.ddcMonitors = text.trim().split("\n\n").filter(d => d.startsWith("Display ")).map(d => ({
|
||||
busNum: d.match(/I2C bus:[ ]*\/dev\/i2c-([0-9]+)/)[1],
|
||||
connector: d.match(/DRM connector:\s+(.*)/)[1].replace(/^card\d+-/, "") // strip "card1-"
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
IpcHandler {
|
||||
target: "brightness"
|
||||
CustomShortcut {
|
||||
description: "Increase brightness"
|
||||
name: "brightnessUp"
|
||||
|
||||
function get(): real {
|
||||
return getFor("active");
|
||||
}
|
||||
onPressed: root.increaseBrightness()
|
||||
}
|
||||
|
||||
// Allows searching by active/model/serial/id/name
|
||||
function getFor(query: string): real {
|
||||
return root.getMonitor(query)?.brightness ?? -1;
|
||||
}
|
||||
CustomShortcut {
|
||||
description: "Decrease brightness"
|
||||
name: "brightnessDown"
|
||||
|
||||
function set(value: string): string {
|
||||
return setFor("active", value);
|
||||
}
|
||||
onPressed: root.decreaseBrightness()
|
||||
}
|
||||
|
||||
// Handles brightness value like brightnessctl: 0.1, +0.1, 0.1-, 10%, +10%, 10%-
|
||||
function setFor(query: string, value: string): string {
|
||||
const monitor = root.getMonitor(query);
|
||||
if (!monitor)
|
||||
return "Invalid monitor: " + query;
|
||||
IpcHandler {
|
||||
function get(): real {
|
||||
return getFor("active");
|
||||
}
|
||||
|
||||
let targetBrightness;
|
||||
if (value.endsWith("%-")) {
|
||||
const percent = parseFloat(value.slice(0, -2));
|
||||
targetBrightness = monitor.brightness - (percent / 100);
|
||||
} else if (value.startsWith("+") && value.endsWith("%")) {
|
||||
const percent = parseFloat(value.slice(1, -1));
|
||||
targetBrightness = monitor.brightness + (percent / 100);
|
||||
} else if (value.endsWith("%")) {
|
||||
const percent = parseFloat(value.slice(0, -1));
|
||||
targetBrightness = percent / 100;
|
||||
} else if (value.startsWith("+")) {
|
||||
const increment = parseFloat(value.slice(1));
|
||||
targetBrightness = monitor.brightness + increment;
|
||||
} else if (value.endsWith("-")) {
|
||||
const decrement = parseFloat(value.slice(0, -1));
|
||||
targetBrightness = monitor.brightness - decrement;
|
||||
} else if (value.includes("%") || value.includes("-") || value.includes("+")) {
|
||||
return `Invalid brightness format: ${value}\nExpected: 0.1, +0.1, 0.1-, 10%, +10%, 10%-`;
|
||||
} else {
|
||||
targetBrightness = parseFloat(value);
|
||||
}
|
||||
// Allows searching by active/model/serial/id/name
|
||||
function getFor(query: string): real {
|
||||
return root.getMonitor(query)?.brightness ?? -1;
|
||||
}
|
||||
|
||||
if (isNaN(targetBrightness))
|
||||
return `Failed to parse value: ${value}\nExpected: 0.1, +0.1, 0.1-, 10%, +10%, 10%-`;
|
||||
function set(value: string): string {
|
||||
return setFor("active", value);
|
||||
}
|
||||
|
||||
monitor.setBrightness(targetBrightness);
|
||||
// Handles brightness value like brightnessctl: 0.1, +0.1, 0.1-, 10%, +10%, 10%-
|
||||
function setFor(query: string, value: string): string {
|
||||
const monitor = root.getMonitor(query);
|
||||
if (!monitor)
|
||||
return "Invalid monitor: " + query;
|
||||
|
||||
return `Set monitor ${monitor.modelData.name} brightness to ${+monitor.brightness.toFixed(2)}`;
|
||||
}
|
||||
}
|
||||
let targetBrightness;
|
||||
if (value.endsWith("%-")) {
|
||||
const percent = parseFloat(value.slice(0, -2));
|
||||
targetBrightness = monitor.brightness - (percent / 100);
|
||||
} else if (value.startsWith("+") && value.endsWith("%")) {
|
||||
const percent = parseFloat(value.slice(1, -1));
|
||||
targetBrightness = monitor.brightness + (percent / 100);
|
||||
} else if (value.endsWith("%")) {
|
||||
const percent = parseFloat(value.slice(0, -1));
|
||||
targetBrightness = percent / 100;
|
||||
} else if (value.startsWith("+")) {
|
||||
const increment = parseFloat(value.slice(1));
|
||||
targetBrightness = monitor.brightness + increment;
|
||||
} else if (value.endsWith("-")) {
|
||||
const decrement = parseFloat(value.slice(0, -1));
|
||||
targetBrightness = monitor.brightness - decrement;
|
||||
} else if (value.includes("%") || value.includes("-") || value.includes("+")) {
|
||||
return `Invalid brightness format: ${value}\nExpected: 0.1, +0.1, 0.1-, 10%, +10%, 10%-`;
|
||||
} else {
|
||||
targetBrightness = parseFloat(value);
|
||||
}
|
||||
|
||||
component Monitor: QtObject {
|
||||
id: monitor
|
||||
if (isNaN(targetBrightness))
|
||||
return `Failed to parse value: ${value}\nExpected: 0.1, +0.1, 0.1-, 10%, +10%, 10%-`;
|
||||
|
||||
required property ShellScreen modelData
|
||||
readonly property bool isDdc: root.ddcMonitors.some(m => m.connector === modelData.name)
|
||||
readonly property string busNum: root.ddcMonitors.find(m => m.connector === modelData.name)?.busNum ?? ""
|
||||
readonly property bool isAppleDisplay: root.appleDisplayPresent && modelData.model.startsWith("StudioDisplay")
|
||||
property real brightness
|
||||
property real queuedBrightness: NaN
|
||||
monitor.setBrightness(targetBrightness);
|
||||
|
||||
readonly property Process initProc: Process {
|
||||
stdout: StdioCollector {
|
||||
onStreamFinished: {
|
||||
if (monitor.isAppleDisplay) {
|
||||
const val = parseInt(text.trim());
|
||||
monitor.brightness = val / 101;
|
||||
} else {
|
||||
const [, , , cur, max] = text.split(" ");
|
||||
monitor.brightness = parseInt(cur) / parseInt(max);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return `Set monitor ${monitor.modelData.name} brightness to ${+monitor.brightness.toFixed(2)}`;
|
||||
}
|
||||
|
||||
readonly property Timer timer: Timer {
|
||||
interval: 500
|
||||
onTriggered: {
|
||||
if (!isNaN(monitor.queuedBrightness)) {
|
||||
monitor.setBrightness(monitor.queuedBrightness);
|
||||
monitor.queuedBrightness = NaN;
|
||||
}
|
||||
}
|
||||
}
|
||||
target: "brightness"
|
||||
}
|
||||
|
||||
function setBrightness(value: real): void {
|
||||
value = Math.max(0, Math.min(1, value));
|
||||
const rounded = Math.round(value * 100);
|
||||
if (Math.round(brightness * 100) === rounded)
|
||||
return;
|
||||
component Monitor: QtObject {
|
||||
id: monitor
|
||||
|
||||
if (isDdc && timer.running) {
|
||||
queuedBrightness = value;
|
||||
return;
|
||||
}
|
||||
property real brightness
|
||||
readonly property string busNum: root.ddcMonitors.find(m => m.connector === modelData.name)?.busNum ?? ""
|
||||
readonly property Process initProc: Process {
|
||||
stdout: StdioCollector {
|
||||
onStreamFinished: {
|
||||
if (monitor.isAppleDisplay) {
|
||||
const val = parseInt(text.trim());
|
||||
monitor.brightness = val / 101;
|
||||
} else {
|
||||
const [, , , cur, max] = text.split(" ");
|
||||
monitor.brightness = parseInt(cur) / parseInt(max);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
readonly property bool isAppleDisplay: root.appleDisplayPresent && modelData.model.startsWith("StudioDisplay")
|
||||
readonly property bool isDdc: root.ddcMonitors.some(m => m.connector === modelData.name)
|
||||
required property ShellScreen modelData
|
||||
property real queuedBrightness: NaN
|
||||
readonly property Timer timer: Timer {
|
||||
interval: 500
|
||||
|
||||
brightness = value;
|
||||
onTriggered: {
|
||||
if (!isNaN(monitor.queuedBrightness)) {
|
||||
monitor.setBrightness(monitor.queuedBrightness);
|
||||
monitor.queuedBrightness = NaN;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (isAppleDisplay)
|
||||
Quickshell.execDetached(["asdbctl", "set", rounded]);
|
||||
else if (isDdc)
|
||||
Quickshell.execDetached(["ddcutil", "--disable-dynamic-sleep", "--sleep-multiplier", ".1", "--skip-ddc-checks", "-b", busNum, "setvcp", "10", rounded]);
|
||||
else
|
||||
Quickshell.execDetached(["brightnessctl", "s", `${rounded}%`]);
|
||||
function initBrightness(): void {
|
||||
if (isAppleDisplay)
|
||||
initProc.command = ["asdbctl", "get"];
|
||||
else if (isDdc)
|
||||
initProc.command = ["ddcutil", "-b", busNum, "getvcp", "10", "--brief"];
|
||||
else
|
||||
initProc.command = ["sh", "-c", "echo a b c $(brightnessctl g) $(brightnessctl m)"];
|
||||
|
||||
if (isDdc)
|
||||
timer.restart();
|
||||
}
|
||||
initProc.running = true;
|
||||
}
|
||||
|
||||
function initBrightness(): void {
|
||||
if (isAppleDisplay)
|
||||
initProc.command = ["asdbctl", "get"];
|
||||
else if (isDdc)
|
||||
initProc.command = ["ddcutil", "-b", busNum, "getvcp", "10", "--brief"];
|
||||
else
|
||||
initProc.command = ["sh", "-c", "echo a b c $(brightnessctl g) $(brightnessctl m)"];
|
||||
function setBrightness(value: real): void {
|
||||
value = Math.max(0, Math.min(1, value));
|
||||
const rounded = Math.round(value * 100);
|
||||
if (Math.round(brightness * 100) === rounded)
|
||||
return;
|
||||
|
||||
initProc.running = true;
|
||||
}
|
||||
if (isDdc && timer.running) {
|
||||
queuedBrightness = value;
|
||||
return;
|
||||
}
|
||||
|
||||
onBusNumChanged: initBrightness()
|
||||
Component.onCompleted: initBrightness()
|
||||
}
|
||||
brightness = value;
|
||||
|
||||
if (isAppleDisplay)
|
||||
Quickshell.execDetached(["asdbctl", "set", rounded]);
|
||||
else if (isDdc)
|
||||
Quickshell.execDetached(["ddcutil", "--disable-dynamic-sleep", "--sleep-multiplier", ".1", "--skip-ddc-checks", "-b", busNum, "setvcp", "10", rounded]);
|
||||
else
|
||||
Quickshell.execDetached(["brightnessctl", "s", `${rounded}%`]);
|
||||
|
||||
if (isDdc)
|
||||
timer.restart();
|
||||
}
|
||||
|
||||
Component.onCompleted: initBrightness()
|
||||
onBusNumChanged: initBrightness()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user