Browse Source

NSViewComponentPeer: Use JUCE-style coordinates to position subviews

v6.1.6
reuk 4 years ago
parent
commit
f73f8ee849
No known key found for this signature in database GPG Key ID: 9ADCD339CFC98A11
3 changed files with 36 additions and 52 deletions
  1. +22
    -26
      modules/juce_gui_basics/native/juce_mac_NSViewComponentPeer.mm
  2. +2
    -8
      modules/juce_gui_basics/native/juce_mac_Windowing.mm
  3. +12
    -18
      modules/juce_gui_extra/native/juce_mac_NSViewComponent.mm

+ 22
- 26
modules/juce_gui_basics/native/juce_mac_NSViewComponentPeer.mm View File

@@ -296,7 +296,6 @@ public:
if (isSharedWindow) if (isSharedWindow)
{ {
r.origin.y = [[view superview] frame].size.height - (r.origin.y + r.size.height);
[view setFrame: r]; [view setFrame: r];
} }
else else
@@ -326,10 +325,6 @@ public:
flipScreenRect (r); flipScreenRect (r);
} }
else
{
r.origin.y = [[view superview] frame].size.height - r.origin.y - r.size.height;
}
return convertToRectInt (r); return convertToRectInt (r);
} }
@@ -437,7 +432,7 @@ public:
if (NSWindow* const viewWindow = [view window]) if (NSWindow* const viewWindow = [view window])
{ {
NSRect windowFrame = [viewWindow frame]; NSRect windowFrame = [viewWindow frame];
NSPoint windowPoint = [view convertPoint: NSMakePoint (localPos.x, viewFrame.size.height - localPos.y) toView: nil];
NSPoint windowPoint = [view convertPoint: NSMakePoint (localPos.x, localPos.y) toView: nil];
NSPoint screenPoint = NSMakePoint (windowFrame.origin.x + windowPoint.x, NSPoint screenPoint = NSMakePoint (windowFrame.origin.x + windowPoint.x,
windowFrame.origin.y + windowPoint.y); windowFrame.origin.y + windowPoint.y);
@@ -448,7 +443,7 @@ public:
} }
NSView* v = [view hitTest: NSMakePoint (viewFrame.origin.x + localPos.getX(), NSView* v = [view hitTest: NSMakePoint (viewFrame.origin.x + localPos.getX(),
viewFrame.origin.y + viewFrame.size.height - localPos.getY())];
viewFrame.origin.y + localPos.getY())];
return trueIfInAChildWindow ? (v != nil) return trueIfInAChildWindow ? (v != nil)
: (v == view); : (v == view);
@@ -463,8 +458,8 @@ public:
NSRect v = [view convertRect: [view frame] toView: nil]; NSRect v = [view convertRect: [view frame] toView: nil];
NSRect w = [window frame]; NSRect w = [window frame];
b.setTop ((int) (w.size.height - (v.origin.y + v.size.height)));
b.setBottom ((int) v.origin.y);
b.setTop ((int) v.origin.y);
b.setBottom ((int) (w.size.height - (v.origin.y + v.size.height)));
b.setLeft ((int) v.origin.x); b.setLeft ((int) v.origin.x);
b.setRight ((int) (w.size.width - (v.origin.x + v.size.width))); b.setRight ((int) (w.size.width - (v.origin.x + v.size.width)));
} }
@@ -913,14 +908,14 @@ public:
#if USE_COREGRAPHICS_RENDERING #if USE_COREGRAPHICS_RENDERING
if (usingCoreGraphics) if (usingCoreGraphics)
{ {
CGContextConcatCTM (cg, CGAffineTransformMake (1, 0, 0, -1, 0, getComponent().getHeight()));
CoreGraphicsContext context (cg, (float) [view frame].size.height); CoreGraphicsContext context (cg, (float) [view frame].size.height);
invokePaint (context);
handlePaint (context);
} }
else else
#endif #endif
{ {
const Point<int> offset (-roundToInt (r.origin.x),
-roundToInt ([view frame].size.height - (r.origin.y + r.size.height)));
const Point<int> offset (-roundToInt (r.origin.x), -roundToInt (r.origin.y));
auto clipW = (int) (r.size.width + 0.5f); auto clipW = (int) (r.size.width + 0.5f);
auto clipH = (int) (r.size.height + 0.5f); auto clipH = (int) (r.size.height + 0.5f);
@@ -941,17 +936,18 @@ public:
clip.scaleAll (intScale); clip.scaleAll (intScale);
auto context = component.getLookAndFeel() auto context = component.getLookAndFeel()
.createGraphicsContext (temp, offset * intScale, clip);
.createGraphicsContext (temp, offset * intScale, clip);
if (intScale != 1) if (intScale != 1)
context->addTransform (AffineTransform::scale (displayScale)); context->addTransform (AffineTransform::scale (displayScale));
invokePaint (*context);
handlePaint (*context);
} }
detail::ColorSpacePtr colourSpace { CGColorSpaceCreateWithName (kCGColorSpaceSRGB) }; detail::ColorSpacePtr colourSpace { CGColorSpaceCreateWithName (kCGColorSpaceSRGB) };
CGImageRef image = juce_createCoreGraphicsImage (temp, colourSpace.get(), false); CGImageRef image = juce_createCoreGraphicsImage (temp, colourSpace.get(), false);
CGContextDrawImage (cg, CGRectMake (r.origin.x, r.origin.y, clipW, clipH), image);
CGContextConcatCTM (cg, CGAffineTransformMake (1, 0, 0, -1, r.origin.x, r.origin.y + clipH));
CGContextDrawImage (cg, CGRectMake (0.0f, 0.0f, clipW, clipH), image);
CGImageRelease (image); CGImageRelease (image);
} }
} }
@@ -964,7 +960,7 @@ public:
// a few when there's a lot of activity. // a few when there's a lot of activity.
// As a work around for this, we use a RectangleList to do our own coalescing of regions before // As a work around for this, we use a RectangleList to do our own coalescing of regions before
// asynchronously asking the OS to repaint them. // asynchronously asking the OS to repaint them.
deferredRepaints.add ((float) area.getX(), (float) ([view frame].size.height - area.getBottom()),
deferredRepaints.add ((float) area.getX(), (float) area.getY(),
(float) area.getWidth(), (float) area.getHeight()); (float) area.getWidth(), (float) area.getHeight());
if (isTimerRunning()) if (isTimerRunning())
@@ -1008,11 +1004,6 @@ public:
deferredRepaints.clear(); deferredRepaints.clear();
} }
void invokePaint (LowLevelGraphicsContext& context)
{
handlePaint (context);
}
void performAnyPendingRepaintsNow() override void performAnyPendingRepaintsNow() override
{ {
[view displayIfNeeded]; [view displayIfNeeded];
@@ -1320,7 +1311,7 @@ public:
static Point<float> getMousePos (NSEvent* e, NSView* view) static Point<float> getMousePos (NSEvent* e, NSView* view)
{ {
NSPoint p = [view convertPoint: [e locationInWindow] fromView: nil]; NSPoint p = [view convertPoint: [e locationInWindow] fromView: nil];
return { (float) p.x, (float) ([view frame].size.height - p.y) };
return { (float) p.x, (float) p.y };
} }
static int getModifierForButtonNumber (const NSInteger num) static int getModifierForButtonNumber (const NSInteger num)
@@ -1356,7 +1347,7 @@ public:
NSPoint p = [view convertPoint: [sender draggingLocation] fromView: nil]; NSPoint p = [view convertPoint: [sender draggingLocation] fromView: nil];
ComponentPeer::DragInfo dragInfo; ComponentPeer::DragInfo dragInfo;
dragInfo.position.setXY ((int) p.x, (int) ([view frame].size.height - p.y));
dragInfo.position.setXY ((int) p.x, (int) p.y);
if (contentType == NSPasteboardTypeString) if (contentType == NSPasteboardTypeString)
dragInfo.text = nsStringToJuce ([pasteboard stringForType: NSPasteboardTypeString]); dragInfo.text = nsStringToJuce ([pasteboard stringForType: NSPasteboardTypeString]);
@@ -1522,13 +1513,12 @@ private:
[view getRectsBeingDrawn: &rects count: &numRects]; [view getRectsBeingDrawn: &rects count: &numRects];
const Rectangle<int> clipBounds (clipW, clipH); const Rectangle<int> clipBounds (clipW, clipH);
auto viewH = [view frame].size.height;
clip.ensureStorageAllocated ((int) numRects); clip.ensureStorageAllocated ((int) numRects);
for (int i = 0; i < numRects; ++i) for (int i = 0; i < numRects; ++i)
clip.addWithoutMerging (clipBounds.getIntersection (Rectangle<int> (roundToInt (rects[i].origin.x) + offset.x, clip.addWithoutMerging (clipBounds.getIntersection (Rectangle<int> (roundToInt (rects[i].origin.x) + offset.x,
roundToInt (viewH - (rects[i].origin.y + rects[i].size.height)) + offset.y,
roundToInt (rects[i].origin.y) + offset.y,
roundToInt (rects[i].size.width), roundToInt (rects[i].size.width),
roundToInt (rects[i].size.height)))); roundToInt (rects[i].size.height))));
} }
@@ -1741,6 +1731,8 @@ struct JuceNSViewClass : public ObjCClass<NSView>
addMethod (@selector (cut:), cut, "v@:@"); addMethod (@selector (cut:), cut, "v@:@");
addMethod (@selector (selectAll:), selectAll, "v@:@"); addMethod (@selector (selectAll:), selectAll, "v@:@");
addMethod (@selector (isFlipped), isFlipped, "c@:");
addMethod (NSViewComponentPeer::dismissModalsSelector, dismissModals, "v@:"); addMethod (NSViewComponentPeer::dismissModalsSelector, dismissModals, "v@:");
addMethod (NSViewComponentPeer::asyncMouseDownSelector, asyncMouseDown, "v@:@"); addMethod (NSViewComponentPeer::asyncMouseDownSelector, asyncMouseDown, "v@:@");
addMethod (NSViewComponentPeer::asyncMouseUpSelector, asyncMouseUp, "v@:@"); addMethod (NSViewComponentPeer::asyncMouseUpSelector, asyncMouseUp, "v@:@");
@@ -1816,6 +1808,8 @@ private:
static void dismissModals (id self, SEL) { if (auto* p = getOwner (self)) p->dismissModals(); } static void dismissModals (id self, SEL) { if (auto* p = getOwner (self)) p->dismissModals(); }
static void becomeKey (id self, SEL) { if (auto* p = getOwner (self)) p->becomeKey(); } static void becomeKey (id self, SEL) { if (auto* p = getOwner (self)) p->becomeKey(); }
static BOOL isFlipped (id, SEL) { return true; }
static void viewWillDraw (id self, SEL) static void viewWillDraw (id self, SEL)
{ {
// Without setting contentsFormat macOS Big Sur will always set the invalid area // Without setting contentsFormat macOS Big Sur will always set the invalid area
@@ -2099,6 +2093,7 @@ struct JuceNSWindowClass : public ObjCClass<NSWindow>
addMethod (@selector (windowWillStartLiveResize:), windowWillStartLiveResize, "v@:@"); addMethod (@selector (windowWillStartLiveResize:), windowWillStartLiveResize, "v@:@");
addMethod (@selector (windowDidEndLiveResize:), windowDidEndLiveResize, "v@:@"); addMethod (@selector (windowDidEndLiveResize:), windowDidEndLiveResize, "v@:@");
addMethod (@selector (window:shouldPopUpDocumentPathMenu:), shouldPopUpPathMenu, "B@:@", @encode (NSMenu*)); addMethod (@selector (window:shouldPopUpDocumentPathMenu:), shouldPopUpPathMenu, "B@:@", @encode (NSMenu*));
addMethod (@selector (isFlipped), isFlipped, "c@:");
addMethod (@selector (window:shouldDragDocumentWithEvent:from:withPasteboard:), addMethod (@selector (window:shouldDragDocumentWithEvent:from:withPasteboard:),
shouldAllowIconDrag, "B@:@", @encode (NSEvent*), @encode (NSPoint), @encode (NSPasteboard*)); shouldAllowIconDrag, "B@:@", @encode (NSEvent*), @encode (NSPoint), @encode (NSPasteboard*));
@@ -2115,6 +2110,8 @@ private:
} }
//============================================================================== //==============================================================================
static BOOL isFlipped (id, SEL) { return true; }
static BOOL canBecomeKeyWindow (id self, SEL) static BOOL canBecomeKeyWindow (id self, SEL)
{ {
auto* owner = getOwner (self); auto* owner = getOwner (self);
@@ -2186,7 +2183,6 @@ private:
return proposedFrameSize; return proposedFrameSize;
NSRect frameRect = [(NSWindow*) self frame]; NSRect frameRect = [(NSWindow*) self frame];
frameRect.origin.y -= proposedFrameSize.height - frameRect.size.height;
frameRect.size = proposedFrameSize; frameRect.size = proposedFrameSize;
frameRect = owner->constrainRect (frameRect); frameRect = owner->constrainRect (frameRect);


+ 2
- 8
modules/juce_gui_basics/native/juce_mac_Windowing.mm View File

@@ -466,12 +466,6 @@ struct DisplaySettingsChangeCallback : private DeletedAtShutdown
JUCE_IMPLEMENT_SINGLETON (DisplaySettingsChangeCallback) JUCE_IMPLEMENT_SINGLETON (DisplaySettingsChangeCallback)
static Rectangle<int> convertDisplayRect (NSRect r, CGFloat mainScreenBottom)
{
r.origin.y = mainScreenBottom - (r.origin.y + r.size.height);
return convertToRectInt (r);
}
static Displays::Display getDisplayFromScreen (NSScreen* s, CGFloat& mainScreenBottom, const float masterScale) static Displays::Display getDisplayFromScreen (NSScreen* s, CGFloat& mainScreenBottom, const float masterScale)
{ {
Displays::Display d; Displays::Display d;
@@ -481,8 +475,8 @@ static Displays::Display getDisplayFromScreen (NSScreen* s, CGFloat& mainScreenB
if (d.isMain) if (d.isMain)
mainScreenBottom = [s frame].size.height; mainScreenBottom = [s frame].size.height;
d.userArea = convertDisplayRect ([s visibleFrame], mainScreenBottom) / masterScale;
d.totalArea = convertDisplayRect ([s frame], mainScreenBottom) / masterScale;
d.userArea = convertToRectInt ([s visibleFrame]) / masterScale;
d.totalArea = convertToRectInt ([s frame]) / masterScale;
d.scale = masterScale; d.scale = masterScale;
if ([s respondsToSelector: @selector (backingScaleFactor)]) if ([s respondsToSelector: @selector (backingScaleFactor)])


+ 12
- 18
modules/juce_gui_extra/native/juce_mac_NSViewComponent.mm View File

@@ -125,25 +125,12 @@ public:
[view release]; [view release];
} }
void componentMovedOrResized (Component& comp, bool wasMoved, bool wasResized) override
{
ComponentMovementWatcher::componentMovedOrResized (comp, wasMoved, wasResized);
// The ComponentMovementWatcher version of this method avoids calling
// us when the top-level comp is resized, but for an NSView we need to know this
// because with inverted coordinates, we need to update the position even if the
// top-left pos hasn't changed
if (comp.isOnDesktop() && wasResized)
componentMovedOrResized (wasMoved, wasResized);
}
void componentMovedOrResized (bool /*wasMoved*/, bool /*wasResized*/) override void componentMovedOrResized (bool /*wasMoved*/, bool /*wasResized*/) override
{ {
if (auto* peer = owner.getTopLevelComponent()->getPeer()) if (auto* peer = owner.getTopLevelComponent()->getPeer())
{ {
auto r = makeNSRect (peer->getAreaCoveredBy (owner));
r.origin.y = [[view superview] frame].size.height - (r.origin.y + r.size.height);
[view setFrame: r];
const auto newArea = makeNSRect (peer->getAreaCoveredBy (owner));
[view setFrame: newArea];
} }
} }
@@ -204,8 +191,8 @@ private:
}; };
//============================================================================== //==============================================================================
NSViewComponent::NSViewComponent() {}
NSViewComponent::~NSViewComponent() {}
NSViewComponent::NSViewComponent() = default;
NSViewComponent::~NSViewComponent() = default;
void NSViewComponent::setView (void* view) void NSViewComponent::setView (void* view)
{ {
@@ -232,8 +219,15 @@ void NSViewComponent::resizeToFitView()
{ {
if (attachment != nullptr) if (attachment != nullptr)
{ {
auto r = [static_cast<NSViewAttachment*> (attachment.get())->view frame];
auto* view = static_cast<NSViewAttachment*> (attachment.get())->view;
auto r = [view frame];
setBounds (Rectangle<int> ((int) r.size.width, (int) r.size.height)); setBounds (Rectangle<int> ((int) r.size.width, (int) r.size.height));
if (auto* peer = getTopLevelComponent()->getPeer())
{
const auto position = peer->getAreaCoveredBy (*this).getPosition().toFloat();
[view setFrameOrigin: NSMakePoint (position.x, position.y)];
}
} }
} }


Loading…
Cancel
Save