From f732864806870d03a5f725375a511102c2d2491c Mon Sep 17 00:00:00 2001 From: Greg Lucas Date: Thu, 25 Nov 2021 16:02:23 -0700 Subject: [PATCH 1/2] 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. --- lib/matplotlib/backends/backend_macosx.py | 3 --- src/_macosx.m | 31 +++++++++++++---------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/lib/matplotlib/backends/backend_macosx.py b/lib/matplotlib/backends/backend_macosx.py index 08cdeb73c2d9..08334ca4ecd2 100644 --- a/lib/matplotlib/backends/backend_macosx.py +++ b/lib/matplotlib/backends/backend_macosx.py @@ -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 diff --git a/src/_macosx.m b/src/_macosx.m index 0e01a1e6e411..75d6b772a357 100755 --- a/src/_macosx.m +++ b/src/_macosx.m @@ -934,20 +934,19 @@ -(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 + rect.size.width = bounds.size.width - rect.origin.x; + 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]; @@ -974,7 +973,7 @@ -(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; @@ -982,17 +981,21 @@ -(void)save_figure:(id)sender { gil_call_method(toolbar, "save_figure"); } 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]; - - // Adjust height with the content size + // We want to control the vertical position of + // the rect by the content size to center it vertically + // TODO: This seems to disable the cursor updates with newlines that + // are included in the image hover text. It is only when trying + // to set the frame height based on the contentRect's size. [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]; } From 5dcdcb8c25ec7742408810c4e73883b1c1fc18c0 Mon Sep 17 00:00:00 2001 From: Greg Lucas Date: Sun, 1 May 2022 10:57:29 -0600 Subject: [PATCH 2/2] FIX: Disable cursorRect updates from within macosx backend We want to keep control of the cursor updates from within MPL, so don't allow the App to push a new cursor update when the window gets updated. This would happen when a TextView would get resized and update the frame. --- src/_macosx.m | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/_macosx.m b/src/_macosx.m index 75d6b772a357..279c1544c731 100755 --- a/src/_macosx.m +++ b/src/_macosx.m @@ -936,7 +936,8 @@ -(void)save_figure:(id)sender { gil_call_method(toolbar, "save_figure"); } NSFont* font = [NSFont systemFontOfSize: 0.0]; // 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 - rect.size.width = bounds.size.width - rect.origin.x; + // 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; @@ -989,14 +990,15 @@ -(void)save_figure:(id)sender { gil_call_method(toolbar, "save_figure"); } [messagebox setFrame: rect]; // We want to control the vertical position of // the rect by the content size to center it vertically - // TODO: This seems to disable the cursor updates with newlines that - // are included in the image hover text. It is only when trying - // to set the frame height based on the contentRect's size. [messagebox.layoutManager ensureLayoutForTextContainer: messagebox.textContainer]; 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;