Class: JLDrill::Gtk::VocabPopupFactory

Inherits:
PopupFactory show all
Defined in:
lib/jldrill/views/gtk/widgets/VocabPopupFactory.rb

Instance Method Summary collapse

Methods inherited from PopupFactory

#belowRect, #block, #closePopup, #getPopupString, #showBusy, #toAbsPos, #unblock

Constructor Details

#initialize(view) ⇒ VocabPopupFactory

Returns a new instance of VocabPopupFactory.



8
9
10
# File 'lib/jldrill/views/gtk/widgets/VocabPopupFactory.rb', line 8

def initialize(view)
    super(view)
end

Instance Method Details

#createPopup(searchString, x, y) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/jldrill/views/gtk/widgets/VocabPopupFactory.rb', line 44

def createPopup(searchString, x, y)
    closePopup
    candidates = getCandidates(searchString)
    if !candidates.nil? && !candidates.empty?
        if !candidates[0].kanji.nil?
            string = candidates[0].kanji
        else
            string = candidates[0].reading
        end
        @currentPopup = VocabPopup.new(string, candidates,
                                  @view.mainWindow, x, y)
    end
end

#dictionaryLoaded?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/jldrill/views/gtk/widgets/VocabPopupFactory.rb', line 12

def dictionaryLoaded?
    @context.dictionaryLoaded?
end

#getCandidates(string) ⇒ Object



21
22
23
# File 'lib/jldrill/views/gtk/widgets/VocabPopupFactory.rb', line 21

def getCandidates(string)
    return @context.search(string)
end

#getStringAt(widget, window, x, y) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/jldrill/views/gtk/widgets/VocabPopupFactory.rb', line 25

def getStringAt(widget, window, x, y)
    type = widget.get_window_type(window)
    coords = widget.window_to_buffer_coords(type, x, y)
    iter, tr = widget.get_iter_at_position(coords[0], coords[1])
    sentenceEnd = iter.buffer.get_iter_at_offset(iter.offset)
    sentenceEnd.forward_sentence_end
    string = iter.get_visible_text(sentenceEnd)
    pos = widget.get_iter_location(iter)
    if (coords[0] > pos.x) && (coords[0] < pos.x + pos.width) &&
            string != ""
        rect = widget.buffer_to_window_coords(type, pos.x, pos.y)
        charPos = belowRect([rect[0], rect[1], pos.width, pos.height])
        screenPos = toAbsPos(widget, charPos[0], charPos[1])
        [string, screenPos]
    else
        [nil, nil]
    end
end

#notify(event) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/jldrill/views/gtk/widgets/VocabPopupFactory.rb', line 58

def notify(event)
    if @blocked || !dictionaryLoaded?
        return
    end
    string, screenPos = getStringAt(event.widget, event.motion.window, 
                                    event.motion.x, event.motion.y)
    if string.nil? || screenPos.nil?
        closePopup
        return
    elsif sameString?(string, screenPos[0], screenPos[1])
        return
    end
    createPopup(string, screenPos[0], screenPos[1])
end

#sameString?(string, x, y) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
# File 'lib/jldrill/views/gtk/widgets/VocabPopupFactory.rb', line 16

def sameString?(string, x, y)
    !@currentPopup.nil? && # @currentPopup.character == string &&
        @currentPopup.x == x && @currentPopup.y == y
end