#!/usr/bin/env python3

import os
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('AyatanaAppIndicator3', '0.1')
from gi.repository import Gtk, AyatanaAppIndicator3

APPINDICATOR_ID = "cputhrottle"

def script1(widget):
    os.popen('`pkexec /usr/bin/cputhrottle`')

def script2(widget):
    os.popen("/usr/share/cputhrottle/menu/about/CPUthrottle")

def script3(widget):
    os.popen("/usr/share/cputhrottle/menu/rm_autostart & pkill -f CPUthrottle >/dev/null 2>&1")

def build_menu():
    menu = Gtk.Menu()
    items = [
        ("🔆️ Menu", script1, "Return to Main Menu"),
        ("💬️ About", script2, "About CPUthrottle"),
        ("❌️ Quit", script3, "Quit Application"),

    ]

    for label, callback, tip in items:
        item = Gtk.MenuItem(label=label)
        item.connect('activate', callback)
        item.set_tooltip_text(tip)
        menu.append(item)

    menu.show_all()
    return menu

indicator = AyatanaAppIndicator3.Indicator.new(
    APPINDICATOR_ID,
    "/usr/share/cputhrottle/icons/cputhrottle.svg",
    AyatanaAppIndicator3.IndicatorCategory.APPLICATION_STATUS
)

indicator.set_status(AyatanaAppIndicator3.IndicatorStatus.ACTIVE)
indicator.set_menu(build_menu())

Gtk.main()

