DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
View: leave Presentation mode with Esc or Shift+W
aiongg7b903cf6b16b
diff --git a/crates/ui-egui/src/i18n.rs b/crates/ui-egui/src/i18n.rsindex e5e8a8d08..5e56abd50 100644--- a/crates/ui-egui/src/i18n.rs+++ b/crates/ui-egui/src/i18n.rs@@ -1233,6 +1233,16 @@ const TABLE: &[(&str, [&str; 5])] = &[("Text Anchors", ["Text Anchors", "Text Anchors", "Text Anchors", "Text Anchors", "文本锚点"]),("Text Variables", ["Text Variables", "Text Variables", "Text Variables", "Text Variables", "文本变量"]),("Toggle Normal/Preview", ["Toggle Normal/Preview", "Toggle Normal/Preview", "Toggle Normal/Preview", "Toggle Normal/Preview", "切换正常/预览"]),+ (+ "Toggle Presentation",+ [+ "Präsentation ein/aus",+ "Activer/désactiver la présentation",+ "Activar/desactivar presentación",+ "プレゼンテーションを切り替え",+ "切换演示模式",+ ],+ ),("Toggle Selection", ["Toggle Selection", "Toggle Selection", "Toggle Selection", "Toggle Selection", "切换选择"]),("Tools: Double Column", ["Tools: Double Column", "Tools: Double Column", "Tools: Double Column", "Tools: Double Column", "工具: 双列"]),("Transform Again", ["Transform Again", "Transform Again", "Transform Again", "Transform Again", "再次变换"]),diff --git a/crates/ui-egui/src/i18n/ar.rs b/crates/ui-egui/src/i18n/ar.rsindex 10e393cd2..a69342ac7 100644--- a/crates/ui-egui/src/i18n/ar.rs+++ b/crates/ui-egui/src/i18n/ar.rs@@ -1332,6 +1332,7 @@ pub(super) const TABLE: &[(&str, &str)] = &[("Text Anchors", "نقاط ارتساء النص"),("Text Variables", "متغيرات النص"),("Toggle Normal/Preview", "تبديل العرض العادي/المعاينة"),+ ("Toggle Presentation", "تبديل وضع العرض التقديمي"),("Toggle Selection", "تبديل التحديد"),("Tools: Double Column", "الأدوات: عمودان"),("Transform Panel", "لوحة التحويل"),diff --git a/crates/ui-egui/src/i18n/it.rs b/crates/ui-egui/src/i18n/it.rsindex c91afd6ba..76c87c46f 100644--- a/crates/ui-egui/src/i18n/it.rs+++ b/crates/ui-egui/src/i18n/it.rs@@ -673,6 +673,7 @@ pub(super) const TABLE: &[(&str, &str)] = &[("Text Anchors", "Ancoraggi testo"),("Text Variables", "Variabili di testo"),("Toggle Normal/Preview", "Alterna Normale/Anteprima"),+ ("Toggle Presentation", "Attiva/disattiva presentazione"),("Toggle Selection", "Alterna selezione"),("Tools: Double Column", "Strumenti: doppia colonna"),("Transform Again", "Ripeti trasformazione"),diff --git a/crates/ui-egui/src/i18n/ja.rs b/crates/ui-egui/src/i18n/ja.rsindex be6e2c4da..3058fabe5 100644--- a/crates/ui-egui/src/i18n/ja.rs+++ b/crates/ui-egui/src/i18n/ja.rs@@ -675,6 +675,7 @@ pub(super) const TABLE: &[(&str, &str)] = &[("Text Anchors", "テキストアンカー"),("Text Variables", "テキスト変数"),("Toggle Normal/Preview", "通常表示・プレビューを切り替え"),+ ("Toggle Presentation", "プレゼンテーションを切り替え"),("Toggle Selection", "選択範囲への追加・解除"),("Tools: Double Column", "ツール: 2列"),("Transform Again", "変形を再適用"),diff --git a/crates/ui-egui/src/i18n/pt_br.rs b/crates/ui-egui/src/i18n/pt_br.rsindex d25717dc6..bc95b1e11 100644--- a/crates/ui-egui/src/i18n/pt_br.rs+++ b/crates/ui-egui/src/i18n/pt_br.rs@@ -114,6 +114,7 @@ pub(super) const TABLE: &[(&str, &str)] = &[("Fit Spread in Window", "Ajustar página dupla na janela"),("Actual Size", "Tamanho real"),("Screen Mode", "Modo de tela"),+ ("Toggle Presentation", "Alternar apresentação"),("Normal", "Normal"),("Preview", "Visualização"),("Rulers", "Réguas"),diff --git a/crates/ui-egui/src/i18n/uk.rs b/crates/ui-egui/src/i18n/uk.rsindex 9a9fb89ba..840e21963 100644--- a/crates/ui-egui/src/i18n/uk.rs+++ b/crates/ui-egui/src/i18n/uk.rs@@ -676,6 +676,7 @@ pub(super) const TABLE: &[(&str, &str)] = &[("Text Anchors", "Текстові прив’язки"),("Text Variables", "Текстові змінні"),("Toggle Normal/Preview", "Перемкнути звичайний режим / перегляд"),+ ("Toggle Presentation", "Перемкнути режим презентації"),("Toggle Selection", "Переключити виділення"),("Tools: Double Column", "Інструменти: дві колонки"),("Transform Again", "Трансформувати знову"),diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex c003154ad..efc60fbb7 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -165,6 +165,8 @@ impl Default for PdfExportSettings {pub struct UiState {pub brightness: theme::Brightness,pub screen_mode: ScreenMode,+ /// The screen mode leaving Presentation returns to: the one it was entered from.+ pub presentation_return: ScreenMode,pub frame_edges: bool,pub rulers: bool,pub guides: bool,@@ -294,6 +296,7 @@ impl Default for UiState {UiState {brightness: theme::Brightness::MediumDark,screen_mode: ScreenMode::Normal,+ presentation_return: ScreenMode::Normal,frame_edges: true,rulers: true,guides: true,diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rsindex d123ec4eb..a1f7637ec 100644--- a/crates/ui-egui/src/menus.rs+++ b/crates/ui-egui/src/menus.rs@@ -85,6 +85,12 @@ pub const UI_COMMANDS: &[(&str, &str, Option<&str>, &str)] = &[("view.zoom", "Zoom To", None, "{zoom: 1.0 = 100%}"),("view.screenMode", "Screen Mode", None, "{mode: normal|preview|bleed|slug|presentation}"),("view.togglePreview", "Toggle Normal/Preview", Some("W"), "{}"),+ (+ "view.togglePresentation",+ "Toggle Presentation",+ Some("Shift+W"),+ "{} — enters Presentation, or returns to the mode it was entered from; Esc also leaves it",+ ),("view.frameEdges", "Show/Hide Frame Edges", Some("Cmd+H"), "{}"),("view.rulers", "Show/Hide Rulers", Some("Cmd+R"), "{}"),("view.guides", "Show/Hide Guides", Some("Cmd+;"), "{}"),@@ -629,6 +635,7 @@ pub const MENUS: &[(&str, &[&str])] = &["-","ui:view.overprintPreview","ui:view.togglePreview",+ "ui:view.togglePresentation",">Display Performance","ui:view.fastDisplay","ui:view.typicalDisplay",@@ -1096,17 +1103,29 @@ pub fn run_ui(app: &mut DesignApp, id: &str, p: &Value) -> Option<Result<Value,Ok(Value::Null)}"view.screenMode" => {- app.ui.screen_mode = match p.get("mode").and_then(Value::as_str).unwrap_or("normal") {+ let mode = match p.get("mode").and_then(Value::as_str).unwrap_or("normal") {"preview" => ScreenMode::Preview,"bleed" => ScreenMode::Bleed,"slug" => ScreenMode::Slug,"presentation" => ScreenMode::Presentation,_ => ScreenMode::Normal,};+ set_screen_mode(app, mode);Ok(Value::Null)}"view.togglePreview" => {- app.ui.screen_mode = if app.ui.screen_mode == ScreenMode::Normal { ScreenMode::Preview } else { ScreenMode::Normal };+ let mode = if app.ui.screen_mode == ScreenMode::Normal { ScreenMode::Preview } else { ScreenMode::Normal };+ set_screen_mode(app, mode);+ Ok(Value::Null)+ }+ "view.togglePresentation" => {+ let mode = match (app.ui.screen_mode, app.ui.presentation_return) {+ // A settings file can hold Presentation as the return mode; leaving must still leave.+ (ScreenMode::Presentation, ScreenMode::Presentation) => ScreenMode::Normal,+ (ScreenMode::Presentation, back) => back,+ _ => ScreenMode::Presentation,+ };+ set_screen_mode(app, mode);Ok(Value::Null)}"view.frameEdges" => flag(&mut app.ui.frame_edges),@@ -1478,6 +1497,14 @@ pub fn run_ui(app: &mut DesignApp, id: &str, p: &Value) -> Option<Result<Value,}/// Save by handing the serialized document to the host as a download (web).+/// Switch screen modes; entering Presentation remembers the mode to return to.+fn set_screen_mode(app: &mut DesignApp, mode: ScreenMode) {+ if mode == ScreenMode::Presentation && app.ui.screen_mode != ScreenMode::Presentation {+ app.ui.presentation_return = app.ui.screen_mode;+ }+ app.ui.screen_mode = mode;+}+fn download_document(app: &mut DesignApp) -> Result<Value, String> {let name = app.session.active().map(|d| format!("{}.designcraft", d.doc.title)).ok_or("no document")?;let ser = app.run("file.serialize", json!({}))?;@@ -1792,6 +1819,7 @@ pub fn checked(app: &DesignApp, id: &str, params: &Value) -> Option<bool> {"window.taskBarPin" => app.ui.task_bar_pin.is_some(),"window.toolsDoubleColumn" => app.ui.tools_double_column,"view.togglePreview" => app.ui.screen_mode == crate::ScreenMode::Preview,+ "view.togglePresentation" => app.ui.screen_mode == crate::ScreenMode::Presentation,"window.brightness" => params.get("brightness").and_then(Value::as_str) == Some(app.ui.brightness.id()),"type.storyDirection" => {let st = app.session.active()?;@@ -2118,6 +2146,13 @@ pub fn shortcuts(app: &mut DesignApp, ctx: &egui::Context) {let events = ctx.input(|i| i.events.clone());for e in events {let egui::Event::Key { key, pressed: true, modifiers, repeat: false, .. } = e else { continue };+ // Presentation hides every control, so Esc always leaves it, and only that: the canvas must+ // not also see the press and switch tools or deselect.+ if key == egui::Key::Escape && app.ui.screen_mode == ScreenMode::Presentation {+ ctx.input_mut(|i| i.consume_key(modifiers, key));+ let _ = app.run("view.togglePresentation", json!({}));+ continue;+ }// Command shortcuts.let mut fired: Option<String> = None;let all = effective_shortcuts(app);@@ -2948,6 +2983,60 @@ mod tests {assert_eq!(app.session.tool_id(), "rectangleFrame");}+ #[test]+ fn escape_and_shift_w_leave_presentation_for_the_previous_mode() {+ // Presentation hides every control, so the keyboard must get out of it (#493).+ let mut app = crate::DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());+ app.session.execute("file.new", &json!({})).unwrap();+ app.session.execute("frame.create", &json!({"rect": [36, 36, 300, 200], "content": "graphic"})).unwrap();+ let ctx = egui::Context::default();+ let frame = |app: &mut crate::DesignApp, events: Vec<egui::Event>| {+ let input = egui::RawInput {+ screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(1400.0, 900.0))),+ events,+ ..Default::default()+ };+ let mut out = ctx.run_ui(input, |ui| {+ app.logic(&ui.ctx().clone());+ app.ui(ui);+ });+ out.textures_delta.clear();+ };+ let key = |k: egui::Key, modifiers: egui::Modifiers| {+ vec![+ egui::Event::Key { key: k, physical_key: None, pressed: true, repeat: false, modifiers },+ egui::Event::Key { key: k, physical_key: None, pressed: false, repeat: false, modifiers },+ ]+ };+ let selected = |app: &crate::DesignApp| app.session.active().map(|d| d.selection.items.len()).unwrap_or(0);+ frame(&mut app, vec![]);+ frame(&mut app, vec![]);+ app.run("view.screenMode", json!({"mode": "preview"})).unwrap();+ app.select_tool("rectangleFrame");+ assert_eq!(selected(&app), 1, "the new frame is selected");++ frame(&mut app, key(egui::Key::W, egui::Modifiers::SHIFT));+ assert_eq!(app.ui.screen_mode, ScreenMode::Presentation, "Shift+W enters Presentation");+ assert_eq!(app.session.tool_id(), "rectangleFrame", "Shift+W is not a tool shortcut");+ frame(&mut app, key(egui::Key::Escape, egui::Modifiers::NONE));+ assert_eq!(app.ui.screen_mode, ScreenMode::Preview, "Esc returns to the mode Presentation was entered from");+ assert_eq!(app.session.tool_id(), "rectangleFrame", "Esc only left Presentation");+ assert_eq!(selected(&app), 1, "Esc only left Presentation");++ frame(&mut app, key(egui::Key::W, egui::Modifiers::SHIFT));+ frame(&mut app, key(egui::Key::W, egui::Modifiers::SHIFT));+ assert_eq!(app.ui.screen_mode, ScreenMode::Preview, "Shift+W leaves Presentation for the previous mode");++ app.run("view.screenMode", json!({"mode": "presentation"})).unwrap();+ frame(&mut app, key(egui::Key::W, egui::Modifiers::NONE));+ assert_eq!(app.ui.screen_mode, ScreenMode::Normal, "W from any mode but Normal goes to Normal");++ // Outside Presentation, Esc keeps its usual meaning.+ frame(&mut app, key(egui::Key::Escape, egui::Modifiers::NONE));+ assert_eq!(app.ui.screen_mode, ScreenMode::Normal);+ assert_eq!(app.session.tool_id(), "selection");+ }+#[test]fn the_quick_apply_shortcut_opens_it_without_running_an_entry() {let mut app = crate::DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());