Class: Charming::Controller

Inherits:
Object
  • Object
show all
Extended by:
ClassMethods
Includes:
CommandPalette, ComponentDispatching, Dispatching, FocusManagement, Rendering, SessionState, SidebarNavigation
Defined in:
lib/charming/controller.rb,
lib/charming/controller/rendering.rb,
lib/charming/controller/dispatching.rb,
lib/charming/controller/class_methods.rb,
lib/charming/controller/session_state.rb,
lib/charming/controller/command_palette.rb,
lib/charming/controller/focus_management.rb,
lib/charming/controller/sidebar_navigation.rb,
lib/charming/controller/component_dispatching.rb

Overview

Controller is the base class for all controller implementations in a Charming application. It provides the action dispatch pipeline, key/command/timer/task bindings, sidebar navigation, command palette management, and view rendering with layout composition.

Defined Under Namespace

Modules: ClassMethods, CommandPalette, ComponentDispatching, Dispatching, FocusManagement, Rendering, SessionState, SidebarNavigation Classes: TaskBinding, TimerBinding

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ClassMethods

auto_render, auto_render_action, command, command_bindings, focus_ring, focus_ring_slots, key, key_binding_scopes, key_bindings, layout, on_task, task_bindings, timer, timer_bindings

Methods included from CommandPalette

#close_command_palette, #command_palette, #command_palette_open?, #open_command_palette

Methods included from SidebarNavigation

#content_focused?, #current_route?, #focus_content, #focus_sidebar, #sidebar_focused?, #sidebar_index, #sidebar_routes

Methods included from FocusManagement

#focus, #focused?

Methods included from SessionState

#form, #run_task, #session, #state

Constructor Details

#initialize(application:, event: nil, params: {}, screen: nil, route: nil) ⇒ Controller

Initializes the controller with its parent application and optional event. Defaults to an 80x24 screen when no backend size is available.



24
25
26
27
28
29
30
31
# File 'lib/charming/controller.rb', line 24

def initialize(application:, event: nil, params: {}, screen: nil, route: nil)
  @application = application
  @event = event
  @params = params
  @screen = screen || Screen.new(width: 80, height: 24)
  @route = route
  @response = nil
end

Instance Attribute Details

#applicationObject (readonly)

Returns the value of attribute application.



20
21
22
# File 'lib/charming/controller.rb', line 20

def application
  @application
end

#eventObject (readonly)

Returns the value of attribute event.



20
21
22
# File 'lib/charming/controller.rb', line 20

def event
  @event
end

#paramsObject (readonly)

Returns the value of attribute params.



20
21
22
# File 'lib/charming/controller.rb', line 20

def params
  @params
end

#routeObject (readonly)

Returns the value of attribute route.



20
21
22
# File 'lib/charming/controller.rb', line 20

def route
  @route
end

#screenObject (readonly)

Returns the value of attribute screen.



20
21
22
# File 'lib/charming/controller.rb', line 20

def screen
  @screen
end

Instance Method Details

#dispatch(action) ⇒ Object

Dispatches a named action on this controller (e.g. :show).



34
35
36
37
38
# File 'lib/charming/controller.rb', line 34

def dispatch(action)
  public_send(action)
  render_default_action if response.nil? && auto_render_after?(action)
  response || render("")
end

#dispatch_keyObject

Key event dispatch: checks command palette first, then global bindings, sidebar (if focused), content bindings, tab traversal, and focused component.



42
43
44
45
46
47
48
49
50
# File 'lib/charming/controller.rb', line 42

def dispatch_key
  return dispatch_command_palette_key if command_palette_open?
  return dispatch(global_key_action) if global_key_action
  return dispatch_sidebar_key if sidebar_focused?
  return dispatch(content_key_action) if content_key_action
  return response if dispatch_tab_traversal == :handled
  return response if dispatch_to_focused_component == :handled
  nil
end

#dispatch_mouseObject

Mouse event dispatcher: checks command palette (if open), sidebar (if focused).



68
69
70
71
72
# File 'lib/charming/controller.rb', line 68

def dispatch_mouse
  return dispatch_command_palette_mouse if command_palette_open?
  return dispatch_sidebar_mouse if sidebar_focused?
  dispatch_component_mouse
end

#dispatch_taskObject

Task event dispatcher: looks up the handler in task bindings.



62
63
64
65
# File 'lib/charming/controller.rb', line 62

def dispatch_task
  b = self.class.task_bindings[event.name.to_sym]
  b ? dispatch(b.action) : nil
end

#dispatch_timerObject

Timer event dispatcher: looks up the named action in timer bindings.



53
54
55
56
57
58
59
# File 'lib/charming/controller.rb', line 53

def dispatch_timer
  b = self.class.timer_bindings[event.name.to_sym]
  return nil unless b

  public_send(b.action)
  response
end

Navigates to the given URL path.



108
109
110
# File 'lib/charming/controller.rb', line 108

def navigate_to(path)
  @response = Response.navigate(path)
end

#open_theme_paletteObject

Opens the theme picker (a CommandPalette populated with the registered themes) and renders.



101
102
103
104
105
# File 'lib/charming/controller.rb', line 101

def open_theme_palette
  session[:command_palette] = command_palette_state(:themes)
  focus.push_scope([:command_palette], origin: :command_palette)
  render_default_action
end

#quitObject

Exits the application — sets a quit response that terminates the event loop.



113
114
115
# File 'lib/charming/controller.rb', line 113

def quit
  @response = Response.quit
end

#render(body = "", **assigns) ⇒ Object

Renders a body or template wrapped in the controller’s layout.



75
76
77
78
# File 'lib/charming/controller.rb', line 75

def render(body = "", **assigns)
  body = view_body(default_template_name(body), **assigns) if body.is_a?(Symbol)
  @response = Response.render(render_with_layout(body))
end

#render_template(name, **assigns) ⇒ Object

Renders a template from ‘app/views` by name, applying the controller’s layout. name is the template path (e.g., “home/show”) and additional keyword assigns are forwarded to the view.



86
87
88
# File 'lib/charming/controller.rb', line 86

def render_template(name, **assigns)
  @response = Response.render(render_with_layout(template_body(name, **assigns)))
end

#render_view(view_class, **assigns) ⇒ Object



80
81
82
# File 'lib/charming/controller.rb', line 80

def render_view(view_class, **assigns)
  @response = Response.render(render_with_layout(view_class.new(**template_assigns(assigns))))
end

#themeObject

Returns the active theme for this request, delegated to the application.



91
92
93
# File 'lib/charming/controller.rb', line 91

def theme
  application.theme
end

#use_theme(name) ⇒ Object

Switches the active theme to name and persists the choice in the application session.



96
97
98
# File 'lib/charming/controller.rb', line 96

def use_theme(name)
  application.use_theme(name)
end