Skip to content

Commit e14107f

Browse files
committed
MNT: Clean up macosx backend set_message
Remove some unnecessary calculations and add some comments. Switch to string passing since we are guaranteed UTF-8 with Python 3 now.
1 parent 73304d2 commit e14107f

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

lib/matplotlib/backends/backend_macosx.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,6 @@ def prepare_configure_subplots(self):
116116
_tool = SubplotTool(self.canvas.figure, toolfig)
117117
return canvas
118118

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

123120
@_Backend.export
124121
class _BackendMac(_Backend):

src/_macosx.m

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,22 +1085,21 @@ -(void)save_figure:(id)sender
10851085
[self->handler installCallbacks: actions forButtons: buttons];
10861086

10871087
NSFont* font = [NSFont systemFontOfSize: 0.0];
1088-
rect.size.width = 300;
1089-
rect.size.height = 0;
1090-
rect.origin.x += height;
1091-
NSTextView* messagebox = [[NSTextView alloc] initWithFrame: rect];
1088+
// rect.origin.x is now at the far right edge of the buttons
1089+
// we want the messagebox to take up the rest of the toolbar area
1090+
rect.size.width = bounds.size.width - rect.origin.x;
1091+
rect.origin.x = bounds.size.width - rect.size.width;
1092+
NSTextView* messagebox = [[[NSTextView alloc] initWithFrame: rect] autorelease];
10921093
if (@available(macOS 10.11, *)) {
10931094
messagebox.textContainer.maximumNumberOfLines = 2;
10941095
messagebox.textContainer.lineBreakMode = NSLineBreakByTruncatingTail;
1096+
messagebox.alignment = NSTextAlignmentRight;
10951097
}
10961098
[messagebox setFont: font];
10971099
[messagebox setDrawsBackground: NO];
10981100
[messagebox setSelectable: NO];
10991101
/* if selectable, the messagebox can become first responder,
11001102
* which is not supposed to happen */
1101-
rect = [messagebox frame];
1102-
rect.origin.y = 0.5 * (height - rect.size.height);
1103-
[messagebox setFrameOrigin: rect.origin];
11041103
[[window contentView] addSubview: messagebox];
11051104
[messagebox release];
11061105
[[window contentView] display];
@@ -1130,25 +1129,29 @@ -(void)save_figure:(id)sender
11301129
{
11311130
const char* message;
11321131

1133-
if (!PyArg_ParseTuple(args, "y", &message)) { return NULL; }
1132+
if (!PyArg_ParseTuple(args, "s", &message)) { return NULL; }
11341133

11351134
NSTextView* messagebox = self->messagebox;
11361135

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

1141-
// Adjust width with the window size
1140+
// Adjust width and height with the window size and content
11421141
NSRect rectWindow = [messagebox.superview frame];
11431142
NSRect rect = [messagebox frame];
1143+
// Entire region to the right of the buttons
11441144
rect.size.width = rectWindow.size.width - rect.origin.x;
11451145
[messagebox setFrame: rect];
1146-
1147-
// Adjust height with the content size
1146+
// We want to control the vertical position of
1147+
// the rect by the content size to center it vertically
1148+
// TODO: This seems to disable the cursor updates with newlines that
1149+
// are included in the image hover text. It is only when trying
1150+
// to set the frame height based on the contentRect's size.
11481151
[messagebox.layoutManager ensureLayoutForTextContainer: messagebox.textContainer];
1149-
NSRect contentSize = [messagebox.layoutManager usedRectForTextContainer: messagebox.textContainer];
1150-
rect = [messagebox frame];
1151-
rect.origin.y = 0.5 * (self->height - contentSize.size.height);
1152+
NSRect contentRect = [messagebox.layoutManager usedRectForTextContainer: messagebox.textContainer];
1153+
rect.origin.y = 0.5 * (self->height - contentRect.size.height);
1154+
rect.size.height = contentRect.size.height;
11521155
[messagebox setFrame: rect];
11531156
}
11541157

0 commit comments

Comments
 (0)