Sha256: e92b42db012f22050745da87daf522eb1080e6da0bcd6064fae2291c37847078

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

require_relative "implementation"

module Clipboard
  # Ruby-Gnome2 based implementation
  # Requires either the gtk3 or the gtk2 gem
  module Gtk
    include Implementation
    extend self

    CLIPBOARDS = %w[clipboard primary secondary].freeze

    unless defined? ::Gtk
      begin
        require 'gtk3'
      rescue LoadError
        begin
          require 'gtk2'
        rescue LoadError
          raise LoadError, 'Could not load the required gtk3 or gtk2 gem, please install it with: gem install gtk3'
        end
      end
    end

    def paste(which = nil, clipboard: "clipboard")
      selection = which || clipboard
      raise ArgumentError, "unknown clipboard selection" unless CLIPBOARDS.include?(selection)

      ::Gtk::Clipboard.get(
        Gdk::Selection.const_get(selection.to_s.upcase)
      ).wait_for_text || ""
    end

    def copy(data, clipboard: "all")
      selections = clipboard.to_s == "all" ? CLIPBOARDS : [clipboard]
      selections.each{ |selection|
        raise ArgumentError, "unknown clipboard selection" unless CLIPBOARDS.include?(selection)

        ::Gtk::Clipboard.get(Gdk::Selection.const_get(selection.to_s.upcase)).set_text(data).store
      }

      true
    end

    def clear(clipboard: "all")
      selections = clipboard.to_s == "all" ? CLIPBOARDS : [clipboard]
      selections.each{ |selection|
        raise ArgumentError, "unknown clipboard selection" unless CLIPBOARDS.include?(selection)

        ::Gtk::Clipboard.get(Gdk::Selection.const_get(selection.to_s.upcase)).clear
      }

      true
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
clipboard-2.0.0 lib/clipboard/gtk.rb