Class: Ruber::World::HintSolver

Inherits:
Object
  • Object
show all
Defined in:
lib/ruber/world/hint_solver.rb

Overview

Helper class used by MainWindow#editor_for and friends to find out which editor should use or where the new editor should be placed according to the hints given them

Instance Method Summary collapse

Constructor Details

#initialize(tabs, manager, view_order) ⇒ HintSolver

Returns a new instance of HintSolver.

Parameters:

  • tabs (KDE::TabWidget)

    the tab widget which contains the tabs

  • manager (KParts::PartManager)

    the part manager used by the main window

  • view_order (Array<EditorView>)

    an array specifying the order in which views were activated. Most recently activated views corresponds to lower indexes. It’s assumed that the main window will keep a reference to this array and update it whenever a view becomes activated



43
44
45
46
47
# File 'lib/ruber/world/hint_solver.rb', line 43

def initialize tabs, manager, view_order
  @tabs = tabs
  @part_manager = manager
  @view_order = view_order
end

Instance Method Details

#find_editor(doc, hints) ⇒ EditorView?

Finds the editor to use for a document according to the given hints

If no editor associated with the document and respecting the given hints exists, nil is returned. This always happens if the @:existing@ hint is @:never@.

Parameters:

  • doc (Document)

    the document to retrieve the editor for

  • hints (see MainWindow#editor_for)

    . See Ruber::World::HintSolver#MainWindow#MainWindow#editor_for for the values it can contain. Only the @:existing@ and @:strategy@ entries are used

Returns:

  • (EditorView, nil)

    a view associated with the document which respects the hints or nil if such a view doesn’t exist



60
61
62
63
64
65
66
67
68
69
# File 'lib/ruber/world/hint_solver.rb', line 60

def find_editor doc, hints
  views = []
  case hints[:existing]
  when :never then return nil
  when :current_tab
    views = @tabs.current_widget.select{|v| v.document == doc}
  else @tabs.each{|t| views += t.select{|v| v.document == doc} }
  end
  choose_editor doc, views, hints
end

#place_editor(hints) ⇒ EditorView?

Finds out where a new editor should respect the given hints

The return value tells where the new editor should be placed. If the return value is nil then the editor should be placed in a new tab; if it’s an EditorView then the editor should be placed in the pane containing the view, splitting it at the view values it can contain. Only the @:newand entry is used

Parameters:

  • hints (see MainWindow#editor_for)

    . See Ruber::World::HintSolver#MainWindow#MainWindow#editor_for for the

Returns:

  • (EditorView, nil)

    an EditorView if the new editor should be placed inside an existing pane. The returned view is the view where it’s parent should be split at. If nil is returned, the editor should be put in a new tab



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ruber/world/hint_solver.rb', line 84

def place_editor hints
  case hints[:new]
  when :new_tab then nil
  when :current then @view_order[0]
  when :current_tab
    current = @tabs.current_widget
    current.each_view.to_a[0] if current
  when Integer
    pane = @tabs.widget hints[:new]
    pane.each_view.to_a[0] if pane
  when EditorView then 
    view = hints[:new]
    view.parent.is_a?(Pane) ? view : nil
  end
end