Skip to content

Commit

Permalink
refactor: simplify headerbar handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dynobo committed Jul 5, 2024
1 parent 8a6a257 commit c0594df
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ jobs:

- uses: ncipollo/release-action@v1
with:
body: See [CHANGELOG.md](https://github.com/dynobo/keyhint/blob/main/CHANGELOG.md] for details.
body: See [CHANGELOG.md](https://github.com/dynobo/keyhint/blob/main/CHANGELOG.md) for details.
20 changes: 10 additions & 10 deletions keyhint/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ def get_gnome_version() -> str:
return "(n/a)"

try:
output = subprocess.check_output(
output = subprocess.check_output( # noqa: S603
["gnome-shell", "--version"], # noqa: S607
shell=False, # noqa: S603
shell=False,
text=True,
)
if result := re.search(r"\s+([\d\.]+)", output.strip()):
Expand All @@ -70,9 +70,9 @@ def get_kde_version() -> str:
return "(n/a)"

try:
output = subprocess.check_output(
output = subprocess.check_output( # noqa: S603
["plasmashell", "--version"], # noqa: S607
shell=False, # noqa: S603
shell=False,
text=True,
)
if result := re.search(r"([\d+\.]+)", output.strip()):
Expand Down Expand Up @@ -216,9 +216,9 @@ def get_active_window_via_kwin() -> tuple[str, str]:
# The output has to be read through journalctl instead. A timestamp for
# filtering speeds up the process.
log_lines = (
subprocess.check_output(
subprocess.check_output( # noqa: S602
f'journalctl --user -o cat --since "{since}"',
shell=True, # noqa: S602
shell=True,
)
.decode()
.split("\n")
Expand All @@ -243,9 +243,9 @@ def get_active_window_via_xprop() -> tuple[str, str]:
Tuple(str, str): window class, window title
"""
# Query id of active window
stdout_bytes: bytes = subprocess.check_output(
stdout_bytes: bytes = subprocess.check_output( # noqa: S602
"xprop -root _NET_ACTIVE_WINDOW", # noqa: S607
shell=True, # noqa: S602
shell=True,
)
stdout = stdout_bytes.decode()

Expand All @@ -257,9 +257,9 @@ def get_active_window_via_xprop() -> tuple[str, str]:
window_id: str = match.group(1)

# Query app_title and app_process
stdout_bytes = subprocess.check_output(
stdout_bytes = subprocess.check_output( # noqa: S602
f"xprop -id {window_id} WM_NAME WM_CLASS",
shell=True, # noqa: S602
shell=True,
)
stdout = stdout_bytes.decode()

Expand Down
27 changes: 13 additions & 14 deletions keyhint/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,15 @@ def init_sheet_dropdown(self) -> None:
for sheet_id in sorted([s["id"] for s in self.sheets]):
model.append(sheet_id)

@property
def active_headerbar(self) -> headerbar.HeaderBarBox:
"""Return the currently active headerbar depending on window state."""
return (
self.headerbars.fullscreen
if self.is_fullscreen()
else self.headerbars.normal
)

@check_state
def on_set_fallback_sheet(
self, action: Gio.SimpleAction, state: GLib.Variant
Expand Down Expand Up @@ -458,12 +467,8 @@ def scroll(self, to_start: bool, by_page: bool) -> None:

def focus_search_entry(self) -> None:
"""Focus search entry of the active headerbar."""
if self.is_fullscreen():
self.headerbars.fullscreen.search_entry.grab_focus()
self.headerbars.fullscreen.search_entry.set_position(-1)
else:
self.headerbars.normal.search_entry.grab_focus()
self.headerbars.normal.search_entry.set_position(-1)
self.active_headerbar.search_entry.grab_focus()
self.active_headerbar.search_entry.set_position(-1)

def show_create_new_sheet_toast(self) -> None:
"""Display a toast notification to offer the creation of a new cheatsheet."""
Expand Down Expand Up @@ -523,15 +528,9 @@ def on_key_pressed(
case Gdk.KEY_F11, _:
self.activate_action("win.fullscreen")
case Gdk.KEY_f, True:
if self.is_fullscreen():
self.headerbars.fullscreen.search_entry.grab_focus()
else:
self.headerbars.normal.search_entry.grab_focus()
self.active_headerbar.grab_focus()
case Gdk.KEY_s, True:
if self.is_fullscreen():
self.headerbars.fullscreen.sheet_dropdown.grab_focus()
else:
self.headerbars.normal.sheet_dropdown.grab_focus()
self.active_headerbar.sheet_dropdown.grab_focus()
case (Gdk.KEY_Up, _) | (Gdk.KEY_k, True):
self.scroll(to_start=True, by_page=False)
case (Gdk.KEY_Down, _) | (Gdk.KEY_j, True):
Expand Down

0 comments on commit c0594df

Please sign in to comment.