Skip to content

Instantly share code, notes, and snippets.

@necauqua
Last active September 23, 2022 19:36
Show Gist options
  • Save necauqua/4faa6a2cba66435e23fa98578862a0c8 to your computer and use it in GitHub Desktop.
Save necauqua/4faa6a2cba66435e23fa98578862a0c8 to your computer and use it in GitHub Desktop.
local layout = require 'awful.widget.keyboardlayout'
local menubar = require 'menubar'
local kbdlayout = {
globally_preferred = 'us',
menubar_preferred = 'us',
}
local function get_idx_by_name(name)
if not name then
return
end
for i, v in ipairs(layout.get_groups_from_group_names(awesome.xkb_get_group_names())) do
if v.file == name then
return i - 1
end
end
end
require 'awful.client' .property.persist('last_layout', 'number')
local oneshot_lock = false
local function on_layout_change()
-- so that it does not override the last_layout
-- when we set it, e.g. from menubar.show
if oneshot_lock then
oneshot_lock = false
return
end
local c = client.focus
if c then
local idx = awesome.xkb_get_layout_group()
c.last_layout = idx
end
end
local function on_focus_changed(c)
local idx = c.last_layout or get_idx_by_name(c.preferred_layout or kbdlayout.globally_preferred)
if idx and awesome.xkb_get_layout_group() ~= idx then
awesome.xkb_set_layout_group(idx)
end
end
awesome.connect_signal('xkb::map_changed', on_layout_change)
awesome.connect_signal('xkb::group_changed', on_layout_change)
client.connect_signal('focus', on_focus_changed)
-- menubar has no signals or anything, so just plain old monkeypatching
local menubar_show = menubar.show
local menubar_hide = menubar.hide
function menubar.show(...)
menubar_show(...)
local idx = get_idx_by_name(kbdlayout.menubar_preferred)
if idx then
oneshot_lock = true
awesome.xkb_set_layout_group(idx)
end
end
function menubar.hide(...)
menubar_hide(...)
local c = client.focus
if c then
on_focus_changed(c)
end
end
return kbdlayout
@necauqua
Copy link
Author

A small module for awesome wm for per-window keyboard layouts.
This is made so that you can change globally_preferred layout for any random new client as well as setting a preferred_layout rule for specific clients.

e.g. most of my usage is us but I use Telegram mostly for Russian messaging so I have a rule where Telegram has ru layout by default, so I alsmost never even have to switch

@splitDEV
Copy link

Works flawless, thank you :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment