Skip to content

Commit 0bf88fd

Browse files
corepunchclaude
andcommitted
Translator: two halves, title bar header, toolbar Clear and Copy
The page is the editor on top and the read-only translation below on the secondary background, splitting the height equally so typing moves nothing. The languages are the title bar's title and subtitle, and Clear and Copy are toolbar items that ask the page whether they are enabled. Framework: - A manifest app of one page has no sidebar, Go menu or page title. - <App><Toolbar>: window-wide items that run the shown page's action, dimmed when the page lacks it or its `validate` method says no. - <Page navigationTitle navigationSubtitle>: what the title bar shows. - <App background>: the color behind the iPhone keyboard's corners. - NavigationStack takes a root <Toolbar> and `subtitle` (iOS 26). - TextEditor `placeholder`, native on AppKit and UIKit; the AppKit editor honours its font and `drawsBackground`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent b499e46 commit 0bf88fd

15 files changed

Lines changed: 288 additions & 70 deletions

File tree

‎apps/translator/app.xml‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
<App name="Translator" startup="translate">
2-
<Page id="translate" title="Translate" icon="character.bubble" color="systemBlue" key="1" />
1+
<App name="Translator" startup="translate" background="secondaryBackground">
2+
<Toolbar>
3+
<ToolbarItem id="clear" label="Clear" icon="xmark.circle" placement="topBarLeading" action="clear" validate="hasText" />
4+
<ToolbarItem id="copy" label="Copy Translation" icon="doc.on.doc" placement="topBarTrailing" action="copy" validate="hasTranslation" />
5+
</Toolbar>
6+
<Page id="translate" title="Translate" navigationTitle="English → Russian" navigationSubtitle="Translates as you type" icon="character.bubble" color="systemBlue" key="1" />
37
</App>

‎apps/translator/pages/Translate.lua‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
-- The one screen: English in, Russian out as the person types. Every
22
-- keystroke is an action that translates (the service re-translates only
33
-- the line that changed); the editor keeps its caret because a page drawn
4-
-- with the text it already shows leaves it alone.
4+
-- with the text it already shows leaves it alone. Clear and Copy are the
5+
-- window's toolbar items (app.xml), which ask `hasText` and
6+
-- `hasTranslation` whether they are enabled.
57
return {
68
translate = {
79
view = "pages/Translate",
@@ -17,5 +19,7 @@ return {
1719
end,
1820
clear = function(self) self.text, self.russian, self.failure = "", "", nil end,
1921
copy = function(self) if self.russian ~= "" then self.app.clipboard.copy(self.russian) end end,
22+
hasText = function(self) return self.text ~= "" end,
23+
hasTranslation = function(self) return self.russian ~= "" end,
2024
},
2125
}

‎apps/translator/resources.xml‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
<Resources>
22
<Number id="pagePadding" value="16" />
3-
<Number id="pageSpacing" value="20" />
4-
<Number id="cardSpacing" value="8" />
5-
<Number id="editorHeight" value="140" />
6-
<Number id="captionSize" value="13" />
7-
<Number id="resultSize" value="20" />
8-
<Number id="recentSpacing" value="2" />
9-
<Number id="recentRowPadding" value="10" />
10-
<String id="sourceLanguage" value="English" />
11-
<String id="targetLanguage" value="Russian" />
3+
<Number id="textSize" value="22" />
4+
<String id="placeholder" value="Enter text" />
125
</Resources>

‎apps/translator/views/components/Result.etlua‎

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
<% --[[
2-
English in, Russian out as the person types. The editor hands every
3-
keystroke to `edit`. Clear hides rather than leaves the tree; the result
4-
card comes and goes after the editor, which is never rebuilt.
5-
The shell titles the page. ]] %>
6-
<ScrollView id="scroll">
7-
<VStack spacing="@pageSpacing" padding="@pagePadding" alignment="leading" maxWidth="infinity">
8-
<GroupBox>
9-
<VStack spacing="@cardSpacing" alignment="leading" maxWidth="infinity">
10-
<HStack alignment="center" maxWidth="infinity">
11-
<Label text="@sourceLanguage" size="@captionSize" weight="semibold" color="secondary" />
12-
<Spacer />
13-
<Button id="clear" systemImage="xmark.circle.fill" style="plain" foregroundStyle="secondary" accessibilityLabel="Clear" action="clear" hidden="<%= text == "" %>" />
14-
</HStack>
15-
<TextEditor id="english" text="<%= text %>" onChange="edit" height="@editorHeight" maxWidth="infinity" drawsBackground="false" accessibilityLabel="English text" />
16-
</VStack>
17-
</GroupBox>
18-
<% if russian ~= "" or failure then %>
19-
<%- partial("../components/Result.etlua", { russian = russian, failure = failure }) %>
2+
English in, Russian out as the person types: the window split in two
3+
equal halves that never change size, so typing moves nothing. The top
4+
half is the editor on the window's own background; the bottom half is
5+
the translation on the secondary background, in a read-only editor so it
6+
can be selected and its text starts on the same edge as the English. The
7+
languages are the title bar's title and subtitle, and Clear and Copy its
8+
items (app.xml). The editor hands every keystroke to `edit` and is never
9+
rebuilt, so it keeps its caret. ]] %>
10+
<VStack spacing="0" maxWidth="infinity" maxHeight="infinity">
11+
<VStack padding="@pagePadding" alignment="leading" maxWidth="infinity" maxHeight="infinity" flexGrow="1" flexBasis="0">
12+
<TextEditor id="english" text="<%= text %>" onChange="edit" placeholder="@placeholder" size="@textSize" design="default" wrapMode="true" drawsBackground="false" maxWidth="infinity" maxHeight="infinity" accessibilityLabel="English text" />
13+
</VStack>
14+
<Divider />
15+
<VStack padding="@pagePadding" alignment="leading" maxWidth="infinity" maxHeight="infinity" flexGrow="1" flexBasis="0" background="secondaryBackground" ignoresSafeArea="bottom">
16+
<% if failure then %>
17+
<Label id="failure" text="<%= failure %>" color="systemRed" lines="0" maxWidth="infinity" />
18+
<% else %>
19+
<TextEditor id="russian" text="<%= russian %>" editable="false" size="@textSize" design="default" wrapMode="true" drawsBackground="false" maxWidth="infinity" maxHeight="infinity" accessibilityLabel="Russian translation" />
2020
<% end %>
2121
</VStack>
22-
</ScrollView>
22+
</VStack>

