kuro-qtile-theme/kuro/theme.py

506 lines
20 KiB
Python

from libqtile.config import Key, Screen, Group, Drag, Click
from libqtile.command import lazy
from libqtile import layout, bar, widget
# Import theme util functions
from kuro.utils import general as utils
# Import variables
from kuro.base import BaseTheme
from kuro.utils.general import display_wm_class, test_popups
from kuro.utils.kb_backlight import handle_focus_change as kb_handle_focus_change
from kuro.utils import layouts as kuro_layouts
try:
from kuro.config import Config
except ImportError:
try:
from kuro.baseconfig import BaseConfig as Config
except ImportError:
Config = None
raise ImportError("Could not load theme Config or BaseConfig!")
# Initialize logging
from libqtile.log_utils import logger
class Kuro(BaseTheme):
# Shorthand for modifier key
mod = Config.get("modifier", "mod4")
# Show debug messages
debug = Config.get('debug', False)
debug_textfields = []
debug_bars = []
# Screen count
num_screens = 0
# Top bars
topbars = []
# Window manager name
wmname = "QTile"
def set_debug_text(self, text):
for field in self.debug_textfields:
field.text = text
for bar in self.debug_bars:
bar.draw()
def log_debug(self, text):
if Config.get('verbose', False):
self.set_debug_text(text)
logger.debug(text)
def log_info(self, text):
self.set_debug_text(text)
logger.info(text)
def initialize(self):
self.log_debug("Initializing Kuro Theme...")
super(Kuro, self).initialize()
self.update()
def update(self):
# Update keys with keys for groups and layouts
self.update_keys()
def init_keys(self):
self.log_debug("Initializing keys")
return [
# Switch between windows in current stack pane
Key([self.mod], "k", lazy.layout.down()),
Key([self.mod], "j", lazy.layout.up()),
# Move windows up or down in current stack
Key([self.mod, "control"], "k", lazy.layout.shuffle_down()),
Key([self.mod, "control"], "j", lazy.layout.shuffle_up()),
# Switch window focus to other pane(s) of stack
Key([self.mod], "space", lazy.layout.next()),
# Swap panes of split stack
Key([self.mod, "shift"], "space", lazy.layout.rotate()),
# Fullscreen toggle
Key([self.mod], 'f', lazy.window.toggle_fullscreen()),
# Floating toggle
Key([self.mod, "shift"], 'f', lazy.window.toggle_floating()),
# Toggle between split and unsplit sides of stack.
# Split = all windows displayed
# Unsplit = 1 window displayed, like Max layout, but still with
# multiple stack panes
Key([self.mod, "shift"], "Return", lazy.layout.toggle_split()),
# Super-Enter to start terminal
Key([self.mod], "Return", lazy.spawn(Config.get('app_terminal', "xterm"))),
# Super-R to start dmenu_run
Key([self.mod], "r", lazy.spawn(Config.get('app_launcher', "dmenu_run"))),
# Super-B to start webbrowser
Key([self.mod], "b", lazy.spawn(Config.get('web_browser', "xterm links"))),
# Super-F to start file manager
# Key([self.mod], "f", lazy.spawn(Config.get('file_manager', "thunar"))),
# Super-Shift-R to start spawncmd
Key([self.mod, "shift"], "r", lazy.spawncmd()),
# Lock shortcut
Key([self.mod], "l", lazy.spawn(Config.get('lock_command', "i3lock"))),
# Backlight keys
Key([], "XF86MonBrightnessUp", lazy.spawn(Config.get('cmd_brightness_up', 'xbacklight -inc 10'))),
Key([], "XF86MonBrightnessDown", lazy.spawn(Config.get('cmd_brightness_down', 'xbacklight -dec 10'))),
# Toggle between different layouts as defined below
Key([self.mod], "Tab", lazy.next_layout()),
# Kill the current window
Key([self.mod], "w", lazy.window.kill()),
# Restart QTile
Key([self.mod, "control"], "r", lazy.restart()),
# Shutdown QTile
Key([self.mod, "control"], "q", lazy.shutdown()),
##
# Debug keyboard shortcuts
##
Key([self.mod, "control"], "w", lazy.function(display_wm_class)),
# Redraw the top bar
Key([self.mod, "shift", "control"], "r", lazy.function(self.redraw_bar)),
# Spawn a popup, and despawn it after 3 seconds
Key([self.mod, "control"], "p", lazy.function(test_popups)),
]
def init_groups(self):
self.log_debug("Initializing groups")
groups = []
# http://fontawesome.io/cheatsheet
groups.append(Group("", spawn=Config.get('web_browser', "xterm links")))
groups.append(Group("", spawn=Config.get('app_terminal', "xterm")))
groups.append(Group(""))
groups.append(Group("", spawn="franz4-bin"))
groups.append(Group("", spawn="quasselclient"))
groups.append(Group("", spawn=Config.get('file_manager', "thunar")))
groups.append(Group("", spawn="thunderbird"))
groups.append(Group(""))
groups.append(Group("", spawn="qupzilla https://music.kurocon.nl/"))
groups.append(Group(""))
return groups
def init_layouts(self):
self.log_debug("Initializing layouts")
return [
kuro_layouts.KuroWmii(
border_focus=Config.get('colour_border_focus', "#ffffff"),
border_focus_stack=Config.get('colour_border_normal', "#777777"),
border_normal=Config.get('colour_border_normal', "#777777"),
border_normal_stack=Config.get('colour_border_normal', "#777777"),
border_width=Config.get('width_border', "1"),
grow_amount=Config.get('grow_amount', "5"),
margin=Config.get('margin_layout', "0"),
),
layout.Max(),
layout.Zoomy(
columnwidth=Config.get('width_zoomy_column', 150),
margin=Config.get('margin_layout', "0"),
)
]
def init_widget_defaults(self):
self.log_debug("Initializing widget_defaults")
return {
"font": Config.get('font_topbar', "Sans"),
"fontsize": Config.get('fontsize_topbar', 16),
"padding": 3,
}
def init_screens(self):
self.log_debug("Initializing screens")
self.num_screens = utils.get_screen_count()
if self.num_screens == 0:
self.num_screens = 1
screens = []
for x in range(self.num_screens):
self.log_info("Initializing bars for screen {}".format(x))
widgets = []
widgets.extend([
widget.GroupBox(
active=Config.get('colour_groupbox_icon_active', '#ffffff'),
borderwidth=Config.get('width_groupbox_border', 1),
disable_drag=Config.get('bool_groupbox_disable_drag', False),
font=Config.get('font_groupbox', 'Arial'),
fontsize=Config.get('fontsize_groupbox', 15),
highlight_color=Config.get("colour_groupbox_border_normal", '#444444'),
inactive=Config.get('colour_groupbox_icon_inactive', '#444444'),
rounded=Config.get('bool_groupbox_rounded_borders', True),
this_current_screen_border=Config.get('colour_groupbox_border_focus', '#ffffff'),
this_screen_border=Config.get('colour_groupbox_border_focus', '#ffffff'),
margin=Config.get('margin_groupbox', 0)
),
widget.Prompt(**self.widget_defaults),
widget.TaskList(
border=Config.get('tasklist_border', '#ffffff'),
borderwidth=Config.get('tasklist_borderwidth', 1),
font=Config.get('tasklist_font', 'Arial'),
fontsize=Config.get('tasklist_fontsize', 15),
highlight_method=Config.get('tasklist_highlight_method', 'border'),
max_title_width=Config.get('tasklist_max_title_width', 200),
rounded=Config.get('tasklist_rounded', True),
urgent_alert_method=Config.get('tasklist_urgent_alert_method', 'border'),
urgent_border=Config.get('tasklist_urgent_border', '#ff0000'),
margin=Config.get('margin_groupbox', 0)
),
utils.ThermalSensorWidget(
font=Config.get('font_topbar', 'Arial'),
fontsize=Config.get('fontsize_topbar', 16),
foreground=Config.get('thermal_colour', '#ffffff'),
foreground_alert=Config.get('thermal_colour_alert', '#ff0000'),
tag_sensor=Config.get('thermal_sensor', 'temp1'),
chip=Config.get('thermal_chip', None),
threshold=Config.get('thermal_threshold', 70),
update_interval=5,
),
widget.CPUGraph(
width=Config.get('cpu_width', 25),
border_color=Config.get('cpu_border_colour', "#000000"),
graph_color=Config.get('cpu_graph_colour', "#00ffff"),
border_width=Config.get('cpu_graph_width', 0),
line_width=Config.get('cpu_line_width', 1),
samples=Config.get('cpu_samples', 10),
frequency=2,
),
widget.MemoryGraph(
width=Config.get('mem_width', 25),
border_color=Config.get('mem_border_colour', "#000000"),
graph_color=Config.get('mem_graph_colour', "#00ffff"),
border_width=Config.get('mem_graph_width', 0),
line_width=Config.get('mem_line_width', 1),
samples=Config.get('mem_samples', 10),
frequency=2,
),
widget.HDDBusyGraph(
width=Config.get('hdd_width', 25),
border_color=Config.get('hdd_border_colour', "#000000"),
graph_color=Config.get('hdd_graph_colour', "#00ffff"),
border_width=Config.get('hdd_border_width', 0),
line_width=Config.get('hdd_line_width', 1),
samples=Config.get('hdd_samples', 10),
frequency=2,
),
widget.NetGraph(
width=Config.get('net_width', 25),
border_color=Config.get('net_border_colour', "#000000"),
graph_color=Config.get('net_graph_colour', "#00ffff"),
border_width=Config.get('net_border_width', 0),
line_width=Config.get('net_line_width', 1),
samples=Config.get('net_samples', 10),
frequency=2,
),
utils.KuroBatteryIcon(
battery_name=Config.get('battery_name', 'BAT0'),
energy_full_file=Config.get('battery_energy_full_file', 'charge_full'),
energy_now_file=Config.get('battery_energy_now_file', 'charge_now'),
theme_path=Config.get('battery_theme_path', '/home/docs/checkouts/readthedocs.org/user_builds/qtile'
'/checkouts/latest/libqtile/resources/battery-icons'),
update_delay=Config.get('battery_update_delay', 30)
),
utils.WifiIconWidget(
interface=Config.get('wifi_interface', 'wlp4s0'),
theme_path=Config.get('wifi_theme_path', '/home/docs/checkouts/readthedocs.org/user_builds/qtile'
'/checkouts/latest/libqtile/resources/battery-icons'),
update_interval=Config.get('wifi_update_interval', 30)
),
utils.PulseVolumeWidget(
cardid=Config.get('volume_cardid', None),
channel=Config.get('volume_channel', 'Master'),
device=Config.get('volume_device', None),
font=Config.get('volume_font', 'Arial'),
fontsize=Config.get('volume_fontsize', 15),
foreground=Config.get('volume_foreground', '#ffffff'),
get_volume_command=Config.get('volume_get_command', None),
mute_command=Config.get('volume_mute_command', None),
theme_path=Config.get('volume_theme_path', '/home/docs/checkouts/readthedocs.org/user_builds/qtile'
'/checkouts/latest/libqtile/resources/volume-icons'),
volume_down_command=Config.get('volume_down_command', None),
volume_up_command=Config.get('volume_up_command', None),
is_bluetooth_icon=Config.get('volume_is_bluetooth_icon', False),
update_interval=Config.get('volume_update_interval', 0.2)
),
utils.PulseVolumeWidget(
cardid=Config.get('bluevol_cardid', None),
channel=Config.get('bluevol_channel', 'Master'),
device=Config.get('bluevol_device', None),
font=Config.get('bluevol_font', 'Arial'),
fontsize=Config.get('bluevol_fontsize', 15),
foreground=Config.get('bluevol_foreground', '#ffffff'),
get_volume_command=Config.get('bluevol_get_command', None),
mute_command=Config.get('bluevol_mute_command', None),
theme_path=Config.get('bluevol_theme_path', '/home/docs/checkouts/readthedocs.org/user_builds/qtile'
'/checkouts/latest/libqtile/resources/volume-icons'),
volume_down_command=Config.get('bluevol_down_command', None),
volume_up_command=Config.get('bluevol_up_command', None),
is_bluetooth_icon=Config.get('bluevol_is_bluetooth_icon', False),
update_interval=Config.get('bluevol_update_interval', 0.2)
)
])
# Systray only on first screen
if x == 0:
widgets.append(widget.Systray(**self.widget_defaults))
widgets.extend([
widget.CurrentLayoutIcon(),
widget.Clock(format="%a %d %b, %H:%M", **self.widget_defaults),
utils.CheckUpdatesYaourt(
colour_no_updates=Config.get('updates_colour_none', '#ffffff'),
colour_have_updates=Config.get('updates_colour_available', '#ff0000'),
display_format=Config.get('updates_display_format', 'Updates: {updates}'),
execute=Config.get('updates_execute_command', None),
**self.widget_defaults
),
widget.TextBox("#{}".format(x), name="default", **self.widget_defaults),
])
topbar = utils.KuroTopBar(
theme=self,
background=Config.get('bar_background', '#000000'),
opacity=Config.get('bar_opacity', 1.0),
widgets=widgets,
size=Config.get('height_groupbox', 30)
)
self.topbars.append(topbar)
screens.append(Screen(top=topbar))
# Add debug bars on each window if debugging is enabled
if Config.get('debug', False):
self.debug_textfields = []
for x in range(self.num_screens):
textfield = widget.TextBox("...", name="debugtext", **self.widget_defaults)
self.debug_textfields.append(textfield)
widgets = []
widgets.extend([
widget.TextBox(" Debugging bar ", name="default", **self.widget_defaults),
textfield,
])
screens[x].bottom = bar.Bar(
widgets=widgets,
size=Config.get('height_debugbar', 30)
)
self.debug_bars.append(screens[x].bottom)
return screens
def init_mouse(self):
self.log_debug("Initializing mouse")
# Drag floating layouts.
mouse = [
Drag([self.mod], "Button1", lazy.window.set_position_floating(),
start=lazy.window.get_position()),
Drag([self.mod], "Button3", lazy.window.set_size_floating(),
start=lazy.window.get_size()),
Click([self.mod], "Button2", lazy.window.bring_to_front())
]
return mouse
def update_keys(self):
self.log_debug("Updating keys")
for i, g in enumerate(self.groups):
if i == 9:
i = -1
# mod1 + number = switch to group
self.keys.append(
Key([self.mod], str(i + 1), lazy.group[g.name].toscreen())
)
# mod1 + shift + number = switch to & move focused window to group
self.keys.append(
Key([self.mod, "shift"], str(i + 1), lazy.window.togroup(g.name))
)
# Keys for the Wmii layout
self.keys.extend([
Key(
[self.mod, "shift", "control"], "l",
lazy.layout.grow_right()
),
Key(
[self.mod, "shift"], "l",
lazy.layout.shuffle_right()
),
Key(
[self.mod, "shift", "control"], "h",
lazy.layout.grow_left()
),
Key(
[self.mod, "shift"], "h",
lazy.layout.shuffle_left()
),
Key(
[self.mod], "s",
lazy.layout.toggle_split()
)
])
# Util functions
@staticmethod
def redraw_bar(qtile):
for b in qtile.topbars:
b.draw()
# QTile base callbacks
def callback_startup(self):
utils.execute("sleep 3")
self.log_info("Restoring wallpaper...")
utils.execute_once("nitrogen --restore")
#
# display = os.environ['DISPLAY']
#
# if not display:
# display = ":0"
#
# # Start compton for each screen
# for x in range(self.num_screens):
# self.log_info("Launching compton for screen {}.{}".format(display, x))
# utils.execute_once("compton --config ~/.config/compton.conf -b -d {}.{}".format(display, x))
# def callback_screen_change(self, *args, **kwargs):
# self.num_screens = utils.get_screen_count()
# return True
def callback_focus_change(self, *args, **kwargs):
kb_handle_focus_change(self)
initial_windows = []
def callback_startup_complete(self, *args, **kwargs):
# Only run on first startup
if not self.qtile.no_spawn:
dg = self.qtile.dgroups
for r in dg.rules:
pid = -1
# noinspection PyProtectedMember
for r2 in r.match._rules:
if r2[0] == "net_wm_pid":
pid = r2[1]
break
if pid != -1:
self.initial_windows.append((pid, r.group))
self.callback_client_new()
def callback_client_new(self, *args, **kwargs):
if len(self.initial_windows) > 0:
init = self.initial_windows.copy()
for pid, gname in init:
for group in self.qtile.groups:
if len(group.windows) > 0:
for window in group.windows:
w_pid = window.window.get_net_wm_pid()
if pid == w_pid:
c = self.qtile.dgroups.rules_map.copy()
for rid, r in c.items():
if r.matches(window):
self.qtile.dgroups.remove_rule(rid)
self.initial_windows.remove((pid, gname))
self.log_info("Removed group rule for PID {}, window {}".format(pid, window.name))
self.log_info(str(self.qtile.dgroups.rules_map))