-
-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathPluginMode.h
More file actions
532 lines (450 loc) · 19.4 KB
/
PluginMode.h
File metadata and controls
532 lines (450 loc) · 19.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/*
// Copyright (c) 2022-2023 Nejrup, Alex Mitchell and Timothy Schoen
// For information on usage and redistribution, and for a DISCLAIMER OF ALL
// WARRANTIES, see the file, "LICENSE.txt," in this distribution.
*/
#pragma once
#include "PluginEditor.h"
#include "Canvas.h"
#include "Standalone/PlugDataWindow.h"
class PluginMode final : public Component
, public NVGComponent {
public:
explicit PluginMode(PluginEditor* editor, pd::Patch::Ptr patch)
: NVGComponent(this)
, patchPtr(patch)
, cnv(std::make_unique<Canvas>(editor, patch, this))
, editor(editor)
, desktopWindow(editor->getPeer())
, windowBounds(editor->getBounds().withPosition(editor->getTopLevelComponent()->getPosition()))
{
setAccessible(false); // Having accessibility enabled seems to cause crashes in Ableton
editor->pd->initialiseIntoPluginmode = false;
#if !JUCE_IOS
if (ProjectInfo::isStandalone) {
// If the window is already maximised, unmaximise it to prevent problems
# if JUCE_LINUX || JUCE_BSD
OSUtils::maximiseLinuxWindow(desktopWindow, false);
# else
if (desktopWindow->isFullScreen()) {
desktopWindow->setFullScreen(false);
}
# endif
}
if (ProjectInfo::isStandalone) {
auto const frameSize = desktopWindow->getFrameSizeIfPresent();
nativeTitleBarHeight = frameSize ? frameSize->getTop() : 0;
}
#endif
auto const& pluginModeTheme = editor->pd->pluginModeTheme;
if (pluginModeTheme) {
pluginModeLnf = std::make_unique<PlugDataLook>();
lastTheme = PlugDataLook::currentTheme;
pluginModeLnf->setTheme(pluginModeTheme);
editor->setLookAndFeel(pluginModeLnf.get());
editor->getTopLevelComponent()->sendLookAndFeelChange();
}
editor->nvgSurface.invalidateAll();
cnv->setCachedComponentImage(new NVGSurface::InvalidationListener(editor->nvgSurface, cnv.get()));
patch->openInPluginMode = true;
// Titlebar
titleBar.setBounds(0, 0, width, titlebarHeight);
titleBar.addMouseListener(this, true);
editorButton = std::make_unique<MainToolbarButton>(Icons::Edit);
editorButton->setTooltip("Show editor");
editorButton->setBounds(getWidth() - titlebarHeight, 0, titlebarHeight, titlebarHeight);
editorButton->onClick = [this] {
closePluginMode();
};
titleBar.addAndMakeVisible(*editorButton);
setAlwaysOnTop(true);
setWantsKeyboardFocus(true);
setInterceptsMouseClicks(true, true);
// Add this view to the editor
editor->addAndMakeVisible(this);
StringArray itemList;
for (auto const scale : pluginScales) {
itemList.add(String(scale.intScale) + "%");
}
scaleComboBox.addItemList(itemList, 1);
if (ProjectInfo::isStandalone) {
scaleComboBox.addSeparator();
scaleComboBox.addItem("Fullscreen", 8);
}
scaleComboBox.setTooltip("Change plugin scale");
scaleComboBox.setText("100%");
scaleComboBox.setBounds(8, 8, 70, titlebarHeight - 16);
scaleComboBox.setColour(ComboBox::outlineColourId, Colours::transparentBlack);
scaleComboBox.setColour(ComboBox::backgroundColourId, PlugDataColours::toolbarHoverColour.withAlpha(0.8f));
auto metaFile = patchPtr.get()->getPatchFile().getSiblingFile("meta.json");
scaleComboBox.onChange = [this, metaFile] {
auto const itemId = scaleComboBox.getSelectedId();
if (itemId == 0)
return;
if (itemId == 8) {
setKioskMode(true);
return;
}
if (selectedZoom != itemId) {
selectedZoom = itemId;
setWidthAndHeight(pluginScales[itemId - 1].floatScale);
patchPtr->pluginModeScale = pluginScales[itemId - 1].intScale;
// If the patch has a meta.json file, remember the zoom amount
if (metaFile.existsAsFile()) {
auto json = JSON::parse(metaFile.loadFileAsString());
if (json.isObject()) {
json.getDynamicObject()->setProperty("Zoom", selectedZoom);
metaFile.replaceWithText(JSON::toString(json));
}
}
}
};
if (metaFile.existsAsFile()) {
auto json = JSON::parse(metaFile.loadFileAsString());
if (json.isObject()) {
auto jsonObject = json.getDynamicObject();
if (jsonObject != nullptr) {
if (jsonObject->hasProperty("Scale")) {
scaleDPIMult = jsonObject->getProperty("Scale");
}
if (jsonObject->hasProperty("Zoom")) {
selectedZoom = static_cast<int>(jsonObject->getProperty("Zoom"));
scaleComboBox.setSelectedId(selectedZoom, dontSendNotification);
setWidthAndHeight(pluginScales[selectedZoom - 1].floatScale);
patchPtr->pluginModeScale = pluginScales[selectedZoom - 1].intScale;
zoomLoadedFromJson = true;
}
}
}
}
titleBar.addAndMakeVisible(scaleComboBox);
#if JUCE_IOS
editor->constrainer.setSizeLimits(1, 1, 99000, 99000);
#endif
addAndMakeVisible(titleBar);
cnv->connectionLayer.setVisible(false);
set_plugdata_debugging_enabled(0);
set_plugdata_activity_enabled(0);
}
~PluginMode() override
{
if (pluginModeLnf) {
editor->setLookAndFeel(editor->pd->lnf);
editor->pd->lnf->setTheme(SettingsFile::getInstance()->getTheme(lastTheme));
editor->getTopLevelComponent()->sendLookAndFeelChange();
}
set_plugdata_debugging_enabled(SettingsFile::getInstance()->getProperty<bool>("debug_connections"));
// set_plugdata_activity_enabled already gets reset by updateOverlays() when we recreate the canvas
}
void updateSize()
{
#if JUCE_IOS
parentSizeChanged();
float const scaleX = static_cast<float>(getWidth()) / width;
float const scaleY = static_cast<float>(getHeight()) / height;
setWidthAndHeight(jmin(scaleX, scaleY));
return;
#endif
// set scale to the last scale that was set for this patches plugin mode
int const previousScale = patchPtr->pluginModeScale;
scaleComboBox.setText(String(previousScale) + String("%"), dontSendNotification);
setWidthAndHeight(previousScale * 0.01f);
}
void setWidthAndHeight(float scale)
{
scale *= scaleDPIMult;
cnv->zoomScale = scale;
auto newWidth = static_cast<int>(width * scale);
auto newHeight = static_cast<int>(height * scale) + titlebarHeight + nativeTitleBarHeight;
#if JUCE_LINUX || JUCE_BSD
// We need to add the window margin for the shadow on Linux, or else X11 will try to make the window smaller than it should be when the window moves
newHeight += 36;
newWidth += 36;
#endif
if (auto* mainWindow = dynamic_cast<PlugDataWindow*>(editor->getTopLevelComponent())) {
#if JUCE_IOS
editor->constrainer.setSizeLimits(1, 1, 99000, 99000);
mainWindow->getConstrainer()->setSizeLimits(1, 1, 99000, 99000);
#else
// Setting the min=max will disable resizing
editor->constrainer.setSizeLimits(newWidth, newHeight, newWidth, newHeight);
mainWindow->getConstrainer()->setSizeLimits(newWidth, newHeight, newWidth, newHeight);
#endif
} else {
editor->pluginConstrainer.setSizeLimits(newWidth, newHeight, newWidth, newHeight);
}
#if JUCE_LINUX || JUCE_BSD
if (ProjectInfo::isStandalone) {
OSUtils::updateLinuxWindowConstraints(getPeer());
}
#endif
if (ProjectInfo::isStandalone) {
editor->getTopLevelComponent()->setSize(newWidth, newHeight);
} else {
// We need to resize twice to prevent glitches on macOS
editor->setSize(newWidth - 1, newHeight - 1);
editor->setSize(newWidth, newHeight);
}
Timer::callAfterDelay(100, [_editor = SafePointer(editor)] {
if (_editor) {
_editor->nvgSurface.invalidateAll();
}
});
}
void render(NVGcontext* nvg, Rectangle<int> const area)
{
NVGScopedState scopedState(nvg);
auto const scale = pluginModeScale;
#if !JUCE_IOS
if (isWindowFullscreen())
#endif
nvgScissor(nvg, (getWidth() - (width * scale)) / 2, (getHeight() - height * scale) / 2, width * scale, height * scale);
nvgTranslate(nvg, 0, isWindowFullscreen() ? 0 : -titlebarHeight);
nvgScale(nvg, scale, scale);
nvgTranslate(nvg, cnv->getX(), cnv->getY());
cnv->performRender(nvg, cnv->getLocalArea(this, area.translated(0, 40)));
}
void closePluginMode()
{
auto const constrainedNewBounds = windowBounds.withWidth(std::max(windowBounds.getWidth(), 890)).withHeight(std::max(windowBounds.getHeight(), 660));
if (auto* mainWindow = dynamic_cast<PlugDataWindow*>(editor->getTopLevelComponent())) {
editor->constrainer.setSizeLimits(890, 660, 99000, 99000);
mainWindow->getConstrainer()->setSizeLimits(890, 660, 99000, 99000);
#if JUCE_LINUX || JUCE_BSD
OSUtils::updateLinuxWindowConstraints(getPeer());
#endif
auto const correctedPosition = constrainedNewBounds.getTopLeft() - Point<int>(0, nativeTitleBarHeight);
mainWindow->setBoundsConstrained(constrainedNewBounds.withPosition(correctedPosition));
} else {
// For some reason it doesn't work well on macOS unless we change the size twice??
editor->setSize(constrainedNewBounds.getWidth() - 1, constrainedNewBounds.getHeight() - 1);
editor->pluginConstrainer.setSizeLimits(890, 660, 99000, 99000);
editor->setBounds(0, 0, constrainedNewBounds.getWidth(), constrainedNewBounds.getHeight());
}
cnv->patch.openInPluginMode = false;
editor->getTabComponent().updateNow();
}
bool isWindowFullscreen() const
{
if (ProjectInfo::isStandalone) {
return isFullScreenKioskMode;
}
return false;
}
void paint(Graphics& g) override
{
if (!cnv)
return;
if (ProjectInfo::isStandalone && isWindowFullscreen()) {
// Fill background for Fullscreen / Kiosk Mode
g.setColour(PlugDataColours::canvasBackgroundColour);
g.fillRect(editor->getTopLevelComponent()->getLocalBounds());
return;
}
auto const baseColour = PlugDataColours::toolbarBackgroundColour;
if (editor->wantsRoundedCorners()) {
// TitleBar background
g.setColour(baseColour);
g.fillRoundedRectangle(0.0f, 0.0f, getWidth(), titlebarHeight, Corners::windowCornerRadius);
} else {
// TitleBar background
g.setColour(baseColour);
g.fillRect(0, 0, getWidth(), titlebarHeight);
}
// TitleBar outline
g.setColour(PlugDataColours::outlineColour);
g.drawLine(0.0f, titlebarHeight, static_cast<float>(getWidth()), titlebarHeight, 1.0f);
// TitleBar text
g.setColour(PlugDataColours::panelTextColour);
g.drawText(cnv->patch.getTitle().upToLastOccurrenceOf(".pd", false, true), titleBar.getBounds(), Justification::centred);
}
void resized() override
{
// Detect if the user exited fullscreen with the macOS's fullscreen button
#if JUCE_MAC
if (ProjectInfo::isStandalone && isWindowFullscreen() && !desktopWindow->isFullScreen()) {
setKioskMode(false);
}
#endif
// On iOS, we always scale pluginmode patches to full size, and we also always show the patch title bar
#if JUCE_IOS
// Calculate the scale factor required to fit the editor in the screen
float const scaleX = static_cast<float>(getWidth()) / width;
float const scaleY = static_cast<float>(getHeight()) / height;
float scale = jmin(scaleX, scaleY);
cnv->zoomScale = scale;
pluginModeScale = scale;
scaleComboBox.setVisible(false);
editorButton->setVisible(true);
// Calculate the position of the editor after scaling
int const scaledWidth = static_cast<int>(width * scale);
int const scaledHeight = static_cast<int>(height * scale);
int const x = (getWidth() - scaledWidth) / 2;
int const y = (getHeight() - scaledHeight) / 2;
titleBar.setBounds(0, 0, getWidth(), titlebarHeight);
scaleComboBox.setBounds(8, 8, 74, titlebarHeight - 16);
editorButton->setBounds(getWidth() - titlebarHeight, 0, titlebarHeight, titlebarHeight);
auto const b = getLocalBounds() + cnv->canvasOrigin;
cnv->setTransform(cnv->getTransform().scale(scale));
cnv->setBounds(-b.getX() + x / scale, -b.getY() + y / scale + titlebarHeight / scale, b.getWidth() / scale + b.getX(), b.getHeight() / scale + b.getY());
repaint();
return;
#endif
if (ProjectInfo::isStandalone && isWindowFullscreen()) {
// Calculate the scale factor required to fit the editor in the screen
float const scaleX = static_cast<float>(getWidth()) / width;
float const scaleY = static_cast<float>(getHeight()) / height;
float scale = jmin(scaleX, scaleY);
// Calculate the position of the editor after scaling
int const scaledWidth = static_cast<int>(width * scale);
int const scaledHeight = static_cast<int>(height * scale);
int const x = (getWidth() - scaledWidth) / 2;
int const y = (getHeight() - scaledHeight) / 2;
pluginModeScale = scale;
cnv->zoomScale = scale;
// Hide titlebar
titleBar.setBounds(0, 0, 0, 0);
scaleComboBox.setVisible(false);
#if !JUCE_IOS
editorButton->setVisible(false);
#endif
auto const b = getLocalBounds() + cnv->canvasOrigin;
cnv->setTransform(cnv->getTransform().scale(scale));
cnv->setBounds(-b.getX() + x / scale, -b.getY() + y / scale, b.getWidth() + b.getX(), b.getHeight() + b.getY());
} else {
float scale = getWidth() / width;
pluginModeScale = scale;
scaleComboBox.setVisible(true);
editorButton->setVisible(true);
titleBar.setBounds(0, 0, getWidth(), titlebarHeight);
scaleComboBox.setBounds(8, 8, 74, titlebarHeight - 16);
editorButton->setBounds(getWidth() - titlebarHeight, 0, titlebarHeight, titlebarHeight);
auto const b = getLocalBounds() + cnv->canvasOrigin;
cnv->setTransform(cnv->getTransform().scale(scale));
cnv->setBounds(-b.getX(), -b.getY() + titlebarHeight / scale, b.getWidth() / scale + b.getX(), b.getHeight() / scale + b.getY());
}
repaint();
}
void parentSizeChanged() override
{
#if JUCE_IOS
setBounds(editor->getLocalBounds().withTrimmedBottom(40));
return;
#endif
// Fullscreen / Kiosk Mode
if (ProjectInfo::isStandalone && isWindowFullscreen()) {
// Determine the screen size
auto const screenBounds = desktopWindow->getBounds();
// Fill the screen
setBounds(0, 0, screenBounds.getWidth(), screenBounds.getHeight());
} else {
setBounds(editor->getLocalBounds());
}
}
void mouseDown(MouseEvent const& e) override
{
if (scaleComboBox.contains(e.getEventRelativeTo(&scaleComboBox).getPosition()) || !e.mods.isLeftButtonDown())
return;
// Offset the start of the drag when dragging the window by Titlebar
#if !JUCE_MAC && !JUCE_IOS
if (auto* mainWindow = dynamic_cast<PlugDataWindow*>(editor->getTopLevelComponent())) {
if (e.getPosition().getY() < titlebarHeight) {
isDraggingWindow = true;
windowDragger.startDraggingWindow(mainWindow, e.getEventRelativeTo(mainWindow));
}
}
#endif
}
void mouseDrag(MouseEvent const& e) override
{
if (!isDraggingWindow)
return;
#if !JUCE_MAC && !JUCE_IOS
if (auto* mainWindow = dynamic_cast<PlugDataWindow*>(editor->getTopLevelComponent())) {
windowDragger.dragWindow(mainWindow, e.getEventRelativeTo(mainWindow));
}
#endif
}
void mouseUp(MouseEvent const& e) override
{
isDraggingWindow = false;
}
void setFullScreen(PlugDataWindow* window, bool shouldBeFullScreen)
{
#if JUCE_LINUX || JUCE_BSD
// linux can make the window take up the whole display by simply setting the bounds to that of the display
auto bounds = shouldBeFullScreen ? Desktop::getInstance().getDisplays().getPrimaryDisplay()->totalArea : originalPluginWindowBounds;
desktopWindow->setBounds(bounds, shouldBeFullScreen);
#else
window->setFullScreen(shouldBeFullScreen);
#endif
}
pd::Patch::Ptr getPatch()
{
return patchPtr;
}
void setKioskMode(bool const shouldBeKiosk)
{
#if JUCE_IOS
return;
#endif
auto* window = dynamic_cast<PlugDataWindow*>(getTopLevelComponent());
if (!window)
return;
isFullScreenKioskMode = shouldBeKiosk;
if (shouldBeKiosk) {
editor->constrainer.setSizeLimits(1, 1, 99000, 99000);
originalPluginWindowBounds = window->getBounds();
desktopWindow = window->getPeer();
setFullScreen(window, true);
parentSizeChanged();
} else {
setFullScreen(window, false);
selectedZoom = 3;
scaleComboBox.setText("100%");
setWidthAndHeight(1.0f);
desktopWindow = window->getPeer();
}
}
bool keyPressed(KeyPress const& key) override
{
if (isWindowFullscreen() && key == KeyPress::escapeKey) {
setKioskMode(false);
return true;
}
grabKeyboardFocus();
return false;
}
Canvas* getCanvas() const
{
return cnv.get();
}
private:
pd::Patch::Ptr patchPtr;
std::unique_ptr<Canvas> cnv;
PluginEditor* editor;
ComponentPeer* desktopWindow;
Component titleBar;
int const titlebarHeight = 40;
int nativeTitleBarHeight = 0;
ComboBox scaleComboBox;
std::unique_ptr<MainToolbarButton> editorButton;
int selectedZoom = 3; // default is 100% for now
WindowDragger windowDragger;
bool isDraggingWindow : 1 = false;
bool isFullScreenKioskMode : 1 = false;
bool zoomLoadedFromJson : 1 = false;
Rectangle<int> originalPluginWindowBounds;
Rectangle<int> windowBounds;
float const width = static_cast<float>(cnv->patchWidth.getValue()) + 1.0f;
float const height = static_cast<float>(cnv->patchHeight.getValue()) + 1.0f;
float pluginModeScale = 1.0f;
float scaleDPIMult = 1.0f;
String lastTheme;
std::unique_ptr<PlugDataLook> pluginModeLnf;
struct Scale {
float floatScale;
int intScale;
};
StackArray<Scale, 7> pluginScales { Scale { 0.5f, 50 }, Scale { 0.75f, 75 }, Scale { 1.0f, 100 }, Scale { 1.25f, 125 }, Scale { 1.5f, 150 }, Scale { 1.75f, 175 }, Scale { 2.0f, 200 } };
};