‎lua/data/app.lua‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
-- --page=<id> starts on that page instead of the manifest's startup page
1414
-- --isolated shows that page alone: no sidebar and no menu
1515
--
16+
-- An app of one page is always shown alone, as with --isolated: there is
17+
-- nothing to navigate to, so no sidebar, menu or navigation title.
18+
--
1619
-- That is "playable from every scene": a page asks its models for what it
1720
-- shows, so it runs on its own.
1821
local ns = require("ns")
@@ -143,20 +146,36 @@ function Launcher:select(id)
143146
end
144147

145148
-- The Go menu's actions: `page_<id>` shows a page, `isPage_<id>` validates.
149+
-- A toolbar item's `tool_<action>` runs that action of the page being shown
150+
-- and `canTool_<action>` says whether it is enabled: the page has the action
151+
-- and its `validate` method, if the item names one, agrees.
146152
function Launcher:actions()
147153
local actions = {}
148154
for _, page in ipairs(self.manifest.order) do
149155
actions["page_" .. page.id] = function() self:show(page.id) end
150156
actions["isPage_" .. page.id] = function() return true, self.current == page.id end
151157
end
158+
for _, item in ipairs(self.manifest.toolbar) do
159+
local name, validate = item.action, item.validate
160+
actions["tool_" .. name] = function()
161+
local action = self.page and self.page.actions[name]
162+
if action then return action() end
163+
end
164+
actions["canTool_" .. name] = function()
165+
local request = self.page and self.page.request
166+
if type(request) ~= "table" or type(request[name]) ~= "function" then return false end
167+
if validate then return type(request[validate]) == "function" and request[validate](request) and true or false end
168+
return true
169+
end
170+
end
152171
return actions
153172
end
154173

155174
function Launcher:createWindow()
156-
local isolated = self.options.isolated == true
175+
local isolated = self.options.isolated == true or #self.manifest.order == 1
157176
local config, refs = xml.renderFile(SHELL, {
158177
app = self.manifest, isolated = isolated, actions = self:actions(),
159-
platform = ns.platform, title = self.manifest.pages[self.startup].title,
178+
platform = ns.platform, page = self.manifest.pages[self.startup],
160179
}, ns)
161180
self.content = refs.content
162181
if ns.platform == "AppKit" and not isolated then

