Skip to content

Commit bcef5d6

Browse files
authored
Merge pull request matplotlib#21755 from greglucas/macosx-message-box
MNT: Clean up macosx backend set_message
2 parents 9926f06 + 5dcdcb8 commit bcef5d6

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

lib/matplotlib/backends/backend_macosx.py

-3
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,6 @@ def prepare_configure_subplots(self):
136136
_tool = SubplotTool(self.canvas.figure, toolfig)
137137
return canvas
138138

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

143140
class FigureManagerMac(_macosx.FigureManager, FigureManagerBase):
144141
_toolbar2_class = NavigationToolbar2Mac

src/_macosx.m

+19-14
Original file line numberDiff line numberDiff line change
@@ -934,20 +934,20 @@ -(void)save_figure:(id)sender { gil_call_method(toolbar, "save_figure"); }
934934
[self->handler installCallbacks: actions forButtons: buttons];
935935

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

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

979979
NSTextView* messagebox = self->messagebox;
980980

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

985-
// Adjust width with the window size
985+
// Adjust width and height with the window size and content
986986
NSRect rectWindow = [messagebox.superview frame];
987987
NSRect rect = [messagebox frame];
988+
// Entire region to the right of the buttons
988989
rect.size.width = rectWindow.size.width - rect.origin.x;
989990
[messagebox setFrame: rect];
990-
991-
// Adjust height with the content size
991+
// We want to control the vertical position of
992+
// the rect by the content size to center it vertically
992993
[messagebox.layoutManager ensureLayoutForTextContainer: messagebox.textContainer];
993-
NSRect contentSize = [messagebox.layoutManager usedRectForTextContainer: messagebox.textContainer];
994-
rect = [messagebox frame];
995-
rect.origin.y = 0.5 * (self->height - contentSize.size.height);
994+
NSRect contentRect = [messagebox.layoutManager usedRectForTextContainer: messagebox.textContainer];
995+
rect.origin.y = 0.5 * (self->height - contentRect.size.height);
996+
rect.size.height = contentRect.size.height;
996997
[messagebox setFrame: rect];
998+
// Disable cursorRects so that the cursor doesn't get updated by events
999+
// in NSApp (like resizing TextViews), we want to handle the cursor
1000+
// changes from within MPL with set_cursor() ourselves
1001+
[[messagebox.superview window] disableCursorRects];
9971002
}
9981003

9991004
Py_RETURN_NONE;

0 commit comments

Comments
 (0)