Browse Source

implement ui template, change librarynames gtk to Gtk etc

unifiedWindowUI
rra 4 years ago
parent
commit
ce89f0f463
  1. 66
      dropship.py

66
dropship.py

@ -12,9 +12,11 @@ import trio_gtk
gi.require_version("Gtk", "3.0") gi.require_version("Gtk", "3.0")
gi.require_version("Gdk", "3.0") gi.require_version("Gdk", "3.0")
from gi.repository import Gdk as gdk from gi.repository import Gdk
from gi.repository import GLib as glib from gi.repository import GLib
from gi.repository import Gtk as gtk from gi.repository import Gtk
from ui_templates import pendingTransmissions
logging.basicConfig(level=os.environ.get("LOGLEVEL", "INFO")) logging.basicConfig(level=os.environ.get("LOGLEVEL", "INFO"))
@ -30,7 +32,7 @@ class DropShip:
self.CSS_FILE = "dropship.css" self.CSS_FILE = "dropship.css"
self.DOWNLOAD_DIR = os.path.expanduser("~") self.DOWNLOAD_DIR = os.path.expanduser("~")
self.clipboard = gtk.Clipboard.get(gdk.SELECTION_CLIPBOARD) self.clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
self.nursery = nursery self.nursery = nursery
self.init_glade() self.init_glade()
@ -40,42 +42,39 @@ class DropShip:
def init_glade(self): def init_glade(self):
"""Initialise the GUI from Glade file.""" """Initialise the GUI from Glade file."""
self.builder = gtk.Builder() self.builder = Gtk.Builder()
self.builder.add_from_file(self.GLADE_FILE) self.builder.add_from_file(self.GLADE_FILE)
self.builder.connect_signals(self) self.builder.connect_signals(self)
def init_css(self): def init_css(self):
"""Initialise CSS injection.""" """Initialise CSS injection."""
self.screen = gdk.Screen.get_default() self.screen = Gdk.Screen.get_default()
self.provider = gtk.CssProvider() self.provider = Gtk.CssProvider()
self.provider.load_from_path(self.CSS_FILE) self.provider.load_from_path(self.CSS_FILE)
gtk.StyleContext.add_provider_for_screen( Gtk.StyleContext.add_provider_for_screen(
self.screen, self.provider, gtk.STYLE_PROVIDER_PRIORITY_APPLICATION self.screen, self.provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
) )
def init_window(self): def init_window(self):
"""Initialise the Main GUI window.""" """Initialise the Main GUI window."""
self.main_window_id = "mainWindow" self.main_window_id = "mainWindow"
self.window = self.builder.get_object(self.main_window_id) self.window = self.builder.get_object(self.main_window_id)
self.window.connect("delete-event", gtk.main_quit) self.window.connect("delete-event", Gtk.main_quit)
self.window.show() self.window.show()
def init_ui_elements(self): def init_ui_elements(self):
"""Initialize the UI elements.""" """Initialize the UI elements."""
# TODO (rra) find out how to use composite templates
# https://github.com/sebp/PyGObject-Tutorial/issues/149
# Send UI # Send UI
# Drag & Drop Box # Drag & Drop Box
self.files_to_send = "" self.files_to_send = ""
self.enforce_target = gtk.TargetEntry.new( self.enforce_target = Gtk.TargetEntry.new(
"text/uri-list", gtk.TargetFlags(4), 129 "text/uri-list", Gtk.TargetFlags(4), 129
) )
self.drop_box = self.builder.get_object("dropBox") self.drop_box = self.builder.get_object("dropBox")
self.drop_box.drag_dest_set( self.drop_box.drag_dest_set(
gtk.DestDefaults.ALL, [self.enforce_target], gdk.DragAction.COPY Gtk.DestDefaults.ALL, [self.enforce_target], Gdk.DragAction.COPY
) )
self.drop_box.connect("drag-data-received", self.on_drop) self.drop_box.connect("drag-data-received", self.on_drop)
self.drop_label = self.builder.get_object("dropLabel") self.drop_label = self.builder.get_object("dropLabel")
@ -84,7 +83,7 @@ class DropShip:
# File chooser # File chooser
self.file_chooser = self.builder.get_object("filePicker") self.file_chooser = self.builder.get_object("filePicker")
self.file_chooser.add_buttons( self.file_chooser.add_buttons(
"Cancel", gtk.ResponseType.CANCEL, "Add", gtk.ResponseType.OK "Cancel", Gtk.ResponseType.CANCEL, "Add", Gtk.ResponseType.OK
) )
# Receive UI # Receive UI
@ -96,16 +95,20 @@ class DropShip:
self.pending_box = self.builder.get_object("pendingBox") self.pending_box = self.builder.get_object("pendingBox")
self.transfer_code = '' self.transfer_code = ''
def on_drop(self, widget, drag_context, x, y, data, info, time): def on_drop(self, widget, drag_context, x, y, data, info, time):
"""Handler for file dropping.""" """Handler for file dropping."""
files = data.get_uris() files = data.get_uris()
self.files_to_send = files self.files_to_send = files
if len(files) == 1: if len(files) == 1:
fpath = files[0].replace("file://", "") fpath = files[0].replace("file://", "")
status = pendingTransmissions(fpath, self.transfer_code) # TODO Luke can u make a callback that spawns pendingTransmissions after we got a code?
self.pending_box.add(status, True, True, 0)
self.nursery.start_soon(self.wormhole_send, fpath) self.nursery.start_soon(self.wormhole_send, fpath)
# TODO Roel/Luke, move this somewhere logical in its own function?
status = pendingTransmissions(self, fpath.split('/')[-1], self.transfer_code)
self.pending_box.add(status)
else: else:
log.info("Multiple file sending coming soon ™") log.info("Multiple file sending coming soon ™")
@ -116,7 +119,7 @@ class DropShip:
def add_files(self, widget, event): def add_files(self, widget, event):
"""Handler for adding files with system interface""" """Handler for adding files with system interface"""
response = self.file_chooser.run() response = self.file_chooser.run()
if response == gtk.ResponseType.OK: if response == Gtk.ResponseType.OK:
fpath = self.file_chooser.get_filenames()[0] fpath = self.file_chooser.get_filenames()[0]
self.nursery.start_soon(self.wormhole_send, fpath) self.nursery.start_soon(self.wormhole_send, fpath)
self.file_chooser.hide() self.file_chooser.hide()
@ -134,9 +137,10 @@ class DropShip:
self.drop_spinner.start() self.drop_spinner.start()
output = await process.stderr.receive_some() output = await process.stderr.receive_some()
self.transfer_code = output.decode().split()[-1] code = output.decode().split()[-1]
self.transfer_code = code
self.drop_label.set_text(self.transfer_code) self.drop_label.set_text(code)
self.drop_label.set_visible(True) self.drop_label.set_visible(True)
self.drop_label.set_selectable(True) self.drop_label.set_selectable(True)
@ -151,24 +155,6 @@ class DropShip:
command = ["wormhole", "receive", "--accept-file", code] command = ["wormhole", "receive", "--accept-file", code]
await trio.run_process(command, stderr=PIPE) await trio.run_process(command, stderr=PIPE)
@gtk.Template.from_file('pendingTransmissions.ui')
class pendingTransmissions(gtk.Box):
__gtype_name__ = 'PendingTransmission'
fileNameLabel = gtk.Template.Child('fileNameLabel')
fileNameMetadata = gtk.Template.Child('fileNameMetadata')
transmissionCodeButton = gtk.Template.Child('transmissionCodeButton')
cancelTransmission = gtk.Template.Child('cancelTransmission')
def __init__(self, widget, fileName, transferCode):
super(Gtk.Box, self).__init__()
self.init_template()
self.fileNameLabel.set_text(fileName)
self.transmissionCodeButton.set_label(transferCode)
async def main(): async def main():
"""Trio main entrypoint.""" """Trio main entrypoint."""
async with trio.open_nursery() as nursery: async with trio.open_nursery() as nursery:

Loading…
Cancel
Save