Sha256: 6a65f8cb0a03f3f9f560055489d5abf5432e56a2b37788ffe2c43621390dfa37

Contents?: true

Size: 1.6 KB

Versions: 3

Compression:

Stored size: 1.6 KB

Contents

# frozen_string_literal: true
class Shoes
  module Common
    module Hover
      attr_reader :hover_blk, :leave_blk

      def self.included(base)
        clazz = Class.new {}
        name = base.name.split("::").last
        Shoes.const_set("#{name}Hover", clazz)
      end

      def hover(&blk)
        @hover_blk = blk
        add_mouse_hover_control
      end

      def leave(&blk)
        @leave_blk = blk
        add_mouse_hover_control
      end

      def hovered?
        @hovered
      end

      def hover_class
        return @hover_class if @hover_class

        name = self.class.name.split("::").last
        @hover_class = Shoes.const_get("#{name}Hover")
      end

      def mouse_hovered
        return if @hovered

        @hovered = true

        apply_style_from_hover_class
        eval_hover_block(@hover_blk)
      end

      def mouse_left
        return unless @hovered

        @hovered = false

        apply_style_from_pre_hover
        eval_hover_block(@leave_blk)
      end

      def add_mouse_hover_control
        app.add_mouse_hover_control(self)
      end

      def eval_hover_block(blk)
        safely_evaluate(self, &blk) if blk
      end

      def apply_style_from_hover_class
        hover_style = @app.element_styles[hover_class]
        return unless hover_style

        @pre_hover_style = hover_style.each_with_object({}) do |(key, _), memo|
          memo[key] = style[key]
          memo
        end

        style(hover_style)
      end

      def apply_style_from_pre_hover
        style(@pre_hover_style) if @pre_hover_style
        @pre_hover_style = nil
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
shoes-core-4.0.0.pre11 lib/shoes/common/hover.rb
shoes-core-4.0.0.pre10 lib/shoes/common/hover.rb
shoes-core-4.0.0.pre9 lib/shoes/common/hover.rb