Skip to content

Commit

Permalink
Merge pull request #383 from gaphor/clipboard
Browse files Browse the repository at this point in the history
Clear clipboard when diagram items are copied
  • Loading branch information
danyeaw committed Jul 31, 2020
2 parents da5cfab + 2ca0609 commit 356757d
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion gaphor/services/copyservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from typing import Set

from gi.repository import Gdk, Gtk

from gaphor.abc import ActionProvider, Service
from gaphor.core import Transaction, action, event_handler
from gaphor.core.modeling import Presentation
Expand Down Expand Up @@ -35,10 +37,22 @@ def __init__(self, event_manager, element_factory, diagrams):

event_manager.subscribe(self._update)

self.clipboard = Gtk.Clipboard.get_default(Gdk.Display.get_default())
self.clipboard.connect("owner_change", self.on_clipboard_owner_change)
self.clipboard_semaphore = 0

def shutdown(self):
self.copy_buffer = set()
self.event_manager.unsubscribe(self._update)

def on_clipboard_owner_change(self, clipboard, event):
if self.clipboard_semaphore > 0:
self.clipboard_semaphore -= 1
else:
self.copy_buffer = set()
view = self.diagrams.get_current_view()
self.update_paste_state(view)

@event_handler(DiagramSelectionChanged)
def _update(self, event):
diagram_view = event.diagram_view
Expand Down Expand Up @@ -86,6 +100,8 @@ def update_paste_state(self, view):
def copy_action(self):
view = self.diagrams.get_current_view()
if view.is_focus():
self.clipboard_semaphore += 1
self.clipboard.set_text("", -1)
items = view.selected_items
self.copy(items)
self.update_paste_state(view)
Expand All @@ -94,6 +110,8 @@ def copy_action(self):
def cut_action(self):
view = self.diagrams.get_current_view()
if view.is_focus():
self.clipboard_semaphore += 1
self.clipboard.set_text("", -1)
items = view.selected_items
self.copy(items)
for i in list(items):
Expand All @@ -104,7 +122,7 @@ def cut_action(self):
def paste_action(self):
view = self.diagrams.get_current_view()
diagram = self.diagrams.get_current_diagram()
if not view:
if not (view and view.is_focus()):
return

if not self.copy_buffer:
Expand Down

0 comments on commit 356757d

Please sign in to comment.