#!/bin/bash
# The MSIME menu for Omarchy, opened by the bar widget in plugin/. Omarchy starts fcitx5 with --disable notificationitem, so the tray menu that carries these entries elsewhere never shows; this offers the ones that do not need a focused text field, through Omarchy's own picker. Entries that act on the text being typed (中英, 简繁, punctuation) stay on their keyboard shortcuts.
set -uo pipefail

settings='/usr/bin/msime-linux-settings'
command -v omarchy-menu-select >/dev/null 2>&1 || exit 0

entries=(
  设置
  词库
  手写
  屏幕键盘
  表情
  语音输入
  云剪贴板
  重载水杉输入法
)
# Only the unit can restart fcitx5 cleanly there: fcitx5 -r would start a copy outside it that fights the unit's own for the bus name.
if systemctl --user is-active --quiet omarchy-fcitx5.service 2>/dev/null; then
  entries+=('重启 Fcitx5')
fi

choice=$(omarchy-menu-select 水杉输入法 "${entries[@]}") || exit 0
case "$choice" in
  设置) exec "$settings" ;;
  词库) exec "$settings" --panel dictionary ;;
  手写) exec "$settings" --panel handwriting ;;
  屏幕键盘) exec "$settings" --panel keyboard ;;
  表情) exec "$settings" --panel emoji ;;
  语音输入) exec "$settings" --panel voice ;;
  云剪贴板) exec "$settings" --panel cloud-clipboard ;;
  # The same in-process reset as the settings page's restart button and Ctrl+Shift+Alt+R: every composition ends and runtime-options.json is read again, other input methods are untouched.
  重载水杉输入法) exec gdbus call --session --dest org.fcitx.Fcitx5 --object-path /controller --method org.fcitx.Fcitx.Controller1.ReloadAddonConfig "'msime'" ;;
  '重启 Fcitx5') exec systemctl --user restart omarchy-fcitx5.service ;;
esac
