Checkbox in menu bar (for settings) and save it

Hello,

I added ‘setting’ to the menubar. I can add elements to but is there a way to add checkboxes?
Also, what is the best way to save these settings and load at startup?

Thanks

You can add checkmarks (not checkboxes, which are ☐:ballot_box_with_check:) to menu items inside menus (you can’t add them to the title of the menus right inside the menu bar (well, you could add “:heavy_check_mark:” but you shouldn’t)).
Check the DesktopMenuItem.HasCheckMark property if this is what you want.

There’s no best way.
My own technique is to save the settings in a binary file, using the BinaryStream class.
You can also use CoreFoundation (Mac), the Registry (Windows), text files, databases, etc.

I save them to and load them from an SQLite database - called Settings.

There is an age old feature request to add support for the native preferences system on the macOS. Xojo doesn’t think it is needed, but if you do, find the report and add your vote.

In fact I was blown away how easy it is with other tools. Add @AppStorage( “propertyname” ) to the declaration of the variable and that’s it… Any time you modify that variable it is auto updated in the preferences for you.

@AppStorage("username") var username: String = "Anonymous"
2 Likes

This is exactly what I want, thanks.

About save/load settings on Mac: If I put a file, SQLite, binary file, whatever in the app folder (and change it later) does it affect code signature/Notarization?
Or better use @AppStorage Sam mentioned above?

Wrong idea… that folder is not made for that.

You have to follow the rules and save the settings (Preferences) where it belongs in their espective OSes…

Seems it’s working on both windows and mac with the same code as it uses

FolderItem = SpecialFolder.Preferences.Child(PrefFileName)

Is that correct?

@AppStorage can’t be used in Xojo.

No, you can’t use a database inside the app folder. The Preferences folder on macOS is for plist files. I use ApplicationSupport to store my settings database.

To be clear:

when people here told you to not write in the Applications folder…

a. it is because you do not hjave the OS rights to write there
b. there is a location especially created to do that.

In no case, it is a personal taste.

Now, try and cry, if you prefer.

OK, thanks, got it :slight_smile:

So store it with ‘FolderItem = SpecialFolder.Preferences.Child(PrefFileName)’ is OK on Mac?

I make a folder inside the Documents folder, that has all the settings and user data. That way the user can move the entire setup quite easily.

No. Preferences is reserved for system APIs now, you cannot assume you have write permission there.

SpecialFolder.ApplicationData.Child("my.bundle.identifier").Child("MySettingsFile")

The only places you can assume you have write access to are your bundle-identifier child of Application Support and Temporary. Everything else, including Documents, assume is off limits without an Open or Save dialog.

3 Likes

Or better: add an “export” MenuItem that copies your Preferences to where you want (and delete or not the original).

That has been a suggestion from Apple for ages, but I peacefully store my binary files there and avoid Apple’s dictation. In practice, it works.

This is a nightmare for anyone wanting to keep his/her documents folder clean (my own experience).
IIRC, this is said to be avoided by the Apple’s HIG.

Can you share a link of where you read this, please?


Arnaud NPro

You can add checkmarks (not checkboxes, which are ☐:ballot_box_with_check:) to menu items inside menus (you can’t add them to the title of the menus right inside the menu bar (well, you could add “:heavy_check_mark:” but you shouldn’t)).
Check the DesktopMenuItem.HasCheckMark property if this is what you want.

I’m having trouble adding a check mark to a menu item.I added a menu item to the File menu, then created a menu handler for it. The first line of the menu handler should toggle the check mark. I first tried me.HasCheckMark then DesktopMenuItem.HasCheckMark and every other reference I could think of, but nothing will compile.

I am developing with Windows and plan to have Windows and Mac versions of the program so the menu handler is in the app rather than a window.

You have to use its name in menuhandler. So if the name of the menuitem is ‘checkmark_test’ then create an new menuhandler ‘checkmark_test’ then add:


If checkmark_test.HasCheckMark = False Then
  checkmark_test.HasCheckMark = True
Else
  checkmark_test.HasCheckMark = False
End

Return True

As to why your attempts didn’t work:

The menu handler executes inside the app class (in your case). “me” refers to that app class, like everywhere inside “app”.

That’s the base class of any menu item. It doesn’t target any, since it’s the definition (class).
It’s like if you said DesktopWindow.Title=“Whatever”: which window would that be?

Also note that you can use the MenuBarSelected event to update your menu items. One benefit with that is you don’t have to duplicate your code (once in the menu handler, another time when the window just opens/the app starts, yet another time if the setting is changed from another mean).

That might be more a workaround than the expected behaviour (just watch out for this while your project progresses).