Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions assets/locales/pending/theme-paste.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"en": {
"prefs.theme_file_or_paste": "file or paste",
"prefs.theme_import_hint": "Click to choose a theme file, drop one here, or press Ctrl+V (Cmd+V on a Mac) with a theme copied",
"prefs.theme_paste": "Paste theme",
"prefs.theme_paste_none": "The clipboard holds no SolidCraft theme"
},
"ar": {
"prefs.theme_file_or_paste": "ملف أو لصق",
"prefs.theme_import_hint": "انقر لاختيار ملف سمة، أو أفلت ملفًا هنا، أو اضغط Ctrl+V (Cmd+V على Mac) بعد نسخ سمة",
"prefs.theme_paste": "لصق السمة",
"prefs.theme_paste_none": "لا تحتوي الحافظة على سمة SolidCraft"
},
"de": {
"prefs.theme_file_or_paste": "Datei oder Einfügen",
"prefs.theme_import_hint": "Klicken, um eine Designdatei zu wählen, eine hierher ziehen oder mit kopiertem Design Strg+V (Cmd+V auf dem Mac) drücken",
"prefs.theme_paste": "Design einfügen",
"prefs.theme_paste_none": "Die Zwischenablage enthält kein SolidCraft-Design"
},
"he": {
"prefs.theme_file_or_paste": "קובץ או הדבקה",
"prefs.theme_import_hint": "לחצו כדי לבחור קובץ ערכת נושא, שחררו קובץ כאן, או הקישו Ctrl+V ‏(Cmd+V ב-Mac) אחרי שהעתקתם ערכת נושא",
"prefs.theme_paste": "הדבקת ערכת נושא",
"prefs.theme_paste_none": "בלוח אין ערכת נושא של SolidCraft"
},
"ja": {
"prefs.theme_file_or_paste": "ファイルまたは貼り付け",
"prefs.theme_import_hint": "クリックしてテーマファイルを選ぶか、ここにドロップするか、テーマをコピーした状態で Ctrl+V(Mac では Cmd+V)を押します",
"prefs.theme_paste": "テーマを貼り付け",
"prefs.theme_paste_none": "クリップボードに SolidCraft のテーマがありません"
},
"vi": {
"prefs.theme_file_or_paste": "tệp hoặc dán",
"prefs.theme_import_hint": "Nhấp để chọn tệp giao diện, thả tệp vào đây, hoặc nhấn Ctrl+V (Cmd+V trên Mac) khi đã sao chép một giao diện",
"prefs.theme_paste": "Dán giao diện",
"prefs.theme_paste_none": "Bộ nhớ tạm không chứa giao diện SolidCraft nào"
},
"zh": {
"prefs.theme_file_or_paste": "文件或粘贴",
"prefs.theme_import_hint": "单击选择主题文件,拖放到此处,或复制主题后按 Ctrl+V(Mac 上为 Cmd+V)",
"prefs.theme_paste": "粘贴主题",
"prefs.theme_paste_none": "剪贴板中没有 SolidCraft 主题"
}
}
2 changes: 2 additions & 0 deletions crates/ui-egui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2128,6 +2128,8 @@ impl SolveApp {
open_elsewhere::tick(self, ctx);
update::tick(self, ctx);
if self.issue_report.draft.is_none() && self.file_picker.is_none() {
// Files dropped on the theme Import tile are themes (#8226).
prefs::take_theme_drops(self, ctx);
// Files dropped on the agent chat attach to its next message instead.
agent_chat::take_drops(self, ctx);
for f in ctx.input(|i| i.raw.dropped_files.clone()) {
Expand Down
151 changes: 140 additions & 11 deletions crates/ui-egui/src/prefs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,8 @@ pub struct PrefsWindow {
land_on: Option<usize>,
/// The row to scroll to after a result is chosen.
reveal: Option<crate::prefs_search::Reveal>,
/// Where the theme Import tile was drawn last frame: a file dropped there is a theme (#8226).
import_tile: Option<egui::Rect>,
}

/// Following a theme file needs the file system: the desktop app only (#7573).
Expand Down Expand Up @@ -1227,6 +1229,7 @@ fn theme_row(app: &mut SolveApp, ui: &mut egui::Ui) {
let mut action_theme: Option<String> = None;
let mut chosen: Option<String> = None;
let mut action: Option<&str> = None;
let mut import_tile = None;
ui_kit::theme_rail(
ui,
|ui| {
Expand Down Expand Up @@ -1279,11 +1282,25 @@ fn theme_row(app: &mut SolveApp, ui: &mut egui::Ui) {
}
},
|ui| {
if ui_kit::theme_import_tile(ui, app.tr_label("Import…"), app.tr("prefs.theme_file")).clicked() {
let tile = ui_kit::theme_import_tile(ui, app.tr_label("Import…"), app.tr("prefs.theme_file_or_paste"))
.on_hover_text(app.tr("prefs.theme_import_hint"));
import_tile = Some(tile.rect);
if tile.clicked() {
action = Some("import");
}
// The keyboard's paste needs no menu; this is for touch screens and the browser's mouse users.
#[cfg(not(target_arch = "wasm32"))]
ui_kit::Menu::context(&tile, |ui| {
ui_kit::Menu::show(ui, Tokens::get().form_card_width, |ui| {
if ui_kit::MenuItem::new("theme_paste", app.tr("prefs.theme_paste"), "paste").show(ui).clicked() {
action = Some("paste");
ui.close();
}
});
});
},
);
app.prefs_window.import_tile = import_tile;
if let Some((kind, id)) = tile_action {
action = Some(kind);
action_theme = Some(id);
Expand All @@ -1297,17 +1314,18 @@ fn theme_row(app: &mut SolveApp, ui: &mut egui::Ui) {
Some("import") => app.pick_open_for(&["json"], |app, path| {
let Some(path) = path else { return };
// Through `vfs`, like every other import: in the web app a picked file is there.
let r = solidcraft_engine::io::vfs::read_to_string(&path, crate::theme_files::MAX_BYTES)
.map_err(|e| e.to_string())
.and_then(|text| import_theme(app, &serde_json::Value::String(text)));
match r {
Ok(id) => {
let name = crate::theme_files::find(&id, &app.preferences.custom_themes).map(|t| t.name.clone()).unwrap_or(id);
app.set_status(format!("Theme imported: {name}"), false);
}
Err(e) => app.set_status(format!("Theme import: {e}"), true),
}
let text = solidcraft_engine::io::vfs::read_to_string(&path, crate::theme_files::MAX_BYTES).map_err(|e| e.to_string());
import_theme_text(app, text);
}),
#[cfg(not(target_arch = "wasm32"))]
Some("paste") => {
let text = arboard::Clipboard::new().and_then(|mut c| c.get_text()).unwrap_or_default();
if looks_like_theme(&text) {
import_theme_text(app, Ok(text));
} else {
app.set_status(app.tr("prefs.theme_paste_none").to_string(), true);
}
}
Some("export") => {
let system_dark = ui.ctx().system_theme().map_or(app.ui.dark, |theme| theme == egui::Theme::Dark);
let file = theme_file(app, action_theme.as_deref().unwrap_or(&app.preferences.theme), system_dark);
Expand Down Expand Up @@ -1759,6 +1777,7 @@ pub fn show(app: &mut SolveApp, ctx: &egui::Context) {
app.keymap.open = false;
open_at(app, PAGE_SHORTCUTS);
}
app.prefs_window.import_tile = None;
if !app.prefs_window.open {
app.prefs_window.filter.clear();
app.prefs_window.search_from = None;
Expand Down Expand Up @@ -2602,6 +2621,60 @@ fn select_theme(app: &mut SolveApp, id: &str) {

/// Import a theme file (its text, or its JSON) into the preferences and turn it on; returns
/// the id it is kept under.
/// Import a theme file's text, from a file, a paste or a drop, and say what happened in the status bar.
fn import_theme_text(app: &mut SolveApp, text: Result<String, String>) {
match text.and_then(|text| import_theme(app, &serde_json::Value::String(text))) {
Ok(id) => {
let name = crate::theme_files::find(&id, &app.preferences.custom_themes).map(|t| t.name.clone()).unwrap_or(id);
app.set_status(format!("Theme imported: {name}"), false);
}
Err(e) => app.set_status(format!("Theme import: {e}"), true),
}
}

/// Text that is meant as a theme file: a JSON object naming the theme format. Anything else
/// pasted while Preferences is open keeps its usual meaning.
fn looks_like_theme(text: &str) -> bool {
text.trim_start().starts_with('{') && text.contains(crate::theme_files::FORMAT)
}

/// Ctrl/Cmd+V on the Interface page with a theme file's text on the clipboard imports it, like
/// Import… with a file (#8226). Called before the global Copy and Paste shortcuts, which would
/// otherwise paste copied components; the paste is taken out of the input once it is a theme's.
pub fn take_theme_paste(app: &mut SolveApp, ctx: &egui::Context) {
if !app.prefs_window.open || app.prefs_window.section != PAGE_INTERFACE {
return;
}
let Some(text) = ctx.input(|i| i.events.iter().find_map(|e| if let egui::Event::Paste(t) = e { Some(t.clone()) } else { None })) else {
return;
};
if !looks_like_theme(&text) {
return;
}
ctx.input_mut(|i| {
i.events.retain(|e| {
!matches!(e, egui::Event::Paste(_) | egui::Event::Key { key: egui::Key::V, .. } | egui::Event::Key { key: egui::Key::Insert, .. })
})
});
import_theme_text(app, Ok(text));
}

/// Files dropped on the theme Import tile are themes to import, not designs to open (#8226).
pub fn take_theme_drops(app: &mut SolveApp, ctx: &egui::Context) {
let Some(rect) = app.prefs_window.import_tile.filter(|_| app.prefs_window.open) else { return };
if !ctx.input(|i| !i.raw.dropped_files.is_empty() && i.pointer.latest_pos().is_some_and(|p| rect.contains(p))) {
return;
}
let files = ctx.input_mut(|i| std::mem::take(&mut i.raw.dropped_files));
for f in files.iter().take(8) {
let text = f
.bytes()
.and_then(|b| if b.len() > crate::theme_files::MAX_BYTES { Err("the file is too large for a theme".into()) } else { Ok(b) })
.and_then(|b| String::from_utf8(b).map_err(|_| "not a theme file".to_string()));
import_theme_text(app, text);
}
}

pub fn import_theme(app: &mut SolveApp, v: &serde_json::Value) -> Result<String, String> {
let theme = match v {
serde_json::Value::String(text) => crate::theme_files::parse(text)?,
Expand Down Expand Up @@ -4440,6 +4513,62 @@ mod tests {
/// web app a picked file is kept there, where `std::fs` can't read it (#7643). On the disk it
/// is the same file; a folder scope active while it is read refuses a path outside it, which
/// shows the read goes through `vfs`.
#[test]
fn a_theme_pasted_or_dropped_on_the_interface_page_imports_and_other_pastes_pass_through() {
use serde_json::json;
let mut h = crate::scenario::Harness::new();
h.app.home.open = false;
let theme = |name: &str| format!(r##"{{"format":"solidcraft-theme/1","name":"{name}","base":"dark","colors":{{"panel":"#102030"}}}}"##);
let has = |h: &crate::scenario::Harness, id: &str| h.app.preferences.custom_themes.iter().any(|t| t.id == id);

// Another page: a theme's text is left to the usual paste.
h.call("ui.prefs", json!({"open":"General"}));
h.frames(2);
h.frame_with(vec![egui::Event::Paste(theme("Elsewhere"))]);
assert!(!has(&h, "elsewhere"));

// The Interface page: plain text is not a theme and changes nothing.
h.call("ui.prefs", json!({"open":"Interface"}));
h.frames(2);
let before = h.app.preferences.clone();
h.frame_with(vec![egui::Event::Paste("just some words".into())]);
assert_eq!(h.app.preferences.custom_themes, before.custom_themes);

// A theme's text imports and turns on, as Import… does with a file.
h.frame_with(vec![egui::Event::Paste(theme("Pasted"))]);
assert!(has(&h, "pasted"), "{:?}", h.app.preferences.custom_themes.iter().map(|t| &t.id).collect::<Vec<_>>());
assert_eq!(h.app.preferences.theme, "pasted");

// An invalid theme says why, and imports nothing.
h.frame_with(vec![egui::Event::Paste(r#"{"format":"solidcraft-theme/1","name":"Bad","base":"sideways"}"#.into())]);
assert!(!has(&h, "bad"));

// A file dropped on the Import tile is a theme to import, not a design to open.
h.frames(2);
let tile = h.app.prefs_window.import_tile.expect("the Import tile is drawn on the Interface page");
let dir = std::env::temp_dir().join(format!("solidcraft-theme-drop-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let file = dir.join("dropped.json");
std::fs::write(&file, theme("Dropped")).unwrap();
#[derive(Debug)]
struct File(std::path::PathBuf);
impl egui::DroppedFile for File {
fn path(&self) -> &std::path::Path {
&self.0
}
fn bytes(&self) -> Result<Vec<u8>, String> {
std::fs::read(&self.0).map_err(|e| e.to_string())
}
}
let tabs = h.app.docs.count();
h.frame_with(vec![egui::Event::PointerMoved(tile.center())]);
h.drop_files(tile.center(), vec![std::sync::Arc::new(File(file)) as egui::DroppedFileHandle]);
h.frames(2);
assert!(has(&h, "dropped"));
assert_eq!(h.app.docs.count(), tabs, "the drop opened no design");
let _ = std::fs::remove_dir_all(&dir);
}

#[test]
fn theme_import_reads_through_vfs() {
use serde_json::json;
Expand Down
2 changes: 2 additions & 0 deletions crates/ui-egui/src/toolbar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2167,6 +2167,8 @@ pub fn shortcuts(app: &mut SolveApp, ctx: &egui::Context) {
.collect();
(keys, i.modifiers)
});
// A theme pasted on the Interface page is a theme, not copied components (#8226).
crate::prefs::take_theme_paste(app, ctx);
// Home hides the design: only New, Open and Preferences work there.
let shown = design_shown(app);
if shown {
Expand Down
5 changes: 4 additions & 1 deletion docs/book/src/guide/ui-tour.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,10 @@ pills when `on_accent` has less than 4.5:1 contrast with the fill. See the

- **Import…** adds a theme file and turns it on. Importing a file with the same name again updates
that theme. A file with an unknown colour name or a malformed colour is refused, and the message
names every problem.
names every problem. A theme shared as text imports the same way without a file: with the
Interface page open, press Ctrl+V (Cmd+V on a Mac), or right-click the Import tile and choose
**Paste theme**. Pasted text that isn't a theme is left alone. A theme file dropped on the tile
imports too.
- **Export…** saves the theme on now as a complete file listing every colour and radius. It is a good starting
point for your own theme.
- **Remove** deletes the selected imported theme.
Expand Down
Loading