Performance improve pickers
This commit is contained in:
+77
-13
@@ -106,22 +106,34 @@ mkdir -p "$cache_dir"
|
||||
cache_key=$(printf '%s' "$image_dirs_env" | md5sum | cut -d ' ' -f 1)
|
||||
rows_cache_file="$cache_dir/$cache_key.rows"
|
||||
rows_signature_file="$cache_dir/$cache_key.signature"
|
||||
rows_fast_signature_file="$cache_dir/$cache_key.fast-signature"
|
||||
rows_signature="v2"$'\n'
|
||||
rows_fast_signature="v1"$'\n'
|
||||
rows_cacheable=true
|
||||
rows_cache_hit=false
|
||||
image_files=()
|
||||
|
||||
for dir in "${image_dirs[@]}"; do
|
||||
if [[ -d $dir ]]; then
|
||||
rows_signature+="$dir:$(stat -Lc '%Y' "$dir")"$'\n'
|
||||
|
||||
while IFS= read -r -d '' image; do
|
||||
image_files+=("$image")
|
||||
image_signature=$(stat -Lc '%s:%Y' "$image") || continue
|
||||
rows_signature+="$image:$image_signature"$'\n'
|
||||
done < <(find -L "$dir" -maxdepth 1 -type f \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' -o -iname '*.gif' -o -iname '*.bmp' -o -iname '*.webp' \) -print0 2>/dev/null | sort -z)
|
||||
fi
|
||||
[[ -d $dir ]] && rows_fast_signature+="$dir:$(stat -Lc '%Y' "$dir")"$'\n'
|
||||
done
|
||||
|
||||
if [[ -f $rows_cache_file && -f $rows_fast_signature_file ]] && cmp -s "$rows_fast_signature_file" <(printf '%s' "$rows_fast_signature"); then
|
||||
rows=$(<"$rows_cache_file")
|
||||
rows_cache_hit=true
|
||||
else
|
||||
for dir in "${image_dirs[@]}"; do
|
||||
if [[ -d $dir ]]; then
|
||||
rows_signature+="$dir:$(stat -Lc '%Y' "$dir")"$'\n'
|
||||
|
||||
while IFS= read -r -d '' image; do
|
||||
image_files+=("$image")
|
||||
image_signature=$(stat -Lc '%s:%Y' "$image") || continue
|
||||
rows_signature+="$image:$image_signature"$'\n'
|
||||
done < <(find -L "$dir" -maxdepth 1 -type f \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' -o -iname '*.gif' -o -iname '*.bmp' -o -iname '*.webp' \) -print0 2>/dev/null | sort -z)
|
||||
fi
|
||||
done
|
||||
fi
|
||||
|
||||
generate_thumbnail() {
|
||||
local image="$1"
|
||||
local thumbnail="$2"
|
||||
@@ -177,9 +189,10 @@ thumbnail_for() {
|
||||
[[ -f $thumbnail ]] && printf '%s' "$thumbnail"
|
||||
}
|
||||
|
||||
if [[ -f $rows_cache_file && -f $rows_signature_file ]] && cmp -s "$rows_signature_file" <(printf '%s' "$rows_signature"); then
|
||||
if [[ $rows_cache_hit != true && -f $rows_cache_file && -f $rows_signature_file ]] && cmp -s "$rows_signature_file" <(printf '%s' "$rows_signature"); then
|
||||
rows=$(<"$rows_cache_file")
|
||||
else
|
||||
printf '%s' "$rows_fast_signature" >"$rows_fast_signature_file"
|
||||
elif [[ $rows_cache_hit != true ]]; then
|
||||
for image in "${image_files[@]}"; do
|
||||
thumbnail=$(thumbnail_for "$image")
|
||||
[[ -n $thumbnail ]] || continue
|
||||
@@ -197,8 +210,9 @@ else
|
||||
if [[ $rows_cacheable == true ]]; then
|
||||
printf '%s' "$rows" >"$rows_cache_file"
|
||||
printf '%s' "$rows_signature" >"$rows_signature_file"
|
||||
printf '%s' "$rows_fast_signature" >"$rows_fast_signature_file"
|
||||
else
|
||||
rm -f "$rows_cache_file" "$rows_signature_file"
|
||||
rm -f "$rows_cache_file" "$rows_signature_file" "$rows_fast_signature_file"
|
||||
fi
|
||||
fi
|
||||
|
||||
@@ -211,7 +225,57 @@ fi
|
||||
# ImagePicker plugin Qt.atob()s on the other side.
|
||||
rows_b64=$(printf '%s' "$rows" | base64 -w 0)
|
||||
|
||||
if ! omarchy-shell-ipc image-selector open \
|
||||
send_builtin_ipc_request() {
|
||||
perl -MCwd=abs_path -MEncode=encode,decode -MSocket \
|
||||
-e '
|
||||
sub read_qstring {
|
||||
my ($data, $offset) = @_;
|
||||
return ("", $offset) if $offset + 4 > length($data);
|
||||
my $length = unpack("N", substr($data, $offset, 4));
|
||||
$offset += 4;
|
||||
return ("", $offset) if $length == 0xffffffff || $offset + $length > length($data);
|
||||
return (decode("UTF-16BE", substr($data, $offset, $length)), $offset + $length);
|
||||
}
|
||||
|
||||
sub qstring {
|
||||
my $encoded = encode("UTF-16BE", $_[0] // "");
|
||||
return pack("N", length($encoded)) . $encoded;
|
||||
}
|
||||
|
||||
my $shell_qml = abs_path(shift @ARGV);
|
||||
my @open_args = @ARGV;
|
||||
my $runtime_dir = $ENV{"XDG_RUNTIME_DIR"} || "/run/user/$<";
|
||||
my $payload = chr(3) . qstring("image-selector") . qstring("open") . pack("N", scalar @open_args) . join("", map { qstring($_) } @open_args);
|
||||
my @candidates;
|
||||
|
||||
for my $lock_path (glob("$runtime_dir/quickshell/by-id/*/instance.lock")) {
|
||||
my $data;
|
||||
next unless open(my $lock, "<:raw", $lock_path);
|
||||
{ local $/; $data = <$lock>; }
|
||||
close($lock);
|
||||
my (undef, $offset) = read_qstring($data, 0);
|
||||
my ($path) = read_qstring($data, $offset);
|
||||
next unless $path && abs_path($path) eq $shell_qml;
|
||||
(my $socket_path = $lock_path) =~ s{/instance\.lock$}{/ipc.sock};
|
||||
push @candidates, [(stat($lock_path))[9] || 0, $socket_path];
|
||||
}
|
||||
|
||||
for my $candidate (sort { $b->[0] <=> $a->[0] } @candidates) {
|
||||
socket(my $client, AF_UNIX, SOCK_STREAM, 0) || next;
|
||||
if (connect($client, sockaddr_un($candidate->[1]))) {
|
||||
syswrite($client, $payload);
|
||||
close($client);
|
||||
exit 0;
|
||||
}
|
||||
close($client);
|
||||
}
|
||||
|
||||
exit 1;
|
||||
' "$OMARCHY_PATH/default/quickshell/omarchy-shell/shell.qml" \
|
||||
"" "$rows_b64" "$selected_list_image" "$selection_file" "$done_file" "$show_labels" "$filterable"
|
||||
}
|
||||
|
||||
if ! send_builtin_ipc_request && ! omarchy-shell-ipc image-selector open \
|
||||
"" \
|
||||
"$rows_b64" \
|
||||
"$selected_list_image" \
|
||||
|
||||
Reference in New Issue
Block a user