<commit_before>class Shoes
  module Swt
    class Image
      import java.io.ByteArrayInputStream

      include Common::Child
      include Common::Resource
      include Common::Clickable
      include Common::Toggle
      include Common::Clear
      include ::Shoes::BackendDimensionsDelegations

      BINARY_ENCODING = Encoding.find('binary')

      attr_reader :parent, :real, :dsl, :container, :painter

      def initialize(dsl, parent, blk)
        @dsl = dsl
        @app = @parent = parent
        @container = @parent.real

        load_image(@dsl.file_path)

        add_paint_listener

        clickable blk if blk
      end

      def update_image
        load_image(@dsl.file_path)
      end

      # it has a painter and therefore this does not need to do anything,
      # as it was already changed on the DSL layer
      # We might want to revisit that though.
      def move(x, y)
      end

      private
      def load_image(name_or_data)
        if url?(name_or_data)
          display_temporary_download_image
          download_and_display_real_image(name_or_data)
        else
          display_image(name_or_data)
        end
      end

      def url?(name_or_data)
        name_or_data =~ /^(http|https):\/\//
      end

      def display_temporary_download_image
        temporary_download_image = File.join(DIR, 'static/downloading.png')
        create_image(temporary_download_image)
      end

      def create_image(data)
        @real        = ::Swt::Graphics::Image.new(::Swt.display, data)
        @full_width  = @real.getImageData.width
        @full_height = @real.getImageData.height
        dsl.width    = dsl.width || default_width
        dsl.height   = dsl.height || default_height
      end

      def default_width
        if dsl.height
          ratio = dsl.height.to_r / @full_height
          (@full_width * ratio).to_i
        else
          @full_width
        end
      end

      def default_height
        if dsl.width
          ratio = dsl.width.to_r / @full_width
          (@full_height * ratio).to_i
        else
          @full_height
        end
      end

      def download_and_display_real_image(url)
        @tmpname = File.join(Dir.tmpdir, "__shoes4_#{Time.now.to_f}.png") unless @tmpname_or_data
        @dsl.app.download url, save: @tmpname do
          create_image @tmpname
        end<commit_msg>Fix dimensions problem after dling image<commit_after>class Shoes
  module Swt
    class Image
      import java.io.ByteArrayInputStream

      include Common::Child
      include Common::Resource
      include Common::Clickable
      include Common::Toggle
      include Common::Clear
      include ::Shoes::BackendDimensionsDelegations

      BINARY_ENCODING = Encoding.find('binary')

      attr_reader :parent, :real, :dsl, :container, :painter

      def initialize(dsl, parent, blk)
        @dsl = dsl
        @app = @parent = parent
        @container = @parent.real

        load_image(@dsl.file_path)

        add_paint_listener

        clickable blk if blk
      end

      def update_image
        load_image(@dsl.file_path)
      end

      # it has a painter and therefore this does not need to do anything,
      # as it was already changed on the DSL layer
      # We might want to revisit that though.
      def move(x, y)
      end

      private
      def load_image(name_or_data)
        if url?(name_or_data)
          save_width_and_height
          display_temporary_download_image
          download_and_display_real_image(name_or_data)
        else
          display_image(name_or_data)
        end
      end

      def url?(name_or_data)
        name_or_data =~ /^(http|https):\/\//
      end

      def save_width_and_height
        @saved_width  = dsl.width
        @saved_height = dsl.height
      end

      def display_temporary_download_image
        temporary_download_image = File.join(DIR, 'static/downloading.png')
        create_image(temporary_download_image)
      end

      def create_image(data)
        @real        = ::Swt::Graphics::Image.new(::Swt.display, data)
        @full_width  = @real.getImageData.width
        @full_height = @real.getImageData.height
        dsl.width    = dsl.width || default_width
        dsl.height   = dsl.height || default_height
      end

      def default_width
        if dsl.height
          ratio = dsl.height.to_r / @full_height
          (@full_width * ratio).to_i
        else
          @full_width
        end
      end

      def default_height
        if dsl.width
          ratio = dsl.width.to_r / @full_width
          (@full_height * ratio).to_i
        else
          @full_height
        end
      end

      def download_and_display_real_image(url)
        @tmpname = File.join(Dir.tmpdir, "__shoes4_#{Time.now.to_f}.png") unless @tmpname_or_data
        @dsl.app.download url, save: @tmpname do
          restore_width_and_height
          create_image @tmpname
        end
      end

      def restore_width_and_height
        dsl.width  = @saved_width
        dsl.height = @saved_height
{'fraction_non_alphanumeric': 0.07602454959413978, 'fraction_numerical': 0.0003959611958028113, 'mean_word_length': 2.0544135429262393, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '317736', 'n_tokens_mistral': 1561, 'n_tokens_neox': 1524, 'n_words': 346}