From 1a1028cbaa9c1d0aea7d8e466b69255c0748f687 Mon Sep 17 00:00:00 2001 From: WarmUpTill Date: Tue, 13 Aug 2024 21:39:24 +0200 Subject: [PATCH] Paste copied macro action depending on cursor position If cursor is above else action section paste it there. If cursor is above regular action section paste it there. In case of neither paste in original position. --- data/locale/en-US.ini | 2 ++ lib/macro/macro-segment-copy-paste.cpp | 20 ++++++++++++ lib/macro/macro-segment-copy-paste.hpp | 1 + lib/macro/macro-tab.cpp | 44 +++++++++++++++++--------- 4 files changed, 52 insertions(+), 15 deletions(-) diff --git a/data/locale/en-US.ini b/data/locale/en-US.ini index a88050fd..5864ff85 100644 --- a/data/locale/en-US.ini +++ b/data/locale/en-US.ini @@ -181,6 +181,8 @@ AdvSceneSwitcher.macroTab.segment.defaultCustomLabel="My label" AdvSceneSwitcher.macroTab.segment.setCustomLabel="Set label ..." AdvSceneSwitcher.macroTab.segment.copy="Copy" AdvSceneSwitcher.macroTab.segment.paste="Paste" +AdvSceneSwitcher.macroTab.segment.pasteAction="Paste as action" +AdvSceneSwitcher.macroTab.segment.pasteElseAction="Paste as else-action" AdvSceneSwitcher.macroTab.highlightSettings="Visual settings" AdvSceneSwitcher.macroTab.hotkeySettings="Hotkey settings" AdvSceneSwitcher.macroTab.generalSettings="General settings" diff --git a/lib/macro/macro-segment-copy-paste.cpp b/lib/macro/macro-segment-copy-paste.cpp index 37394811..d0105eaf 100644 --- a/lib/macro/macro-segment-copy-paste.cpp +++ b/lib/macro/macro-segment-copy-paste.cpp @@ -1,5 +1,7 @@ +#include "macro-segment-copy-paste.hpp" #include "advanced-scene-switcher.hpp" #include "macro.hpp" +#include "ui-helpers.hpp" #include @@ -74,10 +76,22 @@ void AdvSceneSwitcher::PasteMacroSegment() break; } case MacroSegmentCopyInfo::Type::ACTION: + if (IsCursorInWidgetArea(ui->macroElseActions)) { + AddMacroElseAction(macro.get(), + macro->ElseActions().size(), + copyInfo.segment->GetId(), + data.Get()); + break; + } AddMacroAction(macro.get(), macro->Actions().size(), copyInfo.segment->GetId(), data.Get()); break; case MacroSegmentCopyInfo::Type::ELSE: + if (IsCursorInWidgetArea(ui->macroActions)) { + AddMacroAction(macro.get(), macro->Actions().size(), + copyInfo.segment->GetId(), data.Get()); + break; + } AddMacroElseAction(macro.get(), macro->ElseActions().size(), copyInfo.segment->GetId(), data.Get()); break; @@ -91,6 +105,12 @@ bool MacroSegmentIsInClipboard() return copyInfo.type != MacroSegmentCopyInfo::Type::NONE; } +bool MacroActionIsInClipboard() +{ + return copyInfo.type == MacroSegmentCopyInfo::Type::ACTION || + copyInfo.type == MacroSegmentCopyInfo::Type::ELSE; +} + void SetupSegmentCopyPasteShortcutHandlers(AdvSceneSwitcher *window) { auto copyShortcut = new QShortcut(QKeySequence("Ctrl+C"), window); diff --git a/lib/macro/macro-segment-copy-paste.hpp b/lib/macro/macro-segment-copy-paste.hpp index 67398350..d71ab857 100644 --- a/lib/macro/macro-segment-copy-paste.hpp +++ b/lib/macro/macro-segment-copy-paste.hpp @@ -3,6 +3,7 @@ namespace advss { class AdvSceneSwitcher; bool MacroSegmentIsInClipboard(); +bool MacroActionIsInClipboard(); void SetupSegmentCopyPasteShortcutHandlers(AdvSceneSwitcher *window); } // namespace advss diff --git a/lib/macro/macro-tab.cpp b/lib/macro/macro-tab.cpp index cdb68147..76d3cf43 100644 --- a/lib/macro/macro-tab.cpp +++ b/lib/macro/macro-tab.cpp @@ -1051,6 +1051,10 @@ static bool handleCustomLabelRename(MacroSegmentEdit *segmentEdit) { std::string label; auto segment = segmentEdit->Data(); + if (!segment) { + return false; + } + bool accepted = NameDialog::AskForName( GetSettingsWindow(), obs_module_text( @@ -1115,6 +1119,30 @@ static void setupSegmentLabelContextMenuEntries(MacroSegmentEdit *segmentEdit, }); } +static void setupCopyPasteContextMenuEnry(AdvSceneSwitcher *ss, + MacroSegmentEdit *segmentEdit, + QMenu &menu) +{ + auto copy = menu.addAction( + obs_module_text("AdvSceneSwitcher.macroTab.segment.copy"), ss, + [ss]() { ss->CopyMacroSegment(); }); + copy->setEnabled(!!segmentEdit); + + const char *pasteText = "AdvSceneSwitcher.macroTab.segment.paste"; + if (MacroActionIsInClipboard()) { + if (IsCursorInWidgetArea(ss->ui->macroActions)) { + pasteText = + "AdvSceneSwitcher.macroTab.segment.pasteAction"; + } else if (IsCursorInWidgetArea(ss->ui->macroElseActions)) { + pasteText = + "AdvSceneSwitcher.macroTab.segment.pasteElseAction"; + } + } + auto paste = menu.addAction(obs_module_text(pasteText), ss, + [ss]() { ss->PasteMacroSegment(); }); + paste->setEnabled(MacroSegmentIsInClipboard()); +} + static void setupConextMenu(AdvSceneSwitcher *ss, const QPoint &pos, std::function expand, std::function collapse, @@ -1125,33 +1153,19 @@ static void setupConextMenu(AdvSceneSwitcher *ss, const QPoint &pos, QMenu menu; auto segmentEdit = list->WidgetAt(pos); - auto copy = menu.addAction( - obs_module_text("AdvSceneSwitcher.macroTab.segment.copy"), ss, - [ss]() { ss->CopyMacroSegment(); }); - copy->setEnabled(!!segmentEdit); - auto paste = menu.addAction( - obs_module_text("AdvSceneSwitcher.macroTab.segment.paste"), ss, - [ss]() { ss->PasteMacroSegment(); }); - paste->setEnabled(MacroSegmentIsInClipboard()); - + setupCopyPasteContextMenuEnry(ss, segmentEdit, menu); menu.addSeparator(); - setupSegmentLabelContextMenuEntries(segmentEdit, menu); - menu.addSeparator(); - menu.addAction(obs_module_text("AdvSceneSwitcher.macroTab.expandAll"), ss, [ss, expand]() { expand(ss); }); menu.addAction(obs_module_text("AdvSceneSwitcher.macroTab.collapseAll"), ss, [ss, collapse]() { collapse(ss); }); - menu.addSeparator(); - menu.addAction(obs_module_text("AdvSceneSwitcher.macroTab.maximize"), ss, [ss, maximize]() { maximize(ss); }); menu.addAction(obs_module_text("AdvSceneSwitcher.macroTab.minimize"), ss, [ss, minimize]() { minimize(ss); }); - menu.exec(list->mapToGlobal(pos)); }