‎lua/data/manifest.lua‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,24 @@
1212
-- A page is drawn by its route (lua/data/routes.lua): `route` names it, and
1313
-- the page id is the route's name when it is omitted. Every other attribute
1414
-- is kept in `page.attrs`, the route's `self.params` (`workflow="music"`).
15+
-- `navigationTitle` and `navigationSubtitle` are what the title bar shows
16+
-- for the page, as SwiftUI's `.navigationTitle` and `.navigationSubtitle`
17+
-- (`title` names the page in the sidebar and the Go menu);
1518
-- `sidebar="Dev tools"` is a shorter name for the sidebar row;
1619
-- `listed="false"` keeps a page out of the sidebar and the Go menu (a page
1720
-- opened from elsewhere). `arg="topic"` names the param a page's location
1821
-- carries in its path, `/help/shortcuts` (lua/data/location.lua).
1922
--
23+
-- A `<Toolbar>` of `<ToolbarItem id label icon action validate placement>`
24+
-- holds the window-wide actions (the window toolbar on macOS, the navigation
25+
-- bar on iPhone). An item calls `action` on the page being shown and is
26+
-- dimmed on a page that has no such method; `validate` names a page method
27+
-- that says whether it is enabled now (`canCopy`).
28+
--
29+
-- `<App background="secondaryBackground">` is the color behind the pages
30+
-- where they do not reach: on iPhone a page ends at the keyboard's top, and
31+
-- this color fills behind the keyboard's rounded corners.
32+
--
2033
-- `<App routes="pages">` names the module of the app's routes, a module path
2134
-- inside the app; it is `routes` when omitted. `<App controller="Controller">`
2235
-- names a class of the app that coordinates the whole window -- a root
@@ -36,7 +49,8 @@ function Manifest.parse(nodes)
3649
end
3750
if not root then fail("no <App> element") end
3851
local manifest = { name = root.attrs.name or "App", startup = root.attrs.startup, controller = root.attrs.controller,
39-
routes = root.attrs.routes or "routes", sections = {}, pages = {}, order = {} }
52+
routes = root.attrs.routes or "routes", sections = {}, pages = {}, order = {}, toolbar = {},
53+
background = root.attrs.background }
4054
local function page(node, section)
4155
local attrs = node.attrs
4256
for _, key in ipairs({ "id", "title" }) do
@@ -62,6 +76,16 @@ function Manifest.parse(nodes)
6276
page(child, current)
6377
end
6478
end
79+
elseif node.tag == "Toolbar" then
80+
for _, child in ipairs(node.children) do
81+
if child.kind == "element" then
82+
if child.tag ~= "ToolbarItem" then fail("<" .. child.tag .. "> inside <Toolbar>; only <ToolbarItem> belongs there") end
83+
for _, key in ipairs({ "id", "action" }) do
84+
if not child.attrs[key] or child.attrs[key] == "" then fail("<ToolbarItem> needs " .. key) end
85+
end
86+
table.insert(manifest.toolbar, child.attrs)
87+
end
88+
end
6589
elseif node.tag == "Page" then
6690
-- Pages before the first section lead the sidebar without a header.
6791
if #manifest.sections > 1 and current.title then
@@ -70,7 +94,7 @@ function Manifest.parse(nodes)
7094
end
7195
page(node, current)
7296
else
73-
fail("<" .. node.tag .. "> is not part of the manifest; use Section or Page")
97+
fail("<" .. node.tag .. "> is not part of the manifest; use Section, Page or Toolbar")
7498
end
7599
end
76100
end

