gui/applets: all basic functionality, no state save/restore

This commit is contained in:
Sebastien Bourdeauducq 2016-01-08 20:07:02 +08:00
parent 597c2e4b17
commit 1ea73be56d

View File

@ -1,6 +1,7 @@
import logging import logging
import asyncio import asyncio
import shlex import shlex
from functools import partial
from quamash import QtCore, QtGui, QtWidgets from quamash import QtCore, QtGui, QtWidgets
from pyqtgraph import dockarea from pyqtgraph import dockarea
@ -19,16 +20,19 @@ class AppletDock(dockarea.Dock):
self.applet_name = name self.applet_name = name
self.command = command self.command = command
def rename(self, name):
self.applet_name = name
self.label.setText("Applet: " + name)
async def start(self): async def start(self):
command = self.command.format(embed_token=self.token) command = self.command.format(embed_token=self.token)
logger.debug("starting command %s for %s", command, self.applet_name) logger.debug("starting command %s for %s", command, self.applet_name)
try: try:
self.process = await asyncio.create_subprocess_exec( self.process = await asyncio.create_subprocess_exec(
*shlex.split(command)) *shlex.split(command))
except FileNotFoundError: except:
logger.warning("Applet %s failed to start", self.applet_name) logger.warning("Applet %s failed to start", self.applet_name,
else: exc_info=True)
logger.warning("Applet %s exited", self.applet_name)
def capture(self, win_id): def capture(self, win_id):
logger.debug("capturing window 0x%x for %s", win_id, self.applet_name) logger.debug("capturing window 0x%x for %s", win_id, self.applet_name)
@ -54,11 +58,17 @@ class AppletDock(dockarea.Dock):
except ProcessLookupError: except ProcessLookupError:
pass pass
await self.process.wait() await self.process.wait()
del self.process
async def restart(self):
await self.terminate()
await self.start()
class AppletsDock(dockarea.Dock): class AppletsDock(dockarea.Dock):
def __init__(self, manager): def __init__(self, manager):
self.manager = manager self.manager = manager
self.token_to_checkbox = dict()
dockarea.Dock.__init__(self, "Applets") dockarea.Dock.__init__(self, "Applets")
self.setMinimumSize(QtCore.QSize(850, 450)) self.setMinimumSize(QtCore.QSize(850, 450))
@ -81,6 +91,7 @@ class AppletsDock(dockarea.Dock):
new_action.triggered.connect(self.new) new_action.triggered.connect(self.new)
self.table.addAction(new_action) self.table.addAction(new_action)
restart_action = QtGui.QAction("Restart selected applet", self.table) restart_action = QtGui.QAction("Restart selected applet", self.table)
restart_action.triggered.connect(self.restart)
self.table.addAction(restart_action) self.table.addAction(restart_action)
delete_action = QtGui.QAction("Delete selected applet", self.table) delete_action = QtGui.QAction("Delete selected applet", self.table)
delete_action.triggered.connect(self.delete) delete_action.triggered.connect(self.delete)
@ -102,12 +113,26 @@ class AppletsDock(dockarea.Dock):
name = name.text() name = name.text()
token = self.manager.create(name, command) token = self.manager.create(name, command)
item.applet_token = token item.applet_token = token
self.token_to_checkbox[token] = item
else: else:
token = getattr(item, "applet_token", None) token = getattr(item, "applet_token", None)
if token is not None: if token is not None:
# cell_changed is emitted at row creation # cell_changed is emitted at row creation
self.manager.delete(token) self.manager.delete(token)
item.applet_token = None elif column == 1 or column == 2:
new_value = self.table.item(row, column).text()
token = getattr(self.table.item(row, 0), "applet_token", None)
if token is not None:
if column == 1:
self.manager.rename(token, new_value)
else:
self.manager.set_command(token, new_value)
def disable_token(self, token):
checkbox_item = self.token_to_checkbox[token]
checkbox_item.applet_token = None
del self.token_to_checkbox[token]
checkbox_item.setCheckState(QtCore.Qt.Unchecked)
def new(self): def new(self):
row = self.table.rowCount() row = self.table.rowCount()
@ -119,10 +144,22 @@ class AppletsDock(dockarea.Dock):
checkbox.setCheckState(QtCore.Qt.Unchecked) checkbox.setCheckState(QtCore.Qt.Unchecked)
self.table.setItem(row, 0, checkbox) self.table.setItem(row, 0, checkbox)
def restart(self):
selection = self.table.selectedRanges()
if selection:
row = selection[0].topRow()
token = getattr(self.table.item(row, 0), "applet_token", None)
if token is not None:
asyncio.ensure_future(self.manager.restart(token))
def delete(self): def delete(self):
selection = self.table.selectedRanges() selection = self.table.selectedRanges()
if selection: if selection:
self.table.deleteRow(selection[0].topRow()) row = selection[0].topRow()
token = getattr(self.table.item(row, 0), "applet_token", None)
if token is not None:
self.manager.delete(token)
self.table.deleteRow(row)
class AppletManagerRPC: class AppletManagerRPC:
@ -154,11 +191,27 @@ class AppletManager:
self.applet_docks[token] = dock self.applet_docks[token] = dock
self.dock_area.floatDock(dock) self.dock_area.floatDock(dock)
asyncio.ensure_future(dock.start()) asyncio.ensure_future(dock.start())
dock.sigClosed.connect(partial(self.on_dock_closed, token))
return token return token
def delete(self, token): def on_dock_closed(self, token):
asyncio.ensure_future(self.applet_docks[token].terminate())
self.main_dock.disable_token(token)
del self.applet_docks[token] del self.applet_docks[token]
def delete(self, token):
# This in turns calls on_dock_closed and main_dock.disable_token
self.applet_docks[token].close()
def rename(self, token, name):
self.applet_docks[token].rename(name)
def set_command(self, token, command):
self.applet_docks[token].command = command
async def restart(self, token):
await self.applet_docks[token].restart()
async def stop(self): async def stop(self):
for dock in self.applet_docks.values(): for dock in self.applet_docks.values():
await dock.terminate() await dock.terminate()