class TkInspect::Console::Base

Attributes

eval_binding[RW]
main_component[RW]
modal[RW]
root[RW]
tk_root[RW]

Public Class Methods

new(options = {}) click to toggle source
# 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

Public Instance Methods

create_menu() click to toggle source
# 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_root() click to toggle source
# 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) click to toggle source
# File lib/tk_inspect/console/base.rb, line 39
def execute(code)
  eval(code, eval_binding)
end
focus() click to toggle source
# 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_main() click to toggle source
# File lib/tk_inspect/console/base.rb, line 20
def make_main
  self.class.main_console = self
end
modal_loop() click to toggle source
new_canvas_window() click to toggle source
# File lib/tk_inspect/canvas_window/base.rb, line 34
def new_canvas_window
  TkInspect::CanvasWindow::Base.new.refresh
end
open_class_browser() click to toggle source
# File lib/tk_inspect/console/base.rb, line 63
def open_class_browser
  TkInspect::ClassBrowser::Base.new.refresh
end
open_inspector(expression = nil) click to toggle source
# 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
refresh() click to toggle source
# File lib/tk_inspect/console/base.rb, line 30
def refresh
  @main_component.regenerate
  @main_component.focus
end
say(s) click to toggle source
# File lib/tk_inspect/console/base.rb, line 43
def say(s)
  main_component.show_output(s)
  return nil
end