Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 58846db

Browse files
authored
Merge pull request #16992 from dopplershift/fix-mac
Implement FigureManager.resize for macosx backend
2 parents 6621c55 + 706f7f1 commit 58846db

File tree

2 files changed

+37
-29
lines changed

2 files changed

+37
-29
lines changed

lib/matplotlib/backends/backend_macosx.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,14 @@ def __init__(self, figure):
4646
FigureCanvasBase.__init__(self, figure)
4747
width, height = self.get_width_height()
4848
_macosx.FigureCanvas.__init__(self, width, height)
49-
self._device_scale = 1.0
49+
self._dpi_ratio = 1.0
5050

5151
def _set_device_scale(self, value):
52-
if self._device_scale != value:
53-
self.figure.dpi = self.figure.dpi / self._device_scale * value
54-
self._device_scale = value
52+
if self._dpi_ratio != value:
53+
# Need the new value in place before setting figure.dpi, which
54+
# will trigger a resize
55+
self._dpi_ratio, old_value = value, self._dpi_ratio
56+
self.figure.dpi = self.figure.dpi / old_value * self._dpi_ratio
5557

5658
def _draw(self):
5759
renderer = self.get_renderer(cleared=self.figure.stale)
@@ -77,8 +79,8 @@ def resize(self, width, height):
7779
dpi = self.figure.dpi
7880
width /= dpi
7981
height /= dpi
80-
self.figure.set_size_inches(width * self._device_scale,
81-
height * self._device_scale,
82+
self.figure.set_size_inches(width * self._dpi_ratio,
83+
height * self._dpi_ratio,
8284
forward=False)
8385
FigureCanvasBase.resize_event(self)
8486
self.draw_idle()

src/_macosx.m

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,6 @@ - (void)close;
204204
@interface View : NSView <NSWindowDelegate>
205205
{ PyObject* canvas;
206206
NSRect rubberband;
207-
BOOL inside;
208207
NSTrackingRectTag tracking;
209208
@public double device_scale;
210209
}
@@ -337,6 +336,14 @@ static CGFloat _get_device_scale(CGContextRef cr)
337336

338337
NSRect rect = NSMakeRect(0.0, 0.0, width, height);
339338
self->view = [self->view initWithFrame: rect];
339+
self->view.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
340+
int opts = (NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved |
341+
NSTrackingActiveInKeyWindow | NSTrackingInVisibleRect);
342+
[self->view addTrackingArea: [
343+
[NSTrackingArea alloc] initWithRect: rect
344+
options: opts
345+
owner: self->view
346+
userInfo: nil]];
340347
[self->view setCanvas: (PyObject*)self];
341348
return 0;
342349
}
@@ -704,7 +711,6 @@ static CGFloat _get_device_scale(CGContextRef cr)
704711
[window setTitle: [NSString stringWithCString: title
705712
encoding: NSASCIIStringEncoding]];
706713

707-
[window setAcceptsMouseMovedEvents: YES];
708714
[window setDelegate: view];
709715
[window makeFirstResponder: view];
710716
[[window contentView] addSubview: view];
@@ -804,6 +810,22 @@ static CGFloat _get_device_scale(CGContextRef cr)
804810
}
805811
}
806812

813+
static PyObject*
814+
FigureManager_resize(FigureManager* self, PyObject *args, PyObject *kwds)
815+
{
816+
int width, height;
817+
if (!PyArg_ParseTuple(args, "ii", &width, &height)) {
818+
return NULL;
819+
}
820+
Window* window = self->window;
821+
if(window)
822+
{
823+
// 36 comes from hard-coded size of toolbar later in code
824+
[window setContentSize: NSMakeSize(width, height + 36.)];
825+
}
826+
Py_RETURN_NONE;
827+
}
828+
807829
static PyMethodDef FigureManager_methods[] = {
808830
{"show",
809831
(PyCFunction)FigureManager_show,
@@ -825,6 +847,11 @@ static CGFloat _get_device_scale(CGContextRef cr)
825847
METH_NOARGS,
826848
"Returns the title of the window associated with the figure manager."
827849
},
850+
{"resize",
851+
(PyCFunction)FigureManager_resize,
852+
METH_VARARGS,
853+
"Resize the window (in pixels)."
854+
},
828855
{NULL} /* Sentinel */
829856
};
830857

@@ -1564,8 +1591,6 @@ - (View*)initWithFrame:(NSRect)rect
15641591
{
15651592
self = [super initWithFrame: rect];
15661593
rubberband = NSZeroRect;
1567-
inside = false;
1568-
tracking = 0;
15691594
device_scale = 1;
15701595
return self;
15711596
}
@@ -1574,7 +1599,6 @@ - (void)dealloc
15741599
{
15751600
FigureCanvas* fc = (FigureCanvas*)canvas;
15761601
if (fc) fc->view = NULL;
1577-
[self removeTrackingRect: tracking];
15781602
[super dealloc];
15791603
}
15801604

@@ -1703,8 +1727,6 @@ - (void)windowDidResize: (NSNotification*)notification
17031727
width = size.width;
17041728
height = size.height;
17051729

1706-
[self setFrameSize: size];
1707-
17081730
PyGILState_STATE gstate = PyGILState_Ensure();
17091731
PyObject* result = PyObject_CallMethod(
17101732
canvas, "resize", "ii", width, height);
@@ -1713,11 +1735,6 @@ - (void)windowDidResize: (NSNotification*)notification
17131735
else
17141736
PyErr_Print();
17151737
PyGILState_Release(gstate);
1716-
if (tracking) [self removeTrackingRect: tracking];
1717-
tracking = [self addTrackingRect: [self bounds]
1718-
owner: self
1719-
userData: nil
1720-
assumeInside: NO];
17211738
[self setNeedsDisplay: YES];
17221739
}
17231740

@@ -1760,8 +1777,6 @@ - (void)mouseEntered:(NSEvent *)event
17601777
{
17611778
PyGILState_STATE gstate;
17621779
PyObject* result;
1763-
NSWindow* window = [self window];
1764-
if ([window isKeyWindow]==false) return;
17651780

17661781
int x, y;
17671782
NSPoint location = [event locationInWindow];
@@ -1778,29 +1793,20 @@ - (void)mouseEntered:(NSEvent *)event
17781793
else
17791794
PyErr_Print();
17801795
PyGILState_Release(gstate);
1781-
1782-
[window setAcceptsMouseMovedEvents: YES];
1783-
inside = true;
17841796
}
17851797

17861798
- (void)mouseExited:(NSEvent *)event
17871799
{
17881800
PyGILState_STATE gstate;
17891801
PyObject* result;
1790-
NSWindow* window = [self window];
1791-
if ([window isKeyWindow]==false) return;
17921802

1793-
if (inside==false) return;
17941803
gstate = PyGILState_Ensure();
17951804
result = PyObject_CallMethod(canvas, "leave_notify_event", "");
17961805
if(result)
17971806
Py_DECREF(result);
17981807
else
17991808
PyErr_Print();
18001809
PyGILState_Release(gstate);
1801-
1802-
[[self window] setAcceptsMouseMovedEvents: NO];
1803-
inside = false;
18041810
}
18051811

18061812
- (void)mouseDown:(NSEvent *)event

0 commit comments

Comments
 (0)