-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
melatonin_inspector.h
496 lines (409 loc) · 17.1 KB
/
melatonin_inspector.h
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
/*
BEGIN_JUCE_MODULE_DECLARATION
ID: melatonin_inspector
vendor: Sudara
version: 1.4.0
name: Melatonin Inspector
description: A component inspector for JUCE, inspired by Figma, web inspector and Jim Credland's Component Debugger
license: MIT
dependencies: juce_gui_basics, juce_gui_extra
minimumCppStandard: 17
END_JUCE_MODULE_DECLARATION
*/
#pragma once
#ifndef PERFETTO
#define TRACE_COMPONENT(...)
#define TRACE_EVENT(category, ...)
#define TRACE_EVENT_BEGIN(category, ...)
#define TRACE_EVENT_END(category)
#endif
#include "melatonin/lookandfeel.h"
#include "melatonin_inspector/melatonin/components/overlay.h"
#include "melatonin_inspector/melatonin/helpers/inspector_settings.h"
#include "melatonin_inspector/melatonin/helpers/overlay_mouse_listener.h"
#include "melatonin_inspector/melatonin/inspector_component.h"
#include <melatonin_inspector/melatonin/components/fps_meter.h>
namespace melatonin
{
class Inspector : public juce::ComponentListener, public juce::DocumentWindow, private juce::Timer, public juce::FocusChangeListener
{
public:
class InspectorKeyCommands : public juce::KeyListener
{
public:
explicit InspectorKeyCommands (Inspector& i) : inspector (i) {}
Inspector& inspector;
bool keyPressed (const juce::KeyPress& keyPress, Component* /*originatingComponent*/) override
{
#if JUCE_WINDOWS
bool modifierPresent = juce::ModifierKeys::getCurrentModifiers().isCtrlDown();
#else
bool modifierPresent = juce::ModifierKeys::getCurrentModifiers().isCommandDown();
#endif
if (keyPress.isKeyCurrentlyDown ('I') && modifierPresent)
{
inspector.toggle();
return true;
}
if (keyPress.isKeyCode (juce::KeyPress::escapeKey))
{
if (inspector.inspectorEnabled)
{
inspector.clearSelections();
}
return true;
}
// let the keypress propagate
return false;
}
};
explicit Inspector (juce::Component& rootComponent, bool inspectorEnabledAtStart = true)
: juce::DocumentWindow ("Melatonin Inspector", colors::background, 7, true)
{
TRACE_COMPONENT();
this->addKeyListener (&keyListener);
setRoot (rootComponent);
// needs to come before the LNF
restoreBoundsIfNeeded();
restoreStateFromProps();
// use our own lnf for both overlay and inspector
setLookAndFeel (&inspectorLookAndFeel);
overlay.setLookAndFeel (&inspectorLookAndFeel);
inspectorComponent.setLookAndFeel (&inspectorLookAndFeel);
setUsingNativeTitleBar (true);
setContentNonOwned (&inspectorComponent, true);
setupCallbacks();
setResizable (true, false); // calls resized
toggle (inspectorEnabledAtStart);
}
~Inspector() override
{
clearRoot();
this->removeKeyListener (&keyListener);
// the mouse listener is owned and removes itself on destruction
if (selectionMode == FOLLOWS_FOCUS)
juce::Desktop::getInstance().removeFocusChangeListener (this);
// needed, otherwise removing look and feel will save bounds
settings->props.reset();
setLookAndFeel (nullptr);
}
void setRoot (juce::Component& rootComponent)
{
clearRoot();
root = &rootComponent;
root->addChildComponent (overlay);
overlay.setBounds (root->getLocalBounds());
root->addComponentListener (this);
// allow us to open/close the inspector by key command
// bit sketchy because we're modifying the source app to accept key focus
root->addKeyListener (&keyListener);
root->setWantsKeyboardFocus (true);
fpsMeter.setRoot (*root);
overlayMouseListener.setRoot (*root);
inspectorComponent.setRoot (*root);
}
void clearRoot()
{
if (root == nullptr)
return;
root->removeKeyListener (&keyListener);
root->removeComponentListener (this);
fpsMeter.clearRoot();
overlayMouseListener.clearRoot();
inspectorComponent.clearRoot();
}
void moved() override
{
TRACE_COMPONENT();
DocumentWindow::resized();
saveBounds();
}
void resized() override
{
TRACE_COMPONENT();
DocumentWindow::resized();
saveBounds();
}
// this is a bit brittle and called a bit too frequently
// for example 4-5 times on construction
void saveBounds()
{
TRACE_COMPONENT();
if (settings->props == nullptr)
return;
settings->props->setValue ("x", getX());
settings->props->setValue ("y", getY());
// only overwrite width/height when inspectorEnabled.
// the disabled dimensions are fixed,
// so this lets us "open back up" when re-enabling
if (inspectorEnabled)
{
settings->props->setValue ("inspectorEnabledWidth", getWidth());
settings->props->setValue ("inspectorEnabledHeight", getHeight());
}
settings->saveIfNeeded();
}
void restoreBoundsIfNeeded()
{
TRACE_COMPONENT();
// disabled is a fixed 380x400
// inspectorEnabled must be at least 700x800
auto minWidth = inspectorEnabled ? 700 : 380;
auto minHeight = inspectorEnabled ? 800 : 400;
auto x = settings->props->getIntValue ("x", 50);
auto y = settings->props->getIntValue ("y", 50);
if (inspectorEnabled)
{
auto width = settings->props->getIntValue ("inspectorEnabledWidth", minWidth);
auto height = settings->props->getIntValue ("inspectorEnabledHeight", minHeight);
// Note: Ideally we'd call setResizable but it recreates the desktop window
// which adds >30ms in Debug with little change of behavior
// setResizable (true, false);
setResizeLimits (minWidth, minHeight, 1200, 1200);
setBounds (x, y, width, height);
}
else
{
// decrease the resize limits first for the setSize call to work!
// the order of these calls matters a lot
setResizeLimits (minWidth, minHeight, minWidth, minHeight);
setBounds (x, y, minWidth, minHeight);
// Keep this commented out, as it will recreate the desktop window
// setResizable (false, false);
}
inspectorComponent.setBounds (getLocalBounds());
}
void outlineComponent (Component* c)
{
TRACE_COMPONENT();
// don't dogfood the overlay
if (!inspectorEnabled || overlay.isParentOf (c) || selectionLock)
return;
overlay.outlineComponent (c);
inspectorComponent.displayComponentInfo (c, true);
}
void outlineDistanceCallback (Component* c)
{
TRACE_COMPONENT();
if (!inspectorEnabled || overlay.isParentOf (c))
return;
overlay.outlineDistanceCallback (c);
}
void selectComponent (Component* c, bool shouldCollapseTree = false)
{
TRACE_COMPONENT();
if (!inspectorEnabled || overlay.isParentOf (c) || selectionLock)
return;
overlay.selectComponent (c);
inspectorComponent.selectComponent (c, shouldCollapseTree);
}
void dragComponent (Component* c, const juce::MouseEvent& e)
{
TRACE_COMPONENT();
if (!inspectorEnabled || overlay.isParentOf (c))
return;
overlay.dragSelectedComponent (e);
inspectorComponent.displayComponentInfo (c);
}
void startDragComponent (Component* c, const juce::MouseEvent& e)
{
TRACE_COMPONENT();
if (!inspectorEnabled || overlay.isParentOf (c))
return;
overlay.startDraggingComponent (e);
}
void clearSelections()
{
TRACE_COMPONENT();
inspectorComponent.deselectComponent();
overlay.outlineComponent (nullptr);
overlay.selectComponent (nullptr);
}
// closing the window means turning off the inspector
void closeButtonPressed() override
{
if (onClose)
{
// you can provide a callback to destroy the inspector
onClose();
}
else
{
// otherwise we'll just hide it
toggle (false);
setVisible (false);
}
}
void toggle (bool newStatus)
{
TRACE_COMPONENT();
// the DocumentWindow always stays open, even when disabled
setVisible (true);
inspectorEnabled = newStatus;
inspectorComponent.toggle (newStatus);
restoreBoundsIfNeeded();
overlay.setVisible (newStatus);
if (newStatus)
{
if (selectionMode == FOLLOWS_MOUSE)
// without this, target apps that have UI to open the inspector
// will select that piece of UI within the same click, see #70
callAfterDelay (500, [safeThis = juce::Component::SafePointer<Inspector> (this)]
{
if (safeThis == nullptr)
return;
safeThis->overlayMouseListener.enable();
});
}
else
{
clearSelections();
overlayMouseListener.disable();
}
}
void toggle()
{
toggle (!inspectorEnabled);
}
void setRootFollowsComponentUnderMouse (bool follow)
{
rootFollowsComponentUnderMouse = follow;
if (rootFollowsComponentUnderMouse)
startTimerHz (25);
else
stopTimer();
}
std::function<void()> onClose;
private:
juce::SharedResourcePointer<InspectorSettings> settings;
InspectorLookAndFeel inspectorLookAndFeel;
InspectorComponent inspectorComponent;
juce::Component::SafePointer<juce::Component> root;
bool inspectorEnabled = false;
bool selectionLock = false;
Overlay overlay;
FPSMeter fpsMeter;
OverlayMouseListener overlayMouseListener;
InspectorKeyCommands keyListener { *this };
bool rootFollowsComponentUnderMouse = false;
// Resize our overlay when the root component changes
void componentMovedOrResized (Component& rootComponent, bool wasMoved, bool wasResized) override
{
if (wasResized || wasMoved)
{
overlay.setBounds (rootComponent.getLocalBounds());
}
}
void componentBeingDeleted (juce::Component& component) override
{
if (&component == root)
{
clearRoot();
juce::Timer::callAfterDelay (50, [this, safeThis = juce::Component::SafePointer<Inspector> (this)]
{
if (safeThis == nullptr)
return;
auto& d = juce::Desktop::getInstance();
for (int i = 0; i < d.getNumComponents(); i++)
{
if (auto c = d.getComponent (i); c != nullptr && c != this)
{
setRoot (*c);
return;
}
}
});
}
}
void globalFocusChanged (Component* focusedComponent) override
{
// ignore nullptr focus events, they are frequent and cause glitchiness
if (focusedComponent == nullptr)
return;
// This gets sent all components, even subcomponents of the inspector itself
// (which is undesirable since we're not dogfoodiing).
if (focusedComponent->getPeer() == root->getPeer())
selectComponent (focusedComponent);
}
void timerCallback() override
{
for (auto& ms : juce::Desktop::getInstance().getMouseSources())
if (auto c = ms.getComponentUnderMouse())
if (auto top = c->getTopLevelComponent(); top != nullptr && top != root && top != this)
setRoot (*top);
}
void setupCallbacks()
{
overlayMouseListener.outlineComponentCallback = [this] (Component* c) { this->outlineComponent (c); };
overlayMouseListener.outlineDistanceCallback = [this] (Component* c) { this->outlineDistanceCallback (c); };
overlayMouseListener.selectComponentCallback = [this] (Component* c) { this->selectComponent (c, true); };
overlayMouseListener.componentStartDraggingCallback = [this] (Component* c, const juce::MouseEvent& e) { this->startDragComponent (c, e); };
overlayMouseListener.componentDraggedCallback = [this] (Component* c, const juce::MouseEvent& e) { this->dragComponent (c, e); };
overlayMouseListener.mouseExitCallback = [this] { if (this->inspectorEnabled) inspectorComponent.redisplaySelectedComponent(); };
inspectorComponent.selectComponentCallback = [this] (Component* c) { this->selectComponent (c, false); };
inspectorComponent.outlineComponentCallback = [this] (Component* c) { this->outlineComponent (c); };
inspectorComponent.toggleCallback = [this] (const bool enable) { this->toggle (enable); };
inspectorComponent.toggleOverlayCallback = [this] (const bool enable) {
this->overlay.setVisible (enable);
overlayMouseListener.enable();
};
inspectorComponent.toggleFPSCallback = [this] (const bool enable) {
if (enable)
this->fpsMeter.setBounds (root->getLocalBounds().removeFromRight (60).removeFromTop (40));
this->fpsMeter.setVisible (enable);
settings->props->setValue ("fpsEnabled", enable);
};
inspectorComponent.toggleDragEnabledCallback = [this] (const bool enable) { this->setDraggingEnabled (enable); };
inspectorComponent.toggleSelectionMode = [this] (const bool enable) { this->setSelectionMode (enable ? FOLLOWS_FOCUS : FOLLOWS_MOUSE); };
inspectorComponent.toggleLockCallback = [this] (const bool enable) { this->setSelectionLock (enable); };
}
enum SelectionMode {
FOLLOWS_MOUSE,
FOLLOWS_FOCUS
} selectionMode { FOLLOWS_MOUSE };
void setSelectionMode (SelectionMode newMode)
{
if (newMode == selectionMode)
return;
// Out with the old
switch (selectionMode)
{
case FOLLOWS_FOCUS:
selectComponent (nullptr);
juce::Desktop::getInstance().removeFocusChangeListener (this);
break;
case FOLLOWS_MOUSE:
overlayMouseListener.disable();
break;
}
// And in with the new
selectionMode = newMode;
switch (selectionMode)
{
case FOLLOWS_FOCUS:
juce::Desktop::getInstance().addFocusChangeListener (this);
break;
case FOLLOWS_MOUSE:
overlayMouseListener.enable();
break;
}
settings->props->setValue ("inspectorSelectionMode", selectionMode);
settings->saveIfNeeded();
}
void setDraggingEnabled (const bool enable)
{
overlayMouseListener.enableDragging (enable);
overlay.enableDragging (enable);
settings->props->setValue ("enableDragging", enable);
}
void setSelectionLock (const bool enable)
{
selectionLock = enable;
}
void restoreStateFromProps()
{
setDraggingEnabled (settings->props->getBoolValue ("enableDragging", false));
setSelectionMode (static_cast<SelectionMode> (settings->props->getIntValue ("inspectorSelectionMode", FOLLOWS_MOUSE)));
}
};
}