-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfbrowse_tray.sf
378 lines (277 loc) · 9.52 KB
/
fbrowse_tray.sf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#!/usr/bin/ruby
#
## A file-browser through a Gtk3 tray status icon.
#
# Translation of:
# https://github.com/trizen/fbrowse-tray
use('Gtk3 -init')
use('File::MimeInfo')
const pkgname = 'fbrowse-tray'
const version = 0.07
const ICON_THEME = %S<Gtk3::IconTheme>.get_default
var (
FILE_MANAGER = (ENV{:FILEMANAGER} || 'pcmanfm'),
TERMINAL = (ENV{:TERM} || 'xterm'),
ICON_SIZE = 'menu',
STATUS_ICON = 'file-manager',
EXT_MIMETYPE = false,
TOOLTIP_PATH = false,
FILES_FIRST = false,
DIRS_ONLY = false,
)
func add_content() { }
# -------------------------------------------------------------------------------------
func open_file(file) {
Sys.run("#{FILE_MANAGER} #{file.escape} &")
}
# -------------------------------------------------------------------------------------
func add_header(menu, dir) {
# Append 'Open directory'
var open_dir = %O<Gtk3::ImageMenuItem>.new("Open directory")
open_dir.set_image(%O<Gtk3::Image>.new_from_icon_name('folder-open', ICON_SIZE));
open_dir.signal_connect(activate => { open_file(dir) })
menu.append(open_dir)
# Add 'Open in terminal'
var open_term = %O<Gtk3::ImageMenuItem>.new("Open in terminal")
open_term.set_image(%O<Gtk3::Image>.new_from_icon_name('utilities-terminal', ICON_SIZE))
open_term.signal_connect('activate' => { Sys.run("cd #{dir.escape}; #{TERMINAL} &") })
menu.append(open_term)
return true
}
# Add content of a directory as a submenu for an item
func create_submenu(item, dir) {
# Create a new menu
var menu = %O<Gtk3::Menu>.new
# Add 'Browse here...'
add_header(menu, dir)
# Append an horizontal separator
menu.append(%O<Gtk3::SeparatorMenuItem>.new)
# Add the dir content in this new menu
add_content(menu, dir)
# Set submenu for item to this new menu
item.set_submenu(menu)
# Make menu content visible
menu.show_all
return true
}
# -------------------------------------------------------------------------------------
# Append a directory to a submenu
func append_dir(submenu, dirname, dir) {
# Create the dir submenu
var dirmenu = %O<Gtk3::Menu>.new
# Create a new menu item
var item = %O<Gtk3::ImageMenuItem>.new(dirname)
# Set icon
item.set_image(%O<Gtk3::Image>.new_from_icon_name('folder', ICON_SIZE))
# Set a signal
item.signal_connect(activate => { create_submenu(item, dir); dirmenu.destroy })
# Set the submenu to the entry item
item.set_submenu(dirmenu)
# Append the item to the submenu
submenu.append(item)
return true
}
# -------------------------------------------------------------------------------------
# Returns true if a given icon exists in the current icon-theme
func is_icon_valid(icon) is cached {
ICON_THEME.has_icon(icon)
}
# Returns a valid icon name based on file's mime-type
func file_icon(filename, file) {
static alias = Hash()
var mime_type = (
(
(
EXT_MIMETYPE ? [%S<File::MimeInfo>.globs(filename)][0]
: %S<File::MimeInfo>.mimetype(file)
) \\ return 'unknown'
).gsub('/', '-')
)
alias.contains(mime_type) ->
&& return alias{mime_type}
do {
var type = mime_type
static re = /.*\K[[:punct:]]\w++$/
loop {
if (is_icon_valid(type)) {
return (alias{mime_type} = type)
}
elsif (is_icon_valid("gnome-mime-#{type}")) {
return (alias{mime_type} = "gnome-mime-#{type}")
}
type.match(re) ? type.gsub!(re) : break
}
}
{
var type = mime_type
static re = /^application-x-\K.*?-/
loop {
type.match(re) ? type.gsub!(re) : break
if (is_icon_valid(type)) {
return (alias{mime_type} = type)
}
}
}
alias{mime_type} = 'unknown'
}
# -------------------------------------------------------------------------------------
# File action
func file_actions(obj, event, file) {
if ((event.button == 1) || (event.button == 2)) {
open_file(file); # open the file
if (event.button == 1) {
return false # hide the menu when left-clicked
}
return true # keep the menu when middle-clicked
}
# Right-click menu
var menu = %O<Gtk3::Menu>.new
# Open
var open = %O<Gtk3::ImageMenuItem>.new('Open')
# Set icon
open.set_image(%O<Gtk3::Image>.new_from_icon_name('gtk-open', ICON_SIZE))
# Set a signal (activates on click)
open.signal_connect(activate => { open_file(file) })
# Append the item to the menu
menu.append(open)
# Delete
var delete = %O<Gtk3::ImageMenuItem>.new('Delete')
# Set icon
delete.set_image(%O<Gtk3::Image>.new_from_icon_name('gtk-delete', ICON_SIZE))
# Set a signal (activates on click)
delete.signal_connect(activate => { File.delete(file) && obj.destroy })
# Append the item to the menu
menu.append(delete)
# Show menu
menu.show_all
menu.popup(nil, nil, nil, [1, 1], 0, 0)
return true # don't hide the main menu
}
# -------------------------------------------------------------------------------------
# Append a file to a submenu
func append_file(submenu, filename, file) {
# Create a new menu item
var item = %O<Gtk3::ImageMenuItem>.new(filename)
# Set icon
item.set_image(%O<Gtk3::Image>.new_from_icon_name(file_icon(filename, file), ICON_SIZE))
# Set tooltip
TOOLTIP_PATH && item.set_property('tooltip_text', file)
# Set a signal (activates on click)
item.signal_connect('button-release-event' => func(obj, event) { file_actions(obj, event, file) })
# Append the item to the submenu
submenu.append(item)
return true
}
# -------------------------------------------------------------------------------------
# Read a content directory and add it to a submenu
add_content = func(submenu, dir) {
var dirs = []
var files = []
Dir.open(dir, \var dir_h) || return nil
struct Entry {
String name,
File path,
}
dir_h.each { |filename|
# Ignore hidden files
filename.begins_with('.') && next
# Join directory with the filename
var path = File(dir, filename)
path.exists || (path = Dir(dir, filename))
# Resolve absolute path
if (path.is_link) {
path.abs_path!
path.exists || next
}
# Ignore non-directories (with -d)
if (DIRS_ONLY) {
path.is_dir || next
}
# Collect the files and dirs
(path.is_dir ? dirs : files) << Entry(filename.gsub('_', '__'), path)
}
dir_h.close
struct Entries {
Array content,
Block function,
}
var categories = [Entries(dirs, append_dir),
Entries(files, append_file)]
for category in (FILES_FIRST ? categories.reverse : categories) {
category.content.sort_by { .name.fc }.each { |entry|
var label = entry.name
if (label.len > 64) {
label = (label.first(32) + '⋯' + label.last(32))
}
category.function.call(submenu, label, entry.path)
}
}
return true
}
# -------------------------------------------------------------------------------------
# Create the main menu and populate it with the content of $dir
func create_main_menu(icon, dir, event) {
var menu = %O<Gtk3::Menu>.new
if (event.button == 1) {
add_content(menu, dir)
}
elsif (event.button == 3) {
# Create a new menu item
var exit = %O<Gtk3::ImageMenuItem>.new('Quit')
# Set icon
exit.set_image(%O<Gtk3::Image>.new_from_icon_name('application-exit', ICON_SIZE))
# Set a signal (activates on click)
exit.signal_connect(activate => { %O<Gtk3>.main_quit })
# Append the item to the menu
menu.append(exit)
}
menu.show_all
menu.popup(nil, nil, { %S<Gtk3::StatusIcon>.position_menu(menu, 0, 0, icon) }, [1, 1], 0, 0)
return true
}
# -------------------------------------------------------------------------------------
#
## Main
#
func usage(code=0) {
var main = File(__MAIN__).basename
print <<"USAGE"
usage: #{main} [options] [dir]
options:
-r : order files before directories
-d : display only directories
-T : set the path of files as tooltips
-e : get the mimetype by extension only (faster)
-i [name] : name of the status icon (default: #{STATUS_ICON})
-f [command] : command to open the files with (default: #{FILE_MANAGER})
-t [command] : terminal command for "Open in terminal" (default: #{TERMINAL})
-m [type] : type of menu icons (default: #{ICON_SIZE})
more: dnd, dialog, button, small-toolbar, large-toolbar
example:
#{main} -f thunar -m dnd /my/dir
USAGE
Sys.exit(code)
}
func output_version {
say "#{pkgname} #{version}"
Sys.exit(0)
}
ARGV.getopt!(
'd!' => \DIRS_ONLY,
'T!' => \TOOLTIP_PATH,
'r!' => \FILES_FIRST,
'e!' => \EXT_MIMETYPE,
'i=s' => \STATUS_ICON,
'f=s' => \FILE_MANAGER,
't=s' => \TERMINAL,
'm=s' => \ICON_SIZE,
'h' => usage,
'v' => output_version,
)
var dir = Dir(ARGV.shift)
dir.exists || usage(2)
var icon = %O<Gtk3::StatusIcon>.new
icon.set_from_icon_name(STATUS_ICON)
icon.set_visible(true)
icon.signal_connect('button-release-event' => func(_, event) { create_main_menu(icon, dir, event) })
%O<Gtk3>.main