Logo-small

4.2.5 Settings Declaration

All global preferences of Pharo can be viewed or changed from the Settings Browser. A preference is typically a class variable or an instance variable of a singleton. If one want to be able to change its value from the SettingsBrowser, then a setting must be declared for it.

For example, the following method declares the #caseSensitiveFinds setting. Notice that this method is declared class side, that it takes a builder as argument and that it is tagged with the pragma <systemsettings>.

CodeHolderSystemSettings class>>caseSensitiveFindsSettingsOn: aBuilder
<systemsettings>
(aBuilder setting: #caseSensitiveFinds)
label: 'Case sensitive search' translated;
target: ParagraphEditor;
description: 'If true, then the "find" command in text will always make its searches in a case-sensitive fashion' translated.

caseSensitiveFinds, the name of the setting, is also the selector which is used to get the value of the preference. The related method is defined in ParagraphEditor, the target of the setting declaration:

ParagraphEditor class>>caseSensitiveFinds
^ CaseSensitiveFinds ifNil: [CaseSensitiveFinds := false]

The caseSensitiveFinds setting declaration makes also the assumption that the preference value can be changed by sending #caseSensitiveFinds: to ParagraphEditor with the new value passed as argument:

ParagraphEditor class>>caseSensitiveFinds: aBoolean
CaseSensitiveFinds := aBoolean

Thanks to the <systemsettings> pragma, the settings browser is automatically updated when this method is compiled. The result is shown in the screenshot below.

Setting_declaration1

The widget shown for the setting depends on the type of the preference value. Here, a checkbox is used because the actual type of the preference is Boolean.

We can notice that the caseSensitiveFinds is badly placed at top level in the setting browser. In order to place it somewhere else, as an example under the Editing node, one just have to declare Editing as the parent node of the caseSensitiveFinds setting declaration:

CodeHolderSystemSettings class>>caseSensitiveFindsSettingsOn: aBuilder
<systemsettings>
(aBuilder setting: #caseSensitiveFinds)
label: 'Case sensitive search' translated;
parent: #Editing;
target: ParagraphEditor;
description: 'If true, then the "find" command in text will always make its searches in a case-sensitive fashion' translated.

The Editing node can be declared like this:

CodeHolderSystemSettings class>>editingSettingsOn: aBuilder
<systemsettings>
(aBuilder group: #Editing)
description: 'Code editing preferences' translated.

Setting_declaration2.png

It is also possible to declare a node and its children with a single method:

CodeHolderSystemSettings class>>editingSettingsOn: aBuilder
<systemsettings>
(aBuilder group: #Editing)
description: 'Code editing preferences' translated;
with: [(aBuilder setting: #caseSensitiveFinds)
label: 'Case sensitive search' translated;
target: ParagraphEditor;
description: 'If true, then the "find" command in text will always make its searches in a case-sensitive fashion' translated]

Add a Note

Licensed under Creative Commons BY-NC-SA | Published using Pier |