‎lua/data/views/Shell.etlua‎

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
11
<% --[[
22
The window every manifest app gets: a Go menu with one item per page (the
3-
keys come from the manifest), a sidebar toggle and the content area the
4-
generic page controller mounts pages into. `--isolated` leaves only the
5-
content. The sidebar itself is Sidebar.etlua. On iPhone there is no
6-
sidebar or menu: the page sits in a navigation stack titled by it, as a
7-
SwiftUI page sits in NavigationStack, and a page view never makes its own
8-
(a navigation controller cannot be placed inside a stack). ]] %>
9-
<Window title="<%= app.name %>" width="1100" height="720" minWidth="700" minHeight="440" sidebarWidth="226">
3+
keys come from the manifest), a sidebar toggle, the manifest's toolbar
4+
items and the content area the generic page controller mounts pages into.
5+
`--isolated`, and an app of one page, leave out the menu, the sidebar and
6+
the page title. The sidebar itself is Sidebar.etlua. On iPhone there is
7+
no sidebar or menu: the page sits in a navigation stack titled by it, as a
8+
SwiftUI page sits in NavigationStack, with the toolbar items in its bar;
9+
an untitled page without items hides the bar. A page's
10+
`navigationTitle` and `navigationSubtitle` go to the title bar: the
11+
window's title and subtitle on macOS, the navigation bar on iPhone. The
12+
manifest's `background` fills behind the iPhone keyboard, where the page
13+
ends; the page itself keeps the system background. A page
14+
view never makes its own stack (a navigation controller cannot be placed
15+
inside a stack). ]] %>
16+
<% local heading = page.attrs.navigationTitle or (isolated and "" or page.title) %>
17+
<% local subheading = page.attrs.navigationSubtitle or "" %>
18+
<% local function items() %>
19+
<% for _, item in ipairs(app.toolbar) do %>
20+
<ToolbarItem id="<%= item.id %>" label="<%= item.label or "" %>" icon="<%= item.icon or "" %>" tooltip="<%= item.label or "" %>" placement="<%= item.placement or "automatic" %>" action="tool_<%= item.action %>" validate="canTool_<%= item.action %>" />
21+
<% end %>
22+
<% end %>
23+
<Window title="<%= page.attrs.navigationTitle or app.name %>" subtitle="<%= subheading %>"<% if page.attrs.navigationTitle then %> hideTitle="false"<% end %> width="1100" height="720" minWidth="700" minHeight="440" sidebarWidth="226">
1024
<% if not isolated and platform ~= "UIKit" then %>
1125
<Commands appName="<%= app.name %>">
1226
<CommandMenu title="Go">
@@ -18,13 +32,26 @@
1832
<% end %>
1933
</CommandMenu>
2034
</Commands>
35+
<% end %>
36+
<% if platform ~= "UIKit" and (not isolated or #app.toolbar > 0) then %>
2137
<Toolbar>
38+
<% if not isolated then %>
2239
<ToolbarItem id="toggleSidebar" />
40+
<% end %>
41+
<% if #app.toolbar > 0 then %>
42+
<ToolbarSpacer />
43+
<% items() %>
44+
<% end %>
2345
</Toolbar>
2446
<% end %>
2547
<% if platform == "UIKit" then %>
26-
<NavigationStack title="<%= title %>" largeTitle="true">
27-
<VStack id="content" maxWidth="infinity" maxHeight="infinity" />
48+
<NavigationStack title="<%= heading %>" subtitle="<%= subheading %>"<% if app.background then %> background="<%= app.background %>"<% end %> largeTitle="<%= not isolated %>" hidesNavigationBar="<%= heading == "" and #app.toolbar == 0 %>">
49+
<% if #app.toolbar > 0 then %>
50+
<Toolbar>
51+
<% items() %>
52+
</Toolbar>
53+
<% end %>
54+
<VStack id="content" maxWidth="infinity" maxHeight="infinity"<% if app.background then %> background="background"<% end %> />
2855
</NavigationStack>
2956
<% else %>
3057
<VStack id="content" maxWidth="infinity" maxHeight="infinity" />

‎lua/embedded/AppKit.lua‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1230,6 +1230,7 @@ end
12301230
--- @prop drawsBackground boolean optional. Draws the control’s background when true.
12311231
--- @prop editable boolean optional. Allows text editing when true.
12321232
--- @prop onChange function optional. Receives the text after each edit.
1233+
--- @prop placeholder string optional. Prompt shown in the system placeholder color while the editor is empty.
12331234
--- @prop language string optional. Component-specific setting passed to the native control.
12341235
--- @prop selectable boolean optional. Allows text or rows to be selected when true.
12351236
--- @prop size number optional. Component-specific setting passed to the native control.
@@ -1256,12 +1257,14 @@ function AppKit.TextEditor(props)
12561257
end
12571258
if props.drawsBackground ~= nil then
12581259
textView.drawsBackground = props.drawsBackground ~= false
1260+
view.drawsBackground = props.drawsBackground ~= false
12591261
end
12601262
if props.wrapMode ~= nil then
12611263
view.hasHorizontalScroller = not props.wrapMode
12621264
end
12631265
-- SwiftUI `TextEditor(text:)`: each edit hands the new text to the page.
12641266
if props.onChange then view:onChange(props.onChange) end
1267+
if props.placeholder then textView.placeholder = props.placeholder end
12651268
if props.accessibilityLabel then textView.accessibilityLabel = props.accessibilityLabel end
12661269
return applyLayout(view, props)
12671270
end
@@ -2523,11 +2526,17 @@ local navScreenScopes = setmetatable({}, { __mode = "k" })
25232526
--- @prop content value optional. Rendered child content or the control’s text value.
25242527
--- @prop path table optional. A `ui.navigation`.Path value path.
25252528
--- @prop destinations table optional. Map path types to destination view builders.
2529+
--- @prop toolbar table optional. ToolbarItem records for the root view, placed as on a `Page`.
25262530
--- @prop title value optional. Component-specific setting passed to the native control.
25272531
--- @platform AppKit uses the AppKit implementation. UIKit uses the UIKit implementation.
25282532
function AppKit.NavigationStack(props)
25292533
props = props or {}
2530-
local root = AppKit.HostingController(props.content or props[1])
2534+
-- The root's toolbar items, as a pushed Page's, go to the window toolbar.
2535+
local toolbar = {}
2536+
for _, item in ipairs(props.toolbar or {}) do
2537+
if item.placement ~= "keyboard" then table.insert(toolbar, item) end
2538+
end
2539+
local root = bridge._hostingController(props.content or props[1], nil, toolbar)
25312540
root.title = props.title or ""
25322541
local host = applyLayout(bridge._navigationStack(root), props)
25332542
navScreenScopes[host] = {}

‎lua/embedded/UIKit.lua‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ local sheetScopes = {}
237237
--- @prop hidesNavigationBar boolean optional. Hides the navigation bar when true.
238238
--- @prop hidesTabBar boolean optional. Hides the tab bar when true.
239239
--- @prop largeTitle boolean optional. Uses the large navigation title style when true.
240+
--- @prop toolbar table optional. ToolbarItem records for the root view, placed as on a `Page`.
241+
--- @prop subtitle string optional. Second line under the root's title (SwiftUI `.navigationSubtitle`).
242+
--- @prop background string optional. Color behind the root view where it does not reach, such as behind the keyboard.
240243
--- @prop title value optional. Component-specific setting passed to the native control.
241244
--- @platform AppKit uses the AppKit implementation. UIKit uses the UIKit implementation.
242245
function UIKit.NavigationStack(props)
@@ -245,6 +248,12 @@ function UIKit.NavigationStack(props)
245248
local root = asViewController(content)
246249
local navigation = bridge._navigationStack(root, props.hidesNavigationBar == true)
247250
if props.title then root.title = props.title end
251+
-- SwiftUI `.navigationSubtitle`: a second line under the title (iOS 26).
252+
if props.subtitle and props.subtitle ~= "" then root.navigationItem.subtitle = props.subtitle end
253+
if props.toolbar then bridge._pageToolbar(root, props.toolbar) end
254+
-- What shows where the root does not reach: the root ends at the
255+
-- keyboard's top, so this color fills behind the keyboard's corners.
256+
if props.background then root.view.backgroundColor = bridge._systemColor(props.background) end
248257
if props.largeTitle ~= nil then
249258
navigation.navigationBar.prefersLargeTitles = props.largeTitle
250259
end
@@ -754,6 +763,7 @@ end
754763
--- @prop editable boolean optional. Allows text editing when true.
755764
--- @prop italic boolean optional. Component-specific setting passed to the native control.
756765
--- @prop onChange function optional. Receives the text after each edit.
766+
--- @prop placeholder string optional. Prompt shown in the system placeholder color while the editor is empty.
757767
--- @prop selectable boolean optional. Allows text or rows to be selected when true.
758768
--- @prop size number optional. Component-specific setting passed to the native control.
759769
--- @prop text string optional. Initial or displayed text value.
@@ -774,6 +784,7 @@ function UIKit.TextEditor(props)
774784
end
775785
-- SwiftUI `TextEditor(text:)`: each edit hands the new text to the page.
776786
if props.onChange then bridge._textEditorCallbacks(v, props.onChange) end
787+
if props.placeholder then v.placeholder = props.placeholder end
777788
if props.accessibilityLabel then v.accessibilityLabel = props.accessibilityLabel end
778789
return applyLayout(v, props)
779790
end

0 commit comments

Comments
 (0)