Skip to content

MNT: Clean up macosx backend set_message #21755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions lib/matplotlib/backends/backend_macosx.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,6 @@ def prepare_configure_subplots(self):
_tool = SubplotTool(self.canvas.figure, toolfig)
return canvas

def set_message(self, message):
_macosx.NavigationToolbar2.set_message(self, message.encode('utf-8'))


class FigureManagerMac(_macosx.FigureManager, FigureManagerBase):
_toolbar2_class = NavigationToolbar2Mac
Expand Down
33 changes: 19 additions & 14 deletions src/_macosx.m
Original file line number Diff line number Diff line change
Expand Up @@ -934,20 +934,20 @@ -(void)save_figure:(id)sender { gil_call_method(toolbar, "save_figure"); }
[self->handler installCallbacks: actions forButtons: buttons];

NSFont* font = [NSFont systemFontOfSize: 0.0];
rect.size.width = 300;
rect.size.height = 0;
rect.origin.x += height;
NSTextView* messagebox = [[NSTextView alloc] initWithFrame: rect];
// rect.origin.x is now at the far right edge of the buttons
// we want the messagebox to take up the rest of the toolbar area
// Make it a zero-width box if we don't have enough room
rect.size.width = fmax(bounds.size.width - rect.origin.x, 0);
rect.origin.x = bounds.size.width - rect.size.width;
NSTextView* messagebox = [[[NSTextView alloc] initWithFrame: rect] autorelease];
messagebox.textContainer.maximumNumberOfLines = 2;
messagebox.textContainer.lineBreakMode = NSLineBreakByTruncatingTail;
messagebox.alignment = NSTextAlignmentRight;
[messagebox setFont: font];
[messagebox setDrawsBackground: NO];
[messagebox setSelectable: NO];
/* if selectable, the messagebox can become first responder,
* which is not supposed to happen */
rect = [messagebox frame];
rect.origin.y = 0.5 * (height - rect.size.height);
[messagebox setFrameOrigin: rect.origin];
[[window contentView] addSubview: messagebox];
[messagebox release];
[[window contentView] display];
Expand All @@ -974,26 +974,31 @@ -(void)save_figure:(id)sender { gil_call_method(toolbar, "save_figure"); }
{
const char* message;

if (!PyArg_ParseTuple(args, "y", &message)) { return NULL; }
if (!PyArg_ParseTuple(args, "s", &message)) { return NULL; }

NSTextView* messagebox = self->messagebox;

if (messagebox) {
NSString* text = [NSString stringWithUTF8String: message];
[messagebox setString: text];

// Adjust width with the window size
// Adjust width and height with the window size and content
NSRect rectWindow = [messagebox.superview frame];
NSRect rect = [messagebox frame];
// Entire region to the right of the buttons
rect.size.width = rectWindow.size.width - rect.origin.x;
[messagebox setFrame: rect];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The text will wrap depending on width, which is why this width must be set before asking for the height. That is, unless text wrapping is off? But then I guess we would not care about setting the height, so it seems that removing this line will break things.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, updated. This is needed if we want to center the text vertically properly based on window resizes.


// Adjust height with the content size
// We want to control the vertical position of
// the rect by the content size to center it vertically
[messagebox.layoutManager ensureLayoutForTextContainer: messagebox.textContainer];
NSRect contentSize = [messagebox.layoutManager usedRectForTextContainer: messagebox.textContainer];
rect = [messagebox frame];
rect.origin.y = 0.5 * (self->height - contentSize.size.height);
NSRect contentRect = [messagebox.layoutManager usedRectForTextContainer: messagebox.textContainer];
rect.origin.y = 0.5 * (self->height - contentRect.size.height);
rect.size.height = contentRect.size.height;
[messagebox setFrame: rect];
// Disable cursorRects so that the cursor doesn't get updated by events
// in NSApp (like resizing TextViews), we want to handle the cursor
// changes from within MPL with set_cursor() ourselves
[[messagebox.superview window] disableCursorRects];
}

Py_RETURN_NONE;
Expand Down