Debian Codegraph @

obvious/popup_run_prompt/init.lua

raw symbols SCIP
1 ------------------------------------------
2 -- Author: Andrei "Garoth" Thorp --
3 -- Copyright 2009 Andrei "Garoth" Thorp --
4 ------------------------------------------
5
6 local mouse = mouse
7 local awful = require("awful")
8 local wibox = require("wibox")
9 local screen = screen
10 local ipairs = ipairs
11 local pairs = pairs
12 local io = io
13 local beautiful = require("beautiful")
14 local lib = {
15 hooks = require("obvious.lib.hooks")
16 }
17 local capi = {
18 wibox = wibox
19 }
20
21 local popup_run_prompt = {}
22
23 popup_run_prompt.defaults = {}
24 -- Default is 1 for people without compositing
25 popup_run_prompt.defaults.opacity = 1.0
26 popup_run_prompt.defaults.prompt_string = " Run~ "
27 popup_run_prompt.defaults.prompt_font = nil
28 -- Whether or not the bar should slide up or just pop up
29 popup_run_prompt.defaults.slide = false
30 -- Bar will be percentage of screen width
31 popup_run_prompt.defaults.width = 0.6
32 -- Bar will be this high in pixels
33 popup_run_prompt.defaults.height = 22
34 popup_run_prompt.defaults.border_width = 1
35 -- When sliding, it'll move this often (in seconds)
36 popup_run_prompt.defaults.move_speed = 0.02
37 -- When sliding, it'll move this many pixels per move
38 popup_run_prompt.defaults.move_amount = 3
39 -- Default run function
40 popup_run_prompt.defaults.run_function = awful.util.spawn
41 -- Default completion function
42 popup_run_prompt.defaults.completion_function = awful.completion.shell
43 -- Default cache
44 popup_run_prompt.defaults.cache = "/history"
45 -- Default position
46 popup_run_prompt.defaults.position = "top"
47
48 -- Clone the defaults for the used settings
49 local settings = {}
50 for key, value in pairs(popup_run_prompt.defaults) do
51 settings[key] = value
52 end
53
54 local runwibox = {}
55 local mypromptbox = {}
56 local inited = false
57
58 local function set_default(s)
59 runwibox[s]:geometry({
60 width = screen[s].geometry.width * settings.width,
61 height = settings.height,
62 x = screen[s].geometry.x + screen[s].geometry.width *
63 ((1 - settings.width) / 2),
64 y = screen[s].geometry.y + screen[s].geometry.height -
65 settings.height,
66 })
67 end
68
69 -- We want to "lazy init" so that in case beautiful inits late or something,
70 -- this is still likely to work.
71 local function ensure_init()
72 if inited then
73 return
74 end
75
76 inited = true
77 for s = 1, screen.count() do
78 mypromptbox[s] = wibox.widget({
79 name = "mypromptbox" .. s,
80 align = "left",
81 widget = wibox.widget.textbox,
82 })
83
84 runwibox[s] = capi.wibox({
85 fg = beautiful.fg_normal,
86 bg = beautiful.bg_normal,
87 border_width = settings.border_width,
88 border_color = beautiful.bg_focus,
89 })
90 set_default(s)
91 runwibox[s].opacity = settings.opacity
92 runwibox[s].visible = false
93 runwibox[s].screen = s
94 runwibox[s].ontop = true
95
96 -- Widgets for prompt wibox
97 runwibox[s].widget = mypromptbox[s]
98 end
99 end
100
101 local function do_slide_up()
102 local s = mouse.screen.index
103 startgeom = runwibox[s]:geometry()
104 runwibox[s]:geometry({
105 y = startgeom.y - settings.move_amount
106 })
107
108 if runwibox[s]:geometry().y <= screen[s].geometry.y +
109 screen[s].geometry.height - startgeom.height then
110 set_default(s)
111 lib.hooks.timer.stop(do_slide_up)
112 end
113 end
114
115 local function show_wibox(s)
116 runwibox.screen = s
117 if settings.slide == true then
118 startgeom = runwibox[s]:geometry()
119 -- changing visible property would reset wibox geometry to its defaults
120 -- Might be 0 if position is set to "top"
121 -- Thus the wibox has to be shown before setting its original slide up
122 -- position. As a side effect, the top bar might blink if position is set
123 -- to "top".
124 runwibox[s].visible = true
125 runwibox[s]:geometry({
126 y = screen[s].geometry.y + screen[s].geometry.height,
127 })
128 if lib.hooks.timer.has(do_slide_up) then
129 lib.hooks.timer.start(do_slide_up)
130 else
131 lib.hooks.timer.register(
132 settings.move_speed,
133 settings.move_speed*3,
134 do_slide_up,
135 "popup_run_prompt slide up"
136 )
137 end
138 else
139 set_default(s)
140 runwibox[s].visible = true
141 end
142 end
143
144 local function do_slide_down()
145 local s = runwibox.screen
146 startgeom = runwibox[s]:geometry()
147 runwibox[s]:geometry({
148 y = startgeom.y + settings.move_amount,
149 })
150
151 if runwibox[s]:geometry().y >= screen[s].geometry.y +
152 screen[s].geometry.height then
153 runwibox[s].visible = false
154 lib.hooks.timer.stop(do_slide_down)
155 end
156 end
157
158 local function hide_wibox()
159 local s = runwibox.screen or mouse.screen.index
160
161 if settings.slide == true then
162 runwibox[s].visible = true
163 set_default(s)
164
165 if lib.hooks.timer.has(do_slide_down) then
166 lib.hooks.timer.start(do_slide_down)
167 else
168 lib.hooks.timer.register(
169 settings.move_speed,
170 settings.move_speed*3,
171 do_slide_down,
172 "popup_run_prompt slide down"
173 )
174 end
175 else
176 set_default(s)
177 runwibox[s].visible = false
178 end
179 end
180
181 local function run_prompt_callback(command)
182 settings.run_function(command)
183 hide_wibox()
184 end
185
186 popup_run_prompt.hooks = {
187 -- Hide the prompt when user types 'ESC'
188 {{}, "Escape", function(_)
189 hide_wibox()
190 end},
191 }
192
193 function popup_run_prompt.run_prompt()
194 ensure_init()
195 show_wibox(mouse.screen.index)
196
197 awful.prompt.run(
198 { prompt = settings.prompt_string,
199 font = settings.prompt_font ,
200 hooks = popup_run_prompt.hooks,
201 textbox = mypromptbox[mouse.screen.index],
202 exe_callback = run_prompt_callback,
203 completion_callback = settings.completion_function,
204 history_path = awful.util.getdir("cache") .. settings.cache,
205 history_max = 100,
206 }
207 )
208 end
209
210 -- SETTINGS
211 function popup_run_prompt.set_opacity(amount)
212 settings.opacity = amount or defaults.opacity
213 update_settings()
214 end
215
216 function popup_run_prompt.set_prompt_string(string)
217 settings.prompt_string = string or popup_run_prompt.defaults.prompt_string
218 end
219
220 function popup_run_prompt.set_prompt_font(font_string)
221 settings.prompt_font = font_string or popup_run_prompt.defaults.prompt_font
222 end
223
224 function popup_run_prompt.set_slide(tf)
225 settings.slide = tf or popup_run_prompt.defaults.slide
226 end
227
228 function popup_run_prompt.set_width(amount)
229 settings.width = amount or popup_run_prompt.defaults.width
230 update_settings()
231 end
232
233 function popup_run_prompt.set_height(amount)
234 settings.height = amount or popup_run_prompt.defaults.height
235 update_settings()
236 end
237
238 function popup_run_prompt.set_border_width(amount)
239 settings.border_width = amount or popup_run_prompt.defaults.border_width
240 update_settings()
241 end
242
243 function popup_run_prompt.set_move_speed(amount)
244 settings.move_speed = amount or popup_run_prompt.defaults.move_speed
245 end
246
247 function popup_run_prompt.set_move_amount(amount)
248 settings.move_amount = amount or popup_run_prompt.defaults.move_amount
249 end
250
251 function popup_run_prompt.set_run_function(fn)
252 settings.run_function = fn or popup_run_prompt.defaults.run_function
253 end
254
255 function popup_run_prompt.set_completion_function(fn)
256 settings.completion_function = fn or popup_run_prompt.defaults.completion_function
257 end
258
259 function popup_run_prompt.set_position(p)
260 settings.position = p
261 end
262
263 function popup_run_prompt.update_settings()
264 for s, value in ipairs(runwibox) do
265 value.border_width = settings.border_width
266 set_default(s)
267 runwibox[s].opacity = settings.opacity
268 end
269 end
270
271 function popup_run_prompt.set_cache(c)
272 settings.cache = c or popup_run_prompt.defaults.cache
273 end
274
275 return popup_run_prompt
276
277 -- vim:ft=lua:ts=2:sw=2:sts=2:tw=80:et