Class: Ruflet::Control

Inherits:
Object
  • Object
show all
Defined in:
lib/ruflet_ui/ruflet/control.rb

Direct Known Subclasses

UI::Controls::RufletComponents::AlertDialogControl, UI::Controls::RufletComponents::AnimatedSwitcherControl, UI::Controls::RufletComponents::AppBarControl, UI::Controls::RufletComponents::ArcControl, UI::Controls::RufletComponents::AudioControl, UI::Controls::RufletComponents::AutoCompleteControl, UI::Controls::RufletComponents::AutofillGroupControl, UI::Controls::RufletComponents::BadgeControl, UI::Controls::RufletComponents::BannerControl, UI::Controls::RufletComponents::BarChartControl, UI::Controls::RufletComponents::BarChartGroupControl, UI::Controls::RufletComponents::BarChartRodControl, UI::Controls::RufletComponents::BarChartRodStackItemControl, UI::Controls::RufletComponents::BasePageControl, UI::Controls::RufletComponents::BottomAppBarControl, UI::Controls::RufletComponents::BottomSheetControl, UI::Controls::RufletComponents::BrowserContextMenuControl, UI::Controls::RufletComponents::ButtonControl, UI::Controls::RufletComponents::CandlestickChartControl, UI::Controls::RufletComponents::CandlestickChartSpotControl, UI::Controls::RufletComponents::CanvasControl, UI::Controls::RufletComponents::CardControl, UI::Controls::RufletComponents::ChartAxisControl, UI::Controls::RufletComponents::ChartAxisLabelControl, UI::Controls::RufletComponents::CheckboxControl, UI::Controls::RufletComponents::ChipControl, UI::Controls::RufletComponents::CircleAvatarControl, UI::Controls::RufletComponents::CircleControl, UI::Controls::RufletComponents::ColorControl, UI::Controls::RufletComponents::ColumnControl, UI::Controls::RufletComponents::ContainerControl, UI::Controls::RufletComponents::ContextMenuControl, UI::Controls::RufletComponents::CupertinoActionSheetActionControl, UI::Controls::RufletComponents::CupertinoActionSheetControl, UI::Controls::RufletComponents::CupertinoActivityIndicatorControl, UI::Controls::RufletComponents::CupertinoAlertDialogControl, UI::Controls::RufletComponents::CupertinoAppBarControl, UI::Controls::RufletComponents::CupertinoBottomSheetControl, UI::Controls::RufletComponents::CupertinoButtonControl, UI::Controls::RufletComponents::CupertinoCheckboxControl, UI::Controls::RufletComponents::CupertinoContextMenuActionControl, UI::Controls::RufletComponents::CupertinoContextMenuControl, UI::Controls::RufletComponents::CupertinoDatePickerControl, UI::Controls::RufletComponents::CupertinoDialogActionControl, UI::Controls::RufletComponents::CupertinoFilledButtonControl, UI::Controls::RufletComponents::CupertinoListTileControl, UI::Controls::RufletComponents::CupertinoNavigationBarControl, UI::Controls::RufletComponents::CupertinoPickerControl, UI::Controls::RufletComponents::CupertinoRadioControl, UI::Controls::RufletComponents::CupertinoSegmentedButtonControl, UI::Controls::RufletComponents::CupertinoSliderControl, UI::Controls::RufletComponents::CupertinoSlidingSegmentedButtonControl, UI::Controls::RufletComponents::CupertinoSwitchControl, UI::Controls::RufletComponents::CupertinoTextFieldControl, UI::Controls::RufletComponents::CupertinoTimerPickerControl, UI::Controls::RufletComponents::CupertinoTintedButtonControl, UI::Controls::RufletComponents::DataCellControl, UI::Controls::RufletComponents::DataColumnControl, UI::Controls::RufletComponents::DataRowControl, UI::Controls::RufletComponents::DataTableControl, UI::Controls::RufletComponents::DatePickerControl, UI::Controls::RufletComponents::DateRangePickerControl, UI::Controls::RufletComponents::DialogsControl, UI::Controls::RufletComponents::DismissibleControl, UI::Controls::RufletComponents::DividerControl, UI::Controls::RufletComponents::DragTargetControl, UI::Controls::RufletComponents::DraggableControl, UI::Controls::RufletComponents::Dropdown2Control, UI::Controls::RufletComponents::DropdownControl, UI::Controls::RufletComponents::DropdownOptionControl, UI::Controls::RufletComponents::ExpansionPanelControl, UI::Controls::RufletComponents::ExpansionPanelListControl, UI::Controls::RufletComponents::ExpansionTileControl, UI::Controls::RufletComponents::FillControl, UI::Controls::RufletComponents::FilledButtonControl, UI::Controls::RufletComponents::FilledIconButtonControl, UI::Controls::RufletComponents::FilledTonalButtonControl, UI::Controls::RufletComponents::FilledTonalIconButtonControl, UI::Controls::RufletComponents::FletAppControl, UI::Controls::RufletComponents::FloatingActionButtonControl, UI::Controls::RufletComponents::GestureDetectorControl, UI::Controls::RufletComponents::GridViewControl, UI::Controls::RufletComponents::HeroControl, UI::Controls::RufletComponents::IconButtonControl, UI::Controls::RufletComponents::IconControl, UI::Controls::RufletComponents::ImageControl, UI::Controls::RufletComponents::InteractiveViewerControl, UI::Controls::RufletComponents::KeyboardListenerControl, UI::Controls::RufletComponents::LineChartControl, UI::Controls::RufletComponents::LineChartDataControl, UI::Controls::RufletComponents::LineChartDataPointControl, UI::Controls::RufletComponents::LineControl, UI::Controls::RufletComponents::ListTileControl, UI::Controls::RufletComponents::ListViewControl, UI::Controls::RufletComponents::MarkdownControl, UI::Controls::RufletComponents::MenuBarControl, UI::Controls::RufletComponents::MenuItemButtonControl, UI::Controls::RufletComponents::MergeSemanticsControl, UI::Controls::RufletComponents::NavigationBarControl, UI::Controls::RufletComponents::NavigationBarDestinationControl, UI::Controls::RufletComponents::NavigationDrawerControl, UI::Controls::RufletComponents::NavigationDrawerDestinationControl, UI::Controls::RufletComponents::NavigationRailControl, UI::Controls::RufletComponents::NavigationRailDestinationControl, UI::Controls::RufletComponents::OptionControl, UI::Controls::RufletComponents::OutlinedButtonControl, UI::Controls::RufletComponents::OutlinedIconButtonControl, UI::Controls::RufletComponents::OvalControl, UI::Controls::RufletComponents::OverlayControl, UI::Controls::RufletComponents::PageControl, UI::Controls::RufletComponents::PageViewControl, UI::Controls::RufletComponents::PageletControl, UI::Controls::RufletComponents::PathControl, UI::Controls::RufletComponents::PieChartControl, UI::Controls::RufletComponents::PieChartSectionControl, UI::Controls::RufletComponents::PlaceholderControl, UI::Controls::RufletComponents::PointsControl, UI::Controls::RufletComponents::PopupMenuButtonControl, UI::Controls::RufletComponents::PopupMenuItemControl, UI::Controls::RufletComponents::ProgressBarControl, UI::Controls::RufletComponents::ProgressRingControl, UI::Controls::RufletComponents::RadarChartControl, UI::Controls::RufletComponents::RadarChartTitleControl, UI::Controls::RufletComponents::RadarDataSetControl, UI::Controls::RufletComponents::RadarDataSetEntryControl, UI::Controls::RufletComponents::RadioControl, UI::Controls::RufletComponents::RadioGroupControl, UI::Controls::RufletComponents::RangeSliderControl, UI::Controls::RufletComponents::RectControl, UI::Controls::RufletComponents::ReorderableDragHandleControl, UI::Controls::RufletComponents::ReorderableListViewControl, UI::Controls::RufletComponents::ResponsiveRowControl, UI::Controls::RufletComponents::RowControl, UI::Controls::RufletComponents::SafeAreaControl, UI::Controls::RufletComponents::ScatterChartControl, UI::Controls::RufletComponents::ScatterChartSpotControl, UI::Controls::RufletComponents::SearchBarControl, UI::Controls::RufletComponents::SegmentControl, UI::Controls::RufletComponents::SegmentedButtonControl, UI::Controls::RufletComponents::SelectionAreaControl, UI::Controls::RufletComponents::SemanticsControl, UI::Controls::RufletComponents::ServiceRegistryControl, UI::Controls::RufletComponents::ShaderMaskControl, UI::Controls::RufletComponents::ShadowControl, UI::Controls::RufletComponents::ShimmerControl, UI::Controls::RufletComponents::SliderControl, UI::Controls::RufletComponents::SnackBarControl, UI::Controls::RufletComponents::StackControl, UI::Controls::RufletComponents::SubmenuButtonControl, UI::Controls::RufletComponents::SwitchControl, UI::Controls::RufletComponents::TabBarControl, UI::Controls::RufletComponents::TabBarViewControl, UI::Controls::RufletComponents::TabControl, UI::Controls::RufletComponents::TabsControl, UI::Controls::RufletComponents::TextButtonControl, UI::Controls::RufletComponents::TextControl, UI::Controls::RufletComponents::TextFieldControl, UI::Controls::RufletComponents::TextSpanControl, UI::Controls::RufletComponents::TimePickerControl, UI::Controls::RufletComponents::TransparentPointerControl, UI::Controls::RufletComponents::VerticalDividerControl, UI::Controls::RufletComponents::VideoControl, UI::Controls::RufletComponents::ViewControl, UI::Controls::RufletComponents::WebViewControl, UI::Controls::RufletComponents::WindowControl, UI::Controls::RufletComponents::WindowDragAreaControl, UI::Services::RufletServicesComponents::AccelerometerControl, UI::Services::RufletServicesComponents::BarometerControl, UI::Services::RufletServicesComponents::BatteryControl, UI::Services::RufletServicesComponents::CameraControl, UI::Services::RufletServicesComponents::ClipboardControl, UI::Services::RufletServicesComponents::ConnectivityControl, UI::Services::RufletServicesComponents::FilePickerControl, UI::Services::RufletServicesComponents::FlashlightControl, UI::Services::RufletServicesComponents::GyroscopeControl, UI::Services::RufletServicesComponents::HapticFeedbackControl, UI::Services::RufletServicesComponents::MagnetometerControl, UI::Services::RufletServicesComponents::ScreenBrightnessControl, UI::Services::RufletServicesComponents::ScreenshotControl, UI::Services::RufletServicesComponents::SemanticsServiceControl, UI::Services::RufletServicesComponents::ShakeDetectorControl, UI::Services::RufletServicesComponents::ShareControl, UI::Services::RufletServicesComponents::SharedPreferencesControl, UI::Services::RufletServicesComponents::StoragePathsControl, UI::Services::RufletServicesComponents::UrlLauncherControl, UI::Services::RufletServicesComponents::UserAccelerometerControl, UI::Services::RufletServicesComponents::WakelockControl

