Hobbiest programmer. Just wondering if this is a bug or a quirk or just something I don’t understand.
On my Mac apps I create my own buttons by placing a label inside a small rectangle shape. The shape is the same colour as the background.
Then with the mouseEnter event, I can change the shape’s colour and/or bold the label etc. The mouseExit event will then return them both to default. Maybe not elegant, but it works and looks ok. I then use the MouseDown event on the label to execute the action.
One of the ones I usually create is an “X” from SFSymbols and use this to close a window that has been opened. I accidentally put the Window.Close action onto the MouseDown event for the shape recently and it crashed the app. I’ve tested this with another “test” app and the exact same thing happens. That is open a second window from the main one, place a square shape and try and close the window from the MouseDown event.
So, is this a bug or is something happening that I don’t understand?
Hope this all made sense.
You guys are awesome, as always, thanks for any help/insight.
Ok, so the windows I usually create are floating windows with the 3 upper left buttons disabled and made not resizable. They are then opened in modal mode (not sure if that’s the correct terminology). Therefore I have to create a seperate button to close the window. Also, I don’t call the window “Window” I was just calling it that for the purpose of the question.
I appreciate your reply, but it still has missed the point of the question. Why does closing a window from the MouseDown event on a rectangle control crash the app, when it works ok from the Label control?
As far as the debugger, I actually get one of those internal MacOS messages telling me that Test.debug quit unexpectedly (note “Test” is the whatever the name of the app is) with 2 buttons to send a report or ignore. If I ignore I’m then left with XOJO still running as normal, but app shut down.
Personally I use MouseUp for the actual work of the button, as it affords the user, having pressed the mouse in the button, the chance to slide the mouse off trhe button and then release the mouse, without the action being done.
This is really just an education type question. I don’t normally do it from the rectangle, I do it from the label. It was just accidental that I discovered it so was curious. I’ll continue calling it from the label.
Yes, I can confirm what you say: putting
in the mousedown event a rectangle, and clicking the rectangle, the app crashes;
while if the same code is inside the mousedown event of a label inside the rectangle the app does no crash.
Using 2021v2.1 on BigSur
Process: My Application.debug 
Path: /var/folders/*/My Application.debug.app/Contents/MacOS/My Application.debug
Version: ??? (188.8.131.52.0)
Code Type: X86-64 (Native)
Parent Process: ??? 
Responsible: My Application.debug 
User ID: 501
If it makes it easier, this is the full example of what I do.
On any window, I create an array of small rectangles called recButton() and a corresponding array of labels called lblButton(). Each respective label is placed on the rectangle.
In the MouseEnter event of the rectangles:
me.fillColor = color.LightGray
lblButton(x).Bold = True
In the MouseExit event of the rectangles:
me.fillColor = RGB(… (whatever the background is)
lblButton(x).Bold = False
I then create method called ProcessButtons(index As integer)
This method is:
Select Case index
Each case then processes the code for the action of that button. One of them will be WindowXXX.Close
In the MouseDown event of the labels I call ProcessButtons(index) where index is the array number.
This works fine. As I said, maybe not elegant from a pro’s point of view, but it works. I’m always happy to try and learn tho. As for canvas, I think I tried that once a while ago and couldn’t get the hang of it so started using this method.
Whilst it hasn’t answered the initial question, thanks for the other suggestions. I’ll have a look at canvas but, as you can see from the more extensive code I put up, this works because the rectangle has a simple 1 liner to change its colour with Fill. With the canvas I have to write much longer code to paint it don’t I? Or is there another way?
I would say, based on the type of crash, that this is the Xojo framework trying to access a recently freed pointer. That is, events are probably being scheduled before the window closes and then fire for something that no longer exists.
whatever the workarounds, the fact is that a rectangle, even without any other object inside, crashes the app when calling self.close from the mousedown.event.
While it does not happen with labels or other objects.
Xojo would need to be able to look at the code paths and see what is different. Heck, it’s possible that it has to do with the fact that it’s a rectangle vs a control as technically a rectangle isn’t a control, but a decoration and the underlying OS hierarchy is different.