diff --git a/config/ags/main.ts b/config/ags/main.ts index 7b21ab61..e5c31959 100644 --- a/config/ags/main.ts +++ b/config/ags/main.ts @@ -10,6 +10,7 @@ import { media } from "./modules/media.ts" import { cliphist } from "./modules/cliphist.ts" import { emoji } from "./modules/emoji.ts" import { sidebar } from "./modules/sidebar/main.ts" +import { cheatsheet } from './modules/cheatsheet.ts'; import {} from 'apps/settings/main.ts'; import Window from 'types/widgets/window'; const GLib = imports.gi.GLib; @@ -32,6 +33,7 @@ const Windows = () => [ media, applauncher, cliphist, + cheatsheet, emoji, sidebar, ]; diff --git a/config/ags/modules/cheatsheet.ts b/config/ags/modules/cheatsheet.ts new file mode 100644 index 00000000..9197d3fd --- /dev/null +++ b/config/ags/modules/cheatsheet.ts @@ -0,0 +1,117 @@ +import Gtk from "gi://Gtk?version=3.0"; +import popupwindow from "modules/misc/popupwindow"; +const hyprland = await Service.import("hyprland"); + +const WINDOW_NAME = "cheatsheet"; + +const load_keybindings_cmd = `python -OOO ${App.configDir}/scripts/keybindings.py` +let _keybindings = Utils.exec(load_keybindings_cmd); +const keybindings = Variable(JSON.parse(_keybindings)); + +// hyprland.connect("event", (hyprland, name) => { +// if (name == "configreloaded") { +// print(name) +// Utils.execAsync(load_keybindings_cmd).then((out) => { +// keybindings.setValue(JSON.parse(out)); +// }); +// } +// }) + +const icons = { + "super": "" +} + + +const CheatSheet = () => Widget.FlowBox({ + attribute: { + "set": (self) => { + for (let category in keybindings.value) { + const box = Widget.Box({ + class_name: "category", + vertical: true, + vpack: "fill", + children: [ + Widget.Label({ + label: category, + class_name: "title", + hpack: "start" + }), + Widget.Separator() + ] + }) + let commands = keybindings.value[category]; + for (const command in commands) { + const _command = command.replaceAll(" ", " + ") + const key_list = _command.split(" ") + const key_box = Widget.Box({ + class_name: "row" + }) + for (const key of key_list) { + if (key == "+") + key_box.pack_start( + Widget.Label({ + label: "+", + class_name: "plus", + hpack: "start", + vpack: "center" + }), false, false, 0 + ) + else if (icons[key]) + key_box.pack_start( + Widget.Label({ + label: icons[key], + class_name: "awesome_icon key", + hpack: "start", + vpack: "center" + }), false, false, 0 + ) + else + key_box.pack_start( + Widget.Label({ + label: key.charAt(0).toUpperCase() + key.slice(1), + class_name: "key", + hpack: "start", + vpack: "center" + }), false, false, 0 + ) + } + key_box.pack_start( + Widget.Label({ + label: `: `, + class_name: "separator", + hpack: "start", + vpack: "center" + }), false, false, 0 + ) + key_box.pack_start( + Widget.Label({ + label: `${commands[command]}`, + class_name: "description", + hpack: "start", + vpack: "center" + }), false, false, 0 + ) + box.pack_start(key_box, false, false, 0) + } + self.add(box) + } + } + }, + setup: (self) => { + self.set_max_children_per_line(3); + self.set_min_children_per_line(3); + self.set_selection_mode(Gtk.SelectionMode.NONE); + self.attribute.set(self) + } +}) + + +export const cheatsheet = popupwindow({ + name: WINDOW_NAME, + + class_name: "cheatsheet", + visible: false, + keymode: "exclusive", + child: CheatSheet(), + anchor: [] +}) diff --git a/config/ags/scripts/keybindings.py b/config/ags/scripts/keybindings.py new file mode 100644 index 00000000..3b7c2285 --- /dev/null +++ b/config/ags/scripts/keybindings.py @@ -0,0 +1,127 @@ +import os +import re +import json + +HOME = os.path.expanduser("~") +CONFIG = f"{HOME}/dotfiles/hypr" + +files = [ + f"{CONFIG}/UserConfigs/UserKeybinds.conf", + f"{CONFIG}/configs/Keybinds.conf" +] + +categories_dict: dict[str, str] = {} +variables_dict: dict[str, str] = {} + +category_pattern = r"\n#! @(\w+): (.+)" +variable_pattern = r"\n\$(\w+)\s*=\s*(.*)" +bindings: dict[str, dict[str, str]] = {} + + +def extract_category_and_description(line: str) -> tuple[str | None, str | None, tuple | None]: + pattern2 = r'bind\s*=\s*.*! @description:\s*"((?:[^"]|\\")*)"\s*;\s*@(\w+);\s*@replace\s*"([^"]*)"\s*>\s*"([^"]*)";.*' + pattern = r'bind\s*=\s*.*! @description:\s*"((?:[^"]|\\")*)"\s*;\s*@(\w+);.*' + + match = re.search(pattern, line) + match2 = re.search(pattern2, line) + if match2: + description = match2.group(1).strip().replace('\\"', '"') + replace_from = match2.group(3).strip().lower() + replace_to = match2.group(4).strip().lower() + category = match2.group(2).strip() + + if category not in categories_dict: + return None, None, None + + return description, category, (replace_from, replace_to) + elif match: + description = match.group(1).strip().replace('\\"', '"') + category = match.group(2).strip() + + if category not in categories_dict: + return None, None, None + + return description, category, None + else: + return None, None, None + + +def extract_binding(line: str): + line = line.replace(" =", "=") + if not line.startswith("bind="): + return + line = line.lstrip("bind=").strip().lower().replace("super_l", "") + _bindings = line.split(",") + bindings = ' '.join([_bindings[0].strip(), _bindings[1].strip()]) + return bindings + + +def replace_variable(match: re.Match[str]): + var_name = match.group(1) + return variables_dict.get(var_name, match.group(0)) + + +def load_categories(content: str): + matches = re.findall(category_pattern, content) + + for key, value in matches: + categories_dict[key] = value + if value not in bindings: + bindings[value] = {} + return content + + +def load_variables(content: str): + matches = re.findall(variable_pattern, content) + + for var_name, var_value in matches: + variables_dict[var_name] = var_value + + content_lines = content.split("\n") + content_lines = [ + line for line in content_lines if not line.startswith("$") + ] + content = "\n".join(content_lines) + return content + + +def load_file(file: str): + with open(file) as f: + content = f.read() + + content = load_categories(content) + content = load_variables(content) + + # remove comments + content = re.sub(r'^#.*$\n', '', content, flags=re.MULTILINE) + return content + + +def load(): + global bindings + for file in files: + content = load_file(file) + for line in content.split('\n'): + if not line.replace(" ", "").startswith("bind="): + continue + + description, category, replace = extract_category_and_description(line) + if category is None or description is None: + continue + + binding = extract_binding(line) + if binding is None: + continue + + category_name = categories_dict[category] + if category_name not in bindings: + bindings[category_name] = {} + + if replace is not None: + binding = binding.replace(*replace) + + bindings[category_name][binding.strip()] = description.strip() + + +load() +print(json.dumps(bindings, indent=4)) diff --git a/config/ags/style.css b/config/ags/style.css index 3163a8fa..d4a5383c 100644 --- a/config/ags/style.css +++ b/config/ags/style.css @@ -1047,3 +1047,69 @@ switch:checked:hover, switch:checked:active { switch:checked:hover slider, switch:checked:active slider { background-color: @primaryContainer; } + +/* ----------------------------------------------------- + * Cheatsheet + * ----------------------------------------------------- */ + +.cheatsheet flowbox { + margin-top: 14px; + margin-bottom: 14px; + background-color: @background; + border-radius: 24px; + border: 2px solid @outlineVariant; + padding: 5px; +} + +.cheatsheet .category { + background-color: @surfaceContainerLow; + border-radius: 17px; + padding-top: 7px; + padding-bottom: 7px; +} + +.cheatsheet .category separator { + margin-top: 2px; + margin-bottom: 2px; +} + +.cheatsheet .category .row { + margin-bottom: 2px; + margin-left: 7px; + margin-right: 7px; +} + +.cheatsheet .category .key { + background-color: @surfaceContainerLowest; + padding-left: 4px; + padding-right: 4px; + color: @onSurface; + border-radius: 4px; +} + +.cheatsheet .category .plus { + margin-left: 4px; + margin-right: 4px; + color: @outline; +} + +.cheatsheet .category .separator { + color: @outline; +} + +.cheatsheet .category .description { + color: @onSurfaceVariant; + font-size: 16px; +} + +.cheatsheet .category .title { + font-weight: bold; + color: @onSurface; + margin-left: 7px; + margin-right: 7px; + font-size: 17px; +} + +.cheatsheet .category .awesome_icon.key { + padding: 4px; +}