1313# generate errors if unset vars are used.
1414set -o nounset
1515
16- # global widget params
16+ # global panel geometry
1717declare -ri BIW_MARGIN=10
1818declare -ri BIW_PANEL_HEIGHT=20
1919declare -ri BIW_PANEL_WIDTH=60
@@ -27,19 +27,21 @@ source ${BIW_HOME}/biw-panel-credits.sh
2727
2828declare -r BIW_VERSION=0.9
2929
30- # selected result when a panel is closed
30+ # a controller can set a result here when it is closed.
3131declare biw_selection_result
3232
33- # controllers will set this when the app should terminate
34- declare -i biw_terminate_app=0
35-
3633function fn_biw_main()
3734{
3835 # remove any existing result file.
3936 rm -f $BIW_RESULT_FILE
4037
41- # show the widgets
42- fn_biw_show
38+ fn_util_panel_open
39+ fn_theme_init
40+
41+ # show the panel
42+ fn_biw_controller_hmenu_top
43+
44+ fn_util_panel_close
4345
4446 if [ -n " $biw_selection_result " ]
4547 then
@@ -48,89 +50,80 @@ function fn_biw_main()
4850 fi
4951}
5052
51- function fn_biw_show()
53+ # Entires in H-Menu
54+ declare -r BIW_MENU_HISTORY=' History'
55+ declare -r BIW_MENU_BROWSE=' File'
56+ declare -r BIW_MENU_THEME=' Theme'
57+ declare -r BIW_MENU_HOTKEY=' Hotkey'
58+ declare -r BIW_MENU_CREDITS=' Credits'
59+ declare -r BIW_MENU_CONFIG=' Config'
60+ # declare -r BIW_MENU_DEFAULT='Default'
61+
62+ # BIW_DISPATCH_MAP:
63+ # This determines what controller is invoked when an H-Menu entry is
64+ # selected.
65+ #
66+ # A controller can also invoke a second-level H-Menu but regardless
67+ # of the level, it uses this map to determine what its menu entries
68+ # invoke.
69+ declare -A BIW_DISPATCH_MAP=(
70+ [$BIW_MENU_HISTORY ]=fn_biw_controller_history
71+ [$BIW_MENU_BROWSE ]=fn_biw_controller_browse
72+ [$BIW_MENU_CONFIG ]=fn_biw_controller_hmenu_config
73+ [$BIW_MENU_THEME ]=fn_biw_controller_cfg_theme
74+ [$BIW_MENU_HOTKEY ]=fn_biw_controller_hotkey
75+ [$BIW_MENU_CREDITS ]=fn_biw_controller_credits)
76+
77+
78+ function fn_biw_controller_hmenu_top()
5279{
53-
54- fn_utl_panel_open
55-
56- fn_theme_init
57-
58- # Entires in HMenu
59- local -r BIW_MENU_HISTORY=' History'
60- local -r BIW_MENU_BROWSE=' File'
61- local -r BIW_MENU_THEME=' Theme'
62- local -r BIW_MENU_CREDITS=' Credits'
63- local -r BIW_MENU_HOTKEY=' Hotkey'
64-
65- local -a _hmenu_values=(
66- $BIW_MENU_HISTORY
67- $BIW_MENU_BROWSE
68- $BIW_MENU_THEME
69- $BIW_MENU_HOTKEY
80+ local -a _top_menu=(
81+ $BIW_MENU_HISTORY
82+ $BIW_MENU_BROWSE
83+ $BIW_MENU_CONFIG
7084 $BIW_MENU_CREDITS )
7185
72- fn_hmenu_init _hmenu_values[@]
86+ # setup the H-Menu
87+ fn_hmenu_init ' _top_menu[@]'
7388
74- while [ 1 ]
75- do
76- fn_hmenu_get_current_val ' _menu_val'
77- fn_hmenu_redraw
78- biw_selection_result=' '
79-
80- case $_menu_val in
81- $BIW_MENU_HISTORY )
82- fn_ctl_history_controller
83- ;;
84- $BIW_MENU_BROWSE )
85- fn_ctl_browse_controller
86- ;;
87- $BIW_MENU_THEME )
88- fn_ctl_cfg_theme_controller
89- ;;
90- $BIW_MENU_CREDITS )
91- fn_ctl_credits_controller
92- ;;
93- $BIW_MENU_HOTKEY )
94- fn_ctl_hotkey_controller
95- ;;
96- * )
97- fn_ctl_default
98- ;;
99- esac
100-
101- # terminate if controller returned action ignored status
102- if(( biw_terminate_app))
103- then
104- break
105- fi
106- done
107-
108- fn_utl_panel_close
89+ # call the main dispatcher that will invoke the
90+ # appropriate controller.
91+ fn_util_dispatcher
10992}
11093
111- # #
112- # Controller: Default with no panel.
113- # #
114-
115- function fn_ctl_default()
94+ function fn_biw_controller_hmenu_config()
11695{
117- local _key
118-
119- while fn_utl_process_key _key
120- do
121- if [ " $_key " == $CSI_KEY_ENTER ]
122- then
123- biw_terminate_app=1
124- break
125- fi
126- done
96+ # If we are at this point then it means:
97+ # 1. The user selected the "Config" entry in the H-Menu.
98+ # 2. The dispatcher looked up the associated controller
99+ # in BIW_DISPATCH_MAP.
100+ # 3. The dispatcher invoked this controller and is expecting
101+ # it to return when done.
102+
103+ local -a _config_menu=(
104+ $BIW_MENU_THEME
105+ $BIW_MENU_HOTKEY )
106+
107+ # Call the dispatcher that will handle actions
108+ # for a second-level menu
109+ fn_hmenu_controller_sub ' _config_menu[@]'
110+
111+ # If we are at this point and the user used a config panel then it means:
112+ # 1. The H-Menu controller displayed the secondary menu and
113+ # waited for the user to hit the down key.
114+ # 2. If the down key was hit the H-Menu controller invoked
115+ # a second level dispatcher which is a recursive call.
116+ # 3. The dispatcher compared selected item of the secondary H-Menu and invoked
117+ # the associated controller for the panel.
118+ # 4. The panel controller set the util_exit_dispatcher flag and returned.
119+ # 5. The second level dispatcher caught the exit flag and returned.
127120}
128121
129122# #
130123# Controller: Hotkey configuration panel.
131124# #
132125
133- function fn_ctl_hotkey_controller ()
126+ function fn_biw_controller_hotkey ()
134127{
135128 local -A _bind_selections=(
136129 [' Arrow-Up' ]=$CSI_KEY_UP
@@ -150,29 +143,28 @@ function fn_ctl_hotkey_controller()
150143 mapfile -t _key_descr_list < <( echo " ${! _bind_selections[*]} " | sort)
151144
152145 fn_vmenu_init _key_descr_list[@]
153- fn_vmenu_set_message " Choose activation hotkey"
154-
155- fn_ctl_load_hotkey_idx
146+ fn_biw_load_hotkey_idx
156147 vmenu_idx_selected=$?
157148
158- vmenu_ind_values=( [vmenu_idx_selected]=$BIW_CHAR_BULLET )
149+ fn_vmenu_set_message " Choose activation hotkey"
150+ fn_vmenu_set_checked
159151 fn_vmenu_redraw
160152
161153 local _key
162- while fn_utl_process_key _key
154+ while fn_util_process_key _key
163155 do
164156 fn_vmenu_actions " $_key "
165157
166158 case " $_key " in
167159 $CSI_KEY_ENTER |$CSI_KEY_SPC )
168- fn_ctl_save_hotkey
160+ fn_biw_save_hotkey
169161 fn_vmenu_redraw
170162 ;;
171163 esac
172164 done
173165}
174166
175- function fn_ctl_load_hotkey_idx ()
167+ function fn_biw_load_hotkey_idx ()
176168{
177169 # create lookups
178170 local -A _bind_desc_lookup=()
@@ -202,25 +194,23 @@ function fn_ctl_load_hotkey_idx()
202194 return $_selected_bind_idx
203195}
204196
205- function fn_ctl_save_hotkey ()
197+ function fn_biw_save_hotkey ()
206198{
207199 local _selected_bind_desc
208200 fn_vmenu_get_current_val ' _selected_bind_desc'
209201
210202 local _selected_bind_key=${_bind_selections[$_selected_bind_desc]}
211203 fn_settings_set_hotkey $_selected_bind_key
212204
213- # update bullet in panel
214- vmenu_ind_values=( [vmenu_idx_selected]=$BIW_CHAR_BULLET )
215-
205+ fn_vmenu_set_checked
216206 fn_vmenu_set_message " Hotkey saved: [${_selected_bind_desc} ]=${_selected_bind_key} "
217207}
218208
219209# #
220210# Controller: Credits Animation panel
221211# #
222212
223- function fn_ctl_credits_controller ()
213+ function fn_biw_controller_credits ()
224214{
225215 # change to matrix theme
226216 local -i _theme_idx=${theme_id_lookup[THEME_TYPE_MATRIX]}
@@ -241,7 +231,7 @@ function fn_ctl_credits_controller()
241231# max values to load for history and file lists
242232declare -ri BIW_LIST_MAX=50
243233
244- function fn_ctl_history_controller ()
234+ function fn_biw_controller_history ()
245235{
246236 local _panel_command=" fc -lnr -$BIW_LIST_MAX "
247237 local -a _values
@@ -254,14 +244,13 @@ function fn_ctl_history_controller()
254244
255245 fn_vmenu_init _values[@]
256246 fn_vmenu_set_message " [ENTER] key copies to prompt"
257-
258247 fn_vmenu_redraw
259248
260249 local _key
261- while fn_utl_process_key _key
250+ while fn_util_process_key _key
262251 do
263252 fn_vmenu_actions " $_key "
264- if [ $? == $UTL_ACT_CHANGED ]
253+ if [ $? == $UTIL_ACT_CHANGED ]
265254 then
266255 # vmenu handled the action so get next key
267256 continue
@@ -271,7 +260,7 @@ function fn_ctl_history_controller()
271260 $CSI_KEY_ENTER )
272261 # we got the enter key so close the menu
273262 fn_vmenu_get_current_val " biw_selection_result"
274- biw_terminate_app =1
263+ util_exit_dispatcher =1
275264 break
276265 ;;
277266 esac
@@ -282,20 +271,19 @@ function fn_ctl_history_controller()
282271# Controller: Theme configuration panel.
283272# #
284273
285- function fn_ctl_cfg_theme_controller ()
274+ function fn_biw_controller_cfg_theme ()
286275{
287276 # load theme data into menu
288277 fn_vmenu_init " theme_desc_lookup[@]" $theme_active_idx
289278 fn_vmenu_set_message " Choose default theme"
290- vmenu_ind_values=( [theme_active_idx]=$BIW_CHAR_BULLET )
291-
279+ fn_vmenu_set_checked
292280 fn_vmenu_redraw
293281
294282 local _key
295- while fn_utl_process_key _key
283+ while fn_util_process_key _key
296284 do
297285 fn_vmenu_actions " $_key "
298- if [ $? == $UTL_ACT_CHANGED ]
286+ if [ $? == $UTIL_ACT_CHANGED ]
299287 then
300288 # use the vmenu index to determine the selected theme
301289 fn_theme_set_idx_active $vmenu_idx_selected
@@ -317,9 +305,9 @@ function fn_ctl_cfg_theme_controller()
317305function fn_theme_save()
318306{
319307 local _saved_theme=${THEME_LIST[$theme_active_idx]}
320- vmenu_ind_values=( [theme_active_idx]=$BIW_CHAR_BULLET )
321-
322308 fn_settings_set_param $THEME_PARAM_NAME $_saved_theme
309+
310+ fn_vmenu_set_checked
323311 fn_vmenu_set_message " Theme saved: ${_saved_theme} "
324312}
325313
0 commit comments