Permalink
Browse files

zoom properties autoupdating

  • Loading branch information...
1 parent aa2409a commit 392f631aaad5fdf3f95052394fc69c47fb02c1ce @liaonau liaonau committed Sep 17, 2011
Showing with 48 additions and 34 deletions.
  1. +48 −34 lib/autozoom.lua
View
82 lib/autozoom.lua
@@ -4,13 +4,13 @@
-- © 2011 Mason Larobina <mason.larobina@gmail.com> --
-------------------------------------------------------
-local print = print
-- Get lua environment
local math = require "math"
local string = string
local assert = assert
local tostring = tostring
local setmetatable = setmetatable
+local pairs = pairs
-- Get luakit environment
local lousy = require "lousy"
@@ -22,13 +22,13 @@ local capi = { luakit = luakit, sqlite3 = sqlite3 }
module "autozoom"
-local defaults = {
+local settings = {
level = 1.0,
full_content = false,
visible = "non-default",
- text = "zoom:{level}%",
+ text = "(zoom:{level}%,{full_content})",
+ autowrite = true,
}
-setmetatable(_M, {__index = _M.defaults})
-- Open database
local db = capi.sqlite3{ filename = capi.luakit.data_dir .. "/autozoom.db" }
@@ -49,6 +49,7 @@ local function round(num)
end
local function get_domain(uri)
+ if (uri == 'about:blank') then return lousy.util.sql_escape(uri) end
local domain = assert(lousy.uri.parse(uri), "invalid uri").host
return lousy.util.sql_escape(string.match(domain, "^www%.(.+)") or domain)
end
@@ -57,7 +58,7 @@ function set(uri, level, full_content)
local query_insert = [[INSERT OR REPLACE INTO by_domain
(domain, level, full_content)
VALUES(%s, %f, %d);]]
- db:exec(string.format(query_insert, get_domain(uri), level or defaults.level,
+ db:exec(string.format(query_insert, get_domain(uri), level or settings.level,
full_content and 1 or 0))
end
@@ -74,22 +75,34 @@ function get(uri)
if ret and ret[1] then
return ret[1].level, (ret[1].full_content == "1")
end
- return defaults.level, defaults.full_content
+ return settings.level, settings.full_content
end
local function is_default(level, full_content)
- return ((level == defaults.level) and (full_content == defaults.full_content))
+ return ((level == settings.level) and (full_content == settings.full_content))
end
local function get_zoom(v)
return round(v:get_property("zoom-level")), v:get_property("full-content-zoom")
end
-local function set_zoom(v, level, full_content)
+local function set_zoom(v)
+ local level, full_content = get(v.uri)
v:set_property("zoom-level", level)
v:set_property("full-content-zoom", full_content)
end
+local function update_db(v)
+ if settings.autowrite then
+ local level, full_content = get_zoom(v)
+ if is_default(level, full_content) then
+ unset(v.uri)
+ else
+ set(v.uri, level, full_content)
+ end
+ end
+end
+
window.init_funcs.notebook_signals_autoozoom = function (w)
w.tabs:add_signal("switch-page", function (nbook, view, idx)
capi.luakit.idle_add(function ()
@@ -111,12 +124,12 @@ end
window.methods.update_zoom = function (w)
local zoom = w.sbar.l.zoom
local level, full_content = get_zoom(w.view)
- local visible = defaults.visible
+ local visible = settings.visible
if (visible == "non-default") and is_default(level, full_content) then
visible = false
end
- if w.view and visible then
- local text = string.gsub(defaults.text, "{([%w_]+)}",
+ if visible then
+ local text = string.gsub(settings.text, "{([%w_]+)}",
{ level = 100 * level, full_content = tostring(full_content) })
if zoom.text ~= text then zoom.text = text end
zoom:show()
@@ -125,35 +138,36 @@ window.methods.update_zoom = function (w)
end
end
-webview.init_funcs.autozoom_setup = function (view, w)
- local function update(v)
- local level, full_content = get_zoom(v)
- print("updating:")
- if is_default(level, full_content) then
- print("unsetting")
- unset(v.uri)
- else
- print("setting level = "..level..", content = "..tostring(full_content))
- set(v.uri, level, full_content)
+local function callback(v)
+ update_db(v)
+ for _, win in pairs(capi.luakit.windows) do
+ local w = window.bywidget[win]
+ for n = 1, w.tabs:count() do
+ local view = w.tabs:atindex(n)
+ if (view ~= v and get_domain(view.uri) == get_domain(v.uri)) then
+ view:remove_signal("property::zoom-level", callback)
+ view:remove_signal("property::full-content-zoom", callback)
+ set_zoom(view)
+ view:add_signal("property::zoom-level", callback)
+ view:add_signal("property::full-content-zoom", callback)
+ end
+ end
+ if (get_domain(w.tabs:atindex(w.tabs:current()).uri) == get_domain(v.uri)) then
+ w:update_zoom()
end
- w:update_zoom()
- end
- local function start_watch()
- view:add_signal("property::zoom-level", update)
- view:add_signal("property::full-content-zoom", update)
- end
- local function stop_watch()
- view:remove_signal("property::zoom-level", update)
- view:remove_signal("property::full-content-zoom", update)
end
- start_watch()
+end
+
+webview.init_funcs.autozoom_setup = function (view, w)
-- Load zoom changes
view:add_signal("load-status", function (view, status)
if status ~= "first-visual" then return end
- stop_watch()
- set_zoom(view, get(view.uri))
- start_watch()
+ set_zoom(view)
+ w:update_zoom()
end)
+ view:add_signal("property::zoom-level", callback)
+ view:add_signal("property::full-content-zoom", callback)
end
+setmetatable(_M, { __newindex = function (_, k, v) settings[k] = v end })
-- vim: et:sw=4:ts=8:sts=4:tw=80

0 comments on commit 392f631

Please sign in to comment.