Skip to content

Commit

Permalink
daemon detection, daemon install/removal, and layout stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
shadeyg56 committed Feb 7, 2023
1 parent f7e03c9 commit 7149db7
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 28 deletions.
48 changes: 33 additions & 15 deletions auto_cpufreq/gui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import sys

sys.path.append("../")
from auto_cpufreq.gui.objects import RadioButtonView, SystemStatsLabel, CPUFreqStatsLabel, CurrentGovernorBox, DropDownMenu
from auto_cpufreq.core import is_running
from auto_cpufreq.gui.objects import RadioButtonView, SystemStatsLabel, CPUFreqStatsLabel, CurrentGovernorBox, DropDownMenu, DaemonNotRunningView

CSS_FILE = "/usr/local/share/auto-cpufreq/scripts/style.css"

Expand All @@ -17,42 +18,59 @@
class MyWindow(Gtk.Window):
def __init__(self):
super().__init__(title="auto-cpufreq")
self.set_default_size(640, 480)
self.set_default_size(600, 480)
self.set_border_width(10)

self.set_resizable(False)
self.load_css()

settings = Gtk.Settings.get_default()
# Theme
theme = os.environ.get("GTK_THEME")
if theme is not None:
settings.set_property("gtk-theme-name", theme)
# if theme is not None:
# settings.set_property("gtk-theme-name", theme)

self.build()

def main(self):
# main VBOX
self.vbox_top = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
self.vbox_top.set_valign(Gtk.Align.CENTER)
self.vbox_top.set_halign(Gtk.Align.CENTER)
self.add(self.vbox_top)

# main HBOX
self.hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=HBOX_PADDING)
self.hbox.set_valign(Gtk.Align.CENTER)
self.hbox.set_halign(Gtk.Align.CENTER)
self.add(self.hbox)

self.systemstats = SystemStatsLabel()
self.hbox.pack_start(self.systemstats, False, False, 0)

self.vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=20)
self.vbox_right = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=20)

self.menu = DropDownMenu(self)
self.vbox.pack_start(self.menu, False, False, 0)
self.vbox_top.pack_start(self.menu, False, False, 0)

self.currentgovernor = CurrentGovernorBox()
self.vbox.pack_start(self.currentgovernor, False, False, 0)
self.vbox.pack_start(RadioButtonView(), False, False, 0)
self.vbox_right.pack_start(self.currentgovernor, False, False, 0)
self.vbox_right.pack_start(RadioButtonView(), False, False, 0)

self.cpufreqstats = CPUFreqStatsLabel()
self.vbox.pack_start(self.cpufreqstats, False, False, 0)
self.vbox_right.pack_start(self.cpufreqstats, False, False, 0)

self.hbox.pack_start(self.vbox, False, False, 0)
self.hbox.pack_start(self.vbox_right, True, True, 0)

self.vbox_top.pack_start(self.hbox, False, False, 0)

GLib.timeout_add_seconds(5, self.refresh)

def daemon_not_running(self):
self.box = DaemonNotRunningView(self)
self.add(self.box)

def build(self):
if is_running("auto-cpufreq", "--daemon"):
self.main()
else:
self.daemon_not_running()

def load_css(self):
screen = Gdk.Screen.get_default()
self.gtk_provider = Gtk.CssProvider()
Expand Down
105 changes: 92 additions & 13 deletions auto_cpufreq/gui/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

sys.path.append("../../")
from subprocess import getoutput, call
from auto_cpufreq.core import sysinfo, distro_info, set_override, get_override, get_formatted_version, dist_name
from auto_cpufreq.core import sysinfo, distro_info, set_override, get_override, get_formatted_version, dist_name, deploy_daemon, remove_daemon

from io import StringIO

Expand Down Expand Up @@ -48,28 +48,32 @@ def get_version():

class RadioButtonView(Gtk.Box):
def __init__(self):
super().__init__(orientation=Gtk.Orientation.VERTICAL, spacing=5)
self.set_valign(Gtk.Align.START)
super().__init__(orientation=Gtk.Orientation.HORIZONTAL)

self.set_hexpand(True)
self.hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)

self.label = Gtk.Label("Governor Override")

self.default = Gtk.RadioButton.new_with_label_from_widget(None, "Default")
self.default.connect("toggled", self.on_button_toggled, "reset")
self.default.set_halign(Gtk.Align.END)
self.powersave = Gtk.RadioButton.new_with_label_from_widget(self.default, "Powersave")
self.powersave.connect("toggled", self.on_button_toggled, "powersave")
self.powersave.set_halign(Gtk.Align.END)
self.performance = Gtk.RadioButton.new_with_label_from_widget(self.default, "Performance")
self.performance.connect("toggled", self.on_button_toggled, "performance")
self.performance.set_halign(Gtk.Align.END)

self.set_selected()

self.hbox.pack_start(self.default, False, False, 0)
self.hbox.pack_start(self.powersave, False, False, 0)
self.hbox.pack_start(self.performance, False, False, 0)

self.pack_start(self.label, False, False, 0)
self.pack_start(self.hbox, False, False, 0)
self.pack_start(self.default, True, True, 0)
self.pack_start(self.powersave, True, True, 0)
self.pack_start(self.performance, True, True, 0)

#self.pack_start(self.label, False, False, 0)
#self.pack_start(self.hbox, False, False, 0)

def on_button_toggled(self, button, override):
if button.get_active():
Expand All @@ -87,10 +91,9 @@ def set_selected(self):

class CurrentGovernorBox(Gtk.Box):
def __init__(self):
super().__init__(spacing=60)

super().__init__(spacing=25)
self.static = Gtk.Label(label="Current Governor")
self.governor = Gtk.Label(label=getoutput("cpufreqctl.auto-cpufreq --governor").strip().split(" ")[0])
self.governor = Gtk.Label(label=getoutput("cpufreqctl.auto-cpufreq --governor").strip().split(" ")[0], halign=Gtk.Align.END)

self.pack_start(self.static, False, False, 0)
self.pack_start(self.governor, False, False, 0)
Expand Down Expand Up @@ -120,7 +123,6 @@ def refresh(self):
class CPUFreqStatsLabel(Gtk.Label):
def __init__(self):
super().__init__()

self.update()

def update(self):
Expand Down Expand Up @@ -153,6 +155,7 @@ def build_menu(self, parent):
menu = Gtk.Menu()

daemon = Gtk.MenuItem(label="Remove Daemon")
daemon.connect("activate", self._remove_daemon, parent)
menu.append(daemon)

about = Gtk.MenuItem(label="About")
Expand All @@ -167,6 +170,34 @@ def about_dialog(self, MenuItem, parent):
response = dialog.run()
dialog.destroy()

def _remove_daemon(self, MenuItem, parent):
confirm = ConfirmDialog(parent, message="Are you sure you want to remove the daemon?")
response = confirm.run()
confirm.destroy()
if response == Gtk.ResponseType.YES:
try:
remove_daemon()
dialog = Gtk.MessageDialog(
transient_for=parent,
message_type=Gtk.MessageType.INFO,
buttons=Gtk.ButtonsType.OK,
text="Daemon succesfully removed"
)
dialog.format_secondary_text("The app will now close. Please reopen to apply changes")
dialog.run()
dialog.destroy()
parent.destroy()
except Exception as e:
dialog = Gtk.MessageDialog(
transient_for=parent,
message_type=Gtk.MessageType.ERROR,
buttons=Gtk.ButtonsType.OK,
text="Daemon removal failed"
)
dialog.format_secondary_text(f"The following error occured:\n{e}")
dialog.run()
dialog.destroy()


class AboutDialog(Gtk.Dialog):
def __init__(self, parent):
Expand All @@ -191,4 +222,52 @@ def __init__(self, parent):
self.box.pack_start(self.github, False, False, 0)
self.box.pack_start(self.license, False, False, 0)
self.box.pack_start(self.love, False, False, 0)
self.show_all()
self.show_all()

class ConfirmDialog(Gtk.Dialog):
def __init__(self, parent, message: str):
super().__init__(title="Confirmation", transient_for=parent)
self.box = self.get_content_area()
self.set_default_size(400, 100)
self.add_buttons("Yes", Gtk.ResponseType.YES, "No", Gtk.ResponseType.NO)
self.label = Gtk.Label(label=message)

self.box.pack_start(self.label, True, False, 0)

self.show_all()

class DaemonNotRunningView(Gtk.Box):
def __init__(self, parent):
super().__init__(orientation=Gtk.Orientation.VERTICAL, spacing=10, halign=Gtk.Align.CENTER, valign=Gtk.Align.CENTER)

self.label = Gtk.Label(label="auto-cpufreq daemon is not running. Please click the install button")
self.install_button = Gtk.Button.new_with_label("Install")

self.install_button.connect("clicked", self.install_daemon, parent)

self.pack_start(self.label, False, False, 0)
self.pack_start(self.install_button, False, False, 0)

def install_daemon(self, button, parent):
try:
deploy_daemon()
dialog = Gtk.MessageDialog(
transient_for=parent,
message_type=Gtk.MessageType.INFO,
buttons=Gtk.ButtonsType.OK,
text="Daemon succesfully installed"
)
dialog.format_secondary_text("The app will now close. Please reopen to apply changes")
dialog.run()
dialog.destroy()
parent.destroy()
except Exception as e:
dialog = Gtk.MessageDialog(
transient_for=parent,
message_type=Gtk.MessageType.ERROR,
buttons=Gtk.ButtonsType.OK,
text="Daemon install failed"
)
dialog.format_secondary_text(f"The following error occured:\n{e}")
dialog.run()
dialog.destroy()

0 comments on commit 7149db7

Please sign in to comment.