Class: Fusuma::Plugin::Remap::LayerManager

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/fusuma/plugin/remap/layer_manager.rb

Overview

Set layer sent from pipe

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLayerManager

Returns a new instance of LayerManager.



13
14
15
16
17
18
19
# File 'lib/fusuma/plugin/remap/layer_manager.rb', line 13

def initialize
  @layers = {}
  @reader, @writer = IO.pipe
  @current_layer = {} # preserve order
  @last_layer = nil
  @last_remove = nil
end

Instance Attribute Details

#current_layerObject (readonly)

Returns the value of attribute current_layer.



11
12
13
# File 'lib/fusuma/plugin/remap/layer_manager.rb', line 11

def current_layer
  @current_layer
end

#layersObject (readonly)

Returns the value of attribute layers.



11
12
13
# File 'lib/fusuma/plugin/remap/layer_manager.rb', line 11

def layers
  @layers
end

#readerObject (readonly)

Returns the value of attribute reader.



11
12
13
# File 'lib/fusuma/plugin/remap/layer_manager.rb', line 11

def reader
  @reader
end

#writerObject (readonly)

Returns the value of attribute writer.



11
12
13
# File 'lib/fusuma/plugin/remap/layer_manager.rb', line 11

def writer
  @writer
end

Instance Method Details

#find_mapping(layer) ⇒ Hash

Find remap layer from config

Parameters:

  • layer (Hash)

Returns:

  • (Hash)

    remap layer



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/fusuma/plugin/remap/layer_manager.rb', line 64

def find_mapping(layer)
  @layers[layer] ||= begin
    result = nil
    _ = Fusuma::Config::Searcher.find_context(layer) do
      result = Fusuma::Config.search(Fusuma::Config::Index.new(:remap))
      next unless result

      result = result.deep_transform_keys { |key| key.upcase.to_sym }
    end

    result || {}
  end
end

#receive_layerHash

Read layer from pipe and update @current_layer

Examples:

receive_layer
# => { thumbsense: true }
receive_layer
# => { thumbsense: true, application: "Google-chrome" }

Returns:

  • (Hash)

    current layer



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/fusuma/plugin/remap/layer_manager.rb', line 38

def receive_layer
  @layer_unpacker ||= MessagePack::Unpacker.new(@reader)

  data = @layer_unpacker.unpack

  return unless data.is_a? Hash

  data = data.deep_symbolize_keys
  layer = data[:layer] # e.g { thumbsense: true }
  remove = data[:remove] # e.g true

  # update @current_layer
  if remove
    MultiLogger.debug "Remove layer: #{layer}"
    @current_layer.delete_if { |k, _v| layer.key?(k) }
  else
    MultiLogger.debug "Add layer: #{layer}"
    # If duplicate keys exist, order of keys is preserved
    @current_layer.merge!(layer)
  end
  @current_layer
end

#send_layer(layer:, remove: false) ⇒ Object

Parameters:

  • layer (Hash)
  • remove (Boolean) (defaults to: false)


23
24
25
26
27
28
29
# File 'lib/fusuma/plugin/remap/layer_manager.rb', line 23

def send_layer(layer:, remove: false)
  return if (@last_layer == layer) && (@last_remove == remove)

  @last_layer = layer
  @last_remove = remove
  @writer.write({layer: layer, remove: remove}.to_msgpack)
end