Refactor and integrate async-glib

This commit is contained in:
Luke Murphy 2020-07-22 16:55:56 +02:00
parent 62109c0ee5
commit 65f7e2b0ed
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC
2 changed files with 60 additions and 37 deletions

View File

@ -1,3 +1,8 @@
"""Magic-Wormhole bundled up inside a PyGtk GUI."""
import asyncio
import asyncio_glib
import gi import gi
gi.require_version("Gtk", "3.0") gi.require_version("Gtk", "3.0")
@ -7,58 +12,75 @@ from gi.repository import Gdk as gdk
from gi.repository import GLib as glib from gi.repository import GLib as glib
from gi.repository import Gtk as gtk from gi.repository import Gtk as gtk
asyncio.set_event_loop_policy(asyncio_glib.GLibEventLoopPolicy())
class DropShip:
"""Drag it, drop it, ship it."""
class Main:
def __init__(self): def __init__(self):
self.GLADE_FILE = "dropship.glade"
# Connect to the Glade file self.CSS_FILE = "dropship.css"
self.builder = gtk.Builder()
self.builder.add_from_file("dropship.glade")
self.builder.connect_signals(self)
# Connect to the Stylesheet
screen = gdk.Screen.get_default()
provider = gtk.CssProvider()
provider.load_from_path("./dropship.css")
gtk.StyleContext.add_provider_for_screen(
screen, provider, gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
)
window = self.builder.get_object("mainWindow")
window.connect("delete-event", gtk.main_quit)
window.show()
# self.stack = self.builder.get_object("sendReceiveStack")
# Initiate the drag and drop area # Initiate the drag and drop area
# https://python-gtk-3-tutorial.readthedocs.io/en/latest/drag_and_drop.html # https://python-gtk-3-tutorial.readthedocs.io/en/latest/drag_and_drop.html
self.files_to_send = "" self.files_to_send = ""
# todo check the target flags, https://lazka.github.io/pgi-docs/Gtk-3.0/flags.html#Gtk.TargetFlags # todo check the target flags, https://lazka.github.io/pgi-docs/Gtk-3.0/flags.html#Gtk.TargetFlags
enforce_target = gtk.TargetEntry.new("text/plain", gtk.TargetFlags(4), 129) self.enforce_target = gtk.TargetEntry.new("text/plain", gtk.TargetFlags(4), 129)
self.dropBox = self.builder.get_object("dropBox") self.main_window_id = "mainWindow"
self.dropBox.drag_dest_set(
gtk.DestDefaults.ALL, [enforce_target], gdk.DragAction.COPY self.drop_box_id = "dropBox"
self.drop_box_label = "dropLabel"
self.init_glade()
self.init_css()
self.init_drop_box()
def init_glade(self):
"""Initialise the GUI from Glade file."""
self.builder = gtk.Builder()
self.builder.add_from_file(self.GLADE_FILE)
self.builder.connect_signals(self)
def init_css(self):
"""Initialise CSS injection."""
screen = gdk.Screen.get_default()
provider = gtk.CssProvider()
provider.load_from_path(self.CSS_FILE)
gtk.StyleContext.add_provider_for_screen(
screen, provider, gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
) )
self.dropBox.connect("drag-data-received", self.onDrop)
self.dropLabel = self.builder.get_object("dropLabel")
def onDrop(self, widget, drag_context, x, y, data, info, time): async def init_window(self):
print(drag_context, x, y, data, info, time) """Initialise the GUI window."""
window = self.builder.get_object(self.main_window_id)
window.connect("delete-event", gtk.main_quit)
window.show()
def init_drop_box(self):
"""Initialise the drag & drop box."""
self.dropBox = self.builder.get_object(self.drop_box_id)
self.dropBox.drag_dest_set(
gtk.DestDefaults.ALL, [self.enforce_target], gdk.DragAction.COPY
)
self.dropBox.connect("drag-data-received", self.on_drop)
self.dropLabel = self.builder.get_object(self.drop_box_label)
def on_drop(self, widget, drag_context, x, y, data, info, time):
files = data.get_text().split() files = data.get_text().split()
if len(files) == 1: if len(files) == 1:
print(files) # TODO: wormhole send that file
self.dropLabel.set_text("Sending..") self.dropLabel.set_text("Sending..")
elif len(files) > 1:
print("multiple files!")
print(files)
self.dropLabel.set_text("\n".join(files))
self.files_to_send = files self.files_to_send = files
async def main():
"""The application entrypoint."""
dropship = DropShip()
await dropship.init_window()
if __name__ == "__main__": if __name__ == "__main__":
Main() asyncio.run(main())
gtk.main()

View File

@ -1,2 +1,3 @@
PyGObject==3.36.1 PyGObject==3.36.1
asyncio-glib==0.1
magic-wormhole==0.12.0 magic-wormhole==0.12.0