Created
August 11, 2014 17:25
-
-
Save FelixHaller/a0a0a393f67599934f91 to your computer and use it in GitHub Desktop.
Launcher prototype
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
# | |
# launcher.py | |
# | |
# Copyright 2014 Felix Haller <ich@ein-freier-mensch.de> | |
# | |
# This program is free software; you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation; either version 2 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program; if not, write to the Free Software | |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | |
# MA 02110-1301, USA. | |
# | |
# | |
import sys, time, threading, os, subprocess | |
import xdg.Menu | |
import xdg.DesktopEntry | |
import xdg.IconTheme | |
from PyQt5 import QtGui, QtWidgets, QtCore | |
from PyQt5.uic import loadUi | |
class App(QtWidgets.QMainWindow): | |
def __init__(self, parent=None): | |
super(App, self).__init__(parent) | |
self.allApps = {} | |
self.liste = [] | |
centralWidget = QtWidgets.QWidget(parent=self) | |
self.setCentralWidget(centralWidget) | |
threading.Thread(target=self.buildDict,args=(xdg.Menu.parse(),)).start() | |
layout = QtWidgets.QVBoxLayout() | |
self.setWindowFlags(QtCore.Qt.FramelessWindowHint) | |
self.inputLine = QtWidgets.QLineEdit() | |
self.inputLine.textChanged.connect(self.foo) | |
self.centerOnScreen() | |
self.inputLine.setGeometry(0,0,self.width(),25) | |
self.resultWidget = QtWidgets.QListWidget() | |
layout.addWidget(self.inputLine) | |
layout.addWidget(self.resultWidget) | |
centralWidget.setLayout(layout) | |
def foo(self): | |
self.resultWidget.clear() | |
self.liste = [] | |
if len(self.inputLine.text()) > 1: | |
for entry in self.allApps: | |
if self.inputLine.text().lower() in entry.lower(): | |
try: | |
icon = QtGui.QIcon(self.allApps[entry][1]) | |
except AttributeError: | |
icon = QtGui.QIcon(xdg.IconTheme.getIconPath("applications-other")) | |
item = QtWidgets.QListWidgetItem(icon,entry) | |
self.resultWidget.addItem(item) | |
self.liste.append(self.allApps[entry][0].getExec()) | |
def centerOnScreen (self): | |
'''Centers the window on the screen.''' | |
resolution = QtWidgets.QDesktopWidget().screenGeometry() | |
self.setGeometry(0,0,resolution.width() / 2,100) | |
self.move((resolution.width() / 2) - (self.frameSize().width() / 2), | |
(resolution.height() / 2) - (self.frameSize().height() / 2)) | |
def keyPressEvent(self, e): | |
if e.key() == QtCore.Qt.Key_Escape: | |
if self.inputLine.text() == "": | |
self.close() | |
else: | |
self.inputLine.setText("") | |
self.inputLine.setFocus(True) | |
elif e.key() == QtCore.Qt.Key_Down: | |
if self.inputLine.hasFocus() and self.resultWidget.count() > 0: | |
self.resultWidget.setFocus(True) | |
self.resultWidget.item(0).setSelected(True) | |
elif e.key() == QtCore.Qt.Key_Up: | |
if self.resultWidget.hasFocus() and self.resultWidget.item(0).isSelected(): | |
self.inputLine.setFocus(True) | |
elif e.key() == QtCore.Qt.Key_Backspace: | |
self.inputLine.setText("") | |
self.inputLine.setFocus(True) | |
elif e.key() == QtCore.Qt.Key_Enter or e.key() == QtCore.Qt.Key_Return: | |
print("starte: " + self.liste[self.resultWidget.currentRow()]) | |
subprocess.Popen(['nohup', self.liste[self.resultWidget.currentRow()].split(" ")[0]], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) | |
sys.exit(0) | |
def buildDict(self, menu): | |
for entry in menu.getEntries(): | |
if isinstance(entry, xdg.Menu.Menu): | |
self.buildDict(entry) | |
elif isinstance(entry, xdg.Menu.MenuEntry): | |
self.allApps[entry.DesktopEntry.getName()] = [entry.DesktopEntry,xdg.IconTheme.getIconPath(entry.DesktopEntry.getIcon())] | |
if __name__ == "__main__": | |
app = QtWidgets.QApplication(sys.argv) | |
myapp = App() | |
myapp.show() | |
sys.exit(app.exec_()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment