Merge pull request #9854 from dotCooCoo/fix/cms-scoped-working-settings

Color: with_active keeps the working settings on its thread too
This commit is contained in:
.CooCoo authored and GitHub committed 2026-11-27 21:37:24 -08:00
commit 2cabf8ddd6
3 files changed
+46 -5

No files matched your search

+17 -3
View File
@@ -1211,7 +1211,18 @@ pub fn active_settings() -> ColorSettings {
/// The color-management context of Edit → Color Settings (the working spaces, intent and black
/// point compensation), as [`set_active`] last set them, whatever profiles a document is assigned.
/// Inside [`with_active`] on this thread: the working settings of the settings it was given.
pub fn working() -> Arc<Cms> {
if SCOPED_FLAGS.with(Cell::get) & SCOPED_SET != 0
&& let Some(c) = SCOPED.with(|s| s.borrow().clone())
{
return c.working.clone().unwrap_or(c);
}
installed_working()
}
/// The working settings of the process, as [`set_active`] last set them.
fn installed_working() -> Arc<Cms> {
if let Some(c) = WORKING.read().unwrap_or_else(|e| e.into_inner()).as_ref() {
return c.clone();
}
@@ -1241,7 +1252,7 @@ pub fn set_active(settings: &ColorSettings) -> Result<(), CmsError> {
/// is not of its model, leaves the working space. The working settings stay as they are. True when
/// the active settings changed.
pub fn use_document_profiles(rgb: Option<&str>, cmyk: Option<&str>) -> bool {
let working = working();
let working = installed_working();
let st = document_settings(&working.settings, rgb, cmyk);
if st == installed().settings {
return false;
@@ -1281,7 +1292,7 @@ const DOCUMENT_CONTEXTS: usize = 8;
/// loaded gives the working settings.
pub fn for_document(rgb: Option<&str>, cmyk: Option<&str>) -> Arc<Cms> {
static KEPT: Mutex<Vec<(u64, Arc<Cms>)>> = Mutex::new(Vec::new());
let working = working();
let working = installed_working();
let st = document_settings(&working.settings, rgb, cmyk);
let installed = installed();
if st == installed.settings {
@@ -1308,7 +1319,10 @@ pub fn for_document(rgb: Option<&str>, cmyk: Option<&str>) -> Arc<Cms> {
/// Run `f` with `cms` as the active settings on this thread ([`active`], [`crate::Color::to_rgb`]
/// and the conversions that read them), as when drawing a document other than the front one
/// through its own profiles. Other threads, and this thread after `f`, keep the settings they had.
/// through its own profiles, and with the working settings `cms` was made from as the working
/// settings ([`working`]): the ones [`for_document`] put the document's profiles in place of, else
/// `cms` itself. A [`set_active`] while `f` runs, on any thread, does not change either on this
/// thread. Other threads, and this thread after `f`, keep the settings they had.
pub fn with_active<R>(cms: Arc<Cms>, f: impl FnOnce() -> R) -> R {
/// Puts back the settings this thread had before, when `f` returns or unwinds.
struct Restore(Option<Arc<Cms>>, u8);
+27
View File
@@ -519,6 +519,33 @@ fn with_active_puts_settings_in_place_on_this_thread_only() {
assert_eq!((rgb.to_rgb(), ink.to_rgb()), before, "the active settings come back after a panic");
}
#[test]
fn with_active_keeps_its_working_settings_while_another_thread_sets_them() {
let before = working_settings();
let p3 = Arc::new(Cms::new(&ColorSettings { rgb: DISPLAY_P3.into(), cmyk: DEVICE_CMYK.into(), ..Default::default() }).unwrap());
let (inside, after_other, other) = with_active(p3.clone(), || {
let inside = working();
// The same settings again, so other tests in this binary see the same values.
let again = before.clone();
let other = std::thread::spawn(move || {
set_active(&again).unwrap();
working()
})
.join()
.unwrap();
(inside, working(), other)
});
assert!(Arc::ptr_eq(&inside, &p3), "settings made from no others are their own working settings");
assert!(Arc::ptr_eq(&after_other, &p3), "a set_active on another thread leaves this thread's working settings");
assert!(!Arc::ptr_eq(&other, &p3), "the other thread has the settings it set");
assert_eq!(working_settings(), before, "the working settings come back after");
// A document's context keeps the working settings it was made from.
let doc = for_document(Some(DISPLAY_P3), None);
let (active, working) = with_active(doc, || (active_settings(), working_settings()));
assert_eq!(active.rgb, DISPLAY_P3, "the document's profile is active");
assert_eq!(working, before, "the working RGB space stays the working one");
}
#[test]
fn a_document_gets_the_same_context_for_the_same_profiles() {
// Other tests load profiles meanwhile, and a load makes the contexts again: try until one pair
+2 -2
View File
@@ -1191,8 +1191,8 @@ fn convert8(t: &craft::color_ui::display::DisplayTransform, rgb: [u8; 3]) -> [u8
}
/// Run a test that compares two renders with the color settings that are active when it starts,
/// and the default proof view. A test on another thread can change the process's active settings;
/// this thread keeps its own. The proof view is the whole process's, so it is held still.
/// and the default proof view. A test on another thread can change the process's active and working
/// settings; this thread keeps its own. The proof view is the whole process's, so it is held still.
fn keeping_color_settings(test: fn()) {
let _view = crate::testing::DefaultProofView::new();
vectorcraft_color::cms::with_active(vectorcraft_color::cms::active(), test);