Skip to content

Add Actions to Desktop Notifications #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.7.6-dev
1.9.0-dev
100 changes: 81 additions & 19 deletions libinit.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
# Copyright (C) 2024 Raven Computing
# Copyright (C) 2025 Raven Computing
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -366,6 +366,13 @@ HYPERLINK_VALUE="";
# 1.3.0
VAR_FILE_VALUE="";

# [API Global]
# The application name to display to the user. This is shown, for example,
# in desktop notifications. A different value may be set by addons.
# Since:
# 1.9.0
PROJECT_INIT_APPLICATION_NAME="Project Init";

# [API Global]
# The message to show when a project is successfully initialized.
# This text is shown in the terminal and in the desktop notification.
Expand Down Expand Up @@ -679,38 +686,93 @@ function _log_success() {
fi
}

# Invokes notify-send to show a desktop notification.
#
# This implementation function also handles the optional notification actions.
#
# Args:
# $@ - All arguments to be passed to notify-send.
#
function _show_notif_success_impl() {
local act;
act=$(notify-send "$@");
if [[ "$act" == "Open" ]]; then
nautilus --new-window "${var_project_dir}" &> /dev/null;
elif [[ "$act" == "Term" ]]; then
gnome-terminal --window \
--working-directory="${var_project_dir}" &> /dev/null;
fi
}

# Checks if the installed version of notify-send supports notification actions.
# Notification actions are supported in notify-send version 0.7.10 and above.
#
# Returns:
# 0 - If notification actions are supported.
# 1 - If notification actions are not supported.
#
function _can_add_notif_actions() {
local version;
version=$(notify-send --version |cut -d ' ' -f2);
if (( $? != 0 )); then
return 1;
fi
# The format is known to be e.g.: '0.1.2'
# shellcheck disable=SC2206
version=(${version//./ });
local major="${version[0]}";
local minor="${version[1]}";
local patch="${version[2]}";
if (( major == 0 )); then
if (( minor < 7 )); then
return 1;
fi
if (( minor == 7 )); then
if (( patch < 10 )); then
return 1;
fi
fi
fi
return 0;
}

# Shows a system notification indicating a successful operation.
#
# This function will try to display a desktop notification if
# the notify-send command is available. It returns exit status 1
# if the notify-send command is not available, otherwise it returns
# the exit status of notify-send.
# This function will try to display a desktop notification if the
# notify-send command is available. It returns exit status 1 if
# the notify-send command is not available, otherwise it returns 0 (zero).
#
function _show_notif_success() {
if _command_dependency "notify-send"; then
local notif_args=();
local _project_name="New Project";
if [ -n "$var_project_name" ]; then
_project_name="$var_project_name";
fi
local _has_icon=false;
if [ -n "${_STR_NOTIF_SUCCESS_ICON}" ]; then
if [ -r "${_STR_NOTIF_SUCCESS_ICON}" ]; then
_has_icon=true;
notif_args+=("--icon");
notif_args+=("${_STR_NOTIF_SUCCESS_ICON}");
fi
fi
if [[ ${_has_icon} == true ]]; then
notify-send -i "${_STR_NOTIF_SUCCESS_ICON}" \
-t ${_INT_NOTIF_SUCCESS_TIMEOUT} \
--app-name "Project Init" \
"${_project_name}" \
"${PROJECT_INIT_SUCCESS_MESSAGE}";
else
notify-send -t ${_INT_NOTIF_SUCCESS_TIMEOUT} \
--app-name "Project Init" \
"${_project_name}" \
"${PROJECT_INIT_SUCCESS_MESSAGE}";
notif_args+=("--expire-time");
notif_args+=("${_INT_NOTIF_SUCCESS_TIMEOUT}");
notif_args+=("--app-name");
notif_args+=("$PROJECT_INIT_APPLICATION_NAME");
if _can_add_notif_actions; then
if _command_dependency "nautilus"; then
notif_args+=("--action");
notif_args+=("Open=Show Files");
fi
if _command_dependency "gnome-terminal"; then
notif_args+=("--action");
notif_args+=("Term=Open in Terminal");
fi
fi
return $?;
notif_args+=("${_project_name}");
notif_args+=("${PROJECT_INIT_SUCCESS_MESSAGE}");
_show_notif_success_impl "${notif_args[@]}" &
return 0;
fi
return 1;
}
Expand Down