Skip to content

Commit

Permalink
Simplify shortcut add/edit logic
Browse files Browse the repository at this point in the history
Set added and id from JS for new shortcuts also, to simplify the flow
Skip/remove the decimals in the date
Replace the id with shorter ids without dashes and not base16
Use fixed names instead of unique ids for autogenerated shortcuts
Don't return the id from the server route
  • Loading branch information
friday committed Sep 25, 2022
1 parent 46ad331 commit fced1e2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 21 deletions.
8 changes: 3 additions & 5 deletions preferences-src/src/components/pages/EditShortcut.vue
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,16 @@ export default {
return
}
let shortcut = {
const shortcut = {
added: Number(this.added) || Math.floor(new Date().getTime() / 1000),
id: this.id || Array(20).fill().map(n=>(Math.random()*36|0).toString(36)).join(''),
icon: this.localIcon || '',
name: this.localName,
keyword: this.localKeyword.trim(),
cmd: this.localCmd,
is_default_search: this.localIsDefaultSearch,
run_without_argument: this.localRunWithoutArgument
}
if (this.id) { // Editing existing shortcut
shortcut.id = this.id;
shortcut.added = this.added;
}
fetchData('prefs:///shortcut/update', shortcut).then(this.hide, err => bus.$emit('error', err))
},
hide() {
Expand Down
20 changes: 7 additions & 13 deletions ulauncher/modes/shortcuts/ShortcutsDb.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from uuid import uuid4
from time import time
from pathlib import Path
from ulauncher.config import PATHS
Expand All @@ -17,12 +16,6 @@ class Shortcut(JsonData):
added = 0.0
id = ""

def __init__(self, *args, **kwargs):
super().__init__() # Sets class defaults
self.added = time()
self.id = str(uuid4())
self.update(*args, **kwargs)


class ShortcutsDb(JsonData):
# Coerce all values to Shortcuts instead of dict and fold the icon path
Expand All @@ -31,31 +24,32 @@ def __setitem__(self, key, value):
value.icon = fold_user_path(value.icon)
super().__setitem__(key, Shortcut(value))

def add(self, shortcut):
shortcut = Shortcut(shortcut)
self[shortcut.id] = shortcut

return shortcut.id

@classmethod
def load(cls):
file_path = Path(f"{PATHS.CONFIG}/shortcuts.json")
instance = cls.new_from_file(file_path)
if not file_path.exists():
added = int(time())
keywords = [
Shortcut(
id="googlesearch",
added=added,
keyword="g",
name="Google Search",
cmd="https://google.com/search?q=%s",
icon=f"{PATHS.ASSETS}/icons/google-search.png"
),
Shortcut(
id="stackoverflow",
added=added,
keyword="so",
name="Stack Overflow",
cmd="https://stackoverflow.com/search?q=%s",
icon=f"{PATHS.ASSETS}/icons/stackoverflow.svg"
),
Shortcut(
id="wikipedia",
added=added,
keyword="wiki",
name="Wikipedia",
cmd="https://en.wikipedia.org/wiki/%s",
Expand Down
4 changes: 1 addition & 3 deletions ulauncher/ui/preferences_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,11 @@ def shortcut_get_all(self):
return list(ShortcutsDb.load().values())

@route('/shortcut/update')
@route('/shortcut/add')
def shortcut_update(self, shortcut):
logger.info('Add/Update shortcut: %s', json.dumps(shortcut))
shortcuts = ShortcutsDb.load()
id = shortcuts.add(shortcut)
shortcuts[shortcut["id"]] = shortcut
shortcuts.save()
return {'id': id}

@route('/shortcut/remove')
def shortcut_remove(self, shortcut_id):
Expand Down

0 comments on commit fced1e2

Please sign in to comment.