Repository navigation
LR-PREF-DISPLAY: on Windows, previews follow the monitor's colour profile #2439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
FredApps
merged 30 commits into
storytold:main
from
insideTheFlux:feat/windows-monitor-profile
Nov 5, 2026
Merged
Changes from 13 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
423d209
LR-PREF-DISPLAY: Windows previews follow the monitor's colour profile
claude 71ae9fe
LR-PREF-DISPLAY: hardware-parity totals are the sums of their rows
claude f301dbb
Merge main into feat/windows-monitor-profile
claude 5e74061
Merge storytold/main into feat/windows-monitor-profile
claude 2136819
Cargo.lock: lightcraft-wincolor at the workspace version 0.11.0
claude 5b26d49
Merge storytold/main into feat/windows-monitor-profile
insideTheFlux ebcb63a
Merge storytold/main into feat/windows-monitor-profile
insideTheFlux 8d34a14
Cargo.lock: lightcraft-wincolor at the workspace version 0.13.0
insideTheFlux 5ca6648
Merge storytold/main into feat/windows-monitor-profile
insideTheFlux a534241
Merge storytold/main into feat/windows-monitor-profile
insideTheFlux 3457805
Merge storytold/main into feat/windows-monitor-profile
insideTheFlux e7577a1
Merge storytold/main into feat/windows-monitor-profile
insideTheFlux 0b610c8
LR-PREF-DISPLAY: an unreadable Windows advanced-colour state leaves p…
insideTheFlux d0eaf2e
Merge storytold/main into feat/windows-monitor-profile
insideTheFlux ac882a4
Merge storytold/main into feat/windows-monitor-profile
insideTheFlux fc2439d
Merge storytold/main into feat/windows-monitor-profile
insideTheFlux 131dcfd
Merge storytold/main into feat/windows-monitor-profile
insideTheFlux 76f3972
Merge storytold/main into feat/windows-monitor-profile
insideTheFlux d94479f
LR-PREF-DISPLAY: wincolor inherits the workspace lints
insideTheFlux a9f766b
Merge storytold/main into feat/windows-monitor-profile
insideTheFlux 1888e87
Merge storytold/main into feat/windows-monitor-profile
FredApps 9b7f4a8
Merge storytold/main into feat/windows-monitor-profile
FredApps 4432fee
Screen profile: wake an idle window once a second to read it again
FredApps 4da8573
Merge storytold/main into feat/windows-monitor-profile
FredApps 20afbcc
Merge storytold/main into feat/windows-monitor-profile
FredApps 0b9ff64
Merge storytold/main into feat/windows-monitor-profile
FredApps 9056d64
Merge storytold/main into feat/windows-monitor-profile
FredApps c18081f
Merge storytold/main into feat/windows-monitor-profile
FredApps bb0f0d5
Merge storytold/main into feat/windows-monitor-profile
FredApps 2bc668e
Merge storytold/main into feat/windows-monitor-profile
FredApps File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| //! Reading a display's ICC profile file for the screen-profile services (`colord` on Linux, | ||
| //! `win_screen_profile` on Windows). | ||
|
|
||
| use std::path::{Path, PathBuf}; | ||
| use std::time::SystemTime; | ||
|
|
||
| /// The largest profile file read (real display profiles are a few KiB to a few MiB). | ||
| pub const MAX_PROFILE: u64 = 16 << 20; | ||
|
|
||
| /// The bytes of profile file `path`, `None` when it can't be read or is implausibly large. | ||
| pub fn load(path: &Path) -> Option<Vec<u8>> { | ||
| use std::io::Read as _; | ||
| let file = std::fs::File::open(path).ok()?; | ||
| let mut bytes = Vec::new(); | ||
| file.take(MAX_PROFILE + 1).read_to_end(&mut bytes).ok()?; | ||
| (!bytes.is_empty() && bytes.len() as u64 <= MAX_PROFILE).then_some(bytes) | ||
| } | ||
|
|
||
| /// A profile file read again only when it changes: the services are asked about once a second, and | ||
| /// calibration software rewrites its profile under the same name. | ||
| #[cfg_attr(not(windows), allow(dead_code))] | ||
| #[derive(Default)] | ||
| pub struct Cached { | ||
| /// The file last read: its path, length and modification time. | ||
| stamp: Option<(PathBuf, u64, Option<SystemTime>)>, | ||
| bytes: Option<Vec<u8>>, | ||
| } | ||
|
|
||
| #[cfg_attr(not(windows), allow(dead_code))] | ||
| impl Cached { | ||
| /// The bytes of profile file `path`, read again when it isn't the file read last time or has | ||
| /// changed since (see [`load`]). | ||
| pub fn read(&mut self, path: &Path) -> Option<Vec<u8>> { | ||
| let stamp = std::fs::metadata(path).ok().map(|m| (path.to_path_buf(), m.len(), m.modified().ok())); | ||
| if stamp.is_none() || stamp != self.stamp { | ||
| self.bytes = stamp.as_ref().and_then(|_| load(path)); | ||
| self.stamp = stamp; | ||
| } | ||
| self.bytes.clone() | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn profile_files_are_read_with_a_size_cap() { | ||
| let dir = std::env::temp_dir().join(format!("profile-file-test-{}", std::process::id())); | ||
| std::fs::create_dir_all(&dir).unwrap(); | ||
| let small = dir.join("small.icc"); | ||
| std::fs::write(&small, b"acsp").unwrap(); | ||
| assert_eq!(load(&small).as_deref(), Some(&b"acsp"[..])); | ||
| let empty = dir.join("empty.icc"); | ||
| std::fs::write(&empty, b"").unwrap(); | ||
| assert_eq!(load(&empty), None); | ||
| assert_eq!(load(&dir.join("missing.icc")), None); | ||
| let big = dir.join("big.icc"); | ||
| std::fs::File::create(&big).unwrap().set_len(MAX_PROFILE + 1).unwrap(); | ||
| assert_eq!(load(&big), None); | ||
| std::fs::remove_dir_all(&dir).unwrap(); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_cached_profile_is_read_again_when_the_file_changes() { | ||
| let dir = std::env::temp_dir().join(format!("profile-cache-test-{}", std::process::id())); | ||
| std::fs::create_dir_all(&dir).unwrap(); | ||
| let (a, b) = (dir.join("a.icc"), dir.join("b.icc")); | ||
| std::fs::write(&a, b"first").unwrap(); | ||
| std::fs::write(&b, b"other").unwrap(); | ||
| let mut cache = Cached::default(); | ||
| assert_eq!(cache.read(&a).as_deref(), Some(&b"first"[..])); | ||
| assert_eq!(cache.read(&a).as_deref(), Some(&b"first"[..])); | ||
| // another monitor's profile | ||
| assert_eq!(cache.read(&b).as_deref(), Some(&b"other"[..])); | ||
| // rewritten under the same name (a different length, so the change shows whatever the | ||
| // file system's time resolution) | ||
| std::fs::write(&b, b"calibrated").unwrap(); | ||
| assert_eq!(cache.read(&b).as_deref(), Some(&b"calibrated"[..])); | ||
| // gone: nothing, then read again once it is back | ||
| std::fs::remove_file(&b).unwrap(); | ||
| assert_eq!(cache.read(&b), None); | ||
| std::fs::write(&b, b"back").unwrap(); | ||
| assert_eq!(cache.read(&b).as_deref(), Some(&b"back"[..])); | ||
| std::fs::remove_dir_all(&dir).unwrap(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| //! The ICC profile Windows assigns to the monitor showing LightCraft's window. | ||
| //! | ||
| //! On an SDR desktop Windows passes what an app draws to the monitor unchanged, so sRGB previews | ||
| //! look oversaturated on a wide-gamut monitor and ignore a calibration. While no monitor profile is | ||
| //! chosen in Settings ▸ Display, previews are shown through the profile Windows uses for the window's | ||
| //! monitor (`Services::screen_profile`, see `lightcraft_wincolor`), followed when the window moves to | ||
| //! another monitor or the profile changes in Windows' Color Management. When Windows colour-manages | ||
| //! the desktop itself (HDR, Auto Color Management) previews stay sRGB, as Windows expects then. | ||
|
|
||
| use lightcraft_wincolor::MonitorColor; | ||
|
|
||
| /// The service the UI polls, for the window `cc` belongs to; `None` when its handle isn't a Win32 one. | ||
| pub fn service(cc: &eframe::CreationContext<'_>) -> Option<lightcraft_ui_egui::ScreenProfileFn> { | ||
| use raw_window_handle::{HasWindowHandle, RawWindowHandle}; | ||
| let RawWindowHandle::Win32(handle) = cc.window_handle().ok()?.as_raw() else { return None }; | ||
| let hwnd = handle.hwnd.get(); | ||
| let mut file = crate::profile_file::Cached::default(); | ||
| let mut last: Option<MonitorColor> = None; | ||
| Some(Box::new(move || { | ||
| let answer = lightcraft_wincolor::monitor_color(hwnd); | ||
| if last.as_ref() != Some(&answer) { | ||
| log::info!("screen profile: {}", describe(&answer)); | ||
| last = Some(answer.clone()); | ||
| } | ||
| match answer { | ||
| MonitorColor::Profile(path) => file.read(&path), | ||
| MonitorColor::ManagedByWindows | MonitorColor::NoProfile | MonitorColor::Unknown => None, | ||
| } | ||
| })) | ||
| } | ||
|
|
||
| /// For the log: what the window's monitor answered. | ||
| fn describe(answer: &MonitorColor) -> String { | ||
| match answer { | ||
| MonitorColor::Profile(path) => format!("the monitor's profile, {}", path.display()), | ||
| MonitorColor::ManagedByWindows => "Windows colour-manages this monitor (HDR or Auto Color Management): previews are sRGB".into(), | ||
| MonitorColor::NoProfile => "no profile is assigned to the monitor: previews are sRGB".into(), | ||
| MonitorColor::Unknown => "not known for this monitor: previews are sRGB".into(), | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| [package] | ||
| name = "lightcraft-wincolor" | ||
| version.workspace = true | ||
| edition.workspace = true | ||
| license.workspace = true | ||
| description = "LightCraft wincolor (L0): the colour profile Windows assigns to the monitor showing a window. One of the two crates allowed `unsafe` (Win32 calls)." | ||
|
|
||
| # Opts out of the workspace lints only to keep `unsafe_code = "deny"` overridable for the Win32 | ||
| # module below, as `crates/sysmem` does; every other crate root has `#![forbid(unsafe_code)]`. See | ||
| # AGENTS.md and craftrules standards/never-crash.md ("isolated helper library crate"). | ||
| [lints.rust] | ||
| unsafe_code = "deny" | ||
|
|
||
| [lints.clippy] | ||
| dbg_macro = "warn" | ||
| todo = "warn" | ||
|
|
||
| [target.'cfg(windows)'.dependencies] | ||
| windows-sys = { version = "0.61", features = [ | ||
| "Win32_Foundation", | ||
| "Win32_Graphics_Gdi", | ||
| "Win32_UI_ColorSystem", | ||
| "Win32_Devices_Display", | ||
| ] } | ||
|
|
||
| [dev-dependencies] | ||
| # tests record why they skip (`cargo xtask ci` lists the skips) | ||
| lightcraft-testkit = { workspace = true } | ||
|
|
||
| # the desktop window, for a test | ||
| [target.'cfg(windows)'.dev-dependencies] | ||
| windows-sys = { version = "0.61", features = ["Win32_UI_WindowsAndMessaging"] } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.