Minimise = Collapse (Code snippets)

I wanted the minimise functionality in my app to be more Mac-like on the Mac side, so I changed it to collapse/expand the window instead of doing a hide/show. I thought I’d post the code in case anyone else has thought about implementing this.

In juce_mac_Windowing.cpp, make the following modifications:

[code]static pascal OSStatus handleBoundsEvent (EventHandlerCallRef callRef, EventRef theEvent, void* userData)
{
MessageManagerLock messLock;

const UInt32 what = GetEventKind (theEvent);
NativeDesktopWindow* const ownerWindow = getOwnerForWindow ((WindowRef) userData);

if (what == kEventWindowBoundsChanged)
{
    ownerWindow->handleMovedOrResized();
}

// Start Collapse Modification
else if (what == kEventWindowCollapsed)
{
minimisedWindows.addIfNotAlreadyThere(ownerWindow->getNativeHandle());
ownerWindow->handleMovedOrResized();
}
else if (what == kEventWindowExpanded)
{
minimisedWindows.removeValue(ownerWindow->getNativeHandle());
ownerWindow->handleMovedOrResized();
}
// End Collapse Modification[/code]

[code]void NativeDesktopWindow::initialiseNativeWindow (const String& title, int windowStyleFlags)
{
initialiseDragDropHandler();

WindowRef window;
const Rect pos = { 0, 0, 0, 0 };

// Start Collapse Modification
int attributes = kWindowStandardHandlerAttribute | kWindowCollapseBoxAttribute;
// End Collapse Modification
if ((windowStyleFlags & Component::windowHasDropShadow) == 0)
attributes |= kWindowNoShadowAttribute;

const EventTypeSpec types1[] = { { kEventClassWindow, kEventWindowDrawContent } };
InstallWindowEventHandler (window, NewEventHandlerUPP (handleRepaintEvent), 1, types1, (void*) window, 0);

// Start Collapse Modification
const EventTypeSpec types2[] =
{
{ kEventClassWindow, kEventWindowBoundsChanged },
{ kEventClassWindow, kEventWindowCollapsed },
{ kEventClassWindow, kEventWindowExpanded },
{ kEventClassWindow, kEventWindowFocusAcquired },
{ kEventClassWindow, kEventWindowFocusRelinquish }
};

InstallWindowEventHandler (window, NewEventHandlerUPP (handleBoundsEvent), 5, types2, (void*) window, 0);

// End Collapse Modification[/code]

static void setWindowMinimised (WindowRef ref, bool isMinimised) { // Start Collapse Modification if (isMinimised != minimisedWindows.contains (ref)) CollapseWindow(ref, isMinimised); // End Collapse Modification }

To implement a minimise button (in this case, a yellow “-” similar to the red “X”), add a ShapeButton to DialogWindow (or a subclass – you’ll need to make DialogWindow’s private stuff protected) like this:

In Header:

In Constructor:

[code] if (minimiseButton = new ShapeButton(T(“Minimize”),
Colour(0x7fffff33),
Colour(0xd7ffff33),
Colour(0xf7ffff33)))
{
Path shape;
shape.addLineSegment(0.0f, 0.0f, 1.0f, 0.0f, 0.35f);
minimiseButton->setShape(shape, true, true, true);

    addAndMakeVisible(minimiseButton);
    minimiseButton->addButtonListener(this);
}[/code]

In buttonClicked():

if (button == minimiseButton) { setMinimised(true); }

In resized():

minimiseButton->setBounds(closeButton->getX() - closeButton->getWidth() - 4, closeButton->getY(), closeButton->getWidth(), closeButton->getHeight());

Has anybody tried the code or 1.19 with “Window Shade X” from http://www.unsanity.com/haxies/wsx/ ?