Skip to content

Commit

Permalink
refactor(bar): improve structure and organization
Browse files Browse the repository at this point in the history
  • Loading branch information
jx11r committed Jul 26, 2023
1 parent 79d37d3 commit 18e54be
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 125 deletions.
2 changes: 1 addition & 1 deletion core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from core import hooks
from core.bar.utils import defaults
from core.bar.base import defaults
from core.groups import groups
from core.keys import keys
from core.layouts import floating_layout, layouts
Expand Down
7 changes: 2 additions & 5 deletions core/bar/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
from core.bar import theme

bar, tags = theme.bar
from core.bar.base import Bar

__all__ = [
"bar",
"tags",
"Bar",
]
75 changes: 75 additions & 0 deletions core/bar/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from importlib import import_module
from os import listdir
from os.path import isfile, join

from libqtile import bar

from extras import PowerLineDecoration, RectDecoration
from utils.config import cfg

defaults = {
"font": "SauceCodePro Nerd Font Medium",
"fontsize": 10,
"padding": None,
}


class Bar:
def __init__(self, theme: str) -> None:
self.theme = theme

@property
def themes(self) -> set[str]:
path = join(cfg.path(), "core", "bar")
excluded_files = {"__init__.py", "base.py"}
return {
file.removesuffix(".py")
for file in listdir(path)
if isfile(join(path, file)) and file not in excluded_files
}

@property
def config(self) -> dict | None:
if self.theme not in self.themes:
return None
module = import_module(f"core.bar.{self.theme}")
return {"widgets": module.widgets(), **module.bar}

def create(self) -> bar.BarType | None:
if not self.config:
return None
return bar.Bar(**self.config)


def base(bg: str | None, fg: str) -> dict:
return {
"background": bg,
"foreground": fg,
}


def icon_font(size=15) -> dict:
font = "SauceCodePro Nerd Font"
return {"font": font, "fontsize": size}


def powerline(path: str | list[tuple], size=9) -> dict:
return { "decorations": [
PowerLineDecoration(
path=path,
size=size,
)
]} # fmt: skip


def rectangle(side: str = "") -> dict:
return { "decorations": [
RectDecoration(
filled = True,
radius = {
"left": [8, 0, 0, 8],
"right": [0, 8, 8, 0]
}.get(side, 8),
use_widget_background = True,
)
]} # fmt: skip
41 changes: 20 additions & 21 deletions core/bar/decorated.py → core/bar/shapes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from libqtile.bar import CALCULATED
from libqtile.lazy import lazy

from core.bar.utils import base, icon_font, powerline, rectangle
from core.bar.base import base, icon_font, powerline, rectangle
from extras import Clock, GroupBox, TextBox, modify, widget
from utils.palette import palette

Expand All @@ -14,8 +14,6 @@
"size": 18,
}

tags = ["", "", "", "󰈹", "󰇮", ""]


def sep(fg, offset=0, padding=8) -> TextBox:
return TextBox(
Expand Down Expand Up @@ -200,21 +198,22 @@ def clock(bg, fg) -> list:
]


widgets = [
widget.Spacer(length=2),
logo(palette.blue, palette.base),
sep(palette.surface2, offset=-8),
groups(None),
sep(palette.surface2, offset=4, padding=4),
*volume(palette.pink, palette.base),
*updates(palette.red, palette.base),
widget.Spacer(),
window_name(None, palette.text),
widget.Spacer(),
*cpu(palette.green, palette.base),
*ram(palette.yellow, palette.base),
*disk(palette.teal, palette.base),
sep(palette.surface2),
*clock(palette.pink, palette.base),
widget.Spacer(length=2),
]
def widgets():
return [
widget.Spacer(length=2),
logo(palette.blue, palette.base),
sep(palette.surface2, offset=-8),
groups(None),
sep(palette.surface2, offset=4, padding=4),
*volume(palette.pink, palette.base),
*updates(palette.red, palette.base),
widget.Spacer(),
window_name(None, palette.text),
widget.Spacer(),
*cpu(palette.green, palette.base),
*ram(palette.yellow, palette.base),
*disk(palette.teal, palette.base),
sep(palette.surface2),
*clock(palette.pink, palette.base),
widget.Spacer(length=2),
]
17 changes: 0 additions & 17 deletions core/bar/theme.py

