forked from 1b-agent/opencadstudio-upstream
Select Color: one fixed colour book folder instead of a folder list setting
The color_book_dirs setting is gone. Colour books live in color_books in the config folder (book::folder()), like the other fixed locations; Options > Files shows it through Folders::color_books. read_all() and load_from_dialog() use that folder directly. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_013rGmDUNr1hUf4zo1X3dak8
This commit is contained in:
7 files changed
+47
-72
No files matched your search
+31
-31
@@ -77,42 +77,45 @@ pub fn builtin() -> impl Iterator<Item = ColorBook> {
|
||||
parse(include_str!("../../../assets/color_books/ral-classic.acb")).into_iter()
|
||||
}
|
||||
|
||||
/// The built-in book plus every `.acb` file in `dirs`, folder by folder in
|
||||
/// Where colour books are kept: `color_books` in the config folder. The
|
||||
/// web has no folders.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub fn folder() -> Option<std::path::PathBuf> {
|
||||
crate::config::config_dir().map(|dir| dir.join("color_books"))
|
||||
}
|
||||
|
||||
/// The built-in book plus every `.acb` file in the colour book folder, in
|
||||
/// file-name order. A book replaces an earlier one of the same name.
|
||||
pub fn read_all(dirs: &[String]) -> Vec<ColorBook> {
|
||||
pub fn read_all() -> Vec<ColorBook> {
|
||||
#[cfg_attr(target_arch = "wasm32", allow(unused_mut))]
|
||||
let mut books: Vec<_> = builtin().collect();
|
||||
// Browsers have no folders.
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
let _ = dirs;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
for book in dirs
|
||||
.iter()
|
||||
.flat_map(|dir| {
|
||||
let mut files: Vec<_> = std::fs::read_dir(dir)
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.filter_map(|entry| entry.ok().map(|entry| entry.path()))
|
||||
.filter(|path| {
|
||||
path.extension()
|
||||
.is_some_and(|ext| ext.eq_ignore_ascii_case("acb"))
|
||||
})
|
||||
.collect();
|
||||
files.sort();
|
||||
files
|
||||
})
|
||||
.filter_map(|path| std::fs::read_to_string(path).ok())
|
||||
.filter_map(|text| parse(&text).ok())
|
||||
{
|
||||
add_or_replace(&mut books, book);
|
||||
if let Some(dir) = folder() {
|
||||
let mut files: Vec<_> = std::fs::read_dir(dir)
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.filter_map(|entry| entry.ok().map(|entry| entry.path()))
|
||||
.filter(|path| {
|
||||
path.extension()
|
||||
.is_some_and(|ext| ext.eq_ignore_ascii_case("acb"))
|
||||
})
|
||||
.collect();
|
||||
files.sort();
|
||||
for book in files
|
||||
.into_iter()
|
||||
.filter_map(|path| std::fs::read_to_string(path).ok())
|
||||
.filter_map(|text| parse(&text).ok())
|
||||
{
|
||||
add_or_replace(&mut books, book);
|
||||
}
|
||||
}
|
||||
books
|
||||
}
|
||||
|
||||
/// Ask for an `.acb` file and read it. On desktop the file is copied into
|
||||
/// `folder`, the first colour book folder, so it stays when the original
|
||||
/// moves; the web keeps the book for the session only.
|
||||
pub async fn load_from_dialog(folder: Option<String>) -> Result<ColorBook, String> {
|
||||
/// the colour book folder, so it stays when the original moves; the web
|
||||
/// keeps the book for the session only.
|
||||
pub async fn load_from_dialog() -> Result<ColorBook, String> {
|
||||
let file = crate::sys::file_dialog()
|
||||
.add_filter(crate::t!("Color book").as_ref(), &["acb", "ACB"])
|
||||
.pick_file()
|
||||
@@ -121,14 +124,11 @@ pub async fn load_from_dialog(folder: Option<String>) -> Result<ColorBook, Strin
|
||||
let bytes = file.read().await;
|
||||
let book = parse(&String::from_utf8_lossy(&bytes))?;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
if let Some(folder) = folder {
|
||||
let folder = std::path::PathBuf::from(folder);
|
||||
if let Some(folder) = folder() {
|
||||
std::fs::create_dir_all(&folder)
|
||||
.and_then(|()| std::fs::write(folder.join(file.file_name()), &bytes))
|
||||
.map_err(|e| e.to_string())?;
|
||||
}
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
let _ = folder;
|
||||
Ok(book)
|
||||
}
|
||||
|
||||
|
||||
@@ -108,6 +108,8 @@ pub struct Folders {
|
||||
/// Where `.shx` / `.ttf` fonts dropped in by the user (or downloaded for
|
||||
/// a drawing) are found.
|
||||
pub fonts: Option<String>,
|
||||
/// Where `.acb` colour books are kept and loaded ones copied to.
|
||||
pub color_books: Option<String>,
|
||||
}
|
||||
|
||||
/// Values read from the current drawing's header rather than from preferences.
|
||||
@@ -212,7 +214,6 @@ pub fn view_window<'a>(
|
||||
drawing_prefs: DrawingPrefs,
|
||||
folders: Folders,
|
||||
qnew_template: &'a str,
|
||||
color_book_dirs: &'a [String],
|
||||
double_click_block_refedit: bool,
|
||||
double_click_block_attedit: bool,
|
||||
cursor_type: CursorType,
|
||||
@@ -1791,13 +1792,11 @@ pub fn view_window<'a>(
|
||||
folder_row(crate::t!("Autosave files"), folders.autosave.clone()),
|
||||
Space::new().height(12),
|
||||
folder_row(crate::t!("Fonts"), folders.fonts.clone()),
|
||||
// Where `.acb` colour books are found (none on the web).
|
||||
color_book_dirs.iter().fold(column![], |rows, dir| rows
|
||||
.push(Space::new().height(12))
|
||||
.push(folder_row(
|
||||
crate::t!("Color book locations"),
|
||||
Some(dir.clone())
|
||||
))),
|
||||
Space::new().height(12),
|
||||
folder_row(
|
||||
crate::t!("Color book locations"),
|
||||
folders.color_books.clone()
|
||||
),
|
||||
Space::new().height(16),
|
||||
text(crate::t!("Default template for new drawings")).size(12),
|
||||
Space::new().height(4),
|
||||
|
||||
Reference in new issue
Block a user