/
InspectorProperty.lua
526 lines (472 loc) · 20.7 KB
/
InspectorProperty.lua
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
--- === cp.apple.finalcutpro.inspector.InspectorProperty ===
---
--- `InspectorProperty` contains helper functions for handling common property
--- types that occur in various `Inspectors` in FCP.
---
--- In addition to specific property row types like `textField`, `slider`, etc.,
--- there is also a `section`, which is for rows which expand/collapse to reveal
--- other properties.
local require = require
--local log = require "hs.logger".new "InspectorProperty"
local is = require "cp.is"
local prop = require "cp.prop"
local tools = require "cp.tools"
local axutils = require "cp.ui.axutils"
local Button = require "cp.ui.Button"
local CheckBox = require "cp.ui.CheckBox"
local MenuButton = require "cp.ui.MenuButton"
local PopUpButton = require "cp.ui.PopUpButton"
local PropertyRow = require "cp.ui.PropertyRow"
local StaticText = require "cp.ui.StaticText"
local TextField = require "cp.ui.TextField"
local Do = require "cp.rx.go.Do"
local childFromLeft = axutils.childFromLeft
local childFromRight = axutils.childFromRight
local childrenMatching = axutils.childrenMatching
local toRegionalNumber = tools.toRegionalNumber
local toRegionalNumberString = tools.toRegionalNumberString
local mod = {}
--- cp.apple.finalcutpro.inspector.InspectorProperty.hasProperties(parent, uiFinder) -> boolean
--- Function
--- This will prepare the `parent` to handle containing `PropertyRow` children, and returns a function which can pass in a table of properties to bind to the parent.
---
--- Parameters:
--- * parent - The parent table.
--- * uiFinder - The function or cp.prop which will be called to find the parent UI element. Functions will be passed the `parent` when being executed.
---
--- Returns:
--- * boolean
---
--- Notes:
--- * For example:
---
--- ```lua
--- local o = {
--- propertiesUI = ...,
--- }
--- InspectorProperty.hasProperties(o, o.propertiesUI) {
--- propOne = InspectorProperty.textField "FFPropOne",
--- sectionOne = InspectorProperty.section "FFSectionOne" {
--- sliderOne = InspectorProperty.slider "FFSliderOne",
--- },
--- }
--- ```
function mod.hasProperties(parent, uiFinder)
PropertyRow.prepareParent(parent, uiFinder)
return function(props)
prop.bind(parent)(props)
end
end
local function propShow(self)
local parent = self:parent()
parent:show()
self.section:expanded(true)
return self
end
local function propHide(self)
self.section:expanded(false)
return self
end
local function propDoShow(self)
return Do(self:parent():doShow())
:Then(function()
self.section:expanded(true)
end)
:ThenYield()
:Label("cp.apple.finalcutpro.inspector.PropertyRow:doShow()")
end
local function propDoHide(self)
return Do(function()
self.section:expanded(false)
end)
:ThenYield()
:Label("cp.apple.finalcutpro.inspector.PropertyRow:doHide()")
end
--- cp.apple.finalcutpro.inspector.InspectorProperty.section(labelKey[, index]) -> function
--- Function
--- Returns a 'section row' factory function that can be called to create a section row that contains other `PropertyRow' `cp.prop`s.
---
--- Parameters:
--- * labelKey - The I18N lookup key to find the row with.
--- * index - (optional) The occurrence of the key value in the parent. Sometimes multiple rows have the same title. Defaults to `1`.
---
--- Returns:
--- * A function which will create the section row when called.
---
--- Notes:
--- * This does *not* return an actual `cp.prop`. Rather, it returns a 'factory' function that will help configure the sub-properties of of the section. This can be used as follows:
---
--- ```lua
--- local o = {}
--- prop.bind(o) {
--- sectionOne = InspectorProperty.section "FFHeaderOneKey" { -- has sub-properties inside the `{}`
--- subRowOne = InspectorProperty.textField "FFSubRowOneKey",
--- subRowTwo = InspectorProperty.slider "FFSubRowTwoKey",
--- },
--- sectionTwo = InspectorProperty.section "FFHeaderTwoKey" {} -- no sub-properties, still needs `{}`
--- }
---
--- -- access subRowOne
--- local value = o:sectionOne():subRowOne()
--- ```
---
--- * The `o.sectionOne` property will be a `cp.prop` with the following built-in additional properties:
--- ** `enabled` - a `cp.ui.CheckBox` which reports if the section row is enabled.
--- ** `toggle` - a `cp.ui.Button` which will toggle the show/hide button (if present)
--- ** `reset` - a `cp.ui.Button` which will reset the sub-property values, if present in the UI.
--- ** `expanded` - a `cp.prop` which reports if the section is currently expanded.
function mod.section(labelKey, index)
return function(subProps)
local extendFn = nil
local result = prop(function(owner)
local row = PropertyRow(owner, labelKey, index)
-- sections are also parents of other PropertyRows.
PropertyRow.prepareParent(row, row.propertiesUI:mutate(function(original)
local propsUI = original()
local rowUI = row:UI()
if propsUI and rowUI then
local frame = rowUI:attributeValue("AXFrame")
if frame then
local rowPos = frame.y + frame.h
return childrenMatching(propsUI, function(child)
local childFrame = child:attributeValue("AXFrame")
return childFrame ~= nil and childFrame.y >= rowPos - PropertyRow.intersectBuffer
end)
end
end
return nil
end))
row.enabled = CheckBox(row, function() return childFromLeft(row:children(), 1, CheckBox.matches) end)
row.toggle = CheckBox(row, function() return childFromLeft(row:children(), 3, CheckBox.matches) end)
row.reset = Button(row, function() return childFromRight(row:children(), 1, Button.matches) end)
row.expanded = prop(
function(theRow)
--------------------------------------------------------------------------------
-- NOTE: The below works, but is completely impractical.
--------------------------------------------------------------------------------
--[[
local result = false
local resetButton = theRow.reset
local resetButtonUI = resetButton and resetButton:UI()
local resetButtonFrame = resetButtonUI and resetButtonUI:attributeValue("AXFrame")
if resetButtonFrame then
resetButtonFrame.x = resetButtonFrame.x - resetButtonFrame.w
local center = geometry(resetButtonFrame).center
tools.ninjaMouseAction(center, function()
wait(0.1)
local element = ax.systemElementAtPosition(mouse.absolutePosition())
if element and element:attributeValue("AXRole") == "AXCheckBox" then
local iHide = theRow:app():string("FFInspectorHeaderControllerButtonHide")
result = element:title() == iHide
end
end)
end
return result
--]]
--------------------------------------------------------------------------------
-- NOTE: The below works for main rows (i.e. "3D Text"),
-- but not sub-rows (i.e. "Self Shadows").
--------------------------------------------------------------------------------
--[[
local theRowUI = theRow and theRow:UI()
local theRowFrame = theRowUI and theRowUI:attributeValue("AXFrame")
local nextRow = axutils.childrenInNextLine(theRowUI)
local nextRowStaticText = nextRow and axutils.childFromLeft(nextRow, 1, StaticText.matches)
local nextRowStaticTextFrame = nextRowStaticText and nextRowStaticText:attributeValue("AXFrame")
if theRowFrame and nextRowStaticTextFrame and (theRowFrame.y + theRowFrame.h) > (nextRowStaticTextFrame.y - 5) then
return true
else
return false
end
--]]
local iHide = theRow:app():string("FFInspectorHeaderControllerButtonHide")
local toggleUI = theRow.toggle:UI()
return toggleUI and toggleUI:attributeValue("AXTitle") == iHide
end,
function(newValue, theRow, theProp)
local currentValue = theProp:get()
--------------------------------------------------------------------------------
-- NOTE: The below works, but is completely impractical until we have a
-- solution for "getting".
--------------------------------------------------------------------------------
--[[
local resetButton = theRow.reset
local resetButtonUI = resetButton and resetButton:UI()
local resetButtonFrame = resetButtonUI and resetButtonUI:attributeValue("AXFrame")
if resetButtonFrame and newValue ~= currentValue then
resetButtonFrame.x = resetButtonFrame.x - resetButtonFrame.w
local center = geometry(resetButtonFrame).center
ninjaMouseClick(center)
end
--]]
if newValue ~= currentValue then
theRow.toggle:press()
end
end
):bind(row)
if subProps then
prop.bind(row)(subProps)
-- hijack the 'show/hide' functions
for _,p in pairs(subProps) do
local subRow = p()
subRow.section = row
subRow.show = propShow
subRow.hide = propHide
subRow.doShow = propDoShow
subRow.doHide = propDoHide
end
end
if extendFn then
row:extend(extendFn)
end
return row
end):cached()
-- add access to the `PropertyRow:extend()` function
function result:extend(fn)
extendFn = fn
if self:owner() then -- already bound...
self:get():extend(extendFn)
end
return self
end
return result
end
end
--- cp.apple.finalcutpro.inspector.InspectorProperty.simple(labelKey[, prepareFn][, index]]) -> cp.prop <cp.ui.PropertyRow; read-only>
--- Function
--- Creates a new `cp.prop` that contains a `PropertyRow` matching the `labelKey`.
---
--- Parameters:
--- * labelKey - The I18N key that the row lable matches.
--- * prepareFn - The function to call to perform additional preparations on the row.
--- * index - The instance number of that label (defaults to `1`).
---
--- Returns:
--- * The `cp.prop` that returns the `PropertyRow`.
---
--- Notes:
--- * It has no additional properties, but it does allow a `prepareFn` to be provided, which will be called after the `PropertyRow` is created, and passed the new `PropertyRow` as the first argument.
local function simple(labelKey, prepareFn, index)
if is.number(prepareFn) then
index = prepareFn
prepareFn = nil
end
local extendFn = nil
local result = prop(function(owner)
local row = PropertyRow(owner, labelKey, index)
row.reset = Button(row, function() return childFromRight(row:children(), 1) end)
row.keyframe = Button(row, function() return childFromRight(row:children(), 2, Button.matches) end)
function row.keyframe:addKeyframe()
local ui = self:UI()
if ui then
local f = ui:attributeValue("AXFrame")
if f then
local point = {
x = f.x + f.w - 5,
y = f.y + (f.h / 2)
}
tools.ninjaMouseClick(point)
end
end
end
if prepareFn then
prepareFn(row)
end
if extendFn then
row:extend(extendFn)
end
return row
end):cached()
-- add access to the `PropertyRow:extend()` function
function result:extend(fn)
extendFn = fn
if self:owner() then -- already bound...
self:get():extend(extendFn)
end
return self
end
return result
end
mod.simple = simple
--- cp.apple.finalcutpro.inspector.InspectorProperty.textField(labelKey[, index]) -> cp.prop <cp.ui.PropertyRow; read-only>
--- Function
--- Creates a new `cp.prop` that contains a `PropertyRow` matching the `labelKey`.
---
--- Parameters:
--- * labelKey - The I18N key that the row lable matches.
--- * index - The instance number of that label (defaults to `1`).
---
--- Returns:
--- * The `cp.prop` that returns the `PropertyRow`.
---
--- Notes:
--- * It has one additional property:
--- ** `value` - A `cp.ui.TextField` which contains the text value.
function mod.textField(labelKey, index)
return simple(labelKey, function(row)
row.value = TextField(row, function() return childFromRight(row:children(), 1, TextField.matches) end)
end, index)
end
--- cp.apple.finalcutpro.inspector.InspectorProperty.numberField(labelKey[, index]) -> cp.prop <cp.ui.PropertyRow; read-only>
--- Function
--- Creates a new `cp.prop` that contains a `PropertyRow` matching the `labelKey`.
---
--- Parameters:
--- * labelKey - The I18N key that the row lable matches.
--- * index - The instance number of that label (defaults to `1`).
---
--- Returns:
--- * The `cp.prop` that returns the `PropertyRow`.
---
--- Notes:
--- * It has one additional property:
--- ** `value` - A `cp.ui.TextField` which contains the number value.
function mod.numberField(labelKey, index)
return simple(labelKey, function(row)
row.value = TextField(row, function() return childFromRight(row:children(), 1, TextField.matches) end, toRegionalNumber, toRegionalNumberString)
end, index)
end
--- cp.apple.finalcutpro.inspector.InspectorProperty.staticText(labelKey[, index]) -> cp.prop <cp.ui.PropertyRow; read-only>
--- Function
--- Creates a new `cp.prop` that contains a `PropertyRow` matching the `labelKey`.
---
--- Parameters:
--- * labelKey - The I18N key that the row lable matches.
--- * index - The instance number of that label (defaults to `1`).
---
--- Returns:
--- * The `cp.prop` that returns the `PropertyRow`.
---
--- Notes:
--- * It has one additional property:
--- ** `value` - A `cp.ui.StaticText` which contains the text value.
function mod.staticText(labelKey, index)
return simple(labelKey, function(row)
row.value = StaticText(row, function() return childFromRight(row:children(), 1, StaticText.matches) end)
end, index)
end
--- cp.apple.finalcutpro.inspector.InspectorProperty.xy(labelKey[, index]) -> cp.prop <cp.ui.PropertyRow; read-only>
--- Function
--- Creates a new `cp.prop` that contains a `PropertyRow` matching the `labelKey`.
---
--- Parameters:
--- * labelKey - The I18N key that the row lable matches.
--- * index - The instance number of that label (defaults to `1`).
---
--- Returns:
--- * The `cp.prop` that returns the `PropertyRow`.
---
--- Notes:
--- * It has two additional properties:
--- ** `x` - A `cp.ui.TextField` containing the 'X' value.
--- ** `y` - A `cp.ui.TextField` containing the `Y` value.
function mod.xy(labelKey, index)
return mod.simple(labelKey, function(row)
row.x = TextField(row, function() return childFromLeft(row:children(), 1, TextField.matches) end, toRegionalNumber, toRegionalNumberString)
row.y = TextField(row, function() return childFromLeft(row:children(), 2, TextField.matches) end, toRegionalNumber, toRegionalNumberString)
end, index)
end
--- cp.apple.finalcutpro.inspector.InspectorProperty.slider(labelKey[, index]) -> cp.prop <cp.ui.PropertyRow; read-only>
--- Function
--- Creates a new `cp.prop` that contains a `PropertyRow` matching the `labelKey`.
---
--- Parameters:
--- * labelKey - The I18N key that the row lable matches.
--- * index - The instance number of that label (defaults to `1`).
---
--- Returns:
--- * The `cp.prop` that returns the `PropertyRow`.
---
--- Notes:
--- * It has one additional property:
--- ** `value` - A `cp.ui.TextField` which contains the value of the slider.
function mod.slider(labelKey, index)
return mod.simple(labelKey, function(row)
row.value = TextField(row, function() return childFromRight(row:children(), 1, TextField.matches) end, toRegionalNumber, toRegionalNumberString)
end, index)
end
--- cp.apple.finalcutpro.inspector.InspectorProperty.menuButton(labelKey[, index]) -> cp.prop <cp.ui.PropertyRow; read-only>
--- Function
--- Creates a new `cp.prop` that contains a `PropertyRow` matching the `labelKey`.
---
--- Parameters:
--- * labelKey - The I18N key that the row lable matches.
--- * index - The instance number of that label (defaults to `1`).
---
--- Returns:
--- * The `cp.prop` that returns the `PropertyRow`.
---
--- Notes:
--- * It has one additional property:
--- ** `value` - A `cp.ui.MenuButton` which contains the text value.
function mod.menuButton(labelKey, index)
return mod.simple(labelKey, function(row)
row.value = MenuButton(row, function() return childFromRight(row:children(), 1, MenuButton.matches) end)
end, index)
end
--- cp.apple.finalcutpro.inspector.InspectorProperty.button(labelKey[, index]) -> cp.prop <cp.ui.PropertyRow; read-only>
--- Function
--- Creates a new `cp.prop` that contains a `PropertyRow` matching the `labelKey`.
---
--- Parameters:
--- * labelKey - The I18N key that the row lable matches.
--- * index - The instance number of that label (defaults to `1`).
---
--- Returns:
--- * The `cp.prop` that returns the `PropertyRow`.
---
--- Notes:
--- * It has one additional property:
--- ** `button` - A `cp.ui.Button`.
function mod.button(labelKey, index)
return mod.simple(labelKey, function(row)
row.button = Button(row, function() return childFromRight(row:children(), 1, Button.matches) end)
end, index)
end
--- cp.apple.finalcutpro.inspector.InspectorProperty.popUpButton(labelKey[, index]) -> cp.prop <cp.ui.PropertyRow; read-only>
--- Function
--- Creates a new `cp.prop` that contains a `PropertyRow` matching the `labelKey`.
---
--- Parameters:
--- * labelKey - The I18N key that the row lable matches.
--- * index - The instance number of that label (defaults to `1`).
---
--- Returns:
--- * The `cp.prop` that returns the `PropertyRow`.
---
--- Notes:
--- * It has one additional property:
--- ** `value` - A `cp.ui.PopUpButton` which contains the text value.
function mod.popUpButton(labelKey, index)
return mod.simple(labelKey, function(row)
row.value = PopUpButton(row, function() return childFromRight(row:children(), 1, PopUpButton.matches) end)
function row:doSelectValue(value, overrideValue)
return Do(self:doShow())
:Then(self.value:doSelectValue(value, overrideValue))
end
-- returns the PopUpButton.value prop as the observable
function row:toObservable()
return self.value.value:toObservable()
end
end, index)
end
--- cp.apple.finalcutpro.inspector.InspectorProperty.checkBox(labelKey[, index]) -> cp.prop <cp.ui.PropertyRow; read-only>
--- Function
--- Creates a new `cp.prop` that contains a `PropertyRow` matching the `labelKey`.
---
--- Parameters:
--- * labelKey - The I18N key that the row lable matches.
--- * index - The instance number of that label (defaults to `1`).
---
--- Returns:
--- * The `cp.prop` that returns the `PropertyRow`.
---
--- Notes:
--- * It has one additional property:
--- ** `value` - A `cp.ui.CheckBox` which contains the boolean value for the row.
function mod.checkBox(labelKey, index)
return mod.simple(labelKey, function(row)
row.value = CheckBox(row, function() return childFromLeft(row:children(), 1, CheckBox.matches) end)
end, index)
end
return mod