Constant Summary collapse

HOST_EXPANDED_TYPES =
%w[view row column].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, id: nil, **props) ⇒ Control

Returns a new instance of Control.



20
21
22
23
24
25
26
27
# File 'lib/ruflet_ui/ruflet/control.rb', line 20

def initialize(type:, id: nil, **props)
  @type = type.to_s.downcase
  @id = (id || props.delete(:id) || "ctrl_#{self.class.generate_id}").to_s
  @children = []
  @handlers = {}
  @wire_id = nil
  @props = normalize_props(extract_handlers(preprocess_props(props)))
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



17
18
19
# File 'lib/ruflet_ui/ruflet/control.rb', line 17

def children
  @children
end

#idObject (readonly)

Returns the value of attribute id.



17
18
19
# File 'lib/ruflet_ui/ruflet/control.rb', line 17

def id
  @id
end

#propsObject (readonly)

Returns the value of attribute props.



17
18
19
# File 'lib/ruflet_ui/ruflet/control.rb', line 17

def props
  @props
end

#runtime_pageObject

Returns the value of attribute runtime_page.



18
19
20
# File 'lib/ruflet_ui/ruflet/control.rb', line 18

def runtime_page
  @runtime_page
end

#typeObject (readonly)

Returns the value of attribute type.



17
18
19
# File 'lib/ruflet_ui/ruflet/control.rb', line 17

