def create_menu(self): self.icon = DARK_ICONS[0] if utils.dark_mode() else ICONS[0] foreground_tasks = process.family(utils.get_current_app_pid()) background_tasks = process.top(exclude=foreground_tasks) suspender.manage(foreground_tasks, background_tasks) suspended_tasks = suspender.get_suspended_tasks() foreground_menu_items = filter(None, map(self.menu_item_for_process, foreground_tasks)) background_menu_items = filter(None, map(functools.partial(self.menu_item_for_process, suspendable=True), background_tasks)) suspended_menu_items = filter(None, map(functools.partial(self.menu_item_for_process, resumable=True), suspended_tasks)) self.clean_menu() self.menu = ( [ rumps.MenuItem(TITLE_ABOUT % self.version(), callback=self.about), None, rumps.MenuItem(TITLE_CURRENT_PROCESSES), ] + foreground_menu_items + [ None, rumps.MenuItem(TITLE_OTHER_PROCESSES), ] + background_menu_items + [ None, rumps.MenuItem(TITLE_SUSPENDED_PROCESSES), ] + suspended_menu_items + [ None, rumps.MenuItem(TITLE_ON_ETHERNET if utils.on_ethernet() else TITLE_NOT_ON_ETHERNET), rumps.MenuItem(TITLE_QUIT, callback=self.quit), ]) self.need_menu = False
def activate_current_app(): global last_activated_pid if not utils.get_current_app(): return pid = utils.get_current_app_pid() if pid != -1 and pid != last_activated_pid: os.system("osascript -e \"%s\" &" % ACTIVATE_CURRENT_APP) last_activated_pid = pid
def update_current_process(): pid = utils.get_current_app_pid() cpu = process.cpu(pid) if cpu < DORMANT_PROCESS_CPU: return app_name = utils.get_current_app_name() if app_name == "Google Chrome": user, fav, title, url = last_tab user = chrome_users.get(utils.get_active_window_number(), user) else: user, fav, title, url = (process.get_owner(pid), "", utils.get_active_window_name(), "") save_event(int(time.time()), process.cpu(-1), cpu, unicode(user), pid, process.parent_pid(pid), app_name, title, fav, url) return "%s %s" % (user, app_name)
def update(self, force_update=False): if not force_update and self.update_skip_counter > 0: self.update_skip_counter -= 1 return self.update_skip_counter = 8 process.clear_process_cache() utils.clear_windows_cache() self.update_statusbar() percent = process.get_cpu_percent() if force_update or percent > 25 or self.menu_is_open: foreground_tasks = process.family(utils.get_current_app_pid()) background_tasks = process.top(exclude=foreground_tasks) suspender.manage(foreground_tasks, background_tasks) suspended_tasks = suspender.get_suspended_tasks() if force_update or not self.menu_is_highlighted(): self.update_menu(foreground_tasks, background_tasks, suspended_tasks, force_update) suspender.activate_current_app()
def menu_item_for_process(self, p, resumable=False, suspendable=False): if not p: return None name = process.get_name(p.pid) cpu = process.cpu(p.pid) percent = max(0 if resumable else 1, int(100 * cpu)) if p.pid != utils.get_current_app_pid() and not resumable and percent < IDLE_PROCESS_PERCENT_CPU: return None item = rumps.MenuItem("%s - %d%%" % (name, percent)) item.icon = self.get_icon(percent) item.percent = percent item.pid = p.pid item.add(rumps.MenuItem(TITLE_GOOGLE, callback=functools.partial(self.google, pid=p.pid))) if resumable: item.add(rumps.MenuItem(TITLE_RESUME, callback=functools.partial(self.resume, pid=p.pid))) elif suspendable: item.add(rumps.MenuItem(TITLE_SUSPEND, callback=functools.partial(self.suspend, pid=p.pid))) item.add(rumps.MenuItem(TITLE_TERMINATE, callback=functools.partial(self.terminate, pid=p.pid))) return item
def cache_processes(): global cached_processes foreground_tasks = family(utils.get_current_app_pid()) my_pid = os.getpid() exclude_pids = set(p.pid for p in foreground_tasks) def create_process(pid): try: name = get_name(pid) if pid in exclude_pids or pid == my_pid or name == "last": return None return get_process(pid) except: return None processes = filter(None, (create_process(pid) for pid in psutil.pids())) cached_processes = [ foreground_tasks, list(reversed(sorted(processes, key=lambda p: -cpu(p.pid))[:BACKGROUND_PROCESS_COUNT])) ]