From 807796e3d14ba11f9ad0f0d49140b3fb4bd16e00 Mon Sep 17 00:00:00 2001 From: Vadim Lopatin Date: Tue, 27 Jan 2015 16:28:04 +0300 Subject: [PATCH] close file confirmation --- src/dlangide/ui/commands.d | 2 ++ src/dlangide/ui/frame.d | 70 +++++++++++++++++++++++++++++++++++++- views/res/i18n/en.ini | 1 + 3 files changed, 72 insertions(+), 1 deletion(-) diff --git a/src/dlangide/ui/commands.d b/src/dlangide/ui/commands.d index a70540e..a2e1430 100644 --- a/src/dlangide/ui/commands.d +++ b/src/dlangide/ui/commands.d @@ -30,6 +30,7 @@ enum IDEActions : int { DebugStop, DebugPause, HelpAbout, + WindowCloseAllDocuments, } const Action ACTION_FILE_NEW_SOURCE_FILE = new Action(IDEActions.FileNew, "MENU_FILE_NEW_SOURCE_FILE"c, "document-new", KeyCode.KEY_N, KeyFlag.Control); @@ -60,3 +61,4 @@ const Action ACTION_EDIT_CUT = new Action(EditorActions.Cut, "MENU_EDIT_CUT"c, " const Action ACTION_EDIT_UNDO = new Action(EditorActions.Undo, "MENU_EDIT_UNDO"c, "edit-undo"c, KeyCode.KEY_Z, KeyFlag.Control); const Action ACTION_EDIT_REDO = new Action(EditorActions.Redo, "MENU_EDIT_REDO"c, "edit-redo"c, KeyCode.KEY_Z, KeyFlag.Control|KeyFlag.Shift); const Action ACTION_HELP_ABOUT = new Action(IDEActions.HelpAbout, "MENU_HELP_ABOUT"c); +const Action ACTION_WINDOW_CLOSE_ALL_DOCUMENTS = new Action(IDEActions.WindowCloseAllDocuments, "MENU_WINDOW_CLOSE_ALL_DOCUMENTS"c); diff --git a/src/dlangide/ui/frame.d b/src/dlangide/ui/frame.d index 09ce7f1..ffb855e 100644 --- a/src/dlangide/ui/frame.d +++ b/src/dlangide/ui/frame.d @@ -142,6 +142,68 @@ class IDEFrame : AppFrame { _tabs.removeTab(tabId); } + /// close all editor tabs + void closeAllDocuments() { + for (int i = _tabs.tabCount - 1; i >= 0; i--) { + DSourceEdit ed = cast(DSourceEdit)_tabs.tabBody(i); + if (ed) { + closeTab(ed.id); + } + } + } + + /// returns first unsaved document + protected DSourceEdit hasUnsavedEdits() { + for (int i = _tabs.tabCount - 1; i >= 0; i--) { + DSourceEdit ed = cast(DSourceEdit)_tabs.tabBody(i); + if (ed && ed.content.modified) { + return ed; + } + } + return null; + } + + protected void askForUnsavedEdits(void delegate() onConfirm) { + DSourceEdit ed = hasUnsavedEdits(); + if (!ed) { + // no unsaved edits + onConfirm(); + return; + } + string tabId = ed.id; + // tab content is modified - ask for confirmation + window.showMessageBox(UIString("Close file "d ~ toUTF32(baseName(tabId))), UIString("Content of this file has been changed."d), + [ACTION_SAVE, ACTION_SAVE_ALL, ACTION_DISCARD_CHANGES, ACTION_DISCARD_ALL, ACTION_CANCEL], + 0, delegate(const Action result) { + if (result == StandardAction.Save) { + // save and close + ed.save(); + askForUnsavedEdits(onConfirm); + } else if (result == StandardAction.DiscardChanges) { + // close, don't save + closeTab(tabId); + closeAllDocuments(); + onConfirm(); + } else if (result == StandardAction.SaveAll) { + ed.save(); + for(;;) { + DSourceEdit editor = hasUnsavedEdits(); + if (!editor) + break; + editor.save(); + } + closeAllDocuments(); + onConfirm(); + } else if (result == StandardAction.DiscardAll) { + // close, don't save + closeAllDocuments(); + onConfirm(); + } + // else ignore + return true; + }); + } + protected void onTabClose(string tabId) { Log.d("onTabClose ", tabId); int index = _tabs.tabIndex(tabId); @@ -149,7 +211,7 @@ class IDEFrame : AppFrame { DSourceEdit d = cast(DSourceEdit)_tabs.tabBody(tabId); if (d && d.content.modified) { // tab content is modified - ask for confirmation - window.showMessageBox(UIString("Close tab"d), UIString("Content of "d ~ toUTF32(baseName(id)) ~ " file has been changed."d), + window.showMessageBox(UIString("Close tab"d), UIString("Content of "d ~ toUTF32(baseName(tabId)) ~ " file has been changed."d), [ACTION_SAVE, ACTION_DISCARD_CHANGES, ACTION_CANCEL], 0, delegate(const Action result) { if (result == StandardAction.Save) { @@ -231,6 +293,7 @@ class IDEFrame : AppFrame { MenuItem windowItem = new MenuItem(new Action(3, "MENU_WINDOW"c)); windowItem.add(new Action(30, "MENU_WINDOW_PREFERENCES")); + windowItem.add(ACTION_WINDOW_CLOSE_ALL_DOCUMENTS); MenuItem helpItem = new MenuItem(new Action(4, "MENU_HELP"c)); helpItem.add(new Action(40, "MENU_HELP_VIEW_HELP")); helpItem.add(ACTION_HELP_ABOUT); @@ -295,6 +358,11 @@ class IDEFrame : AppFrame { }; dlg.show(); return true; + case IDEActions.WindowCloseAllDocuments: + askForUnsavedEdits(delegate() { + closeAllDocuments(); + }); + return true; case IDEActions.FileOpenWorkspace: UIString caption; caption = "Open Workspace or Project"d; diff --git a/views/res/i18n/en.ini b/views/res/i18n/en.ini index 684f162..fa69540 100644 --- a/views/res/i18n/en.ini +++ b/views/res/i18n/en.ini @@ -43,6 +43,7 @@ MENU_VIEW_THEME_DEFAULT=&Default MENU_VIEW_THEME_CUSTOM1=&Custom 1 MENU_WINDOW=&WINDOW MENU_WINDOW_PREFERENCES=&Preferences +MENU_WINDOW_CLOSE_ALL_DOCUMENTS=Close All Documents MENU_HELP=&HELP MENU_HELP_VIEW_HELP=&View help MENU_HELP_ABOUT=&About