Search the widget picker by widget name or the app it comes from

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KNP1cMweg13VdKrtriNJ5C
This commit is contained in:
Claude committed 2026-10-21 23:43:14 +00:00
1 parent a387ff9708
commit d8b51496ae
6 files changed
+97 -18

No files matched your search

@@ -11,6 +11,7 @@ import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.LauncherActivityInfo;
import android.content.pm.LauncherApps;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
@@ -68,7 +69,7 @@ import java.util.concurrent.Executors;
*
* apps.json {"apps": [{"label", "component", "icon"}]}, every launchable activity
* icon?c=COMP the activity's icon as a PNG
* widgets.json {"widgets": [{"label", "provider", "icon", "h"}]}, every widget provider
* widgets.json {"widgets": [{"label", "app", "provider", "icon", "h"}]}, every widget provider
* wicon?p=PROV the provider's icon as a PNG
*
* and exposes window.Android (launch, appInfo, addWidget, removeWidget, placeWidgets,
@@ -352,6 +353,7 @@ public class MainActivity extends Activity {
int h = Math.round(w.minHeight / d);
out.put(new JSONObject()
.put("label", labels.get(w))
.put("app", appLabel(w.provider.getPackageName()))
.put("provider", p)
.put("icon", "wicon?p=" + Uri.encode(p))
.put("h", Math.max(80, Math.min(h + 16, 420))));
@@ -359,6 +361,16 @@ public class MainActivity extends Activity {
return new JSONObject().put("widgets", out).toString();
}
/** The label of the app a widget comes from, for the picker's search. */
private String appLabel(String pkg) {
try {
PackageManager pm = getPackageManager();
return pm.getApplicationLabel(pm.getApplicationInfo(pkg, 0)).toString();
} catch (PackageManager.NameNotFoundException e) {
return "";
}
}
private AppWidgetProviderInfo provider(String p) {
ComponentName cn = p == null ? null : ComponentName.unflattenFromString(p);
if (cn == null) return null;
+9
View File
@@ -34,6 +34,15 @@ const BASE = process.env.BASE || "http://localhost:8000";
await longpress(".clock");
await p.click(".sheet-item >> text=Add widget");
await p.waitForSelector(".widget-list li");
// search the picker by widget name and by the app it comes from
const widgetNames = () => p.$$eval(".widget-list .widget-name", (ns) => ns.map((n) => n.firstChild.textContent));
await p.fill(".widget-search", "cal"); await settle();
assert.deepEqual(await widgetNames(), ["Calendar"]);
await p.fill(".widget-search", "goo"); await settle();
assert.deepEqual(await widgetNames(), ["Weather"]);
await p.fill(".widget-search", "zzz"); await settle();
assert.equal(await p.textContent(".sheet .empty"), "No widgets match.");
await p.fill(".widget-search", ""); await settle();
await p.click(".widget-list li:first-child"); await settle();
assert.equal(await p.getAttribute(".widget-slot", "data-wid"), "100");
assert.equal(await p.textContent(".widget-label"), "Clock");
+67 -16
View File
@@ -97,6 +97,9 @@ RECENTS: Const[i32] = 33
LIFT: Const[i32] = 34 # arg: home slot (long press picks it up)
DRAG_TO: Const[i32] = 35 # arg: home slot the lifted one is dragged over
DROP: Const[i32] = 36 # arg: 1 if it was dragged, 0 if just held (opens its menu)
WQUERY: Const[i32] = 37 # widget picker search
WCLEAR: Const[i32] = 38
CHOOSE_FIRST_WIDGET: Const[i32] = 39
CAP: Const[i32] = 512
HOME_CAP: Const[i32] = 40
@@ -134,12 +137,14 @@ next_key: i32 = 1
wlabels: i32[512] = empty(512, dtype=int32)
wprovs: i32[512] = empty(512, dtype=int32)
wicons: i32[512] = empty(512, dtype=int32)
wapps: i32[512] = empty(512, dtype=int32) # label of the app the widget comes from
wheights: i32[512] = empty(512, dtype=int32)
nw: i32 = 0
wloading: i32 = 0
chosen: i32 = -1 # widget row being added
query: i32 = 0
wquery: i32 = 0 # widget picker search
now: i32 = 0
today: i32 = 0
err: i32 = 0
@@ -156,11 +161,12 @@ pending: i32 = 0 # JSON handle of a request in flight
def init() -> None:
global query, now, today, err, notice, scratch, nh, next_key, pending
global query, wquery, now, today, err, notice, scratch, nh, next_key, pending
i: i32
h: i32
k: i32
query = s_new()
wquery = s_new()
now = s_new()
today = s_new()
err = s_new()
@@ -258,6 +264,7 @@ def load_widgets(doc: i32) -> None:
s_free(wlabels[i])
s_free(wprovs[i])
s_free(wicons[i])
s_free(wapps[i])
nw = 0
if j_ok(doc) == 0:
j_free(doc)
@@ -271,9 +278,11 @@ def load_widgets(doc: i32) -> None:
wlabels[nw] = s_new()
wprovs[nw] = s_new()
wicons[nw] = s_new()
wapps[nw] = s_new()
ok = j_str(row, "label", wlabels[nw])
ok = j_str(row, "provider", wprovs[nw])
ok = j_str(row, "icon", wicons[nw])
ok = j_str(row, "app", wapps[nw])
wheights[nw] = j_int(row, "h")
j_free(row)
nw += 1
@@ -303,6 +312,18 @@ def matches(i: i32) -> bool:
return s_has(labels[i], query) == 1
def widget_matches(i: i32) -> bool:
return s_has(wlabels[i], wquery) == 1 or s_has(wapps[i], wquery) == 1
def first_widget_match() -> i32:
i: i32
for i in range(nw):
if widget_matches(i):
return i
return -1
def first_match() -> i32:
i: i32
for i in range(n):
@@ -410,6 +431,10 @@ def update(msg: i32, arg: i32) -> None:
s_copy(query, arg)
elif msg == CLEAR:
s_clear(query)
elif msg == WQUERY:
s_copy(wquery, arg)
elif msg == WCLEAR:
s_clear(wquery)
elif msg == OPEN_DRAWER or msg == SWIPE_DOWN:
if sheet == NONE:
drawer = 1
@@ -466,15 +491,20 @@ def update(msg: i32, arg: i32) -> None:
picking = 1
elif msg == PICK_WIDGET:
sheet = SHEET_WIDGETS
s_clear(wquery)
wloading = 1
pending = http_get("widgets.json", WIDGETS_LOADED)
elif msg == CHOOSE_WIDGET:
if nh < HOME_CAP:
chosen = arg
add_widget(wprovs[arg])
else:
say("The home screen is full")
sheet = NONE
elif msg == CHOOSE_WIDGET or msg == CHOOSE_FIRST_WIDGET:
i = arg
if msg == CHOOSE_FIRST_WIDGET:
i = first_widget_match()
if i >= 0:
if nh < HOME_CAP:
chosen = i
add_widget(wprovs[i])
else:
say("The home screen is full")
sheet = NONE
elif msg == WIDGET_ADDED:
if chosen >= 0 and nh < HOME_CAP:
push_slot(WIDGET, s_dup(wlabels[chosen]), arg, wheights[chosen])
@@ -702,6 +732,7 @@ def sheet_btn(s: str, msg: i32, arg: i32) -> None:
def view_sheet() -> None:
i: i32
row: i32
shown: i32 = 0
el("div")
cls("sheet-overlay")
on_click(CLOSE_SHEET, 0)
@@ -754,18 +785,38 @@ def view_sheet() -> None:
cls("empty")
text("No widgets installed.")
end()
else:
input_open("Search widgets", wquery, WQUERY)
cls("widget-search")
attr("type", "search")
attr("enterkeyhint", "go")
on_enter(CHOOSE_FIRST_WIDGET, 0)
on_escape(WCLEAR, 0)
end()
el("ul")
cls("widget-list")
for i in range(nw):
el("li")
key(i + 1)
on_click(CHOOSE_WIDGET, i)
view_icon(wicons[i])
el("span")
text_ref(wlabels[i])
end()
end()
if widget_matches(i):
shown += 1
el("li")
key(i + 1)
on_click(CHOOSE_WIDGET, i)
view_icon(wicons[i])
el("span")
cls("widget-name")
text_ref(wlabels[i])
if s_len(wapps[i]) > 0 and s_eq(wapps[i], wlabels[i]) == 0:
el("small")
text_ref(wapps[i])
end()
end()
end()
end()
if wloading == 0 and nw > 0 and shown == 0:
el("p")
cls("empty")
text("No widgets match.")
end()
end()
BIN
View File
Binary file not shown.
+3
View File
@@ -111,6 +111,9 @@ body { margin: 0; min-height: 100vh; color: #fff; -webkit-tap-highlight-color: t
cursor: pointer; }
.widget-list li:hover, .widget-list li:active { background: var(--accent); }
.widget-list .icon { width: 2.5rem; height: 2.5rem; filter: none; }
.widget-name { display: grid; }
.widget-name small { color: var(--muted-foreground); font-size: .8rem; }
.widget-search { width: 100%; margin-bottom: .5rem; }
.notice, #toast { position: fixed; left: 50%; bottom: calc(var(--bottom) + 2rem); z-index: 30;
transform: translateX(-50%); padding: .5rem 1rem; border-radius: 999px;
+5 -1
View File
@@ -2,27 +2,31 @@
"widgets": [
{
"label": "Clock",
"app": "Clock",
"provider": "com.example.clock/.Widget",
"icon": "data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20viewBox%3D%220%200%2048%2048%22%3E%3Crect%20width%3D%2248%22%20height%3D%2248%22%20rx%3D%2212%22%20fill%3D%22%230ea5e9%22/%3E%3Crect%20x%3D%2210%22%20y%3D%2214%22%20width%3D%2228%22%20height%3D%2220%22%20rx%3D%224%22%20fill%3D%22%23fff%22%20opacity%3D%22.85%22/%3E%3C/svg%3E",
"h": 140
},
{
"label": "Weather",
"app": "Google",
"provider": "com.example.weather/.Widget",
"icon": "data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20viewBox%3D%220%200%2048%2048%22%3E%3Crect%20width%3D%2248%22%20height%3D%2248%22%20rx%3D%2212%22%20fill%3D%22%2338bdf8%22/%3E%3Crect%20x%3D%2210%22%20y%3D%2214%22%20width%3D%2228%22%20height%3D%2220%22%20rx%3D%224%22%20fill%3D%22%23fff%22%20opacity%3D%22.85%22/%3E%3C/svg%3E",
"h": 120
},
{
"label": "Calendar",
"app": "Calendar",
"provider": "com.example.calendar/.Widget",
"icon": "data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20viewBox%3D%220%200%2048%2048%22%3E%3Crect%20width%3D%2248%22%20height%3D%2248%22%20rx%3D%2212%22%20fill%3D%22%233b82f6%22/%3E%3Crect%20x%3D%2210%22%20y%3D%2214%22%20width%3D%2228%22%20height%3D%2220%22%20rx%3D%224%22%20fill%3D%22%23fff%22%20opacity%3D%22.85%22/%3E%3C/svg%3E",
"h": 200
},
{
"label": "Music",
"app": "Music",
"provider": "com.example.music/.Widget",
"icon": "data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20viewBox%3D%220%200%2048%2048%22%3E%3Crect%20width%3D%2248%22%20height%3D%2248%22%20rx%3D%2212%22%20fill%3D%22%23ec4899%22/%3E%3Crect%20x%3D%2210%22%20y%3D%2214%22%20width%3D%2228%22%20height%3D%2220%22%20rx%3D%224%22%20fill%3D%22%23fff%22%20opacity%3D%22.85%22/%3E%3C/svg%3E",
"h": 110
}
]
}
}