Skip to content

Commit

Permalink
Merge pull request #364 from nwg-piotr/update_kb
Browse files Browse the repository at this point in the history
add kb-layout label refresh in time interval #352
  • Loading branch information
nwg-piotr authored Dec 17, 2024
2 parents 2976318 + 16c0106 commit 60cbd16
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 39 deletions.
10 changes: 10 additions & 0 deletions nwg_panel/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2394,6 +2394,7 @@ def edit_keyboard_layout(self, *args):
"css-name": "executor",
"show-icon": True,
"icon-size": 16,
"interval": 0,
"icon-placement": "left",
"tooltip-text": "LMB: Next layout, RMB: Menu",
"angle": 0.0
Expand All @@ -2408,6 +2409,7 @@ def edit_keyboard_layout(self, *args):
builder.get_object("lbl-css-name").set_text("{}:".format(voc["css-name"]))
builder.get_object("lbl-icon-placement").set_text("{}:".format(voc["icon-placement"]))
builder.get_object("lbl-icon-size").set_text("{}:".format(voc["icon-size"]))
builder.get_object("lbl-interval").set_text("{}:".format(voc["refresh-interval"]))
builder.get_object("show-icon").set_label("{}".format(voc["show-icon"]))
builder.get_object("lbl-angle").set_text("{}:".format(voc["angle"]))

Expand Down Expand Up @@ -2459,6 +2461,13 @@ def edit_keyboard_layout(self, *args):
self.ws_angle.set_tooltip_text(voc["angle-tooltip"])
self.ws_angle.set_active_id(str(settings["angle"]))

self.kl_interval = builder.get_object("interval")
self.kl_interval.set_tooltip_text(voc["refresh-interval-tooltip"])
self.kl_interval.set_numeric(True)
adj = Gtk.Adjustment(value=0, lower=0, upper=3601, step_increment=1, page_increment=10, page_size=1)
self.kl_interval.configure(adj, 1, 0)
self.kl_interval.set_value(settings["interval"])

self.cb_show_icon = builder.get_object("show-icon")
self.cb_show_icon.set_active(settings["show-icon"])

Expand All @@ -2475,6 +2484,7 @@ def update_keyboard_layout(self):
settings["css-name"] = self.kl_css_name.get_text()
settings["icon-placement"] = self.kl_icon_placement.get_active_id()
settings["icon-size"] = self.kl_icon_size.get_value()
settings["interval"] = self.kl_interval.get_value()
settings["show-icon"] = self.cb_show_icon.get_active()
try:
settings["angle"] = float(self.ws_angle.get_active_id())
Expand Down
80 changes: 51 additions & 29 deletions nwg_panel/glade/config_keyboard_layout.glade
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<property name="label-xalign">0.5</property>
<property name="shadow-type">out</property>
<child>
<!-- n-columns=2 n-rows=10 -->
<!-- n-columns=2 n-rows=11 -->
<object class="GtkGrid" id="grid">
<property name="visible">True</property>
<property name="can-focus">False</property>
Expand All @@ -18,21 +18,6 @@
<property name="margin-bottom">6</property>
<property name="row-spacing">6</property>
<property name="column-spacing">12</property>
<child>
<object class="GtkLabel" id="note">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="halign">start</property>
<property name="label" translatable="yes">&lt;span size="small"&gt;&lt;b&gt;Note:&lt;/b&gt; on sway switching layouts for a certain keyboard device does not work.
We will be switching for devices of 'type:keyboard' ¯\_(•_•)_/¯&lt;/span&gt;</property>
<property name="use-markup">True</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">9</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="tooltip-text">
<property name="visible">True</property>
Expand Down Expand Up @@ -161,38 +146,38 @@ We will be switching for devices of 'type:keyboard' ¯\_(•_•)_/¯&lt;/span&g
</packing>
</child>
<child>
<object class="GtkComboBoxText" id="angle">
<object class="GtkLabel" id="note">
<property name="visible">True</property>
<property name="can-focus">False</property>
<items>
<item id="0.0" translatable="yes">0°</item>
<item id="90.0" translatable="yes">90°</item>
<item id="270.0" translatable="yes">270°</item>
</items>
<property name="halign">start</property>
<property name="label" translatable="yes">&lt;span size="small"&gt;&lt;b&gt;Note:&lt;/b&gt; on sway switching layouts for a certain keyboard device does not work.
We will be switching for devices of 'type:keyboard' ¯\_(•_•)_/¯&lt;/span&gt;</property>
<property name="use-markup">True</property>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">7</property>
<property name="left-attach">0</property>
<property name="top-attach">10</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="lbl-angle">
<object class="GtkLabel" id="lbl-device">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="halign">end</property>
<property name="label" translatable="yes">Angle:</property>
<property name="label" translatable="yes">Device:</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">7</property>
<property name="top-attach">9</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="lbl-device">
<object class="GtkLabel" id="lbl-angle">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="halign">end</property>
<property name="label" translatable="yes">Device:</property>
<property name="label" translatable="yes">Angle:</property>
</object>
<packing>
<property name="left-attach">0</property>
Expand All @@ -204,11 +189,48 @@ We will be switching for devices of 'type:keyboard' ¯\_(•_•)_/¯&lt;/span&g
<property name="visible">True</property>
<property name="can-focus">False</property>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">9</property>
</packing>
</child>
<child>
<object class="GtkComboBoxText" id="angle">
<property name="visible">True</property>
<property name="can-focus">False</property>
<items>
<item id="0.0" translatable="yes">0°</item>
<item id="90.0" translatable="yes">90°</item>
<item id="270.0" translatable="yes">270°</item>
</items>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">8</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="lbl-interval">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="halign">end</property>
<property name="label" translatable="yes">Interval:</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">7</property>
</packing>
</child>
<child>
<object class="GtkSpinButton" id="interval">
<property name="visible">True</property>
<property name="can-focus">True</property>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">7</property>
</packing>
</child>
<child>
<placeholder/>
</child>
Expand Down
23 changes: 15 additions & 8 deletions nwg_panel/modules/keyboard_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

