File: webinspector.lua

package info (click to toggle)
luakit 2012.09.13-r1-8
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,160 kB
  • ctags: 1,276
  • sloc: ansic: 6,086; makefile: 153; ruby: 79; sh: 38
file content (81 lines) | stat: -rw-r--r-- 2,260 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
--------------------------------------------------------
-- Bindings for the web inspector                     --
-- (C) 2012 Fabian Streitel <karottenreibe@gmail.com> --
-- (C) 2012 Mason Larobina <mason.larobina@gmail.com> --
--------------------------------------------------------

local windows = setmetatable({}, { __mode = "k" })

local function switch_inspector(w, view)
    -- Hide old widget
    if w.paned.bottom then w.paned:remove(w.paned.bottom) end
    -- Show new widget
    local iview = view.inspector
    if iview and not windows[iview] then
        w.paned:pack2(iview)
    end
end

local function close_window(iview)
    local win = windows[iview]
    if win then
        win:remove(iview)
        windows[iview] = nil
        win:destroy()
        return true
    end
end

window.init_funcs.inspector_setup = function (w)
    w.tabs:add_signal("switch-page", function (_, view)
        switch_inspector(w, view)
    end)
end

webview.init_funcs.inspector_setup = function (view, w)
    view.enable_developer_extras = true

    view:add_signal("create-inspector-web-view", function ()
        return widget{type="webview"}
    end)

    view:add_signal("show-inspector", function ()
        switch_inspector(w, view)
        -- We start in paned view
        view.inspector:eval_js("WebInspector.attached = true;")
    end)

    view:add_signal("close-inspector", function (_, iview)
        if not close_window(iview) then
            w.paned:remove(iview)
        end
        iview:destroy()
    end)

    view:add_signal("attach-inspector", function ()
        local iview = view.inspector
        close_window(iview)
        switch_inspector(w, view)
    end)

    view:add_signal("detach-inspector", function ()
        local iview = view.inspector
        local win = widget{type="window"}
        w.paned:remove(iview)
        win.child = iview
        windows[iview] = win
        win:show()
    end)
end

local cmd = lousy.bind.cmd
add_cmds({
    cmd("in[spect]", "open DOM inspector", function (w, _, o)
        local v = w.view
        if o.bang then -- "inspect!" toggles inspector
            (v.inspector and v.close_inspector or v.show_inspector)(v)
        else
            w.view:show_inspector()
        end
    end),
})