Skip to content
This repository has been archived by the owner on Sep 26, 2024. It is now read-only.

Commit

Permalink
check on first write
Browse files Browse the repository at this point in the history
  • Loading branch information
ruihe774 committed Dec 1, 2023
1 parent 8a2ff9a commit 6d0c420
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 33 deletions.
28 changes: 14 additions & 14 deletions src/components/FeatureSlider.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default defineComponent({
type: String,
required: true,
},
feature: {
featureName: {
type: String,
required: true,
},
Expand All @@ -28,23 +28,20 @@ export default defineComponent({
};
},
computed: {
featureObject() {
return monitorManager.getFeature(this.monitorId, this.feature);
feature() {
return monitorManager.getFeature(this.monitorId, this.featureName);
},
readonly() {
return this.featureObject.readonly;
},
featureValue() {
return this.featureObject.value;
return this.feature.readonly;
},
current() {
return this.featureValue.current;
return this.feature.value.current;
},
maximum() {
return this.featureValue.maximum;
return this.feature.value.maximum;
},
icon() {
return (iconMap as { [key: string]: string })[this.feature];
return (iconMap as { [key: string]: string })[this.featureName];
},
},
methods: {
Expand All @@ -53,18 +50,21 @@ export default defineComponent({
const target = e.target! as HTMLInputElement;
if (target.validity.valid) {
const value = Number(target.value);
monitorManager.updateFeature(this.monitorId, this.feature, value);
monitorManager.updateFeature(this.monitorId, this.featureName, value);
}
},
handleWheel(event: Event) {
if (this.readonly) {
return;
}
const e = event as WheelEvent;
const target = e.currentTarget! as HTMLInputElement;
if (e.deltaMode == WheelEvent.DOM_DELTA_PIXEL) {
const offset = Math.abs(e.deltaX) > Math.abs(e.deltaY) ? e.deltaX : -e.deltaY;
const current = Number(target.value);
monitorManager.updateFeature(
this.monitorId,
this.feature,
this.featureName,
Math.max(0, Math.min(this.maximum, Math.round(current + offset * 0.01))),
);
}
Expand All @@ -74,8 +74,8 @@ export default defineComponent({
</script>

<template>
<label :class="[sheet.flex, sheet.cozyLine]">
<span :class="sheet.bigIcon" :aria-label="feature">
<label :class="[sheet.flex, sheet.cozyLine, { [sheet.inactive]: readonly }]">
<span :class="sheet.bigIcon" :aria-label="featureName">
{{ icon }}
</span>
<input
Expand Down
11 changes: 7 additions & 4 deletions src/components/MonitorItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ export default defineComponent({
>
<div>
<span :class="sheet.bigIcon" aria-label="monitor">&#xE7F4;</span>
<span :class="sheet.titleFont" style="margin-inline-start: 0.15em">
<span
:class="[sheet.titleFont, sheet.selectable]"
style="margin-inline-start: 0.15em"
>
{{ name }}
</span>
</div>
Expand All @@ -68,9 +71,9 @@ export default defineComponent({
</button>
</div>
<ul :class="[sheet.resetSpacing, sheet.verticalFlex, sheet.stretchItems]">
<template v-for="{ name: feature, value } in monitor.features" :key="feature">
<li v-if="feature != 'powerstate' && value.maximum" :class="sheet.resetSpacing">
<FeatureSlider :monitor-id="monitorId" :feature="feature" />
<template v-for="{ name: featureName, value } in monitor.features" :key="featureName">
<li v-if="featureName != 'powerstate' && value.maximum" :class="sheet.resetSpacing">
<FeatureSlider :monitor-id="monitorId" :feature-name="featureName" />
</li>
</template>
</ul>
Expand Down
52 changes: 40 additions & 12 deletions src/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface Feature {
value: Reply;
syncing: boolean;
readonly: boolean;
written: boolean;
}

export interface Monitor {
Expand All @@ -27,6 +28,8 @@ function timeout(millis: number): Promise<undefined> {
});
}

const featureTestTime = 200;

export class Manager {
readonly monitors: DeepReadonly<Monitor[]> = reactive([]);
private refreshing: boolean = false;
Expand Down Expand Up @@ -64,7 +67,6 @@ export class Manager {
for (const monitor of monitors) {
pool.push(
(async () => {
const featureTestTime = 200;
const featureNames = monitor.features.length
? monitor.features.map((feature) => feature.name)
: ["luminance", "contrast", "brightness", "volume", "powerstate"];
Expand Down Expand Up @@ -93,6 +95,7 @@ export class Manager {
value,
syncing: false,
readonly: false,
written: false,
});
}
} else if (idx != -1) {
Expand Down Expand Up @@ -136,8 +139,10 @@ export class Manager {

updateFeature(id: string, name: string, value: number): void {
const feature = this.getFeature(id, name) as Feature;
feature.value.current = value;
feature.syncing = true;
if (feature.value.current != value) {
feature.value.current = value;
feature.syncing = true;
}
}
}

Expand All @@ -153,15 +158,38 @@ watch(
for (const monitor of monitors) {
for (const feature of monitor.features) {
if (feature.syncing) {
feature.syncing = false;
invoke("set_monitor_feature", {
id: monitor.id,
feature: feature.name,
value: feature.value.current,
}).catch(() => {
feature.readonly = true;
monitorManager.refresh();
});
(async () => {
try {
feature.syncing = false;
if (!feature.written) {
feature.readonly = true;
}
await invoke("set_monitor_feature", {
id: monitor.id,
feature: feature.name,
value: feature.value.current,
});
if (!feature.written) {
await timeout(featureTestTime);
const value = await invoke<Reply>(
"get_monitor_feature",
{
id: monitor.id,
feature: feature.name,
},
);
if (value.current == feature.value.current) {
feature.written = true;
feature.readonly = false;
} else {
feature.value = value;
}
}
} catch {
feature.readonly = true;
monitorManager.refresh();
}
})();
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/style.global.sass
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
:root
font-family: "Segoe UI Variable Text", "Segoe UI"
font-size: 14px
color: black
color: var(--foreground)
user-select: none
@media screen and (prefers-color-scheme: dark)
color: white
&[data-writing-mode=horizontal-tb]
writing-mode: horizontal-tb
&[data-writing-mode=vertical-lr]
Expand All @@ -16,3 +14,7 @@ body
margin-block: 1.2rem
margin-inline: 1rem
overflow: hidden

::selection
background: var(--accent)
color: white
9 changes: 9 additions & 0 deletions src/style.module.sass
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@
@extend .resetInput
margin: 0.15rem

&:disabled
pointer-events: none

&::-webkit-slider-thumb
@extend .resetInput
box-sizing: content-box
Expand Down Expand Up @@ -136,3 +139,9 @@
background: linear-gradient(to var(--slider-end), var(--slider-thumb-color), var(--slider-thumb-color) var(--slider-value), var(--slider-track-color) var(--slider-value), var(--slider-track-color))
block-size: 0.3rem
border-radius: 0.6rem

.selectable
user-select: text

.inactive
filter: grayscale(0.7) brightness(0.7)

0 comments on commit 6d0c420

Please sign in to comment.