forked from MyriaCore/ulauncher-emoji
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
158 lines (137 loc) Β· 6.72 KB
/
main.py
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
import os
import logging
import sqlite3
from pprint import pprint
from ulauncher.api.client.Extension import Extension
from ulauncher.api.client.EventListener import EventListener
from ulauncher.api.shared.event import KeywordQueryEvent, ItemEnterEvent
from ulauncher.api.shared.item.ExtensionResultItem import ExtensionResultItem
from ulauncher.api.shared.action.RenderResultListAction import RenderResultListAction
from ulauncher.api.shared.action.CopyToClipboardAction import CopyToClipboardAction
from ulauncher.api.shared.action.DoNothingAction import DoNothingAction
from ulauncher.api.shared.action.ExtensionCustomAction import ExtensionCustomAction
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
from Xlib import X, display
from Xlib.ext import xtest
logger = logging.getLogger(__name__)
extension_icon = 'images/icon.png'
db_path = os.path.join(os.path.dirname(__file__), 'emoji.sqlite')
conn = sqlite3.connect(db_path, check_same_thread=False)
conn.row_factory = sqlite3.Row
xdisplay = display.Display()
def normalize_skin_tone(tone):
"""
Converts from the more visual skin tone preferences string to a more
machine-readable format.
"""
if tone == "π default": return ''
elif tone == "ππ» light": return 'light'
elif tone == "ππΌ medium-light": return 'medium-light'
elif tone == "ππ½ medium": return 'medium'
elif tone == "ππΎ medium-dark": return 'medium-dark'
elif tone == "ππΏ dark": return 'dark'
else: return None
class EmojiExtension(Extension):
def __init__(self):
super(EmojiExtension, self).__init__()
self.subscribe(KeywordQueryEvent, KeywordQueryEventListener())
self.subscribe(ItemEnterEvent, ItemEnterEventListener())
self.allowed_skin_tones = ["", "dark", "light", "medium", "medium-dark", "medium-light"]
class KeywordQueryEventListener(EventListener):
def on_event(self, event, extension):
icon_style = extension.preferences['emoji_style']
fallback_icon_style = extension.preferences['fallback_emoji_style']
search_term = event.get_argument().replace('%', '') if event.get_argument() else None
search_with_shortcodes = search_term and search_term.startswith(':')
# Add %'s to search term (since LIKE %?% doesn't work)
if search_term and search_with_shortcodes:
search_term = ''.join([search_term, '%'])
elif search_term:
search_term = ''.join(['%', search_term, '%'])
if search_with_shortcodes:
query = '''
SELECT em.name, em.code, em.keywords,
em.icon_apple, em.icon_twemoji, em.icon_noto, em.icon_blobmoji,
skt.icon_apple AS skt_icon_apple, skt.icon_twemoji AS skt_icon_twemoji,
skt.icon_noto AS skt_icon_noto, skt.icon_blobmoji AS skt_icon_blobmoji,
skt.code AS skt_code, sc.code as "shortcode"
FROM emoji AS em
LEFT JOIN skin_tone AS skt
ON skt.name = em.name AND tone = ?
LEFT JOIN shortcode AS sc
ON sc.name = em.name
WHERE sc.code LIKE ?
GROUP BY em.name
ORDER BY length(replace(sc.code, trim('{st}', '%'), ''))
LIMIT 8
'''.format(st=search_term)
else:
query = '''
SELECT em.name, em.code, em.keywords,
em.icon_apple, em.icon_twemoji, em.icon_noto, em.icon_blobmoji,
skt.icon_apple AS skt_icon_apple, skt.icon_twemoji AS skt_icon_twemoji,
skt.icon_noto AS skt_icon_noto, skt.icon_blobmoji AS skt_icon_blobmoji,
skt.code AS skt_code
FROM emoji AS em
LEFT JOIN skin_tone AS skt
ON skt.name = em.name AND tone = ?
WHERE em.name LIKE ?
LIMIT 8
'''
# Display blank prompt if user hasn't typed anything
if not search_term:
search_icon = 'images/%s/icon.png' % icon_style
return RenderResultListAction([
ExtensionResultItem(icon=search_icon,
name='Type in emoji name...',
on_enter=DoNothingAction())
])
skin_tone = normalize_skin_tone(extension.preferences['skin_tone'])
if skin_tone not in extension.allowed_skin_tones:
logger.warning('Unknown skin tone "%s"' % skin_tone)
skin_tone = ''
# Get list of results from sqlite DB
items = []
display_char = extension.preferences['display_char'] != 'no'
for row in conn.execute(query, [skin_tone, search_term]):
if row['skt_code']:
icon = row['skt_icon_%s' % icon_style]
icon = row['skt_icon_%s' % fallback_icon_style] if not icon else icon
code = row['skt_code']
else:
icon = row['icon_%s' % icon_style]
icon = row['icon_%s' % fallback_icon_style] if not icon else icon
code = row['code']
name = row['shortcode'] if search_with_shortcodes else row['name'].capitalize()
if display_char: name += ' | %s' % code
items.append(ExtensionResultItem(icon=icon, name=name,
on_enter=ExtensionCustomAction(code)))
return RenderResultListAction(items)
class ItemEnterEventListener(EventListener):
def paste(self):
self.perform_key_event("<Shift>Insert", True, 100)
self.perform_key_event("<Shift>Insert", False, 0)
def perform_key_event(self, accelerator, press, delay=0):
key, modifiers = Gtk.accelerator_parse(accelerator)
keycode = xdisplay.keysym_to_keycode(key)
event_type = X.KeyPress if press else X.KeyRelease
if keycode != 0:
if modifiers & Gdk.ModifierType.CONTROL_MASK:
modcode = xdisplay.keysym_to_keycode(Gdk.KEY_Control_L)
xtest.fake_input(xdisplay, event_type, modcode, delay)
if modifiers & Gdk.ModifierType.SHIFT_MASK:
modcode = xdisplay.keysym_to_keycode(Gdk.KEY_Shift_L)
xtest.fake_input(xdisplay, event_type, modcode, delay)
xtest.fake_input(xdisplay, event_type, keycode, delay)
xdisplay.sync()
def on_event(self, event, extension):
code = event.get_data()
action = extension.preferences['action']
copy_action = CopyToClipboardAction(code)
copy_action.run()
if action == 'Auto-Insert':
self.paste()
if __name__ == '__main__':
EmojiExtension().run()