This file was deleted.

41 changes: 0 additions & 41 deletions core/bar/utils.py

This file was deleted.

21 changes: 10 additions & 11 deletions core/groups.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
from libqtile.config import Group, Key
from libqtile.lazy import lazy

from core import bar
from core.keys import keys, mod
from utils.match import wm_class

groups, tag = [], bar.tags
groups: list[Group] = []

for i, (key, layout, matches) in enumerate([
("1", None, wm_class("wezterm")),
("2", "max", wm_class("code")),
("3", None, wm_class("insomnia", "obs", "evince")),
("q", "max", wm_class("brave-browser", "firefox")),
("w", "max", wm_class("discord", "telegram-desktop")),
("e", "max", wm_class("spotify", "vlc")),
]): # fmt: skip
groups.append(Group(key, matches, layout=layout, label=tag[i]))
for key, label, layout, matches in [
("1", "", None, wm_class("wezterm")),
("2", "", "max", wm_class("code")),
("3", "", None, wm_class("insomnia", "obs", "evince")),
("q", "󰈹", "max", wm_class("brave-browser", "firefox")),
("w", "󰇮", "max", wm_class("discord", "telegram-desktop")),
("e", "", "max", wm_class("spotify", "vlc")),
]:
groups.append(Group(key, matches, label=label, layout=layout))

keys.extend([
# mod1 + letter of group = switch to group
Expand Down
20 changes: 12 additions & 8 deletions core/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@

from libqtile import hook

from core.bar import bar
from core.screens import screens

bars = [screen.top for screen in screens]
margins = [sum(bar.margin) if bar else -1 for bar in bars] # type: ignore


@hook.subscribe.startup
def startup():
if bar and not sum(bar.margin):
bar.window.window.set_property(
name="WM_NAME",
value="QTILE_BAR",
type="STRING",
format=8,
)
for bar, margin in zip(bars, margins):
if not margin:
bar.window.window.set_property(
name="WM_NAME",
value="QTILE_BAR",
type="STRING",
format=8,
)


@hook.subscribe.client_new
Expand Down
5 changes: 3 additions & 2 deletions core/screens.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
from libqtile.config import Screen

from core.bar import bar
from core.bar import Bar
from utils.config import cfg

screens = [
Screen(
wallpaper=cfg.wallpaper,
wallpaper_mode="fill",
top=bar,
top=Bar(cfg.bar).create(),
),
Screen(
wallpaper=cfg.wallpaper,
wallpaper_mode="fill",
top=Bar(cfg.bar2).create(),
),
]
43 changes: 24 additions & 19 deletions utils/config.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,38 @@
import json
import os
from dataclasses import asdict, dataclass
from os import path
from os import getcwd
from os.path import exists, expanduser, join


@dataclass
class Config:
bar: str | None = "decorated"
bar: str = "shapes"
bar2: str = ""
browser: str = ""
term: str | None = ""
term2: str | None = ""
term2: str = ""
wallpaper: str = ""

@staticmethod
def path() -> str:
xdg = expanduser("~/.config/qtile")
return xdg if exists(xdg) else getcwd()

def get_directory():
XDG = path.expanduser("~/.config/qtile")
if path.exists(XDG):
return XDG
return os.getcwd()
@classmethod
def load(cls) -> "Config":
file = join(cls.path(), "cfg.json")
if not exists(file):
cls.generate(file)
return cls()
with open(file, "r") as f:
content = json.load(f)
return cls(**content)

@classmethod
def generate(cls, file: str) -> None:
with open(file, "w") as f:
content = asdict(cls())
f.write(json.dumps(content, indent=2))

file = f"{get_directory()}/cfg.json"

if not path.exists(file):
cfg = Config()
with open(file, "w") as f:
content = asdict(cfg)
f.write(json.dumps(content, indent=2))
else:
with open(file, "r") as f:
content = json.load(f)
cfg = Config(**content)
cfg = Config.load()

0 comments on commit 18e54be

Please sign in to comment.