Browse Source

Mechanism to select commands by type

Source commit: 9c38e728db12668e0cd941dc0a73db8388660148
Martin Prikryl 9 years ago
parent
commit
5066977d41

+ 53 - 42
source/forms/CustomScpExplorer.cpp

@@ -1550,24 +1550,8 @@ bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
   return (FTerminal != NULL) && (FTerminal->IsCapable[fcSecondaryShell] || FTerminal->IsCapable[fcShellAnyCommand]);
 }
 //---------------------------------------------------------------------------
-int __fastcall TCustomScpExplorerForm::BothCustomCommandState(const TCustomCommandType & Command)
-{
-  bool Result = FLAGSET(Command.Params, ccLocal);
-  if (Result)
-  {
-    TLocalCustomCommand LocalCustomCommand;
-    TInteractiveCustomCommand InteractiveCustomCommand(&LocalCustomCommand);
-    UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
-
-    Result =
-      LocalCustomCommand.IsFileCommand(Cmd) &&
-      LocalCustomCommand.HasLocalFileName(Cmd);
-  }
-  return Result ? 1 : -1;
-}
-//---------------------------------------------------------------------------
 int __fastcall TCustomScpExplorerForm::CustomCommandState(
-  const TCustomCommandType & Command, bool OnFocused)
+  const TCustomCommandType & Command, bool /*OnFocused*/, TCustomCommandListType ListType)
 {
   int Result;
 
@@ -1580,20 +1564,29 @@ int __fastcall TCustomScpExplorerForm::CustomCommandState(
 
   if (FLAGCLEAR(Command.Params, ccLocal))
   {
-    Result = CustomCommandRemoteAllowed();
-    if (Result)
+    int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
+    // custom command that does not operate with files can be executed anytime ...
+    if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
+    {
+      if ((ListType == ccltAll) || (ListType == ccltNonFile))
+      {
+        // ... but do not show such command in remote file menu (TODO)
+        Result = AllowedState;
+      }
+      else
+      {
+        Result = -1;
+      }
+    }
+    else
     {
-      // custom command that does not operate with files can be executed anytime ...
-      if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
+      if (ListType == ccltAll)
       {
-        // ... but do not show such command in remote file menu
-        // (TODO, currently custom commands are in file menu only, so we cannot hide
-        // such command, because it won't be accessible from elsewhere)
-        Result = OnFocused ? /*-1*/ true : true;
+        Result = ((FCurrentSide == osRemote) && EnableSelectedOperation[osRemote]) ? AllowedState : 0;
       }
       else
       {
-        Result = (FCurrentSide == osRemote) && EnableSelectedOperation[osRemote];
+        Result = -1;
       }
     }
   }
@@ -1602,22 +1595,40 @@ int __fastcall TCustomScpExplorerForm::CustomCommandState(
     // custom command that does not operate with files can be executed anytime
     if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
     {
-      Result = true;
+      Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
     }
     else if (LocalCustomCommand.HasLocalFileName(Cmd))
     {
-      // special case is "diff"-style command that can be executed over any side,
-      // if we have both sides
-      Result =
-        // Cannot have focus on both panels, so we have to call AnyFileSelected
-        // directly (instead of EnableSelectedOperation) to pass
-        // false to FocusedFileOnlyWhenFocused when panel is inactive.
-        (HasDirView[osLocal] && DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
-        DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote));
+      if (ListType == ccltAll)
+      {
+        // special case is "diff"-style command that can be executed over any side,
+        // if we have both sides
+        Result =
+          // Cannot have focus on both panels, so we have to call AnyFileSelected
+          // directly (instead of EnableSelectedOperation) to pass
+          // false to FocusedFileOnlyWhenFocused when panel is inactive.
+          ((HasDirView[osLocal] && DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
+            DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
+      }
+      else if (ListType == ccltBoth)
+      {
+        Result = 1;
+      }
+      else
+      {
+        Result = -1;
+      }
     }
     else
     {
-      Result = EnableSelectedOperation[FCurrentSide];
+      if (ListType == ccltAll)
+      {
+        Result = EnableSelectedOperation[FCurrentSide] ? 1 : 0;
+      }
+      else
+      {
+        Result = -1;
+      }
     }
   }
 
@@ -2053,12 +2064,12 @@ void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
   {
     FCustomCommandMenu->Items->Clear();
 
-    NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, true);
+    NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth);
     MenuPopup(FCustomCommandMenu, Button);
   }
   else
   {
-    NonVisualDataModule->CreateCustomCommandsMenu(Action, false, true);
+    NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
   }
 }
 //---------------------------------------------------------------------------