def type
  @type
end

#wire_idObject

Returns the value of attribute wire_id.



18
19
20
# File 'lib/ruflet_ui/ruflet/control.rb', line 18

def wire_id
  @wire_id
end

Class Method Details

.generate_idObject



78
79
80
81
82
83
84
# File 'lib/ruflet_ui/ruflet/control.rb', line 78

def generate_id
  if defined?(SecureRandom) && SecureRandom.respond_to?(:hex)
    SecureRandom.hex(4)
  else
    format("%08x", rand(0..0xffff_ffff))
  end
end

Instance Method Details

#emit(event_name, event) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/ruflet_ui/ruflet/control.rb', line 38

def emit(event_name, event)
  handler = @handlers[normalized_event_name(event_name)]
  return false unless handler

  handler.call(event)
  true
end

#has_handler?(event_name) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/ruflet_ui/ruflet/control.rb', line 46

def has_handler?(event_name)
  @handlers.key?(normalized_event_name(event_name))
end

#on(event_name, &block) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/ruflet_ui/ruflet/control.rb', line 29

def on(event_name, &block)
  name = normalized_event_name(event_name)
  validate_event_name!(name)
  @handlers[name] = block
  @props["on_#{name}"] = true
  runtime_page&.update(self, "on_#{name}": true) if wire_id
  self
end

#to_patchObject

Raises:

  • (ArgumentError)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ruflet_ui/ruflet/control.rb', line 50

def to_patch
  wire_type = schema_wire_type_for_class
  if wire_type.nil?
    compact_type_key = type.delete("_")
    wire_type = type_map[type] || type_map[compact_type_key]
  end
  raise ArgumentError, "Unknown control type: #{type}" unless wire_type
  patch = {
    "_c" => wire_type,
    "_i" => wire_id
  }

  internals = {}
  internals["host_positioned"] = true if type == "stack"
  internals["host_expanded"] = true if HOST_EXPANDED_TYPES.include?(type)
  patch["_internals"] = internals unless internals.empty?

  props.each { |k, v| patch[k] = serialize_value(v) }
  patch["controls"] = children.map(&:to_patch) unless children.empty?
  if ENV["RUFLET_DEBUG"] == "1" && type == "floatingactionbutton"
    Kernel.warn("[to_patch] #{patch.inspect}")
  end
  patch
end