Skip to content

Commit 77de0fb

Browse files
committed
Merge branch 'feature/notif-success-actions'
Signed-off-by: kilo52 <[email protected]>
2 parents 6a41cc6 + 9641210 commit 77de0fb

File tree

1 file changed

+80
-18
lines changed

1 file changed

+80
-18
lines changed

libinit.sh

Lines changed: 80 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,13 @@ HYPERLINK_VALUE="";
366366
# 1.3.0
367367
VAR_FILE_VALUE="";
368368

369+
# [API Global]
370+
# The application name to display to the user. This is shown, for example,
371+
# in desktop notifications. A different value may be set by addons.
372+
# Since:
373+
# 1.9.0
374+
PROJECT_INIT_APPLICATION_NAME="Project Init";
375+
369376
# [API Global]
370377
# The message to show when a project is successfully initialized.
371378
# This text is shown in the terminal and in the desktop notification.
@@ -759,38 +766,93 @@ function _ensure_project_files_copied() {
759766
fi
760767
}
761768

769+
# Invokes notify-send to show a desktop notification.
770+
#
771+
# This implementation function also handles the optional notification actions.
772+
#
773+
# Args:
774+
# $@ - All arguments to be passed to notify-send.
775+
#
776+
function _show_notif_success_impl() {
777+
local act;
778+
act=$(notify-send "$@");
779+
if [[ "$act" == "Open" ]]; then
780+
nautilus --new-window "${var_project_dir}" &> /dev/null;
781+
elif [[ "$act" == "Term" ]]; then
782+
gnome-terminal --window \
783+
--working-directory="${var_project_dir}" &> /dev/null;
784+
fi
785+
}
786+
787+
# Checks if the installed version of notify-send supports notification actions.
788+
# Notification actions are supported in notify-send version 0.7.10 and above.
789+
#
790+
# Returns:
791+
# 0 - If notification actions are supported.
792+
# 1 - If notification actions are not supported.
793+
#
794+
function _can_add_notif_actions() {
795+
local version;
796+
version=$(notify-send --version |cut -d ' ' -f2);
797+
if (( $? != 0 )); then
798+
return 1;
799+
fi
800+
# The format is known to be e.g.: '0.1.2'
801+
# shellcheck disable=SC2206
802+
version=(${version//./ });
803+
local major="${version[0]}";
804+
local minor="${version[1]}";
805+
local patch="${version[2]}";
806+
if (( major == 0 )); then
807+
if (( minor < 7 )); then
808+
return 1;
809+
fi
810+
if (( minor == 7 )); then
811+
if (( patch < 10 )); then
812+
return 1;
813+
fi
814+
fi
815+
fi
816+
return 0;
817+
}
818+
762819
# Shows a system notification indicating a successful operation.
763820
#
764-
# This function will try to display a desktop notification if
765-
# the notify-send command is available. It returns exit status 1
766-
# if the notify-send command is not available, otherwise it returns
767-
# the exit status of notify-send.
821+
# This function will try to display a desktop notification if the
822+
# notify-send command is available. It returns exit status 1 if
823+
# the notify-send command is not available, otherwise it returns 0 (zero).
768824
#
769825
function _show_notif_success() {
770826
if _command_dependency "notify-send"; then
827+
local notif_args=();
771828
local _project_name="New Project";
772829
if [ -n "$var_project_name" ]; then
773830
_project_name="$var_project_name";
774831
fi
775-
local _has_icon=false;
776832
if [ -n "${_STR_NOTIF_SUCCESS_ICON}" ]; then
777833
if [ -r "${_STR_NOTIF_SUCCESS_ICON}" ]; then
778-
_has_icon=true;
834+
notif_args+=("--icon");
835+
notif_args+=("${_STR_NOTIF_SUCCESS_ICON}");
779836
fi
780837
fi
781-
if [[ ${_has_icon} == true ]]; then
782-
notify-send -i "${_STR_NOTIF_SUCCESS_ICON}" \
783-
-t ${_INT_NOTIF_SUCCESS_TIMEOUT} \
784-
--app-name "Project Init" \
785-
"${_project_name}" \
786-
"${PROJECT_INIT_SUCCESS_MESSAGE}";
787-
else
788-
notify-send -t ${_INT_NOTIF_SUCCESS_TIMEOUT} \
789-
--app-name "Project Init" \
790-
"${_project_name}" \
791-
"${PROJECT_INIT_SUCCESS_MESSAGE}";
838+
notif_args+=("--expire-time");
839+
notif_args+=("${_INT_NOTIF_SUCCESS_TIMEOUT}");
840+
notif_args+=("--app-name");
841+
notif_args+=("$PROJECT_INIT_APPLICATION_NAME");
842+
if _can_add_notif_actions; then
843+
if _command_dependency "nautilus"; then
844+
notif_args+=("--action");
845+
notif_args+=("Open=Show Files");
846+
fi
847+
if _command_dependency "gnome-terminal"; then
848+
notif_args+=("--action");
849+
notif_args+=("Term=Open in Terminal");
850+
fi
792851
fi
793-
return $?;
852+
notif_args+=("${_project_name}");
853+
notif_args+=("${PROJECT_INIT_SUCCESS_MESSAGE}");
854+
_show_notif_success_impl "${notif_args[@]}" &
855+
return 0;
794856
fi
795857
return 1;
796858
}

0 commit comments

Comments
 (0)