├── .github └── workflows │ └── rust.yml ├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── LICENSE ├── README.md └── src ├── color.rs ├── config.rs ├── config ├── anchor.rs ├── compat.rs ├── entry.rs ├── font.rs └── namespace.rs ├── key.rs ├── main.rs ├── menu.rs └── text.rs /.github/workflows/rust.yml: -------------------------------------------------------------------------------- 1 | on: [push, pull_request] 2 | 3 | name: Rust 4 | 5 | jobs: 6 | check: 7 | name: Check 8 | runs-on: ubuntu-latest 9 | steps: 10 | - name: Get required packages 11 | run: sudo apt-get update && sudo apt-get install libpango1.0-dev libxkbcommon-dev 12 | - uses: actions/checkout@v4 13 | - uses: dtolnay/rust-toolchain@stable 14 | - name: Check 15 | run: cargo check --all --all-features 16 | 17 | test: 18 | name: Test Suite 19 | runs-on: ubuntu-latest 20 | steps: 21 | - name: Get required packages 22 | run: sudo apt-get update && sudo apt-get install libpango1.0-dev libxkbcommon-dev 23 | - uses: actions/checkout@v4 24 | - uses: dtolnay/rust-toolchain@stable 25 | - name: Tests 26 | run: cargo test --all --all-features 27 | 28 | fmt: 29 | name: Rustfmt 30 | runs-on: ubuntu-latest 31 | steps: 32 | - name: Get required packages 33 | run: sudo apt-get update && sudo apt-get install libpango1.0-dev libxkbcommon-dev 34 | - uses: actions/checkout@v4 35 | - uses: dtolnay/rust-toolchain@stable 36 | with: 37 | components: rustfmt 38 | - name: Fmt check 39 | run: rustfmt --check --edition 2021 $(fdfind -e rs) 40 | 41 | clippy: 42 | name: Clippy 43 | runs-on: ubuntu-latest 44 | steps: 45 | - name: Get required packages 46 | run: sudo apt-get update && sudo apt-get install libpango1.0-dev libxkbcommon-dev 47 | - uses: actions/checkout@v4 48 | - uses: dtolnay/rust-toolchain@stable 49 | with: 50 | components: clippy 51 | - name: Clippy check 52 | run: cargo clippy --all --all-features -- -D warnings -A unknown-lints 53 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 4 4 | 5 | [[package]] 6 | name = "anstyle" 7 | version = "1.0.11" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "862ed96ca487e809f1c8e5a8447f6ee2cf102f846893800b20cebdf541fc6bbd" 10 | 11 | [[package]] 12 | name = "anyhow" 13 | version = "1.0.98" 14 | source = "registry+https://github.com/rust-lang/crates.io-index" 15 | checksum = "e16d2d3311acee920a9eb8d33b8cbc1787ce4a264e85f964c2404b969bdcd487" 16 | 17 | [[package]] 18 | name = "bitflags" 19 | version = "2.9.1" 20 | source = "registry+https://github.com/rust-lang/crates.io-index" 21 | checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967" 22 | 23 | [[package]] 24 | name = "cairo-rs" 25 | version = "0.20.10" 26 | source = "registry+https://github.com/rust-lang/crates.io-index" 27 | checksum = "b58e62a27cd02fb3f63f82bb31fdda7e6c43141497cbe97e8816d7c914043f55" 28 | dependencies = [ 29 | "bitflags", 30 | "cairo-sys-rs", 31 | "glib", 32 | "libc", 33 | ] 34 | 35 | [[package]] 36 | name = "cairo-sys-rs" 37 | version = "0.20.10" 38 | source = "registry+https://github.com/rust-lang/crates.io-index" 39 | checksum = "059cc746549898cbfd9a47754288e5a958756650ef4652bbb6c5f71a6bda4f8b" 40 | dependencies = [ 41 | "glib-sys", 42 | "libc", 43 | "system-deps", 44 | ] 45 | 46 | [[package]] 47 | name = "cfg-expr" 48 | version = "0.20.0" 49 | source = "registry+https://github.com/rust-lang/crates.io-index" 50 | checksum = "e34e221e91c7eb5e8315b5c9cf1a61670938c0626451f954a51693ed44b37f45" 51 | dependencies = [ 52 | "smallvec", 53 | "target-lexicon", 54 | ] 55 | 56 | [[package]] 57 | name = "clap" 58 | version = "4.5.40" 59 | source = "registry+https://github.com/rust-lang/crates.io-index" 60 | checksum = "40b6887a1d8685cebccf115538db5c0efe625ccac9696ad45c409d96566e910f" 61 | dependencies = [ 62 | "clap_builder", 63 | "clap_derive", 64 | ] 65 | 66 | [[package]] 67 | name = "clap_builder" 68 | version = "4.5.40" 69 | source = "registry+https://github.com/rust-lang/crates.io-index" 70 | checksum = "e0c66c08ce9f0c698cbce5c0279d0bb6ac936d8674174fe48f736533b964f59e" 71 | dependencies = [ 72 | "anstyle", 73 | "clap_lex", 74 | ] 75 | 76 | [[package]] 77 | name = "clap_derive" 78 | version = "4.5.40" 79 | source = "registry+https://github.com/rust-lang/crates.io-index" 80 | checksum = "d2c7947ae4cc3d851207c1adb5b5e260ff0cca11446b1d6d1423788e442257ce" 81 | dependencies = [ 82 | "heck", 83 | "proc-macro2", 84 | "quote", 85 | "syn", 86 | ] 87 | 88 | [[package]] 89 | name = "clap_lex" 90 | version = "0.7.5" 91 | source = "registry+https://github.com/rust-lang/crates.io-index" 92 | checksum = "b94f61472cee1439c0b966b47e3aca9ae07e45d070759512cd390ea2bebc6675" 93 | 94 | [[package]] 95 | name = "equivalent" 96 | version = "1.0.2" 97 | source = "registry+https://github.com/rust-lang/crates.io-index" 98 | checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" 99 | 100 | [[package]] 101 | name = "futures-channel" 102 | version = "0.3.31" 103 | source = "registry+https://github.com/rust-lang/crates.io-index" 104 | checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10" 105 | dependencies = [ 106 | "futures-core", 107 | ] 108 | 109 | [[package]] 110 | name = "futures-core" 111 | version = "0.3.31" 112 | source = "registry+https://github.com/rust-lang/crates.io-index" 113 | checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" 114 | 115 | [[package]] 116 | name = "futures-executor" 117 | version = "0.3.31" 118 | source = "registry+https://github.com/rust-lang/crates.io-index" 119 | checksum = "1e28d1d997f585e54aebc3f97d39e72338912123a67330d723fdbb564d646c9f" 120 | dependencies = [ 121 | "futures-core", 122 | "futures-task", 123 | "futures-util", 124 | ] 125 | 126 | [[package]] 127 | name = "futures-io" 128 | version = "0.3.31" 129 | source = "registry+https://github.com/rust-lang/crates.io-index" 130 | checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6" 131 | 132 | [[package]] 133 | name = "futures-macro" 134 | version = "0.3.31" 135 | source = "registry+https://github.com/rust-lang/crates.io-index" 136 | checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650" 137 | dependencies = [ 138 | "proc-macro2", 139 | "quote", 140 | "syn", 141 | ] 142 | 143 | [[package]] 144 | name = "futures-task" 145 | version = "0.3.31" 146 | source = "registry+https://github.com/rust-lang/crates.io-index" 147 | checksum = "f90f7dce0722e95104fcb095585910c0977252f286e354b5e3bd38902cd99988" 148 | 149 | [[package]] 150 | name = "futures-util" 151 | version = "0.3.31" 152 | source = "registry+https://github.com/rust-lang/crates.io-index" 153 | checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81" 154 | dependencies = [ 155 | "futures-core", 156 | "futures-macro", 157 | "futures-task", 158 | "pin-project-lite", 159 | "pin-utils", 160 | "slab", 161 | ] 162 | 163 | [[package]] 164 | name = "gio" 165 | version = "0.20.11" 166 | source = "registry+https://github.com/rust-lang/crates.io-index" 167 | checksum = "d2a5c3829f5794cb15120db87707b2ec03720edff7ad09eb7b711b532e3fe747" 168 | dependencies = [ 169 | "futures-channel", 170 | "futures-core", 171 | "futures-io", 172 | "futures-util", 173 | "gio-sys", 174 | "glib", 175 | "libc", 176 | "pin-project-lite", 177 | "smallvec", 178 | ] 179 | 180 | [[package]] 181 | name = "gio-sys" 182 | version = "0.20.10" 183 | source = "registry+https://github.com/rust-lang/crates.io-index" 184 | checksum = "521e93a7e56fc89e84aea9a52cfc9436816a4b363b030260b699950ff1336c83" 185 | dependencies = [ 186 | "glib-sys", 187 | "gobject-sys", 188 | "libc", 189 | "system-deps", 190 | "windows-sys", 191 | ] 192 | 193 | [[package]] 194 | name = "glib" 195 | version = "0.20.10" 196 | source = "registry+https://github.com/rust-lang/crates.io-index" 197 | checksum = "c501c495842c2b23cdacead803a5a343ca2a5d7a7ddaff14cc5f6cf22cfb92c2" 198 | dependencies = [ 199 | "bitflags", 200 | "futures-channel", 201 | "futures-core", 202 | "futures-executor", 203 | "futures-task", 204 | "futures-util", 205 | "gio-sys", 206 | "glib-macros", 207 | "glib-sys", 208 | "gobject-sys", 209 | "libc", 210 | "memchr", 211 | "smallvec", 212 | ] 213 | 214 | [[package]] 215 | name = "glib-macros" 216 | version = "0.20.10" 217 | source = "registry+https://github.com/rust-lang/crates.io-index" 218 | checksum = "ebe6dc9ce29887c4b3b74d78d5ba473db160a258ae7ed883d23632ac7fed7bc9" 219 | dependencies = [ 220 | "heck", 221 | "proc-macro-crate", 222 | "proc-macro2", 223 | "quote", 224 | "syn", 225 | ] 226 | 227 | [[package]] 228 | name = "glib-sys" 229 | version = "0.20.10" 230 | source = "registry+https://github.com/rust-lang/crates.io-index" 231 | checksum = "8ab79e1ed126803a8fb827e3de0e2ff95191912b8db65cee467edb56fc4cc215" 232 | dependencies = [ 233 | "libc", 234 | "system-deps", 235 | ] 236 | 237 | [[package]] 238 | name = "gobject-sys" 239 | version = "0.20.10" 240 | source = "registry+https://github.com/rust-lang/crates.io-index" 241 | checksum = "ec9aca94bb73989e3cfdbf8f2e0f1f6da04db4d291c431f444838925c4c63eda" 242 | dependencies = [ 243 | "glib-sys", 244 | "libc", 245 | "system-deps", 246 | ] 247 | 248 | [[package]] 249 | name = "hashbrown" 250 | version = "0.15.4" 251 | source = "registry+https://github.com/rust-lang/crates.io-index" 252 | checksum = "5971ac85611da7067dbfcabef3c70ebb5606018acd9e2a3903a0da507521e0d5" 253 | 254 | [[package]] 255 | name = "heck" 256 | version = "0.5.0" 257 | source = "registry+https://github.com/rust-lang/crates.io-index" 258 | checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" 259 | 260 | [[package]] 261 | name = "indexmap" 262 | version = "2.9.0" 263 | source = "registry+https://github.com/rust-lang/crates.io-index" 264 | checksum = "cea70ddb795996207ad57735b50c5982d8844f38ba9ee5f1aedcfb708a2aa11e" 265 | dependencies = [ 266 | "equivalent", 267 | "hashbrown", 268 | "serde", 269 | ] 270 | 271 | [[package]] 272 | name = "itoa" 273 | version = "1.0.15" 274 | source = "registry+https://github.com/rust-lang/crates.io-index" 275 | checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" 276 | 277 | [[package]] 278 | name = "libc" 279 | version = "0.2.174" 280 | source = "registry+https://github.com/rust-lang/crates.io-index" 281 | checksum = "1171693293099992e19cddea4e8b849964e9846f4acee11b3948bcc337be8776" 282 | 283 | [[package]] 284 | name = "memchr" 285 | version = "2.7.5" 286 | source = "registry+https://github.com/rust-lang/crates.io-index" 287 | checksum = "32a282da65faaf38286cf3be983213fcf1d2e2a58700e808f83f4ea9a4804bc0" 288 | 289 | [[package]] 290 | name = "memmap2" 291 | version = "0.9.5" 292 | source = "registry+https://github.com/rust-lang/crates.io-index" 293 | checksum = "fd3f7eed9d3848f8b98834af67102b720745c4ec028fcd0aa0239277e7de374f" 294 | dependencies = [ 295 | "libc", 296 | ] 297 | 298 | [[package]] 299 | name = "pango" 300 | version = "0.20.10" 301 | source = "registry+https://github.com/rust-lang/crates.io-index" 302 | checksum = "d88d37c161f2848f0d9382597f0168484c9335ac800995f3956641abb7002938" 303 | dependencies = [ 304 | "gio", 305 | "glib", 306 | "libc", 307 | "pango-sys", 308 | ] 309 | 310 | [[package]] 311 | name = "pango-sys" 312 | version = "0.20.10" 313 | source = "registry+https://github.com/rust-lang/crates.io-index" 314 | checksum = "186909673fc09be354555c302c0b3dcf753cd9fa08dcb8077fa663c80fb243fa" 315 | dependencies = [ 316 | "glib-sys", 317 | "gobject-sys", 318 | "libc", 319 | "system-deps", 320 | ] 321 | 322 | [[package]] 323 | name = "pangocairo" 324 | version = "0.20.10" 325 | source = "registry+https://github.com/rust-lang/crates.io-index" 326 | checksum = "58890dc451db9964ac2d8874f903a4370a4b3932aa5281ff0c8d9810937ad84f" 327 | dependencies = [ 328 | "cairo-rs", 329 | "glib", 330 | "libc", 331 | "pango", 332 | "pangocairo-sys", 333 | ] 334 | 335 | [[package]] 336 | name = "pangocairo-sys" 337 | version = "0.20.10" 338 | source = "registry+https://github.com/rust-lang/crates.io-index" 339 | checksum = "b9952903f88aa93e2927e7bca2d1ebae64fc26545a9280b4ce6bddeda26b5c42" 340 | dependencies = [ 341 | "cairo-sys-rs", 342 | "glib-sys", 343 | "libc", 344 | "pango-sys", 345 | "system-deps", 346 | ] 347 | 348 | [[package]] 349 | name = "pin-project-lite" 350 | version = "0.2.16" 351 | source = "registry+https://github.com/rust-lang/crates.io-index" 352 | checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" 353 | 354 | [[package]] 355 | name = "pin-utils" 356 | version = "0.1.0" 357 | source = "registry+https://github.com/rust-lang/crates.io-index" 358 | checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" 359 | 360 | [[package]] 361 | name = "pkg-config" 362 | version = "0.3.32" 363 | source = "registry+https://github.com/rust-lang/crates.io-index" 364 | checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c" 365 | 366 | [[package]] 367 | name = "proc-macro-crate" 368 | version = "3.3.0" 369 | source = "registry+https://github.com/rust-lang/crates.io-index" 370 | checksum = "edce586971a4dfaa28950c6f18ed55e0406c1ab88bbce2c6f6293a7aaba73d35" 371 | dependencies = [ 372 | "toml_edit", 373 | ] 374 | 375 | [[package]] 376 | name = "proc-macro2" 377 | version = "1.0.95" 378 | source = "registry+https://github.com/rust-lang/crates.io-index" 379 | checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778" 380 | dependencies = [ 381 | "unicode-ident", 382 | ] 383 | 384 | [[package]] 385 | name = "quick-xml" 386 | version = "0.37.5" 387 | source = "registry+https://github.com/rust-lang/crates.io-index" 388 | checksum = "331e97a1af0bf59823e6eadffe373d7b27f485be8748f71471c662c1f269b7fb" 389 | dependencies = [ 390 | "memchr", 391 | ] 392 | 393 | [[package]] 394 | name = "quote" 395 | version = "1.0.40" 396 | source = "registry+https://github.com/rust-lang/crates.io-index" 397 | checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" 398 | dependencies = [ 399 | "proc-macro2", 400 | ] 401 | 402 | [[package]] 403 | name = "ryu" 404 | version = "1.0.20" 405 | source = "registry+https://github.com/rust-lang/crates.io-index" 406 | checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f" 407 | 408 | [[package]] 409 | name = "serde" 410 | version = "1.0.219" 411 | source = "registry+https://github.com/rust-lang/crates.io-index" 412 | checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6" 413 | dependencies = [ 414 | "serde_derive", 415 | ] 416 | 417 | [[package]] 418 | name = "serde_derive" 419 | version = "1.0.219" 420 | source = "registry+https://github.com/rust-lang/crates.io-index" 421 | checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00" 422 | dependencies = [ 423 | "proc-macro2", 424 | "quote", 425 | "syn", 426 | ] 427 | 428 | [[package]] 429 | name = "serde_spanned" 430 | version = "0.6.9" 431 | source = "registry+https://github.com/rust-lang/crates.io-index" 432 | checksum = "bf41e0cfaf7226dca15e8197172c295a782857fcb97fad1808a166870dee75a3" 433 | dependencies = [ 434 | "serde", 435 | ] 436 | 437 | [[package]] 438 | name = "serde_yaml" 439 | version = "0.9.34+deprecated" 440 | source = "registry+https://github.com/rust-lang/crates.io-index" 441 | checksum = "6a8b1a1a2ebf674015cc02edccce75287f1a0130d394307b36743c2f5d504b47" 442 | dependencies = [ 443 | "indexmap", 444 | "itoa", 445 | "ryu", 446 | "serde", 447 | "unsafe-libyaml", 448 | ] 449 | 450 | [[package]] 451 | name = "shmemfdrs2" 452 | version = "1.0.0" 453 | source = "registry+https://github.com/rust-lang/crates.io-index" 454 | checksum = "70a05cf957f811e44f99c629e6d34025429912ffb2333f2960372669e670f54c" 455 | dependencies = [ 456 | "libc", 457 | ] 458 | 459 | [[package]] 460 | name = "slab" 461 | version = "0.4.10" 462 | source = "registry+https://github.com/rust-lang/crates.io-index" 463 | checksum = "04dc19736151f35336d325007ac991178d504a119863a2fcb3758cdb5e52c50d" 464 | 465 | [[package]] 466 | name = "smallvec" 467 | version = "1.15.1" 468 | source = "registry+https://github.com/rust-lang/crates.io-index" 469 | checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" 470 | 471 | [[package]] 472 | name = "smart-default" 473 | version = "0.7.1" 474 | source = "registry+https://github.com/rust-lang/crates.io-index" 475 | checksum = "0eb01866308440fc64d6c44d9e86c5cc17adfe33c4d6eed55da9145044d0ffc1" 476 | dependencies = [ 477 | "proc-macro2", 478 | "quote", 479 | "syn", 480 | ] 481 | 482 | [[package]] 483 | name = "syn" 484 | version = "2.0.103" 485 | source = "registry+https://github.com/rust-lang/crates.io-index" 486 | checksum = "e4307e30089d6fd6aff212f2da3a1f9e32f3223b1f010fb09b7c95f90f3ca1e8" 487 | dependencies = [ 488 | "proc-macro2", 489 | "quote", 490 | "unicode-ident", 491 | ] 492 | 493 | [[package]] 494 | name = "system-deps" 495 | version = "7.0.5" 496 | source = "registry+https://github.com/rust-lang/crates.io-index" 497 | checksum = "e4be53aa0cba896d2dc615bd42bbc130acdcffa239e0a2d965ea5b3b2a86ffdb" 498 | dependencies = [ 499 | "cfg-expr", 500 | "heck", 501 | "pkg-config", 502 | "toml", 503 | "version-compare", 504 | ] 505 | 506 | [[package]] 507 | name = "target-lexicon" 508 | version = "0.13.2" 509 | source = "registry+https://github.com/rust-lang/crates.io-index" 510 | checksum = "e502f78cdbb8ba4718f566c418c52bc729126ffd16baee5baa718cf25dd5a69a" 511 | 512 | [[package]] 513 | name = "toml" 514 | version = "0.8.23" 515 | source = "registry+https://github.com/rust-lang/crates.io-index" 516 | checksum = "dc1beb996b9d83529a9e75c17a1686767d148d70663143c7854d8b4a09ced362" 517 | dependencies = [ 518 | "serde", 519 | "serde_spanned", 520 | "toml_datetime", 521 | "toml_edit", 522 | ] 523 | 524 | [[package]] 525 | name = "toml_datetime" 526 | version = "0.6.11" 527 | source = "registry+https://github.com/rust-lang/crates.io-index" 528 | checksum = "22cddaf88f4fbc13c51aebbf5f8eceb5c7c5a9da2ac40a13519eb5b0a0e8f11c" 529 | dependencies = [ 530 | "serde", 531 | ] 532 | 533 | [[package]] 534 | name = "toml_edit" 535 | version = "0.22.27" 536 | source = "registry+https://github.com/rust-lang/crates.io-index" 537 | checksum = "41fe8c660ae4257887cf66394862d21dbca4a6ddd26f04a3560410406a2f819a" 538 | dependencies = [ 539 | "indexmap", 540 | "serde", 541 | "serde_spanned", 542 | "toml_datetime", 543 | "winnow", 544 | ] 545 | 546 | [[package]] 547 | name = "unicode-ident" 548 | version = "1.0.18" 549 | source = "registry+https://github.com/rust-lang/crates.io-index" 550 | checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" 551 | 552 | [[package]] 553 | name = "unsafe-libyaml" 554 | version = "0.2.11" 555 | source = "registry+https://github.com/rust-lang/crates.io-index" 556 | checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861" 557 | 558 | [[package]] 559 | name = "version-compare" 560 | version = "0.2.0" 561 | source = "registry+https://github.com/rust-lang/crates.io-index" 562 | checksum = "852e951cb7832cb45cb1169900d19760cfa39b82bc0ea9c0e5a14ae88411c98b" 563 | 564 | [[package]] 565 | name = "wayrs-client" 566 | version = "1.3.1" 567 | source = "registry+https://github.com/rust-lang/crates.io-index" 568 | checksum = "ce7a46942472add8b391a170bbec77bdab6f7deb93e46da388b285f9c8f7fc7e" 569 | dependencies = [ 570 | "wayrs-core", 571 | "wayrs-scanner", 572 | ] 573 | 574 | [[package]] 575 | name = "wayrs-core" 576 | version = "1.0.5" 577 | source = "registry+https://github.com/rust-lang/crates.io-index" 578 | checksum = "015f4f9bd84931309d0c55437de7e436719f291ef44d828c078f39ca5cd6675d" 579 | dependencies = [ 580 | "libc", 581 | ] 582 | 583 | [[package]] 584 | name = "wayrs-proto-parser" 585 | version = "3.0.1" 586 | source = "registry+https://github.com/rust-lang/crates.io-index" 587 | checksum = "41679c033d8ad15f8d9bbb73e50bb4ded3d4b12b8ba2123e5c526ed809b2e43c" 588 | dependencies = [ 589 | "quick-xml", 590 | ] 591 | 592 | [[package]] 593 | name = "wayrs-protocols" 594 | version = "0.14.11+1.45" 595 | source = "registry+https://github.com/rust-lang/crates.io-index" 596 | checksum = "28e7bc60d2bcad468fc6ee5864b54b57989bba29674e25164dca77fca1382884" 597 | dependencies = [ 598 | "wayrs-client", 599 | ] 600 | 601 | [[package]] 602 | name = "wayrs-scanner" 603 | version = "0.15.4" 604 | source = "registry+https://github.com/rust-lang/crates.io-index" 605 | checksum = "16b2f7c560a8d7cbca6af14443be51fb386acd02a558724c85dac8c7bf368d28" 606 | dependencies = [ 607 | "proc-macro2", 608 | "quote", 609 | "wayrs-proto-parser", 610 | ] 611 | 612 | [[package]] 613 | name = "wayrs-utils" 614 | version = "0.17.2" 615 | source = "registry+https://github.com/rust-lang/crates.io-index" 616 | checksum = "095323113ee4c37d9df748367182d6869cc38705bfb92f2aac60c7c87646d49e" 617 | dependencies = [ 618 | "libc", 619 | "memmap2", 620 | "shmemfdrs2", 621 | "wayrs-client", 622 | "xkbcommon", 623 | ] 624 | 625 | [[package]] 626 | name = "windows-sys" 627 | version = "0.59.0" 628 | source = "registry+https://github.com/rust-lang/crates.io-index" 629 | checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" 630 | dependencies = [ 631 | "windows-targets", 632 | ] 633 | 634 | [[package]] 635 | name = "windows-targets" 636 | version = "0.52.6" 637 | source = "registry+https://github.com/rust-lang/crates.io-index" 638 | checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" 639 | dependencies = [ 640 | "windows_aarch64_gnullvm", 641 | "windows_aarch64_msvc", 642 | "windows_i686_gnu", 643 | "windows_i686_gnullvm", 644 | "windows_i686_msvc", 645 | "windows_x86_64_gnu", 646 | "windows_x86_64_gnullvm", 647 | "windows_x86_64_msvc", 648 | ] 649 | 650 | [[package]] 651 | name = "windows_aarch64_gnullvm" 652 | version = "0.52.6" 653 | source = "registry+https://github.com/rust-lang/crates.io-index" 654 | checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" 655 | 656 | [[package]] 657 | name = "windows_aarch64_msvc" 658 | version = "0.52.6" 659 | source = "registry+https://github.com/rust-lang/crates.io-index" 660 | checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" 661 | 662 | [[package]] 663 | name = "windows_i686_gnu" 664 | version = "0.52.6" 665 | source = "registry+https://github.com/rust-lang/crates.io-index" 666 | checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" 667 | 668 | [[package]] 669 | name = "windows_i686_gnullvm" 670 | version = "0.52.6" 671 | source = "registry+https://github.com/rust-lang/crates.io-index" 672 | checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" 673 | 674 | [[package]] 675 | name = "windows_i686_msvc" 676 | version = "0.52.6" 677 | source = "registry+https://github.com/rust-lang/crates.io-index" 678 | checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" 679 | 680 | [[package]] 681 | name = "windows_x86_64_gnu" 682 | version = "0.52.6" 683 | source = "registry+https://github.com/rust-lang/crates.io-index" 684 | checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" 685 | 686 | [[package]] 687 | name = "windows_x86_64_gnullvm" 688 | version = "0.52.6" 689 | source = "registry+https://github.com/rust-lang/crates.io-index" 690 | checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" 691 | 692 | [[package]] 693 | name = "windows_x86_64_msvc" 694 | version = "0.52.6" 695 | source = "registry+https://github.com/rust-lang/crates.io-index" 696 | checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" 697 | 698 | [[package]] 699 | name = "winnow" 700 | version = "0.7.11" 701 | source = "registry+https://github.com/rust-lang/crates.io-index" 702 | checksum = "74c7b26e3480b707944fc872477815d29a8e429d2f93a1ce000f5fa84a15cbcd" 703 | dependencies = [ 704 | "memchr", 705 | ] 706 | 707 | [[package]] 708 | name = "wlr-which-key" 709 | version = "1.3.0" 710 | dependencies = [ 711 | "anyhow", 712 | "clap", 713 | "indexmap", 714 | "libc", 715 | "pangocairo", 716 | "serde", 717 | "serde_yaml", 718 | "smart-default", 719 | "wayrs-client", 720 | "wayrs-protocols", 721 | "wayrs-utils", 722 | ] 723 | 724 | [[package]] 725 | name = "xkbcommon" 726 | version = "0.8.0" 727 | source = "registry+https://github.com/rust-lang/crates.io-index" 728 | checksum = "8d66ca9352cbd4eecbbc40871d8a11b4ac8107cfc528a6e14d7c19c69d0e1ac9" 729 | dependencies = [ 730 | "libc", 731 | "memmap2", 732 | "xkeysym", 733 | ] 734 | 735 | [[package]] 736 | name = "xkeysym" 737 | version = "0.2.1" 738 | source = "registry+https://github.com/rust-lang/crates.io-index" 739 | checksum = "b9cc00251562a284751c9973bace760d86c0276c471b4be569fe6b068ee97a56" 740 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "wlr-which-key" 3 | version = "1.3.0" 4 | edition = "2024" 5 | description = "Keymap manager for wlroots-based compositors" 6 | repository = "https://github.com/MaxVerevkin/wlr-which-key/" 7 | readme = "README.md" 8 | license = "GPL-3.0-only" 9 | authors = ["MaxVerevkin "] 10 | 11 | [dependencies] 12 | pangocairo = "0.20" 13 | anyhow = "1" 14 | libc = "0.2" 15 | indexmap = { version = "2.0", features = ["serde"] } 16 | serde = { version = "1", features = ["derive"] } 17 | serde_yaml = "0.9" 18 | wayrs-client = "1.0" 19 | wayrs-protocols = { version = "0.14", features = [ 20 | "wlr-layer-shell-unstable-v1", 21 | "keyboard-shortcuts-inhibit-unstable-v1", 22 | ] } 23 | wayrs-utils = { version = "0.17", features = [ 24 | "shm_alloc", 25 | "seats", 26 | "keyboard", 27 | ] } 28 | smart-default = "0.7.0" 29 | clap = { version = "4.3.0", default-features = false, features = [ 30 | "std", 31 | "derive", 32 | "help", 33 | "usage", 34 | ] } 35 | 36 | [profile.release] 37 | lto = "fat" 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | {project} Copyright (C) {year} {fullname} 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | 676 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # wlr-which-key 2 | 3 | Keymap manager for wlroots-based compositors. Inspired by [which-key.nvim](https://github.com/folke/which-key.nvim). 4 | 5 | ## Installation 6 | 7 | [![Packaging status](https://repology.org/badge/vertical-allrepos/wlr-which-key.svg)](https://repology.org/project/wlr-which-key/versions) 8 | 9 | ### From Source 10 | 11 | ```sh 12 | cargo install wlr-which-key --locked 13 | ``` 14 | 15 | ## Usage 16 | 17 | ```sh 18 | wlr-which-key [config_name] # Start with default menu 19 | wlr-which-key --initial-keys "p s" # Navigate to submenu or execute command 20 | ``` 21 | 22 | ## Configuration 23 | 24 | Default config file: `$XDG_CONFIG_HOME/wlr-which-key/config.yaml` or `~/.config/wlr-which-key/config.yaml`. Run `wlr-which-key --help` for more info. 25 | 26 | Keybindings may be single characters (e.g. `a`, `B`) or [xkb key labels](https://github.com/xkbcommon/libxkbcommon/blob/master/include/xkbcommon/xkbcommon-keysyms.h) (without the `XKB_KEY_` prefix, e.g. `Return`, `Insert`). Ctrl, Alt, and Mod4/Logo modifiers are supported (like `Ctrl+Return` or `Ctrl+Alt+a` or `Mod4+Return` or `Logo+Return`). A `key` may also be a list of strings, in which case a keybinding will match if any of the keys match (e.g. `key: [Left, h]`) will match both left arrow and 'h'. 27 | 28 | When executed a command will normally end the `wlr_which_key` process. If you want certain commands to keep the UI open after they execute then 29 | configure those specific commands with (`keep_open: true`). 30 | 31 | Example config: 32 | 33 | ```yaml 34 | # Theming 35 | font: JetBrainsMono Nerd Font 12 36 | background: "#282828d0" 37 | color: "#fbf1c7" 38 | border: "#8ec07c" 39 | separator: " ➜ " 40 | border_width: 2 41 | corner_r: 10 42 | padding: 15 # Defaults to corner_r 43 | rows_per_column: 5 # No limit by default 44 | column_padding: 25 # Defaults to padding 45 | 46 | # Anchor and margin 47 | anchor: center # One of center, left, right, top, bottom, bottom-left, top-left, etc. 48 | # Only relevant when anchor is not center 49 | margin_right: 0 50 | margin_bottom: 0 51 | margin_left: 0 52 | margin_top: 0 53 | 54 | # namespace to use for the layer shell surface 55 | namespace: "wlr_which_key" 56 | 57 | # Permits key bindings that conflict with compositor key bindings. 58 | # Default is `false`. 59 | inhibit_compositor_keyboard_shortcuts: true 60 | 61 | # Try to guess the correct keyboard layout to use. Default is `false`. 62 | auto_kbd_layout: true 63 | 64 | menu: 65 | - key: "p" 66 | desc: Power 67 | submenu: 68 | - key: "s" 69 | desc: Sleep 70 | cmd: systemctl suspend 71 | - key: "r" 72 | desc: Reboot 73 | cmd: reboot 74 | - key: "o" 75 | desc: Off 76 | cmd: poweroff 77 | - key: "l" 78 | desc: Laptop Screen 79 | submenu: 80 | - key: "t" 81 | desc: Toggle On/Off 82 | cmd: toggle-laptop-display.sh 83 | - key: "s" 84 | desc: Scale 85 | submenu: 86 | - key: "1" 87 | desc: Set Scale to 1.0 88 | cmd: wlr-randr --output eDP-1 --scale 1 89 | - key: "2" 90 | desc: Set Scale to 1.1 91 | cmd: wlr-randr --output eDP-1 --scale 1.1 92 | - key: "3" 93 | desc: Set Scale to 1.2 94 | cmd: wlr-randr --output eDP-1 --scale 1.2 95 | - key: "4" 96 | desc: Set Scale to 1.3 97 | cmd: wlr-randr --output eDP-1 --scale 1.3 98 | ``` 99 | 100 |
101 | Old config format (v1.1.0 and earlier) 102 | 103 | ```yaml 104 | # Theming 105 | font: JetBrainsMono Nerd Font 12 106 | background: "#282828d0" 107 | color: "#fbf1c7" 108 | border: "#8ec07c" 109 | separator: " ➜ " 110 | border_width: 2 111 | corner_r: 10 112 | padding: 15 # Defaults to corner_r 113 | 114 | # Anchor and margin 115 | anchor: center # One of center, left, right, top, bottom, bottom-left, top-left, etc. 116 | # Only relevant when anchor is not center 117 | margin_right: 0 118 | margin_bottom: 0 119 | margin_left: 0 120 | margin_top: 0 121 | 122 | menu: 123 | "w": 124 | desc: WiFi 125 | submenu: 126 | "t": { desc: Toggle, cmd: wifi_toggle.sh } 127 | "c": { desc: Connections, cmd: kitty --class nmtui-connect nmtui-connect } 128 | "p": 129 | desc: Power 130 | submenu: 131 | "s": { desc: Sleep, cmd: systemctl suspend } 132 | "r": { desc: Reboot, cmd: reboot } 133 | "o": { desc: Off, cmd: poweroff } 134 | "t": 135 | desc: Theme 136 | submenu: 137 | "d": { desc: Dark, cmd: dark-theme on } 138 | "l": { desc: Light, cmd: dark-theme off } 139 | "t": { desc: Toggle, cmd: dark-theme toggle, keep_open: true } 140 | "l": 141 | desc: Laptop Screen 142 | submenu: 143 | "t": { desc: Toggle On/Off, cmd: toggle-laptop-display.sh } 144 | "s": 145 | desc: Scale 146 | submenu: 147 | "1": { desc: Set Scale to 1.0, cmd: wlr-randr --output eDP-1 --scale 1 } 148 | "2": { desc: Set Scale to 1.1, cmd: wlr-randr --output eDP-1 --scale 1.1 } 149 | "3": { desc: Set Scale to 1.2, cmd: wlr-randr --output eDP-1 --scale 1.2 } 150 | "4": { desc: Set Scale to 1.3, cmd: wlr-randr --output eDP-1 --scale 1.3 } 151 | ``` 152 |
153 | 154 | ![image](https://user-images.githubusercontent.com/34583604/233025292-af0d5798-1854-4809-b08f-2e8f1a65b3ce.png) 155 | 156 | ![image](https://user-images.githubusercontent.com/34583604/233025368-e59a386a-6a52-4168-a6e3-5102ea6329cf.png) 157 | -------------------------------------------------------------------------------- /src/color.rs: -------------------------------------------------------------------------------- 1 | use pangocairo::cairo::Context; 2 | use serde::de; 3 | use std::fmt; 4 | use std::str::FromStr; 5 | 6 | #[derive(Debug, Clone, Copy, PartialEq)] 7 | pub struct Color { 8 | red: f64, 9 | green: f64, 10 | blue: f64, 11 | alpha: f64, 12 | } 13 | 14 | impl Color { 15 | pub const TRANSPARENT: Self = Self { 16 | red: 0.0, 17 | green: 0.0, 18 | blue: 0.0, 19 | alpha: 0.0, 20 | }; 21 | 22 | pub fn apply(self, cr: &Context) { 23 | if self.alpha.is_nan() { 24 | cr.set_source_rgb(self.red, self.green, self.blue); 25 | } else { 26 | cr.set_source_rgba(self.red, self.green, self.blue, self.alpha); 27 | } 28 | } 29 | 30 | pub fn from_rgba(r: u8, g: u8, b: u8, a: u8) -> Self { 31 | Self { 32 | red: r as f64 / 255.0, 33 | green: g as f64 / 255.0, 34 | blue: b as f64 / 255.0, 35 | alpha: if a == 255 { f64::NAN } else { a as f64 / 255.0 }, 36 | } 37 | } 38 | 39 | pub fn from_rgba_hex(hex: u32) -> Self { 40 | let r = (hex >> 24) as u8; 41 | let g = (hex >> 16) as u8; 42 | let b = (hex >> 8) as u8; 43 | let a = hex as u8; 44 | Self::from_rgba(r, g, b, a) 45 | } 46 | } 47 | 48 | impl FromStr for Color { 49 | type Err = (); 50 | 51 | fn from_str(color: &str) -> Result { 52 | let rgb = color.get(1..7).ok_or(())?; 53 | let rgb = u32::from_str_radix(rgb, 16).map_err(|_| ())?; 54 | let r = (rgb >> 16) as u8; 55 | let g = (rgb >> 8) as u8; 56 | let b = rgb as u8; 57 | 58 | let a = match color.get(7..9) { 59 | Some(a) => u8::from_str_radix(a, 16).map_err(|_| ())?, 60 | None => 255, 61 | }; 62 | 63 | Ok(Self::from_rgba(r, g, b, a)) 64 | } 65 | } 66 | 67 | impl<'de> de::Deserialize<'de> for Color { 68 | fn deserialize(deserializer: D) -> Result 69 | where 70 | D: de::Deserializer<'de>, 71 | { 72 | struct ColorVisitor; 73 | 74 | impl de::Visitor<'_> for ColorVisitor { 75 | type Value = Color; 76 | 77 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { 78 | formatter.write_str("RBG or RGBA color (in hex)") 79 | } 80 | 81 | fn visit_str(self, s: &str) -> Result 82 | where 83 | E: de::Error, 84 | { 85 | s.parse() 86 | .map_err(|_| E::custom(format!("'{s}' is not a valid RGB/RGBA color"))) 87 | } 88 | } 89 | 90 | deserializer.deserialize_str(ColorVisitor) 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/config.rs: -------------------------------------------------------------------------------- 1 | mod anchor; 2 | mod compat; 3 | mod entry; 4 | mod font; 5 | mod namespace; 6 | 7 | use std::env; 8 | use std::fs::read_to_string; 9 | use std::path::PathBuf; 10 | 11 | use anyhow::{Context, Result, bail}; 12 | use serde::Deserialize; 13 | use smart_default::SmartDefault; 14 | 15 | pub use self::anchor::ConfigAnchor; 16 | pub use self::entry::Entry; 17 | pub use self::font::Font; 18 | pub use self::namespace::Namespace; 19 | use crate::color::Color; 20 | 21 | #[derive(Deserialize, SmartDefault)] 22 | #[serde(deny_unknown_fields, default)] 23 | pub struct Config { 24 | #[default(Color::from_rgba_hex(0x282828ff))] 25 | pub background: Color, 26 | #[default(Color::from_rgba_hex(0xfbf1c7ff))] 27 | pub color: Color, 28 | #[default(Color::from_rgba_hex(0x8ec07cff))] 29 | pub border: Color, 30 | 31 | pub anchor: ConfigAnchor, 32 | pub margin_top: i32, 33 | pub margin_right: i32, 34 | pub margin_bottom: i32, 35 | pub margin_left: i32, 36 | 37 | #[default(Font::new("monospace 10"))] 38 | pub font: Font, 39 | #[default(" ➜ ".into())] 40 | pub separator: String, 41 | #[default(4.0)] 42 | pub border_width: f64, 43 | #[default(20.0)] 44 | pub corner_r: f64, 45 | pub padding: Option, 46 | pub rows_per_column: Option, 47 | pub column_padding: Option, 48 | 49 | pub inhibit_compositor_keyboard_shortcuts: bool, 50 | pub auto_kbd_layout: bool, 51 | 52 | pub menu: Vec, 53 | 54 | #[default(Namespace::new(c"wlr_which_key".to_owned()))] 55 | pub namespace: Namespace, 56 | } 57 | 58 | impl Config { 59 | pub fn new(name: &str) -> Result { 60 | let mut config_path = config_dir().context("Cound not find config directory")?; 61 | config_path.push("wlr-which-key"); 62 | config_path.push(name); 63 | config_path.set_extension("yaml"); 64 | 65 | if !config_path.exists() { 66 | bail!("config file not found: {}", config_path.display()); 67 | } 68 | 69 | let config_str = read_to_string(config_path).context("Failed to read configuration")?; 70 | 71 | match serde_yaml::from_str::(&config_str) 72 | .context("Failed to deserialize configuration") 73 | { 74 | Ok(config) => Ok(config), 75 | Err(err) => match serde_yaml::from_str::(&config_str) { 76 | Ok(compat) => { 77 | eprintln!( 78 | "Warning: using the old config format, which will be removed in a future version." 79 | ); 80 | Ok(compat.into()) 81 | } 82 | Err(_compat_err) => Err(err), 83 | }, 84 | } 85 | } 86 | 87 | pub fn padding(&self) -> f64 { 88 | self.padding.unwrap_or(self.corner_r) 89 | } 90 | 91 | pub fn column_padding(&self) -> f64 { 92 | self.column_padding.unwrap_or_else(|| self.padding()) 93 | } 94 | } 95 | 96 | fn config_dir() -> Option { 97 | env::var_os("XDG_CONFIG_HOME") 98 | .map(PathBuf::from) 99 | .or_else(|| Some(PathBuf::from(env::var_os("HOME")?).join(".config"))) 100 | } 101 | -------------------------------------------------------------------------------- /src/config/anchor.rs: -------------------------------------------------------------------------------- 1 | use serde::Deserialize; 2 | use wayrs_protocols::wlr_layer_shell_unstable_v1::zwlr_layer_surface_v1::Anchor; 3 | 4 | /// Light wrapper around `Anchor` which also supports the "no anchor" value. 5 | /// 6 | /// This type is also requires to derive `Deserialize` for the foreign type. 7 | #[derive(Deserialize, Default, Clone, Copy)] 8 | #[serde(rename_all(deserialize = "kebab-case"))] 9 | pub enum ConfigAnchor { 10 | #[default] 11 | Center, 12 | Top, 13 | Bottom, 14 | Left, 15 | Right, 16 | TopLeft, 17 | TopRight, 18 | BottomLeft, 19 | BottomRight, 20 | } 21 | 22 | /// Convert this anchor into the type expected by `wayrs`. 23 | impl From for Anchor { 24 | fn from(value: ConfigAnchor) -> Self { 25 | match value { 26 | ConfigAnchor::Center => Anchor::empty(), 27 | ConfigAnchor::Top => Anchor::Top, 28 | ConfigAnchor::Bottom => Anchor::Bottom, 29 | ConfigAnchor::Left => Anchor::Left, 30 | ConfigAnchor::Right => Anchor::Right, 31 | ConfigAnchor::TopLeft => Anchor::Top | Anchor::Left, 32 | ConfigAnchor::TopRight => Anchor::Top | Anchor::Right, 33 | ConfigAnchor::BottomLeft => Anchor::Bottom | Anchor::Left, 34 | ConfigAnchor::BottomRight => Anchor::Bottom | Anchor::Right, 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/config/compat.rs: -------------------------------------------------------------------------------- 1 | use indexmap::IndexMap; 2 | use serde::Deserialize; 3 | use smart_default::SmartDefault; 4 | 5 | use crate::color::Color; 6 | use crate::key::SingleKey; 7 | 8 | use super::{ConfigAnchor, Font, Namespace}; 9 | 10 | #[derive(Deserialize, Default)] 11 | #[serde(transparent)] 12 | pub struct Entries(pub IndexMap); 13 | 14 | #[derive(Deserialize, SmartDefault)] 15 | #[serde(deny_unknown_fields, default)] 16 | pub struct Config { 17 | #[default(Color::from_rgba_hex(0x282828ff))] 18 | pub background: Color, 19 | #[default(Color::from_rgba_hex(0xfbf1c7ff))] 20 | pub color: Color, 21 | #[default(Color::from_rgba_hex(0x8ec07cff))] 22 | pub border: Color, 23 | 24 | pub anchor: ConfigAnchor, 25 | pub margin_top: i32, 26 | pub margin_right: i32, 27 | pub margin_bottom: i32, 28 | pub margin_left: i32, 29 | 30 | #[default(Font::new("monospace 10"))] 31 | pub font: Font, 32 | #[default(" ➜ ".into())] 33 | pub separator: String, 34 | #[default(4.0)] 35 | pub border_width: f64, 36 | #[default(20.0)] 37 | pub corner_r: f64, 38 | // defaults to `corner_r` 39 | pub padding: Option, 40 | 41 | pub menu: Entries, 42 | } 43 | 44 | #[derive(Deserialize)] 45 | #[serde(untagged, deny_unknown_fields)] 46 | pub enum Entry { 47 | Cmd { 48 | cmd: String, 49 | desc: String, 50 | #[serde(default)] 51 | keep_open: bool, 52 | }, 53 | Recursive { 54 | submenu: Entries, 55 | desc: String, 56 | }, 57 | } 58 | 59 | impl From for super::Config { 60 | fn from(value: Config) -> Self { 61 | fn map_entries(value: Entries) -> Vec { 62 | value 63 | .0 64 | .into_iter() 65 | .map(|(key, entry)| match entry { 66 | Entry::Cmd { 67 | cmd, 68 | desc, 69 | keep_open, 70 | } => super::Entry::Cmd { 71 | key: key.into(), 72 | cmd, 73 | desc, 74 | keep_open, 75 | }, 76 | Entry::Recursive { submenu, desc } => super::Entry::Recursive { 77 | key: key.into(), 78 | submenu: map_entries(submenu), 79 | desc, 80 | }, 81 | }) 82 | .collect() 83 | } 84 | 85 | Self { 86 | background: value.background, 87 | color: value.color, 88 | border: value.border, 89 | anchor: value.anchor, 90 | margin_top: value.margin_top, 91 | margin_right: value.margin_right, 92 | margin_bottom: value.margin_bottom, 93 | margin_left: value.margin_left, 94 | font: value.font, 95 | separator: value.separator, 96 | border_width: value.border_width, 97 | corner_r: value.corner_r, 98 | padding: value.padding, 99 | rows_per_column: None, 100 | column_padding: None, 101 | menu: map_entries(value.menu), 102 | inhibit_compositor_keyboard_shortcuts: false, 103 | auto_kbd_layout: false, 104 | namespace: Namespace::new(c"wlr_which_key".to_owned()), 105 | } 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /src/config/entry.rs: -------------------------------------------------------------------------------- 1 | use anyhow::{Context, bail}; 2 | use serde::Deserialize; 3 | 4 | use crate::key::Key; 5 | 6 | #[derive(Deserialize)] 7 | #[serde(try_from = "RawEntry")] 8 | pub enum Entry { 9 | Cmd { 10 | key: Key, 11 | cmd: String, 12 | desc: String, 13 | keep_open: bool, 14 | }, 15 | Recursive { 16 | key: Key, 17 | submenu: Vec, 18 | desc: String, 19 | }, 20 | } 21 | 22 | #[derive(Deserialize)] 23 | #[serde(deny_unknown_fields)] 24 | struct RawEntry { 25 | key: Key, 26 | desc: String, 27 | cmd: Option, 28 | keep_open: Option, 29 | submenu: Option>, 30 | } 31 | 32 | impl TryFrom for Entry { 33 | type Error = anyhow::Error; 34 | 35 | fn try_from(value: RawEntry) -> Result { 36 | if let Some(submenu) = value.submenu { 37 | if value.cmd.is_some() { 38 | bail!("cannot have both 'submenu' and 'cmd'"); 39 | } 40 | if value.keep_open.is_some() { 41 | bail!("cannot have both 'submenu' and 'keep_open'"); 42 | } 43 | Ok(Self::Recursive { 44 | key: value.key, 45 | submenu, 46 | desc: value.desc, 47 | }) 48 | } else { 49 | Ok(Self::Cmd { 50 | key: value.key, 51 | cmd: value 52 | .cmd 53 | .context("either or 'submenu' or 'cmd' is required")?, 54 | desc: value.desc, 55 | keep_open: value.keep_open.unwrap_or(false), 56 | }) 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/config/font.rs: -------------------------------------------------------------------------------- 1 | use std::fmt; 2 | 3 | use pangocairo::pango::FontDescription; 4 | use serde::de; 5 | 6 | pub struct Font(pub FontDescription); 7 | 8 | impl Font { 9 | pub fn new(desc: &str) -> Self { 10 | Self(FontDescription::from_string(desc)) 11 | } 12 | } 13 | 14 | impl<'de> de::Deserialize<'de> for Font { 15 | fn deserialize(deserializer: D) -> Result 16 | where 17 | D: de::Deserializer<'de>, 18 | { 19 | struct FontVisitor; 20 | 21 | impl de::Visitor<'_> for FontVisitor { 22 | type Value = Font; 23 | 24 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { 25 | formatter.write_str("font description") 26 | } 27 | 28 | fn visit_str(self, s: &str) -> Result 29 | where 30 | E: de::Error, 31 | { 32 | Ok(Font::new(s)) 33 | } 34 | } 35 | 36 | deserializer.deserialize_str(FontVisitor) 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/config/namespace.rs: -------------------------------------------------------------------------------- 1 | use std::{ 2 | ffi::{CString, NulError}, 3 | str::FromStr, 4 | }; 5 | 6 | use serde::de; 7 | 8 | #[derive(Default)] 9 | pub struct Namespace(pub CString); 10 | 11 | impl Namespace { 12 | pub fn new(namespace: CString) -> Self { 13 | Self(namespace) 14 | } 15 | } 16 | 17 | impl FromStr for Namespace { 18 | type Err = NulError; 19 | 20 | fn from_str(s: &str) -> Result { 21 | Ok(Self::new(CString::new(s)?)) 22 | } 23 | } 24 | 25 | impl<'de> de::Deserialize<'de> for Namespace { 26 | fn deserialize(deserializer: D) -> Result 27 | where 28 | D: de::Deserializer<'de>, 29 | { 30 | struct NamespaceVisitor; 31 | 32 | impl de::Visitor<'_> for NamespaceVisitor { 33 | type Value = Namespace; 34 | 35 | fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { 36 | formatter.write_str("namespace name") 37 | } 38 | 39 | fn visit_str(self, s: &str) -> Result 40 | where 41 | E: de::Error, 42 | { 43 | s.parse().map_err(|_| { 44 | E::custom(format!("'{}' contains a null character", s.escape_debug())) 45 | }) 46 | } 47 | } 48 | 49 | deserializer.deserialize_str(NamespaceVisitor) 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/key.rs: -------------------------------------------------------------------------------- 1 | use std::fmt; 2 | use std::str::FromStr; 3 | 4 | use serde::de; 5 | use wayrs_utils::keyboard::xkb; 6 | 7 | #[derive(Clone)] 8 | pub struct Key { 9 | any_of: Vec, 10 | } 11 | 12 | #[derive(Clone, Copy, PartialEq, Eq, Hash, Default)] 13 | pub struct ModifierState { 14 | pub mod_ctrl: bool, 15 | pub mod_alt: bool, 16 | pub mod_mod4: bool, 17 | } 18 | 19 | impl ModifierState { 20 | pub fn from_xkb_state(xkb: &xkb::State) -> Self { 21 | Self { 22 | mod_ctrl: xkb.mod_name_is_active(xkb::MOD_NAME_CTRL, xkb::STATE_MODS_EFFECTIVE), 23 | mod_alt: xkb.mod_name_is_active(xkb::MOD_NAME_ALT, xkb::STATE_MODS_EFFECTIVE), 24 | mod_mod4: xkb.mod_name_is_active(xkb::MOD_NAME_LOGO, xkb::STATE_MODS_EFFECTIVE), 25 | } 26 | } 27 | } 28 | 29 | #[derive(Clone, PartialEq, Eq, Hash)] 30 | pub struct SingleKey { 31 | pub keysym: xkb::Keysym, 32 | pub repr: String, 33 | pub modifiers: ModifierState, 34 | } 35 | 36 | impl Key { 37 | pub fn matches(&self, sym: xkb::Keysym, modifiers: ModifierState) -> bool { 38 | self.any_of 39 | .iter() 40 | .any(|key| key.modifiers == modifiers && key.keysym == sym) 41 | } 42 | } 43 | 44 | impl fmt::Display for Key { 45 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 46 | for (i, key) in self.any_of.iter().enumerate() { 47 | f.write_str(&key.repr)?; 48 | if i + 1 != self.any_of.len() { 49 | f.write_str(" | ")? 50 | } 51 | } 52 | Ok(()) 53 | } 54 | } 55 | 56 | impl From for Key { 57 | fn from(value: SingleKey) -> Self { 58 | Self { 59 | any_of: vec![value], 60 | } 61 | } 62 | } 63 | 64 | impl FromStr for SingleKey { 65 | type Err = String; 66 | 67 | fn from_str(s: &str) -> Result { 68 | if s == "+" { 69 | return Ok(Self { 70 | keysym: xkb::Keysym::plus, 71 | repr: String::from("+"), 72 | modifiers: Default::default(), 73 | }); 74 | } 75 | 76 | let mut components = s.split('+'); 77 | let key = components.next_back().unwrap_or(s); 78 | let keysym = to_keysym(key).ok_or_else(|| format!("invalid key '{key}'"))?; 79 | 80 | let mut modifiers = ModifierState::default(); 81 | for modifier in components { 82 | if modifier.eq_ignore_ascii_case("ctrl") { 83 | modifiers.mod_ctrl = true; 84 | } else if modifier.eq_ignore_ascii_case("alt") { 85 | modifiers.mod_alt = true; 86 | } else if modifier.eq_ignore_ascii_case("mod4") || modifier.eq_ignore_ascii_case("logo") 87 | { 88 | modifiers.mod_mod4 = true; 89 | } else { 90 | return Err(format!("unknown modifier '{modifier}")); 91 | } 92 | } 93 | 94 | Ok(Self { 95 | keysym, 96 | repr: s.to_owned(), 97 | modifiers, 98 | }) 99 | } 100 | } 101 | 102 | fn to_keysym(s: &str) -> Option { 103 | let mut chars = s.chars(); 104 | let first_char = chars.next()?; 105 | 106 | let keysym = if chars.next().is_none() { 107 | xkb::utf32_to_keysym(first_char as u32) 108 | } else { 109 | xkb::keysym_from_name(s, xkb::KEYSYM_NO_FLAGS) 110 | }; 111 | 112 | if keysym.raw() == xkb::keysyms::KEY_NoSymbol { 113 | None 114 | } else { 115 | Some(keysym) 116 | } 117 | } 118 | 119 | impl<'de> de::Deserialize<'de> for Key { 120 | fn deserialize(deserializer: D) -> Result 121 | where 122 | D: de::Deserializer<'de>, 123 | { 124 | struct KeyVisitor; 125 | 126 | impl<'de> de::Visitor<'de> for KeyVisitor { 127 | type Value = Key; 128 | 129 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { 130 | formatter.write_str("a key or a list of keys") 131 | } 132 | 133 | fn visit_str(self, s: &str) -> Result 134 | where 135 | E: de::Error, 136 | { 137 | Ok(Key::from(s.parse::().map_err(E::custom)?)) 138 | } 139 | 140 | fn visit_seq(self, mut seq: A) -> Result 141 | where 142 | A: de::SeqAccess<'de>, 143 | { 144 | let mut any_of = Vec::new(); 145 | while let Some(next) = seq.next_element()? { 146 | any_of.push(next); 147 | } 148 | Ok(Key { any_of }) 149 | } 150 | } 151 | 152 | deserializer.deserialize_any(KeyVisitor) 153 | } 154 | } 155 | 156 | impl<'de> de::Deserialize<'de> for SingleKey { 157 | fn deserialize(deserializer: D) -> Result 158 | where 159 | D: de::Deserializer<'de>, 160 | { 161 | struct KeyVisitor; 162 | 163 | impl de::Visitor<'_> for KeyVisitor { 164 | type Value = SingleKey; 165 | 166 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { 167 | formatter.write_str("a key") 168 | } 169 | 170 | fn visit_str(self, s: &str) -> Result 171 | where 172 | E: de::Error, 173 | { 174 | s.parse().map_err(E::custom) 175 | } 176 | } 177 | 178 | deserializer.deserialize_str(KeyVisitor) 179 | } 180 | } 181 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | mod color; 2 | mod config; 3 | mod key; 4 | mod menu; 5 | mod text; 6 | 7 | use std::collections::{HashMap, HashSet}; 8 | use std::f64::consts::{FRAC_PI_2, PI, TAU}; 9 | use std::io; 10 | use std::os::fd::{AsRawFd, RawFd}; 11 | use std::os::unix::process::CommandExt; 12 | use std::process::{Command, Stdio}; 13 | use std::sync::LazyLock; 14 | use std::time::Duration; 15 | 16 | use anyhow::bail; 17 | use clap::Parser; 18 | use pangocairo::cairo; 19 | 20 | use wayrs_client::object::ObjectId; 21 | use wayrs_client::protocol::*; 22 | use wayrs_client::proxy::Proxy; 23 | use wayrs_client::{Connection, IoMode}; 24 | use wayrs_client::{EventCtx, global::*}; 25 | use wayrs_protocols::keyboard_shortcuts_inhibit_unstable_v1::*; 26 | use wayrs_protocols::wlr_layer_shell_unstable_v1::*; 27 | use wayrs_utils::keyboard::{Keyboard, KeyboardEvent, KeyboardHandler, xkb}; 28 | use wayrs_utils::seats::{SeatHandler, Seats}; 29 | use wayrs_utils::shm_alloc::{BufferSpec, ShmAlloc}; 30 | use wayrs_utils::timer::Timer; 31 | 32 | use crate::key::ModifierState; 33 | 34 | #[derive(Debug, Parser)] 35 | #[command(author, version, about)] 36 | struct Args { 37 | /// The name of the config file to use. 38 | /// 39 | /// By default, $XDG_CONFIG_HOME/wlr-which-key/config.yaml or 40 | /// ~/.config/wlr-which-key/config.yaml is used. 41 | /// 42 | /// For example, to use ~/.config/wlr-which-key/print-srceen.yaml, set this to 43 | /// "print-srceen". An absolute path can be used too, extension is optional. 44 | config: Option, 45 | 46 | /// Initial key sequence to navigate to a specific submenu on startup. 47 | /// 48 | /// Provide a sequence of keys separated by spaces to navigate directly to a submenu. 49 | /// For example, "p s" would navigate to the submenu at key 'p', then 's'. 50 | /// The application will show an error and exit if the key sequence is invalid. 51 | #[arg(long, short = 'k')] 52 | initial_keys: Option, 53 | } 54 | 55 | static DEBUG_LAYOUT: LazyLock = 56 | LazyLock::new(|| std::env::var("WLR_WHICH_KEY_LAYOUT_DEBUG").as_deref() == Ok("1")); 57 | 58 | fn main() -> anyhow::Result<()> { 59 | let args = Args::parse(); 60 | let config = config::Config::new(args.config.as_deref().unwrap_or("config"))?; 61 | let mut menu = menu::Menu::new(&config)?; 62 | 63 | if let Some(initial_keys) = &args.initial_keys 64 | && let Some(initial_action) = menu.navigate_to_key_sequence(initial_keys)? 65 | { 66 | match initial_action { 67 | menu::Action::Submenu(_) => unreachable!(), 68 | menu::Action::Quit => return Ok(()), 69 | menu::Action::Exec { cmd, keep_open } => { 70 | if keep_open { 71 | bail!("Initial key sequence cannot trigger an action with keep_open=true"); 72 | } 73 | exec(&cmd); 74 | return Ok(()); 75 | } 76 | } 77 | } 78 | 79 | let mut conn = Connection::connect()?; 80 | conn.blocking_roundtrip()?; 81 | conn.add_registry_cb(wl_registry_cb); 82 | 83 | let wl_compositor: WlCompositor = conn.bind_singleton(4..=6)?; 84 | let wlr_layer_shell: ZwlrLayerShellV1 = conn.bind_singleton(2)?; 85 | let keyboard_shortcuts_inhibit_manager = match config.inhibit_compositor_keyboard_shortcuts { 86 | true => Some(conn.bind_singleton(1)?), 87 | false => None, 88 | }; 89 | 90 | let seats = Seats::new(&mut conn); 91 | let shm_alloc = ShmAlloc::bind(&mut conn)?; 92 | 93 | let width = menu.width(&config) as u32; 94 | let height = menu.height(&config) as u32; 95 | 96 | let wl_surface = wl_compositor.create_surface_with_cb(&mut conn, wl_surface_cb); 97 | 98 | let layer_surface = wlr_layer_shell.get_layer_surface_with_cb( 99 | &mut conn, 100 | wl_surface, 101 | None, 102 | zwlr_layer_shell_v1::Layer::Overlay, 103 | config.namespace.0.to_owned(), 104 | layer_surface_cb, 105 | ); 106 | layer_surface.set_anchor(&mut conn, config.anchor.into()); 107 | layer_surface.set_size(&mut conn, width, height); 108 | layer_surface.set_margin( 109 | &mut conn, 110 | config.margin_top, 111 | config.margin_right, 112 | config.margin_bottom, 113 | config.margin_left, 114 | ); 115 | layer_surface.set_keyboard_interactivity( 116 | &mut conn, 117 | zwlr_layer_surface_v1::KeyboardInteractivity::Exclusive, 118 | ); 119 | wl_surface.commit(&mut conn); 120 | 121 | let mut state = State { 122 | shm_alloc, 123 | seats, 124 | keyboards: Vec::new(), 125 | kbd_repeat: None, 126 | outputs: Vec::new(), 127 | keyboard_shortcuts_inhibit_manager, 128 | keyboard_shortcuts_inhibitors: HashMap::new(), 129 | 130 | wl_surface, 131 | layer_surface, 132 | visible_on_outputs: HashSet::new(), 133 | surface_scale: 1, 134 | exit: false, 135 | configured: false, 136 | width, 137 | height, 138 | throttle_cb: None, 139 | throttled: false, 140 | 141 | menu, 142 | config, 143 | }; 144 | 145 | while !state.exit { 146 | conn.flush(IoMode::Blocking)?; 147 | 148 | poll( 149 | conn.as_raw_fd(), 150 | state.kbd_repeat.as_ref().map(|x| x.0.sleep()), 151 | )?; 152 | 153 | if let Some((timer, action)) = &mut state.kbd_repeat 154 | && timer.tick() 155 | { 156 | let action = action.clone(); 157 | state.handle_action(&mut conn, action); 158 | } 159 | 160 | match conn.recv_events(IoMode::NonBlocking) { 161 | Ok(()) => conn.dispatch_events(&mut state), 162 | Err(e) if e.kind() == io::ErrorKind::WouldBlock => (), 163 | Err(e) => return Err(e.into()), 164 | } 165 | } 166 | 167 | Ok(()) 168 | } 169 | 170 | struct State { 171 | shm_alloc: ShmAlloc, 172 | seats: Seats, 173 | keyboards: Vec, 174 | kbd_repeat: Option<(Timer, menu::Action)>, 175 | outputs: Vec, 176 | keyboard_shortcuts_inhibit_manager: Option, 177 | keyboard_shortcuts_inhibitors: HashMap, 178 | 179 | wl_surface: WlSurface, 180 | layer_surface: ZwlrLayerSurfaceV1, 181 | visible_on_outputs: HashSet, 182 | surface_scale: u32, 183 | exit: bool, 184 | configured: bool, 185 | width: u32, 186 | height: u32, 187 | throttle_cb: Option, 188 | throttled: bool, 189 | 190 | menu: menu::Menu, 191 | config: config::Config, 192 | } 193 | 194 | struct Output { 195 | wl: WlOutput, 196 | reg_name: u32, 197 | scale: u32, 198 | } 199 | 200 | impl State { 201 | fn draw(&mut self, conn: &mut Connection) { 202 | if !self.configured { 203 | return; 204 | } 205 | 206 | if self.throttle_cb.is_some() { 207 | self.throttled = true; 208 | return; 209 | } 210 | 211 | self.throttle_cb = Some(self.wl_surface.frame_with_cb(conn, |ctx| { 212 | assert_eq!(ctx.state.throttle_cb, Some(ctx.proxy)); 213 | ctx.state.throttle_cb = None; 214 | if ctx.state.throttled { 215 | ctx.state.throttled = false; 216 | ctx.state.draw(ctx.conn); 217 | } 218 | })); 219 | 220 | let scale = if self.wl_surface.version() >= 6 { 221 | self.surface_scale 222 | } else { 223 | self.outputs 224 | .iter() 225 | .filter(|o| self.visible_on_outputs.contains(&o.wl.id())) 226 | .map(|o| o.scale) 227 | .max() 228 | .unwrap_or(1) 229 | }; 230 | 231 | let width_f = self.width as f64; 232 | let height_f = self.height as f64; 233 | 234 | let (buffer, canvas) = self 235 | .shm_alloc 236 | .alloc_buffer( 237 | conn, 238 | BufferSpec { 239 | width: self.width * scale, 240 | height: self.height * scale, 241 | stride: self.width * 4 * scale, 242 | format: wl_shm::Format::Argb8888, 243 | }, 244 | ) 245 | .expect("could not allocate frame shm buffer"); 246 | 247 | let cairo_surf = unsafe { 248 | cairo::ImageSurface::create_for_data_unsafe( 249 | canvas.as_mut_ptr(), 250 | cairo::Format::ARgb32, 251 | (self.width * scale) as i32, 252 | (self.height * scale) as i32, 253 | (self.width * 4 * scale) as i32, 254 | ) 255 | .expect("cairo surface") 256 | }; 257 | 258 | let cairo_ctx = cairo::Context::new(&cairo_surf).expect("cairo context"); 259 | cairo_ctx.scale(scale as f64, scale as f64); 260 | self.wl_surface.set_buffer_scale(conn, scale as i32); 261 | 262 | // background with rounded corners 263 | cairo_ctx.save().unwrap(); 264 | cairo_ctx.set_operator(cairo::Operator::Source); 265 | color::Color::TRANSPARENT.apply(&cairo_ctx); 266 | cairo_ctx.paint().unwrap(); 267 | cairo_ctx.restore().unwrap(); 268 | 269 | cairo_ctx.new_sub_path(); 270 | let half_border = self.config.border_width * 0.5; 271 | let r = self.config.corner_r; 272 | cairo_ctx.arc(r + half_border, r + half_border, r, PI, 3.0 * FRAC_PI_2); 273 | cairo_ctx.arc( 274 | width_f - r - half_border, 275 | r + half_border, 276 | r, 277 | 3.0 * FRAC_PI_2, 278 | TAU, 279 | ); 280 | cairo_ctx.arc( 281 | width_f - r - half_border, 282 | height_f - r - half_border, 283 | r, 284 | 0.0, 285 | FRAC_PI_2, 286 | ); 287 | cairo_ctx.arc( 288 | r + half_border, 289 | height_f - r - half_border, 290 | r, 291 | FRAC_PI_2, 292 | PI, 293 | ); 294 | cairo_ctx.close_path(); 295 | self.config.background.apply(&cairo_ctx); 296 | cairo_ctx.fill_preserve().unwrap(); 297 | self.config.border.apply(&cairo_ctx); 298 | cairo_ctx.set_line_width(self.config.border_width); 299 | cairo_ctx.stroke().unwrap(); 300 | 301 | // draw our menu 302 | self.menu.render(&self.config, &cairo_ctx).unwrap(); 303 | 304 | // Damage the entire window 305 | self.wl_surface.damage_buffer( 306 | conn, 307 | 0, 308 | 0, 309 | (self.width * scale) as i32, 310 | (self.height * scale) as i32, 311 | ); 312 | 313 | // Attach and commit to present. 314 | self.wl_surface 315 | .attach(conn, Some(buffer.into_wl_buffer()), 0, 0); 316 | self.wl_surface.commit(conn); 317 | } 318 | 319 | fn handle_action(&mut self, conn: &mut Connection, action: menu::Action) { 320 | match action { 321 | menu::Action::Quit => { 322 | self.exit = true; 323 | conn.break_dispatch_loop(); 324 | } 325 | menu::Action::Exec { cmd, keep_open } => { 326 | exec(&cmd); 327 | if !keep_open { 328 | self.exit = true; 329 | conn.break_dispatch_loop(); 330 | } 331 | } 332 | menu::Action::Submenu(page) => { 333 | self.menu.set_page(page); 334 | self.width = self.menu.width(&self.config) as u32; 335 | self.height = self.menu.height(&self.config) as u32; 336 | self.layer_surface.set_size(conn, self.width, self.height); 337 | self.wl_surface.commit(conn); 338 | } 339 | } 340 | } 341 | } 342 | 343 | impl SeatHandler for State { 344 | fn get_seats(&mut self) -> &mut Seats { 345 | &mut self.seats 346 | } 347 | 348 | fn seat_added(&mut self, conn: &mut Connection, seat: WlSeat) { 349 | if let Some(inhibit_manager) = self.keyboard_shortcuts_inhibit_manager { 350 | self.keyboard_shortcuts_inhibitors.insert( 351 | seat, 352 | inhibit_manager.inhibit_shortcuts(conn, self.wl_surface, seat), 353 | ); 354 | } 355 | } 356 | 357 | fn seat_removed(&mut self, conn: &mut Connection, seat: WlSeat) { 358 | if let Some(inhibitor) = self.keyboard_shortcuts_inhibitors.remove(&seat) { 359 | inhibitor.destroy(conn); 360 | } 361 | } 362 | 363 | fn keyboard_added(&mut self, conn: &mut Connection, seat: WlSeat) { 364 | self.keyboards.push(Keyboard::new(conn, seat)); 365 | } 366 | 367 | fn keyboard_removed(&mut self, conn: &mut Connection, seat: WlSeat) { 368 | let i = self 369 | .keyboards 370 | .iter() 371 | .position(|k| k.seat() == seat) 372 | .unwrap(); 373 | let keyboard = self.keyboards.swap_remove(i); 374 | keyboard.destroy(conn); 375 | } 376 | } 377 | 378 | impl KeyboardHandler for State { 379 | fn get_keyboard(&mut self, wl_keyboard: WlKeyboard) -> &mut Keyboard { 380 | self.keyboards 381 | .iter_mut() 382 | .find(|k| k.wl_keyboard() == wl_keyboard) 383 | .unwrap() 384 | } 385 | 386 | fn key_presed(&mut self, conn: &mut Connection, event: KeyboardEvent) { 387 | self.kbd_repeat = None; 388 | let modifiers = ModifierState::from_xkb_state(&event.xkb_state); 389 | let action = if let Some(action) = self.menu.get_action(modifiers, event.keysym) { 390 | Some(action) 391 | } else if self.config.auto_kbd_layout { 392 | let mask = XkbMaskState::new(&event.xkb_state); 393 | let mut action = None; 394 | // Try each layout 395 | for layout in 0..event.xkb_state.get_keymap().num_layouts() { 396 | mask.with_locked_layout(layout).apply(&event.xkb_state); 397 | if let Some(a) = self 398 | .menu 399 | .get_action(modifiers, event.xkb_state.key_get_one_sym(event.keycode)) 400 | { 401 | action = Some(a); 402 | break; 403 | } 404 | } 405 | mask.apply(&event.xkb_state); // Restore the state 406 | action 407 | } else { 408 | None 409 | }; 410 | if let Some(action) = action { 411 | if let Some(repeat) = event.repeat_info { 412 | self.kbd_repeat = Some((Timer::new(repeat.delay, repeat.interval), action.clone())); 413 | } 414 | self.handle_action(conn, action); 415 | } 416 | } 417 | 418 | fn key_released(&mut self, _: &mut Connection, _: KeyboardEvent) { 419 | self.kbd_repeat = None; 420 | } 421 | } 422 | 423 | fn wl_registry_cb(conn: &mut Connection, state: &mut State, event: &wl_registry::Event) { 424 | match event { 425 | wl_registry::Event::Global(g) if g.is::() => { 426 | state.outputs.push(Output { 427 | wl: g.bind_with_cb(conn, 1..=4, wl_output_cb).unwrap(), 428 | reg_name: g.name, 429 | scale: 1, 430 | }); 431 | } 432 | wl_registry::Event::GlobalRemove(name) => { 433 | if let Some(output_i) = state.outputs.iter().position(|o| o.reg_name == *name) { 434 | let output = state.outputs.swap_remove(output_i); 435 | state.visible_on_outputs.remove(&output.wl.id()); 436 | if output.wl.version() >= 3 { 437 | output.wl.release(conn); 438 | } 439 | } 440 | } 441 | _ => (), 442 | } 443 | } 444 | 445 | fn wl_output_cb(ctx: EventCtx) { 446 | if let wl_output::Event::Scale(scale) = ctx.event { 447 | let output = ctx 448 | .state 449 | .outputs 450 | .iter_mut() 451 | .find(|o| o.wl == ctx.proxy) 452 | .unwrap(); 453 | let scale: u32 = scale.try_into().unwrap(); 454 | if output.scale != scale { 455 | output.scale = scale; 456 | ctx.state.draw(ctx.conn); 457 | } 458 | } 459 | } 460 | 461 | fn wl_surface_cb(ctx: EventCtx) { 462 | assert_eq!(ctx.proxy, ctx.state.wl_surface); 463 | match ctx.event { 464 | wl_surface::Event::Enter(output) => { 465 | ctx.state.visible_on_outputs.insert(output); 466 | ctx.state.draw(ctx.conn); 467 | } 468 | wl_surface::Event::Leave(output) => { 469 | ctx.state.visible_on_outputs.remove(&output); 470 | } 471 | wl_surface::Event::PreferredBufferScale(scale) => { 472 | assert!(scale >= 1); 473 | let scale = scale as u32; 474 | if ctx.state.surface_scale != scale { 475 | ctx.state.surface_scale = scale; 476 | ctx.state.draw(ctx.conn); 477 | } 478 | } 479 | _ => (), 480 | } 481 | } 482 | 483 | fn layer_surface_cb(ctx: EventCtx) { 484 | assert_eq!(ctx.proxy, ctx.state.layer_surface); 485 | match ctx.event { 486 | zwlr_layer_surface_v1::Event::Configure(args) => { 487 | if args.width != 0 { 488 | ctx.state.width = args.width; 489 | } 490 | if args.height != 0 { 491 | ctx.state.height = args.height; 492 | } 493 | ctx.state.configured = true; 494 | ctx.proxy.ack_configure(ctx.conn, args.serial); 495 | ctx.state.draw(ctx.conn); 496 | } 497 | zwlr_layer_surface_v1::Event::Closed => { 498 | ctx.state.exit = true; 499 | ctx.conn.break_dispatch_loop(); 500 | } 501 | _ => (), 502 | } 503 | } 504 | 505 | fn poll(fd: RawFd, timeout: Option) -> io::Result<()> { 506 | let mut fds = [libc::pollfd { 507 | fd, 508 | events: libc::POLLIN, 509 | revents: 0, 510 | }]; 511 | let res = unsafe { 512 | libc::poll( 513 | fds.as_mut_ptr(), 514 | 1, 515 | timeout.map_or(-1, |t| t.as_millis() as _), 516 | ) 517 | }; 518 | match res { 519 | -1 => Err(io::Error::last_os_error()), 520 | _ => Ok(()), 521 | } 522 | } 523 | 524 | fn exec(cmd: &str) { 525 | let mut proc = Command::new("sh"); 526 | proc.args(["-c", cmd]); 527 | proc.stdin(Stdio::null()); 528 | proc.stdout(Stdio::null()); 529 | // Safety: libc::daemon() is async-signal-safe 530 | unsafe { 531 | proc.pre_exec(|| match libc::daemon(1, 0) { 532 | -1 => Err(io::Error::other("Failed to detach new process")), 533 | _ => Ok(()), 534 | }); 535 | } 536 | proc.spawn().unwrap().wait().unwrap(); 537 | } 538 | 539 | #[derive(Clone, Copy)] 540 | struct XkbMaskState { 541 | depressed_mods: u32, 542 | latched_mods: u32, 543 | locked_mods: u32, 544 | depressed_layout: u32, 545 | latched_layout: u32, 546 | locked_layout: u32, 547 | } 548 | 549 | impl XkbMaskState { 550 | fn new(xkb_state: &xkb::State) -> Self { 551 | Self { 552 | depressed_mods: xkb_state.serialize_mods(xkb::STATE_MODS_DEPRESSED), 553 | latched_mods: xkb_state.serialize_mods(xkb::STATE_MODS_LATCHED), 554 | locked_mods: xkb_state.serialize_mods(xkb::STATE_MODS_LOCKED), 555 | depressed_layout: xkb_state.serialize_layout(xkb::STATE_LAYOUT_DEPRESSED), 556 | latched_layout: xkb_state.serialize_layout(xkb::STATE_LAYOUT_LATCHED), 557 | locked_layout: xkb_state.serialize_layout(xkb::STATE_LAYOUT_LOCKED), 558 | } 559 | } 560 | 561 | fn with_locked_layout(&self, locked_layout: u32) -> Self { 562 | Self { 563 | locked_layout, 564 | ..*self 565 | } 566 | } 567 | 568 | fn apply(&self, xkb_state: &xkb::State) { 569 | // Hack: this is just ref counting, no actual cloning. `update_mask` should probably just 570 | // accept `&self` instead of `&mut self`. 571 | let mut xkb_state = xkb_state.clone(); 572 | xkb_state.update_mask( 573 | self.depressed_mods, 574 | self.latched_mods, 575 | self.locked_mods, 576 | self.depressed_layout, 577 | self.latched_layout, 578 | self.locked_layout, 579 | ); 580 | } 581 | } 582 | -------------------------------------------------------------------------------- /src/menu.rs: -------------------------------------------------------------------------------- 1 | use std::str::FromStr; 2 | 3 | use anyhow::{Error, Result, bail}; 4 | use pangocairo::{cairo, pango}; 5 | use wayrs_utils::keyboard::xkb; 6 | 7 | use crate::DEBUG_LAYOUT; 8 | use crate::color::Color; 9 | use crate::config::{self, Config}; 10 | use crate::key::{Key, ModifierState, SingleKey}; 11 | use crate::text::{self, ComputedText}; 12 | 13 | pub struct Menu { 14 | pages: Vec, 15 | cur_page: usize, 16 | separator: ComputedText, 17 | } 18 | 19 | struct MenuPage { 20 | item_height: f64, 21 | columns: Vec, 22 | parent: Option, 23 | } 24 | 25 | struct MenuColumn { 26 | key_col_width: f64, 27 | val_col_width: f64, 28 | items: Vec, 29 | } 30 | 31 | struct MenuItem { 32 | action: Action, 33 | key_comp: ComputedText, 34 | val_comp: ComputedText, 35 | key: Key, 36 | } 37 | 38 | #[derive(Clone)] 39 | pub enum Action { 40 | Quit, 41 | Exec { cmd: String, keep_open: bool }, 42 | Submenu(usize), 43 | } 44 | 45 | impl Menu { 46 | pub fn new(config: &Config) -> Result { 47 | let context = pango::Context::new(); 48 | let fontmap = pangocairo::FontMap::new(); 49 | context.set_font_map(Some(&fontmap)); 50 | 51 | let mut this = Self { 52 | pages: Vec::new(), 53 | cur_page: 0, 54 | separator: ComputedText::new(&config.separator, &context, &config.font.0), 55 | }; 56 | 57 | this.push_page(&context, &config.menu, config, None)?; 58 | 59 | Ok(this) 60 | } 61 | 62 | fn push_page( 63 | &mut self, 64 | context: &pango::Context, 65 | entries: &[config::Entry], 66 | config: &Config, 67 | parent: Option, 68 | ) -> Result { 69 | if entries.is_empty() { 70 | bail!("Empty menu pages are not allowed"); 71 | } 72 | 73 | let cur_page = self.pages.len(); 74 | 75 | self.pages.push(MenuPage { 76 | item_height: self.separator.height, 77 | columns: Vec::new(), 78 | parent, 79 | }); 80 | 81 | for (entry_i, entry) in entries.iter().enumerate() { 82 | let item = match entry { 83 | config::Entry::Cmd { 84 | key, 85 | cmd, 86 | desc, 87 | keep_open, 88 | } => MenuItem { 89 | action: Action::Exec { 90 | cmd: cmd.into(), 91 | keep_open: *keep_open, 92 | }, 93 | key_comp: ComputedText::new(key.to_string(), context, &config.font.0), 94 | val_comp: ComputedText::new(desc, context, &config.font.0), 95 | key: key.clone(), 96 | }, 97 | config::Entry::Recursive { 98 | key, 99 | submenu: entries, 100 | desc, 101 | } => { 102 | let new_page = self.push_page(context, entries, config, Some(cur_page))?; 103 | MenuItem { 104 | action: Action::Submenu(new_page), 105 | key_comp: ComputedText::new(key.to_string(), context, &config.font.0), 106 | val_comp: ComputedText::new(format!("+{desc}"), context, &config.font.0), 107 | key: key.clone(), 108 | } 109 | } 110 | }; 111 | 112 | let height = f64::max(item.key_comp.height, item.val_comp.height); 113 | if height > self.pages[cur_page].item_height { 114 | self.pages[cur_page].item_height = height; 115 | } 116 | 117 | let col_i = config 118 | .rows_per_column 119 | .map_or(0, |rows_per_column| entry_i / rows_per_column); 120 | 121 | if col_i == self.pages[cur_page].columns.len() { 122 | self.pages[cur_page].columns.push(MenuColumn { 123 | key_col_width: item.key_comp.width, 124 | val_col_width: item.val_comp.width, 125 | items: vec![item], 126 | }); 127 | } else { 128 | let col = &mut self.pages[cur_page].columns[col_i]; 129 | col.key_col_width = col.key_col_width.max(item.key_comp.width); 130 | col.val_col_width = col.val_col_width.max(item.val_comp.width); 131 | col.items.push(item); 132 | } 133 | } 134 | 135 | Ok(cur_page) 136 | } 137 | 138 | pub fn width(&self, config: &Config) -> f64 { 139 | let page = &self.pages[self.cur_page]; 140 | page.columns 141 | .iter() 142 | .map(|col| col.key_col_width + col.val_col_width + self.separator.width) 143 | .sum::() 144 | + (page.columns.len() - 1) as f64 * config.column_padding() 145 | + (config.padding() + config.border_width) * 2.0 146 | } 147 | 148 | pub fn height(&self, config: &Config) -> f64 { 149 | let page = &self.pages[self.cur_page]; 150 | page.columns 151 | .iter() 152 | .map(|col| page.item_height * col.items.len() as f64) 153 | .max_by(f64::total_cmp) 154 | .unwrap() 155 | + (config.padding() + config.border_width) * 2.0 156 | } 157 | 158 | pub fn render(&self, config: &config::Config, cairo_ctx: &cairo::Context) -> Result<()> { 159 | let mut dx = config.padding() + config.border_width; 160 | let dy = config.padding() + config.border_width; 161 | let page = &self.pages[self.cur_page]; 162 | for col in &page.columns { 163 | self.render_column(config, cairo_ctx, dx, dy, page, col)?; 164 | dx += col.key_col_width 165 | + col.val_col_width 166 | + self.separator.width 167 | + config.column_padding(); 168 | } 169 | Ok(()) 170 | } 171 | 172 | fn render_column( 173 | &self, 174 | config: &Config, 175 | cairo_ctx: &cairo::Context, 176 | dx: f64, 177 | dy: f64, 178 | page: &MenuPage, 179 | column: &MenuColumn, 180 | ) -> Result<()> { 181 | for (i, comp) in column.items.iter().enumerate() { 182 | comp.key_comp.render( 183 | cairo_ctx, 184 | text::RenderOptions { 185 | x: dx + column.key_col_width - comp.key_comp.width, 186 | y: dy + page.item_height * (i as f64), 187 | fg_color: config.color, 188 | height: page.item_height, 189 | }, 190 | )?; 191 | self.separator.render( 192 | cairo_ctx, 193 | text::RenderOptions { 194 | x: dx + column.key_col_width, 195 | y: dy + page.item_height * (i as f64), 196 | fg_color: config.color, 197 | height: page.item_height, 198 | }, 199 | )?; 200 | comp.val_comp.render( 201 | cairo_ctx, 202 | text::RenderOptions { 203 | x: dx + column.key_col_width + self.separator.width, 204 | y: dy + page.item_height * (i as f64), 205 | fg_color: config.color, 206 | height: page.item_height, 207 | }, 208 | )?; 209 | } 210 | 211 | if *DEBUG_LAYOUT { 212 | Color::from_rgba(0, 0, 255, 255).apply(cairo_ctx); 213 | cairo_ctx.rectangle( 214 | dx, 215 | dy, 216 | column.key_col_width + column.val_col_width + self.separator.width, 217 | column.items.len() as f64 * page.item_height, 218 | ); 219 | cairo_ctx.set_line_width(1.0); 220 | cairo_ctx.stroke().unwrap(); 221 | } 222 | 223 | Ok(()) 224 | } 225 | 226 | pub fn get_action(&self, modifiers: ModifierState, sym: xkb::Keysym) -> Option { 227 | let page = &self.pages[self.cur_page]; 228 | 229 | let action = page.columns.iter().find_map(|col| { 230 | col.items 231 | .iter() 232 | .find_map(|i| i.key.matches(sym, modifiers).then(|| i.action.clone())) 233 | }); 234 | if action.is_some() { 235 | return action; 236 | } 237 | 238 | match sym { 239 | xkb::Keysym::Escape => { 240 | return Some(Action::Quit); 241 | } 242 | xkb::Keysym::bracketleft | xkb::Keysym::g if modifiers.mod_ctrl => { 243 | return Some(Action::Quit); 244 | } 245 | xkb::Keysym::BackSpace => { 246 | if let Some(parent) = page.parent { 247 | return Some(Action::Submenu(parent)); 248 | } 249 | } 250 | _ => (), 251 | } 252 | 253 | None 254 | } 255 | 256 | pub fn set_page(&mut self, page: usize) { 257 | self.cur_page = page; 258 | } 259 | 260 | pub fn navigate_to_key_sequence(&mut self, key_sequence: &str) -> Result> { 261 | let mut last_action = None; 262 | for key_str in key_sequence.split_whitespace() { 263 | if let Some((last_key_str, _action)) = &last_action { 264 | bail!("Key '{last_key_str}' leads to a command, but more keys follow in sequence"); 265 | } 266 | let key = SingleKey::from_str(key_str).map_err(Error::msg)?; 267 | match self.get_action(key.modifiers, key.keysym) { 268 | Some(Action::Submenu(submenu_page)) => self.set_page(submenu_page), 269 | Some(action) => last_action = Some((key_str, action)), 270 | None => bail!("Key '{}' not found in current menu", key_str), 271 | } 272 | } 273 | Ok(last_action.map(|x| x.1)) 274 | } 275 | } 276 | -------------------------------------------------------------------------------- /src/text.rs: -------------------------------------------------------------------------------- 1 | use crate::{DEBUG_LAYOUT, color::Color}; 2 | use anyhow::Result; 3 | use pango::FontDescription; 4 | use pangocairo::{cairo, pango}; 5 | 6 | #[derive(Clone, Debug, PartialEq)] 7 | pub struct RenderOptions { 8 | pub x: f64, 9 | pub y: f64, 10 | pub fg_color: Color, 11 | pub height: f64, 12 | } 13 | 14 | #[derive(Clone, Debug)] 15 | pub struct ComputedText { 16 | pub layout: pango::Layout, 17 | pub width: f64, 18 | pub height: f64, 19 | } 20 | 21 | impl ComputedText { 22 | pub fn new(text: impl AsRef, context: &pango::Context, font: &FontDescription) -> Self { 23 | let layout = pango::Layout::new(context); 24 | layout.set_font_description(Some(font)); 25 | layout.set_text(text.as_ref()); 26 | 27 | let (width, height) = layout.pixel_size(); 28 | 29 | ComputedText { 30 | layout, 31 | width: width as f64, 32 | height: height as f64, 33 | } 34 | } 35 | 36 | pub fn render(&self, context: &cairo::Context, options: RenderOptions) -> Result<()> { 37 | pangocairo::functions::update_layout(context, &self.layout); 38 | 39 | context.save()?; 40 | context.translate(options.x, options.y + (options.height - self.height) * 0.5); 41 | 42 | options.fg_color.apply(context); 43 | pangocairo::functions::show_layout(context, &self.layout); 44 | 45 | if *DEBUG_LAYOUT { 46 | Color::from_rgba(255, 0, 0, 255).apply(context); 47 | context.rectangle(0.0, 0.0, self.width, self.height); 48 | context.set_line_width(1.0); 49 | context.stroke().unwrap(); 50 | } 51 | 52 | context.restore()?; 53 | 54 | Ok(()) 55 | } 56 | } 57 | --------------------------------------------------------------------------------