Emit signal on parent set
[luakit.git] / rc.lua
blobebd50a4dcc9333b99d46af7f69063150f647ea60
1 #!./luakit -c
3 require("mode")
5 -- Widget construction aliases
6 function eventbox() return widget{type="eventbox"} end
7 function hbox() return widget{type="hbox"} end
8 function label() return widget{type="label"} end
9 function notebook() return widget{type="notebook"} end
10 function vbox() return widget{type="vbox"} end
11 function webview() return widget{type="webview"} end
12 function window() return widget{type="window"} end
13 function entry() return widget{type="entry"} end
15 -- Keep a list of windows and data objects
16 windows = {}
18 -- Widget type-specific default settings
19 widget.add_signal("new", function(w)
20 w:add_signal("init", function(w)
21 if w.type == "window" then
22 -- Call the quit function if this was the last window left
23 w:add_signal("destroy", function ()
24 if #luakit.windows == 0 then luakit.quit() end
25 end)
26 elseif w.type == "label" or w.type == "entry" then
27 w.font = "monospace normal 9"
28 end
29 end)
30 end)
32 -- Returns a nice window title
33 function mktitle(view)
34 if not view:get_prop("title") and not view.uri then return "luakit" end
35 return (view:get_prop("title") or "luakit") .. " - " .. (view.uri or "about:blank")
36 end
38 -- Returns true if the given view is the currently active view
39 function iscurrent(nbook, view)
40 return nbook:current() == nbook:indexof(view)
41 end
43 function mktabcount(nbook, i)
44 return string.format("[%d/%d]", i or nbook:current(), nbook:count())
45 end
47 -- Returns a vim-like scroll indicator
48 function scroll_parse(view)
49 val, max = view:get_vscroll()
50 if max == 0 then
51 return "All"
52 elseif val == 0 then
53 return "Top"
54 elseif val == max then
55 return "Bot"
56 else
57 return string.format("%2d%%", (val/max) * 100)
58 end
59 end
61 function autohide(nbook, n)
62 if not n then n = nbook:count() end
63 if n == 1 then
64 nbook.show_tabs = false
65 else
66 nbook.show_tabs = true
67 end
68 end
70 function progress_update(w, view, p)
71 if not p then p = view:get_prop("progress") end
72 if not view:loading() or p == 1 then
73 w.sbar.loaded:hide()
74 else
75 w.sbar.loaded:show()
76 w.sbar.loaded.text = string.format("(%d%%)", p * 100)
77 end
78 end
80 function new_tab(w, uri)
81 view = webview()
82 w.tabs:append(view)
83 autohide(w.tabs)
85 -- Attach webview signals
86 view:add_signal("title-changed", function (v)
87 w.tabs:set_title(v, v:get_prop("title") or "(Untitled)")
88 if iscurrent(w.tabs, v) then
89 w.win.title = mktitle(v)
90 end
91 end)
93 view:add_signal("property::uri", function(v)
94 if not w.tabs:get_title(v) then
95 w.tabs:set_title(v, v.uri or "about:blank")
96 end
98 if iscurrent(w.tabs, v) then
99 w.sbar.uri.text = v.uri or "about:blank"
101 end)
103 view:add_signal("load-start", function (v)
104 if iscurrent(w.tabs, v) then progress_update(w, v, 0) end
105 end)
106 view:add_signal("progress-update", function (v)
107 if iscurrent(w.tabs, v) then progress_update(w, v) end
108 end)
110 view:add_signal("expose", function(v)
111 if iscurrent(w.tabs, v) then
112 w.sbar.scroll.text = scroll_parse(v)
114 end)
116 -- Navigate to uri
117 view.uri = uri
119 return view
122 -- Construct new window
123 function new_window(uris)
124 -- Widget & state variables
125 local w = {
126 win = window(),
127 layout = vbox(),
128 tabs = notebook(),
129 -- Status bar widgets
130 sbar = {
131 layout = hbox(),
132 ebox = eventbox(),
133 uri = label(),
134 loaded = label(),
135 sep = label(),
136 rlayout = hbox(),
137 rebox = eventbox(),
138 tabi = label(),
139 scroll = label(),
141 -- Input bar widgets
142 ibar = {
143 layout = hbox(),
144 ebox = eventbox(),
145 prompt = label(),
146 input = entry(),
150 -- Pack widgets
151 w.win:set_child(w.layout)
152 w.sbar.ebox:set_child(w.sbar.layout)
153 w.layout:pack_start(w.tabs, true, true, 0)
154 w.sbar.layout:pack_start(w.sbar.uri, false, false, 0)
155 w.sbar.layout:pack_start(w.sbar.loaded, false, false, 0)
156 w.sbar.layout:pack_start(w.sbar.sep, true, true, 0)
158 -- Put the right-most labels in something backgroundable
159 w.sbar.rlayout:pack_start(w.sbar.tabi, false, false, 0)
160 w.sbar.rlayout:pack_start(w.sbar.scroll, false, false, 2)
161 w.sbar.rebox:set_child(w.sbar.rlayout)
163 w.sbar.layout:pack_start(w.sbar.rebox, false, false, 0)
164 w.layout:pack_start(w.sbar.ebox, false, false, 0)
166 -- Pack input bar
167 w.ibar.layout:pack_start(w.ibar.prompt, false, false, 0)
168 w.ibar.layout:pack_start(w.ibar.input, false, false, 0)
169 w.ibar.ebox:set_child(w.ibar.layout)
170 w.layout:pack_start(w.ibar.ebox, false, false, 0)
172 w.sbar.uri.fg = "#fff"
173 w.sbar.uri.selectable = true
174 w.sbar.loaded.fg = "#888"
175 w.sbar.loaded:hide()
176 w.sbar.rebox.bg = "#000"
177 w.sbar.scroll.fg = "#fff"
178 w.sbar.scroll.text = "All"
179 w.sbar.tabi.text = "[0/0]"
180 w.sbar.tabi.fg = "#fff"
181 w.sbar.ebox.bg = "#000"
183 w.ibar.input.show_frame = false
184 w.ibar.input.bg = "#fff"
185 w.ibar.input.fg = "#000"
187 w.ibar.ebox.bg = "#fff"
188 w.ibar.prompt.text = "Hello"
189 w.ibar.prompt.fg = "#000"
191 -- Attach notebook signals
192 w.tabs:add_signal("page-added", function(nbook, view, idx)
193 w.sbar.tabi.text = mktabcount(nbook)
194 end)
195 w.tabs:add_signal("switch-page", function(nbook, view, idx)
196 w.sbar.tabi.text = mktabcount(nbook, idx)
197 w.sbar.uri.text = view.uri or "about:blank"
198 w.win.title = mktitle(view)
199 progress_update(w, view)
200 autohide(nbook)
201 end)
203 -- Mode specific actions
204 w.win:add_signal("mode-changed", function(win, mode)
205 if mode == "normal" then
206 w.ibar.prompt.text = ""
207 w.ibar.prompt:show()
208 w.ibar.input:hide()
209 w.ibar.input.text = ""
210 elseif mode == "input" then
211 w.ibar.input:hide()
212 w.ibar.input.text = ""
213 w.ibar.prompt.text = " -- INPUT -- "
214 w.ibar.prompt:show()
215 elseif mode == "command" then
216 w.ibar.prompt:hide()
217 w.ibar.prompt.text = ""
218 w.ibar.input.text = ":"
219 w.ibar.input:show()
220 w.ibar.input:focus()
222 end)
224 -- Populate notebook
225 for _, uri in ipairs(uris) do
226 new_tab(w, uri)
229 -- Make sure something is loaded
230 if w.tabs:count() == 0 then
231 new_tab(w, "http://github.com/mason-larobina/luakit")
234 -- Set initial mode
235 mode(w.win)
237 return w
240 new_window(uris)