-
Notifications
You must be signed in to change notification settings - Fork 213
Implement triggers #1097
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement triggers #1097
Conversation
This was written before settling on the new terminology: My first attempt used the same event and method for both types of triggers, but I think that was worse, because the main argument for the input trigger is the input (aka query argument), which the other one doesn't have. I also think what we call them matters a lot for the documentation and how people think of them. The new type is far less important than the old keyword one, so I'm fine with it taking a back seat though. I think we should move this extension to the Ulauncher repos and also use it for the documentation since it's extremely simple, yet demonstrating a valid use case of multiple keyword arguments. import hashlib
from ulauncher.api import Extension, ExtensionResult
from ulauncher.api.shared.action.CopyToClipboardAction import CopyToClipboardAction
class Hash2(Extension):
def on_input(self, input_text, trigger_id):
# Show the algorithm specified as keyword, or all if the keyword was "hash"
algorithms = hashlib.algorithms_guaranteed if trigger_id == 'hash' else [trigger_id]
for algorithm in algorithms:
try:
seed = hashlib.new(algorithm)
seed.update(input_text.encode())
hash = seed.hexdigest()
yield ExtensionResult(
name=hash,
description=algorithm,
on_enter=CopyToClipboardAction(hash),
)
except:
pass
if __name__ == '__main__':
Hash2().run() |
ulauncher/api/extension.py
Outdated
self.logger.debug('No listeners for event %s', event_type.__name__) | ||
return | ||
if event_type == QueryChangeEvent and self._listeners[KeywordQueryEvent]: | ||
# convert QueryChangeEvent to KeywordQueryEvent for backwards compatibility |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good consideration 👍
Good idea! |
@brpaz Would be interested in hearing if you have thoughts on the API changes. You have written more code against it than anyone (including the original hash extension). The PR redefines preferences with the type "keyword" as "input triggers" and the query argument to "input". And also adds a new type of trigger "launch trigger" which is a simpler type of trigger that doesn't take user input but "launches" (calls the extension listener) directly. The idea is that this will enable future changes so the other type of "keywordless" (like our calculator) will be possible also down the road. But it's more complex and won't be part of v6.0.0. |
@friday hey. just saw this now. I think it looks good. I have been using more and more this pattern of having multiple keywords/triggers in an extension, inside of having a single entry point and then parsing the entire input text, to know which sub-action to execute. I think this "trigger" approach simplifies a lot this process. The new |
Great :) Separating preferences and triggers was definitely the right move. The differ quite a lot (one having different properties and uses). Triggers custom keywords are not a concern the extension, and removing it completely would be best (which is almost the case with this PR, but they will still be in |
Implements #1074
New terminology:
on_input
) for the established trigger type (previously referred to as "keywords"). They are for getting user input after all.on_launch
) for the new type of triggers that trigger instantly when you activate them. They do work similar to apps (which are "launched"). "launch" isn't 100% spot on, since the action might not actually be to launch something. It could be to refresh/reload a file for example (like this: feature suggestion: clear list of frequently used apps #338). But it's the best I could think of which works well for the method name too.Can be tested with this extension (v6 should support the gnome settings panels by default on most systems, so if this "works" you get duplicates):
It's as simple as this:
manifest.json:
main.py:
Hence, the
on_launch
method is only for the new trigger type. The old trigger type (now called "input triggers") need a "keyword" property in the manifest and then you will be able to use this method:input_text
is a string with the user input after the keyword. So if the user enters "find pdf" you only get "pdf", but you get the trigger_id corresponding to "find" as the second argument. This is better since the actual keyword can be customized by the user and hence can't be used safely to determine which trigger. It's also easier for people who want typing information to not have to import ourQuery
class.The documentation still doesn't include
on_launch
yet, but it was always kind of basic and doesn't include some of the other ones either.Checklist
./ul test
is passing (the CI server will check this if you don't)