Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

zoom properties autoupdating

  • Loading branch information...
commit 392f631aaad5fdf3f95052394fc69c47fb02c1ce 1 parent aa2409a
Roman Leonov authored September 18, 2011

Showing 1 changed file with 48 additions and 34 deletions. Show diff stats Hide diff stats

  1. 82  lib/autozoom.lua
82  lib/autozoom.lua
@@ -4,13 +4,13 @@
4 4
 -- © 2011 Mason Larobina <mason.larobina@gmail.com>  --
5 5
 -------------------------------------------------------
6 6
 
7  
-local print = print
8 7
 -- Get lua environment
9 8
 local math         = require "math"
10 9
 local string       = string
11 10
 local assert       = assert
12 11
 local tostring     = tostring
13 12
 local setmetatable = setmetatable
  13
+local pairs        = pairs
14 14
 
15 15
 -- Get luakit environment
16 16
 local lousy   = require "lousy"
@@ -22,13 +22,13 @@ local capi    = { luakit = luakit, sqlite3 = sqlite3 }
22 22
 
23 23
 module "autozoom"
24 24
 
25  
-local defaults = {
  25
+local settings = {
26 26
     level        = 1.0,
27 27
     full_content = false,
28 28
     visible      = "non-default",
29  
-    text         = "zoom:{level}%",
  29
+    text         = "(zoom:{level}%,{full_content})",
  30
+    autowrite    = true,
30 31
 }
31  
-setmetatable(_M, {__index = _M.defaults})
32 32
 
33 33
 -- Open database
34 34
 local db = capi.sqlite3{ filename = capi.luakit.data_dir .. "/autozoom.db" }
@@ -49,6 +49,7 @@ local function round(num)
49 49
 end
50 50
 
51 51
 local function get_domain(uri)
  52
+    if (uri == 'about:blank') then return lousy.util.sql_escape(uri) end
52 53
     local domain = assert(lousy.uri.parse(uri), "invalid uri").host
53 54
     return lousy.util.sql_escape(string.match(domain, "^www%.(.+)") or domain)
54 55
 end
@@ -57,7 +58,7 @@ function set(uri, level, full_content)
57 58
     local query_insert = [[INSERT OR REPLACE INTO by_domain
58 59
     (domain, level, full_content)
59 60
     VALUES(%s, %f, %d);]]
60  
-    db:exec(string.format(query_insert, get_domain(uri), level or defaults.level,
  61
+    db:exec(string.format(query_insert, get_domain(uri), level or settings.level,
61 62
         full_content and 1 or 0))
62 63
 end
63 64
 
@@ -74,22 +75,34 @@ function get(uri)
74 75
     if ret and ret[1] then
75 76
         return ret[1].level, (ret[1].full_content == "1")
76 77
     end
77  
-    return defaults.level, defaults.full_content
  78
+    return settings.level, settings.full_content
78 79
 end
79 80
 
80 81
 local function is_default(level, full_content)
81  
-    return ((level == defaults.level) and (full_content == defaults.full_content))
  82
+    return ((level == settings.level) and (full_content == settings.full_content))
82 83
 end
83 84
 
84 85
 local function get_zoom(v)
85 86
     return  round(v:get_property("zoom-level")), v:get_property("full-content-zoom")
86 87
 end
87 88
 
88  
-local function set_zoom(v, level, full_content)
  89
+local function set_zoom(v)
  90
+    local level, full_content = get(v.uri)
89 91
     v:set_property("zoom-level", level)
90 92
     v:set_property("full-content-zoom", full_content)
91 93
 end
92 94
 
  95
+local function update_db(v)
  96
+    if settings.autowrite then
  97
+        local level, full_content = get_zoom(v)
  98
+        if is_default(level, full_content) then
  99
+            unset(v.uri)
  100
+        else
  101
+            set(v.uri, level, full_content)
  102
+        end
  103
+    end
  104
+end
  105
+
93 106
 window.init_funcs.notebook_signals_autoozoom = function (w)
94 107
     w.tabs:add_signal("switch-page", function (nbook, view, idx)
95 108
         capi.luakit.idle_add(function ()
@@ -111,12 +124,12 @@ end
111 124
 window.methods.update_zoom = function (w)
112 125
     local zoom = w.sbar.l.zoom
113 126
     local level, full_content = get_zoom(w.view)
114  
-    local visible = defaults.visible
  127
+    local visible = settings.visible
115 128
     if (visible == "non-default") and is_default(level, full_content) then
116 129
         visible = false
117 130
     end
118  
-    if w.view and visible then
119  
-        local text = string.gsub(defaults.text, "{([%w_]+)}",
  131
+    if visible then
  132
+        local text = string.gsub(settings.text, "{([%w_]+)}",
120 133
             { level = 100 * level, full_content = tostring(full_content) })
121 134
         if zoom.text ~= text then zoom.text = text end
122 135
         zoom:show()
@@ -125,35 +138,36 @@ window.methods.update_zoom = function (w)
125 138
     end
126 139
 end
127 140
 
128  
-webview.init_funcs.autozoom_setup = function (view, w)
129  
-    local function update(v)
130  
-        local level, full_content = get_zoom(v)
131  
-        print("updating:")
132  
-        if is_default(level, full_content) then
133  
-        print("unsetting")
134  
-            unset(v.uri)
135  
-        else
136  
-        print("setting level = "..level..", content = "..tostring(full_content))
137  
-            set(v.uri, level, full_content)
  141
+local function callback(v)
  142
+    update_db(v)
  143
+    for _, win in pairs(capi.luakit.windows) do
  144
+        local w = window.bywidget[win]
  145
+        for n = 1, w.tabs:count() do
  146
+            local view = w.tabs:atindex(n)
  147
+            if (view ~= v and get_domain(view.uri) == get_domain(v.uri)) then
  148
+                view:remove_signal("property::zoom-level",        callback)
  149
+                view:remove_signal("property::full-content-zoom", callback)
  150
+                set_zoom(view)
  151
+                view:add_signal("property::zoom-level",        callback)
  152
+                view:add_signal("property::full-content-zoom", callback)
  153
+            end
  154
+        end
  155
+        if (get_domain(w.tabs:atindex(w.tabs:current()).uri) == get_domain(v.uri)) then
  156
+            w:update_zoom()
138 157
         end
139  
-        w:update_zoom()
140  
-    end
141  
-    local function start_watch()
142  
-        view:add_signal("property::zoom-level",        update)
143  
-        view:add_signal("property::full-content-zoom", update)
144  
-    end
145  
-    local function stop_watch()
146  
-        view:remove_signal("property::zoom-level",        update)
147  
-        view:remove_signal("property::full-content-zoom", update)
148 158
     end
149  
-    start_watch()
  159
+end
  160
+
  161
+webview.init_funcs.autozoom_setup = function (view, w)
150 162
     -- Load zoom changes
151 163
     view:add_signal("load-status", function (view, status)
152 164
         if status ~= "first-visual" then return end
153  
-        stop_watch()
154  
-        set_zoom(view, get(view.uri))
155  
-        start_watch()
  165
+        set_zoom(view)
  166
+        w:update_zoom()
156 167
     end)
  168
+    view:add_signal("property::zoom-level",        callback)
  169
+    view:add_signal("property::full-content-zoom", callback)
157 170
 end
158 171
 
  172
+setmetatable(_M, { __newindex = function (_, k, v) settings[k] = v end })
159 173
 -- vim: et:sw=4:ts=8:sts=4:tw=80

0 notes on commit 392f631

Please sign in to comment.
Something went wrong with that request. Please try again.