import gi

from nwg_panel.tools import check_key, update_image, eprint, hyprctl
from nwg_panel.tools import check_key, update_image, create_background_task, eprint, hyprctl

gi.require_version('Gtk', '3.0')
gi.require_version('Gdk', '3.0')

from gi.repository import Gtk, Gdk
from gi.repository import Gtk, Gdk, GLib


def on_enter_notify_event(widget, event):
Expand Down Expand Up @@ -66,6 +66,7 @@ def __init__(self, settings, icons_path):
check_key(settings, "icon-placement", "left")
check_key(settings, "icon-size", 16)
check_key(settings, "show-icon", True)
check_key(settings, "interval", 0)
check_key(settings, "tooltip-text", "LMB: Next layout, RMB: Menu")
check_key(settings, "angle", 0.0)

Expand Down Expand Up @@ -97,6 +98,8 @@ def __init__(self, settings, icons_path):
else:
print("KeyboardLayout module: failed listing devices, won't create UI, sorry.")

self.refresh()

def list_keyboards(self):
if self.compositor == "Hyprland":
o = hyprctl("j/devices")
Expand Down Expand Up @@ -146,11 +149,15 @@ def get_current_layout(self):
return self.keyboards[0].xkb_active_layout_name
return "unknown"

def refresh(self):
def update_label(self):
self.keyboards = self.list_keyboards()
label = self.get_current_layout()
if label:
self.label.set_text(label)
txt = self.get_current_layout()
if txt:
self.label.set_text(txt)

def refresh(self, *args):
thread = create_background_task(self.update_label, self.settings["interval"])
thread.start()

def build_box(self):
if self.settings["show-icon"] and self.settings["icon-placement"] == "left":
Expand All @@ -172,7 +179,7 @@ def on_left_click(self):
# apply to all devices of type:keyboard
self.i3.command(f'input type:keyboard xkb_switch_layout next')

self.refresh()
self.update_label()

def on_menu_item(self, item, idx):
if self.compositor == "Hyprland":
Expand All @@ -187,7 +194,7 @@ def on_menu_item(self, item, idx):
# apply to all devices of type:keyboard
self.i3.command(f'input type:keyboard xkb_switch_layout {idx}')

self.refresh()
self.update_label()

def on_right_click(self):
menu = Gtk.Menu()
Expand Down
6 changes: 4 additions & 2 deletions nwg_panel/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def temp_dir():

return "/tmp"


def local_dir():
local_dir = os.path.join(os.path.join(os.getenv("HOME"), ".local/share/nwg-panel"))
if not os.path.isdir(local_dir):
Expand Down Expand Up @@ -709,6 +710,7 @@ def get_cache_dir():
else:
return None


def file_age(path):
return time.time() - os.stat(path)[stat.ST_MTIME]

Expand Down Expand Up @@ -824,7 +826,7 @@ def cmd_through_compositor(cmd):
cmd = cmd.replace("\"", "'")
cs_file = os.path.join(get_config_dir(), "common-settings.json")
common_settings = load_json(cs_file)
if "run-through-compositor" not in common_settings or common_settings["run-through-compositor"] :
if "run-through-compositor" not in common_settings or common_settings["run-through-compositor"]:
if os.getenv("SWAYSOCK"):
if os.getenv("XDG_SESSION_DESKTOP") and "miracle-wm" in os.getenv("XDG_SESSION_DESKTOP"):
cmd = f'miraclemsg exec "{cmd}"'
Expand All @@ -842,7 +844,7 @@ def load_resource(package, resource_name):
return resource_file.read()
except:
pass

try:
import importlib.util
spec = importlib.util.find_spec(package)
Expand Down

0 comments on commit 60cbd16

Please sign in to comment.