Skip to content

Commit 019226b

Browse files
authored
Merge pull request #386 from FireChickenProductivity/better-setting-example
Better setting example
2 parents dcf050c + 220a014 commit 019226b

1 file changed

Lines changed: 23 additions & 11 deletions

File tree

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,40 @@
11
# Settings
22

3-
Settings allow you to control some of the parameters of your python files by changing their value in a .talon file. This can be used to make a Talon user file set easier to customize for end users, such as exposing the background color of a UI element. It can also be useful to have certain settings change when the context changes, by setting them to different values in different .talon files.
3+
Settings allow you to control behavior of your Python code by editing a .talon file. This can be used to make a Talon user file set easier to customize for end users, such as exposing the background color of a UI element. It can also be useful to have certain settings change when the context changes, by setting them to different values in different .talon files.
44

5-
Settings are defined on Modules. Each setting has a name, type, default value, and description. The following example shows how to define a setting in python and get its contextually dependent value.
5+
Declare a setting on a Module by calling its`.setting()` method. Each setting has a name, type, default value, and description. A setting's value can be obtained from Python by calling `settings.get()` with the setting name as the argument.
6+
7+
The following example shows how to define a setting in Python and get its context-dependent value. If you are using Community and Python is the active programming language, you can use the command `snip module setting` to define a setting. Because settings are undefined during Talon startup, only obtain setting values in a function registered to be called by Talon (e.g., `ready` callback) or an action.
68

79
`setting.py`
810

911
```python
10-
from talon import Module, settings
12+
from talon import Module, settings, actions
1113

1214
mod = Module()
1315

1416
mod.setting(
15-
"my_user_file_set_horizontal_position",
17+
"my_prefix_sleep_amount",
1618
type=int,
17-
default=0,
18-
desc="Set the horizontal display position of some UI element",
19+
default=200,
20+
desc="Set the amount of time to sleep in milliseconds",
1921
)
2022

21-
value = settings.get("user.my_user_file_set_horizontal_position")
22-
print("The current value of the setting is " + value)
23+
@mod.action_class
24+
class Actions:
25+
def my_prefix_paste_file_to_next_window():
26+
"""Copy the text from the current file to the next window"""
27+
actions.edit.select_all()
28+
actions.edit.copy()
29+
actions.app.window_next()
30+
# Sleep to avoid pasting during the window switching process
31+
value = settings.get("user.my_prefix_sleep_amount")
32+
print(f"The current value of the setting is {value}")
33+
actions.sleep(f"{value}ms")
34+
actions.edit.paste()
2335
```
2436

25-
Note that the name of the setting (the first argument to mod.setting) in the example included the prefix "my_user_file_set". All user defined settings names share the same namespace so it's important to avoid overly generic setting names that may conflict.
37+
Note that the name of the setting (the first argument to `mod.setting`) in the example included the prefix "my_prefix". All user-defined settings names share the same namespace, so it's important to avoid overly generic setting names that may conflict.
2638

2739
The following example shows how you would change the value for that setting in a .talon file. Any number of settings can be defined in a single settings block, but any invalid syntax will prevent the entire block from applying.
2840

@@ -31,7 +43,7 @@ The following example shows how you would change the value for that setting in a
3143
```talon
3244
-
3345
settings():
34-
user.my_user_file_set_horizontal_position = 50
46+
user.my_prefix_sleep_amount = 50
3547
# Any number of other settings could be defined here
3648
```
3749

@@ -44,5 +56,5 @@ from talon import Context
4456

4557
ctx = Context()
4658

47-
ctx.settings["user.my_user_file_set_horizontal_position"] = 50
59+
ctx.settings["user.my_prefix_sleep_amount"] = 50
4860
```

0 commit comments

Comments
 (0)