Files
Minixer/Source/Components/ChannelStripComponent.cpp
T
XmengMD a9bbf9771f 修复插件槽加载/卸载交互缺陷,并解决删除插件、加载预设阻塞界面
问题
1. 插件加载中只在底部状态栏提示 Loading,槽位自身没有加载态;且加载中仍可再次点开
   load plugin,可对同一槽位重复发起加载。
2. 删除插件会阻塞界面(大型插件约 2 秒),加载 .minixer 预设时更严重——预设里 N 个
   插件会导致数秒至十几秒完全无响应;而加载单个插件却不卡。

根因
- 槽位只有“有/无插件”两种显示状态,而加载期间 pluginSlotNodes 必为 nullptr,被误判
  成空槽,于是所有空槽分支(可点击加载、显示 “+”、空槽右键菜单、可拖拽、不可复制)
  全部串错。
- 删除阻塞的根因不在槽位层:AudioProcessorGraph 的渲染序列
  GraphRenderSequence::NodeOp 持有 Node::Ptr 强引用(juce_AudioProcessorGraph.cpp:889),
  节点从图中摘除后仍被旧渲染序列保活;而 RenderSequenceExchange::set() 与其 500ms
  timerCallback() 都在消息线程销毁被淘汰的序列,因此
  ~PluginBridgeNode() -> shutdown() -> launcher->waitForExit(2000) 依旧在消息线程执行。
  预设加载会一次摘除 N 个节点、旧序列一次性析构,故冻结 N×2s。

修改
一、槽位进行中状态(PluginSlotComponent / ChannelStripComponent / MainComponent)
- 新增 PluginSlotBusyState{none, loading, removing} 取代原 bool loading;槽位自身显示
  “Loading xxx…/Removing xxx…”,加淡蓝底 + 强调色描边,进行中屏蔽加载、打开编辑器、
  拖拽、旁通等交互。
- 加载中右侧按钮改为“取消加载”(新增 pluginSlotLoadCancelRequested 回调),右键仅提供
  Cancel loading;卸载中不可取消,右键仅提示当前状态。
- 替换插件时不再先移除旧插件:加载期间旧插件继续工作,待新插件在后台就绪后再原子替换;
  加载失败或取消时保留原插件,不再出现替换瞬间断音与槽位清空。
- “卸载中”状态改由子进程真正退出后的回调解除,状态栏同步从 Removing… 更新为
  Slot N cleared(仅用户主动删除时回写,预设批量清空不刷屏)。

二、删除/替换/预设加载/退出不再阻塞(PluginHostLauncher / PluginBridgeNode)
- 新增 PluginHostProcessReaper:把子进程交给后台线程池完成“等待退出(宽限 3s)→ 超时
  强杀”,绝不在调用线程上等待;reapAsync 支持完成后在消息线程回调。
- PluginBridgeNode::shutdown() 不再同步 waitForExit,改为把 launcher 交给收割器;新增
  setShutdownCompletionCallback 供槽位解除“卸载中”状态。
- PluginHostLauncher 析构时强杀仍在运行的子进程(juce::ChildProcess 析构并不会结束进程),
  避免残留孤儿进程;程序退出时调用 drain(2000) 清场。

三、其他同源问题
- 槽位内容整体移位时,取消移位区间内所有在途加载,避免加载完成后写回错位槽位并产生
  孤立节点。
- 空槽与进行中的槽位不再可拖拽(mouseDrag 与 isInterestedInDragSource 双重判断)。
- 复制空槽/进行中槽位给出状态栏提示,不再静默无反馈。
- 跨实例粘贴改为按最后一个 ':' 拆分,插件名含 ':' 不再被截断。
- 右键 Move down 的硬编码槽位数 11 改为按实际槽位数判断。
2026-09-24 21:34:44 +08:00

328 lines
11 KiB
C++