@@ -3972,7 +3983,7 @@ void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
   if (Command != NULL)
   {
     KeyProcessed(Key, Shift);
-    if (CustomCommandState(*Command, false) > 0)
+    if (CustomCommandState(*Command, false, ccltAll) > 0)
     {
       ExecuteFileOperationCommand(foCustomCommand, osRemote,
         false, false, const_cast<TCustomCommandType *>(Command));
@@ -7792,7 +7803,7 @@ void __fastcall TCustomScpExplorerForm::PreferencesDialog(
 void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
   const TCustomCommandType & Command)
 {
-  if (CustomCommandState(Command, FEditingFocusedAdHocCommand) <= 0)
+  if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
   {
     throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (Command.Command)));
   }
@@ -7830,7 +7841,7 @@ void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
 {
   DebugAssert(!FLastCustomCommand.Command.IsEmpty());
 
-  int State = CustomCommandState(FLastCustomCommand, OnFocused);
+  int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
   DebugAssert(State > 0);
   if (State <= 0)
   {
@@ -7849,7 +7860,7 @@ bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
   {
     Command = FLastCustomCommand;
 
-    State = CustomCommandState(FLastCustomCommand, OnFocused);
+    State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
   }
 
   return Result;

+ 2 - 2
source/forms/CustomScpExplorer.h

@@ -56,6 +56,7 @@ enum TPanelExportDestination { pedClipboard, pedCommandLine };
 enum TCopyOperationCommandFlag {
   cocNone = 0x00, cocShortCutHint = 0x01, cocQueue = 0x02, cocNonQueue = 0x04
 };
+enum TCustomCommandListType { ccltAll, ccltBoth, ccltNonFile };
 //---------------------------------------------------------------------------
 class TCustomScpExplorerForm : public TForm
 {
@@ -584,8 +585,7 @@ public:
   virtual bool __fastcall DirViewEnabled(TOperationSide Side);
   virtual void __fastcall ChangePath(TOperationSide Side) = 0;
   virtual void __fastcall StoreParams();
-  int __fastcall CustomCommandState(const TCustomCommandType & Command, bool OnFocused);
-  int __fastcall BothCustomCommandState(const TCustomCommandType & Command);
+  int __fastcall CustomCommandState(const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType);
   bool __fastcall GetLastCustomCommand(bool OnFocused,
     TCustomCommandType & CustomCommand, int & State);
   void __fastcall LastCustomCommand(bool OnFocused);

+ 16 - 25
source/forms/NonVisual.cpp

@@ -779,7 +779,7 @@ void __fastcall TNonVisualDataModule::ExplorerActionsExecute(
     EXE(EditorListCustomizeAction, PreferencesDialog(pmEditor))
 
     // CUSTOM COMMANDS
-    EXE(CustomCommandsAction, CreateCustomCommandsMenu(CustomCommandsAction))
+    EXE(CustomCommandsAction, CreateCustomCommandsMenu(CustomCommandsAction, ccltAll))
     EXE(CustomCommandsEnterAction, ScpExplorer->AdHocCustomCommand(false))
     EXE(CustomCommandsEnterFocusedAction, ScpExplorer->AdHocCustomCommand(true))
     EXE(CustomCommandsLastAction, ScpExplorer->LastCustomCommand(false))
@@ -1041,21 +1041,12 @@ const int CustomCommandExtension = 0x0400;
 const int CustomCommandIndexMask = 0x00FF;
 //---------------------------------------------------------------------------
 void __fastcall TNonVisualDataModule::CreateCustomCommandsListMenu(
-  TCustomCommandList * List, TTBCustomItem * Menu, bool OnFocused, bool Toolbar, bool Both, int Tag)
+  TCustomCommandList * List, TTBCustomItem * Menu, bool OnFocused, bool Toolbar, TCustomCommandListType ListType, int Tag)
 {
   for (int Index = 0; Index < List->Count; Index++)
   {
     const TCustomCommandType * Command = List->Commands[Index];
-    int State;
-
-    if (!Both)
-    {
-      State = ScpExplorer->CustomCommandState(*Command, OnFocused);
-    }
-    else
-    {
-      State = ScpExplorer->BothCustomCommandState(*Command);
-    }
+    int State = ScpExplorer->CustomCommandState(*Command, OnFocused, ListType);
 
     if (State >= 0)
     {
@@ -1067,12 +1058,12 @@ void __fastcall TNonVisualDataModule::CreateCustomCommandsListMenu(
       {
         Item->Tag = Item->Tag | CustomCommandOnFocused;
       }
-      if (Both)
+      if (ListType == ccltBoth)
       {
         Item->Tag = Item->Tag | CustomCommandBoth;
       }
       Item->Hint = CustomCommandHint(Command);
-      if (!Both)
+      if (ListType != ccltBoth)
       {
         Item->ShortCut = Command->ShortCut;
       }
@@ -1084,13 +1075,13 @@ void __fastcall TNonVisualDataModule::CreateCustomCommandsListMenu(
 }
 //---------------------------------------------------------------------------
 void __fastcall TNonVisualDataModule::CreateCustomCommandsMenu(
-  TTBCustomItem * Menu, bool OnFocused, bool Toolbar, bool Both)
+  TTBCustomItem * Menu, bool OnFocused, bool Toolbar, TCustomCommandListType ListType)
 {
-  CreateCustomCommandsListMenu(WinConfiguration->CustomCommandList, Menu, OnFocused, Toolbar, Both, 0);
+  CreateCustomCommandsListMenu(WinConfiguration->CustomCommandList, Menu, OnFocused, Toolbar, ListType, 0);
 
   TTBCustomItem * Item;
 
-  if (!Both)
+  if (ListType == ccltAll)
   {
     Item = new TTBXItem(Menu);
     Item->Action = OnFocused ? CustomCommandsEnterFocusedAction : CustomCommandsEnterAction;
@@ -1108,11 +1099,11 @@ void __fastcall TNonVisualDataModule::CreateCustomCommandsMenu(
   TTBXSeparatorItem * Separator = AddMenuSeparator(Menu);
   Separator->Visible = (WinConfiguration->ExtensionList->Count > 0);
 
-  CreateCustomCommandsListMenu(WinConfiguration->ExtensionList, Menu, OnFocused, Toolbar, Both, CustomCommandExtension);
+  CreateCustomCommandsListMenu(WinConfiguration->ExtensionList, Menu, OnFocused, Toolbar, ListType, CustomCommandExtension);
 
   AddMenuSeparator(Menu);
 
-  if (!Toolbar && !Both)
+  if (!Toolbar && (ListType != ccltBoth))
   {
     Item = new TTBXItem(Menu);
     Item->Action = CustomCommandsBandAction;
@@ -1125,7 +1116,7 @@ void __fastcall TNonVisualDataModule::CreateCustomCommandsMenu(
 }
 //---------------------------------------------------------------------------
 void __fastcall TNonVisualDataModule::CreateCustomCommandsMenu(
-  TAction * Action, bool OnFocused, bool Both)
+  TAction * Action, bool OnFocused, TCustomCommandListType ListType)
 {
   DebugAssert(Action);
   TTBCustomItem * Menu = dynamic_cast<TTBCustomItem *>(Action->ActionComponent);
@@ -1133,7 +1124,7 @@ void __fastcall TNonVisualDataModule::CreateCustomCommandsMenu(
   {
     int PrevCount = Menu->Count;
 
-    CreateCustomCommandsMenu(Menu, OnFocused, false, Both);
+    CreateCustomCommandsMenu(Menu, OnFocused, false, ListType);
 
     for (int Index = 0; Index < PrevCount; Index++)
     {
@@ -1142,13 +1133,13 @@ void __fastcall TNonVisualDataModule::CreateCustomCommandsMenu(
   }
 }
 //---------------------------------------------------------------------------
-void __fastcall TNonVisualDataModule::CreateCustomCommandsMenu(TAction * Action)
+void __fastcall TNonVisualDataModule::CreateCustomCommandsMenu(TAction * Action, TCustomCommandListType ListType)
 {
   TTBCustomItem * Menu = dynamic_cast<TTBCustomItem *>(Action->ActionComponent);
   if (DebugAlwaysTrue(Menu != NULL))
   {
     bool OnFocused = (Menu == RemoteDirViewCustomCommandsMenu);
-    CreateCustomCommandsMenu(Action, OnFocused, false);
+    CreateCustomCommandsMenu(Action, OnFocused, ListType);
   }
 }
 //---------------------------------------------------------------------------
@@ -1179,7 +1170,7 @@ void __fastcall TNonVisualDataModule::UpdateCustomCommandsToolbarList(TTBXToolba
     TTBCustomItem * Item = Toolbar->Items->Items[Index];
     int CommandIndex2 = (Item->Tag & CustomCommandIndexMask);
     DebugAssert(CommandIndex2 == CommandIndex);
-    int State = ScpExplorer->CustomCommandState(*List->Commands[CommandIndex], false);
+    int State = ScpExplorer->CustomCommandState(*List->Commands[CommandIndex], false, ccltAll);
     DebugAssert(State >= 0);
     Item->Enabled = (State > 0);
   }
@@ -1224,7 +1215,7 @@ void __fastcall TNonVisualDataModule::UpdateCustomCommandsToolbar(TTBXToolbar *
     try
     {
       Toolbar->Items->Clear();
-      CreateCustomCommandsMenu(Toolbar->Items, false, true, false);
+      CreateCustomCommandsMenu(Toolbar->Items, false, true, ccltAll);
       DebugAssert(CommandCount + AdditionalCommands == Toolbar->Items->Count);
     }
     __finally

+ 5 - 4
source/forms/NonVisual.h

@@ -644,8 +644,9 @@ protected:
   UnicodeString __fastcall GetSessionFolderRoot(TSessionData * Data, int Level);
   void __fastcall CreateWorkspacesMenu(TAction * Action);
   void __fastcall WorkspaceItemClick(TObject * Sender);
-  void __fastcall CreateCustomCommandsListMenu(TCustomCommandList * List, TTBCustomItem * Menu, bool OnFocused, bool Toolbar, bool Both, int Tag);
-  void __fastcall CreateCustomCommandsMenu(TAction * Action);
+  void __fastcall CreateCustomCommandsListMenu(TCustomCommandList * List, TTBCustomItem * Menu, bool OnFocused,
+    bool Toolbar, TCustomCommandListType ListType, int Tag);
+  void __fastcall CreateCustomCommandsMenu(TAction * Action, TCustomCommandListType ListType);
   bool __fastcall CheckCustomCommandsToolbarList(TTBXToolbar * Toolbar, TCustomCommandList * List, int & Index);
   void __fastcall UpdateCustomCommandsToolbarList(TTBXToolbar * Toolbar, TCustomCommandList * List, int & Index);
   void __fastcall CreateSessionColorMenu(TAction * Action);
@@ -685,8 +686,8 @@ public:
   void __fastcall QueueSpeedComboBoxItem(TTBXComboBoxItem * Item);
   void __fastcall QueueSpeedComboBoxItemUpdate(TTBXComboBoxItem * Item);
   void __fastcall CreateCustomCommandsMenu(TTBCustomItem * Menu, bool OnFocused,
-    bool Toolbar, bool Both);
-  void __fastcall CreateCustomCommandsMenu(TAction * Action, bool OnFocused, bool Both);
+    bool Toolbar, TCustomCommandListType ListType);
+  void __fastcall CreateCustomCommandsMenu(TAction * Action, bool OnFocused, TCustomCommandListType ListType);
   TOnceDoneOperation __fastcall CurrentQueueOnceEmptyOperation();
   void __fastcall ResetQueueOnceEmptyOperation();
   void __fastcall StartBusy();