Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/Hyprland: Allow matching against Window Titles for Rewrite #2563

Merged
merged 8 commits into from
Oct 15, 2023

Conversation

Syndelis
Copy link
Contributor

@Syndelis Syndelis commented Oct 9, 2023

Resolves #2561

About this PR

This PR builds upon #2529 to introduce matching against a window's title in order to get its icon. In order to do that, the function that matches against regexes will now be supplied a string in this format: "class<WINDOW_CLASS> title<WINDOW_TITLE>", as opposed to just the class. This way, users may provide rules in the following forms:

"window-rewrite": {
  "title<.*youtube.*>": "", // Windows whose titles contain "youtube"
  "class<firefox>": "", // Windows whose classes are "firefox"
  "class<firefox> title<.*github.*>": "", // Windows whose class is "firefox" and title contains "github". Note that "class" always comes first.
  "foot": "", // Windows that contain "foot" in either class or title. For optimization reasons, it will only match against a title if at least one other window explicitly matches against a title.
}

Rules are now also prioritized, from more specilized to least. In the previous example, the priority order would be:

  1. GitHub — Contains both class and title matching;
  2. YouTube — Matches against a title, which is preferred over simply a class;
  3. FireFox — Explicitly matches against a class;
  4. Foot — Doesn't explicitly match against anything and, thus, is considered low priority. This prevents having your browser be displayed as a terminal if you search for "foot" in FireFox, for example.

Backwards Compatibility

Because of the possible problem pointed out in 4., matching against titles is disabled unless at least one rule explicitly matches against titles. If no rules do so, titles are not sent to the aforementioned function, and, instead, the string "class<WINDOW_CLASS>" is used.

This does mean, however, that rules written as "^something_here$" will not work anymore, and should be replace by either "somethind_here", "class<something_here>" or "^class<something_here>$". While I don't believe anyone might've written such a specific rule, I'm open to hear the reasoning for that and work out something that doesn't break this niche use case.

Implementation Details

A refactor has been made to separate the logic behind the regex map, its cache and default values. This whole logic is now available as the waybar::util::RegexCollection class, which can be constructed from a Json::Value. This class also provides a way to sort rules, giving priority to whichever rule scores higher in the parameter function.

When titles are used in any rules, the workspace module will be subscribed to an additional event called windowtitle, which, according to Hyprland's IPC documentation, only features the window's address as a parameter. This means that an additional call to Hyprland's "clients" socket1 must be made in order to retrieve the window's latest name.

The module is not subscribed to this event when titles aren't explicitly used simply because it would be of no use and could break compatibility as mentioned above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is unrelated to this PR and was simply linted it in f8340d8

@khaneliman
Copy link
Contributor

Looks like freebsd check failed to build properly.

@Syndelis
Copy link
Contributor Author

Looks like freebsd check failed to build properly.

Yeah, my last PR #2568 did too. Unfortunately I can't do much to help it; the logs don't mention the reason it failed, it just goes from "Copying files" to "/bin/bash failed". Would need to re-run it with debug logging enable, but I don't have permissions for that.

@khaneliman
Copy link
Contributor

khaneliman commented Oct 12, 2023

This is what I see looking at the checks logs under Run 'run' in VM

  In file included from /usr/include/c++/v1/__functional/function.h:20:
  /usr/include/c++/v1/__memory/allocator_traits.h:298:9: error: no matching function for call to 'construct_at'
          _VSTD::construct_at(__p, _VSTD::forward<_Args>(__args)...);
          ^~~~~~~~~~~~~~~~~~~
  /usr/include/c++/v1/__config:826:15: note: expanded from macro '_VSTD'
  #define _VSTD std
                ^
  /usr/include/c++/v1/vector:778:21: note: in instantiation of function template specialization 'std::allocator_traits<std::allocator<waybar::util::Rule>>::construct<waybar::util::Rule, const std::regex &, std::string, int &, void, void>' requested here
      __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__tx.__pos_),
                      ^
  /usr/include/c++/v1/vector:1577:9: note: in instantiation of function template specialization 'std::vector<waybar::util::Rule>::__construct_one_at_end<const std::regex &, std::string, int &>' requested here
          __construct_one_at_end(_VSTD::forward<_Args>(__args)...);
          ^
  ../src/util/regex_collection.cpp:24:15: note: in instantiation of function template specialization 'std::vector<waybar::util::Rule>::emplace_back<const std::regex &, std::string, int &>' requested here
          rules.emplace_back(rule, it->asString(), priority);
                ^
  /usr/include/c++/v1/__memory/construct_at.h:36:16: note: candidate template ignored: substitution failure [with _Tp = waybar::util::Rule, _Args = <const std::regex &, std::string, int &>]: no matching constructor for initialization of 'waybar::util::Rule'
  constexpr _Tp* construct_at(_Tp* __location, _Args&& ...__args) {
                 ^
  1 error generated.

@Syndelis
Copy link
Contributor Author

This is what I see looking at the checks logs under Run 'run' in VM

  In file included from /usr/include/c++/v1/__functional/function.h:20:
  /usr/include/c++/v1/__memory/allocator_traits.h:298:9: error: no matching function for call to 'construct_at'
          _VSTD::construct_at(__p, _VSTD::forward<_Args>(__args)...);
          ^~~~~~~~~~~~~~~~~~~
  /usr/include/c++/v1/__config:826:15: note: expanded from macro '_VSTD'
  #define _VSTD std
                ^
  /usr/include/c++/v1/vector:778:21: note: in instantiation of function template specialization 'std::allocator_traits<std::allocator<waybar::util::Rule>>::construct<waybar::util::Rule, const std::regex &, std::string, int &, void, void>' requested here
      __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__tx.__pos_),
                      ^
  /usr/include/c++/v1/vector:1577:9: note: in instantiation of function template specialization 'std::vector<waybar::util::Rule>::__construct_one_at_end<const std::regex &, std::string, int &>' requested here
          __construct_one_at_end(_VSTD::forward<_Args>(__args)...);
          ^
  ../src/util/regex_collection.cpp:24:15: note: in instantiation of function template specialization 'std::vector<waybar::util::Rule>::emplace_back<const std::regex &, std::string, int &>' requested here
          rules.emplace_back(rule, it->asString(), priority);
                ^
  /usr/include/c++/v1/__memory/construct_at.h:36:16: note: candidate template ignored: substitution failure [with _Tp = waybar::util::Rule, _Args = <const std::regex &, std::string, int &>]: no matching constructor for initialization of 'waybar::util::Rule'
  constexpr _Tp* construct_at(_Tp* __location, _Args&& ...__args) {
                 ^
  1 error generated.

My bad, I was looking at the wrong step. I'll take a look at it

@Syndelis
Copy link
Contributor Author

Syndelis commented Oct 12, 2023

Apparently the pipeline runs with Clang 14, which still didn't support direct initialization. Fixed in c59264d. Thanks, @khaneliman !

@khaneliman
Copy link
Contributor

Tested locally seems to work, as intended. @Alexays should be good to go

@Syndelis Syndelis mentioned this pull request Oct 14, 2023
@Alexays Alexays merged commit 7645ae1 into Alexays:master Oct 15, 2023
8 checks passed
@Alexays
Copy link
Owner

Alexays commented Oct 15, 2023

Thanks!

@Syndelis Syndelis deleted the feat/hyprland-window-rename-by-title branch January 25, 2024 01:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: in hyprland/workspaces allow also for window title and regex
3 participants