summaryrefslogtreecommitdiff
path: root/translate-widget
diff options
context:
space:
mode:
Diffstat (limited to 'translate-widget')
-rw-r--r--translate-widget/translate.lua94
1 files changed, 46 insertions, 48 deletions
diff --git a/translate-widget/translate.lua b/translate-widget/translate.lua
index 0f1cbb3..bbcaefd 100644
--- a/translate-widget/translate.lua
+++ b/translate-widget/translate.lua
@@ -4,6 +4,7 @@ local awful = require("awful")
local json = require("json")
local https = require("ssl.https")
local wibox = require("wibox")
+local capi = {keygrabber = keygrabber }
local api_key = secrets.translate_widget_api_key
local base_url = 'https://translate.yandex.net/api/v1.5/tr.json/translate'
@@ -26,46 +27,42 @@ local function urlencode(str)
return str
end
-local translate_widget_txt = wibox.widget {
--- align = 'center',
- layout = wibox.layout.flex.vertical
+local w = wibox {
+ width = 300,
+ height = 80,
+ ontop = true,
+ screen = mouse.screen,
+ expand = true,
+ bg = '#1e252c',
+ max_widget_size = 500
}
-local lang_wdgt = wibox.widget{
- widget = wibox.widget.textbox,
--- align = 'center',
--- valign = 'center'
-}
-
-local to_translate_wdgt = wibox.widget{
- widget = wibox.widget.textbox,
--- align = 'center',
--- valign = 'center'
-}
-
-local translation_wdgt = wibox.widget{
- widget = wibox.widget.textbox,
--- align = 'center',
--- valign = 'center',
- wrap = 'word_char',
-}
-
-translate_widget_txt:add(lang_wdgt)
-translate_widget_txt:add(to_translate_wdgt)
-translate_widget_txt:add(translation_wdgt)
-
-local image = wibox.widget {
- image = '/usr/share/icons/Papirus-Dark/48x48/apps/gnome-translate.svg',
- resize = false,
- widget = wibox.widget.imagebox
-}
-local translate_widget = wibox.widget {
- image,
- translate_widget_txt,
+w:setup {
+ {
+ image = '/usr/share/icons/Papirus-Dark/48x48/apps/gnome-translate.svg',
+ resize = false,
+ widget = wibox.widget.imagebox
+ },
+ {
+ {
+ id = 'header',
+ widget = wibox.widget.textbox
+ },
+ {
+ id = 'src',
+ widget = wibox.widget.textbox
+ },
+ {
+ id = 'res',
+ widget = wibox.widget.textbox
+ },
+ id = 'text',
+ layout = wibox.layout.flex.vertical,
+ },
+ id = 'left',
layout = wibox.layout.fixed.horizontal
}
-
local function translate(request_string)
local to_translate, lang = extract(request_string)
local urll = base_url .. '?lang=' .. lang .. '&text=' .. urlencode(to_translate) .. '&key=' .. api_key
@@ -74,31 +71,32 @@ local function translate(request_string)
if (code == 200 and resp_json ~= nil) then
local resp = json.decode(resp_json).text[1]
- lang_wdgt:set_markup('<big>' .. lang.. '</big>')
- to_translate_wdgt:set_markup('<span color="#FFFFFF"> ' .. to_translate .. '</span>')
- translation_wdgt:set_markup('<span color="#FFFFFF"> ' .. resp .. '</span>')
+ w.left.text.header:set_markup('<big>' .. lang .. '</big>')
+ w.left.text.src:set_markup('<span color="#FFFFFF"> ' .. to_translate .. '</span>')
+ w.left.text.res:set_markup('<span color="#FFFFFF"> ' .. resp .. '</span>')
- local w = wibox {
- width = 300,
- height = 50,
- ontop = true,
- screen = mouse.screen,
- expand = true,
- widget = translate_widget
- }
awful.placement.top(w, { margins = {top = 25}})
w.visible = true
w:buttons(
awful.util.table.join(
- awful.button({}, 1, function() awful.spawn("echo left | xclip")
+ awful.button({}, 1, function()
+ awful.spawn.with_shell("echo '" .. resp .. "' | xclip -selection clipboard")
w.visible = false
end),
awful.button({}, 3, function()
- awful.spawn.with_shell("echo right | xclip")
+ awful.spawn.with_shell("echo '" .. to_translate .."' | xclip -selection clipboard")
w.visible = false
end)
)
)
+
+ capi.keygrabber.run(function(_, key, event)
+ if event == "release" then return end
+ if key then
+ capi.keygrabber.stop()
+ w.visible = false
+ end
+ end)
end
end