1--- Web page history - status bar widget.
2--
3-- Indicates whether the current page can go back or go forward.
4--
5-- The widget will not be shown if the current page cannot go back or forward.
6--
7-- @module lousy.widget.hist
8-- @copyright 2017 Aidan Holm <aidanholm@gmail.com>
9-- @copyright 2010 Mason Larobina <mason.larobina@gmail.com>
10
11local webview = require("webview")
12local lousy = require("lousy")
13local theme = lousy.theme.get()
14local wc = require("lousy.widget.common")
15
16local _M = {}
17
18--- Format string which defines the appearance of the widget.
19-- The text `{back}` is replaced with the back indicator, and the text
20-- `{forward}` is replaced with the forward indicator.
21-- @type string
22-- @readwrite
23_M.format = "[{back}{forward}]"
24
25--- Text used to indicate that the current page can go back.
26-- @type string
27-- @readwrite
28_M.back_indicator = "+"
29
30--- Text used to indicate that the current page can go forward.
31-- @type string
32-- @readwrite
33_M.forward_indicator = "-"
34
35local widgets = {
36    update = function (w, hist)
37        local back, forward = w.view:can_go_back(), w.view:can_go_forward()
38        if back or forward then
39            hist.text  = string.gsub(_M.format, "{(%w+)}", {
40                back = back and _M.back_indicator or "",
41                forward = forward and _M.forward_indicator or "",
42            })
43            hist:show()
44        else
45            hist:hide()
46        end
47    end,
48}
49
50webview.add_signal("init", function (view)
51    -- Update widget when current page changes status
52    view:add_signal("load-status", function (v)
53        local w = webview.window(v)
54        if w and w.view == v then
55            wc.update_widgets_on_w(widgets, w)
56        end
57    end)
58    view:add_signal("switched-page", function (v)
59        wc.update_widgets_on_w(widgets, webview.window(v))
60    end)
61end)
62
63local function new()
64    local hist = widget{type="label"}
65    hist:hide()
66    hist.fg = theme.hist_sbar_fg
67    hist.font = theme.hist_sbar_font
68    return wc.add_widget(widgets, hist)
69end
70
71return setmetatable(_M, { __call = function(_, ...) return new(...) end })
72
73-- vim: et:sw=4:ts=8:sts=4:tw=80
74