Module: Charming::Generators::AppGenerator::LayoutTemplate

Included in:
Charming::Generators::AppGenerator
Defined in:
lib/charming/generators/app_generator/layout_template.rb

Instance Method Summary collapse

Instance Method Details

#layoutObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/charming/generators/app_generator/layout_template.rb', line 7

def layout
  %(# frozen_string_literal: true

module #{name.class_name}
  module Layouts
    class Application < Charming::View
      def render
body = Charming::UI.place(app_frame, width: screen.width, height: screen.height)
return body unless palette

Charming::UI.overlay(body, command_palette_modal)
      end
#{layout_helpers}
    end
  end
end
)
end

#layout_dimension_helpersObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/charming/generators/app_generator/layout_template.rb', line 136

def layout_dimension_helpers
  %(

      def narrow?
screen.width < 72 && screen.height >= 20
      end

      def sidebar_width
narrow? ? [screen.width - 6, 20].max : 22
      end

      def main_content_width
narrow? ? [screen.width - 6, 20].max : [screen.width - sidebar_width - 13, 20].max
      end

      def panel_height
return nil if narrow?

[screen.height - 4, 5].max
      end)
end

#layout_frame_helpersObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/charming/generators/app_generator/layout_template.rb', line 35

def layout_frame_helpers
  %(
      def app_frame
narrow? ? column(sidebar, main_content, gap: 1) : row(sidebar, main_content, gap: 1)
      end

      def sidebar
box(column(app_title, navigation, shortcuts, gap: 1), style: sidebar_style)
      end

      def main_content
box(yield_content, style: main_content_style)
      end)
end

#layout_frame_style_helpersObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/charming/generators/app_generator/layout_template.rb', line 121

def layout_frame_style_helpers
  %(
      def sidebar_style
base = sidebar_focused? ? theme.title : theme.border
base = base.border(:rounded).padding(1, 2).width(sidebar_width).height(panel_height)
palette ? base.faint : base
      end

      def main_content_style
base = content_focused? ? theme.title : theme.border
base = base.border(:rounded).padding(1, 2).width(main_content_width).height(panel_height)
palette ? base.faint : base
      end)
end

#layout_helpersObject



26
27
28
29
30
31
32
33
# File 'lib/charming/generators/app_generator/layout_template.rb', line 26

def layout_helpers
  %(
      private
#{layout_frame_helpers}
#{layout_navigation_helpers}
#{layout_modal_helpers}
#{layout_style_helpers})
end

#layout_modal_helpersObject



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/charming/generators/app_generator/layout_template.rb', line 101

def layout_modal_helpers
  %(

      def command_palette_modal
render_component Charming::Components::Modal.new(
  content: palette,
  title: "Command palette",
  help: "Type to filter. Enter selects. Escape closes.",
  width: 52,
  theme: theme
)
      end)
end

#layout_navigation_helpersObject



50
51
52
53
54
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/charming/generators/app_generator/layout_template.rb', line 50

def layout_navigation_helpers
  %(

      def app_title
text "#{name.class_name}", style: theme.header_accent.align(:center).width(sidebar_width)
      end

      def navigation
column(*nav_items)
      end

      def nav_items
controller.application.routes.all.each_with_index.map do |route, index|
  text nav_label(route, index), style: nav_style(route, index)
end
      end

      def nav_label(route, index)
cursor = sidebar_focused? && index == sidebar_index ? ">" : " "
active = current_route?(route) ? "●" : " "
"\#{cursor} \#{active} \#{route.title}"
      end

      def nav_style(route, index)
return theme.selected if sidebar_focused? && index == sidebar_index
return theme.title if current_route?(route)

theme.muted
      end

      def shortcuts
text "tab focus\\np commands\\nq quit", style: theme.muted
      end

      def sidebar_focused?
controller.sidebar_focused?
      end

      def content_focused?
controller.content_focused?
      end

      def sidebar_index
controller.sidebar_index
      end

      def current_route?(route)
route.controller_class == controller.class && route.action == :show
      end)
end

#layout_style_helpersObject



115
116
117
118
119
# File 'lib/charming/generators/app_generator/layout_template.rb', line 115

def layout_style_helpers
  %(
#{layout_frame_style_helpers}
#{layout_dimension_helpers})
end