Class: Scarpe::Webview::DisplayService

Inherits:
Shoes::DisplayService show all
Includes:
Shoes::Log
Defined in:
lib/scarpe/wv/webview_local_display.rb

Overview

This is the simplest type of Webview DisplayService. It creates Webview drawables corresponding to Shoes drawables, manages the Webview and its DOM tree, and generally keeps the Shoes/Webview connection working.

This is an in-process Webview-based display service, with all the limitations that entails. Slow handlers will crash, ending this display service will end the process, too many or too large evals can crash the process, etc. Frequently it's better to use a RelayDisplayService to a second process containing one of these.

Constant Summary

Constants included from Shoes::Log

Shoes::Log::DEFAULT_COMPONENT, Shoes::Log::DEFAULT_DEBUG_LOG_CONFIG, Shoes::Log::DEFAULT_LOG_CONFIG

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Shoes::Log

configure_logger, #log_init, logger

Methods inherited from Shoes::DisplayService

dispatch_event, display_service, full_reset!, #query_display_drawable_for, set_display_service_class, #set_drawable_pairing, subscribe_to_event, unsub_from_events

Constructor Details

#initializeDisplayService

This is called before any of the various Webview::Drawables are created, to be able to create them and look them up.



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/scarpe/wv/webview_local_display.rb', line 34

def initialize
  if Webview::DisplayService.instance
    raise Shoes::Errors::SingletonError, "ERROR! This is meant to be a singleton!"
  end

  Webview::DisplayService.instance = self

  super()
  log_init("Webview::DisplayService")

  @display_drawable_for = {}
end

Class Attribute Details

.instanceObject

Returns the value of attribute instance.



17
18
19
# File 'lib/scarpe/wv/webview_local_display.rb', line 17

def instance
  @instance
end

Instance Attribute Details

#appObject (readonly)

app is the Scarpe::Webview::App



27
28
29
# File 'lib/scarpe/wv/webview_local_display.rb', line 27

def app
  @app
end

#control_interfaceObject (readonly)

The ControlInterface is used to handle internal events in Webview Scarpe



21
22
23
# File 'lib/scarpe/wv/webview_local_display.rb', line 21

def control_interface
  @control_interface
end

#doc_rootObject (readonly)

The DocumentRoot is the top drawable of the Webview-side drawable tree



24
25
26
# File 'lib/scarpe/wv/webview_local_display.rb', line 24

def doc_root
  @doc_root
end

#wranglerObject (readonly)

wrangler is the Scarpe::WebWrangler



30
31
32
# File 'lib/scarpe/wv/webview_local_display.rb', line 30

def wrangler
  @wrangler
end

Instance Method Details

#create_display_drawable_for(drawable_class_name, drawable_id, properties, is_widget:) ⇒ Webview::Drawable

Create a Webview display drawable for a specific Shoes drawable, and pair it with the linkable ID for this Shoes drawable.

Parameters:

  • The class name of the Shoes drawable, e.g. Shoes::Button

  • the linkable ID for drawable events

  • a JSON-serialisable Hash with the drawable's Shoes styles

  • whether the class is a user-defined Shoes::Widget subclass

Returns:

  • the newly-created Webview drawable



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/scarpe/wv/webview_local_display.rb', line 55

def create_display_drawable_for(drawable_class_name, drawable_id, properties, is_widget:)
  existing = query_display_drawable_for(drawable_id, nil_ok: true)
  if existing
    @log.warn("There is already a display drawable for #{drawable_id.inspect}! Returning #{existing.class.name}.")
    return existing
  end

  if drawable_class_name == "App"
    unless @doc_root
      raise Scarpe::MissingDocRootError, "Webview::DocumentRoot is supposed to be created before Webview::App!"
    end

    display_app = Scarpe::Webview::App.new(properties)
    display_app.document_root = @doc_root
    @control_interface = display_app.control_interface
    @control_interface.doc_root = @doc_root
    @app = @control_interface.app
    @wrangler = @control_interface.wrangler

    set_drawable_pairing(drawable_id, display_app)

    return display_app
  end

  # Create a corresponding display drawable

  if is_widget
    display_class = Scarpe::Webview::Flow
  else
    display_class = Scarpe::Webview::Drawable.display_class_for(drawable_class_name)
    unless display_class < Scarpe::Webview::Drawable
      raise Scarpe::BadDisplayClassType, "Wrong display class type #{display_class.inspect} for class name #{drawable_class_name.inspect}!"
    end
  end
  display_drawable = display_class.new(properties)
  set_drawable_pairing(drawable_id, display_drawable)

  if drawable_class_name == "DocumentRoot"
    # DocumentRoot is created before App. Mostly doc_root is just like any other drawable,
    # but we'll want a reference to it when we create App.
    @doc_root = display_drawable
  end

  display_drawable
end

#destroyvoid

This method returns an undefined value.

Destroy the display service and the app. Quit the process (eventually.)



104
105
106
107
# File 'lib/scarpe/wv/webview_local_display.rb', line 104

def destroy
  @app.destroy
  Webview::DisplayService.instance = nil
end