Class: TkInspect::Console::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/tk_inspect/console/base.rb,
lib/tk_inspect/canvas_window/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



12
13
14
15
16
17
18
# File 'lib/tk_inspect/console/base.rb', line 12

def initialize(options = {})
  @tk_root = nil
  @main_component = nil
  @eval_binding = binding
  @modal = !!options[:modal]
  @root = !!options[:root]
end

Instance Attribute Details

#eval_bindingObject

Returns the value of attribute eval_binding.



8
9
10
# File 'lib/tk_inspect/console/base.rb', line 8

def eval_binding
  @eval_binding
end

#main_componentObject

Returns the value of attribute main_component.



7
8
9
# File 'lib/tk_inspect/console/base.rb', line 7

def main_component
  @main_component
end

Returns the value of attribute modal.



9
10
11
# File 'lib/tk_inspect/console/base.rb', line 9

def modal
  @modal
end

#rootObject

Returns the value of attribute root.



10
11
12
# File 'lib/tk_inspect/console/base.rb', line 10

def root
  @root
end

#tk_rootObject

Returns the value of attribute tk_root.



6
7
8
# File 'lib/tk_inspect/console/base.rb', line 6

def tk_root
  @tk_root
end

Instance Method Details

#create_menuObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/tk_inspect/console/base.rb', line 76

def create_menu
  @menubar = TkMenu.new(@tk_root.tk_item.native_item)
  @menu = {}
  file = TkMenu.new(@menubar)
  @menu[:file] = file
  edit = TkMenu.new(@menubar)
  @menu[:edit] = edit
  edit.add :command, label: "Undo", accelerator: 'Command+z', command: -> { Tk.event_generate(Tk.focus, "<Undo>") }
  edit.add :command, label: "Redo", accelerator: 'Command+Shift+z', command: -> { Tk.event_generate(Tk.focus, "<Redo>") }
  edit.add :separator
  edit.add :command, label: "Cut", accelerator: 'Command+x', command: -> { Tk.event_generate(Tk.focus, "<Cut>") }
  edit.add :command, label: "Copy", accelerator: 'Command+c', command: -> { Tk.event_generate(Tk.focus, "<Copy>") }
  edit.add :command, label: "Paste", accelerator: 'Command+v', command: -> { Tk.event_generate(Tk.focus, "<Paste>") }
  edit.add :command, label: "Clear", accelerator: 'Delete', command: -> { Tk.event_generate(Tk.focus, "<Clear>") }
  view = TkMenu.new(@menubar)
  @menu[:view] = view
  view.add :command, label: "Bigger font", accelerator: 'Command++', command: -> { main_component.zoom_in(nil) }
  view.add :command, label: "Smaller font", accelerator: 'Command+-', command: -> { main_component.zoom_out(nil) }
  tools = TkMenu.new(@menubar)
  @menu[:tools] = tools
  tools.add :command, label: "Run selection", accelerator: 'Command+r', command: -> { main_component.run_selected(nil) }
  tools.add :command, label: "Inspect selection", accelerator: 'Command+i', command: -> { main_component.inspect_selected(nil) }
  tools.add :separator
  tools.add :command, label: "Clear output", accelerator: 'Command+k', command: -> { main_component.clear_output(nil) }
  tools.add :separator
  tools.add :command, label: "Inspector ...", accelerator: 'Command+Shift+i', command: -> { open_inspector }
  tools.add :command, label: "Class Browser ...", accelerator: 'Command+Shift+b', command: -> { open_class_browser }
  @menubar.add :cascade, menu: file, label: 'File'
  @menubar.add :cascade, menu: edit, label: 'Edit'
  @menubar.add :cascade, menu: view, label: 'View'
  @menubar.add :cascade, menu: tools, label: 'Tools'
  @tk_root.tk_item.native_item['menu'] = @menubar
  @tk_root.tk_item.native_item.bind('Command-r', -> { main_component.run_selected(nil) })
  @tk_root.tk_item.native_item.bind('Command-k', -> { main_component.clear_output(nil) })
  @tk_root.tk_item.native_item.bind('Command-+', -> { main_component.zoom_in(nil) })
  @tk_root.tk_item.native_item.bind('Command-minus', -> { main_component.zoom_out(nil) })
  @tk_root.tk_item.native_item.bind('Command-i', -> { main_component.inspect_selected(nil) })
  @tk_root.tk_item.native_item.bind('Command-Shift-i', -> { open_inspector })
  @tk_root.tk_item.native_item.bind('Command-Shift-b', -> { open_class_browser })
end

#create_rootObject



67
68
69
70
71
72
73
74
# File 'lib/tk_inspect/console/base.rb', line 67

def create_root
  @tk_root = TkComponent::Window.new(title: "Ruby Console", root: @root)
  @main_component = RootComponent.new
  @main_component.console = self
  @tk_root.place_root_component(@main_component)
  create_menu
  @tk_root.tk_item.native_item.after(100) { @main_component.focus_on_code }
end

#execute(code) ⇒ Object



39
40
41
# File 'lib/tk_inspect/console/base.rb', line 39

def execute(code)
  eval(code, eval_binding)
end

#focusObject



24
25
26
27
28
# File 'lib/tk_inspect/console/base.rb', line 24

def focus
  create_root if @main_component.nil?
  @tk_root.focus
  @tk_root.tk_item.native_item.after(100) { @main_component.focus_on_code }
end

#make_mainObject



20
21
22
# File 'lib/tk_inspect/console/base.rb', line 20

def make_main
  self.class.main_console = self
end


35
36
37
# File 'lib/tk_inspect/console/base.rb', line 35

def modal_loop
  @main_component.run_modal_loop
end

#new_canvas_windowObject



34
35
36
# File 'lib/tk_inspect/canvas_window/base.rb', line 34

def new_canvas_window
  TkInspect::CanvasWindow::Base.new.refresh
end

#open_class_browserObject



63
64
65
# File 'lib/tk_inspect/console/base.rb', line 63

def open_class_browser
  TkInspect::ClassBrowser::Base.new.refresh
end

#open_inspector(expression = nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/tk_inspect/console/base.rb', line 48

def open_inspector(expression = nil)
  if expression.present?
    val = eval(expression, eval_binding)
    TkInspect::Inspector::Base
      .inspector_for_object(val)
      .new(expression: expression,
           binding: eval_binding,
           value: val).refresh
  else
    TkInspect::Inspector::Base
      .new(expression: expression,
           binding: eval_binding).refresh
  end
end

#refreshObject



30
31
32
33
# File 'lib/tk_inspect/console/base.rb', line 30

def refresh
  @main_component.regenerate
  @main_component.focus
end

#say(s) ⇒ Object



43
44
45
46
# File 'lib/tk_inspect/console/base.rb', line 43

def say(s)
  main_component.show_output(s)
  return nil
end