Adobe 65007312 Programming Guide - Page 180

Adding a data observer, Set up the dialog and table

Page 180 highlights

CHAPTER 9: Getting Started: A Tutorial Example Adding a data observer 180 Adding a data observer The most flexible way of connecting to your data is to create an observer for the property table; this is an independent object that is notified of changes in the table, and can take any action in response to the change, including setting UI values in any way you want. This example demonstrates how to set up an observer that is notified when a data value changes, and how to define a function that responds to that notification by setting UI values. Set up the dialog and table 1. Edit the LibraryMenuItem.lua file to create a new function, showCustomDialogWithObserver(): function MyHWLibraryItem.showCustomDialogWithObserver() -- body of function end 2. Within this function, make the function-context call you need for the property table: LrFunctionContext.callWithContext( "showCustomDialogWithObserver", function( context ) -- body of function end ) 3. In this context, create the observable table, and add a property named myObservedString, with an initial value: -- body of function local props = LrBinding.makePropertyTable( context ) props.myObservedString = "This is a string" -- new prop with initial value 4. Obtain a view factory and use it to create a static text field, which initially displays the static value of the property. (We will put it into the view hierarchy later.) local f = LrView.osFactory() -- obtain the view factory object local showValue_st = f:static_text { -- create the label control title = props.myObservedString,-- set text to the static property value text_color = LrColor( 1, 0, 0 ) -- set color to a new color object } The title, which is the displayed text, is assigned to be the current value of the property we defined in the data table, props.myObservedString. This is not a dynamic binding, just an assignment to the current value. So far, if the property value changes, it will not change the text in the control. 5. Create an edit box (which we will also add to the view hierarchy later). Notice that this box updates its value with every keystroke: local updateField = f:edit_field { -- create an edit box value = "Enter some text", -- initial text, not bound to data immediate = true -- update value with every keystroke }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204

C
HAPTER
9: Getting Started: A Tutorial Example
Adding a data observer
180
Adding a data observer
The most flexible way of connecting to your data is to create an
observer
for the property table; this is an
independent object that is notified of changes in the table, and can take any action in response to the
change, including setting UI values in any way you want.
This example demonstrates how to set up an observer that is notified when a data value changes, and how
to define a function that responds to that notification by setting UI values.
Set up the dialog and table
1.
Edit the
LibraryMenuItem.lua
file to create a new function,
showCustomDialogWithObserver()
:
function MyHWLibraryItem.showCustomDialogWithObserver()
-- body of function
end
2.
Within this function, make the function-context call you need for the property table:
LrFunctionContext.callWithContext( "showCustomDialogWithObserver",
function( context )
-- body of function
end )
3.
In this context, create the observable table, and add a property named
myObservedString
, with an
initial value:
-- body of function
local props = LrBinding.makePropertyTable( context )
props.myObservedString = "This is a string" -- new prop with initial value
4.
Obtain a view factory and use it to create a static text field, which initially displays the static value of
the property. (We will put it into the view hierarchy later.)
local f = LrView.osFactory() -- obtain the view factory object
local showValue_st = f:static_text { -- create the label control
title = props.myObservedString,-- set text to the static property value
text_color = LrColor( 1, 0, 0 ) -- set color to a new color object
}
The
title
, which is the displayed text, is assigned to be the current value of the property we defined
in the data table,
props.myObservedString
. This is not a dynamic binding, just an assignment to the
current value. So far, if the property value changes, it will not change the text in the control.
5.
Create an edit box (which we will also add to the view hierarchy later). Notice that this box updates its
value
with every keystroke:
local updateField = f:edit_field { -- create an edit box
value = "Enter some text", -- initial text, not bound to data
immediate = true -- update value with every keystroke
}