24. Windows and layouts
The studio's windows, again. Since chapter 9 the studio has drawn its own View menu and kept a flag per panel, and since chapter 16 it has saved a layout file for Reset layout. FIGURE keeps that registry itself: the program names its panels and the layouts it offers, FIGURE draws their part of the View menu, remembers what the user closed, and tells the program which layout to build. In a window, the user's settings are kept between runs too.
The whole program: tut24_windows.F90, chapter 23's studio with the changes below.
What this chapter adds to chapter 23's program, tut23_player.F90:
register_windows: The studio's panels and layouts, registered with FIGUREload_prefs: The user's settings of the last runsettle: Draw the studio until its interface is idle- changed:
add_recent,draw_menus,draw_panels,draw_run,draw_tools,open_window,self_test
The panels, registered
call app%add_window('Case', closable=.false.) ! always there: no close button
do k = 2_int32, size(PANELS, kind=int32)
call app%add_window(trim(PANELS(k))) ! Input, Run, Result, 3-D: closed with their tab's button
enddo
call app%add_window('Messages', visible=.false.) ! opened from Help > Messages, or the View menu
call app%add_layout('Setup', windows=[character(len=8) :: 'Case', 'Input', 'Run', 'Result', '3-D'])
call app%add_layout('Analysis', windows=[character(len=8) :: 'Case', 'Run', 'Result', '3-D', 'Messages'])app%add_window(name) registers a window by the name ig_begin is given; app%add_layout(name, windows=...) declares a layout and the panels it shows (Windows and layouts). The studio does it once, after app%init (and after init_headless in its test).
- Case is not closable (
closable=.false.): the panel that says which case is open stays. The others get a close button on their tab. - Messages starts hidden (
visible=.false.): Help > Messages and the View menu show it. - A layout's
windowsare shown when it becomes current, the other registered windows hidden: Analysis has no Input panel, and shows Messages.
A registered window is drawn while app%window_visible(name) is true, and ig_begin is called without open: the close button writes FIGURE's flag, which the next frame reads. The flags of chapters 9 and 16 (shown(5), messages_shown) are gone:
if (app%window_visible('Case')) then ! a registered window is drawn while it is shown
if (ig_begin('Case')) call draw_case() ! no open=: FIGURE's flag, the close button hides it
call ig_end()
endif
if (app%window_visible('Input')) then
if (ig_begin('Input')) call draw_input()
call ig_end()
endifThe tools' bar shows a panel the user closed with app%set_window_visible(name, .true.).
The View menu
call app%view_menu() ! the panels, the layouts, Reset layout
if (ig_begin_menu('View')) then ! the same menu again: the studio's items follow FIGURE's
call ig_separator()
if (ig_menu_item('Larger text', selected=large_text)) then
call set_text_size()
call app%save_pref('larger text', large_text)
endifapp%view_menu() draws the menu View: a check item per registered panel, the layouts with the current one checked, then Reset layout. The studio's own items, Larger text, Look and Palette, follow in the same menu: Dear ImGui appends the items of a second ig_begin_menu('View') to the menu of that name. Help > Messages uses the registry too: app%window_visible('Messages') checks it, app%set_window_visible follows the click.
Layouts the program builds
root = ig_dock_id('studio')
if (ig_dock_layout_begin(root)) then ! the first frame, after a switch of layout and after a reset
call ig_dock_split(root, 'left', 0.25_real64, left, right)
call ig_dock_window('Case', left)
select case (app%layout()) ! the one the user chose
case ('Setup') ! the input in front, the run below
call ig_dock_split(right, 'down', 0.45_real64, run, work)
call ig_dock_window('3-D', work)
call ig_dock_window('Result', work)
call ig_dock_window('Input', work)
call ig_dock_window('Run', run)
call ig_dock_window('Messages', run)
case ('Analysis') ! the result and the 3-D view side by side
call ig_dock_split(right, 'down', 0.35_real64, run, work)
call ig_dock_split(work, 'right', 0.45_real64, view3, left)
call ig_dock_window('Input', left) ! hidden here: a tab beside Result once shown
call ig_dock_window('Result', left)
call ig_dock_window('3-D', view3)
call ig_dock_window('Run', run)
call ig_dock_window('Messages', run)
endselect
call ig_dock_layout_end(root)
laid_out = .true.
endif
call ig_dock_space(root)FIGURE does not call the program to build a layout (no callbacks into Fortran): ig_dock_layout_begin is true on the dock space's first frame and on the frame after a switch or a reset, and the program builds app%layout(), the one the user chose. Chapter 16's Reset layout, a layout file saved at the first frame and loaded again, is gone with it: FIGURE's Reset layout forgets the user's changes to the current layout and has the program build it again.
- The user's changes are kept per layout. Dragging the 3-D tab out in Analysis, switching to Setup and back: the 3-D panel is where the user left it, and
ig_dock_layout_beginis false (nothing to build). - Messages is docked beside Run by both layouts; chapter 16's
ig_set_next_window_dockfor it is not needed.
Settings kept between runs
if (.not. remote) then ! a browser's user is not this machine's: nothing kept
call app%open_prefs('figure-studio') ! the platform's folder; the panels, the layouts, the style
call load_prefs()
endifapp%open_prefs('figure-studio') keeps the studio's preferences in the platform's folder (~/.local/share/figure-studio/ on Linux): the panels shown, the current layout and each layout's arrangement, the style chosen in View > Look and Palette, the window's place and size (Preferences). The studio adds its own values:
do k = MAX_RECENT, 1_int32, -1_int32 ! the oldest first: add_recent puts each in front
write(key, '(A,I0)') 'recent.', k
if (app%load_pref(trim(key), dir)) call add_recent(trim(dir))
enddo
if (app%load_pref('larger text', large_text)) call set_text_size()add_recent saves the recent cases with app%save_pref('recent.1', dir) and so on, Larger text its check mark; the file is written when something changed, every 30 s and at app%destroy. The test calls no open_prefs: it starts from the program's defaults, and a recording neither reads nor writes the file (Capture). In remote mode the studio keeps nothing: the browser's user is not this machine's.
The test
The test picks the layouts from the View menu as the user does, with test_menu_click, and draws the studio until it is idle with test_run (Finding items by path):
call test_menu_click('View/Analysis') ! the layout of a finished run, as the user picks it
call settle()
call check(app%layout() == 'Analysis' .and. .not. app%window_visible('Input') .and. &
app%window_visible('Messages'), 'View > Analysis: Input hidden, Messages shown beside Run')
call test_window_rect('3-D', r(1), r(2), r(3), r(4), found)
call check(found .and. r(1) > 0.6_real64 * 1100.0_real64, 'the 3-D view docked on the right of the result')
call test_menu_click('View/Input') ! a panel shown again from the menu
call settle()
call check(app%window_visible('Input'), 'View > Input shows the panel the layout hid')
call test_menu_click('View/Setup') ! back: the arrangement the user left Setup in
call settle()
call check(app%layout() == 'Setup' .and. .not. app%window_visible('Messages'), &
'View > Setup gives back the input''s layout')do while (test_run(60_int32))
call frames(1_int32)
enddo$ ./tut24_windows --test tut24-cases
[ok] the title bar holds the studio's menus
[ok] the title drags the window
[ok] ctrl+o shows the file browser on the example cases
[ok] Choose opens the case, the window's title says which
[ok] escape leaves the title as it was, unedited
[ok] enter commits the title, one undo step, and the window's title marks it unsaved
[ok] a drop asks before discarding the edit
[ok] Discard opens the dropped case
[ok] F5 runs the solver to convergence
[ok] the task reads the field the solver wrote
[ok] right moves the probe line while Result has the focus
[ok] and not while the title field is edited
[ok] nor while another panel has the focus
[ok] the run's snapshots are listed; a frame with nothing changed colours nothing
[ok] playing steps through the snapshots, each coloured once
[ok] the pointer over the monitor reads iteration 111: residual 3.91E-05, mean 20.615 C
[ok] the averaging window spans the run: its mean is the mean of every iteration
[ok] two runs in the table, sorted by iterations on a click of its header
[ok] a second click reverses the order
[ok] a right click on a run opens its menu
[ok] View > Analysis: Input hidden, Messages shown beside Run
[ok] the 3-D view docked on the right of the result
[ok] View > Input shows the panel the layout hid
[ok] View > Setup gives back the input's layout
[ok] a click on a problem shows the Body section, the radius in keys
[ok] a click on the sketch's east side chooses it
[ok] an input with the body out of the plate is refused
[ok] and accepted once corrected
[ok] larger text: one em is 19 pixels, the bar 5 em
[ok] a stray ig_end_tab is reported by the next frame, naming the call
ig_end_tab: no ig_begin_tab is open (no scope is open)
[ok] no other frame of the test had a usage error
[ok] the title bar's close button ends the studio
32 of 32 checks passedWhat you learned
app%add_window, app%window_visible and app%set_window_visible, a registry of the panels; app%add_layout and app%layout(), layouts the program builds when ig_dock_layout_begin says so; app%view_menu, the View menu's part FIGURE draws; app%open_prefs, app%save_pref and app%load_pref, the user's settings kept. Reference: Windows and layouts, Preferences.
Run it: build it as chapter 7 shows (with tut22_heat beside it), then ./tut24_windows --examples /tmp/studio --case /tmp/studio/plate.
Next: 25. The input in an editor, the help in Markdown: the input in a code editor with each mistake marked on its line, and help pages in Markdown.