Broadcast Preference Change

The To-Do application has two places in the UI that can change the value of the hide completed items preference. The first is the checkbox in the UI. The second is the Edit menu item. Let's look at a simple example that shows how Broadcasting can be used to keep the UI in sync when the preference value is changed by calling glxapp_setPref.

The framework uses the included Broadcasting API in order to send messages when preference values are changed. We will talk more about Broadcasting in Session III.

Add a Broadcast Message

Open the Framework plugin and select the Preferences pane (1). Select hide completed items (2). Click on the Add Message button (3).

Specify the Target

The first dialog wants you to enter a control reference. Type in

card 1 of stack "program"

and then press OK.

Specify Message To Send

The next dialog wants to know the name of the message to send when the preference value is updated. Enter PreferenceUpdated and click OK.

The plugin should now list the Broadcast message you added.

Save Your Work

Click on the Save Settings button to save your work to disk.

Edit the Card Script

Edit the script of the program stack's card by choosing Object > Card Script from the menu with the program stack frontmost.

Add New Handlers

Now that we have told the framework to broadcast a message to this card we need to add that handler. We are going to add the PreferenceUpdated handler and a helper command called uiSynchUIToPrefs.

Update Script

Add the following RevTalk code to the card script. I added it right after the ResizeView handler.

on PreferenceUpdated pPrefName

switch pPrefName

case "hide completed items"

## Redo search whenever pref is updated

uiSyncUItoPrefs

uiPopulateToDos

break

end switch

end PreferenceUpdated

command uiSyncUItoPrefs

set the hilite of button "HideCompleted" to \

glxapp_getPref("hide completed items")

end uiSyncUItoPrefs

When the hide completed items preference is updated the PreferenceUpdated message will be sent to the card (1). The 1st parameter tells us which preference was updated.

Whenever the hide completed items preference is updated we want to update the UI and update the list of to-dos in the main display (2). uiSynchUIToPrefs (3) takes care of the former by setting the hilite of the checkbox to the current value of the pref. uiPopulateToDos does the later and is already defined in the application.

Edit HideCompleted Checkbox Button Script

We need to make a change to the checkbox script. Originally it update the list of to-do items by calling uiPopulateToDos right before calling glxapp_setPref. Since calling glxapp_setPref will now trigger a broadcast we no longer want to do that. Remember that PreferenceUpdated in the card script will do that for us.

Comment out the uiPopulateToDos call.

Test Your Work

Check one of the to-do items in the list (1).

Then check the Hide Completed Items button. The list should update and remove the checked to-do list item.

Toggle Setting Using Edit Menu

Now open the Edit menu. Hide Completed Items should be checked. Choose it from the menu to toggle the setting. Remember that the menuPick handler for the Edit button calls glxapp_setPref so the PreferenceUpdated message will be sent to the card.

UI Updates

The UI should update to show the completed item in the to-do list (1) and uncheck the Hide Completed Items checkbox (2).

Add uiSyncUIToPrefs to PreOpenView

Now that we have a preference that affects the setting of the search filter in the UI we can remember the setting between sessions of the application. When the program window opens we should sync the UI to the preference value. Add a call to uiSyncUItoPrefs to the PreOpenView handler (1). This will only be triggered the first time the card opens during the session because it is wrapped in an if statement (2).

0 Comments

Add your comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.