#include "ChannelStripComponent.h"
#include "../LookAndFeel/MixerLookAndFeel.h"
namespace minixer
{
//==============================================================================
ChannelStripComponent::ChannelStripComponent (const juce::String& /*channelName*/)
{
addAndMakeVisible (inputTrim);
inputTrim.addListener (this);
addAndMakeVisible (inputMeter);
for (int i = 0; i < defaultNumPluginSlots; ++i)
{
auto slot = std::make_unique<PluginSlotComponent> (i, defaultNumPluginSlots);
slot->addListener (this);
addAndMakeVisible (*slot);
pluginSlots.push_back (std::move (slot));
}
addAndMakeVisible (panKnob);
panKnob.addListener (this);
panKnob.setValueFormatter ([] (double value)
{
if (value <= -0.01)
return juce::String::formatted (TRANS ("L %.0f%%"), -value * 100.0);
if (value >= 0.01)
return juce::String::formatted (TRANS ("R %.0f%%"), value * 100.0);
return TRANS ("C");
});
addAndMakeVisible (stereoSeparation);
stereoSeparation.addListener (this);
stereoSeparation.setValueFormatter ([] (double value)
{
if (value < -0.05)
return juce::String::formatted (TRANS ("separated %.0f%%"), -value);
if (value > 0.05)
return juce::String::formatted (TRANS ("merged %.0f%%"), value);
return TRANS ("off");
});
addAndMakeVisible (outputFader);
outputFader.addListener (this);
addAndMakeVisible (outputMeter);
}
//==============================================================================
PluginSlotComponent& ChannelStripComponent::getPluginSlot (int index)
{
jassert (juce::isPositiveAndBelow (index, pluginSlots.size()));
return *pluginSlots[static_cast<size_t> (index)];
}
//==============================================================================
void ChannelStripComponent::setPluginSlotInfo (int slotIndex, const juce::String& pluginName, bool isBypassed)
{
if (juce::isPositiveAndBelow (slotIndex, pluginSlots.size()))
pluginSlots[static_cast<size_t> (slotIndex)]->setPluginInfo (pluginName, isBypassed);
}
//==============================================================================
void ChannelStripComponent::setPluginSlotBusyState (int slotIndex, PluginSlotBusyState busyState, const juce::String& busyPluginName)
{
if (juce::isPositiveAndBelow (slotIndex, pluginSlots.size()))
pluginSlots[static_cast<size_t> (slotIndex)]->setPluginBusyState (busyState, busyPluginName);
}
//==============================================================================
void ChannelStripComponent::paint (juce::Graphics& g)
{
auto bounds = getLocalBounds().toFloat().reduced (2.0f);
g.setColour (MixerLookAndFeel::getElevatedColour().withAlpha (0.3f));
g.fillRoundedRectangle (bounds, 6.0f);
g.setColour (MixerLookAndFeel::getBorderColour());
g.drawRoundedRectangle (bounds, 6.0f, 1.0f);
}
//==============================================================================
void ChannelStripComponent::resized()
{
auto bounds = getLocalBounds().reduced (6, 4);
// 左侧:插件机架(12 个插槽,每个固定 40 像素高)
const auto rackWidth = 180;
const auto slotHeight = 40;
const auto rackHeight = slotHeight * defaultNumPluginSlots;
auto rackArea = bounds.removeFromLeft (rackWidth).withHeight (rackHeight);
for (auto& slot : pluginSlots)
slot->setBounds (rackArea.removeFromTop (slotHeight).reduced (1, 1));
bounds.removeFromLeft (8);
// 右侧:控制区(Input/Pan/Output + 电平表),高度与插件机架对齐
auto controlArea = bounds.withHeight (rackHeight);
const auto sectionHeight = controlArea.getHeight() / 3;
// 上部:Input Trim 旋钮 + 输入电平表
auto inputSection = controlArea.removeFromTop (sectionHeight);
auto inputMeterWidth = inputSection.getWidth() / 3;
inputMeter.setBounds (inputSection.removeFromRight (inputMeterWidth).reduced (2, 4));
inputTrim.setBounds (inputSection.reduced (2, 4));
// 中部:Pan 旋钮 + Stereo separation 旋钮
auto panSection = controlArea.removeFromTop (sectionHeight);
auto panKnobWidth = panSection.getWidth() / 2;
panKnob.setBounds (panSection.removeFromLeft (panKnobWidth).reduced (2, 4));
stereoSeparation.setBounds (panSection.reduced (2, 4));
// 下部:Output 推子 + 输出电平表
auto outputSection = controlArea;
auto outputMeterWidth = outputSection.getWidth() / 3;
outputMeter.setBounds (outputSection.removeFromRight (outputMeterWidth).reduced (2, 4));
outputFader.setBounds (outputSection.reduced (2, 4));
}
//==============================================================================
void ChannelStripComponent::pluginSlotClicked (int slotIndex)
{
listeners.call ([slotIndex] (Listener& l) { l.pluginSlotClicked (slotIndex); });
}
//==============================================================================
void ChannelStripComponent::pluginSlotReplaceRequested (int slotIndex)
{
listeners.call ([slotIndex] (Listener& l) { l.pluginSlotReplaceRequested (slotIndex); });
}
//==============================================================================
void ChannelStripComponent::pluginSlotBypassToggled (int slotIndex, bool shouldBypass)
{
listeners.call ([slotIndex, shouldBypass] (Listener& l)
{
l.pluginSlotBypassToggled (slotIndex, shouldBypass);
});
}
//==============================================================================
void ChannelStripComponent::pluginSlotDeleteRequested (int slotIndex)
{
listeners.call ([slotIndex] (Listener& l) { l.pluginSlotDeleteRequested (slotIndex); });
}
//==============================================================================
void ChannelStripComponent::pluginSlotCopyRequested (int slotIndex)
{
listeners.call ([slotIndex] (Listener& l) { l.pluginSlotCopyRequested (slotIndex); });
}
//==============================================================================
void ChannelStripComponent::pluginSlotPasteRequested (int slotIndex)
{
listeners.call ([slotIndex] (Listener& l) { l.pluginSlotPasteRequested (slotIndex); });
}
//==============================================================================
void ChannelStripComponent::pluginSlotMoveRequested (int fromSlotIndex, int toSlotIndex)
{
listeners.call ([fromSlotIndex, toSlotIndex] (Listener& l)
{
l.pluginSlotMoveRequested (fromSlotIndex, toSlotIndex);
});
}
//==============================================================================
void ChannelStripComponent::pluginSlotLoadCancelRequested (int slotIndex)
{
listeners.call ([slotIndex] (Listener& l) { l.pluginSlotLoadCancelRequested (slotIndex); });
}
//==============================================================================
int ChannelStripComponent::getFocusedPluginSlotIndex() const
{
for (size_t i = 0; i < pluginSlots.size(); ++i)
{
if (pluginSlots[i]->hasKeyboardFocus (true))
return static_cast<int> (i);
}
return -1;
}
//==============================================================================
void ChannelStripComponent::setAllPluginsBypassed (bool shouldBypass)
{
for (auto& slot : pluginSlots)
{
if (slot->hasPlugin())
slot->setBypassed (shouldBypass);
}
}
//==============================================================================
void ChannelStripComponent::rotaryKnobValueChanged (RotaryKnobComponent* /*knob*/)
{
notifyParameterChanged();
}
//==============================================================================
void ChannelStripComponent::faderValueChanged (FaderComponent* /*fader*/)
{
notifyParameterChanged();
}
//==============================================================================
void ChannelStripComponent::notifyParameterChanged()
{
listeners.call ([] (Listener& l) { l.channelStripParameterChanged(); });
}
//==============================================================================
int ChannelStripComponent::getSlotIndexAtPoint (juce::Point<int> pos) const
{
for (size_t i = 0; i < pluginSlots.size(); ++i)
{
if (pluginSlots[i]->getBounds().contains (pos))
return static_cast<int> (i);
}
return -1;
}
//==============================================================================
void ChannelStripComponent::updateDragHoverSlot (int hoverIndex)
{
for (size_t i = 0; i < pluginSlots.size(); ++i)
pluginSlots[i]->setDropTargetHighlighted (static_cast<int> (i) == hoverIndex);
}
//==============================================================================
bool ChannelStripComponent::isInterestedInDragSource (const SourceDetails& dragSourceDetails)
{
auto* sourceComp = dragSourceDetails.sourceComponent.get();
if (sourceComp == nullptr)
return false;
auto* slotComp = dynamic_cast<PluginSlotComponent*> (sourceComp);
if (slotComp == nullptr)
return false;
// 空槽与进行中的槽位没有可移动的插件
if (slotComp->isBusy() || ! slotComp->hasPlugin())
return false;
if (! dragSourceDetails.description.isInt())
return false;
int sourceIndex = dragSourceDetails.description;
return juce::isPositiveAndBelow (sourceIndex, static_cast<int> (pluginSlots.size()));
}
//==============================================================================
void ChannelStripComponent::itemDragEnter (const SourceDetails& dragSourceDetails)
{
auto hoverIndex = getSlotIndexAtPoint (dragSourceDetails.localPosition);
updateDragHoverSlot (hoverIndex);
}
//==============================================================================
void ChannelStripComponent::itemDragMove (const SourceDetails& dragSourceDetails)
{
auto hoverIndex = getSlotIndexAtPoint (dragSourceDetails.localPosition);
updateDragHoverSlot (hoverIndex);
}
//==============================================================================
void ChannelStripComponent::itemDragExit (const SourceDetails& /*dragSourceDetails*/)
{
updateDragHoverSlot (-1);
}
//==============================================================================
void ChannelStripComponent::itemDropped (const SourceDetails& dragSourceDetails)
{
updateDragHoverSlot (-1);
int fromIndex = dragSourceDetails.description;
int toIndex = getSlotIndexAtPoint (dragSourceDetails.localPosition);
if (toIndex >= 0 && fromIndex != toIndex)
pluginSlotMoveRequested (fromIndex, toIndex);
}
//==============================================================================
bool ChannelStripComponent::keyPressed (const juce::KeyPress& key)
{
if (key == juce::KeyPress (juce::KeyPress::upKey, juce::ModifierKeys::altModifier, 0))
{
moveFocusedSlot (-1);
return true;
}
if (key == juce::KeyPress (juce::KeyPress::downKey, juce::ModifierKeys::altModifier, 0))
{
moveFocusedSlot (1);
return true;
}
return false;
}
//==============================================================================
void ChannelStripComponent::moveFocusedSlot (int delta)
{
auto focused = getFocusedPluginSlotIndex();
if (focused < 0)
return;
auto target = focused + delta;
if (! juce::isPositiveAndBelow (target, static_cast<int> (pluginSlots.size())))
return;
pluginSlotMoveRequested (focused, target);
// 保持焦点跟随被移动的槽位
pluginSlots[static_cast<size_t> (target)]->grabKeyboardFocus();
}
} // namespace minixer