Started migrating to module architecture
This commit is contained in:
12
modules/ibus/init.lua
Normal file
12
modules/ibus/init.lua
Normal file
@ -0,0 +1,12 @@
|
||||
local util = require("modules.ibus.util")
|
||||
|
||||
return {
|
||||
widgets = {
|
||||
ibus = require("modules.ibus.widget").widget
|
||||
},
|
||||
keybinds = require("modules.ibus.keybinds"),
|
||||
|
||||
init = function()
|
||||
util.set(1)
|
||||
end
|
||||
}
|
46
modules/ibus/keybinds.lua
Normal file
46
modules/ibus/keybinds.lua
Normal file
@ -0,0 +1,46 @@
|
||||
local util = req_rel(..., "util")
|
||||
local popup = req_rel(..., "popup")
|
||||
local widget = req_rel(..., "widget")
|
||||
|
||||
return gears.table.join(
|
||||
awful.key( {"Mod4"}, "space",
|
||||
function()
|
||||
util.next(function()
|
||||
popup.update()
|
||||
widget.update()
|
||||
end)
|
||||
end,
|
||||
{
|
||||
description = "Change input language",
|
||||
group = "System"
|
||||
}
|
||||
),
|
||||
|
||||
|
||||
awful.key( {"Mod4", "Shift", "Control"}, "9",
|
||||
function()
|
||||
util.set(2, function()
|
||||
popup.update()
|
||||
widget.update()
|
||||
end)
|
||||
end,
|
||||
{
|
||||
description = "Set input language to RU",
|
||||
group = "System"
|
||||
}
|
||||
),
|
||||
|
||||
|
||||
awful.key( {"Mod4", "Shift", "Control"}, "0",
|
||||
function()
|
||||
util.set(1, function()
|
||||
popup.update()
|
||||
widget.update()
|
||||
end)
|
||||
end,
|
||||
{
|
||||
description = "Set input language to EN",
|
||||
group = "System"
|
||||
}
|
||||
)
|
||||
)
|
111
modules/ibus/popup.lua
Normal file
111
modules/ibus/popup.lua
Normal file
@ -0,0 +1,111 @@
|
||||
local popup = {}
|
||||
local util = req_rel(..., "util")
|
||||
|
||||
|
||||
popup.widget = wibox.widget {
|
||||
homogeneous = false,
|
||||
vertical_homogeneous = true,
|
||||
horizontal_homogeneous = false,
|
||||
vertical_spacing = beautiful.dpi(10),
|
||||
horizontal_spacing = beautiful.dpi(0),
|
||||
min_cols_size = beautiful.dpi(20),
|
||||
min_rows_size = beautiful.dpi(20),
|
||||
layout = wibox.layout.grid
|
||||
}
|
||||
|
||||
|
||||
for k, l in pairs(conf.ibus_language_list) do
|
||||
|
||||
l["widget_checkbox"] = wibox.widget {
|
||||
checked = false,
|
||||
border_width = beautiful.dpi(3),
|
||||
paddings = beautiful.dpi(4),
|
||||
margins = beautiful.dpi(5),
|
||||
color = beautiful.color.bar.active,
|
||||
border_color = beautiful.color.bar.inactive,
|
||||
widget = wibox.widget.checkbox,
|
||||
forced_height = beautiful.dpi(30),
|
||||
forced_width = beautiful.dpi(30),
|
||||
shape = gears.shape.circle,
|
||||
}
|
||||
|
||||
l["widget_text"] = wibox.widget {
|
||||
{
|
||||
markup = "<b>" .. l["title"] .. "</b>",
|
||||
align = "left",
|
||||
valign = "center",
|
||||
font = "Comfortaa 16",
|
||||
widget = wibox.widget.textbox
|
||||
},
|
||||
layout = wibox.container.background,
|
||||
}
|
||||
|
||||
l["widget_text"].bg = "#00000000"
|
||||
|
||||
popup.widget:add_widget_at(l["widget_text"], k, 1, 1, 1)
|
||||
--popup.widget:add_widget_at(l["widget_checkbox"], k, 1, 1, 1)
|
||||
--popup.widget:add_widget_at(l["widget_text"], k, 2, 0, 1)
|
||||
end
|
||||
|
||||
popup.update = function()
|
||||
-- Update checkmarks
|
||||
for _, l in pairs(conf.ibus_language_list) do
|
||||
if (util.current_engine == l["ibus_engine"]) then
|
||||
l["widget_text"].bg = "#FF0000"
|
||||
else
|
||||
l["widget_text"].bg = "#00000000"
|
||||
end
|
||||
l["widget_checkbox"].checked = (util.current_engine == l["ibus_engine"])
|
||||
end
|
||||
|
||||
-- Show popup
|
||||
popup.popup.screen = awful.screen.focused()
|
||||
popup.popup.visible = true
|
||||
popup.popup_timer:again()
|
||||
end
|
||||
|
||||
|
||||
popup.popup = awful.popup {
|
||||
widget = {
|
||||
{
|
||||
popup.widget,
|
||||
margins = 10,
|
||||
widget = wibox.container.margin
|
||||
},
|
||||
bg = "#000000",
|
||||
opacity = 1,
|
||||
widget = wibox.container.background
|
||||
},
|
||||
border_color = "#000000",
|
||||
border_width = 0,
|
||||
opacity = 1,
|
||||
|
||||
type = "menu",
|
||||
ontop = true,
|
||||
visible = false,
|
||||
hide_on_right_click = true,
|
||||
|
||||
shape = gears.shape.rectangle,
|
||||
placement = function(d)
|
||||
return awful.placement.bottom_right(
|
||||
d,
|
||||
{
|
||||
honor_workarea = true
|
||||
}
|
||||
)
|
||||
end,
|
||||
}
|
||||
|
||||
popup.popup_timer = gears.timer {
|
||||
timeout = 1,
|
||||
autostart = false,
|
||||
call_now = false,
|
||||
single_shot = true,
|
||||
|
||||
callback = function()
|
||||
popup.popup.visible = false
|
||||
end
|
||||
}
|
||||
|
||||
|
||||
return popup
|
94
modules/ibus/util.lua
Normal file
94
modules/ibus/util.lua
Normal file
@ -0,0 +1,94 @@
|
||||
local util = {}
|
||||
|
||||
util.current_engine = ""
|
||||
util.current_engine_index = nil
|
||||
util.current_xkbmap = ""
|
||||
|
||||
|
||||
-- Update current_engine and current_engine index,
|
||||
-- call callback() when done.
|
||||
util.get = function(callback)
|
||||
-- Get current ibus engine
|
||||
awful.spawn.easy_async(
|
||||
"ibus engine",
|
||||
function(stdout, stderr, exitreason, exitcode)
|
||||
util.current_engine = string.gsub(stdout, "\n", "")
|
||||
util.current_engine_index = nil
|
||||
|
||||
-- Find the current engine's index in conf.ibus_language_list.
|
||||
-- If it is not there, util.current_engine_index will be nil.
|
||||
for k, v in pairs(conf.ibus_language_list) do
|
||||
if (v["ibus_engine"] == util.current_engine) then
|
||||
util.current_engine_index = k
|
||||
end
|
||||
end
|
||||
|
||||
-- Get current xkbmap
|
||||
awful.spawn.easy_async(
|
||||
"setxkbmap -query",
|
||||
function(stdout, stderr, exitreason, exitcode)
|
||||
util.current_xkbmap = string.match(stdout, "layout:%s+(%w+)")
|
||||
|
||||
-- When everything is done, run callback if it is given.
|
||||
if callback then
|
||||
callback()
|
||||
end
|
||||
end
|
||||
)
|
||||
end
|
||||
)
|
||||
end
|
||||
|
||||
|
||||
-- Set engine to language_index.
|
||||
-- calls util.get(callback) when done.
|
||||
util.set = function(language_index, callback)
|
||||
-- engine is an index of the language list above
|
||||
|
||||
local engine = conf.ibus_language_list[language_index]["ibus_engine"]
|
||||
|
||||
-- Get required engine, if one is given
|
||||
local requires_engine
|
||||
for k, v in pairs(conf.ibus_language_list) do
|
||||
if (v["ibus_engine"] == engine) then
|
||||
requires_engine = v["requires_engine"]
|
||||
end
|
||||
end
|
||||
|
||||
-- If a required xkb engine is given, but it is not active, switch to it before switching to the target
|
||||
if (requires_engine ~= util.current_engine) and (requires_engine ~= nil) then
|
||||
awful.spawn.easy_async(
|
||||
"ibus engine " .. requires_engine,
|
||||
function(stdout, stderr, exitreason, exitcode)
|
||||
awful.spawn.easy_async(
|
||||
"ibus engine " .. engine,
|
||||
function(stdout, stderr, exitreason, exitcode)
|
||||
util.get(callback)
|
||||
end
|
||||
)
|
||||
end
|
||||
)
|
||||
else
|
||||
awful.spawn.easy_async(
|
||||
"ibus engine " .. engine,
|
||||
function(stdout, stderr, exitreason, exitcode)
|
||||
util.get(callback)
|
||||
end
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
-- Calls util.set(callback) with next language in list.
|
||||
util.next = function(callback)
|
||||
if (util.current_engine_index == nil) or (util.current_engine_index == #conf.ibus_language_list) then
|
||||
util.current_engine_index = 1
|
||||
else
|
||||
util.current_engine_index = util.current_engine_index + 1
|
||||
end
|
||||
util.set(util.current_engine_index, callback)
|
||||
end
|
||||
|
||||
|
||||
|
||||
return util
|
72
modules/ibus/widget.lua
Executable file
72
modules/ibus/widget.lua
Executable file
@ -0,0 +1,72 @@
|
||||
local util = req_rel(..., "util")
|
||||
local widget = {}
|
||||
|
||||
widget.ibus_indicator_text = wibox.widget.textbox("??")
|
||||
widget.ibus_indicator_text.valign = "center"
|
||||
widget.ibus_indicator_text.align = "center"
|
||||
widget.ibus_indicator_text.font = "Hack NF 14"
|
||||
|
||||
widget.xkb_indicator_text = wibox.widget.textbox("??")
|
||||
widget.xkb_indicator_text.valign = "center"
|
||||
widget.xkb_indicator_text.align = "center"
|
||||
widget.xkb_indicator_text.font = "Hack NF 10"
|
||||
|
||||
|
||||
widget.widget = wibox.widget {
|
||||
{
|
||||
{ -- Right spacer
|
||||
widget = wibox.widget.separator,
|
||||
color = beautiful.color.transparent,
|
||||
forced_width = beautiful.dpi(3)
|
||||
},
|
||||
|
||||
wibox.widget {
|
||||
widget.ibus_indicator_text,
|
||||
widget.xkb_indicator_text,
|
||||
|
||||
forced_num_cols = 1,
|
||||
forced_num_rows = 2,
|
||||
homogeneous = true,
|
||||
expand = true,
|
||||
layout = wibox.layout.grid
|
||||
},
|
||||
|
||||
{ -- Left spacer
|
||||
widget = wibox.widget.separator,
|
||||
color = beautiful.color.transparent,
|
||||
forced_width = beautiful.dpi(3)
|
||||
},
|
||||
|
||||
layout = wibox.layout.align.horizontal,
|
||||
},
|
||||
layout = wibox.container.background,
|
||||
}
|
||||
|
||||
|
||||
-- Change background when mouse is over widget
|
||||
widget.widget:connect_signal("mouse::enter", function(result)
|
||||
widget.widget.bg = beautiful.color.bar.hover_bg
|
||||
end)
|
||||
|
||||
widget.widget:connect_signal("mouse::leave", function(result)
|
||||
widget.widget.bg = beautiful.color.transparent
|
||||
end)
|
||||
|
||||
widget.update = function()
|
||||
widget.ibus_indicator_text.markup = conf.ibus_language_list[util.current_engine_index]["indicator_code"]
|
||||
widget.xkb_indicator_text.markup = util.current_xkbmap
|
||||
end
|
||||
|
||||
|
||||
-- Update every 2 seconds, just in case.
|
||||
widget.update_timer = gears.timer {
|
||||
timeout = 2,
|
||||
call_now = false,
|
||||
autostart = true,
|
||||
single_shot = false,
|
||||
callback = function ()
|
||||
util.get(widget.update)
|
||||
end
|
||||
}
|
||||
|
||||
return widget
|
Reference in New Issue
Block a user